Skip to content

Moonshot: auto-discover API key from Kimi CLI config#793

Open
maxitoon wants to merge 3 commits intosteipete:mainfrom
maxitoon:feature/kimi-cli-config-fallback
Open

Moonshot: auto-discover API key from Kimi CLI config#793
maxitoon wants to merge 3 commits intosteipete:mainfrom
maxitoon:feature/kimi-cli-config-fallback

Conversation

@maxitoon
Copy link
Copy Markdown

@maxitoon maxitoon commented Apr 26, 2026

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.toml under [providers."managed:moonshot-ai"], but CodexBar never looked there.

Solution: Extend MoonshotSettingsReader.apiKey() to fall back to ~/.kimi/config.toml when MOONSHOT_API_KEY / MOONSHOT_KEY env vars are absent.

  • parseKimiConfigAPIKey() — simple TOML-line parser that respects section boundaries, strips quotes, and ignores comments
  • loadKimiConfigContents() — reads ~/.kimi/config.toml
  • Fallback priority: env vars → Kimi CLI config → nil
  • 7 unit tests covering sections, quotes, comments, and boundaries

2. 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
  • Standalone logic tests (14/14 passed against real ~/.kimi/config.toml) ✅
  • Built and running locally with both changes ✅

Checklist

  • Logic follows existing CodexBar patterns
  • No breaking changes to existing env-var resolution
  • Unit tests added
  • Minimal diff

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.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@maxitoon
Copy link
Copy Markdown
Author

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 (~/.kimi/config.toml).

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 ~/.kimi/config.toml under [providers."managed:moonshot-ai"]. This change makes the Moonshot provider "just work" for terminal users without requiring manual env vars.

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.
@giuseppebisemi
Copy link
Copy Markdown
Contributor

giuseppebisemi commented May 4, 2026

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 moonshotBalanceDisplayText strips Balance: and keeps only the first amount, e.g. Balance: $49.58 · $0.42 in deficit -> $49.58.

For the icon alignment, I adjusted the shared ProviderIcon-kimi.svg by applying a small upward translation to the main path:

<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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants