Files
flynn/src/cli/setup/providers.ts
T
William Valentin 6090508bad style: auto-fix ESLint issues (curly braces and formatting)
- Add curly braces to all if/else/for/while statements
- Fix indentation and trailing spaces
- Auto-fixed 372 linting errors using eslint --fix
- Remaining issues are warnings only (non-null assertions, explicit any types)
2026-02-11 10:30:24 -08:00

82 lines
3.9 KiB
TypeScript

import type { Prompter } from './prompts.js';
import type { ConfigBuilder } from './config.js';
interface ProviderDef {
name: string;
provider: string;
defaultModel: string;
fastModel?: string;
needsApiKey: boolean;
needsEndpoint: boolean;
defaultEndpoint?: string;
apiKeyLabel?: string;
}
const TOP_TIER: ProviderDef[] = [
{ name: 'Anthropic', provider: 'anthropic', defaultModel: 'claude-sonnet-4-20250514', fastModel: 'claude-haiku-4-5-20251001', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'Anthropic API key' },
{ name: 'OpenAI', provider: 'openai', defaultModel: 'gpt-4.1', fastModel: 'gpt-4.1-mini', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'OpenAI API key' },
{ name: 'Ollama (local)', provider: 'ollama', defaultModel: 'llama3.3', fastModel: 'llama3.2:3b', needsApiKey: false, needsEndpoint: true, defaultEndpoint: 'http://localhost:11434' },
];
const SECOND_TIER: ProviderDef[] = [
{ name: 'Gemini', provider: 'gemini', defaultModel: 'gemini-2.5-flash', fastModel: 'gemini-2.0-flash-lite', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'Gemini API key' },
{ name: 'OpenRouter', provider: 'openrouter', defaultModel: 'anthropic/claude-sonnet-4', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'OpenRouter API key' },
{ name: 'xAI (Grok)', provider: 'xai', defaultModel: 'grok-3', fastModel: 'grok-3-mini', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'xAI API key' },
{ name: 'Amazon Bedrock', provider: 'bedrock', defaultModel: 'anthropic.claude-sonnet-4-20250514-v1:0', needsApiKey: false, needsEndpoint: false },
{ name: 'GitHub Models', provider: 'github', defaultModel: 'claude-sonnet-4-20250514', needsApiKey: false, needsEndpoint: false },
];
const PROVIDER_HELP: Record<string, string> = {
anthropic: 'Get your API key at https://console.anthropic.com/settings/keys',
openai: 'Get your API key at https://platform.openai.com/api-keys',
ollama: 'Ollama runs locally — install from https://ollama.com and run: ollama serve',
gemini: 'Get your API key at https://aistudio.google.com/apikey',
openrouter: 'Get your API key at https://openrouter.ai/keys (supports 200+ models)',
xai: 'Get your API key at https://console.x.ai',
bedrock: 'Uses AWS credentials from environment (~/.aws/credentials or IAM role)',
github: 'Uses GitHub Copilot — authenticate via OAuth device flow on first use',
};
async function configureProvider(p: Prompter, def: ProviderDef): Promise<{
provider: string; model: string; api_key?: string; endpoint?: string;
}> {
const help = PROVIDER_HELP[def.provider];
if (help) {p.println(` ${help}`);}
const config: Record<string, string> = { provider: def.provider };
if (def.needsApiKey) {config.api_key = await p.password(def.apiKeyLabel ?? 'API key');}
if (def.needsEndpoint) {config.endpoint = await p.ask('Host', def.defaultEndpoint);}
config.model = await p.ask('Model', def.defaultModel);
return config as { provider: string; model: string; api_key?: string; endpoint?: string };
}
export async function setupProviders(p: Prompter, builder: ConfigBuilder): Promise<void> {
const allOptions = [
...TOP_TIER.map(d => ({ label: d.name, value: d })),
{ label: 'More providers...', value: null as ProviderDef | null },
];
let chosen: ProviderDef;
const selection = await p.choose('Model provider:', allOptions);
if (selection === null) {
const secondOptions = SECOND_TIER.map(d => ({ label: d.name, value: d }));
chosen = await p.choose('Model provider:', secondOptions);
} else {
chosen = selection;
}
p.println();
const cfg = await configureProvider(p, chosen);
builder.setProvider('default', cfg);
if (chosen.fastModel) {
p.println();
const wantFast = await p.confirm('Configure a fast tier for compaction/delegation?', false);
if (wantFast) {
const fastModel = await p.ask('Fast model', chosen.fastModel);
builder.setProvider('fast', { ...cfg, model: fastModel });
}
}
}