Skip to content

Commit a0b27d9

Browse files
committed
Refactor configuration splitting logic into a shared utility function to reduce duplication across message handlers
- the quick pick of recent web configurations should behave like the one with recent api configurations - extract the logic of finding recent/rest configurations to `apps/editor/src/views/panel/backend/utils`
1 parent 806dd4b commit a0b27d9

6 files changed

Lines changed: 148 additions & 151 deletions

File tree

‎apps/editor/src/views/panel/backend/message-handlers/handle-code-at-cursor.ts‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { dictionary } from '@shared/constants/dictionary'
1717
import { randomUUID } from 'crypto'
1818
import { build_user_content } from '@/utils/build-user-content'
1919
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
20+
import { split_recent_and_rest_configurations } from '@/views/panel/backend/utils/split-recent-and-rest-configurations'
2021

2122
const get_code_at_cursor_api_configuration = async (
2223
api_providers_manager: ModelProvidersManager,
@@ -74,26 +75,12 @@ const get_code_at_cursor_api_configuration = async (
7475
RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY
7576
) || []
7677

77-
const matched_recent_api_configurations: ApiConfiguration[] = []
78-
const remaining_api_configurations: ApiConfiguration[] = []
79-
80-
code_completions_api_configurations.forEach((api_configuration) => {
81-
const id = get_api_configuration_id(api_configuration)
82-
if (recent_ids.includes(id)) {
83-
matched_recent_api_configurations.push(api_configuration)
84-
} else {
85-
remaining_api_configurations.push(api_configuration)
86-
}
87-
})
88-
89-
matched_recent_api_configurations.sort((a, b) => {
90-
const idA = get_api_configuration_id(a)
91-
const idB = get_api_configuration_id(b)
92-
return recent_ids.indexOf(idA) - recent_ids.indexOf(idB)
93-
})
94-
95-
const recent_api_configurations = matched_recent_api_configurations
96-
const other_api_configurations = remaining_api_configurations
78+
const { recent: recent_api_configurations, rest: other_api_configurations } =
79+
split_recent_and_rest_configurations(
80+
code_completions_api_configurations,
81+
recent_ids,
82+
get_api_configuration_id
83+
)
9784

9885
const map_api_configuration_to_item = (api_configuration: ApiConfiguration) => {
9986
const description_parts = [api_configuration.model_provider_name]

‎apps/editor/src/views/panel/backend/message-handlers/handle-edit-context.ts‎

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { default_system_instructions } from '@shared/constants/default-system-instructions'
2727
import { build_user_content } from '@/utils/build-user-content'
2828
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
29+
import { split_recent_and_rest_configurations } from '@/views/panel/backend/utils/split-recent-and-rest-configurations'
2930

3031
const get_edit_context_api_configuration = async (params: {
3132
api_providers_manager: ModelProvidersManager
@@ -99,26 +100,12 @@ const get_edit_context_api_configuration = async (params: {
99100
RECENTLY_USED_EDIT_CONTEXT_CONFIG_IDS_STATE_KEY
100101
) || []
101102

102-
const matched_recent_api_configurations: ApiConfiguration[] = []
103-
const remaining_api_configurations: ApiConfiguration[] = []
104-
105-
edit_context_api_configurations.forEach((api_configuration) => {
106-
const id = get_api_configuration_id(api_configuration)
107-
if (recent_ids.includes(id)) {
108-
matched_recent_api_configurations.push(api_configuration)
109-
} else {
110-
remaining_api_configurations.push(api_configuration)
111-
}
112-
})
113-
114-
matched_recent_api_configurations.sort((a, b) => {
115-
const idA = get_api_configuration_id(a)
116-
const idB = get_api_configuration_id(b)
117-
return recent_ids.indexOf(idA) - recent_ids.indexOf(idB)
118-
})
119-
120-
const recent_api_configurations = matched_recent_api_configurations
121-
const other_api_configurations = remaining_api_configurations
103+
const { recent: recent_api_configurations, rest: other_api_configurations } =
104+
split_recent_and_rest_configurations(
105+
edit_context_api_configurations,
106+
recent_ids,
107+
get_api_configuration_id
108+
)
122109

123110
const map_api_configuration_to_item = (api_configuration: ApiConfiguration) => {
124111
const description_parts = [api_configuration.model_provider_name]
@@ -153,7 +140,7 @@ const get_edit_context_api_configuration = async (params: {
153140
if (other_api_configurations.length > 0) {
154141
if (recent_api_configurations.length > 0) {
155142
items.push({
156-
label: 'other API configurations',
143+
label: 'other configurations',
157144
kind: vscode.QuickPickItemKind.Separator
158145
})
159146
}
@@ -165,8 +152,8 @@ const get_edit_context_api_configuration = async (params: {
165152

166153
const quick_pick = vscode.window.createQuickPick<Item>()
167154
quick_pick.items = await create_items()
168-
quick_pick.title = 'API Configurations'
169-
quick_pick.placeholder = 'Select an API configuration'
155+
quick_pick.title = 'Configurations'
156+
quick_pick.placeholder = 'Select a configuration'
170157
quick_pick.matchOnDescription = true
171158
quick_pick.buttons = [
172159
{

‎apps/editor/src/views/panel/backend/message-handlers/handle-find-relevant-files.ts‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from '@/constants/instructions'
2323
import { build_user_content } from '@/utils/build-user-content'
2424
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
25+
import { split_recent_and_rest_configurations } from '@/views/panel/backend/utils/split-recent-and-rest-configurations'
2526

2627
const get_find_relevant_files_api_configuration = async (params: {
2728
api_providers_manager: ModelProvidersManager
@@ -95,26 +96,12 @@ const get_find_relevant_files_api_configuration = async (params: {
9596
RECENTLY_USED_FIND_RELEVANT_FILES_CONFIG_IDS_STATE_KEY
9697
) || []
9798

98-
const matched_recent_api_configurations: ApiConfiguration[] = []
99-
const remaining_api_configurations: ApiConfiguration[] = []
100-
101-
find_relevant_files_api_configurations.forEach((api_configuration) => {
102-
const id = get_api_configuration_id(api_configuration)
103-
if (recent_ids.includes(id)) {
104-
matched_recent_api_configurations.push(api_configuration)
105-
} else {
106-
remaining_api_configurations.push(api_configuration)
107-
}
108-
})
109-
110-
matched_recent_api_configurations.sort((a, b) => {
111-
const idA = get_api_configuration_id(a)
112-
const idB = get_api_configuration_id(b)
113-
return recent_ids.indexOf(idA) - recent_ids.indexOf(idB)
114-
})
115-
116-
const recent_api_configurations = matched_recent_api_configurations
117-
const other_api_configurations = remaining_api_configurations
99+
const { recent: recent_api_configurations, rest: other_api_configurations } =
100+
split_recent_and_rest_configurations(
101+
find_relevant_files_api_configurations,
102+
recent_ids,
103+
get_api_configuration_id
104+
)
118105

119106
const map_api_configuration_to_item = (api_configuration: ApiConfiguration) => {
120107
const description_parts = [api_configuration.model_provider_name]
@@ -161,8 +148,8 @@ const get_find_relevant_files_api_configuration = async (params: {
161148

162149
const quick_pick = vscode.window.createQuickPick<Item>()
163150
quick_pick.items = await create_items()
164-
quick_pick.title = 'API Configurations'
165-
quick_pick.placeholder = 'Select an API configuration'
151+
quick_pick.title = 'Configurations'
152+
quick_pick.placeholder = 'Select a configuration'
166153
quick_pick.matchOnDescription = true
167154
quick_pick.buttons = [
168155
{

‎apps/editor/src/views/panel/backend/message-handlers/handle-send-to-browser.ts‎

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '@/constants/edit-format-instructions'
2424
import { handle_update_last_used_web_configuration_or_group } from './handle-update-last-used-web-configuration-or-group'
2525
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
26+
import { split_recent_and_rest_configurations } from '@/views/panel/backend/utils/split-recent-and-rest-configurations'
2627

2728
export const handle_send_to_browser = async (params: {
2829
panel_provider: PanelProvider
@@ -227,6 +228,14 @@ const show_web_configuration_quick_pick = async (params: {
227228
}): Promise<{ web_configuration_name: string | undefined } | null> => {
228229
const { web_configurations, context, prompt_type, panel_provider } = params
229230

231+
const valid_web_configurations = web_configurations.filter((c) => c.chatbot)
232+
233+
if (valid_web_configurations.length == 0) {
234+
vscode.commands.executeCommand('codeWebChat.settings')
235+
vscode.window.showInformationMessage('No configurations found.')
236+
return null
237+
}
238+
230239
const quick_pick = vscode.window.createQuickPick<
231240
vscode.QuickPickItem & { web_configuration_name?: string }
232241
>()
@@ -237,50 +246,55 @@ const show_web_configuration_quick_pick = async (params: {
237246
context.globalState.get<string[]>(recents_key) ??
238247
[]
239248

240-
if (recent_names.length == 0) {
241-
vscode.window.showWarningMessage(
242-
dictionary.warning_message.NO_RECENTLY_USED_PRESETS_OR_GROUPS // the dictionary key is kept, string may differ
243-
)
244-
return null
249+
const { recent: matched_recent, rest: remaining } = split_recent_and_rest_configurations(
250+
valid_web_configurations,
251+
recent_names,
252+
(c) => c.name
253+
)
254+
255+
const map_web_configuration_to_item = (web_configuration: ConfigWebConfigurationFormat) => {
256+
const is_unnamed = !web_configuration.name || /^\(\d+\)$/.test(web_configuration.name.trim())
257+
const chatbot_models =
258+
CHATBOTS[web_configuration.chatbot as keyof typeof CHATBOTS]?.models
259+
const model = web_configuration.model
260+
? chatbot_models?.[web_configuration.model]?.label || web_configuration.model
261+
: ''
262+
return {
263+
label: `${
264+
is_unnamed
265+
? web_configuration.chatbot!
266+
: web_configuration.name!.replace(/\s*\(\d+\)$/, '')
267+
}`,
268+
web_configuration_name: web_configuration.name,
269+
description: is_unnamed
270+
? model
271+
: `${web_configuration.chatbot}${model ? ` · ${model}` : ''}`
272+
}
245273
}
246274

247-
const items = recent_names
248-
.map((name) => {
249-
const item = web_configurations.find((p) => p.name == name)
250-
251-
if (item && item.chatbot) {
252-
const web_configuration = item
253-
const is_unnamed = !web_configuration.name || /^\(\d+\)$/.test(web_configuration.name.trim())
254-
const chatbot_models =
255-
CHATBOTS[web_configuration.chatbot as keyof typeof CHATBOTS]?.models
256-
const model = web_configuration.model
257-
? chatbot_models?.[web_configuration.model]?.label || web_configuration.model
258-
: ''
259-
return {
260-
label: `${
261-
is_unnamed
262-
? web_configuration.chatbot!
263-
: web_configuration.name!.replace(/\s*\(\d+\)$/, '')
264-
}`,
265-
web_configuration_name: web_configuration.name,
266-
description: is_unnamed
267-
? model
268-
: `${web_configuration.chatbot}${model ? ` · ${model}` : ''}`
269-
}
270-
}
271-
return null
275+
const items: (vscode.QuickPickItem & { web_configuration_name?: string })[] = []
276+
277+
if (matched_recent.length > 0) {
278+
items.push({
279+
label: 'recently used',
280+
kind: vscode.QuickPickItemKind.Separator
272281
})
273-
.filter((i): i is NonNullable<typeof i> => i !== null)
282+
items.push(...matched_recent.map(map_web_configuration_to_item))
283+
}
274284

275-
quick_pick.items = items
276-
if (items.length == 0) {
277-
vscode.window.showWarningMessage(
278-
dictionary.warning_message.NO_RECENTLY_USED_PRESETS_OR_GROUPS
279-
)
280-
return null
285+
if (remaining.length > 0) {
286+
if (matched_recent.length > 0) {
287+
items.push({
288+
label: 'other configurations',
289+
kind: vscode.QuickPickItemKind.Separator
290+
})
291+
}
292+
items.push(...remaining.map(map_web_configuration_to_item))
281293
}
282-
quick_pick.placeholder = 'Search recently used presets'
283-
quick_pick.title = 'Recently Used Presets'
294+
295+
quick_pick.items = items
296+
quick_pick.placeholder = 'Select a configuration'
297+
quick_pick.title = 'Configurations'
284298
quick_pick.matchOnDescription = true
285299

286300
const close_button = {
@@ -297,17 +311,27 @@ const show_web_configuration_quick_pick = async (params: {
297311

298312
const last_selected_name = recent_names[0]
299313

300-
let last_selected_web_configuration_name: string | undefined
301314
if (last_selected_name) {
302-
const item = web_configurations.find((p) => p.name == last_selected_name)
303-
if (item && item.chatbot) last_selected_web_configuration_name = last_selected_name
304-
}
305-
306-
if (last_selected_web_configuration_name) {
307315
const last_item = quick_pick.items.find(
308-
(item) => item.web_configuration_name == last_selected_web_configuration_name
316+
(item) => item.web_configuration_name == last_selected_name
309317
)
310-
if (last_item) quick_pick.activeItems = [last_item]
318+
if (last_item) {
319+
quick_pick.activeItems = [last_item]
320+
} else if (items.length > 0) {
321+
const first_selectable = items.find(
322+
(i) => i.kind != vscode.QuickPickItemKind.Separator
323+
)
324+
if (first_selectable) {
325+
quick_pick.activeItems = [first_selectable]
326+
}
327+
}
328+
} else if (items.length > 0) {
329+
const first_selectable = items.find(
330+
(i) => i.kind != vscode.QuickPickItemKind.Separator
331+
)
332+
if (first_selectable) {
333+
quick_pick.activeItems = [first_selectable]
334+
}
311335
}
312336

313337
return new Promise<{ web_configuration_name: string | undefined } | null>((resolve) => {
@@ -322,36 +346,31 @@ const show_web_configuration_quick_pick = async (params: {
322346
quick_pick.onDidAccept(async () => {
323347
const selected = quick_pick.selectedItems[0] as any
324348

325-
if (!selected) {
349+
if (!selected || !selected.web_configuration_name) {
326350
quick_pick.hide()
327351
do_resolve(null)
328352
return
329353
}
330354

331-
if (selected.web_configuration_name) {
332-
const web_configuration = web_configurations.find((p) => p.name == selected.web_configuration_name)!
333-
if (params.get_is_web_configuration_disabled(web_configuration)) {
334-
if (
335-
!params.is_in_code_completions_mode &&
336-
!params.current_instructions
337-
) {
338-
params.panel_provider.send_message({
339-
command: 'SHOW_AUTO_CLOSING_MODAL',
340-
title: 'Type something to use this web configuration',
341-
type: 'warning'
342-
})
343-
}
344-
do_resolve(null)
345-
} else {
346-
handle_update_last_used_web_configuration_or_group({
347-
panel_provider,
348-
web_configuration_name: selected.web_configuration_name
355+
const web_configuration = valid_web_configurations.find((p) => p.name == selected.web_configuration_name)!
356+
if (params.get_is_web_configuration_disabled(web_configuration)) {
357+
if (
358+
!params.is_in_code_completions_mode &&
359+
!params.current_instructions
360+
) {
361+
params.panel_provider.send_message({
362+
command: 'SHOW_AUTO_CLOSING_MODAL',
363+
title: 'Type something to use this web configuration',
364+
type: 'warning'
349365
})
350-
do_resolve({ web_configuration_name: selected.web_configuration_name })
351366
}
367+
do_resolve(null)
352368
} else {
353-
quick_pick.hide()
354-
do_resolve({ web_configuration_name: undefined })
369+
handle_update_last_used_web_configuration_or_group({
370+
panel_provider,
371+
web_configuration_name: selected.web_configuration_name
372+
})
373+
do_resolve({ web_configuration_name: selected.web_configuration_name })
355374
}
356375
})
357376

0 commit comments

Comments
 (0)