|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { dictionary } from '@shared/constants/dictionary' |
| 3 | +import { PanelProvider } from '@/views/panel/backend/panel-provider' |
| 4 | +import { UpdateWebConfigurationMessage } from '@/views/panel/types/messages' |
| 5 | +import { WebConfiguration } from '@shared/types/web-configuration' |
| 6 | +import { |
| 7 | + ConfigWebConfigurationFormat, |
| 8 | + ui_web_configuration_to_config_format |
| 9 | +} from '@/views/utils/web-configuration-format-converters' |
| 10 | + |
| 11 | +export const handle_update_web_configuration = async ( |
| 12 | + panel_provider: PanelProvider, |
| 13 | + message: UpdateWebConfigurationMessage |
| 14 | +): Promise<void> => { |
| 15 | + const config = vscode.workspace.getConfiguration('codeWebChat') |
| 16 | + const current_web_configurations = |
| 17 | + config.get<ConfigWebConfigurationFormat[]>('webConfigurations', []) || [] |
| 18 | + |
| 19 | + const web_configuration_index = current_web_configurations.findIndex( |
| 20 | + (p) => p.name == message.updating_web_configuration.name |
| 21 | + ) |
| 22 | + |
| 23 | + if (web_configuration_index == -1) { |
| 24 | + console.error( |
| 25 | + `web configuration with original name "${message.updating_web_configuration.name}" not found.` |
| 26 | + ) |
| 27 | + vscode.window.showErrorMessage( |
| 28 | + dictionary.error_message.COULD_NOT_UPDATE_ITEM_NOT_FOUND( |
| 29 | + 'web configuration', |
| 30 | + message.updating_web_configuration.name! |
| 31 | + ) |
| 32 | + ) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + const are_web_configurations_equal = (a: WebConfiguration, b: WebConfiguration): boolean => { |
| 37 | + return ( |
| 38 | + a.name == b.name && |
| 39 | + a.chatbot == b.chatbot && |
| 40 | + a.model == b.model && |
| 41 | + a.temperature === b.temperature && |
| 42 | + a.top_p === b.top_p && |
| 43 | + a.thinking_budget === b.thinking_budget && |
| 44 | + a.reasoning_effort == b.reasoning_effort && |
| 45 | + a.system_instructions == b.system_instructions && |
| 46 | + JSON.stringify(a.options) == JSON.stringify(b.options) && |
| 47 | + a.port == b.port && |
| 48 | + a.new_url == b.new_url && |
| 49 | + a.is_pinned == b.is_pinned |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + const final_updated_web_configuration = { ...message.updated_web_configuration } |
| 54 | + |
| 55 | + const has_changes = !are_web_configurations_equal( |
| 56 | + message.updating_web_configuration, |
| 57 | + final_updated_web_configuration |
| 58 | + ) |
| 59 | + |
| 60 | + if (!has_changes) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if (message.origin == 'back_button') { |
| 65 | + const save_changes_button = 'Save' |
| 66 | + const discard_changes = 'Discard changes' |
| 67 | + const result = await vscode.window.showInformationMessage( |
| 68 | + dictionary.information_message.CONFIRM_SAVE_CHANGES_TO_ITEM('web configuration'), |
| 69 | + { |
| 70 | + modal: true, |
| 71 | + detail: dictionary.information_message.UNSAVED_CHANGES_TO_ITEM_WILL_BE_LOST( |
| 72 | + 'web configuration' |
| 73 | + ) |
| 74 | + }, |
| 75 | + save_changes_button, |
| 76 | + discard_changes |
| 77 | + ) |
| 78 | + |
| 79 | + if (result == discard_changes) { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + if (result != save_changes_button) { |
| 84 | + return |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const updated_ui_web_configuration = { ...final_updated_web_configuration } |
| 89 | + if (!updated_ui_web_configuration.name || !updated_ui_web_configuration.name.trim()) { |
| 90 | + let counter = 1 |
| 91 | + while (true) { |
| 92 | + const candidate = `(${counter})` |
| 93 | + const conflict = current_web_configurations.some( |
| 94 | + (p, index) => index != web_configuration_index && p.name == candidate |
| 95 | + ) |
| 96 | + if (!conflict) { |
| 97 | + updated_ui_web_configuration.name = candidate |
| 98 | + break |
| 99 | + } |
| 100 | + counter++ |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (updated_ui_web_configuration.name) { |
| 105 | + let final_name = updated_ui_web_configuration.name.trim() |
| 106 | + |
| 107 | + let is_unique = false |
| 108 | + let copy_number = 0 |
| 109 | + const base_name = final_name |
| 110 | + |
| 111 | + while (!is_unique) { |
| 112 | + const name_to_check = |
| 113 | + copy_number == 0 ? base_name : `${base_name} (${copy_number})`.trim() |
| 114 | + |
| 115 | + const conflict = current_web_configurations.some( |
| 116 | + (p, index) => index != web_configuration_index && p.name == name_to_check |
| 117 | + ) |
| 118 | + |
| 119 | + if (!conflict) { |
| 120 | + final_name = name_to_check |
| 121 | + is_unique = true |
| 122 | + } else { |
| 123 | + copy_number++ |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (final_name != updated_ui_web_configuration.name) { |
| 128 | + updated_ui_web_configuration.name = final_name |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + const updated_web_configurations = [...current_web_configurations] |
| 133 | + updated_web_configurations[web_configuration_index] = ui_web_configuration_to_config_format( |
| 134 | + updated_ui_web_configuration |
| 135 | + ) |
| 136 | + |
| 137 | + await config.update( |
| 138 | + 'webConfigurations', |
| 139 | + updated_web_configurations, |
| 140 | + vscode.ConfigurationTarget.Global |
| 141 | + ) |
| 142 | + |
| 143 | + if (updated_ui_web_configuration && updated_ui_web_configuration.name) { |
| 144 | + panel_provider.send_message({ |
| 145 | + command: 'SELECTED_WEB_CONFIGURATION_CHANGED', |
| 146 | + prompt_type: panel_provider.web_prompt_type, |
| 147 | + name: updated_ui_web_configuration.name |
| 148 | + }) |
| 149 | + } |
| 150 | + |
| 151 | + panel_provider.send_message({ |
| 152 | + command: 'WEB_CONFIGURATION_UPDATED' |
| 153 | + }) |
| 154 | +} |
0 commit comments