Skip to content

Commit 5a69565

Browse files
committed
Refactor comparison operators from strict ===, !== to loose ==, != for consistency within the file's logic flow
1 parent 500988a commit 5a69565

1 file changed

Lines changed: 39 additions & 21 deletions

File tree

‎packages/vscode/src/views/settings/backend/message-handlers/handle-edit-custom-model-provider.ts‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from 'vscode'
2+
import axios from 'axios'
23
import { SettingsProvider } from '@/views/settings/backend/settings-provider'
34
import {
45
ModelProvidersManager,
@@ -295,19 +296,19 @@ export const handle_edit_custom_model_provider = async (
295296
break
296297
}
297298

298-
if (selected_id === 'rename') {
299+
if (selected_id == 'rename') {
299300
const new_name = await prompt_for_name()
300301
const trimmed_new_name = new_name.trim()
301302
if (trimmed_new_name && trimmed_new_name !== provider_to_edit.name) {
302303
provider_to_edit.name = trimmed_new_name
303304
}
304-
} else if (selected_id === 'edit-url') {
305+
} else if (selected_id == 'edit-url') {
305306
const new_url = await prompt_for_url()
306307
const normalized_new_url = normalize_base_url(new_url)
307308
if (normalized_new_url !== provider_to_edit.base_url) {
308309
provider_to_edit.base_url = normalized_new_url
309310
}
310-
} else if (selected_id === 'change-key') {
311+
} else if (selected_id == 'change-key') {
311312
if (!provider_to_edit.api_key) {
312313
// API key is not set, prompt for it directly
313314
const { value: new_key, accepted } = await prompt_for_key()
@@ -324,7 +325,7 @@ export const handle_edit_custom_model_provider = async (
324325
break // User escaped from change/clear, go to main menu
325326
}
326327

327-
if (action === 'change') {
328+
if (action == 'change') {
328329
const current_api_key = provider_to_edit.api_key
329330
const { value: new_key, accepted } = await prompt_for_key()
330331
const trimmed_api_key = new_key.trim()
@@ -341,30 +342,30 @@ export const handle_edit_custom_model_provider = async (
341342
}
342343

343344
if (
344-
trimmed_api_key === '' &&
345+
trimmed_api_key == '' &&
345346
current_api_key &&
346-
current_api_key !== ''
347+
current_api_key != ''
347348
) {
348349
const confirmation = await vscode.window.showWarningMessage(
349350
`Are you sure you want to clear the API key for ${provider_to_edit.name}? This action cannot be undone.`,
350351
{ modal: true },
351352
'Clear API Key'
352353
)
353-
if (confirmation !== 'Clear API Key') {
354+
if (confirmation != 'Clear API Key') {
354355
break // Don't clear, back to main menu
355356
}
356357
}
357358
provider_to_edit.api_key = trimmed_api_key
358359
break // Key changed, back to main menu
359-
} else if (action === 'clear') {
360-
if (provider_to_edit.api_key && provider_to_edit.api_key !== '') {
360+
} else if (action == 'clear') {
361+
if (provider_to_edit.api_key && provider_to_edit.api_key != '') {
361362
const confirmation = await vscode.window.showWarningMessage(
362363
`Are you sure you want to clear the API key for ${provider_to_edit.name}? This action cannot be undone.`,
363364
{ modal: true },
364365
'Clear API Key'
365366
)
366367

367-
if (confirmation === 'Clear API Key') {
368+
if (confirmation == 'Clear API Key') {
368369
provider_to_edit.api_key = ''
369370
}
370371
}
@@ -376,23 +377,40 @@ export const handle_edit_custom_model_provider = async (
376377
}
377378

378379
if (
379-
provider_to_edit.base_url !== original_provider.base_url &&
380+
provider_to_edit.base_url != original_provider.base_url &&
380381
!provider_to_edit.base_url.endsWith('/v1')
381382
) {
382-
const add_v1_suffix = 'Add "/v1" suffix'
383-
const choice = await vscode.window.showInformationMessage(
384-
'The Base URL does not end with "/v1". Many OpenAI-compatible APIs require this. Would you like to add it?',
385-
{ modal: true },
386-
add_v1_suffix
387-
)
383+
let should_ask_to_add_v1 = true
384+
try {
385+
const headers: { [key: string]: string } = {}
386+
if (provider_to_edit.api_key) {
387+
headers['Authorization'] = `Bearer ${provider_to_edit.api_key}`
388+
}
389+
await axios.get(`${provider_to_edit.base_url}/models`, {
390+
timeout: 2000,
391+
headers
392+
})
393+
should_ask_to_add_v1 = false
394+
} catch (error) {
395+
// If the request fails, we should ask the user to add /v1.
396+
}
388397

389-
if (choice === add_v1_suffix) {
390-
provider_to_edit.base_url = `${provider_to_edit.base_url}/v1`
398+
if (should_ask_to_add_v1) {
399+
const add_v1_suffix = 'Add "/v1" suffix'
400+
const choice = await vscode.window.showInformationMessage(
401+
'The Base URL does not end with "/v1". Many OpenAI-compatible APIs require this. Would you like to add it?',
402+
{ modal: true },
403+
add_v1_suffix
404+
)
405+
406+
if (choice == add_v1_suffix) {
407+
provider_to_edit.base_url = `${provider_to_edit.base_url}/v1`
408+
}
391409
}
392410
}
393411

394412
const has_changed =
395-
JSON.stringify(provider_to_edit) !== JSON.stringify(original_provider)
413+
JSON.stringify(provider_to_edit) != JSON.stringify(original_provider)
396414

397415
if (has_changed) {
398416
const providers = await providers_manager.get_providers()
@@ -402,7 +420,7 @@ export const handle_edit_custom_model_provider = async (
402420
: p
403421
)
404422
await providers_manager.save_providers(updated_providers)
405-
if (provider_to_edit.name !== original_provider.name) {
423+
if (provider_to_edit.name != original_provider.name) {
406424
await providers_manager.update_provider_name_in_configs({
407425
old_name: original_provider.name,
408426
new_name: provider_to_edit.name

0 commit comments

Comments
 (0)