Skip to content

Commit a2b2373

Browse files
committed
Implement the prune-context mode and enhance file compaction logic to optimize token usage in prompts
1 parent 0e6653b commit a2b2373

19 files changed

Lines changed: 152 additions & 78 deletions

File tree

‎packages/shared/src/constants/dictionary.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ export const dictionary = {
143143
TYPE_SOMETHING_TO_USE_GROUP: 'Type something to use this group.',
144144
GROUP_HAS_NO_SELECTED_PRESETS:
145145
'The chosen group has no selected presets to run.',
146-
NO_RECENTLY_USED_PRESETS_OR_GROUPS:
147-
'No recently used presets or groups. Select from the list below.',
146+
NO_RECENTLY_USED_PRESETS_OR_GROUPS: 'No recently used presets or groups.',
148147
CHECKPOINT_OPERATION_IN_PROGRESS:
149148
'Another checkpoint operation is currently in progress. Please wait for it to complete.',
150149
URL_OVERRIDE_DIFFERENT_DOMAIN: (preset_name: string) =>

‎packages/vscode/src/constants/instructions.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ export const intelligent_update_instructions =
2525
export const commit_message_instructions =
2626
"Write a brief and precise summary for the changes, limited to a single sentence, if possible and nothing else. Because the summary will be used for a commit message, don't use any markdown formatting. Use an imperative tone to ensure clarity and focus on the primary change or purpose."
2727

28-
export const optimize_context_instructions = (instructions: string) =>
29-
`Optimistically list relevant files to accomplish following task: ${instructions}
28+
export const prune_context_instructions = `Find relevant files for the task.
3029
<system>
31-
Your response must list paths one under another, and show nothing else, e.g.:
30+
Your response must begin with a markdown heading, then list paths relevant for the task one under another. Example:
31+
32+
### Relevant files:
33+
3234
- \`src/hello.ts\`
3335
- \`src/welcome.ts\`
3436
</system>`

‎packages/vscode/src/context/utils/compact-file/languages/c-style.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export const compact_c_style = (content: string): string => {
88
let last_code_buffer = ''
99

1010
for (const line of lines) {
11+
// Capture indentation for use in body stripping
12+
const indent_match = line.match(/^\s*/)
13+
const current_indent = indent_match ? indent_match[0] : ''
14+
1115
let processed_line = ''
1216
let i = 0
1317
let is_in_string: false | '"' | "'" | '`' = false
@@ -85,7 +89,7 @@ export const compact_c_style = (content: string): string => {
8589
// We look at last_code_buffer for keywords
8690
// We also want to preserve control flow (if, for, while, etc.)
8791
const is_keeper =
88-
/(^|\s)(class|interface|enum|namespace|type|if|else|for|while|do|switch|try|catch|finally)(\s|$)/.test(
92+
/(^|\s)(class|interface|enum|namespace|type|if|else|for|while|do|switch|try|catch|finally|import|export)(\s|$)/.test(
8993
last_code_buffer
9094
)
9195
// Also preserve object literals (preceded by = or :)
@@ -94,7 +98,7 @@ export const compact_c_style = (content: string): string => {
9498
if (!is_keeper && !is_object_literal) {
9599
// Start skipping
96100
skip_body_depth = 1
97-
processed_line += `{\n// ...\n}`
101+
processed_line += `{\n${current_indent} // ...\n${current_indent}}`
98102
last_code_buffer += '{}'
99103
i++
100104
continue
@@ -124,7 +128,7 @@ export const compact_c_style = (content: string): string => {
124128
// If a line contained only comments, it becomes empty and is skipped.
125129
const trimmed = processed_line.trim()
126130
if (trimmed) {
127-
result.push(trimmed)
131+
result.push(processed_line.trimEnd())
128132
}
129133

130134
// Add a space to buffer for newline to avoid concatenation issues (e.g. "class Foo" + "{")

‎packages/vscode/src/context/utils/compact-file/languages/css.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export const compact_css = (content: string): string => {
120120
// If a line contained only comments, it becomes empty and is skipped.
121121
const trimmed = processed_line.trim()
122122
if (trimmed) {
123-
result.push(trimmed)
123+
result.push(processed_line.trimEnd())
124124
}
125125

126126
// Add a space to buffer for newline

‎packages/vscode/src/context/utils/compact-file/languages/html.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export const compact_html = (content: string): string => {
3131
const lines = processed.split(/\r?\n/)
3232
const compacted_lines: string[] = []
3333
for (const line of lines) {
34-
const trimmed = line.trim()
35-
if (trimmed) {
34+
const trimmed = line.trimEnd()
35+
if (trimmed.trim()) {
3636
compacted_lines.push(trimmed)
3737
}
3838
}
@@ -43,15 +43,15 @@ export const compact_html = (content: string): string => {
4343
const { open, content } = scripts[parseInt(idx)]
4444
// Treat as C-style but do NOT strip bodies (preserve logic, just strip comments)
4545
// as scripts in HTML are often minimal or structural.
46-
const compacted = compact_c_style(content).trim()
46+
const compacted = compact_c_style(content).trimEnd()
4747
return `${open}\n${compacted}\n</script>`
4848
})
4949

5050
// 6. Restore Styles
5151
processed = processed.replace(/___STYLE_(\d+)___/g, (_, idx) => {
5252
const { open, content } = styles[parseInt(idx)]
5353
// Compact CSS (strips bodies/properties usually)
54-
const compacted = compact_css(content).trim()
54+
const compacted = compact_css(content).trimEnd()
5555
return `${open}\n${compacted}\n</style>`
5656
})
5757

‎packages/vscode/src/context/utils/compact-file/languages/jsx.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export const compact_jsx = (content: string): string => {
88
let last_code_buffer = ''
99

1010
for (const line of lines) {
11+
// Capture indentation for use in body stripping
12+
const indent_match = line.match(/^\s*/)
13+
const current_indent = indent_match ? indent_match[0] : ''
14+
1115
let processed_line = ''
1216
let i = 0
1317
let is_in_string: false | '"' | "'" | '`' = false
@@ -84,7 +88,7 @@ export const compact_jsx = (content: string): string => {
8488
// Check if we should preserve this block (class, interface, etc.)
8589
// We look at last_code_buffer for keywords
8690
const is_keeper =
87-
/(^|\s)(class|interface|enum|namespace|type)(\s|$)/.test(
91+
/(^|\s)(class|interface|enum|namespace|type|import|export)(\s|$)/.test(
8892
last_code_buffer
8993
)
9094
// Also preserve object literals (preceded by =, :, or open paren for destructuring)
@@ -95,7 +99,7 @@ export const compact_jsx = (content: string): string => {
9599
if (!is_keeper && !is_object_literal) {
96100
// Start skipping
97101
skip_body_depth = 1
98-
processed_line += `{\n// ...\n}`
102+
processed_line += `{\n${current_indent} // ...\n${current_indent}}`
99103
last_code_buffer += '{}'
100104
i++
101105
continue
@@ -125,7 +129,7 @@ export const compact_jsx = (content: string): string => {
125129
// If a line contained only comments, it becomes empty and is skipped.
126130
const trimmed = processed_line.trim()
127131
if (trimmed) {
128-
result.push(trimmed)
132+
result.push(processed_line.trimEnd())
129133
}
130134

131135
// Add a space to buffer for newline to avoid concatenation issues (e.g. "class Foo" + "{")

‎packages/vscode/src/context/utils/compact-file/languages/sql.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const compact_sql = (content: string): string => {
7272

7373
const trimmed = processed_line.trim()
7474
if (trimmed) {
75-
result.push(trimmed)
75+
result.push(processed_line.trimEnd())
7676
}
7777
}
7878

‎packages/vscode/src/context/utils/compact-file/test-cases/c-style/input.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,13 @@ const a = 1; /* comment */ const b = 2;
5858
const a = 1; // This is a comment
5959
// This is a full line comment
6060
const b = 2;
61+
62+
import {
63+
Readable,
64+
Writable
65+
} from 'stream';
66+
67+
export {
68+
foo,
69+
bar
70+
};
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
function test() {
2-
// ...
2+
// ...
33
}
44
const str = "String with \"// quote";
55
function add(a, b) {
6-
// ...
6+
// ...
77
}
88
class Calculator {
9-
constructor() {
10-
// ...
11-
}
12-
add(n) {
13-
// ...
14-
}
9+
constructor() {
10+
// ...
11+
}
12+
add(n) {
13+
// ...
14+
}
1515
}
1616
interface Item {
17-
id: string;
18-
name: string;
17+
id: string;
18+
name: string;
1919
}
2020
const double = (x) => {
21-
// ...
21+
// ...
2222
}
2323
const config = {
24-
env: "production",
25-
debug: false
24+
env: "production",
25+
debug: false
2626
};
2727
const str = "/* this is not a comment */";
2828
const str = "// this is not a comment";
2929
const str = '// this is not a comment';
3030
const str = `// this is not a comment`;
3131
const a = 1; const b = 2;
32-
const a = 1;
33-
const b = 2;
34-
const a = 1;
35-
const b = 2;
32+
const a = 1;
33+
const b = 2;
34+
const a = 1;
35+
const b = 2;
36+
import {
37+
Readable,
38+
Writable
39+
} from 'stream';
40+
export {
41+
foo,
42+
bar
43+
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
header {}
22
@media (min-width: 768px) {
3-
header {}
4-
.sidebar {}
3+
header {}
4+
.sidebar {}
55
}
66
.card {}
77
@keyframes slideIn {
8-
from {}
9-
to {}
8+
from {}
9+
to {}
1010
}

0 commit comments

Comments
 (0)