Skip to content

Commit 5e94a83

Browse files
committed
Add test case for fragment symbol handling and fix diff processor's replace block logic
1 parent 62eb04e commit 5e94a83

5 files changed

Lines changed: 620 additions & 3 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/diffs/diff-processor.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,19 @@ describe('diff-processor', () => {
126126

127127
expect(result).toBe(expected)
128128
})
129+
130+
it('applies diff with generic test case 6 correctly', async () => {
131+
const test_case = 'generic-6'
132+
const original = load_test_case_file('', test_case, 'original.txt')
133+
const diff = load_test_case_file('', test_case, 'diff.txt')
134+
const expected = load_test_case_file('', test_case, 'expected.txt')
135+
136+
const result = apply_diff({
137+
original_code: original,
138+
diff_patch: diff
139+
})
140+
141+
expect(result).toBe(expected)
142+
})
129143
})
130144
})

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/diffs/diff-processor.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ export const apply_diff = (params: {
134134
}
135135

136136
if (line.startsWith('-') || line.startsWith('~')) {
137-
if (inside_replace_block) {
138-
push_block()
139-
}
137+
inside_replace_block = false
140138

141139
if (line.startsWith('-')) {
142140
current_block_has_changes = true
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--- a/apps/editor/src/views/panel/backend/message-handlers/handle-preview-preset.ts
2+
+++ b/apps/editor/src/views/panel/backend/message-handlers/handle-preview-preset.ts
3+
@@ -14,6 +14,7 @@
4+
import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-symbol'
5+
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
6+
import { replace_document_symbol } from '../utils/replace-document-symbol'
7+
+import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
8+
import { apply_preset_affixes_to_instruction } from '@/utils/apply-preset-affixes'
9+
import { dictionary } from '@shared/constants/dictionary'
10+
import {
11+
@@ -124,6 +125,12 @@
12+
})
13+
}
14+
15+
+ if (processed_completion_instructions.includes('<fragment')) {
16+
+ processed_completion_instructions = replace_fragment_symbol(
17+
+ processed_completion_instructions
18+
+ )
19+
+ }
20+
+
21+
const missing_text_tag = processed_completion_instructions
22+
? `<missing_text>${processed_completion_instructions}</missing_text>`
23+
: '<missing_text>'
24+
@@ -213,6 +220,10 @@
25+
})
26+
}
27+
28+
+ if (processed_instructions.includes('<fragment')) {
29+
+ processed_instructions = replace_fragment_symbol(processed_instructions)
30+
+ }
31+
+
32+
let system_instructions_xml = ''
33+
34+
if (panel_provider.web_prompt_type == 'edit-context') {
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
import * as vscode from 'vscode'
2+
import { PanelProvider } from '@/views/panel/backend/panel-provider'
3+
import { PreviewPresetMessage } from '@/views/panel/types/messages'
4+
import { FilesCollector } from '@/utils/files-collector'
5+
import { replace_selection_symbol } from '@/views/panel/backend/utils/replace-selection-symbol'
6+
import { replace_saved_context_symbol } from '@/views/panel/backend/utils/replace-saved-context-symbol'
7+
import {
8+
replace_changes_symbol,
9+
replace_context_at_commit_symbol,
10+
replace_commit_symbol
11+
} from '@/views/panel/backend/utils/replace-git-symbols'
12+
import { Preset } from '@shared/types/preset'
13+
import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-symbol'
14+
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
15+
import { replace_document_symbol } from '../utils/replace-document-symbol'
16+
import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
17+
import { apply_preset_affixes_to_instruction } from '@/utils/apply-preset-affixes'
18+
import { dictionary } from '@shared/constants/dictionary'
19+
import {
20+
EDIT_FORMAT_INSTRUCTIONS_WHOLE,
21+
EDIT_FORMAT_INSTRUCTIONS_TRUNCATED,
22+
EDIT_FORMAT_INSTRUCTIONS_BEFORE_AFTER,
23+
EDIT_FORMAT_INSTRUCTIONS_DIFF
24+
} from '@/constants/edit-format-instructions'
25+
26+
export const handle_preview_preset = async (
27+
panel_provider: PanelProvider,
28+
message: PreviewPresetMessage
29+
): Promise<void> => {
30+
await vscode.workspace.saveAll()
31+
32+
const files_collector = new FilesCollector({
33+
workspace_provider: panel_provider.workspace_provider,
34+
open_editors_provider: panel_provider.open_editors_provider
35+
})
36+
37+
const active_editor = vscode.window.activeTextEditor
38+
const active_path = active_editor?.document.uri.fsPath
39+
40+
let text_to_send: string
41+
const current_instructions = panel_provider.current_instruction
42+
43+
if (panel_provider.web_prompt_type == 'code-at-cursor' && active_editor) {
44+
const document = active_editor.document
45+
const position = active_editor.selection.active
46+
47+
const text_before_cursor = document.getText(
48+
new vscode.Range(new vscode.Position(0, 0), position)
49+
)
50+
const text_after_cursor = document.getText(
51+
new vscode.Range(position, document.positionAt(document.getText().length))
52+
)
53+
54+
const context_text = await files_collector.collect_files()
55+
56+
const workspace_folder = vscode.workspace.workspaceFolders?.[0].uri.fsPath
57+
const relative_path = active_path!.replace(workspace_folder + '/', '')
58+
59+
const config = vscode.workspace.getConfiguration('codeWebChat')
60+
const system_instructions =
61+
config.get<string>('chatCodeCompletionsInstructions') || ''
62+
63+
let processed_completion_instructions = current_instructions
64+
let skill_definitions = ''
65+
66+
if (processed_completion_instructions.includes('#Selection')) {
67+
processed_completion_instructions = replace_selection_symbol(
68+
processed_completion_instructions
69+
)
70+
}
71+
72+
if (processed_completion_instructions.includes('#Changes(')) {
73+
const result = await replace_changes_symbol({
74+
instruction: processed_completion_instructions
75+
})
76+
processed_completion_instructions = result.instruction
77+
skill_definitions += result.changes_definitions
78+
}
79+
80+
if (processed_completion_instructions.includes('#Commit(')) {
81+
const result = await replace_commit_symbol({
82+
instruction: processed_completion_instructions
83+
})
84+
processed_completion_instructions = result.instruction
85+
skill_definitions += result.commit_definitions
86+
}
87+
88+
if (processed_completion_instructions.includes('#ContextAtCommit(')) {
89+
processed_completion_instructions =
90+
await replace_context_at_commit_symbol({
91+
instruction: processed_completion_instructions,
92+
workspace_provider: panel_provider.workspace_provider
93+
})
94+
}
95+
96+
if (processed_completion_instructions.includes('#SavedContext(')) {
97+
const result = await replace_saved_context_symbol({
98+
instruction: processed_completion_instructions,
99+
context: panel_provider.context,
100+
workspace_provider: panel_provider.workspace_provider
101+
})
102+
processed_completion_instructions = result.instruction
103+
skill_definitions += result.context_definitions
104+
}
105+
106+
if (processed_completion_instructions.includes('#Skill(')) {
107+
const result = await replace_skill_symbol({
108+
instruction: processed_completion_instructions
109+
})
110+
processed_completion_instructions = result.instruction
111+
skill_definitions += result.skill_definitions
112+
}
113+
114+
if (processed_completion_instructions.includes('#Image(')) {
115+
processed_completion_instructions = await replace_image_symbol({
116+
instruction: processed_completion_instructions,
117+
remove: true
118+
})
119+
}
120+
121+
if (processed_completion_instructions.includes('#Document(')) {
122+
processed_completion_instructions = await replace_document_symbol({
123+
instruction: processed_completion_instructions
124+
})
125+
}
126+
127+
if (processed_completion_instructions.includes('<fragment')) {
128+
processed_completion_instructions = replace_fragment_symbol(
129+
processed_completion_instructions
130+
)
131+
}
132+
133+
const missing_text_tag = processed_completion_instructions
134+
? `<missing_text>${processed_completion_instructions}</missing_text>`
135+
: '<missing_text>'
136+
137+
text_to_send = `<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}${missing_text_tag}${text_after_cursor}\n]]>\n</file>\n</files>\n${skill_definitions}${system_instructions}`
138+
} else if (panel_provider.web_prompt_type != 'code-at-cursor') {
139+
let instructions = apply_preset_affixes_to_instruction({
140+
instruction: current_instructions,
141+
preset_name: message.preset.name!,
142+
presets_config_key: panel_provider.get_presets_config_key(),
143+
override_affixes: {
144+
promptPrefix: message.preset.prompt_prefix,
145+
promptSuffix: message.preset.prompt_suffix
146+
}
147+
})
148+
149+
const has_selection =
150+
!!active_editor &&
151+
!active_editor.selection.isEmpty &&
152+
instructions.includes('#Selection')
153+
154+
const context_text =
155+
panel_provider.web_prompt_type != 'no-context'
156+
? await files_collector.collect_files({})
157+
: ''
158+
159+
if (has_selection) {
160+
instructions = replace_selection_symbol(instructions)
161+
}
162+
163+
let processed_instructions = instructions
164+
let skill_definitions = ''
165+
166+
if (processed_instructions.includes('#Changes(')) {
167+
const result = await replace_changes_symbol({
168+
instruction: processed_instructions
169+
})
170+
processed_instructions = result.instruction
171+
skill_definitions += result.changes_definitions
172+
}
173+
174+
if (processed_instructions.includes('#Commit(')) {
175+
const result = await replace_commit_symbol({
176+
instruction: processed_instructions
177+
})
178+
processed_instructions = result.instruction
179+
skill_definitions += result.commit_definitions
180+
}
181+
182+
if (processed_instructions.includes('#ContextAtCommit(')) {
183+
processed_instructions = await replace_context_at_commit_symbol({
184+
instruction: processed_instructions,
185+
workspace_provider: panel_provider.workspace_provider
186+
})
187+
}
188+
189+
if (processed_instructions.includes('#SavedContext(')) {
190+
const result = await replace_saved_context_symbol({
191+
instruction: processed_instructions,
192+
context: panel_provider.context,
193+
workspace_provider: panel_provider.workspace_provider
194+
})
195+
processed_instructions = result.instruction
196+
skill_definitions += result.context_definitions
197+
}
198+
199+
if (processed_instructions.includes('#Skill(')) {
200+
const result = await replace_skill_symbol({
201+
instruction: processed_instructions
202+
})
203+
processed_instructions = result.instruction
204+
skill_definitions += result.skill_definitions
205+
}
206+
207+
if (processed_instructions.includes('#Image(')) {
208+
processed_instructions = await replace_image_symbol({
209+
instruction: processed_instructions,
210+
remove: true
211+
})
212+
}
213+
214+
if (processed_instructions.includes('#Document(')) {
215+
processed_instructions = await replace_document_symbol({
216+
instruction: processed_instructions
217+
})
218+
}
219+
220+
if (processed_instructions.includes('<fragment')) {
221+
processed_instructions = replace_fragment_symbol(processed_instructions)
222+
}
223+
224+
let system_instructions_xml = ''
225+
if (panel_provider.web_prompt_type == 'edit-context') {
226+
const config = vscode.workspace.getConfiguration('codeWebChat')
227+
const instructions_key = {
228+
whole: 'editFormatInstructionsWhole',
229+
truncated: 'editFormatInstructionsTruncated',
230+
'before-after': 'editFormatInstructionsBeforeAfter',
231+
diff: 'editFormatInstructionsDiff'
232+
}[panel_provider.chat_edit_format]
233+
const default_instructions = {
234+
whole: EDIT_FORMAT_INSTRUCTIONS_WHOLE,
235+
truncated: EDIT_FORMAT_INSTRUCTIONS_TRUNCATED,
236+
'before-after': EDIT_FORMAT_INSTRUCTIONS_BEFORE_AFTER,
237+
diff: EDIT_FORMAT_INSTRUCTIONS_DIFF
238+
}[panel_provider.chat_edit_format]
239+
const edit_format_instructions =
240+
config.get<string>(instructions_key) || default_instructions
241+
if (edit_format_instructions) {
242+
system_instructions_xml = `<system>\n${edit_format_instructions}\n</system>`
243+
}
244+
}
245+
246+
text_to_send = context_text
247+
? `<files>\n${context_text}</files>\n${skill_definitions}${
248+
system_instructions_xml ? system_instructions_xml + '\n' : ''
249+
}${processed_instructions}`
250+
: `${
251+
system_instructions_xml ? system_instructions_xml + '\n' : ''
252+
}${skill_definitions}${processed_instructions}`
253+
} else {
254+
vscode.window.showWarningMessage(
255+
dictionary.warning_message
256+
.CANNOT_PREVIEW_IN_CODE_COMPLETION_WITHOUT_EDITOR
257+
)
258+
return
259+
}
260+
261+
const preset_for_preview: Preset = {
262+
name: message.preset.name,
263+
chatbot: message.preset.chatbot,
264+
prompt_prefix: message.preset.prompt_prefix,
265+
prompt_suffix: message.preset.prompt_suffix,
266+
model: message.preset.model,
267+
temperature: message.preset.temperature,
268+
top_p: message.preset.top_p,
269+
thinking_budget: message.preset.thinking_budget,
270+
reasoning_effort: message.preset.reasoning_effort,
271+
system_instructions: message.preset.system_instructions,
272+
options: message.preset.options,
273+
port: message.preset.port,
274+
new_url: message.preset.new_url
275+
}
276+
277+
const sent = await panel_provider.websocket_server_instance.preview_preset({
278+
instruction: text_to_send,
279+
preset: preset_for_preview,
280+
prompt_type: panel_provider.web_prompt_type,
281+
raw_instructions: current_instructions
282+
})
283+
284+
if (sent) {
285+
panel_provider.send_message({
286+
command: 'SHOW_AUTO_CLOSING_MODAL',
287+
title: 'Opened in the connected browser',
288+
type: 'success'
289+
})
290+
}
291+
}

0 commit comments

Comments
 (0)