|
1 | | -import * as vscode from 'vscode' |
2 | 1 | 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 | 2 | import { PickModelMessage } from '@/views/panel/types/messages' |
| 3 | +import { pick_model } from '@/views/actions/pick-model' |
8 | 4 |
|
9 | 5 | export const handle_pick_model = async ( |
10 | 6 | panel_provider: PanelProvider, |
11 | 7 | message: PickModelMessage |
12 | 8 | ): Promise<void> => { |
13 | | - try { |
14 | | - let items: (vscode.QuickPickItem & { model_id: string })[] = [] |
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 | | - model_id: model.id |
23 | | - })) |
24 | | - } else { |
25 | | - const chatbot_config = |
26 | | - CHATBOTS[message.chatbot_name as keyof typeof CHATBOTS] |
27 | | - if (chatbot_config && chatbot_config.models) { |
28 | | - items = Object.entries(chatbot_config.models).map(([id, data]) => ({ |
29 | | - label: (data as any).label || id, |
30 | | - model_id: 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 | | - |
40 | | - if (message.current_model_id) { |
41 | | - const active_item = items.find( |
42 | | - (i) => i.model_id === message.current_model_id |
43 | | - ) |
44 | | - if (active_item) quick_pick.activeItems = [active_item] |
45 | | - } |
46 | | - |
47 | | - quick_pick.buttons = [ |
48 | | - { |
49 | | - iconPath: new vscode.ThemeIcon('close'), |
50 | | - tooltip: 'Close' |
51 | | - } |
52 | | - ] |
53 | | - |
54 | | - quick_pick.onDidTriggerButton((button) => { |
55 | | - if (button.tooltip == 'Close') { |
56 | | - quick_pick.hide() |
57 | | - } |
58 | | - }) |
59 | | - |
60 | | - return new Promise<void>((resolve) => { |
61 | | - quick_pick.onDidAccept(() => { |
62 | | - const selected = quick_pick.selectedItems[0] as any |
63 | | - if (selected) { |
64 | | - panel_provider.send_message({ |
65 | | - command: 'NEWLY_PICKED_MODEL', |
66 | | - model_id: selected.model_id |
67 | | - }) |
68 | | - } |
69 | | - quick_pick.hide() |
70 | | - }) |
71 | | - |
72 | | - quick_pick.onDidHide(() => { |
73 | | - quick_pick.dispose() |
74 | | - resolve() |
75 | | - }) |
76 | | - |
77 | | - quick_pick.show() |
78 | | - }) |
79 | | - } catch (error) { |
80 | | - Logger.error({ |
81 | | - function_name: 'handle_pick_model', |
82 | | - message: 'Error picking model', |
83 | | - data: error |
| 9 | + const selected = await pick_model({ |
| 10 | + chatbot_name: message.chatbot_name, |
| 11 | + current_model_id: message.current_model_id |
| 12 | + }) |
| 13 | + if (selected) { |
| 14 | + panel_provider.send_message({ |
| 15 | + command: 'NEWLY_PICKED_MODEL', |
| 16 | + model_id: selected |
84 | 17 | }) |
85 | | - vscode.window.showErrorMessage( |
86 | | - message.chatbot_name == 'OpenRouter' |
87 | | - ? dictionary.error_message.FAILED_TO_FETCH_OPEN_ROUTER_MODELS |
88 | | - : 'Failed to pick model' |
89 | | - ) |
90 | 18 | } |
91 | 19 | } |
0 commit comments