|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { |
| 3 | + ModelProvidersManager, |
| 4 | + ToolConfig |
| 5 | +} from '@/services/model-providers-manager' |
| 6 | +import { |
| 7 | + ModelFetcher, |
| 8 | + MODELS_ROUTE_NOT_FOUND_ERROR |
| 9 | +} from '@/services/model-fetcher' |
| 10 | +import { PROVIDERS } from '@/constants/providers' |
| 11 | +import { Logger } from '@shared/utils/logger' |
| 12 | +import { dictionary } from '@shared/constants/dictionary' |
| 13 | +import { ToolType } from '../../../settings/types/tools' |
| 14 | +import { verify_model } from './verify-model' |
| 15 | + |
| 16 | +export const edit_model_for_config = async (params: { |
| 17 | + config: ToolConfig |
| 18 | + providers_manager: ModelProvidersManager |
| 19 | + model_fetcher: ModelFetcher |
| 20 | + tool_type?: ToolType |
| 21 | +}) => { |
| 22 | + const provider_from_manager = await params.providers_manager.get_provider( |
| 23 | + params.config.provider_name |
| 24 | + ) |
| 25 | + if (!provider_from_manager) { |
| 26 | + vscode.window.showErrorMessage( |
| 27 | + dictionary.error_message.PROVIDER_NOT_FOUND(params.config.provider_name) |
| 28 | + ) |
| 29 | + return undefined |
| 30 | + } |
| 31 | + |
| 32 | + const base_url = |
| 33 | + provider_from_manager.type == 'built-in' |
| 34 | + ? PROVIDERS[provider_from_manager.name]?.base_url |
| 35 | + : provider_from_manager.base_url |
| 36 | + |
| 37 | + if (!base_url) { |
| 38 | + vscode.window.showErrorMessage( |
| 39 | + dictionary.error_message.BASE_URL_NOT_FOUND_FOR_PROVIDER( |
| 40 | + params.config.provider_name |
| 41 | + ) |
| 42 | + ) |
| 43 | + return undefined |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + const models = await params.model_fetcher.get_models({ |
| 48 | + base_url, |
| 49 | + api_key: provider_from_manager.api_key |
| 50 | + }) |
| 51 | + |
| 52 | + if (models.length > 0) { |
| 53 | + const model_items = models.map((model) => ({ |
| 54 | + label: model.name || model.id, |
| 55 | + description: model.name ? model.id : undefined, |
| 56 | + detail: model.description |
| 57 | + })) |
| 58 | + |
| 59 | + let last_selected_model_id = params.config.model |
| 60 | + |
| 61 | + while (true) { |
| 62 | + const selected_model_item = await new Promise< |
| 63 | + (typeof model_items)[0] | undefined |
| 64 | + >((resolve) => { |
| 65 | + const quick_pick = |
| 66 | + vscode.window.createQuickPick<(typeof model_items)[0]>() |
| 67 | + quick_pick.items = model_items |
| 68 | + quick_pick.title = 'Models' |
| 69 | + quick_pick.placeholder = 'Choose a model' |
| 70 | + quick_pick.buttons = [vscode.QuickInputButtons.Back] |
| 71 | + if (last_selected_model_id) { |
| 72 | + const active = model_items.find( |
| 73 | + (item) => |
| 74 | + (item.description || item.label) === last_selected_model_id |
| 75 | + ) |
| 76 | + if (active) quick_pick.activeItems = [active] |
| 77 | + } |
| 78 | + let accepted = false |
| 79 | + const disposables: vscode.Disposable[] = [] |
| 80 | + |
| 81 | + disposables.push( |
| 82 | + quick_pick.onDidAccept(() => { |
| 83 | + accepted = true |
| 84 | + resolve(quick_pick.selectedItems[0]) |
| 85 | + quick_pick.hide() |
| 86 | + }), |
| 87 | + quick_pick.onDidTriggerButton((button) => { |
| 88 | + if (button === vscode.QuickInputButtons.Back) { |
| 89 | + quick_pick.hide() |
| 90 | + } |
| 91 | + }), |
| 92 | + quick_pick.onDidHide(() => { |
| 93 | + if (!accepted) resolve(undefined) |
| 94 | + disposables.forEach((d) => d.dispose()) |
| 95 | + quick_pick.dispose() |
| 96 | + }) |
| 97 | + ) |
| 98 | + quick_pick.show() |
| 99 | + }) |
| 100 | + |
| 101 | + if (!selected_model_item) return undefined |
| 102 | + |
| 103 | + const model = ( |
| 104 | + selected_model_item.description || selected_model_item.label |
| 105 | + ).trim() |
| 106 | + |
| 107 | + last_selected_model_id = model |
| 108 | + |
| 109 | + if ( |
| 110 | + await verify_model({ |
| 111 | + model, |
| 112 | + base_url, |
| 113 | + api_key: provider_from_manager.api_key, |
| 114 | + is_voice_input: params.tool_type == 'voice-input' |
| 115 | + }) |
| 116 | + ) { |
| 117 | + return model |
| 118 | + } |
| 119 | + } |
| 120 | + } else { |
| 121 | + vscode.window.showWarningMessage( |
| 122 | + dictionary.warning_message.NO_MODELS_FOUND_MANUAL_ENTRY( |
| 123 | + params.config.provider_name |
| 124 | + ) |
| 125 | + ) |
| 126 | + } |
| 127 | + } catch (error) { |
| 128 | + Logger.error({ |
| 129 | + function_name: 'edit_model_for_config', |
| 130 | + message: 'Failed to fetch models', |
| 131 | + data: error |
| 132 | + }) |
| 133 | + if ( |
| 134 | + error instanceof Error && |
| 135 | + error.message == MODELS_ROUTE_NOT_FOUND_ERROR |
| 136 | + ) { |
| 137 | + vscode.window.showInformationMessage( |
| 138 | + dictionary.information_message.MODELS_ROUTE_NOT_FOUND_MANUAL_ENTRY( |
| 139 | + params.config.provider_name |
| 140 | + ), |
| 141 | + { modal: true } |
| 142 | + ) |
| 143 | + } else { |
| 144 | + vscode.window.showErrorMessage( |
| 145 | + dictionary.error_message.FAILED_TO_FETCH_MODELS( |
| 146 | + error instanceof Error ? error.message : String(error) |
| 147 | + ) |
| 148 | + ) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + while (true) { |
| 153 | + const new_model_input = await vscode.window.showInputBox({ |
| 154 | + title: 'Models', |
| 155 | + value: params.config.model, |
| 156 | + prompt: `Enter a model name (ID)` |
| 157 | + }) |
| 158 | + |
| 159 | + if (!new_model_input) return undefined |
| 160 | + const model = new_model_input.trim() |
| 161 | + |
| 162 | + if ( |
| 163 | + await verify_model({ |
| 164 | + model, |
| 165 | + base_url, |
| 166 | + api_key: provider_from_manager.api_key, |
| 167 | + is_voice_input: params.tool_type == 'voice-input' |
| 168 | + }) |
| 169 | + ) { |
| 170 | + return model |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments