Skip to content

Commit 9451609

Browse files
arafatkatzeCline Evaluation
andauthored
Support Diff Editing for Claude 4 family of Models (RooCodeInc#3816)
* feat: add JSON-based diff format for Claude 4 model family - Bump version to 3.17.5 - Add @streamparser/json dependency for streaming JSON parsing - Implement new JSON diff format in replace_in_file tool for Claude 4 models - Add diff-json.ts module for handling JSON-based file replacements - Update system prompts to use JSON format when Claude 4 model detected - Enhance DiffViewProvider to support new JSON diff format * Adding diffs * changeset --------- Co-authored-by: Cline Evaluation <[email protected]>
1 parent adf2568 commit 9451609

File tree

7 files changed

+438
-50
lines changed

7 files changed

+438
-50
lines changed

.changeset/thin-kangaroos-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"claude-dev": patch
3+
---
4+
5+
Fixed diff editing support for claude 4 family of models

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
"@opentelemetry/sdk-trace-node": "^1.30.1",
364364
"@opentelemetry/semantic-conventions": "^1.30.0",
365365
"@sentry/browser": "^9.12.0",
366+
"@streamparser/json": "^0.0.22",
366367
"@vscode/codicons": "^0.0.36",
367368
"archiver": "^7.0.1",
368369
"axios": "^1.8.2",
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { JSONParser } from "@streamparser/json"
2+
3+
// Fallback type definition based on the error message: "Property 'value' is optional in type 'ParsedElementInfo'"
4+
type ParsedElementInfo = {
5+
value?: any
6+
key?: string | number
7+
parent?: any
8+
stack?: any[]
9+
}
10+
11+
export interface ReplacementItem {
12+
old_str: string
13+
new_str: string
14+
}
15+
16+
export interface ChangeLocation {
17+
startLine: number
18+
endLine: number
19+
startChar: number
20+
endChar: number
21+
}
22+
23+
export class StreamingJsonReplacer {
24+
private currentFileContent: string
25+
private parser: JSONParser
26+
private onContentUpdated: (newContent: string, isFinalItem: boolean, changeLocation?: ChangeLocation) => void
27+
private onErrorCallback: (error: Error) => void
28+
private itemsProcessed: number = 0
29+
private successfullyParsedItems: ReplacementItem[] = []
30+
31+
constructor(
32+
initialContent: string,
33+
onContentUpdatedCallback: (newContent: string, isFinalItem: boolean, changeLocation?: ChangeLocation) => void,
34+
onErrorCallback: (error: Error) => void,
35+
) {
36+
this.currentFileContent = initialContent
37+
this.onContentUpdated = onContentUpdatedCallback
38+
this.onErrorCallback = onErrorCallback
39+
40+
this.parser = new JSONParser({ paths: ["$.replacements.*"] })
41+
42+
this.parser.onValue = (parsedElementInfo: ParsedElementInfo) => {
43+
const { value } = parsedElementInfo // Destructure to get value, which might be undefined
44+
// This callback is triggered for each item matched by '$.replacements.*'
45+
if (value && typeof value === "object" && "old_str" in value && "new_str" in value) {
46+
const item = value as ReplacementItem // Value here is confirmed to be an object
47+
48+
if (typeof item.old_str === "string" && typeof item.new_str === "string") {
49+
this.successfullyParsedItems.push(item) // Store the structurally valid item
50+
51+
if (this.currentFileContent.includes(item.old_str)) {
52+
// Calculate the change location before making the replacement
53+
const changeLocation = this.calculateChangeLocation(item.old_str, item.new_str)
54+
55+
this.currentFileContent = this.currentFileContent.replace(item.old_str, item.new_str)
56+
this.itemsProcessed++
57+
// Notify that an item has been processed. The `isFinalItem` argument here is tricky
58+
// as we don't know from the parser alone if this is the *absolute* last item
59+
// until the stream ends. The caller (Task.ts) will manage the final update.
60+
// For now, we'll pass `false` and let Task.ts handle the final diff view update.
61+
this.onContentUpdated(this.currentFileContent, false, changeLocation)
62+
} else {
63+
const snippet = item.old_str.length > 50 ? item.old_str.substring(0, 47) + "..." : item.old_str
64+
const error = new Error(`Streaming Replacement failed: 'old_str' not found. Snippet: "${snippet}"`)
65+
this.onErrorCallback(error) // Call our own error callback
66+
}
67+
} else {
68+
const error = new Error(`Invalid item structure in replacements stream: ${JSON.stringify(item)}`)
69+
this.onErrorCallback(error) // Call our own error callback
70+
}
71+
} else if (value && (Array.isArray(value) || (typeof value === "object" && "replacements" in value))) {
72+
// This might be the 'replacements' array itself or the root object.
73+
// The `paths: ['$.replacements.*']` should mean we only get items.
74+
// If we get here, it's likely the root object if paths wasn't specific enough or if it's an empty replacements array.
75+
console.log("Streaming parser emitted container:", value)
76+
} else {
77+
// Value is not a ReplacementItem or a known container, could be an issue with the JSON structure or path.
78+
// If `paths` is correct, this path should ideally not be hit often for valid streams.
79+
console.warn("Streaming parser emitted unexpected value:", value)
80+
}
81+
}
82+
83+
this.parser.onError = (err: Error) => {
84+
// Propagate the error to the caller via the callback
85+
this.onErrorCallback(err)
86+
// Note: The @streamparser/json library might throw synchronously on write if onError is not set,
87+
// or if it re-throws. We'll ensure Task.ts wraps write/end in try-catch.
88+
}
89+
}
90+
91+
public write(jsonChunk: string): void {
92+
// Errors during write will be caught by the parser's onError or thrown.
93+
this.parser.write(jsonChunk)
94+
}
95+
96+
public getCurrentContent(): string {
97+
return this.currentFileContent
98+
}
99+
100+
public getSuccessfullyParsedItems(): ReplacementItem[] {
101+
return [...this.successfullyParsedItems] // Return a copy
102+
}
103+
104+
private calculateChangeLocation(oldStr: string, newStr: string): ChangeLocation {
105+
// Find the index where the old string starts
106+
const startIndex = this.currentFileContent.indexOf(oldStr)
107+
if (startIndex === -1) {
108+
// This shouldn't happen since we already checked includes(), but just in case
109+
return { startLine: 0, endLine: 0, startChar: 0, endChar: 0 }
110+
}
111+
112+
// Calculate line numbers by counting newlines before the start index
113+
const contentBeforeStart = this.currentFileContent.substring(0, startIndex)
114+
const startLine = (contentBeforeStart.match(/\n/g) || []).length
115+
116+
// Calculate the end index after replacement
117+
const endIndex = startIndex + oldStr.length
118+
const contentBeforeEnd = this.currentFileContent.substring(0, endIndex)
119+
const endLine = (contentBeforeEnd.match(/\n/g) || []).length
120+
121+
// Calculate character positions within their respective lines
122+
const lastNewlineBeforeStart = contentBeforeStart.lastIndexOf("\n")
123+
const startChar = lastNewlineBeforeStart === -1 ? startIndex : startIndex - lastNewlineBeforeStart - 1
124+
125+
const lastNewlineBeforeEnd = contentBeforeEnd.lastIndexOf("\n")
126+
const endChar = lastNewlineBeforeEnd === -1 ? endIndex : endIndex - lastNewlineBeforeEnd - 1
127+
128+
return {
129+
startLine,
130+
endLine,
131+
startChar,
132+
endChar,
133+
}
134+
}
135+
}

src/core/prompts/system.ts

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const SYSTEM_PROMPT = async (
99
supportsBrowserUse: boolean,
1010
mcpHub: McpHub,
1111
browserSettings: BrowserSettings,
12+
isClaude4ModelFamily: boolean,
1213
) => `You are Cline, a highly skilled software engineer with extensive knowledge in many programming languages, frameworks, design patterns, and best practices.
1314
1415
====
@@ -71,7 +72,46 @@ Your file content here
7172
</write_to_file>
7273
7374
## replace_in_file
74-
Description: Request to replace sections of content in an existing file using SEARCH/REPLACE blocks that define exact changes to specific parts of the file. This tool should be used when you need to make targeted changes to specific parts of a file.
75+
${
76+
isClaude4ModelFamily
77+
? `
78+
"Description: Return your edits as a JSON object with a "replacements" array. Each replacement should have "old_str" and "new_str" fields. The old_str must match exactly what's in the file (including whitespace, indentation and new lines). You can edit multiple lines, but please keep the replacements as simple as possible.
79+
Both old_str and new_str can be multiline strings, but they must be valid JSON strings.
80+
81+
Usage:
82+
<replace_in_file>
83+
<path>File path here</path>
84+
<diff>
85+
{{
86+
"replacements": [
87+
{{
88+
"old_str": "exact string from file",
89+
"new_str": "replacement string"
90+
}}
91+
]
92+
}}
93+
</diff>
94+
</replace_in_file>
95+
96+
Important: Make sure each old_str matches the exact text in the file, character for character.
97+
Parameters:
98+
- path: (required) The path of the file to modify (relative to the current working directory ${cwd.toPosix()})
99+
- replacements_json: (required) A JSON string containing an object with a "replacements" array. Each object in the array must have "old_str" (the exact string to find in the file) and "new_str" (the string to replace it with). Refer to the example format in the main description.
100+
Usage:
101+
<replace_in_file>
102+
<path>File path here</path>
103+
<diff>
104+
{{
105+
"replacements": [
106+
{{
107+
"old_str": "exact string from file",
108+
"new_str": "replacement string"
109+
}}
110+
]
111+
}}
112+
</diff>
113+
</replace_in_file>`
114+
: `Description: Request to replace sections of content in an existing file using SEARCH/REPLACE blocks that define exact changes to specific parts of the file. This tool should be used when you need to make targeted changes to specific parts of a file.
75115
Parameters:
76116
- path: (required) The path of the file to modify (relative to the current working directory ${cwd.toPosix()})
77117
- diff: (required) One or more SEARCH/REPLACE blocks following this exact format:
@@ -103,8 +143,9 @@ Usage:
103143
<path>File path here</path>
104144
<diff>
105145
Search and replace blocks here
106-
</diff>
107-
</replace_in_file>
146+
</diff>
147+
</replace_in_file>`
148+
}
108149
109150
## search_files
110151
Description: Request to perform a regex search across files in a specified directory, providing context-rich results. This tool searches for patterns or specific content across multiple files, displaying each match with encapsulating context.
@@ -329,6 +370,31 @@ Usage:
329370
## Example 4: Requesting to make targeted edits to a file
330371
331372
<replace_in_file>
373+
${
374+
isClaude4ModelFamily
375+
? `
376+
<path>src/baseApp.py</path>
377+
<diff>
378+
{
379+
"replacements": [
380+
{
381+
"old_str": "def try_dotdotdots(whole, part, replace):",
382+
"new_str": "# Handles search/replace blocks that use ellipsis (...) to represent omitted code sections\n# Validates that ellipsis usage is consistent between search and replace blocks\ndef try_dotdotdots(whole, part, replace):"
383+
},
384+
{
385+
"old_str": "def strip_filename(filename, fence):",
386+
"new_str": "# Extracts and cleans filename from various markdown formatting styles\n# Handles filenames with different prefixes, suffixes, and decorations\ndef strip_filename(filename, fence):"
387+
},
388+
{
389+
"old_str": "def main():",
390+
"new_str": "# Main entry point for command-line usage\n# Processes chat history and displays diffs for all found edit blocks\ndef main():"
391+
}
392+
]
393+
}
394+
</diff>
395+
</replace_in_file>
396+
`
397+
: `
332398
<path>src/components/App.tsx</path>
333399
<diff>
334400
<<<<<<< SEARCH
@@ -360,6 +426,9 @@ return (
360426
>>>>>>> REPLACE
361427
</diff>
362428
</replace_in_file>
429+
`
430+
}
431+
363432
364433
## Example 5: Requesting to use an MCP tool
365434
@@ -529,9 +598,21 @@ You have access to two tools for working with files: **write_to_file** and **rep
529598
# Workflow Tips
530599
531600
1. Before editing, assess the scope of your changes and decide which tool to use.
601+
${
602+
isClaude4ModelFamily
603+
? `
604+
2. For major overhauls or initial file creation, rely on write_to_file.
605+
3. Once the file has been edited with either write_to_file or replace_in_file, the system will provide you with the final state of the modified file. Use this updated content as the reference point for any subsequent SEARCH/REPLACE operations, since it reflects any auto-formatting or user-applied changes.
606+
4. All edits are applied in sequence, in the order they are provided
607+
5. All edits must be valid for the operation to succeed - if any edit fails, none will be applied
608+
609+
`
610+
: `
532611
2. For targeted edits, apply replace_in_file with carefully crafted SEARCH/REPLACE blocks. If you need multiple changes, you can stack multiple SEARCH/REPLACE blocks within a single replace_in_file call.
533612
3. For major overhauls or initial file creation, rely on write_to_file.
534613
4. Once the file has been edited with either write_to_file or replace_in_file, the system will provide you with the final state of the modified file. Use this updated content as the reference point for any subsequent SEARCH/REPLACE operations, since it reflects any auto-formatting or user-applied changes.
614+
`
615+
}
535616
536617
By thoughtfully selecting between write_to_file and replace_in_file, you can make your file editing process smoother, safer, and more efficient.
537618
@@ -602,9 +683,17 @@ RULES
602683
- When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task.
603684
- At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details.
604685
- Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal.
686+
${
687+
isClaude4ModelFamily
688+
? `
689+
- When using the replace_in_file tool, you must include complete lines
690+
`
691+
: `
605692
- When using the replace_in_file tool, you must include complete lines in your SEARCH blocks, not partial lines. The system requires exact line matches and cannot match partial lines. For example, if you want to match a line containing "const x = 5;", your SEARCH block must include the entire line, not just "x = 5" or other fragments.
606693
- When using the replace_in_file tool, if you use multiple SEARCH/REPLACE blocks, list them in the order they appear in the file. For example if you need to make changes to both line 10 and line 50, first include the SEARCH/REPLACE block for line 10, followed by the SEARCH/REPLACE block for line 50.
607694
- When using the replace_in_file tool, Do NOT add extra characters to the markers (e.g., <<<<<<< SEARCH> is INVALID). Do NOT forget to use the closing >>>>>>> REPLACE marker. Do NOT modify the marker format in any way. Malformed XML will cause complete tool failure and break the entire editing process.
695+
`
696+
}
608697
- It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc.${
609698
supportsBrowserUse
610699
? " Then if you want to test your work, you might use browser_action to launch the site, wait for the user's response confirming the site was launched along with a screenshot, then perhaps e.g., click a button to test functionality if needed, wait for the user's response confirming the button was clicked along with a screenshot of the new state, before finally closing the browser."

0 commit comments

Comments
 (0)