Skip to content

Commit 1393655

Browse files
committed
Standardize prompt symbols and edit format instructions to use markdown formatting instead of XML tags
- the 'changes' symbol should use markdown heading instead of XML ('# Changes' heading, - File: ``). below when referenced in instructions use [CHANGES] placeholder - regarding 'changes' symbol: append branch name below '# Changes' heading and in symbol (in instructions. Example: # Changes **Branch:** main - File: `` symbol in instructions: [CHANGES:main] - multiple 'changes' symbols should share a single '# Changes' heading. Each branch inside should start with e.g. ## Branch "main" - for 'changes' symbol: instead of [Pasted text], use separate e.g. `# Diff with main` headings - for 'changes' symbol: the reference in instructions should not look like [CHANGES:main], but like link to section - following the approach to 'changes' symbol, update 'commit' symbol handling accordingly - regarding 'commit' symbol handling - don't hash placed in markdown should be of shorter form (7 characters) - regarding 'commit' symbol handling - don't place commit message in markdown - following 'changes' and 'commit' symbol handling, update 'saved context' to use markdown instead of xml as well - change `# Context: XYZ` to `# XYZ` └── apps └── editor └── src ├── utils │ └── files-collector.ts └── views └── prompt └── backend ├── message-handlers │ └── handle-autofill.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 ├── selection │ └── replace-selection-symbol.ts ├── skill │ └── replace-skill-symbol.ts └── website └── replace-website-symbol.ts
1 parent b5d91d1 commit 1393655

3 files changed

Lines changed: 88 additions & 57 deletions

File tree

‎apps/editor/src/constants/edit-format-instructions.ts‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const intro = `Whenever showing a new, updated, renamed, or deleted file, provide a brief explanation, then print the path in a markdown heading (e.g. ### New file: \`src/examples/hello.py\`, ### Updated file: \`src/examples/hello.py\`, ### Renamed file: \`src/examples/hello.py\` (old) \`src/welcome.py\` (new), ### Deleted file: \`src/examples/hello.py\`)`
1+
const intro = `Whenever showing a new, updated, renamed, or deleted file, provide a brief explanation, then print the path in a markdown heading (e.g. - New file: \`src/examples/hello.py\`, - Updated file: \`src/examples/hello.py\`, - Renamed file: \`src/examples/hello.py\` (old) \`src/welcome.py\` (new), - Deleted file: \`src/examples/hello.py\`)`
22

33
export const EDIT_FORMAT_INSTRUCTIONS_WHOLE = `${intro}, followed by a markdown code block with the file's FULL (from cover to cover) contents. Example:
44
Updated the greeting text.
55
6-
### Updated file: \`src/examples/hello.py\`
6+
- Updated file: \`src/examples/hello.py\`
77
88
\`\`\`python
99
GREETING = "Hello, World!"
@@ -14,7 +14,7 @@ def show_greeting():
1414
1515
Created a simple calculator with addition and subtraction.
1616
17-
### Created file: \`src/examples/calculator.py\`
17+
- Created file: \`src/examples/calculator.py\`
1818
1919
\`\`\`python
2020
def add(a, b):
@@ -24,14 +24,14 @@ def subtract(a, b):
2424
return a - b
2525
\`\`\`
2626
27-
### Deleted file: \`src/examples/weekdays.py\`
27+
- Deleted file: \`src/examples/weekdays.py\`
2828
29-
### Renamed file: \`src/examples/math_utils.py\` (old) \`src/examples/calculator.py\` (new)`
29+
- Renamed file: \`src/examples/math_utils.py\` (old) \`src/examples/calculator.py\` (new)`
3030

3131
export const EDIT_FORMAT_INSTRUCTIONS_TRUNCATED = `${intro}, followed by a markdown code block with file's contents. As a space saving measure, truncate blocks like unchanged function bodies (code inside {}) by replacing with ellipsis comments. Example:
3232
Updated the greeting text.
3333
34-
### Updated file: \`src/examples/hello.js\`
34+
- Updated file: \`src/examples/hello.js\`
3535
3636
\`\`\`javascript
3737
const DURATION = 5;
@@ -48,7 +48,7 @@ function showGreeting() {
4848
4949
Updated the purpose text.
5050
51-
### Updated file: \`src/README.md\`
51+
- Updated file: \`src/README.md\`
5252
5353
\`\`\`markdown
5454
## Purpose
@@ -66,7 +66,7 @@ Greet user saying "Hello, World!", for a duration of 5 seconds.
6666
6767
Created a simple calculator with addition and subtraction.
6868
69-
### Created file: \`src/examples/calculator.js\`
69+
- Created file: \`src/examples/calculator.js\`
7070
7171
\`\`\`javascript
7272
function add(a, b) {
@@ -78,14 +78,14 @@ function subtract(a, b) {
7878
}
7979
\`\`\`
8080
81-
### Deleted file: \`src/examples/weekdays.js\`
81+
- Deleted file: \`src/examples/weekdays.js\`
8282
83-
### Renamed file: \`src/examples/math-tools.js\` (old) \`src/examples/calculator.js\` (new)`
83+
- Renamed file: \`src/examples/math-tools.js\` (old) \`src/examples/calculator.js\` (new)`
8484

8585
export const EDIT_FORMAT_INSTRUCTIONS_DIFF = `${intro}, followed by a unified diff within a markdown code block. Example:
8686
Updated the greeting text.
8787
88-
### Updated file: \`src/examples/hello.py\`
88+
- Updated file: \`src/examples/hello.py\`
8989
9090
\`\`\`diff
9191
--- a/src/examples/hello.py
@@ -101,7 +101,7 @@ Updated the greeting text.
101101
102102
Created a simple calculator with addition and subtraction.
103103
104-
### Created file: \`src/examples/calculator.py\`
104+
- Created file: \`src/examples/calculator.py\`
105105
106106
\`\`\`python
107107
def add(a, b):
@@ -111,14 +111,14 @@ def subtract(a, b):
111111
return a - b
112112
\`\`\`
113113
114-
### Deleted file: \`src/examples/weekdays.py\`
114+
- Deleted file: \`src/examples/weekdays.py\`
115115
116-
### Renamed file: \`src/examples/math_utils.py\` (old) \`src/examples/calculator.py\` (new)`
116+
- Renamed file: \`src/examples/math_utils.py\` (old) \`src/examples/calculator.py\` (new)`
117117

118118
export const EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE = `${intro}, followed by a markdown code block showing the original and updated code snippets with Git-style merge conflict syntax. Example:
119119
Changed greeting to wishes.
120120
121-
### Updated file: \`src/examples/hello.py\`
121+
- Updated file: \`src/examples/hello.py\`
122122
123123
\`\`\`python
124124
<<<<<<< SEARCH
@@ -137,7 +137,7 @@ WISHES = "Have a nice day!"
137137
138138
Created a simple calculator with addition and subtraction.
139139
140-
### Created file: \`src/examples/calculator.py\`
140+
- Created file: \`src/examples/calculator.py\`
141141
142142
\`\`\`python
143143
def add(a, b):
@@ -147,6 +147,6 @@ def subtract(a, b):
147147
return a - b
148148
\`\`\`
149149
150-
### Deleted file: \`src/examples/weekdays.py\`
150+
- Deleted file: \`src/examples/weekdays.py\`
151151
152-
### Renamed file: \`src/examples/math_utils.py\` (old) \`src/examples/calculator.py\` (new)`
152+
- Renamed file: \`src/examples/math_utils.py\` (old) \`src/examples/calculator.py\` (new)`

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

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ const patch_diff_paths = (
4141
return lines.join('\n')
4242
}
4343

44-
const build_changes_xml = (
44+
const build_changes_markdown = (
4545
diff: string,
4646
cwd: string,
4747
diff_base: string,
48+
branch_name: string,
4849
path_prefix?: string
4950
): string => {
5051
// Split diff into per-file sections. Each section starts with 'diff --git '.
@@ -85,7 +86,7 @@ const build_changes_xml = (
8586
? `${path_prefix}/${file_path}`
8687
: file_path
8788

88-
changes_content += `<file path="${display_path}">\n`
89+
changes_content += `- File: \`${display_path}\`\n\n`
8990

9091
let file_content = ''
9192
try {
@@ -100,7 +101,7 @@ const build_changes_xml = (
100101
// In this case, the original content is correctly an empty string.
101102
if (!is_deleted) {
102103
Logger.warn({
103-
function_name: 'build_changes_xml',
104+
function_name: 'build_changes_markdown',
104105
message: `Could not get file content from git base ${diff_base} for path ${file_path}. Assuming it's a new file.`,
105106
data: e
106107
})
@@ -117,15 +118,16 @@ const build_changes_xml = (
117118
)
118119
}
119120

120-
changes_content += `\`\`\`\n${full_file_diff}\n\`\`\`\n`
121+
changes_content += `\`\`\`\n${full_file_diff}\n\`\`\`\n\n`
121122
if (file_content) {
122-
changes_content += `\`\`\`\n${file_content}\n\`\`\`\n`
123+
changes_content += `\`\`\`\n${file_content}\n\`\`\`\n\n`
123124
}
124-
changes_content += `</file>\n`
125125
}
126126
}
127127

128-
return changes_content ? `<changes>\n${changes_content}</changes>\n` : ''
128+
return changes_content
129+
? `# Diff with ${branch_name}\n\n${changes_content}`
130+
: ''
129131
}
130132

131133
export const replace_changes_symbol = async (params: {
@@ -215,16 +217,21 @@ export const replace_changes_symbol = async (params: {
215217
continue
216218
}
217219

218-
const replacement_text = build_changes_xml(
220+
const replacement_text = build_changes_markdown(
219221
diff,
220222
target_folder.uri.fsPath,
221223
diff_base,
224+
branch_name,
222225
workspace_folders.length > 1 ? folder_name : undefined
223226
)
224227
changes_definitions += replacement_text
228+
const link_hash = `diff-with-${branch_name
229+
.toLowerCase()
230+
.replace(/[^a-z0-9]+/g, '-')
231+
.replace(/^-|-$/g, '')}`
225232
result_instruction = result_instruction.replace(
226233
replacement_regex,
227-
` <changes branch="${branch_spec}" /> `
234+
` [Diff with ${branch_name}](#${link_hash}) `
228235
)
229236
} catch (error) {
230237
vscode.window.showErrorMessage(
@@ -284,15 +291,20 @@ export const replace_changes_symbol = async (params: {
284291
continue
285292
}
286293

287-
const replacement_text = build_changes_xml(
294+
const replacement_text = build_changes_markdown(
288295
diff,
289296
repository.rootUri.fsPath,
290-
diff_base
297+
diff_base,
298+
branch_name
291299
)
292300
changes_definitions += replacement_text
301+
const link_hash = `diff-with-${branch_name
302+
.toLowerCase()
303+
.replace(/[^a-z0-9]+/g, '-')
304+
.replace(/^-|-$/g, '')}`
293305
result_instruction = result_instruction.replace(
294306
replacement_regex,
295-
` <changes branch="${branch_spec}" /> `
307+
` [Diff with ${branch_name}](#${link_hash}) `
296308
)
297309
} catch (error) {
298310
vscode.window.showErrorMessage(
@@ -313,11 +325,10 @@ export const replace_changes_symbol = async (params: {
313325
return { instruction: result_instruction, changes_definitions }
314326
}
315327

316-
const build_commit_changes_xml = (
328+
const build_commit_changes_markdown = (
317329
diff: string,
318330
cwd: string,
319331
commit_hash: string,
320-
commit_message?: string,
321332
path_prefix?: string
322333
): string => {
323334
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
@@ -354,28 +365,29 @@ const build_commit_changes_xml = (
354365
}
355366

356367
if (file_path) {
368+
const display_path = path_prefix
369+
? `${path_prefix}/${file_path}`
370+
: file_path
371+
372+
changes_content += `- File: \`${display_path}\`\n\n`
373+
357374
let file_content = ''
358375
if (!is_deleted) {
359376
try {
360377
file_content = execSync(`git show ${commit_hash}:"./${file_path}"`, {
361378
cwd,
362-
encoding: 'utf-8'
379+
encoding: 'utf-8',
380+
stdio: ['pipe', 'pipe', 'ignore']
363381
})
364382
} catch (e) {
365383
Logger.error({
366-
function_name: 'build_commit_changes_xml',
384+
function_name: 'build_commit_changes_markdown',
367385
message: `Could not read file for diff from commit: ${file_path}`,
368386
data: e
369387
})
370388
}
371389
}
372390

373-
const display_path = path_prefix
374-
? `${path_prefix}/${file_path}`
375-
: file_path
376-
377-
changes_content += `<file path="${display_path}">\n`
378-
379391
if (path_prefix) {
380392
full_file_diff = patch_diff_paths(
381393
full_file_diff,
@@ -386,17 +398,17 @@ const build_commit_changes_xml = (
386398
)
387399
}
388400

389-
changes_content += `\`\`\`\n${full_file_diff}\n\`\`\`\n`
401+
changes_content += `\`\`\`\n${full_file_diff}\n\`\`\`\n\n`
390402
if (file_content) {
391-
changes_content += `\`\`\`\n${file_content}\n\`\`\`\n`
403+
changes_content += `\`\`\`\n${file_content}\n\`\`\`\n\n`
392404
}
393-
changes_content += `</file>\n`
394405
}
395406
}
396407

397408
if (changes_content) {
398-
const safe_message = (commit_message || '').replace(/"/g, '&quot;')
399-
return `<commit message="${safe_message}">\n${changes_content}</commit>\n`
409+
const short_hash = commit_hash.substring(0, 7)
410+
const title_text = `Commit ${short_hash}`
411+
return `# ${title_text}\n\n${changes_content}`
400412
}
401413
return ''
402414
}
@@ -452,22 +464,29 @@ export const replace_commit_symbol = async (params: {
452464
continue
453465
}
454466

455-
const replacement_text = build_commit_changes_xml(
467+
const replacement_text = build_commit_changes_markdown(
456468
diff,
457469
target_folder.uri.fsPath,
458470
commit_hash,
459-
commit_message,
460471
workspace_folders.length > 1 ? folder_name : undefined
461472
)
462473
commit_definitions += replacement_text
463474

464-
const safe_message = (commit_message || '').replace(/"/g, '&quot;')
475+
const short_hash = commit_hash.substring(0, 7)
476+
const header_text = `Commit ${short_hash}`
477+
const title_text = commit_message
478+
? `Commit ${short_hash} (${commit_message})`
479+
: `Commit ${short_hash}`
480+
const link_hash = header_text
481+
.toLowerCase()
482+
.replace(/[^a-z0-9]+/g, '-')
483+
.replace(/^-|-$/g, '')
465484

466485
result_instruction = result_instruction.replace(
467486
new RegExp(
468487
`\\s*${full_match.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*`
469488
),
470-
` <commit message="${safe_message}" /> `
489+
` [${title_text}](#${link_hash}) `
471490
)
472491
} catch (error) {
473492
vscode.window.showErrorMessage(

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,20 @@ export const replace_saved_context_symbol = async (params: {
4040
context_name = quoted_match[1]
4141
}
4242

43+
const escaped_match = full_match.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
44+
const replacement_regex = new RegExp(`\\s*${escaped_match}\\s*`, 'g')
45+
46+
if (!result_instruction.match(replacement_regex)) {
47+
continue
48+
}
49+
4350
const saved_context = all_contexts.find((c) => c.name === context_name)
4451

4552
if (!saved_context) {
4653
vscode.window.showWarningMessage(
4754
`Saved context "${context_name}" not found.`
4855
)
49-
result_instruction = result_instruction.replace(full_match, '')
56+
result_instruction = result_instruction.replace(replacement_regex, ' ')
5057
continue
5158
}
5259

@@ -56,18 +63,19 @@ export const replace_saved_context_symbol = async (params: {
5663
workspace_provider: params.workspace_provider
5764
})
5865

59-
let files_xml = ''
66+
let files_markdown = ''
6067
for (const p of paths) {
6168
try {
6269
if (fs.existsSync(p) && fs.statSync(p).isFile()) {
6370
const content = fs.readFileSync(p, 'utf-8')
6471
const root = params.workspace_provider.get_workspace_root_for_file(p)
6572
const relative_path = root ? path.relative(root, p) : p
6673

67-
files_xml += `<file path="${relative_path.replace(
74+
const backticks = content.includes('```') ? '````' : '```'
75+
files_markdown += `- File: \`${relative_path.replace(
6876
/\\/g,
6977
'/'
70-
)}">\n\`\`\`\n${content}\n\`\`\`\n</file>\n`
78+
)}\`\n\n${backticks}\n${content}\n${backticks}\n\n`
7179
}
7280
} catch (error) {
7381
Logger.warn({
@@ -78,13 +86,17 @@ export const replace_saved_context_symbol = async (params: {
7886
}
7987
}
8088

81-
if (files_xml) {
82-
context_definitions += `<context name="${context_name}">\n${files_xml}</context>\n`
89+
if (files_markdown) {
90+
context_definitions += `# ${context_name}\n\n${files_markdown}`
8391
}
8492

93+
const link_hash = context_name
94+
.toLowerCase()
95+
.replace(/[^a-z0-9]+/g, '-')
96+
.replace(/^-|-$/g, '')
8597
result_instruction = result_instruction.replace(
86-
full_match,
87-
() => `<context name="${context_name}" />`
98+
replacement_regex,
99+
` [${context_name}](#${link_hash}) `
88100
)
89101
}
90102

0 commit comments

Comments
 (0)