支持实时响应时解析编辑工具的代码行号

This commit is contained in:
xintaofei
2026-03-28 17:13:29 +08:00
parent 75139a1226
commit 0ab9d46b63
4 changed files with 84 additions and 11 deletions

View File

@@ -1163,12 +1163,21 @@ function EditToolInput({ input }: { input: Record<string, unknown> }) {
const filePath = str(input, "file_path")
const oldString = str(input, "old_string") ?? ""
const newString = str(input, "new_string") ?? ""
const startLine = num(input, "_start_line")
const diffCode = useMemo(() => {
return (
generateUnifiedDiff(oldString, newString, filePath ?? undefined) ?? ""
const diff = generateUnifiedDiff(
oldString,
newString,
filePath ?? undefined
)
}, [oldString, newString, filePath])
if (!diff || !startLine || startLine <= 1) return diff ?? ""
// Replace line numbers in hunk headers with real start line
return diff.replace(
/^@@ -(\d+),(\d+) \+(\d+),(\d+) @@/gm,
(_, _o, oc, _n, nc) => `@@ -${startLine},${oc} +${startLine},${nc} @@`
)
}, [oldString, newString, filePath, startLine])
return diffCode ? <UnifiedDiffPreview diffText={diffCode} /> : null
}