Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/opencode/src/acp/permission.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentSideConnection, PermissionOption, RequestPermissionResponse } from "@agentclientprotocol/sdk"
import type { AgentSideConnection, PermissionOption, RequestPermissionResponse, ToolCallContent } from "@agentclientprotocol/sdk"
import type { Event, OpencodeClient } from "@opencode-ai/sdk/v2"
import { applyPatch } from "diff"
import { exists, readText } from "@/util/filesystem"
Expand Down Expand Up @@ -61,6 +61,7 @@ export class Handler {
rawInput: permission.metadata,
kind: toToolKind(permission.permission),
locations: toLocations(permission.permission, permission.metadata),
content: editDiffContent(permission.permission, permission.metadata),
},
options: permissionOptions,
})
Expand Down Expand Up @@ -111,6 +112,15 @@ export class Handler {
}
}

function editDiffContent(permission: string, metadata: ToolInput): ToolCallContent[] | undefined {
if (permission !== "edit") return undefined
const path = stringValue(metadata.filepath)
const oldText = stringValue(metadata.oldString)
const newText = stringValue(metadata.newString)
if (!path || oldText === undefined || newText === undefined) return undefined
return [{ type: "diff", path, oldText, newText }]
}

function selectedReply(result: RequestPermissionResponse): Reply {
if (result.outcome.outcome !== "selected") return "reject"
if (result.outcome.optionId === "once" || result.outcome.optionId === "always") return result.outcome.optionId
Expand Down
6 changes: 5 additions & 1 deletion packages/opencode/src/acp/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ export function completedToolContent(toolName: string, state: CompletedToolState
]

if (toToolKind(toolName) === "edit") {
content.push(...diffContent(state.input))
const inputForDiff =
toolName.toLowerCase() === "write" && state.metadata && typeof state.metadata === "object"
? { ...state.input, ...(state.metadata as Record<string, unknown>) }
: state.input
content.push(...diffContent(inputForDiff))
}

content.push(...imageContents(state.attachments ?? []))
Expand Down
4 changes: 4 additions & 0 deletions packages/opencode/src/tool/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -149,6 +151,8 @@ export const EditTool = Tool.define(
metadata: {
filepath: filePath,
diff,
oldString: contentOld,
newString: contentNew,
},
})

Expand Down
4 changes: 4 additions & 0 deletions packages/opencode/src/tool/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const WriteTool = Tool.define(
metadata: {
filepath,
diff,
oldString: contentOld,
newString: contentNew,
},
})

Expand Down Expand Up @@ -95,6 +97,8 @@ export const WriteTool = Tool.define(
diagnostics,
filepath,
exists: exists,
oldString: contentOld,
newString: contentNew,
},
output,
}
Expand Down
Loading