Skip to content

Commit 9973e93

Browse files
committed
Refactor the API response cleanup utility to support code block extraction from surrounding text and add complete unit test coverage
1 parent 02d96a6 commit 9973e93

4 files changed

Lines changed: 127 additions & 12 deletions

File tree

‎packages/ui/src/components/editor/panel/Button/Button.module.scss‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
&:focus {
2222
outline: 1px solid var(--vscode-focusBorder);
23-
outline-offset: 2px;
2423
}
2524

2625
&:disabled {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { cleanup_api_response } from './cleanup-api-response'
2+
3+
describe('cleanup_api_response', () => {
4+
it('should return plain text as is', () => {
5+
const content = 'This is some plain text.'
6+
expect(cleanup_api_response({ content })).toBe('This is some plain text.')
7+
})
8+
9+
it('should trim whitespace from the final result', () => {
10+
const content = ' \n some text \n '
11+
expect(cleanup_api_response({ content })).toBe('some text')
12+
})
13+
14+
it('should handle empty string input', () => {
15+
const content = ''
16+
expect(cleanup_api_response({ content })).toBe('')
17+
})
18+
19+
describe('wrapper removal', () => {
20+
it('should remove markdown code block wrappers', () => {
21+
const content = '```\nconst a = 1;\n```'
22+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
23+
})
24+
25+
it('should remove markdown code block wrappers with language specifier', () => {
26+
const content = '```javascript\nconst a = 1;\n```'
27+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
28+
})
29+
30+
it('should extract a markdown code block with surrounding text', () => {
31+
const content =
32+
'Here is the code you requested:\n```javascript\nconst a = 1;\n```\nThanks!'
33+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
34+
})
35+
36+
it('should remove <file> tags', () => {
37+
const content = '<file path="test.ts">\nconst a = 1;\n</file>'
38+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
39+
})
40+
41+
it('should remove <files> tags', () => {
42+
const content = '<files>\nconst a = 1;\n</files>'
43+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
44+
})
45+
46+
it('should remove CDATA wrappers', () => {
47+
const content = '<![CDATA[\nconst a = 1;\n]]>'
48+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
49+
})
50+
51+
it('should handle incomplete CDATA closing tag', () => {
52+
const content = '<![CDATA[const a = 1;]]'
53+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
54+
})
55+
56+
it('should remove DOCTYPE declaration', () => {
57+
const content = '<!DOCTYPE html>\n<html></html>'
58+
expect(cleanup_api_response({ content })).toBe('<html></html>')
59+
})
60+
61+
it('should handle nested wrappers iteratively', () => {
62+
const content =
63+
'```typescript\n<file path="test.ts">\n<![CDATA[\nconsole.log("hello");\n]]>\n</file>\n```'
64+
expect(cleanup_api_response({ content })).toBe('console.log("hello");')
65+
})
66+
67+
it('should handle content with only wrappers', () => {
68+
const content = '```\n<file path="test.ts">\n</file>\n```'
69+
expect(cleanup_api_response({ content })).toBe('')
70+
})
71+
72+
it('should not remove wrappers from the middle of the content', () => {
73+
const content = 'Some text\n```\ncode\n```\nSome other text'
74+
expect(cleanup_api_response({ content })).toBe(
75+
'Some text\n```\ncode\n```\nSome other text'
76+
)
77+
})
78+
})
79+
80+
describe('special tag removal', () => {
81+
it('should remove <think> block from the beginning', () => {
82+
const content = '<think>I should write some code.</think>\nconst a = 1;'
83+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
84+
})
85+
86+
it('should remove <thought> block from the beginning', () => {
87+
const content =
88+
'<thought>I should write some code.</thought>\nconst a = 1;'
89+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
90+
})
91+
92+
it('should handle <think> block with newlines', () => {
93+
const content =
94+
'<think>\nI should write some code.\n</think>\nconst a = 1;'
95+
expect(cleanup_api_response({ content })).toBe('const a = 1;')
96+
})
97+
98+
it('should only remove <think> block if it is at the start', () => {
99+
const content = 'const a = 1;\n<think>I should write some code.</think>'
100+
expect(cleanup_api_response({ content })).toBe(
101+
'const a = 1;\n<think>I should write some code.</think>'
102+
)
103+
})
104+
})
105+
})

packages/vscode/src/utils/cleanup-api-response.ts renamed to packages/vscode/src/utils/cleanup-api-response/cleanup-api-response.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* at the beginning and end of the content, without affecting the middle content.
44
* Used by intelligent update/code completions command.
55
*/
6-
export function cleanup_api_response(params: { content: string }): string {
6+
export const cleanup_api_response = (params: { content: string }): string => {
77
let content = params.content
88
let changed = true
99

@@ -19,15 +19,26 @@ export function cleanup_api_response(params: { content: string }): string {
1919
}
2020
}
2121

22+
const code_block_count = (content.match(/```/g) || []).length
23+
if (code_block_count === 2) {
24+
const match = content.match(/```[^\n]*\n([\s\S]*?)\s*```/)
25+
if (match && typeof match[1] === 'string' && match.index !== undefined) {
26+
const before_text = content.substring(0, match.index).trim()
27+
if (before_text.endsWith(':')) {
28+
content = match[1]
29+
}
30+
}
31+
}
32+
2233
while (changed) {
2334
const original_content = content
2435

2536
const opening_patterns = [
26-
/^```[^\n]*\n/, // Markdown code block start
27-
/^<files[^>]*>\s*\n?/, // Files wrapper start
28-
/^<file[^>]*>\s*\n?/, // File wrapper start
29-
/^<!\[CDATA\[\s*\n?/, // CDATA start
30-
/^<!DOCTYPE[^>]*>\s*\n?/ // DOCTYPE declaration
37+
/^```[^\n]*\n/,
38+
/^<files[^>]*>\s*\n?/,
39+
/^<file[^>]*>\s*\n?/,
40+
/^<!\[CDATA\[\s*\n?/,
41+
/^<!DOCTYPE[^>]*>\s*\n?/
3142
]
3243

3344
for (const pattern of opening_patterns) {
@@ -39,11 +50,10 @@ export function cleanup_api_response(params: { content: string }): string {
3950
}
4051

4152
const closing_patterns = [
42-
/\s*```\s*$/, // Markdown code block end
43-
/\s*<\/files>\s*$/, // Files wrapper end
44-
/\s*<\/file>\s*$/, // File wrapper end
45-
/\s*\]\]>\s*$/, // CDATA end
46-
/\s*\]\]\s*$/ // Potentially incomplete CDATA end (just "]]")
53+
/\s*```\s*$/,
54+
/\s*<\/files>\s*$/,
55+
/\s*<\/file>\s*$/,
56+
/\s*\]\]>\s*$/
4757
]
4858

4959
for (const pattern of closing_patterns) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './cleanup-api-response'

0 commit comments

Comments
 (0)