fix: clear error messages for missing API keys on provider switch

Previously, switching to zhipuai/openrouter/xai via /model would throw a
confusing 'OPENAI_API_KEY missing' error from the OpenAI SDK. Now
createClientFromConfig validates API keys before constructing the client,
throwing errors that name the correct env var (e.g. ZHIPUAI_API_KEY).

Also fixes the misleading 'as anthropic' type cast in the /model handler
to validate against MODEL_PROVIDERS and use the ModelProvider type.
This commit is contained in:
William Valentin
2026-02-10 21:32:44 -08:00
parent aaaf4a361a
commit 5c90640e2a
2 changed files with 26 additions and 5 deletions
+8 -2
View File
@@ -5,7 +5,8 @@ import type { ModelRouter, ModelTier } from '../../models/router.js';
import type { NativeAgent } from '../../backends/native/agent.js';
import { parseCommand, getHelpText, resolveModelAlias, getCommandCompletions, getCommandTooltip, type Command } from './commands.js';
import { renderMarkdown } from './markdown.js';
import type { ModelConfig } from '../../config/schema.js';
import type { ModelConfig, ModelProvider } from '../../config/schema.js';
import { MODEL_PROVIDERS } from '../../config/schema.js';
import { OllamaClient, LlamaCppClient } from '../../models/index.js';
import { createClientFromConfig } from '../../daemon/index.js';
import { loginGitHub } from '../../auth/index.js';
@@ -232,8 +233,13 @@ export class MinimalTui {
const provider = providerModel.slice(0, slashIdx);
const model = providerModel.slice(slashIdx + 1);
if (!MODEL_PROVIDERS.includes(provider as ModelProvider)) {
console.log(`${colors.gray}Unknown provider "${provider}". Known providers: ${MODEL_PROVIDERS.join(', ')}${colors.reset}\n`);
return;
}
try {
const client = createClientFromConfig({ provider: provider as 'anthropic', model });
const client = createClientFromConfig({ provider: provider as ModelProvider, model });
router.setClient(tier, client, providerModel);
console.log(`${colors.gray}Set ${tier} to:${colors.reset} ${providerModel}\n`);
} catch (error) {