Skip to content

Commit cb365f3

Browse files
authored
prompts: avoid wrapping file attachments in code fences (#569)
* prompts: avoid wrapping file attachments in code fences We previously included attached files like this ``` <attachment id="test.js" filePath="/home/jola/test.js"> \```javascript bar asd ( \``` </attachment> ``` Which, very reasonably, caused the model to get confused and think that the file actually contained those backticks. This change removes that so that it's just ``` <attachment id="test.js" filePath="/home/jola/test.js"> bar asd ( </attachment> ``` Refs microsoft/vscode#260772 (comment) Busted all the caches with associated baseline noise. * fixup
1 parent b094865 commit cb365f3

20 files changed

+80
-78
lines changed

src/extension/prompts/node/agent/test/__snapshots__/agentPrompt.spec.tsx.snap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,11 @@ copilot_cache_control: { type: 'ephemeral' }
345345
~~~md
346346
<attachments>
347347
<attachment id="file" filePath="/workspace/file.ts">
348-
\`\`\`ts
349348
line 1
350349
line 2
351350
352351
line 4
353352
line 5
354-
\`\`\`
355353
</attachment>
356354
357355
</attachments>
@@ -829,13 +827,11 @@ copilot_cache_control: { type: 'ephemeral' }
829827
~~~md
830828
<attachments>
831829
<attachment id="file" filePath="/workspace/file.ts">
832-
\`\`\`ts
833830
line 1
834831
line 2
835832
836833
line 4
837834
line 5
838-
\`\`\`
839835
</attachment>
840836
841837
</attachments>
@@ -1021,13 +1017,11 @@ copilot_cache_control: { type: 'ephemeral' }
10211017
~~~md
10221018
<attachments>
10231019
<attachment id="file" filePath="/workspace/file.ts">
1024-
\`\`\`ts
10251020
line 1
10261021
line 2
10271022
10281023
line 4
10291024
line 5
1030-
\`\`\`
10311025
</attachment>
10321026
10331027
</attachments>

src/extension/prompts/node/agent/test/__snapshots__/summarization-duringToolCalling-FullSumm.spec.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
~~~md
33
<attachments>
44
<attachment id="file" filePath="/workspace/file.ts">
5-
```ts
65
line 1
76
line 2
87

98
line 4
109
line 5
11-
```
1210
</attachment>
1311

1412
</attachments>

src/extension/prompts/node/agent/test/__snapshots__/summarization-previousTurnMultiple-Agent.spec.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ copilot_cache_control: { type: 'ephemeral' }
5252
~~~md
5353
<attachments>
5454
<attachment id="file" filePath="/workspace/file.ts">
55-
```ts
5655
line 1
5756
line 2
5857

5958
line 4
6059
line 5
61-
```
6260
</attachment>
6361

6462
</attachments>

src/extension/prompts/node/agent/test/__snapshots__/summarization-previousTurnNoRounds-Agent.spec.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ copilot_cache_control: { type: 'ephemeral' }
6969
~~~md
7070
<attachments>
7171
<attachment id="file" filePath="/workspace/file.ts">
72-
```ts
7372
line 1
7473
line 2
7574

7675
line 4
7776
line 5
78-
```
7977
</attachment>
8078

8179
</attachments>

src/extension/prompts/node/panel/fileVariable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class CodeSummary extends PromptElement<CodeSummaryProps, unknown> {
262262
return (
263263
<Tag name='attachment' attrs={attrs} >
264264
{this.props.description ? this.props.description + ':\n' : ''}
265-
<CodeBlock includeFilepath={this.props.filePathMode === FilePathMode.AsComment} languageId={document.languageId} uri={uri} references={references} code={code} />
265+
<CodeBlock includeFilepath={this.props.filePathMode === FilePathMode.AsComment} languageId={document.languageId} uri={uri} references={references} code={code} fence='' />
266266
</Tag>
267267
);
268268
}

src/extension/prompts/node/panel/safeElements.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export type CodeBlockProps = PromptElementProps<{
6767
* @default true
6868
*/
6969
readonly shouldTrim?: boolean;
70+
71+
/**
72+
* Fence style, defaults to '```'. An empty string omits the fence.
73+
*/
74+
readonly fence?: string;
7075
}>;
7176

7277
export class CodeBlock extends SafePromptElement<CodeBlockProps> {
@@ -87,7 +92,7 @@ export class CodeBlock extends SafePromptElement<CodeBlockProps> {
8792
return this._handleFoulPrompt();
8893
}
8994
const filePath = this.props.includeFilepath ? this._promptPathRepresentationService.getFilePath(this.props.uri) : undefined;
90-
const code = createFencedCodeBlock(this.props.languageId ?? '', this.props.code, this.props.shouldTrim ?? true, filePath);
95+
const code = createFencedCodeBlock(this.props.languageId ?? '', this.props.code, this.props.shouldTrim ?? true, filePath, this.props.fence);
9196
const reference = this.props.references && <references value={this.props.references} />;
9297

9398
if (this.props.lineBasedPriority) {

src/extension/prompts/node/panel/test/__snapshots__/fileVariable.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ exports[`FileVariable > does include known untitled file 1`] = `
5757
{
5858
"type": 2,
5959
"priority": 9007199254740991,
60-
"text": "\`\`\`python\\ntest!\\n\`\`\`",
60+
"text": "test!",
6161
"references": [
6262
{
6363
"anchor": {

src/util/common/markdown.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ export function createFilepathRegexp(languageId?: string): RegExp {
4444
* Create a markdown code block with an optional language id and an optional file path.
4545
* @param filePath The file path to include in the code block. To create the file path use the {@link IPromptPathRepresentationService}
4646
*/
47-
export function createFencedCodeBlock(languageId: string, code: string, shouldTrim = true, filePath?: string, minNumberOfBackticks = 3): string {
48-
const fence = getFenceForCodeBlock(code, minNumberOfBackticks);
47+
export function createFencedCodeBlock(languageId: string, code: string, shouldTrim = true, filePath?: string, minNumberOfBackticksOrStyle: string | number = 3): string {
48+
const fence = typeof minNumberOfBackticksOrStyle === 'number'
49+
? getFenceForCodeBlock(code, minNumberOfBackticksOrStyle)
50+
: minNumberOfBackticksOrStyle;
4951

5052
let filepathComment = '';
5153
if (filePath) {
5254
filepathComment = getFilepathComment(languageId, filePath);
5355
}
5456

55-
return `${fence}${languageIdToMDCodeBlockLang(languageId)}\n${filepathComment}${shouldTrim ? code.trim() : code}\n${fence}`;
57+
return `${fence}${fence && (languageIdToMDCodeBlockLang(languageId) + '\n')}${filepathComment}${shouldTrim ? code.trim() : code}${fence && ('\n' + fence)}`;
5658
}
5759

5860
export function getFilepathComment(languageId: string, filePath: string): string {

test/outcome/-doc-inline.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
{
111111
"name": "/doc [inline] [typescript] - supports chat variables",
112112
"requests": [
113-
"6cedff5109b6ec584c5c8c677d6ab534fbfe771bde11ab4a1f392fcd0d6309cf"
113+
"2c7eaaa330d710106d2cacb58b7fcb5e01598d15f75c5f2af08089b543f1b2b1"
114114
]
115115
}
116116
]

test/outcome/-tests-inline.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
{
130130
"name": "/tests [inline] [typescript] - supports chat variables",
131131
"requests": [
132-
"591150e19e6ccfc73da99c993bed686a4992bf1c16ac743cfd963621eb156000"
132+
"1c6d4cb42ef1e2b49c136000de18ff4273d4c01297f97850ef4aad0ac1098940"
133133
]
134134
},
135135
{

0 commit comments

Comments
 (0)