Moonshot: auto-discover API key from Kimi CLI config#793
Moonshot: auto-discover API key from Kimi CLI config#793maxitoon wants to merge 3 commits intosteipete:mainfrom
Conversation
The Kimi CLI stores its Moonshot API key in ~/.kimi/config.toml under the [providers."managed:moonshot-ai"] section. This change lets the Moonshot provider fall back to that file when MOONSHOT_API_KEY / MOONSHOT_KEY environment variables are not set. This removes a common foot-gun for Kimi CLI users who enable the Kimi provider in CodexBar (which only supports web cookies) and don't realize their K2.6 usage is actually served by the Moonshot API. - Add parseKimiConfigAPIKey() to extract api_key from the TOML section - Add loadKimiConfigContents() to read ~/.kimi/config.toml - Extend apiKey() to fall back: env vars → Kimi CLI config → nil - Add 7 unit tests for section boundaries, quotes, comments, etc.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 74e8a3494f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| guard let apiKey = Self.resolveToken(environment: context.env) else { | ||
| throw MoonshotUsageError.missingCredentials | ||
| } | ||
| let region = context.settings?.moonshot?.region ?? MoonshotSettingsReader.region(environment: context.env) |
There was a problem hiding this comment.
Honor MOONSHOT_REGION when region config is unset
fetch currently resolves region with context.settings?.moonshot?.region ?? MoonshotSettingsReader.region(environment: context.env), but in both runtime paths we always supply a Moonshot settings snapshot (MoonshotProviderImplementation.settingsSnapshot in app and TokenAccountCLIContext.settingsSnapshot in CLI) and both default missing config to .international, so the right-hand MOONSHOT_REGION fallback is effectively unreachable. This means users who rely on env-only region selection will still call api.moonshot.ai even when MOONSHOT_REGION=china, which can break China-region accounts and contradicts the documented config/env precedence.
Useful? React with 👍 / 👎.
|
cc @steipete — this PR builds on top of #705 (Moonshot provider) and adds auto-discovery of the Moonshot API key from the Kimi CLI config file ( Why this matters: Kimi CLI users (like myself) naturally enable the "Kimi" provider in CodexBar, but that only supports web cookies from kimi.com. Their actual API key lives in Tested: Built and running locally — the Moonshot provider now fetches usage automatically from my Kimi CLI config. ✅ |
Power users often have 4–5 active providers (Codex, Claude, Moonshot, OpenRouter, Cursor). The previous limit of 3 forced switching in the provider picker. All UI logic already derives from the constant, so this is a safe single-line change.
|
I tested this locally and these were the concrete UI changes that worked well for me: For the descriptor, Moonshot can reuse the existing Kimi branding: iconStyle: .kimi,
iconResourceName: "ProviderIcon-kimi",For the menu bar label, I special-cased Moonshot to display the balance amount from the identity text: if provider == .moonshot,
let balance = Self.moonshotBalanceDisplayText(snapshot: snapshot)
{
return balance
}where For the icon alignment, I adjusted the shared <path transform="translate(0 -0.8)" ... />That made the Kimi glyph sit closer to the optical center of the 16px menu bar icon slot, and improves alignment for Kimi, Kimi K2, and Moonshot since they share the same asset. |
Changes
1. Moonshot: auto-discover API key from Kimi CLI config
Users of the Kimi CLI (which uses Moonshot AI models like
kimi-k2.6) often enable the Kimi provider in CodexBar. That provider only supports web cookies from kimi.com, so terminal-only users see "No available fetch strategy for kimi."Their API key is actually stored in
~/.kimi/config.tomlunder[providers."managed:moonshot-ai"], but CodexBar never looked there.Solution: Extend
MoonshotSettingsReader.apiKey()to fall back to~/.kimi/config.tomlwhenMOONSHOT_API_KEY/MOONSHOT_KEYenv vars are absent.parseKimiConfigAPIKey()— simple TOML-line parser that respects section boundaries, strips quotes, and ignores commentsloadKimiConfigContents()— reads~/.kimi/config.toml2. Increase Overview tab provider limit from 3 → 5
Power users often have 4–5 active providers (Codex, Claude, Moonshot, OpenRouter, Cursor). The previous limit of 3 forced switching in the provider picker. All UI logic already derives from the constant, so this is a safe single-line change.
Verification
swift build --target CodexBarCore✅~/.kimi/config.toml) ✅Checklist