import { Box, Text, useInput } from 'ink'; import React, { useState } from 'react'; import type { ModelConfig } from '../config/configFile.js'; import type { OnlineModelAvailability } from '../team/onlineToolContract.js'; import { theme } from './theme.js'; import { terminalSingleLine } from './terminalText.js'; export interface GatewayPickerModel { id: string; source: 'gateway'; apiFormat: 'openai' | 'anthropic'; displayName: string; model: string; contextWindow: number; maxTokens: number; supportsVision: boolean; enabled: true; isDefault?: boolean; availability?: OnlineModelAvailability; } export type PickerModel = | (ModelConfig & { source?: 'local'; isDefault?: boolean; availability?: never; }) | GatewayPickerModel; interface ModelPickerProps { models: PickerModel[]; currentModelId: string | null; title: string; status?: string; /** null 选项标签(用于顾问模型"不使用");不传则不显示 null 选项 */ nullOptionLabel?: string; onSelect: (modelId: string | null) => void; onCancel: () => void; } export function ModelPicker({ models, currentModelId, title, status, nullOptionLabel, onSelect, onCancel, }: ModelPickerProps): JSX.Element { const enabledModels = models.filter( (m) => m.enabled && (m.source === 'gateway' || m.apiKey), ); const hasNullOption = Boolean(nullOptionLabel); const nullIndex = hasNullOption ? enabledModels.length : -1; const listLength = enabledModels.length + (hasNullOption ? 1 : 0); const defaultIdx = (() => { const idx = enabledModels.findIndex((m) => m.id === currentModelId); if (idx >= 0) return idx; if (hasNullOption && !currentModelId) return nullIndex; return 0; })(); const [chosen, setChosen] = useState(); const indexFor = (id: string | null | undefined) => { const found = id === null && hasNullOption ? nullIndex : enabledModels.findIndex(m => m.id === id); return found >= 0 ? found : defaultIdx; }; const idx = indexFor(chosen); useInput((input, key) => { if (key.escape || (key.ctrl && input === 'c')) { onCancel(); return; } if (!listLength) return; if (key.upArrow || key.downArrow) { setChosen(previous => { const next = (indexFor(previous) + (key.upArrow ? -1 : 1) + listLength) % listLength; return next === nullIndex ? null : enabledModels[next]?.id; }); return; } if (key.return) { if (hasNullOption && idx === nullIndex) { onSelect(null); } else { const target = enabledModels[idx]; if ( target && (target.source !== 'gateway' || !target.availability || target.availability.state === 'available') ) onSelect(target.id); } } }); return ( {title} {status && {status}} {enabledModels.map((m, i) => { const active = i === idx; const isCurrent = m.id === currentModelId; const displayName = terminalSingleLine(m.displayName || m.model); const reason = m.availability?.reason ? terminalSingleLine(m.availability.reason) : ''; return ( {i > 0 && enabledModels[i - 1]?.source !== m.source ? ( {' '} ── {m.source === 'gateway' ? '团队在线模型' : '本机模型'} ── ) : null} {active ? '> ' : ' '} {isCurrent ? '✓ ' : ' '} {m.source === 'gateway' ? '🌐 ' : ''} {displayName} {m.isDefault ? ' · 默认' : ''} {reason ? ` · ${reason}` : ''} {isCurrent ? (当前) : null} {' '} {terminalSingleLine(m.model)} · {formatContextWindow(m.contextWindow)} ); })} {hasNullOption && ( {idx === nullIndex ? '> ' : ' '} {!currentModelId ? '✓ ' : ' '} {nullOptionLabel} )} ↑↓ 选择 · Enter 确认 · ESC 取消 ); } function formatContextWindow(tokens: number): string { if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M context`; if (tokens >= 1_000) return `${Math.round(tokens / 1_000)}K context`; return `${tokens} context`; }