-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Improve China Kimi/GLM routes: Coding Plan 5h, Kimi Monthly, clearer labels #2351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Leehow
wants to merge
4
commits into
steipete:main
Choose a base branch
from
Leehow:local/cn-route-ux
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
26e0f32
Clarify Kimi Code vs China open-platform (Moonshot) and GLM regions.
c54a8cc
Show Kimi Monthly from Desktop session; date-only resets; clearer GLM…
51f4c2e
Treat GLM Coding Plan as 5-hour primary and auto-load local key.
56e6225
Document GLM Coding Plan 5-hour mapping and Kimi Monthly enrichment.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
Sources/CodexBarCore/Providers/Kimi/KimiDesktopAuthToken.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import Foundation | ||
|
|
||
| #if canImport(SQLite3) | ||
| import SQLite3 | ||
| #elseif canImport(CSQLite3) | ||
| import CSQLite3 | ||
| #endif | ||
|
|
||
| #if canImport(FoundationNetworking) | ||
| import FoundationNetworking | ||
| #endif | ||
|
|
||
| /// Reads the `kimi-auth` web session token from the official Kimi Desktop Electron app. | ||
| /// | ||
| /// Kimi Code CLI tokens can fetch weekly/rate-limit usage from `api.kimi.com`, but the | ||
| /// **Monthly** subscription pool still comes from the web membership API and needs a | ||
| /// `kimi-auth` cookie. Users who only run the CLI (no browser login) often still have | ||
| /// Kimi Desktop signed in — its Cookies DB stores `kimi-auth` in plaintext. | ||
| public enum KimiDesktopAuthToken: Sendable { | ||
| private static let log = CodexBarLog.logger(LogCategories.kimiCookie) | ||
|
|
||
| public static func cookiesDatabaseURL( | ||
| homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser) -> URL | ||
| { | ||
| homeDirectory | ||
| .appendingPathComponent("Library", isDirectory: true) | ||
| .appendingPathComponent("Application Support", isDirectory: true) | ||
| .appendingPathComponent("kimi-desktop", isDirectory: true) | ||
| .appendingPathComponent("Cookies", isDirectory: false) | ||
| } | ||
|
|
||
| /// Returns a non-empty `kimi-auth` value, or nil when missing/unreadable. | ||
| public static func load( | ||
| homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser) -> String? | ||
| { | ||
| #if os(macOS) | ||
| let dbURL = self.cookiesDatabaseURL(homeDirectory: homeDirectory) | ||
| guard FileManager.default.isReadableFile(atPath: dbURL.path) else { return nil } | ||
|
|
||
| // Chrome/Electron may hold a write lock; copy to a temp file first. | ||
| let tempURL = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent("codexbar-kimi-desktop-cookies-\(UUID().uuidString).db") | ||
| do { | ||
| try FileManager.default.copyItem(at: dbURL, to: tempURL) | ||
| } catch { | ||
| Self.log.debug("Kimi Desktop Cookies copy failed: \(error.localizedDescription)") | ||
| return nil | ||
| } | ||
| defer { try? FileManager.default.removeItem(at: tempURL) } | ||
|
|
||
| guard let token = self.readKimiAuth(fromSQLitePath: tempURL.path) else { return nil } | ||
| let trimmed = token.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| return trimmed.isEmpty ? nil : trimmed | ||
| #else | ||
| return nil | ||
| #endif | ||
| } | ||
|
|
||
| private static func readKimiAuth(fromSQLitePath path: String) -> String? { | ||
| // Minimal SQLite3 open without a package dependency — Cookies is a standard Chromium DB. | ||
| var db: OpaquePointer? | ||
| guard sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, nil) == SQLITE_OK, let db else { | ||
| return nil | ||
| } | ||
| defer { sqlite3_close(db) } | ||
|
|
||
| let sql = """ | ||
| SELECT value, length(encrypted_value) | ||
| FROM cookies | ||
| WHERE name = 'kimi-auth' | ||
| AND (host_key = 'www.kimi.com' OR host_key = '.www.kimi.com' OR host_key = '.kimi.com' OR host_key = 'kimi.com') | ||
| ORDER BY last_access_utc DESC | ||
| LIMIT 1; | ||
| """ | ||
| var statement: OpaquePointer? | ||
| guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK, let statement else { | ||
| return nil | ||
| } | ||
| defer { sqlite3_finalize(statement) } | ||
|
|
||
| guard sqlite3_step(statement) == SQLITE_ROW else { return nil } | ||
| if let cString = sqlite3_column_text(statement, 0) { | ||
| let value = String(cString: cString) | ||
| if !value.isEmpty { return value } | ||
| } | ||
| // Encrypted-only rows need Keychain AES — leave those to BrowserCookieClient. | ||
| return nil | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Kimi Desktop has its Chromium Cookies database open in WAL mode, recent cookie writes live in
Cookies-walrather than the mainCookiesfile. Copying only the main database and opening the isolated copy can therefore return a stale token or nokimi-authrow, so Desktop-only users miss the new monthly enrichment. Use SQLite's backup API or copy the WAL/SHM sidecars consistently before opening the temporary database.Useful? React with 👍 / 👎.