Bläddra i källkod

core: fix additions and deletions counting in edit tool filediff

Dax Raad 3 månader sedan
förälder
incheckning
c1515316f5
1 ändrade filer med 15 tillägg och 1 borttagningar
  1. 15 1
      packages/opencode/src/tool/edit.ts

+ 15 - 1
packages/opencode/src/tool/edit.ts

@@ -7,7 +7,7 @@ import z from "zod"
 import * as path from "path"
 import * as path from "path"
 import { Tool } from "./tool"
 import { Tool } from "./tool"
 import { LSP } from "../lsp"
 import { LSP } from "../lsp"
-import { createTwoFilesPatch } from "diff"
+import { createTwoFilesPatch, diffLines } from "diff"
 import { Permission } from "../permission"
 import { Permission } from "../permission"
 import DESCRIPTION from "./edit.txt"
 import DESCRIPTION from "./edit.txt"
 import { File } from "../file"
 import { File } from "../file"
@@ -16,6 +16,7 @@ import { FileTime } from "../file/time"
 import { Filesystem } from "../util/filesystem"
 import { Filesystem } from "../util/filesystem"
 import { Instance } from "../project/instance"
 import { Instance } from "../project/instance"
 import { Agent } from "../agent/agent"
 import { Agent } from "../agent/agent"
+import { Snapshot } from "@/snapshot"
 
 
 export const EditTool = Tool.define("edit", {
 export const EditTool = Tool.define("edit", {
   description: DESCRIPTION,
   description: DESCRIPTION,
@@ -114,10 +115,23 @@ export const EditTool = Tool.define("edit", {
       }
       }
     }
     }
 
 
+    const filediff: Snapshot.FileDiff = {
+      file: filePath,
+      before: contentOld,
+      after: contentNew,
+      additions: 0,
+      deletions: 0,
+    }
+    for (const change of diffLines(contentOld, contentNew)) {
+      if (change.added) filediff.additions += change.count || 0
+      if (change.removed) filediff.deletions += change.count || 0
+    }
+
     return {
     return {
       metadata: {
       metadata: {
         diagnostics,
         diagnostics,
         diff,
         diff,
+        filediff,
       },
       },
       title: `${path.relative(Instance.worktree, filePath)}`,
       title: `${path.relative(Instance.worktree, filePath)}`,
       output,
       output,