Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Sources/CodexBar/Providers/Kimi/KimiProviderImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ struct KimiProviderImplementation: ProviderImplementation {
}
}

@MainActor
func tokenAccountsVisibility(context: ProviderSettingsContext, support: TokenAccountSupport) -> Bool {
guard support.requiresManualCookieSource else { return true }
// Kimi has no dedicated login runner, so the multi-account editor must
// stay visible even before the first account exists. Otherwise users
// cannot discover how to add web-auth accounts.
return true
}

@MainActor
func applyTokenAccountCookieSource(settings: SettingsStore) {
if settings.kimiCookieSource != .manual {
settings.kimiCookieSource = .manual
}
// Token accounts are cookie-based; an API-only usage source would bypass
// the per-account cookie, so fall back to the web pipeline.
if settings.kimiUsageDataSource == .api {
settings.kimiUsageDataSource = .web

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Force token accounts onto the web source from Auto

When Kimi is left at its default .auto source, adding or selecting an account does not change the source here. UsageStore.makeFetchContext uses that base source directly, so an existing API key or Kimi CLI credential causes the earlier API/CLI strategy to succeed for every account; the cards then show the shared API/CLI quota rather than each saved cookie's web quota. The descriptor's new selectedAccountSourceModeResolver only affects CLI commands, not app refreshes, so this method must also switch .auto to .web while token accounts are active.

Useful? React with 👍 / 👎.

}
}

@MainActor
func settingsPickers(context: ProviderSettingsContext) -> [ProviderSettingsPickerDescriptor] {
let usageBinding = Binding(
Expand Down
20 changes: 19 additions & 1 deletion Sources/CodexBarCore/Providers/Kimi/KimiProviderDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public enum KimiProviderDescriptor {
guard let token else { return nil }
return ProviderTokenResolution(token: token, source: .environment)
},
tokenAccountSupport: TokenAccountSupport(
title: "Kimi accounts",
subtitle: "Store multiple Kimi kimi-auth tokens (web login). Each account is fetched with its own cookie.",
placeholder: "Paste kimi-auth token\u{2026}",
injection: .cookieHeader,
requiresManualCookieSource: true,
cookieName: "kimi-auth",
environmentOverride: { token in
[KimiSettingsReader.authTokenEnvironmentKey: token]
},
environmentScrubber: { environment, _ in
environment.removeValue(forKey: KimiSettingsReader.authTokenEnvironmentKey)
environment.removeValue(forKey: "KIMI_MANUAL_COOKIE")
}),
authDetector: { environment, _ in
var modes: [String] = []
if KimiSettingsReader.apiKey(environment: environment) != nil {
Expand All @@ -29,7 +43,11 @@ public enum KimiProviderDescriptor {
}
return modes
},
missingCredentialMessage: { _ in KimiAPIError.missingToken.errorDescription })
missingCredentialMessage: { _ in KimiAPIError.missingToken.errorDescription },
selectedAccountSourceModeResolver: { base, account, _ in
guard account != nil else { return base }
return .web
})

static func makeDescriptor() -> ProviderDescriptor {
ProviderDescriptor(
Expand Down
3 changes: 2 additions & 1 deletion Sources/CodexBarCore/Providers/Kimi/KimiSettingsReader.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation

public enum KimiSettingsReader {
public static let authTokenEnvironmentKey = "KIMI_AUTH_TOKEN"
public static let apiKeyEnvironmentKeys = ["KIMI_CODE_API_KEY"]
public static let codeAPIBaseURLEnvironmentKey = "KIMI_CODE_BASE_URL"
public static let codeHomeEnvironmentKey = "KIMI_CODE_HOME"
Expand All @@ -9,7 +10,7 @@ public enum KimiSettingsReader {
private static let codePlatform = "kimi_code_cli"

public static func authToken(environment: [String: String] = ProcessInfo.processInfo.environment) -> String? {
let raw = environment["KIMI_AUTH_TOKEN"] ?? environment["kimi_auth_token"]
let raw = environment[self.authTokenEnvironmentKey] ?? environment["kimi_auth_token"]
return self.cleaned(raw)
}

Expand Down