Open History to see the last commit
1import type { OrchestrationV2TurnItem } from "@t3tools/contracts";
2import { msg } from "gt-react";
3
4import { m, type MessageResolver } from "../i18n/messages";
5import { getRenderablePatch, resolveFileDiffPath } from "./diffRendering";
6
7type FileChange = Extract<OrchestrationV2TurnItem, { type: "file_change" }>;
8
9function toolCallPatch(item: Pick<FileChange, "diffStr" | "fileName" | "changes">) {
10 let patch = item.diffStr;
11 if (!patch) return patch;
12 const change = item.changes?.[0];
13 const rawFileOperation = change?.operation === "add" || change?.operation === "delete";
14 if (patch.startsWith("diff --git ") || patch.startsWith("--- ")) {
15 if (!rawFileOperation) return patch;
16 const parsed = getRenderablePatch(patch, "tool-call-format");
17 if (
18 parsed?.kind === "files" &&
19 parsed.files.every((file) => {
20 const path = resolveFileDiffPath(file).replaceAll("\\", "/");
21 return (
22 file.hunks.length > 0 &&
23 item.changes?.some((recorded) => {
24 const recordedPath = recorded.path.replaceAll("\\", "/");
25 return (
26 (recorded.operation === "add"
27 ? file.deletionLines.length === 0
28 : recorded.operation === "delete"
29 ? file.additionLines.length === 0
30 : true) &&
31 (recordedPath === path || recordedPath.endsWith(`/${path}`))
32 );
33 })
34 );
35 })
36 )
37 return patch;
38 }
39 // Codex can record added/deleted file contents or bare hunks instead of a complete patch.
40 if (rawFileOperation || !patch.trimStart().startsWith("@@ ")) {
41 if (!rawFileOperation) return patch;
42 const lines = patch.split("\n");
43 const hasFinalNewline = patch.endsWith("\n");
44 if (hasFinalNewline) lines.pop();
45 const added = change.operation === "add";
46 const range = `1,${lines.length}`;
47 patch = `@@ -${added ? "0,0" : range} +${added ? range : "0,0"} @@\n${lines.map((line) => `${added ? "+" : "-"}${line}`).join("\n")}\n${hasFinalNewline ? "" : "\\ No newline at end of file\n"}`;
48 }
49 const oldPath = change?.oldPath ?? item.fileName;
50 const newPath = change?.path ?? item.fileName;
51 const before = change?.operation === "add" ? "/dev/null" : JSON.stringify(`a/${oldPath}`);
52 const after = change?.operation === "delete" ? "/dev/null" : JSON.stringify(`b/${newPath}`);
53 return `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${newPath}`)}\n--- ${before}\n+++ ${after}\n${patch.trimStart()}`;
54}
55
56function recordedText(value: string | undefined) {
57 const suffix = "\n… output truncated for transport";
58 const truncated = value !== undefined && value.endsWith(suffix);
59 return { text: truncated ? value.slice(0, -suffix.length) : value, truncated };
60}
61
62export function toolCallDiff(
63 item: Pick<FileChange, "diffStr" | "fileName" | "changes" | "oldStr" | "newStr">,
64 resolve: MessageResolver = m,
65) {
66 const diff = recordedText(item.diffStr);
67 const operation = item.changes?.[0]?.operation;
68 const emptyContent = diff.text === "" ? "" : undefined;
69 const before = recordedText(item.oldStr ?? (operation === "delete" ? emptyContent : undefined));
70 const after = recordedText(item.newStr ?? (operation === "add" ? emptyContent : undefined));
71 return {
72 patch: toolCallPatch({ ...item, ...(diff.text === undefined ? {} : { diffStr: diff.text }) }),
73 content: diff.text?.trim()
74 ? []
75 : [
76 { label: resolve(msg("Recorded content before")), text: before.text },
77 { label: resolve(msg("Recorded content after")), text: after.text },
78 ].filter((entry): entry is { label: string; text: string } => entry.text !== undefined),
79 truncated: diff.truncated || before.truncated || after.truncated,
80 };
81}