diff --git a/packages/opencode/src/acp/permission.ts b/packages/opencode/src/acp/permission.ts index 4eeca28f09f5..877d74025f48 100644 --- a/packages/opencode/src/acp/permission.ts +++ b/packages/opencode/src/acp/permission.ts @@ -187,33 +187,18 @@ async function permissionContent(toolName: string, input: ToolInput): Promise { - if (!file.patch) return [] - const content = await diffContentForPatch(file.filePath, file.patch, file.movePath) - return content ? [content] : [] - }), - ) - return content.flat() -} - -async function diffContentForPatch(filepath: string, diff: string, displayPath = filepath) { - const content = (await exists(filepath)) ? await readText(filepath) : "" - const next = applyPatch(content, diff) - if (next === false) return undefined - return { - type: "diff" as const, - path: displayPath, - oldText: content, - newText: next, - } +function diffContentForFiles(files: PermissionFileMetadata[]): ToolCallContent[] { + return files.flatMap((file) => { + if (file.oldString === undefined || file.newString === undefined) return [] + const displayPath = file.movePath ?? file.filePath + return [{ type: "diff" as const, path: displayPath, oldText: file.oldString, newText: file.newString }] + }) } function selectedReply(result: RequestPermissionResponse): Reply { @@ -231,6 +216,8 @@ type PermissionFileMetadata = { readonly relativePath?: string readonly movePath?: string readonly patch?: string + readonly oldString?: string + readonly newString?: string } function fileMetadata(input: ToolInput): PermissionFileMetadata[] { @@ -246,6 +233,8 @@ function fileMetadata(input: ToolInput): PermissionFileMetadata[] { relativePath: stringValue(info.relativePath), movePath: stringValue(info.movePath), patch: stringValue(info.patch), + oldString: stringValue(info.oldString), + newString: stringValue(info.newString), }, ] }) diff --git a/packages/opencode/src/tool/apply_patch.ts b/packages/opencode/src/tool/apply_patch.ts index f9201be8a7db..acab79ae0eca 100644 --- a/packages/opencode/src/tool/apply_patch.ts +++ b/packages/opencode/src/tool/apply_patch.ts @@ -196,6 +196,8 @@ export const ApplyPatchTool = Tool.define( relativePath: path.relative(instance.worktree, change.movePath ?? change.filePath).replaceAll("\\", "/"), type: change.type, patch: change.diff, + oldString: change.oldContent, + newString: change.newContent, additions: change.additions, deletions: change.deletions, movePath: change.movePath, diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index a92e4720c0fc..140ea210ec0a 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -106,6 +106,8 @@ export const EditTool = Tool.define( metadata: { filepath: filePath, diff, + oldString: contentOld, + newString: contentNew, }, }) yield* afs.writeWithDirs(filePath, Bom.join(contentNew, desiredBom)) @@ -149,6 +151,8 @@ export const EditTool = Tool.define( metadata: { filepath: filePath, diff, + oldString: contentOld, + newString: contentNew, }, }) diff --git a/packages/opencode/src/tool/write.ts b/packages/opencode/src/tool/write.ts index 37be6d8c47bc..51d08e69e06e 100644 --- a/packages/opencode/src/tool/write.ts +++ b/packages/opencode/src/tool/write.ts @@ -58,6 +58,8 @@ export const WriteTool = Tool.define( metadata: { filepath, diff, + oldString: contentOld, + newString: contentNew, }, })