Skip to content

Commit 3861a59

Browse files
committed
Refactor web configuration management by creating dedicated handlers and utility functions for create, update, delete, reorder, and toggle pinned operations
- the core api configuration reorder logic should be moved to `apps/editor/src/views/utils` - the core web configuration deletion logic should moved to `apps/editor/src/views/utils` - `apps/editor/src/views/panel/backend/message-handlers/handle-delete-web-configuration.ts` doesnt have to call send_web_configurations_to_webview. simplify delete_web_configuration to return void - instead of 'replace web configurations' we should have two separate backend handlers: 'toggle pinned web configuration' and 'reorder web configurations' - delete web configuration by name (refer to as simple 'name' not 'web configuration name' - in settings, handle upsert web configuration (for now just for creation, don't handle any editing) - instead of 'upsert web configuration' should have two separate handlers 'create web configuration' and 'update web configuration'. Because updating is panel-only, locate the logic within handler - without the use of shared util.
1 parent e120010 commit 3861a59

27 files changed

Lines changed: 563 additions & 455 deletions

apps/editor/src/views/panel/backend/message-handlers/handle-upsert-web-configuration.ts renamed to apps/editor/src/views/panel/backend/message-handlers/handle-create-web-configuration.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { PanelProvider } from '@/views/panel/backend/panel-provider'
2-
import { UpsertWebConfigurationMessage } from '@/views/panel/types/messages'
3-
import { upsert_web_configuration } from '@/views/utils/upsert-web-configuration'
2+
import { CreateWebConfigurationMessage } from '@/views/panel/types/messages'
3+
import { create_web_configuration } from '@/views/utils/create-web-configuration'
44

5-
export const handle_upsert_web_configuration = async (
5+
export const handle_create_web_configuration = async (
66
panel_provider: PanelProvider,
7-
message: UpsertWebConfigurationMessage
7+
message: CreateWebConfigurationMessage
88
): Promise<void> => {
9-
const new_config = await upsert_web_configuration(message)
9+
const new_config = await create_web_configuration(message)
1010

1111
if (new_config && new_config.name) {
1212
panel_provider.send_message({
Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,8 @@
1-
import * as vscode from 'vscode'
2-
import { PanelProvider } from '@/views/panel/backend/panel-provider'
3-
import { dictionary } from '@shared/constants/dictionary'
41
import { DeleteWebConfigurationMessage } from '@/views/panel/types/messages'
5-
import { ConfigWebConfigurationFormat } from '@/views/utils/web-configuration-format-converters'
2+
import { delete_web_configuration } from '@/views/utils/delete-web-configuration'
63

74
export const handle_delete_web_configuration = async (
8-
panel_provider: PanelProvider,
9-
message: DeleteWebConfigurationMessage,
10-
webview_view: vscode.WebviewView
5+
message: DeleteWebConfigurationMessage
116
): Promise<void> => {
12-
const { index } = message
13-
const config = vscode.workspace.getConfiguration('codeWebChat')
14-
const current_web_configurations =
15-
config.get<ConfigWebConfigurationFormat[]>('webConfigurations', []) || []
16-
17-
if (index < 0 || index >= current_web_configurations.length) return
18-
19-
const item_to_delete = current_web_configurations[index]
20-
const item_name = item_to_delete.name
21-
const is_unnamed = !item_name || /^\(\d+\)$/.test(item_name?.trim() ?? '')
22-
const display_item_name = is_unnamed ? 'Unnamed' : item_name!
23-
24-
const delete_button = 'Delete'
25-
const result = await vscode.window.showWarningMessage(
26-
dictionary.warning_message.PLEASE_CONFIRM,
27-
{
28-
modal: true,
29-
detail: is_unnamed
30-
? dictionary.warning_message.CONFIRM_DELETE_ITEM('configuration')
31-
: dictionary.warning_message.CONFIRM_DELETE_NAMED_ITEM('web configuration', display_item_name)
32-
},
33-
delete_button
34-
)
35-
36-
if (result != delete_button) {
37-
return
38-
}
39-
40-
const updated_web_configurations = [...current_web_configurations]
41-
updated_web_configurations.splice(index, 1)
42-
43-
try {
44-
await config.update(
45-
'webConfigurations',
46-
updated_web_configurations,
47-
vscode.ConfigurationTarget.Global
48-
)
49-
50-
panel_provider.send_web_configurations_to_webview(webview_view.webview)
51-
} catch (error) {
52-
vscode.window.showErrorMessage(
53-
dictionary.error_message.FAILED_TO_DELETE_ITEM('web configuration', error)
54-
)
55-
}
7+
await delete_web_configuration({ name: message.name })
568
}
Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
import { PanelProvider } from '@/views/panel/backend/panel-provider'
22
import { ReorderApiConfigurationsMessage } from '@/views/panel/types/messages'
3-
import {
4-
ModelProvidersManager,
5-
ApiConfiguration
6-
} from '@/services/model-providers-manager'
7-
import { get_api_configuration_id } from '@/services/model-providers-manager'
3+
import { reorder_api_configurations } from '@/views/utils/reorder-api-configurations'
84

95
export const handle_reorder_api_configurations = async (
106
panel_provider: PanelProvider,
117
message: ReorderApiConfigurationsMessage
128
): Promise<void> => {
13-
const providers_manager = new ModelProvidersManager(panel_provider.context)
149
const reordered_ids = message.configurations.map((p) => p.id)
15-
16-
const current_api_configurations = await providers_manager.get_api_configurations()
17-
const reordered_api_configurations = reordered_ids
18-
.map((id) => {
19-
const found = current_api_configurations.find((p) => get_api_configuration_id(p) === id)
20-
if (!found) {
21-
console.error(`Config with id ${id} not found during reorder.`)
22-
return null
23-
}
24-
return found
25-
})
26-
.filter((p): p is ApiConfiguration => p !== null)
27-
28-
if (reordered_api_configurations.length === current_api_configurations.length) {
29-
await providers_manager.save_api_configurations(reordered_api_configurations)
30-
}
10+
await reorder_api_configurations({ context: panel_provider.context, reordered_ids })
3111
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ReorderWebConfigurationsMessage } from '@/views/panel/types/messages'
2+
import { reorder_web_configurations } from '@/views/utils/reorder-web-configurations'
3+
4+
export const handle_reorder_web_configurations = async (
5+
message: ReorderWebConfigurationsMessage
6+
): Promise<void> => {
7+
await reorder_web_configurations({
8+
reordered_web_configurations: message.web_configurations
9+
})
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { TogglePinnedWebConfigurationMessage } from '@/views/panel/types/messages'
2+
import { toggle_pinned_web_configuration } from '@/views/utils/toggle-pinned-web-configuration'
3+
4+
export const handle_toggle_pinned_web_configuration = async (
5+
message: TogglePinnedWebConfigurationMessage
6+
): Promise<void> => {
7+
await toggle_pinned_web_configuration({
8+
web_configuration_name: message.web_configuration_name
9+
})
10+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}

‎apps/editor/src/views/panel/backend/message-handlers/index.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export * from './handle-request-editor-state'
1212
export * from './handle-save-edit-format'
1313
export * from './handle-save-history'
1414
export * from './handle-save-instructions'
15-
export * from './handle-replace-web-configurations'
15+
export * from './handle-reorder-web-configurations'
16+
export * from './handle-toggle-pinned-web-configuration'
1617
export * from './handle-send-to-browser'
1718
export * from './handle-show-prompt-template-quick-pick'
18-
export * from './handle-upsert-web-configuration'
19+
export * from './handle-create-web-configuration'
20+
export * from './handle-update-web-configuration'
1921
export * from './handle-pick-chatbot'
2022
export * from './handle-pick-model'
2123
export * from './handle-pick-reasoning-effort'

‎apps/editor/src/views/panel/backend/panel-provider.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ import {
1818
import {
1919
handle_copy_prompt,
2020
handle_send_to_browser,
21-
handle_upsert_web_configuration,
21+
handle_create_web_configuration,
22+
handle_update_web_configuration,
2223
handle_delete_web_configuration,
2324
handle_create_checkpoint,
2425
handle_clear_all_checkpoints,
2526
handle_duplicate_web_configuration,
2627
handle_preview_web_configuration,
2728
handle_save_edit_format,
28-
handle_replace_web_configurations,
29+
handle_reorder_web_configurations,
30+
handle_toggle_pinned_web_configuration,
2931
handle_get_connection_status,
3032
handle_get_history,
3133
handle_apply_response_from_history,
@@ -785,14 +787,18 @@ export class PanelProvider implements vscode.WebviewViewProvider {
785787
this._send_context_size_warning_threshold()
786788
} else if (message.command == 'REQUEST_CURRENTLY_OPEN_FILE_TEXT') {
787789
this.send_currently_open_file_text()
788-
} else if (message.command == 'REPLACE_WEB_CONFIGURATIONS') {
789-
await handle_replace_web_configurations(message)
790+
} else if (message.command == 'REORDER_WEB_CONFIGURATIONS') {
791+
await handle_reorder_web_configurations(message)
792+
} else if (message.command == 'TOGGLE_PINNED_WEB_CONFIGURATION') {
793+
await handle_toggle_pinned_web_configuration(message)
790794
} else if (message.command == 'GET_SEND_WITH_SHIFT_ENTER') {
791795
this._send_send_with_shift_enter()
792-
} else if (message.command == 'UPSERT_WEB_CONFIGURATION') {
793-
await handle_upsert_web_configuration(this, message)
796+
} else if (message.command == 'CREATE_WEB_CONFIGURATION') {
797+
await handle_create_web_configuration(this, message)
798+
} else if (message.command == 'UPDATE_WEB_CONFIGURATION') {
799+
await handle_update_web_configuration(this, message)
794800
} else if (message.command == 'DELETE_WEB_CONFIGURATION') {
795-
await handle_delete_web_configuration(this, message, webview_view)
801+
await handle_delete_web_configuration( message)
796802
} else if (message.command == 'DUPLICATE_WEB_CONFIGURATION') {
797803
await handle_duplicate_web_configuration(
798804
this,

0 commit comments

Comments
 (0)