|
| 1 | +import path from "node:path"; |
| 2 | +import { |
| 3 | + asOptionalObjectRecord, |
| 4 | + readStringField, |
| 5 | +} from "@openclaw/normalization-core/record-coerce"; |
| 6 | +import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce"; |
| 7 | +import { extractApplyPatchTargetPaths } from "./apply-patch-paths.js"; |
| 8 | +import type { FileMutationToolName } from "./tool-mutation-names.js"; |
| 9 | + |
| 10 | +type FileMutationLineCount = { added: number; removed: number }; |
| 11 | +type FileMutationDelta = FileMutationLineCount & { files: string[] }; |
| 12 | + |
| 13 | +function readTarget(record: Record<string, unknown>): string | undefined { |
| 14 | + const target = normalizeOptionalString(record.path ?? record.file_path ?? record.filePath); |
| 15 | + return target ? path.resolve(target) : undefined; |
| 16 | +} |
| 17 | + |
| 18 | +function readEdits(args: Record<string, unknown>): Record<string, unknown>[] { |
| 19 | + const candidates = Array.isArray(args.edits) ? args.edits : [args]; |
| 20 | + return candidates.flatMap((candidate) => { |
| 21 | + const edit = asOptionalObjectRecord(candidate); |
| 22 | + return edit ? [edit] : []; |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +function countNewlines(value: unknown): number { |
| 27 | + if (typeof value !== "string") { |
| 28 | + return 0; |
| 29 | + } |
| 30 | + let count = 0; |
| 31 | + for (let index = value.indexOf("\n"); index >= 0; index = value.indexOf("\n", index + 1)) { |
| 32 | + count += 1; |
| 33 | + } |
| 34 | + return count; |
| 35 | +} |
| 36 | + |
| 37 | +/** Counts only newline-terminated content so partial streamed JSON never guesses a line. */ |
| 38 | +export function countStreamingFileMutationLines( |
| 39 | + kind: FileMutationToolName, |
| 40 | + args: Record<string, unknown>, |
| 41 | +): FileMutationLineCount { |
| 42 | + if (kind === "write") { |
| 43 | + return { added: countNewlines(readStringField(args, "content")), removed: 0 }; |
| 44 | + } |
| 45 | + if (kind === "edit") { |
| 46 | + return readEdits(args).reduce<FileMutationLineCount>( |
| 47 | + (total, edit) => ({ |
| 48 | + added: total.added + countNewlines(edit.newText ?? edit.new_string), |
| 49 | + removed: total.removed + countNewlines(edit.oldText ?? edit.old_string), |
| 50 | + }), |
| 51 | + { added: 0, removed: 0 }, |
| 52 | + ); |
| 53 | + } |
| 54 | + const patch = args.input ?? args.patch; |
| 55 | + if (typeof patch !== "string") { |
| 56 | + return { added: 0, removed: 0 }; |
| 57 | + } |
| 58 | + let added = 0; |
| 59 | + let removed = 0; |
| 60 | + let lineStart = 0; |
| 61 | + for (let lineEnd = patch.indexOf("\n"); lineEnd >= 0; lineEnd = patch.indexOf("\n", lineStart)) { |
| 62 | + added += Number(patch[lineStart] === "+"); |
| 63 | + removed += Number(patch[lineStart] === "-"); |
| 64 | + lineStart = lineEnd + 1; |
| 65 | + } |
| 66 | + return { added, removed }; |
| 67 | +} |
| 68 | + |
| 69 | +function readCodexChangeDelta(args: Record<string, unknown>): FileMutationDelta | undefined { |
| 70 | + const files: string[] = []; |
| 71 | + let added = 0; |
| 72 | + let removed = 0; |
| 73 | + for (const candidate of Array.isArray(args.changes) ? args.changes : []) { |
| 74 | + const change = asOptionalObjectRecord(candidate); |
| 75 | + const target = change ? readTarget(change) : undefined; |
| 76 | + if (!change || !target) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + files.push(target); |
| 80 | + const stat = asOptionalObjectRecord(change.stat); |
| 81 | + added += |
| 82 | + typeof stat?.added === "number" && Number.isFinite(stat.added) ? Math.max(0, stat.added) : 0; |
| 83 | + removed += |
| 84 | + typeof stat?.removed === "number" && Number.isFinite(stat.removed) |
| 85 | + ? Math.max(0, stat.removed) |
| 86 | + : 0; |
| 87 | + } |
| 88 | + return files.length > 0 ? { files, added, removed } : undefined; |
| 89 | +} |
| 90 | + |
| 91 | +/** Reads complete tool arguments using task-fold line semantics. */ |
| 92 | +export function readCompletedFileMutationDelta( |
| 93 | + kind: FileMutationToolName, |
| 94 | + args: Record<string, unknown>, |
| 95 | +): FileMutationDelta | undefined { |
| 96 | + if (kind === "apply_patch") { |
| 97 | + const patch = readStringField(args, "input"); |
| 98 | + if (patch === undefined) { |
| 99 | + return readCodexChangeDelta(args); |
| 100 | + } |
| 101 | + const files = extractApplyPatchTargetPaths(args); |
| 102 | + if (files.length === 0) { |
| 103 | + return undefined; |
| 104 | + } |
| 105 | + let added = 0; |
| 106 | + let removed = 0; |
| 107 | + let inBody = false; |
| 108 | + for (const line of patch.split(/\r\n|\r|\n/)) { |
| 109 | + if (/^\s*\*\*\* (?:Add|Update|Delete) File: /.test(line)) { |
| 110 | + inBody = true; |
| 111 | + } else if (!/^\s*\*\* /.test(line) && inBody) { |
| 112 | + added += Number(line.startsWith("+")); |
| 113 | + removed += Number(line.startsWith("-")); |
| 114 | + } |
| 115 | + } |
| 116 | + return { files, added, removed }; |
| 117 | + } |
| 118 | + const target = readTarget(args); |
| 119 | + if (!target) { |
| 120 | + return undefined; |
| 121 | + } |
| 122 | + if (kind === "write") { |
| 123 | + const content = readStringField(args, "content"); |
| 124 | + return content === undefined |
| 125 | + ? undefined |
| 126 | + : { |
| 127 | + files: [target], |
| 128 | + added: content.length === 0 ? 0 : content.split(/\r\n|\r|\n/).length, |
| 129 | + removed: 0, |
| 130 | + }; |
| 131 | + } |
| 132 | + let added = 0; |
| 133 | + let removed = 0; |
| 134 | + let hasCompleteEdit = false; |
| 135 | + for (const edit of readEdits(args)) { |
| 136 | + const oldText = |
| 137 | + typeof edit.oldText === "string" |
| 138 | + ? edit.oldText |
| 139 | + : typeof edit.old_string === "string" |
| 140 | + ? edit.old_string |
| 141 | + : undefined; |
| 142 | + const newText = |
| 143 | + typeof edit.newText === "string" |
| 144 | + ? edit.newText |
| 145 | + : typeof edit.new_string === "string" |
| 146 | + ? edit.new_string |
| 147 | + : undefined; |
| 148 | + if (oldText === undefined || newText === undefined) { |
| 149 | + continue; |
| 150 | + } |
| 151 | + hasCompleteEdit = true; |
| 152 | + added += newText.length === 0 ? 0 : newText.split(/\r\n|\r|\n/).length; |
| 153 | + removed += oldText.length === 0 ? 0 : oldText.split(/\r\n|\r|\n/).length; |
| 154 | + } |
| 155 | + return hasCompleteEdit ? { files: [target], added, removed } : undefined; |
| 156 | +} |
0 commit comments