The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { OrchestrationV2TurnItem } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { m, type MessageResolver } from "../i18n/messages"; |
| 5 | import { getRenderablePatch, resolveFileDiffPath } from "./diffRendering"; |
| 6 | |
| 7 | type FileChange = Extract<OrchestrationV2TurnItem, { type: "file_change" }>; |
| 8 | |
| 9 | function 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 | |
| 56 | // Claude records absolute paths; name files under the workspace relative to it, as other diffs do. |
| 57 | // Only header lines, from the start or a `diff --git` line up to the first hunk, are rewritten. |
| 58 | function relativeHeaderPaths(patch: string | undefined, workspaceRoot: string | undefined) { |
| 59 | const windows = /^(?:[A-Za-z]:[\\/]|\\\\)/.test(workspaceRoot ?? ""); |
| 60 | const segments = workspaceRoot?.split(windows ? /[\\/]+/ : /\/+/); |
| 61 | while (segments?.at(-1) === "") segments.pop(); |
| 62 | if (!patch || !segments?.some(Boolean)) return patch; |
| 63 | // Windows separates with / or \ (\\ in a quoted header); POSIX only with /, where a \ is part of |
| 64 | // a name and a quoted header doubles it. |
| 65 | const separator = windows ? String.raw`[\\/]+` : "/+"; |
| 66 | const root = segments |
| 67 | .map((segment) => |
| 68 | segment |
| 69 | .split("\\") |
| 70 | .map((text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) |
| 71 | .join(String.raw`\\\\?`), |
| 72 | ) |
| 73 | .join(separator); |
| 74 | const prefix = new RegExp(String.raw`^("?(?:[ab]/)?)${root}${separator}`, windows ? "i" : ""); |
| 75 | const relative = (name: string) => name.replace(prefix, "$1"); |
| 76 | let inHeader = true; |
| 77 | return patch |
| 78 | .split("\n") |
| 79 | .map((line) => { |
| 80 | if (line.startsWith("diff --git ")) { |
| 81 | inHeader = true; |
| 82 | // Split the names as Pierre's parser does, so a name containing a space stays whole. |
| 83 | const names = |
| 84 | /^diff --git ("a\/(?:[^"\\]|\\.)*"|a\/.+?) ("b\/(?:[^"\\]|\\.)*"|b\/.+?)$/.exec(line); |
| 85 | return names ? `diff --git ${relative(names[1]!)} ${relative(names[2]!)}` : line; |
| 86 | } |
| 87 | if (line.startsWith("@@")) inHeader = false; |
| 88 | const name = inHeader ? /^(---|\+\+\+)(\s+)(.*)$/.exec(line) : null; |
| 89 | return name ? `${name[1]}${name[2]}${relative(name[3]!)}` : line; |
| 90 | }) |
| 91 | .join("\n"); |
| 92 | } |
| 93 | |
| 94 | function recordedText(value: string | undefined) { |
| 95 | const suffix = "\n… output truncated for transport"; |
| 96 | const truncated = value !== undefined && value.endsWith(suffix); |
| 97 | return { text: truncated ? value.slice(0, -suffix.length) : value, truncated }; |
| 98 | } |
| 99 | |
| 100 | export function toolCallDiff( |
| 101 | item: Pick<FileChange, "diffStr" | "fileName" | "changes" | "oldStr" | "newStr">, |
| 102 | workspaceRoot?: string, |
| 103 | resolve: MessageResolver = m, |
| 104 | ) { |
| 105 | const diff = recordedText(item.diffStr); |
| 106 | const operation = item.changes?.[0]?.operation; |
| 107 | const emptyContent = diff.text === "" ? "" : undefined; |
| 108 | const before = recordedText(item.oldStr ?? (operation === "delete" ? emptyContent : undefined)); |
| 109 | const after = recordedText(item.newStr ?? (operation === "add" ? emptyContent : undefined)); |
| 110 | return { |
| 111 | patch: relativeHeaderPaths( |
| 112 | toolCallPatch({ ...item, ...(diff.text === undefined ? {} : { diffStr: diff.text }) }), |
| 113 | workspaceRoot, |
| 114 | ), |
| 115 | content: diff.text?.trim() |
| 116 | ? [] |
| 117 | : [ |
| 118 | { label: resolve(msg("Recorded content before")), text: before.text }, |
| 119 | { label: resolve(msg("Recorded content after")), text: after.text }, |
| 120 | ].filter((entry): entry is { label: string; text: string } => entry.text !== undefined), |
| 121 | truncated: diff.truncated || before.truncated || after.truncated, |
| 122 | }; |
| 123 | } |