Skip to content

Commit b79d0b2

Browse files
committed
Refactor API configuration selection to use a shared quick pick utility and transition from tracking recently used lists to storing only the last used configuration ID
- Instead of storing a list of recent configurations, store only one item. The quick pick should not show a list of recent configurations, but highlight the recently used one. - in quick picks, show pinned configurations above without filtering them out from the overall list of configurations. - in `apps/editor/src/utils` create util that handles logic of showing configurations in a quick pick
1 parent 056ee0d commit b79d0b2

14 files changed

Lines changed: 366 additions & 1287 deletions

‎apps/editor/src/commands/code-at-cursor-command/utils/get-code-at-cursor-config.ts‎

Lines changed: 26 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import {
55
ApiConfiguration
66
} from '../../../services/model-providers-manager'
77
import { Logger } from '@shared/utils/logger'
8-
import { RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY } from '@/constants/state-keys'
8+
import { LAST_USED_CODE_AT_CURSOR_CONFIG_ID_STATE_KEY } from '@/constants/state-keys'
99
import { PanelProvider } from '@/views/panel/backend/panel-provider'
1010
import { dictionary } from '@shared/constants/dictionary'
1111
import { t } from '@/i18n'
12+
import { show_api_configuration_quick_pick } from '@/utils/show-api-configuration-quick-pick'
1213

1314
export const get_code_at_cursor_api_configuration = async (params: {
1415
model_providers_manager: ModelProvidersManager
@@ -41,10 +42,9 @@ export const get_code_at_cursor_api_configuration = async (params: {
4142
if (default_api_configuration) {
4243
selected_api_configuration = default_api_configuration
4344
} else {
44-
const recents = params.context.workspaceState.get<string[]>(
45-
RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY
45+
const last_selected_id = params.context.workspaceState.get<string>(
46+
LAST_USED_CODE_AT_CURSOR_CONFIG_ID_STATE_KEY
4647
)
47-
const last_selected_id = recents?.[0]
4848
if (last_selected_id) {
4949
selected_api_configuration =
5050
code_at_cursor_api_configurations.find(
@@ -61,167 +61,36 @@ export const get_code_at_cursor_api_configuration = async (params: {
6161
}
6262

6363
if (!selected_api_configuration || params.show_quick_pick) {
64-
const create_items = () => {
65-
const recent_ids =
66-
params.context.workspaceState.get<string[]>(
67-
RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY
68-
) || []
69-
70-
const matched_recent_api_configurations: ApiConfiguration[] = []
71-
const remaining_api_configurations: ApiConfiguration[] = []
72-
73-
code_at_cursor_api_configurations.forEach((api_configuration) => {
74-
const id = get_api_configuration_id(api_configuration)
75-
if (recent_ids.includes(id)) {
76-
matched_recent_api_configurations.push(api_configuration)
77-
} else {
78-
remaining_api_configurations.push(api_configuration)
79-
}
80-
})
81-
82-
matched_recent_api_configurations.sort((a, b) => {
83-
const id_a = get_api_configuration_id(a)
84-
const id_b = get_api_configuration_id(b)
85-
return recent_ids.indexOf(id_a) - recent_ids.indexOf(id_b)
86-
})
87-
88-
const recent_api_configurations = matched_recent_api_configurations
89-
const other_api_configurations = remaining_api_configurations
90-
91-
const map_api_configuration_to_item = (
92-
api_configuration: ApiConfiguration
93-
) => {
94-
const description_parts = [api_configuration.model_provider_name]
95-
if (api_configuration.reasoning_effort) {
96-
description_parts.push(`${api_configuration.reasoning_effort}`)
97-
}
98-
99-
const buttons: vscode.QuickInputButton[] = []
100-
101-
return {
102-
label: api_configuration.model,
103-
description: description_parts.join(' · '),
104-
buttons,
105-
api_configuration,
106-
id: get_api_configuration_id(api_configuration)
107-
}
108-
}
109-
110-
const items: (vscode.QuickPickItem & {
111-
api_configuration?: ApiConfiguration
112-
id?: string
113-
})[] = []
114-
115-
if (recent_api_configurations.length > 0) {
116-
items.push({
117-
label: t('common.separator.recently-used'),
118-
kind: vscode.QuickPickItemKind.Separator
119-
})
120-
items.push(
121-
...recent_api_configurations.map(map_api_configuration_to_item)
122-
)
123-
}
124-
125-
if (other_api_configurations.length > 0) {
126-
if (recent_api_configurations.length > 0) {
127-
items.push({
128-
label: t('common.config.other'),
129-
kind: vscode.QuickPickItemKind.Separator
130-
})
131-
}
132-
items.push(
133-
...other_api_configurations.map(map_api_configuration_to_item)
134-
)
135-
}
136-
137-
return items
138-
}
139-
140-
const quick_pick = vscode.window.createQuickPick()
141-
quick_pick.buttons = [
142-
{
143-
iconPath: new vscode.ThemeIcon('close'),
144-
tooltip: t('common.close')
145-
}
146-
]
147-
quick_pick.items = create_items()
148-
quick_pick.title = t('common.config.title')
149-
quick_pick.placeholder = t('command.code-at-cursor.config.placeholder')
150-
quick_pick.matchOnDescription = true
64+
const last_selected_id = params.context.workspaceState.get<string>(
65+
LAST_USED_CODE_AT_CURSOR_CONFIG_ID_STATE_KEY
66+
)
15167

152-
const items = quick_pick.items as (vscode.QuickPickItem & { id: string })[]
68+
const result = await show_api_configuration_quick_pick({
69+
api_configurations: code_at_cursor_api_configurations,
70+
last_selected_id,
71+
placeholder: t('command.code-at-cursor.config.placeholder')
72+
})
15373

154-
if (items.length > 0) {
155-
const first_selectable = items.find(
156-
(i) => i.kind != vscode.QuickPickItemKind.Separator
157-
)
158-
if (first_selectable) {
159-
quick_pick.activeItems = [first_selectable]
160-
}
74+
if (!result || result === 'back') {
75+
return undefined
16176
}
16277

163-
return new Promise<
164-
{ model_provider: any; api_configuration: any } | undefined
165-
>((resolve) => {
166-
quick_pick.onDidTriggerButton((button) => {
167-
if (button.tooltip == t('common.close')) {
168-
quick_pick.hide()
169-
resolve(undefined)
170-
}
171-
})
172-
173-
quick_pick.onDidAccept(async () => {
174-
const selected = quick_pick.selectedItems[0] as any
175-
quick_pick.hide()
78+
const { api_configuration, id } = result
17679

177-
if (!selected || !selected.api_configuration) {
178-
resolve(undefined)
179-
return
180-
}
181-
182-
let recents =
183-
params.context.workspaceState.get<string[]>(
184-
RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY
185-
) || []
186-
recents = [selected.id, ...recents.filter((id) => id != selected.id)]
187-
params.context.workspaceState.update(
188-
RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY,
189-
recents
190-
)
191-
192-
if (params.panel_provider) {
193-
params.panel_provider.send_message({
194-
command: 'SELECTED_API_CONFIGURATION_CHANGED',
195-
prompt_type: 'code-at-cursor',
196-
id: selected.id
197-
})
198-
}
199-
200-
const model_provider =
201-
await params.model_providers_manager.get_model_provider(
202-
selected.api_configuration.model_provider_name
203-
)
204-
if (!model_provider) {
205-
vscode.window.showErrorMessage(
206-
dictionary.error_message.API_PROVIDER_NOT_FOUND
207-
)
208-
resolve(undefined)
209-
return
210-
}
211-
212-
resolve({
213-
model_provider,
214-
api_configuration: selected.api_configuration
215-
})
216-
})
80+
params.context.workspaceState.update(
81+
LAST_USED_CODE_AT_CURSOR_CONFIG_ID_STATE_KEY,
82+
id
83+
)
21784

218-
quick_pick.onDidHide(() => {
219-
quick_pick.dispose()
220-
resolve(undefined)
85+
if (params.panel_provider) {
86+
params.panel_provider.send_message({
87+
command: 'SELECTED_API_CONFIGURATION_CHANGED',
88+
prompt_type: 'code-at-cursor',
89+
id: id
22190
})
91+
}
22292

223-
quick_pick.show()
224-
})
93+
selected_api_configuration = api_configuration
22594
}
22695

22796
const model_provider =

‎apps/editor/src/commands/search-files-command/utils/prompt-for-config.ts‎

Lines changed: 18 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
ApiConfiguration,
66
ModelProvider
77
} from '../../../services/model-providers-manager'
8-
import { RECENTLY_USED_FIND_RELEVANT_FILES_CONFIG_IDS_STATE_KEY } from '../../../constants/state-keys'
8+
import { LAST_USED_FIND_RELEVANT_FILES_CONFIG_ID_STATE_KEY } from '../../../constants/state-keys'
99
import { display_token_count } from '../../../utils/display-token-count'
1010
import { t } from '@/i18n'
11+
import { show_api_configuration_quick_pick } from '../../../utils/show-api-configuration-quick-pick'
1112

1213
export const prompt_for_api_configuration = async (params: {
1314
model_providers_manager: ModelProvidersManager
@@ -41,143 +42,32 @@ export const prompt_for_api_configuration = async (params: {
4142
}
4243

4344
if (!selected_api_configuration) {
44-
const create_items = () => {
45-
const recent_ids =
46-
params.extension_context.workspaceState.get<string[]>(
47-
RECENTLY_USED_FIND_RELEVANT_FILES_CONFIG_IDS_STATE_KEY
48-
) || []
49-
const matched_recent_api_configurations: ApiConfiguration[] = []
50-
const remaining_api_configurations: ApiConfiguration[] = []
51-
52-
params.api_configurations.forEach(
53-
(api_configuration: ApiConfiguration) => {
54-
const id = get_api_configuration_id(api_configuration)
55-
if (recent_ids.includes(id)) {
56-
matched_recent_api_configurations.push(api_configuration)
57-
} else {
58-
remaining_api_configurations.push(api_configuration)
59-
}
60-
}
45+
const last_selected_id =
46+
params.extension_context.workspaceState.get<string>(
47+
LAST_USED_FIND_RELEVANT_FILES_CONFIG_ID_STATE_KEY
6148
)
6249

63-
matched_recent_api_configurations.sort((a, b) => {
64-
const id_a = get_api_configuration_id(a)
65-
const id_b = get_api_configuration_id(b)
66-
return recent_ids.indexOf(id_a) - recent_ids.indexOf(id_b)
67-
})
68-
69-
const map_api_configuration_to_item = (
70-
api_configuration: ApiConfiguration
71-
) => {
72-
const description_parts = [api_configuration.model_provider_name]
73-
if (api_configuration.reasoning_effort)
74-
description_parts.push(`${api_configuration.reasoning_effort}`)
75-
76-
return {
77-
label: api_configuration.model,
78-
description: description_parts.join(' · '),
79-
api_configuration,
80-
id: get_api_configuration_id(api_configuration)
81-
}
82-
}
83-
84-
const items: (vscode.QuickPickItem & {
85-
api_configuration?: ApiConfiguration
86-
id?: string
87-
})[] = []
88-
89-
if (matched_recent_api_configurations.length > 0) {
90-
items.push({
91-
label: t('common.separator.recently-used'),
92-
kind: vscode.QuickPickItemKind.Separator
93-
})
94-
items.push(
95-
...matched_recent_api_configurations.map(
96-
map_api_configuration_to_item
97-
)
98-
)
99-
}
100-
if (remaining_api_configurations.length > 0) {
101-
if (matched_recent_api_configurations.length > 0) {
102-
items.push({
103-
label: t('common.config.other'),
104-
kind: vscode.QuickPickItemKind.Separator
105-
})
106-
}
107-
items.push(
108-
...remaining_api_configurations.map(map_api_configuration_to_item)
109-
)
110-
}
111-
return items
112-
}
113-
114-
const api_configuration_quick_pick = vscode.window.createQuickPick()
115-
api_configuration_quick_pick.items = create_items()
116-
api_configuration_quick_pick.title = t('common.config.title')
117-
api_configuration_quick_pick.placeholder = t(
118-
'common.config.placeholder-with-tokens',
119-
{
120-
tokens: display_token_count(params.tokens_to_process)
121-
}
122-
)
123-
api_configuration_quick_pick.matchOnDescription = true
124-
api_configuration_quick_pick.buttons = [vscode.QuickInputButtons.Back]
125-
126-
const items =
127-
api_configuration_quick_pick.items as (vscode.QuickPickItem & {
128-
id?: string
129-
})[]
130-
const first_selectable = items.find(
131-
(i) => i.kind !== vscode.QuickPickItemKind.Separator
132-
)
133-
if (first_selectable) {
134-
api_configuration_quick_pick.activeItems = [first_selectable]
135-
}
50+
const placeholder = t('common.config.placeholder-with-tokens', {
51+
tokens: display_token_count(params.tokens_to_process)
52+
})
13653

137-
const api_configuration_result = await new Promise<
138-
ApiConfiguration | 'back' | 'cancel'
139-
>((resolve) => {
140-
let is_resolved = false
141-
api_configuration_quick_pick.onDidTriggerButton((button) => {
142-
if (button === vscode.QuickInputButtons.Back) {
143-
is_resolved = true
144-
resolve('back')
145-
api_configuration_quick_pick.hide()
146-
}
147-
})
148-
api_configuration_quick_pick.onDidAccept(() => {
149-
is_resolved = true
150-
const selected = api_configuration_quick_pick.selectedItems[0] as any
151-
resolve(selected?.api_configuration)
152-
api_configuration_quick_pick.hide()
153-
})
154-
api_configuration_quick_pick.onDidHide(() => {
155-
if (!is_resolved) {
156-
resolve('cancel')
157-
}
158-
api_configuration_quick_pick.dispose()
159-
})
160-
api_configuration_quick_pick.show()
54+
const result = await show_api_configuration_quick_pick({
55+
api_configurations: params.api_configurations,
56+
last_selected_id,
57+
placeholder,
58+
show_back_button: true
16159
})
16260

163-
if (api_configuration_result === 'back') return 'back'
164-
if (api_configuration_result === 'cancel') return 'cancel'
165-
selected_api_configuration = api_configuration_result
61+
if (result === 'back') return 'back'
62+
if (!result) return 'cancel'
63+
selected_api_configuration = result.api_configuration
16664
}
16765

16866
if (selected_api_configuration) {
16967
const selected_id = get_api_configuration_id(selected_api_configuration)
170-
const recents =
171-
params.extension_context.workspaceState.get<string[]>(
172-
RECENTLY_USED_FIND_RELEVANT_FILES_CONFIG_IDS_STATE_KEY
173-
) || []
174-
const updated_recents = [
175-
selected_id,
176-
...recents.filter((id) => id !== selected_id)
177-
]
17868
await params.extension_context.workspaceState.update(
179-
RECENTLY_USED_FIND_RELEVANT_FILES_CONFIG_IDS_STATE_KEY,
180-
updated_recents
69+
LAST_USED_FIND_RELEVANT_FILES_CONFIG_ID_STATE_KEY,
70+
selected_id
18171
)
18272
}
18373

0 commit comments

Comments
 (0)