Skip to content

Commit 589ecbf

Browse files
committed
Refine agentic CLI prompt requirements and use cached initial file content for more reliable diff generation
- Update `invoke-agentic-cli.ts` to read and store the file contents in `file_mappings` before the CLI runs, and modify `generate_diff_markdown` to diff against these cached contents instead of re-reading the original files from disk. └── apps └── editor └── src ├── utils │ └── agentic-cli-invocation │ ├── invoke-agentic-cli.ts │ └── utils │ └── generate-diff-markdown.ts └── views └── prompt └── backend └── message-handlers └── handle-invoke-agentic-cli.ts
1 parent df72658 commit 589ecbf

4 files changed

Lines changed: 21 additions & 19 deletions

File tree

‎apps/editor/src/constants/instructions.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,8 @@ export const voice_input_instructions =
7171

7272
export const cli_edit_ask_requirements = {
7373
preloaded_files: 'All project files are shown in [files](#files) section.',
74-
referenced_files_with_some_preloaded:
75-
'Begin by looking at [files](#files) section for unread files (### Unread file: `[PROJECT-RELATIVE PATH]`) and read them one-by-one.',
76-
referenced_files_only:
77-
'Begin by looking at [files](#files) section for referenced files (### File: `[PROJECT-RELATIVE PATH]`) and read them one-by-one.',
74+
read_files:
75+
'You must start by looking at [files](#files) section for unread files (### Unread file: `[PROJECT-RELATIVE PATH]`) and read them one-by-one.',
7876
read_images:
7977
'If [task](#task) section includes *.png image paths, read them.',
8078
restrict_tool_calls:

‎apps/editor/src/utils/agentic-cli-invocation/invoke-agentic-cli.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export const invoke_agentic_cli = async (params: {
303303
while (true) {
304304
let selected_root: string | undefined
305305
let temp_dir_path: string | undefined
306-
const file_mappings = new Map<string, { original: string; dest_rel: string }>()
306+
const file_mappings = new Map<string, { original: string; dest_rel: string; initial_content?: string }>()
307307

308308
if (params.panel_prompt_type) {
309309
temp_dir_path = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'cwc-cli-'))
@@ -319,7 +319,9 @@ export const invoke_agentic_cli = async (params: {
319319
await fs.promises.mkdir(path.dirname(dest_path), { recursive: true })
320320
await fs.promises.copyFile(file_path, dest_path)
321321

322-
file_mappings.set(dest_path, { original: file_path, dest_rel: dest_rel.replace(/\\/g, '/') })
322+
const initial_content = await fs.promises.readFile(file_path, 'utf-8').catch(() => undefined)
323+
324+
file_mappings.set(dest_path, { original: file_path, dest_rel: dest_rel.replace(/\\/g, '/'), initial_content })
323325
}
324326

325327
selected_root = temp_dir_path

‎apps/editor/src/utils/agentic-cli-invocation/utils/generate-diff-markdown.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ async function get_all_files(dir: string): Promise<string[]> {
1818

1919
export const generate_diff_markdown = async (
2020
temp_dir_path: string,
21-
file_mappings: Map<string, { original: string; dest_rel: string }>
21+
file_mappings: Map<string, { original: string; dest_rel: string; initial_content?: string }>
2222
): Promise<string> => {
2323
let diff_markdown = ''
2424
const temp_files = await get_all_files(temp_dir_path)
2525

2626
for (const temp_file of temp_files) {
27+
if (path.basename(temp_file).startsWith('._orig_')) {
28+
continue
29+
}
30+
2731
const rel_path = path.relative(temp_dir_path, temp_file)
2832
const unix_rel_path = rel_path.replace(/\\/g, '/')
2933

@@ -39,18 +43,20 @@ export const generate_diff_markdown = async (
3943
}
4044

4145
if (mapping) {
42-
const original_content = await fs.promises.readFile(
43-
mapping.original,
44-
'utf-8'
45-
)
46+
const original_content = mapping.initial_content !== undefined
47+
? mapping.initial_content
48+
: await fs.promises.readFile(mapping.original, 'utf-8').catch(() => '')
4649
const new_content = await fs.promises.readFile(temp_file, 'utf-8')
4750

4851
if (original_content !== new_content) {
52+
const temp_original = path.join(path.dirname(temp_file), `._orig_${path.basename(temp_file)}`)
53+
await fs.promises.writeFile(temp_original, original_content, 'utf-8')
54+
4955
try {
5056
await execFileAsync('git', [
5157
'diff',
5258
'--no-index',
53-
mapping.original,
59+
temp_original,
5460
temp_file
5561
])
5662
} catch (err: any) {
@@ -102,6 +108,8 @@ export const generate_diff_markdown = async (
102108
(new_content.endsWith('\n') ? '' : '\n') +
103109
'```\n'
104110
}
111+
} finally {
112+
await fs.promises.unlink(temp_original).catch(() => {})
105113
}
106114
}
107115
} else {

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,9 @@ export const build_cli_prompt = async (params: {
7676

7777
let files_section = ''
7878
let are_all_files_preloaded = false
79-
let are_any_files_preloaded = false
8079

8180
if (total_content_length <= MAX_CLI_PROMPT_TOTAL_INLINED_CHARS) {
8281
are_all_files_preloaded = true
83-
are_any_files_preloaded = files_data.length > 0
8482
const file_blocks = files_data.map((data) =>
8583
data.content !== undefined
8684
? `### File: \`${data.relative_path}\`\n\n\`\`\`\n${data.content}\n\`\`\``
@@ -103,15 +101,13 @@ export const build_cli_prompt = async (params: {
103101
file_blocks.push(
104102
`### File: \`${data.relative_path}\`\n\n\`\`\`\n${data.content}\n\`\`\``
105103
)
106-
are_any_files_preloaded = true
107104
} else {
108105
file_blocks.push(`### Unread file: \`${data.relative_path}\``)
109106
}
110107
} else {
111108
file_blocks.push(
112109
`### File: \`${data.relative_path}\`\n\n\`\`\`\n\n\`\`\``
113110
)
114-
are_any_files_preloaded = true
115111
}
116112
}
117113

@@ -123,9 +119,7 @@ export const build_cli_prompt = async (params: {
123119
const requirements_list = [
124120
are_all_files_preloaded
125121
? cli_edit_ask_requirements.preloaded_files
126-
: are_any_files_preloaded
127-
? cli_edit_ask_requirements.referenced_files_with_some_preloaded
128-
: cli_edit_ask_requirements.referenced_files_only,
122+
: cli_edit_ask_requirements.read_files,
129123
cli_edit_ask_requirements.read_images,
130124
prompt_view_provider.cli_prompt_type == 'edit-files'
131125
? cli_edit_ask_requirements.restrict_tool_calls_with_exceptions

0 commit comments

Comments
 (0)