Skip to content

Commit c8c48a1

Browse files
committed
Consolidate symbol replacement logic into a centralized utility function to streamline instruction processing
1 parent ab94caa commit c8c48a1

15 files changed

Lines changed: 166 additions & 704 deletions

‎apps/editor/src/views/panel/backend/message-handlers/handle-code-at-cursor.ts‎

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,8 @@ import { CodeAtCursorMessage } from '@/views/panel/types/messages'
1414
import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
1515
import { dictionary } from '@shared/constants/dictionary'
1616
import { randomUUID } from 'crypto'
17-
import { replace_selection_symbol } from '../utils/replace-selection-symbol'
18-
import {
19-
replace_changes_symbol,
20-
replace_commit_symbol,
21-
replace_context_at_commit_symbol
22-
} from '../utils/replace-git-symbols'
23-
import { replace_saved_context_symbol } from '@/views/panel/backend/utils/replace-saved-context-symbol'
24-
import { replace_skill_symbol } from '../utils/replace-skill-symbol'
25-
import { replace_image_symbol } from '../utils/replace-image-symbol'
26-
import { replace_pasted_text_symbol } from '../utils/replace-pasted-text-symbol'
27-
import { replace_website_symbol } from '../utils/replace-website-symbol'
28-
import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
2917
import { build_user_content } from '@/utils/build-user-content'
18+
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
3019

3120
const get_code_at_cursor_config = async (
3221
api_providers_manager: ModelProvidersManager,
@@ -319,80 +308,14 @@ export const handle_code_at_cursor = async (
319308
column: position.character
320309
})
321310

322-
let processed_completion_instructions = completion_instructions
323-
let skill_definitions = ''
324-
325-
if (processed_completion_instructions.includes('#Selection')) {
326-
processed_completion_instructions = replace_selection_symbol(
327-
processed_completion_instructions
328-
)
329-
}
330-
331-
if (processed_completion_instructions.includes('#Changes(')) {
332-
const result = await replace_changes_symbol({
333-
instruction: processed_completion_instructions
334-
})
335-
processed_completion_instructions = result.instruction
336-
skill_definitions += result.changes_definitions
337-
}
338-
339-
if (processed_completion_instructions.includes('#Commit(')) {
340-
const result = await replace_commit_symbol({
341-
instruction: processed_completion_instructions
342-
})
343-
processed_completion_instructions = result.instruction
344-
skill_definitions += result.commit_definitions
345-
}
346-
347-
if (processed_completion_instructions.includes('#ContextAtCommit(')) {
348-
processed_completion_instructions =
349-
await replace_context_at_commit_symbol({
350-
instruction: processed_completion_instructions,
351-
workspace_provider: panel_provider.workspace_provider
352-
})
353-
}
354-
355-
if (processed_completion_instructions.includes('#SavedContext(')) {
356-
const result = await replace_saved_context_symbol({
357-
instruction: processed_completion_instructions,
358-
context: panel_provider.context,
359-
workspace_provider: panel_provider.workspace_provider
360-
})
361-
processed_completion_instructions = result.instruction
362-
skill_definitions += result.context_definitions
363-
}
364-
365-
if (processed_completion_instructions.includes('#Skill(')) {
366-
const result = await replace_skill_symbol({
367-
instruction: processed_completion_instructions
368-
})
369-
processed_completion_instructions = result.instruction
370-
skill_definitions += result.skill_definitions
371-
}
372-
373-
if (processed_completion_instructions.includes('#Image(')) {
374-
processed_completion_instructions = await replace_image_symbol({
375-
instruction: processed_completion_instructions
376-
})
377-
}
378-
379-
if (processed_completion_instructions.includes('#PastedText(')) {
380-
processed_completion_instructions = await replace_pasted_text_symbol({
381-
instruction: processed_completion_instructions
382-
})
383-
}
384-
385-
if (processed_completion_instructions.includes('#Website(')) {
386-
processed_completion_instructions = await replace_website_symbol({
387-
instruction: processed_completion_instructions
388-
})
389-
}
390-
391-
if (processed_completion_instructions.includes('<fragment')) {
392-
processed_completion_instructions = replace_fragment_symbol(
393-
processed_completion_instructions
394-
)
395-
}
311+
const {
312+
instruction: processed_completion_instructions,
313+
skill_definitions
314+
} = await replace_symbols({
315+
instruction: completion_instructions,
316+
context: panel_provider.context,
317+
workspace_provider: panel_provider.workspace_provider
318+
})
396319

397320
const files_collector = new FilesCollector({
398321
workspace_provider: panel_provider.workspace_provider,

‎apps/editor/src/views/panel/backend/message-handlers/handle-copy-prompt.ts‎

Lines changed: 15 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
import { PanelProvider } from '@/views/panel/backend/panel-provider'
22
import * as vscode from 'vscode'
33
import { FilesCollector } from '@/utils/files-collector'
4-
import { replace_selection_symbol } from '@/views/panel/backend/utils/replace-selection-symbol'
5-
import {
6-
replace_changes_symbol,
7-
replace_commit_symbol,
8-
replace_context_at_commit_symbol
9-
} from '@/views/panel/backend/utils/replace-git-symbols'
10-
import { replace_saved_context_symbol } from '@/views/panel/backend/utils/replace-saved-context-symbol'
11-
import { replace_skill_symbol } from '@/views/panel/backend/utils/replace-skill-symbol'
12-
import { replace_image_symbol } from '@/views/panel/backend/utils/replace-image-symbol'
13-
import { replace_pasted_text_symbol } from '../utils/replace-pasted-text-symbol'
14-
import { replace_website_symbol } from '../utils/replace-website-symbol'
15-
import { replace_fragment_symbol } from '../utils/replace-fragment-symbol'
164
import {
175
code_at_cursor_instructions_for_panel,
186
find_relevant_files_instructions,
@@ -28,6 +16,7 @@ import {
2816
EDIT_FORMAT_INSTRUCTIONS_DIFF
2917
} from '@/constants/edit-format-instructions'
3018
import { FIND_RELEVANT_FILES_SHRINK_SOURCE_CODE_STATE_KEY } from '@/constants/state-keys'
19+
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
3120

3221
export const handle_copy_prompt = async (params: {
3322
panel_provider: PanelProvider
@@ -92,81 +81,15 @@ export const handle_copy_prompt = async (params: {
9281
column: position.character
9382
})
9483

95-
let processed_completion_instructions = final_instruction
96-
let skill_definitions = ''
97-
98-
if (processed_completion_instructions.includes('#Selection')) {
99-
processed_completion_instructions = replace_selection_symbol(
100-
processed_completion_instructions
101-
)
102-
}
103-
104-
if (processed_completion_instructions.includes('#Changes(')) {
105-
const result = await replace_changes_symbol({
106-
instruction: processed_completion_instructions
107-
})
108-
processed_completion_instructions = result.instruction
109-
skill_definitions += result.changes_definitions
110-
}
111-
112-
if (processed_completion_instructions.includes('#Commit(')) {
113-
const result = await replace_commit_symbol({
114-
instruction: processed_completion_instructions
115-
})
116-
processed_completion_instructions = result.instruction
117-
skill_definitions += result.commit_definitions
118-
}
119-
120-
if (processed_completion_instructions.includes('#ContextAtCommit(')) {
121-
processed_completion_instructions =
122-
await replace_context_at_commit_symbol({
123-
instruction: processed_completion_instructions,
124-
workspace_provider: params.panel_provider.workspace_provider
125-
})
126-
}
127-
128-
if (processed_completion_instructions.includes('#SavedContext(')) {
129-
const result = await replace_saved_context_symbol({
130-
instruction: processed_completion_instructions,
131-
context: params.panel_provider.context,
132-
workspace_provider: params.panel_provider.workspace_provider
133-
})
134-
processed_completion_instructions = result.instruction
135-
skill_definitions += result.context_definitions
136-
}
137-
138-
if (processed_completion_instructions.includes('#Skill(')) {
139-
const result = await replace_skill_symbol({
140-
instruction: processed_completion_instructions
141-
})
142-
processed_completion_instructions = result.instruction
143-
skill_definitions += result.skill_definitions
144-
}
145-
146-
if (processed_completion_instructions.includes('#Image(')) {
147-
processed_completion_instructions = await replace_image_symbol({
148-
instruction: processed_completion_instructions,
149-
remove: true
150-
})
151-
}
152-
153-
if (processed_completion_instructions.includes('#PastedText(')) {
154-
processed_completion_instructions = await replace_pasted_text_symbol({
155-
instruction: processed_completion_instructions
156-
})
157-
}
158-
159-
if (processed_completion_instructions.includes('#Website(')) {
160-
processed_completion_instructions = await replace_website_symbol({
161-
instruction: processed_completion_instructions
162-
})
163-
}
164-
165-
if (processed_completion_instructions.includes('<fragment')) {
166-
processed_completion_instructions = replace_fragment_symbol(
167-
processed_completion_instructions
168-
)
169-
}
84+
const {
85+
instruction: processed_completion_instructions,
86+
skill_definitions
87+
} = await replace_symbols({
88+
instruction: final_instruction,
89+
context: params.panel_provider.context,
90+
workspace_provider: params.panel_provider.workspace_provider,
91+
remove_images: true
92+
})
17093

17194
const missing_text_tag = processed_completion_instructions
17295
? `<missing_text>${processed_completion_instructions}</missing_text>`
@@ -194,74 +117,13 @@ export const handle_copy_prompt = async (params: {
194117
})
195118
const context_text = collected.other_files + collected.recent_files
196119

197-
const instructions = replace_selection_symbol(final_instruction)
198-
199-
let processed_instructions = instructions
200-
let skill_definitions = ''
201-
202-
if (processed_instructions.includes('#Changes(')) {
203-
const result = await replace_changes_symbol({
204-
instruction: processed_instructions
205-
})
206-
processed_instructions = result.instruction
207-
skill_definitions += result.changes_definitions
208-
}
209-
210-
if (processed_instructions.includes('#Commit(')) {
211-
const result = await replace_commit_symbol({
212-
instruction: processed_instructions
213-
})
214-
processed_instructions = result.instruction
215-
skill_definitions += result.commit_definitions
216-
}
217-
218-
if (processed_instructions.includes('#ContextAtCommit(')) {
219-
processed_instructions = await replace_context_at_commit_symbol({
220-
instruction: processed_instructions,
221-
workspace_provider: params.panel_provider.workspace_provider
222-
})
223-
}
224-
225-
if (processed_instructions.includes('#SavedContext(')) {
226-
const result = await replace_saved_context_symbol({
227-
instruction: processed_instructions,
120+
const { instruction: processed_instructions, skill_definitions } =
121+
await replace_symbols({
122+
instruction: final_instruction,
228123
context: params.panel_provider.context,
229-
workspace_provider: params.panel_provider.workspace_provider
124+
workspace_provider: params.panel_provider.workspace_provider,
125+
remove_images: true
230126
})
231-
processed_instructions = result.instruction
232-
skill_definitions += result.context_definitions
233-
}
234-
235-
if (processed_instructions.includes('#Skill(')) {
236-
const result = await replace_skill_symbol({
237-
instruction: processed_instructions
238-
})
239-
processed_instructions = result.instruction
240-
skill_definitions += result.skill_definitions
241-
}
242-
243-
if (processed_instructions.includes('#Image(')) {
244-
processed_instructions = await replace_image_symbol({
245-
instruction: processed_instructions,
246-
remove: true
247-
})
248-
}
249-
250-
if (processed_instructions.includes('#PastedText(')) {
251-
processed_instructions = await replace_pasted_text_symbol({
252-
instruction: processed_instructions
253-
})
254-
}
255-
256-
if (processed_instructions.includes('#Website(')) {
257-
processed_instructions = await replace_website_symbol({
258-
instruction: processed_instructions
259-
})
260-
}
261-
262-
if (processed_instructions.includes('<fragment')) {
263-
processed_instructions = replace_fragment_symbol(processed_instructions)
264-
}
265127

266128
let system_instructions_xml = ''
267129

0 commit comments

Comments
 (0)