Skip to content

Commit a8e8fb3

Browse files
committed
fix(web): distinguish file contents from embedded patches
1 parent ed0e183 commit a8e8fb3

2 files changed

Lines changed: 63 additions & 7 deletions

File tree

‎apps/web/src/lib/toolCallDiff.test.ts‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,35 @@ describe("toolCallDiff", () => {
2525

2626
it("renders added and deleted file contents as a file diff", () => {
2727
for (const operation of ["add", "delete"]) {
28-
for (const ending of ["", "\n"]) {
28+
for (const [content, ending] of [
29+
["first\nsecond", ""],
30+
["first\nsecond", "\n"],
31+
["--- notes\nsecond", "\n"],
32+
["diff --git notes\nsecond", "\n"],
33+
["--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after", "\n"],
34+
[
35+
"diff --git a/embedded.ts b/embedded.ts\n--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after",
36+
"\n",
37+
],
38+
] as const) {
39+
const recordedContent = `${content}${ending}`;
2940
const rendered = getRenderablePatch(
3041
toolCallDiff({
3142
fileName: "file with spaces.ts",
32-
diffStr: `first\nsecond${ending}`,
43+
diffStr: recordedContent,
3344
changes: [{ operation, path: "file with spaces.ts" }],
3445
}).patch,
3546
);
3647
expect(rendered?.kind).toBe("files");
3748
if (rendered?.kind !== "files") throw new Error("Expected a file diff");
3849
expect(resolveFileDiffPath(rendered.files[0]!)).toBe("file with spaces.ts");
39-
expect(rendered.files[0]!.additionLines).toHaveLength(operation === "add" ? 2 : 0);
40-
expect(rendered.files[0]!.deletionLines).toHaveLength(operation === "delete" ? 2 : 0);
50+
const file = rendered.files[0]!;
51+
const lines = content.split("\n").length;
52+
expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0);
53+
expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0);
54+
expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe(
55+
recordedContent.replace(/\n$/, ""),
56+
);
4157
}
4258
}
4359
});
@@ -73,6 +89,19 @@ describe("toolCallDiff", () => {
7389
});
7490

7591
it("preserves complete patches and unsupported provider output", () => {
92+
for (const operation of ["add", "delete"]) {
93+
const diffStr = [
94+
"diff --git a/a.ts b/a.ts",
95+
`--- ${operation === "add" ? "/dev/null" : "a/a.ts"}`,
96+
`+++ ${operation === "add" ? "b/a.ts" : "/dev/null"}`,
97+
operation === "add" ? "@@ -0,0 +1 @@" : "@@ -1 +0,0 @@",
98+
operation === "add" ? "+content" : "-content",
99+
"",
100+
].join("\n");
101+
expect(
102+
toolCallDiff({ fileName: "a.ts", diffStr, changes: [{ operation, path: "a.ts" }] }).patch,
103+
).toBe(diffStr);
104+
}
76105
for (const diffStr of [
77106
undefined,
78107
"",

‎apps/web/src/lib/toolCallDiff.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
import type { OrchestrationV2TurnItem } from "@t3tools/contracts";
2+
import { getRenderablePatch, resolveFileDiffPath } from "./diffRendering";
23

34
type FileChange = Extract<OrchestrationV2TurnItem, { type: "file_change" }>;
45

56
function toolCallPatch(item: Pick<FileChange, "diffStr" | "fileName" | "changes">) {
67
let patch = item.diffStr;
7-
if (!patch || patch.startsWith("diff --git ") || patch.startsWith("--- ")) return patch;
8+
if (!patch) return patch;
89
const change = item.changes?.[0];
10+
const rawFileOperation = change?.operation === "add" || change?.operation === "delete";
11+
if (patch.startsWith("diff --git ") || patch.startsWith("--- ")) {
12+
if (!rawFileOperation) return patch;
13+
const parsed = getRenderablePatch(patch, "tool-call-format");
14+
if (
15+
parsed?.kind === "files" &&
16+
parsed.files.every((file) => {
17+
const path = resolveFileDiffPath(file).replaceAll("\\", "/");
18+
return (
19+
file.hunks.length > 0 &&
20+
item.changes?.some((recorded) => {
21+
const recordedPath = recorded.path.replaceAll("\\", "/");
22+
return (
23+
(recorded.operation === "add"
24+
? file.deletionLines.length === 0
25+
: recorded.operation === "delete"
26+
? file.additionLines.length === 0
27+
: true) &&
28+
(recordedPath === path || recordedPath.endsWith(`/${path}`))
29+
);
30+
})
31+
);
32+
})
33+
)
34+
return patch;
35+
}
936
// Codex can record added/deleted file contents or bare hunks instead of a complete patch.
10-
if (!patch.trimStart().startsWith("@@ ")) {
11-
if (change?.operation !== "add" && change?.operation !== "delete") return patch;
37+
if (rawFileOperation || !patch.trimStart().startsWith("@@ ")) {
38+
if (!rawFileOperation) return patch;
1239
const lines = patch.split("\n");
1340
const hasFinalNewline = patch.endsWith("\n");
1441
if (hasFinalNewline) lines.pop();

0 commit comments

Comments
 (0)