Skip to content
Open
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
39 changes: 14 additions & 25 deletions packages/opencode/src/acp/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,33 +187,18 @@ async function permissionContent(toolName: string, input: ToolInput): Promise<To
if (files.length) return diffContentForFiles(files)

const filepath = stringValue(input.filepath) ?? stringValue(input.filePath)
const diff = stringValue(input.diff)
if (!filepath || !diff) return []
const content = await diffContentForPatch(filepath, diff)
return content ? [content] : []
const oldText = stringValue(input.oldString)
const newText = stringValue(input.newString)
if (!filepath || oldText === undefined || newText === undefined) return []
return [{ type: "diff" as const, path: filepath, oldText, newText }]
}

async function diffContentForFiles(files: PermissionFileMetadata[]) {
const content = await Promise.all(
files.map(async (file) => {
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 {
Expand All @@ -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[] {
Expand All @@ -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),
},
]
})
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/tool/apply_patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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
2 changes: 2 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
Loading