Skip to content

Commit 9be0af5

Browse files
committed
Modify chat commands and websocket service to initialize distinct chats for each selected preset, applying specific preset affixes per chat
1 parent b184f09 commit 9be0af5

5 files changed

Lines changed: 107 additions & 93 deletions

File tree

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ async function handle_chat_command(
6262
return
6363
}
6464

65-
instructions = apply_preset_affixes_to_instruction(instructions, preset_names)
66-
6765
const edit_format = context.workspaceState.get<EditFormat>(
6866
'editFormat',
6967
'truncated'
@@ -76,15 +74,27 @@ async function handle_chat_command(
7674
}`
7775
)
7876

79-
if (edit_format_instructions && context_text) {
80-
instructions += `\n${edit_format_instructions}`
81-
}
77+
const chats = preset_names.map((preset_name) => {
78+
let processed_instructions = apply_preset_affixes_to_instruction(
79+
instructions,
80+
preset_name
81+
)
82+
83+
if (edit_format_instructions && context_text) {
84+
processed_instructions += `\n${edit_format_instructions}`
85+
}
86+
87+
const chat_text = context_text
88+
? `${processed_instructions}\n<files>\n${context_text}</files>\n${processed_instructions}`
89+
: processed_instructions
8290

83-
const text = `${
84-
context_text ? `${instructions}\n<files>\n${context_text}</files>\n` : ''
85-
}${instructions}`
91+
return {
92+
text: chat_text,
93+
preset_name: preset_name
94+
}
95+
})
8696

87-
websocket_server_instance.initialize_chats(text, preset_names)
97+
websocket_server_instance.initialize_chats(chats)
8898
}
8999

90100
export function chat_using_command(
@@ -176,7 +186,7 @@ export function chat_using_command(
176186

177187
processed_instructions = apply_preset_affixes_to_instruction(
178188
processed_instructions,
179-
[selected_preset.label]
189+
selected_preset.label
180190
)
181191

182192
const edit_format = config.get<EditFormat>('editFormat')!
@@ -196,7 +206,10 @@ export function chat_using_command(
196206
: processed_instructions
197207
}`
198208

199-
websocket_server_instance.initialize_chats(text, [selected_preset.label])
209+
// Update initialize_chats call to match the new signature
210+
websocket_server_instance.initialize_chats([
211+
{ text: text, preset_name: selected_preset.label }
212+
])
200213
})
201214
}
202215

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,15 @@ async function handle_code_completion_in_chat_command(
9393
)
9494
}
9595

96+
const chats = preset_names.map((preset_name) => {
97+
return {
98+
text,
99+
preset_name
100+
}
101+
})
102+
96103
// Initialize chats with selected preset names in FIM mode
97-
websocket_server_instance.initialize_chats(text, preset_names)
104+
websocket_server_instance.initialize_chats(chats)
98105
} catch (error: any) {
99106
console.error('Error in FIM in Chat:', error)
100107
vscode.window.showErrorMessage('Error in FIM in Chat: ' + error.message)
Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import * as vscode from 'vscode'
22

3-
/**
4-
* Gets presets by their names from configuration
5-
*/
63
export function get_presets_by_names(preset_names: string[]): Array<{
74
name: string
85
prompt_prefix?: string
@@ -20,33 +17,21 @@ export function get_presets_by_names(preset_names: string[]): Array<{
2017
}))
2118
}
2219

23-
/**
24-
* Applies the prefixes and suffixes from the selected presets to the instruction
25-
*/
2620
export function apply_preset_affixes_to_instruction(
2721
instruction: string,
28-
preset_names: string[]
22+
preset_name: string
2923
): string {
30-
const presets = get_presets_by_names(preset_names)
31-
32-
// Apply prefixes and suffixes for all selected presets
33-
if (presets.length == 0) {
34-
return instruction
35-
}
36-
37-
// For multiple presets, we apply the first preset's prefixes and suffixes
38-
// Future enhancement could handle multiple presets differently
39-
const preset = presets[0]
40-
41-
let modified_instruction = instruction
42-
43-
if (preset.prompt_prefix) {
44-
modified_instruction = `${preset.prompt_prefix} ${modified_instruction}`
45-
}
46-
47-
if (preset.prompt_suffix) {
48-
modified_instruction = `${modified_instruction} ${preset.prompt_suffix}`
24+
const presets = get_presets_by_names([preset_name])
25+
if (presets.length > 0) {
26+
const preset = presets[0]
27+
let result = instruction
28+
if (preset.prompt_prefix) {
29+
result = `${preset.prompt_prefix} ${result}`
30+
}
31+
if (preset.prompt_suffix) {
32+
result = `${result} ${preset.prompt_suffix}`
33+
}
34+
return result
4935
}
50-
51-
return modified_instruction
36+
return instruction
5237
}

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

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ export class WebSocketManager {
267267
return this.has_connected_browsers
268268
}
269269

270+
// TODO: This needs attention - should be renamed to "initialize-chat" and handle only one at a time.
270271
public async initialize_chats(
271-
text: string,
272-
preset_names: string[]
272+
chats: Array<{ text: string; preset_name: string }>
273273
): Promise<void> {
274274
if (!this.has_connected_browsers) {
275275
throw new Error('Does not have connected browsers.')
@@ -278,47 +278,48 @@ export class WebSocketManager {
278278
const config = vscode.workspace.getConfiguration('codeWebChat')
279279
const web_chat_presets = config.get<any[]>('presets') ?? []
280280

281-
const message: InitializeChatsMessage = {
282-
action: 'initialize-chats',
283-
text,
284-
chats: preset_names
285-
.map((name) => {
286-
const preset = web_chat_presets.find((p) => p.name == name)
287-
if (!preset) {
288-
return null
289-
}
281+
for (const chat of chats) {
282+
const preset = web_chat_presets.find((p) => p.name == chat.preset_name)
283+
if (!preset) {
284+
continue
285+
}
290286

291-
const chatbot = CHATBOTS[preset.chatbot as keyof typeof CHATBOTS]
292-
let url: string
293-
if (preset.chatbot == 'Open WebUI') {
294-
if (preset.port) {
295-
url = `http://localhost:${preset.port}/`
296-
} else {
297-
url = 'http://openwebui/'
298-
}
299-
} else {
300-
url = chatbot.url
301-
}
302-
return {
287+
const chatbot = CHATBOTS[preset.chatbot as keyof typeof CHATBOTS]
288+
let url: string
289+
if (preset.chatbot == 'Open WebUI') {
290+
if (preset.port) {
291+
url = `http://localhost:${preset.port}/`
292+
} else {
293+
url = 'http://openwebui/'
294+
}
295+
} else {
296+
url = chatbot.url
297+
}
298+
299+
const message: InitializeChatsMessage = {
300+
action: 'initialize-chats',
301+
text: chat.text,
302+
chats: [
303+
{
303304
url,
304305
model: preset.model,
305306
temperature: preset.temperature,
306307
top_p: preset.top_p,
307308
system_instructions: preset.systemInstructions,
308309
options: preset.options
309310
}
310-
})
311-
.filter((chat) => chat !== null), // Filter out any null chats
312-
client_id: this.client_id || 0 // 0 is a temporary fallback and should be removed few weeks from 28.03.25
313-
}
311+
],
312+
client_id: this.client_id || 0 // 0 is a temporary fallback and should be removed few weeks from 28.03.25
313+
}
314314

315-
Logger.log({
316-
function_name: 'initialize_chats',
317-
message: 'Sending initialize chats message',
318-
data: message
319-
})
315+
Logger.log({
316+
function_name: 'initialize_chats',
317+
message: 'Sending initialize chat message',
318+
data: message
319+
})
320320

321-
this.client?.send(JSON.stringify(message))
321+
this.client?.send(JSON.stringify(message))
322+
}
322323
}
323324

324325
public async preview_preset(

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ export const handle_send_prompt = async (
6060

6161
const text = `${instructions}\n<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}<missing text>${text_after_cursor}\n]]>\n</file>\n</files>\n${instructions}`
6262

63-
provider.websocket_server_instance.initialize_chats(
64-
text,
65-
valid_preset_names
66-
)
63+
const chats = valid_preset_names.map((preset_name) => {
64+
return {
65+
text,
66+
preset_name
67+
}
68+
})
69+
70+
provider.websocket_server_instance.initialize_chats(chats)
6771
} else if (!provider.is_code_completions_mode) {
6872
if (!provider.instructions) return
6973

7074
const context_text = await files_collector.collect_files({
7175
active_path
7276
})
7377

74-
let instructions = provider.instructions
75-
instructions = replace_selection_placeholder(instructions)
76-
instructions = apply_preset_affixes_to_instruction(
77-
instructions,
78-
valid_preset_names
79-
)
78+
let base_instructions = provider.instructions
79+
base_instructions = replace_selection_placeholder(base_instructions)
8080

8181
const config = vscode.workspace.getConfiguration('codeWebChat')
8282
const edit_format_instructions = config.get<string>(
@@ -85,18 +85,26 @@ export const handle_send_prompt = async (
8585
provider.edit_format.slice(1)
8686
}`
8787
)
88-
if (edit_format_instructions) {
89-
instructions += `\n${edit_format_instructions}`
90-
}
9188

92-
const text = `${
93-
context_text ? `${instructions}\n<files>\n${context_text}</files>\n` : ''
94-
}${instructions}`
89+
const chats = valid_preset_names.map((preset_name) => {
90+
let instructions = apply_preset_affixes_to_instruction(
91+
base_instructions,
92+
preset_name
93+
)
9594

96-
provider.websocket_server_instance.initialize_chats(
97-
text,
98-
valid_preset_names
99-
)
95+
if (edit_format_instructions) {
96+
instructions += `\n${edit_format_instructions}`
97+
}
98+
99+
return {
100+
text: context_text
101+
? `${instructions}\n<files>\n${context_text}</files>\n${instructions}`
102+
: instructions,
103+
preset_name
104+
}
105+
})
106+
107+
provider.websocket_server_instance.initialize_chats(chats)
100108
}
101109

102110
vscode.window.showInformationMessage(

0 commit comments

Comments
 (0)