Skip to content

Commit eb9aa9a

Browse files
committed
Refactor symbol replacement functions to accept instruction arrays instead of single strings
- Refactor `replace_symbols` and its underlying symbol replacement utilities to accept `instructions: string[]` instead of a single string, using a shared Set or the existing cache object to deduplicate definitions across the entire array, and update `build-prompt-payload.ts` and `build-cli-prompt.ts` to pass arrays instead of joining strings with a delimiter. └── apps └── editor └── src ├── features │ └── agentic-search │ └── agentic-search.ts └── views └── prompt └── backend ├── message-handlers │ ├── handle-autofill.ts │ ├── handle-copy-prompt.ts │ ├── handle-invoke-agentic-cli.ts │ ├── handle-make-api-call.ts │ ├── handle-preview-changes-symbol.ts │ ├── handle-preview-commit-symbol.ts │ ├── handle-preview-prompt.ts │ ├── handle-preview-skill-symbol.ts │ ├── handle-preview-web-configuration.ts │ └── utils │ ├── build-cli-prompt.ts │ └── build-prompt-payload.ts ├── prompt-view-provider.ts └── utils └── symbols ├── fragment │ └── replace-fragment-symbol.ts ├── git │ └── replace-git-symbols.ts ├── image │ └── replace-image-symbol.ts ├── pasted-text │ └── replace-pasted-text-symbol.ts ├── replace-symbols.ts ├── saved-context │ └── replace-saved-context-symbol.ts ├── skill │ └── replace-skill-symbol.ts ├── symbol-cache.ts └── website └── replace-website-symbol.ts
1 parent b998edc commit eb9aa9a

15 files changed

Lines changed: 890 additions & 802 deletions

‎apps/editor/src/features/agentic-search/agentic-search.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,16 @@ export const agentic_search = async (params: {
7272
default_agent_key: 'isDefaultForAgenticSearch',
7373
show_back_button: true,
7474
build_prompt: async () => {
75-
const { instructions: processed_query, skill_definitions } =
75+
const { instructions: processed_queries, skill_definitions } =
7676
await replace_symbols({
77-
instructions: query,
77+
instructions: [query],
7878
extension_context: params.extension_context,
7979
workspace_provider: params.workspace_provider,
8080
image_as_paths: true
8181
})
8282

83+
const processed_query = processed_queries[0]
84+
8385
const final_query = skill_definitions
8486
? `${processed_query}\n\n${skill_definitions}`
8587
: processed_query

‎apps/editor/src/views/prompt/backend/message-handlers/handle-preview-changes-symbol.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const handle_preview_changes_symbol = async (message: {
88
try {
99
const instruction = `#Changes(${message.branch_name})`
1010
const { changes_definitions } = await replace_changes_symbol({
11-
instruction
11+
instructions: [instruction]
1212
})
1313

1414
if (changes_definitions) {

‎apps/editor/src/views/prompt/backend/message-handlers/handle-preview-commit-symbol.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const handle_preview_commit_symbol = async (message: {
1414
: ''
1515
const instruction = `#${message.type}(${message.repo_name}:${message.commit_hash}${message_escaped})`
1616
const { commit_definitions } = await replace_commit_symbol({
17-
instruction
17+
instructions: [instruction]
1818
})
1919

2020
if (commit_definitions) {

‎apps/editor/src/views/prompt/backend/message-handlers/handle-preview-skill-symbol.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const handle_preview_skill_symbol = async (message: {
1010
try {
1111
const instruction = `#Skill(${message.agent}:${message.repo}:${message.skill_name})`
1212
const { skill_definitions } = await replace_skill_symbol({
13-
instruction
13+
instructions: [instruction]
1414
})
1515

1616
if (skill_definitions) {

‎apps/editor/src/views/prompt/backend/message-handlers/utils/build-cli-prompt.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@ export const build_cli_prompt = async (params: {
1515

1616
const current_instructions = prompt_view_provider.current_instructions.trim()
1717

18-
const delimiter = `__CWC_TASK_DELIMITER_${Math.random().toString(36).substring(2)}_${Date.now()}__`
19-
const combined_instructions = [
18+
const instructions_to_process = [
2019
current_instructions,
2120
...prompt_view_provider.completed_tasks
22-
].join(delimiter)
21+
]
2322

24-
const { instructions: processed_combined, skill_definitions } =
23+
const { instructions: processed_instructions_array, skill_definitions } =
2524
await replace_symbols({
26-
instructions: combined_instructions,
25+
instructions: instructions_to_process,
2726
extension_context: prompt_view_provider.extension_context,
2827
workspace_provider: prompt_view_provider.workspace_provider,
2928
image_as_paths: true,
3029
symbols_cache: prompt_view_provider.symbols_cache
3130
})
3231

33-
const split_processed = processed_combined.split(delimiter)
34-
const processed_query = split_processed[0]
35-
const processed_completed_tasks = split_processed.slice(1)
32+
const processed_query = processed_instructions_array[0]
33+
const processed_completed_tasks = processed_instructions_array.slice(1)
3634

3735
const roots = prompt_view_provider.workspace_provider.get_workspace_roots()
3836

‎apps/editor/src/views/prompt/backend/message-handlers/utils/build-prompt-payload.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@ export const build_prompt_payload = async (params: {
2626
recent_files = collected.recent_files
2727
collected_files = other_files + recent_files
2828

29-
const delimiter = `__CWC_TASK_DELIMITER_${Math.random().toString(36).substring(2)}_${Date.now()}__`
30-
const combined_instructions = [
29+
const instructions_to_process = [
3130
prompt_view_provider.current_instructions.trim() || '',
3231
...prompt_view_provider.completed_tasks
33-
].join(delimiter)
32+
]
3433

35-
const { instructions: processed_combined, skill_definitions } =
34+
const { instructions: processed_instructions_array, skill_definitions } =
3635
await replace_symbols({
37-
instructions: combined_instructions,
36+
instructions: instructions_to_process,
3837
extension_context: prompt_view_provider.extension_context,
3938
workspace_provider: prompt_view_provider.workspace_provider,
4039
remove_images,
4140
symbols_cache: prompt_view_provider.symbols_cache
4241
})
4342

44-
const split_processed = processed_combined.split(delimiter)
45-
const processed_instructions = split_processed[0]
46-
const processed_completed_tasks = split_processed.slice(1)
43+
const processed_instructions = processed_instructions_array[0]
44+
const processed_completed_tasks = processed_instructions_array.slice(1)
4745

4846
return {
4947
other_files,

‎apps/editor/src/views/prompt/backend/prompt-view-provider.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,14 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
562562

563563
public async send_token_count() {
564564
const edit_instructions_result = await replace_symbols({
565-
instructions: this.current_edit_files_instruction.trim(),
565+
instructions: [this.current_edit_files_instruction.trim()],
566566
extension_context: this.extension_context,
567567
workspace_provider: this.workspace_provider,
568568
remove_images: true,
569569
symbols_cache: this.symbols_cache
570570
})
571571
const ask_instructions_result = await replace_symbols({
572-
instructions: this.current_ask_about_context_instruction.trim(),
572+
instructions: [this.current_ask_about_context_instruction.trim()],
573573
extension_context: this.extension_context,
574574
workspace_provider: this.workspace_provider,
575575
remove_images: true,
@@ -580,12 +580,12 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
580580
command: 'TOKEN_COUNT_UPDATED',
581581
selected_files_token_count: this.current_selected_files_token_count,
582582
edit_instructions_token_count: Math.ceil(
583-
(edit_instructions_result.instructions.length +
583+
(edit_instructions_result.instructions[0].length +
584584
edit_instructions_result.skill_definitions.length) /
585585
4
586586
),
587587
ask_instructions_token_count: Math.ceil(
588-
(ask_instructions_result.instructions.length +
588+
(ask_instructions_result.instructions[0].length +
589589
ask_instructions_result.skill_definitions.length) /
590590
4
591591
)

‎apps/editor/src/views/prompt/backend/utils/symbols/fragment/replace-fragment-symbol.ts‎

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,85 @@ import * as vscode from 'vscode'
44
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
55

66
export const replace_fragment_symbol = async (params: {
7-
instruction: string
7+
instructions: string[]
88
workspace_provider: WorkspaceProvider
9-
}): Promise<string> => {
10-
const regex = /#Fragment\((.+?):(\d+):(\d+)-(\d+):(\d+)\)/g
9+
}): Promise<string[]> => {
10+
return Promise.all(
11+
params.instructions.map(async (instruction) => {
12+
const regex = /#Fragment\((.+?):(\d+):(\d+)-(\d+):(\d+)\)/g
1113

12-
const matches = [...params.instruction.matchAll(regex)]
13-
if (matches.length === 0) return params.instruction
14+
const matches = [...instruction.matchAll(regex)]
15+
if (matches.length === 0) return instruction
1416

15-
let result = params.instruction
17+
let result = instruction
1618

17-
for (const match of matches) {
18-
const full_match = match[0]
19-
const file_path = match[1]
20-
const start_line = parseInt(match[2], 10)
21-
const end_line = parseInt(match[4], 10)
19+
for (const match of matches) {
20+
const full_match = match[0]
21+
const file_path = match[1]
22+
const start_line = parseInt(match[2], 10)
23+
const end_line = parseInt(match[4], 10)
2224

23-
let content = ''
24-
try {
25-
let absolute_path = file_path
26-
if (!path.isAbsolute(file_path)) {
27-
const roots = params.workspace_provider.get_workspace_roots()
28-
for (const root of roots) {
29-
const test_path = path.join(root, file_path)
30-
if (fs.existsSync(test_path)) {
31-
absolute_path = test_path
32-
break
33-
}
25+
let content = ''
26+
try {
27+
let absolute_path = file_path
28+
if (!path.isAbsolute(file_path)) {
29+
const roots = params.workspace_provider.get_workspace_roots()
30+
for (const root of roots) {
31+
const test_path = path.join(root, file_path)
32+
if (fs.existsSync(test_path)) {
33+
absolute_path = test_path
34+
break
35+
}
3436

35-
if (roots.length > 1) {
36-
const workspace_name =
37-
params.workspace_provider.get_workspace_name(root)
38-
const prefix = `${workspace_name}/`
39-
const prefix_win = `${workspace_name}\\`
37+
if (roots.length > 1) {
38+
const workspace_name =
39+
params.workspace_provider.get_workspace_name(root)
40+
const prefix = `${workspace_name}/`
41+
const prefix_win = `${workspace_name}\\`
4042

41-
if (
42-
file_path.startsWith(prefix) ||
43-
file_path.startsWith(prefix_win)
44-
) {
45-
const stripped_test_path = path.join(
46-
root,
47-
file_path.substring(prefix.length)
48-
)
49-
if (fs.existsSync(stripped_test_path)) {
50-
absolute_path = stripped_test_path
51-
break
43+
if (
44+
file_path.startsWith(prefix) ||
45+
file_path.startsWith(prefix_win)
46+
) {
47+
const stripped_test_path = path.join(
48+
root,
49+
file_path.substring(prefix.length)
50+
)
51+
if (fs.existsSync(stripped_test_path)) {
52+
absolute_path = stripped_test_path
53+
break
54+
}
55+
}
5256
}
5357
}
5458
}
55-
}
56-
}
5759

58-
const file_uri = vscode.Uri.file(absolute_path)
59-
const document = await vscode.workspace.openTextDocument(file_uri)
60+
const file_uri = vscode.Uri.file(absolute_path)
61+
const document = await vscode.workspace.openTextDocument(file_uri)
6062

61-
const start_pos = new vscode.Position(
62-
start_line - 1,
63-
parseInt(match[3], 10) - 1
64-
)
65-
const end_pos = new vscode.Position(
66-
end_line - 1,
67-
parseInt(match[5], 10) - 1
68-
)
69-
const valid_range = document.validateRange(
70-
new vscode.Range(start_pos, end_pos)
71-
)
72-
content = document.getText(valid_range)
73-
} catch (e) {
74-
content = `// Error reading file ${file_path}`
75-
}
63+
const start_pos = new vscode.Position(
64+
start_line - 1,
65+
parseInt(match[3], 10) - 1
66+
)
67+
const end_pos = new vscode.Position(
68+
end_line - 1,
69+
parseInt(match[5], 10) - 1
70+
)
71+
const valid_range = document.validateRange(
72+
new vscode.Range(start_pos, end_pos)
73+
)
74+
content = document.getText(valid_range)
75+
} catch (e) {
76+
content = `// Error reading file ${file_path}`
77+
}
7678

77-
const range_str = ` (${start_line}-${end_line})`
78-
const replacement = `\n\n\`${file_path}\`${range_str}\n\n\`\`\`\n${content}\n\`\`\`\n\n`
79+
const range_str = ` (${start_line}-${end_line})`
80+
const replacement = `\n\n\`${file_path}\`${range_str}\n\n\`\`\`\n${content}\n\`\`\`\n\n`
7981

80-
result = result.replace(full_match, replacement)
81-
}
82+
result = result.replace(full_match, replacement)
83+
}
8284

83-
return result
85+
return result
86+
})
87+
)
8488
}

0 commit comments

Comments
 (0)