Skip to content

Commit 6191a95

Browse files
committed
Refactor preset management to dynamically select and apply affixes based on the active chat mode
1 parent 13a11af commit 6191a95

18 files changed

Lines changed: 200 additions & 206 deletions

packages/ui/src/components/editor/EditPresetForm/EditPresetForm.tsx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Props = {
1212
preset: Preset
1313
on_update: (updated_preset: Preset) => void
1414
pick_open_router_model: () => void
15+
can_set_affixes: boolean
1516
}
1617

1718
export const EditPresetForm: React.FC<Props> = (props) => {
@@ -284,29 +285,33 @@ export const EditPresetForm: React.FC<Props> = (props) => {
284285
</Field>
285286
)}
286287

287-
<Field
288-
label="Prompt Prefix"
289-
html_for="prefix"
290-
info="Text prepended to prompts used with this preset"
291-
>
292-
<textarea
293-
id="prefix"
294-
value={prompt_prefix}
295-
onChange={(e) => set_prompt_prefix(e.target.value)}
296-
/>
297-
</Field>
288+
{props.can_set_affixes && (
289+
<>
290+
<Field
291+
label="Prompt Prefix"
292+
html_for="prefix"
293+
info="Text prepended to prompts used with this preset"
294+
>
295+
<textarea
296+
id="prefix"
297+
value={prompt_prefix}
298+
onChange={(e) => set_prompt_prefix(e.target.value)}
299+
/>
300+
</Field>
298301

299-
<Field
300-
label="Prompt Suffix"
301-
html_for="suffix"
302-
info="Text appended to prompts used with this preset"
303-
>
304-
<textarea
305-
id="suffix"
306-
value={prompt_suffix}
307-
onChange={(e) => set_prompt_suffix(e.target.value)}
308-
/>
309-
</Field>
302+
<Field
303+
label="Prompt Suffix"
304+
html_for="suffix"
305+
info="Text appended to prompts used with this preset"
306+
>
307+
<textarea
308+
id="suffix"
309+
value={prompt_suffix}
310+
onChange={(e) => set_prompt_suffix(e.target.value)}
311+
/>
312+
</Field>
313+
</>
314+
)}
310315
</div>
311316
)
312317
}

packages/ui/src/components/editor/Presets/Presets.module.scss

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,14 @@
6868
width: 16px;
6969
height: 16px;
7070
}
71-
72-
&--selected > svg {
73-
fill: var(--vscode-textLink-foreground);
74-
}
7571
}
7672

7773
&__text {
7874
overflow: hidden;
7975
text-overflow: ellipsis;
8076

81-
&--selected {
82-
color: var(--vscode-textLink-foreground);
77+
&--selected > span:first-child {
78+
font-weight: 600;
8379
}
8480

8581
&--disabled {

packages/ui/src/components/editor/Presets/Presets.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const with_ids = (
4444

4545
const ChatbotIcon: React.FC<{
4646
chatbot: keyof typeof CHATBOTS
47-
is_selected: boolean
4847
is_disabled: boolean
4948
}> = (params) => {
5049
const icon_variant = chatbot_to_icon[params.chatbot]
@@ -54,7 +53,6 @@ const ChatbotIcon: React.FC<{
5453
return (
5554
<div
5655
className={cn(styles.presets__item__left__icon, {
57-
[styles['presets__item__left__icon--selected']]: params.is_selected,
5856
[styles['presets__item__left__icon--disabled']]: params.is_disabled
5957
})}
6058
>
@@ -74,7 +72,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
7472
})}
7573
>
7674
<div className={styles['my-presets']}>
77-
<div className={styles['my-presets__left']}>MY PRESETS</div>
75+
<div className={styles['my-presets__left']}>MY CHAT PRESETS</div>
7876

7977
<TextButton
8078
on_click={props.on_set_default_presets}
@@ -134,11 +132,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
134132
title={display_name}
135133
>
136134
<div className={styles.presets__item__left}>
137-
<ChatbotIcon
138-
chatbot={preset.chatbot}
139-
is_selected={props.selected_presets.includes(preset.name)}
140-
is_disabled={false}
141-
/>
135+
<ChatbotIcon chatbot={preset.chatbot} is_disabled={false} />
142136

143137
<div
144138
className={cn(styles.presets__item__left__text, {

packages/vscode/src/commands/chat-commands.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ async function process_chat_instructions(
111111
preset_names: string[],
112112
context: vscode.ExtensionContext,
113113
workspace_provider: any,
114-
open_editors_provider: any
114+
open_editors_provider: any,
115+
presets_config_key: string
115116
) {
116117
const files_collector = new FilesCollector(
117118
workspace_provider,
@@ -143,7 +144,8 @@ async function process_chat_instructions(
143144
preset_names.map(async (preset_name) => {
144145
let base_instructions = apply_preset_affixes_to_instruction(
145146
instructions,
146-
preset_name
147+
preset_name,
148+
presets_config_key
147149
)
148150

149151
if (base_instructions.includes('@Selection')) {
@@ -195,7 +197,8 @@ export async function handle_chat_command(
195197
file_tree_provider: any,
196198
open_editors_provider: any,
197199
websocket_server_instance: WebSocketManager,
198-
preset_names: string[]
200+
preset_names: string[],
201+
presets_config_key: string
199202
) {
200203
const instructions = await get_chat_instructions(context)
201204
if (!instructions) return
@@ -205,11 +208,12 @@ export async function handle_chat_command(
205208
preset_names,
206209
context,
207210
file_tree_provider,
208-
open_editors_provider
211+
open_editors_provider,
212+
presets_config_key
209213
)
210214
if (!chats) return
211215

212-
websocket_server_instance.initialize_chats(chats)
216+
websocket_server_instance.initialize_chats(chats, presets_config_key)
213217
}
214218

215219
export function chat_using_command(
@@ -230,8 +234,9 @@ export function chat_using_command(
230234
if (!instructions) return
231235

232236
const config = vscode.workspace.getConfiguration('codeWebChat')
237+
const presets_config_key = 'chatPresetsForEditContext'
233238
const web_chat_presets = config.get<ConfigPresetFormat[]>(
234-
'chatPresetsForEditContext',
239+
presets_config_key,
235240
[]
236241
)
237242

@@ -258,11 +263,12 @@ export function chat_using_command(
258263
[selected_preset.label],
259264
context,
260265
file_tree_provider,
261-
open_editors_provider
266+
open_editors_provider,
267+
presets_config_key
262268
)
263269
if (!chats) return
264270

265-
websocket_server_instance.initialize_chats(chats)
271+
websocket_server_instance.initialize_chats(chats, presets_config_key)
266272
})
267273
}
268274

@@ -323,7 +329,8 @@ export function chat_command(
323329
file_tree_provider,
324330
open_editors_provider,
325331
websocket_server_instance,
326-
selected_names
332+
selected_names,
333+
'chatPresetsForEditContext'
327334
)
328335
})
329336
}

packages/vscode/src/commands/code-completion-in-chat-commands.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ async function handle_code_completion_in_chat_command(
9090
}
9191
})
9292

93-
websocket_server_instance.initialize_chats(chats)
93+
websocket_server_instance.initialize_chats(
94+
chats,
95+
'chatPresetsForCodeAtCursor'
96+
)
9497
} catch (error: any) {
9598
console.error('Error in FIM in Chat:', error)
9699
vscode.window.showErrorMessage('Error in FIM in Chat: ' + error.message)
@@ -119,7 +122,7 @@ export function code_completion_in_chat_with_command(
119122
async () => {
120123
const config = vscode.workspace.getConfiguration('codeWebChat')
121124
const all_presets = config.get<ConfigPresetFormat[]>(
122-
'chatPresetsForEditContext',
125+
'chatPresetsForCodeAtCursor',
123126
[]
124127
)
125128
const presets = filter_presets_with_affixes(all_presets)
@@ -174,7 +177,7 @@ export function code_completion_in_chat_command(
174177
async () => {
175178
const config = vscode.workspace.getConfiguration('codeWebChat')
176179
const all_presets = config.get<ConfigPresetFormat[]>(
177-
'chatPresetsForEditContext',
180+
'chatPresetsForCodeAtCursor',
178181
[]
179182
)
180183
const presets = filter_presets_with_affixes(all_presets)

packages/vscode/src/services/websocket-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,15 @@ export class WebSocketManager {
258258

259259
// TODO: This needs attention - should be renamed to "initialize-chat" and handle only one at a time.
260260
public async initialize_chats(
261-
chats: Array<{ text: string; preset_name: string }>
261+
chats: Array<{ text: string; preset_name: string }>,
262+
presets_config_key: string
262263
): Promise<void> {
263264
if (!this.has_connected_browsers) {
264265
throw new Error('Does not have connected browsers.')
265266
}
266267

267268
const config = vscode.workspace.getConfiguration('codeWebChat')
268-
const web_chat_presets =
269-
config.get<any[]>('chatPresetsForEditContext') ?? []
269+
const web_chat_presets = config.get<any[]>(presets_config_key) ?? []
270270

271271
for (const chat of chats) {
272272
const preset = web_chat_presets.find((p) => p.name == chat.preset_name)

packages/vscode/src/utils/apply-preset-affixes.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { ConfigPresetFormat } from '@/view/backend/helpers/preset-format-converters'
22
import * as vscode from 'vscode'
33

4-
export function get_presets_by_names(preset_names: string[]): Array<{
4+
export function get_presets_by_names(
5+
preset_names: string[],
6+
presets_config_key: string
7+
): Array<{
58
name: string
69
prompt_prefix?: string
710
prompt_suffix?: string
811
}> {
912
const config = vscode.workspace.getConfiguration('codeWebChat')
1013
const all_presets = config.get<ConfigPresetFormat[]>(
11-
'chatPresetsForEditContext',
14+
presets_config_key,
1215
[]
1316
)
1417

@@ -23,9 +26,10 @@ export function get_presets_by_names(preset_names: string[]): Array<{
2326

2427
export function apply_preset_affixes_to_instruction(
2528
instruction: string,
26-
preset_name: string
29+
preset_name: string,
30+
presets_config_key: string
2731
): string {
28-
const presets = get_presets_by_names([preset_name])
32+
const presets = get_presets_by_names([preset_name], presets_config_key)
2933
if (presets.length > 0) {
3034
const preset = presets[0]
3135
let result = instruction

packages/vscode/src/view/backend/message-handlers/handle-copy-prompt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const handle_copy_prompt = async (
2525
if (preset_name !== undefined) {
2626
final_instruction = apply_preset_affixes_to_instruction(
2727
instruction_to_copy,
28-
preset_name
28+
preset_name,
29+
provider.get_presets_config_key()
2930
)
3031
}
3132

packages/vscode/src/view/backend/message-handlers/handle-get-code-completions-mode.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/vscode/src/view/backend/message-handlers/handle-save-code-completions-mode.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)