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

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
}

View File

@@ -896,7 +896,15 @@ function buildDiffChunk(
const newStr =
typeof parsed.new_string === "string" ? parsed.new_string : ""
return buildUnifiedDiff(filePath, oldStr, newStr)
const diff = buildUnifiedDiff(filePath, oldStr, newStr)
if (!diff) return null
const startLine =
typeof parsed._start_line === "number" ? parsed._start_line : 0
if (startLine <= 1) return diff
return diff.replace(
/^@@ -(\d+),(\d+) \+(\d+),(\d+) @@/gm,
(_, _o, oc, _n, nc) => `@@ -${startLine},${oc} +${startLine},${nc} @@`
)
}
if (op === "write") {