Skip to content

Commit 1de631a

Browse files
committed
Add recently used configuration tracking to code completion and edit context quick picks
1 parent f596bb9 commit 1de631a

3 files changed

Lines changed: 161 additions & 17 deletions

File tree

‎packages/vscode/src/constants/state-keys.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export const PINNED_HISTORY_CODE_COMPLETIONS_STATE_KEY =
6565
'pinned-history-code-completions'
6666
export const PINNED_HISTORY_NO_CONTEXT_STATE_KEY = 'pinned-history-no-context'
6767

68+
export const RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY =
69+
'recently-used-code-completion-config-ids'
70+
export const RECENTLY_USED_EDIT_CONTEXT_CONFIG_IDS_STATE_KEY =
71+
'recently-used-edit-context-config-ids'
72+
6873
export const get_presets_collapsed_state_key = (web_prompt_type: string) =>
6974
`presets-collapsed-${web_prompt_type}`
7075
export const get_configurations_collapsed_state_key = (

‎packages/vscode/src/views/panel/backend/message-handlers/handle-code-completion.ts‎

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
} from '@/services/model-providers-manager'
1010
import { Logger } from '@shared/utils/logger'
1111
import { PROVIDERS } from '@shared/constants/providers'
12-
import { LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY } from '@/constants/state-keys'
12+
import {
13+
LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY,
14+
RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY
15+
} from '@/constants/state-keys'
1316
import { PanelProvider } from '@/views/panel/backend/panel-provider'
1417
import { CodeCompletionMessage } from '@/views/panel/types/messages'
1518
import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
@@ -81,7 +84,33 @@ const get_code_completion_config = async (
8184

8285
if (!selected_config || show_quick_pick) {
8386
const create_items = () => {
84-
return code_completions_configs.map((config: ToolConfig, index) => {
87+
const recent_ids =
88+
context.workspaceState.get<string[]>(
89+
RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY
90+
) || []
91+
92+
const matched_recent_configs: ToolConfig[] = []
93+
const remaining_configs: ToolConfig[] = []
94+
95+
code_completions_configs.forEach((config) => {
96+
const id = get_tool_config_id(config)
97+
if (recent_ids.includes(id)) {
98+
matched_recent_configs.push(config)
99+
} else {
100+
remaining_configs.push(config)
101+
}
102+
})
103+
104+
matched_recent_configs.sort((a, b) => {
105+
const idA = get_tool_config_id(a)
106+
const idB = get_tool_config_id(b)
107+
return recent_ids.indexOf(idA) - recent_ids.indexOf(idB)
108+
})
109+
110+
const recent_configs = matched_recent_configs
111+
const other_configs = remaining_configs
112+
113+
const map_config_to_item = (config: ToolConfig) => {
85114
const description_parts = [config.provider_name]
86115
if (config.temperature != null) {
87116
description_parts.push(`${config.temperature}`)
@@ -97,10 +126,34 @@ const get_code_completion_config = async (
97126
description: description_parts.join(' · '),
98127
buttons,
99128
config,
100-
index,
101129
id: get_tool_config_id(config)
102130
}
103-
})
131+
}
132+
133+
const items: (vscode.QuickPickItem & {
134+
config?: ToolConfig
135+
id?: string
136+
})[] = []
137+
138+
if (recent_configs.length > 0) {
139+
items.push({
140+
label: 'recently used',
141+
kind: vscode.QuickPickItemKind.Separator
142+
})
143+
items.push(...recent_configs.map(map_config_to_item))
144+
}
145+
146+
if (other_configs.length > 0) {
147+
if (recent_configs.length > 0) {
148+
items.push({
149+
label: 'other configurations',
150+
kind: vscode.QuickPickItemKind.Separator
151+
})
152+
}
153+
items.push(...other_configs.map(map_config_to_item))
154+
}
155+
156+
return items
104157
}
105158

106159
const quick_pick = vscode.window.createQuickPick()
@@ -124,7 +177,12 @@ const get_code_completion_config = async (
124177
if (last_selected_item) {
125178
quick_pick.activeItems = [last_selected_item]
126179
} else if (items.length > 0) {
127-
quick_pick.activeItems = [items[0]]
180+
const first_selectable = items.find(
181+
(i) => i.kind !== vscode.QuickPickItemKind.Separator
182+
)
183+
if (first_selectable) {
184+
quick_pick.activeItems = [first_selectable]
185+
}
128186
}
129187

130188
return new Promise<{ provider: any; config: any } | undefined>(
@@ -133,7 +191,7 @@ const get_code_completion_config = async (
133191
const selected = quick_pick.selectedItems[0] as any
134192
quick_pick.hide()
135193

136-
if (!selected) {
194+
if (!selected || !selected.config) {
137195
resolve(undefined)
138196
return
139197
}
@@ -147,6 +205,19 @@ const get_code_completion_config = async (
147205
selected.id
148206
)
149207

208+
let recents =
209+
context.workspaceState.get<string[]>(
210+
RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY
211+
) || []
212+
recents = [
213+
selected.id,
214+
...recents.filter((id) => id != selected.id)
215+
].slice(0, 10)
216+
context.workspaceState.update(
217+
RECENTLY_USED_CODE_COMPLETION_CONFIG_IDS_STATE_KEY,
218+
recents
219+
)
220+
150221
if (panel_provider) {
151222
panel_provider.send_message({
152223
command: 'SELECTED_CONFIGURATION_CHANGED',
@@ -369,7 +440,10 @@ const perform_code_completion = async (params: {
369440
endpoint_url,
370441
api_key: provider.api_key,
371442
body,
372-
request_id
443+
request_id,
444+
provider_name: code_completions_config.provider_name,
445+
model: code_completions_config.model,
446+
reasoning_effort: code_completions_config.reasoning_effort
373447
})
374448

375449
if (result) {

‎packages/vscode/src/views/panel/backend/message-handlers/handle-edit-context.ts‎

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import axios from 'axios'
1010
import { PROVIDERS } from '@shared/constants/providers'
1111
import {
1212
API_EDIT_FORMAT_STATE_KEY,
13-
LAST_SELECTED_EDIT_CONTEXT_CONFIG_ID_STATE_KEY
13+
LAST_SELECTED_EDIT_CONTEXT_CONFIG_ID_STATE_KEY,
14+
RECENTLY_USED_EDIT_CONTEXT_CONFIG_IDS_STATE_KEY
1415
} from '@/constants/state-keys'
1516
import { EditFormat } from '@shared/types/edit-format'
1617
import { ToolConfig } from '@/services/model-providers-manager'
@@ -92,12 +93,37 @@ const get_edit_context_config = async (
9293

9394
if (!selected_config || show_quick_pick) {
9495
type Item = vscode.QuickPickItem & {
95-
config: ToolConfig
96-
index: number
97-
id: string
96+
config?: ToolConfig
97+
id?: string
9898
}
9999
const create_items = async (): Promise<Item[]> => {
100-
return edit_context_configs.map((config: ToolConfig, index) => {
100+
const recent_ids =
101+
context.workspaceState.get<string[]>(
102+
RECENTLY_USED_EDIT_CONTEXT_CONFIG_IDS_STATE_KEY
103+
) || []
104+
105+
const matched_recent_configs: ToolConfig[] = []
106+
const remaining_configs: ToolConfig[] = []
107+
108+
edit_context_configs.forEach((config) => {
109+
const id = get_tool_config_id(config)
110+
if (recent_ids.includes(id)) {
111+
matched_recent_configs.push(config)
112+
} else {
113+
remaining_configs.push(config)
114+
}
115+
})
116+
117+
matched_recent_configs.sort((a, b) => {
118+
const idA = get_tool_config_id(a)
119+
const idB = get_tool_config_id(b)
120+
return recent_ids.indexOf(idA) - recent_ids.indexOf(idB)
121+
})
122+
123+
const recent_configs = matched_recent_configs
124+
const other_configs = remaining_configs
125+
126+
const map_config_to_item = (config: ToolConfig) => {
101127
const description_parts = [config.provider_name]
102128
if (config.temperature != null) {
103129
description_parts.push(`${config.temperature}`)
@@ -113,10 +139,31 @@ const get_edit_context_config = async (
113139
description: description_parts.join(' · '),
114140
buttons,
115141
config,
116-
index,
117142
id: get_tool_config_id(config)
118143
}
119-
})
144+
}
145+
146+
const items: Item[] = []
147+
148+
if (recent_configs.length > 0) {
149+
items.push({
150+
label: 'recently used',
151+
kind: vscode.QuickPickItemKind.Separator
152+
})
153+
items.push(...recent_configs.map(map_config_to_item))
154+
}
155+
156+
if (other_configs.length > 0) {
157+
if (recent_configs.length > 0) {
158+
items.push({
159+
label: 'other configurations',
160+
kind: vscode.QuickPickItemKind.Separator
161+
})
162+
}
163+
items.push(...other_configs.map(map_config_to_item))
164+
}
165+
166+
return items
120167
}
121168

122169
const quick_pick = vscode.window.createQuickPick<Item>()
@@ -138,7 +185,12 @@ const get_edit_context_config = async (
138185
if (last_selected_item) {
139186
quick_pick.activeItems = [last_selected_item]
140187
} else if (items.length > 0) {
141-
quick_pick.activeItems = [items[0]]
188+
const first_selectable = items.find(
189+
(i) => i.kind !== vscode.QuickPickItemKind.Separator
190+
)
191+
if (first_selectable) {
192+
quick_pick.activeItems = [first_selectable]
193+
}
142194
}
143195

144196
return new Promise<{ provider: Provider; config: ToolConfig } | undefined>(
@@ -150,7 +202,7 @@ const get_edit_context_config = async (
150202
const selected = quick_pick.selectedItems[0]
151203
quick_pick.hide()
152204

153-
if (!selected) {
205+
if (!selected || !selected.config) {
154206
resolve(undefined)
155207
return
156208
}
@@ -164,11 +216,24 @@ const get_edit_context_config = async (
164216
selected.id
165217
)
166218

219+
let recents =
220+
context.workspaceState.get<string[]>(
221+
RECENTLY_USED_EDIT_CONTEXT_CONFIG_IDS_STATE_KEY
222+
) || []
223+
recents = [
224+
selected.id!,
225+
...recents.filter((id) => id !== selected.id)
226+
].slice(0, 10)
227+
context.workspaceState.update(
228+
RECENTLY_USED_EDIT_CONTEXT_CONFIG_IDS_STATE_KEY,
229+
recents
230+
)
231+
167232
if (panel_provider) {
168233
panel_provider.send_message({
169234
command: 'SELECTED_CONFIGURATION_CHANGED',
170235
mode: 'edit-context',
171-
id: selected.id
236+
id: selected.id!
172237
})
173238
}
174239

0 commit comments

Comments
 (0)