From 9d53040f00a391fa6b170fa6fd16ef739db92c1c Mon Sep 17 00:00:00 2001 From: Arvind Date: Tue, 27 Jan 2026 11:27:54 +0530 Subject: [PATCH] Fix token account override being overwritten by config apiKey When a token account is selected, the environment override for the token was being applied first, but then ProviderConfigEnvironment.applyAPIKeyOverride() was called afterward, which would overwrite the token account's value with the config's root apiKey. This caused issues when: 1. A user has both a root apiKey and token accounts configured 2. The root apiKey is invalid or doesn't have a plan 3. The token account's token is valid The fix returns early after applying the token account override, preventing the config's apiKey from overwriting it. Fixes both ProviderRegistry.makeEnvironment (app) and TokenAccountCLIContext.environment (CLI). --- Sources/CodexBar/ProviderRegistry.swift | 2 ++ Sources/CodexBarCLI/TokenAccountCLI.swift | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Sources/CodexBar/ProviderRegistry.swift b/Sources/CodexBar/ProviderRegistry.swift index 465020b0..4e74ba37 100644 --- a/Sources/CodexBar/ProviderRegistry.swift +++ b/Sources/CodexBar/ProviderRegistry.swift @@ -98,6 +98,7 @@ struct ProviderRegistry { settings: settings, override: tokenOverride) var env = base + // If token account is selected, use its token instead of config's apiKey if let account, let override = TokenAccountSupportCatalog.envOverride( for: provider, token: account.token) @@ -105,6 +106,7 @@ struct ProviderRegistry { for (key, value) in override { env[key] = value } + return env } return ProviderConfigEnvironment.applyAPIKeyOverride( base: env, diff --git a/Sources/CodexBarCLI/TokenAccountCLI.swift b/Sources/CodexBarCLI/TokenAccountCLI.swift index a0a88371..2291b674 100644 --- a/Sources/CodexBarCLI/TokenAccountCLI.swift +++ b/Sources/CodexBarCLI/TokenAccountCLI.swift @@ -185,12 +185,14 @@ struct TokenAccountCLIContext { account: ProviderTokenAccount?) -> [String: String] { var env = base + // If token account is selected, use its token instead of config's apiKey if let account, let override = TokenAccountSupportCatalog.envOverride(for: provider, token: account.token) { for (key, value) in override { env[key] = value } + return env } let providerConfig = self.providerConfig(for: provider) env = ProviderConfigEnvironment.applyAPIKeyOverride(