|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { getAvailableModels, formatModelForDisplay } from '../services/aiModelService'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Command: Select preferred AI model for all AI features |
| 6 | + * Sets the global AI model preference used by templates, tagging, summarization, and search |
| 7 | + */ |
| 8 | +export async function handleSelectAIModel(): Promise<void> { |
| 9 | + try { |
| 10 | + // Get all available models |
| 11 | + const models = await getAvailableModels(); |
| 12 | + |
| 13 | + if (models.length === 0) { |
| 14 | + vscode.window.showErrorMessage( |
| 15 | + 'No AI models available. Please install GitHub Copilot, Claude, or another LLM extension.' |
| 16 | + ); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + // Get current preference (check both global and template-specific for backwards compatibility) |
| 21 | + const config = vscode.workspace.getConfiguration('noted'); |
| 22 | + const currentModelId = config.get<string>('ai.preferredModel') || config.get<string>('templates.preferredModel') || ''; |
| 23 | + |
| 24 | + // Build quick pick items |
| 25 | + interface ModelQuickPickItem extends vscode.QuickPickItem { |
| 26 | + modelId: string; |
| 27 | + } |
| 28 | + |
| 29 | + const items: ModelQuickPickItem[] = models.map(model => { |
| 30 | + const formatted = formatModelForDisplay(model); |
| 31 | + const isSelected = model.id === currentModelId; |
| 32 | + const icon = isSelected ? '$(check)' : '$(circle-outline)'; |
| 33 | + |
| 34 | + return { |
| 35 | + modelId: model.id, |
| 36 | + label: `${icon} ${formatted.label}`, |
| 37 | + description: formatted.description, |
| 38 | + detail: formatted.detail, |
| 39 | + picked: isSelected |
| 40 | + }; |
| 41 | + }); |
| 42 | + |
| 43 | + // Add "Automatic" option at the top |
| 44 | + items.unshift({ |
| 45 | + modelId: '', |
| 46 | + label: `${currentModelId === '' ? '$(check)' : '$(circle-outline)'} Automatic (Recommended)`, |
| 47 | + description: 'Smart selection based on availability', |
| 48 | + detail: 'Priority: Claude Sonnet → Opus → GPT-4 → Gemini', |
| 49 | + picked: currentModelId === '' |
| 50 | + }); |
| 51 | + |
| 52 | + // Show picker |
| 53 | + const selected = await vscode.window.showQuickPick(items, { |
| 54 | + placeHolder: 'Select AI model for all AI features (templates, tagging, summarization, search)', |
| 55 | + title: 'AI Model Selection', |
| 56 | + matchOnDescription: true, |
| 57 | + matchOnDetail: true |
| 58 | + }); |
| 59 | + |
| 60 | + if (!selected) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Save preference to global config |
| 65 | + await config.update('ai.preferredModel', selected.modelId, vscode.ConfigurationTarget.Global); |
| 66 | + |
| 67 | + const modelName = selected.modelId === '' ? 'Automatic selection' : selected.label.replace(/\$\(.*?\)\s/, ''); |
| 68 | + vscode.window.showInformationMessage( |
| 69 | + `AI model set to: ${modelName}\n\nThis will be used for templates, tagging, summarization, and search.` |
| 70 | + ); |
| 71 | + } catch (error) { |
| 72 | + if (error instanceof Error) { |
| 73 | + vscode.window.showErrorMessage(`Failed to select AI model: ${error.message}`); |
| 74 | + } else { |
| 75 | + vscode.window.showErrorMessage('Failed to select AI model: Unknown error'); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments