|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { PanelProvider } from '@/views/panel/backend/panel-provider' |
| 3 | +import { dictionary } from '@shared/constants/dictionary' |
| 4 | +import { Logger } from '@shared/utils/logger' |
| 5 | +import axios from 'axios' |
| 6 | +import { CHATBOTS } from '@shared/constants/chatbots' |
| 7 | +import { PickModelMessage } from '@/views/panel/types/messages' |
| 8 | + |
| 9 | +export const handle_pick_model = async ( |
| 10 | + panel_provider: PanelProvider, |
| 11 | + message: PickModelMessage |
| 12 | +): Promise<void> => { |
| 13 | + try { |
| 14 | + let items: vscode.QuickPickItem[] = [] |
| 15 | + |
| 16 | + if (message.chatbot_name == 'OpenRouter') { |
| 17 | + const response = await axios.get('https://openrouter.ai/api/v1/models') |
| 18 | + const models = response.data.data |
| 19 | + |
| 20 | + items = models.map((model: any) => ({ |
| 21 | + label: model.name, |
| 22 | + description: model.id, |
| 23 | + detail: model.description |
| 24 | + })) |
| 25 | + } else { |
| 26 | + const chatbot_config = |
| 27 | + CHATBOTS[message.chatbot_name as keyof typeof CHATBOTS] |
| 28 | + if (chatbot_config && chatbot_config.models) { |
| 29 | + items = Object.entries(chatbot_config.models).map(([id, data]) => ({ |
| 30 | + label: (data as any).label || id |
| 31 | + })) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + const quick_pick = vscode.window.createQuickPick() |
| 36 | + quick_pick.items = items |
| 37 | + quick_pick.title = 'Models' |
| 38 | + quick_pick.placeholder = 'Choose a model' |
| 39 | + quick_pick.buttons = [ |
| 40 | + { |
| 41 | + iconPath: new vscode.ThemeIcon('close'), |
| 42 | + tooltip: 'Close' |
| 43 | + } |
| 44 | + ] |
| 45 | + |
| 46 | + quick_pick.onDidTriggerButton((button) => { |
| 47 | + if (button.tooltip == 'Close') { |
| 48 | + quick_pick.hide() |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + return new Promise<void>((resolve) => { |
| 53 | + quick_pick.onDidAccept(() => { |
| 54 | + const selected = quick_pick.selectedItems[0] |
| 55 | + if (selected) { |
| 56 | + panel_provider.send_message({ |
| 57 | + command: 'NEWLY_PICKED_MODEL', |
| 58 | + model_id: selected.description! |
| 59 | + }) |
| 60 | + } |
| 61 | + quick_pick.hide() |
| 62 | + }) |
| 63 | + |
| 64 | + quick_pick.onDidHide(() => { |
| 65 | + quick_pick.dispose() |
| 66 | + resolve() |
| 67 | + }) |
| 68 | + |
| 69 | + quick_pick.show() |
| 70 | + }) |
| 71 | + } catch (error) { |
| 72 | + Logger.error({ |
| 73 | + function_name: 'handle_pick_model', |
| 74 | + message: 'Error picking model', |
| 75 | + data: error |
| 76 | + }) |
| 77 | + vscode.window.showErrorMessage( |
| 78 | + message.chatbot_name == 'OpenRouter' |
| 79 | + ? dictionary.error_message.FAILED_TO_FETCH_OPEN_ROUTER_MODELS |
| 80 | + : 'Failed to pick model' |
| 81 | + ) |
| 82 | + } |
| 83 | +} |
0 commit comments