symfonic.core.kimi_credentials¶
kimi_credentials ¶
Kimi credential store — auto-load + auto-refresh of the kimi-cli token.
DEV/TEST USE ONLY. Reads the plaintext OAuth credential file written by a
kimi-cli login (~/.kimi-code/credentials/kimi-code.json) and yields an
always-valid short-lived access token, refreshing transparently against
auth.kimi.com when the cached token is within expiry_skew_seconds of
expiry.
Side effect (deliberate and necessary)¶
The kimi-code OAuth refresh_token ROTATES: each refresh returns a NEW
refresh_token and INVALIDATES the previous one. To keep the developer's
kimi-cli login working, the rotated token MUST be written back to the
credential file. This module therefore mutates the local kimi-cli credential
file on refresh. The write is atomic (temp file + os.replace) and guarded
by an exclusive file lock so a concurrent kimi-cli refresh cannot corrupt
the file or race the rotation.
Tokens are NEVER logged; only a short non-leaking fingerprint is emitted.
KimiCredentialError ¶
Bases: RuntimeError
Raised when a Kimi subscription token cannot be resolved or refreshed.
KimiCredentialStore ¶
KimiCredentialStore(
credentials_path: Path | None = None,
*,
client_id: str = _KIMI_CODE_PUBLIC_CLIENT_ID,
token_url: str = _DEFAULT_TOKEN_URL,
expiry_skew_seconds: int = 120,
)
Yields an always-valid kimi-cli access token, refreshing as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials_path
|
Path | None
|
Override the credential file. Defaults to the
|
None
|
client_id
|
str
|
OAuth device-flow client id. Defaults to the kimi-code CLI's public client id. |
_KIMI_CODE_PUBLIC_CLIENT_ID
|
token_url
|
str
|
OAuth token endpoint. Defaults to |
_DEFAULT_TOKEN_URL
|
expiry_skew_seconds
|
int
|
Refresh this many seconds BEFORE the cached
token's |
120
|
Source code in src/symfonic/core/kimi_credentials.py
access_token ¶
Return a valid access token, refreshing if within the skew window.
peek_access_token ¶
Return the cached token without refreshing; None if unavailable.
For logging / fingerprinting at construction time — must never raise or trigger a network refresh.
Source code in src/symfonic/core/kimi_credentials.py
default_credentials_path ¶
Resolve the kimi-cli credential file path (env override -> default).