mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-25 03:34:22 +01:00
ask instead of throwing tool error if file is outside cwd
This commit is contained in:
@@ -37,7 +37,19 @@ export const EditTool = Tool.define("edit", {
|
||||
|
||||
const filePath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)
|
||||
if (!Filesystem.contains(Instance.directory, filePath)) {
|
||||
throw new Error(`File ${filePath} is not in the current working directory`)
|
||||
const parentDir = path.dirname(filePath)
|
||||
await Permission.ask({
|
||||
type: "external-directory",
|
||||
pattern: parentDir,
|
||||
sessionID: ctx.sessionID,
|
||||
messageID: ctx.messageID,
|
||||
callID: ctx.callID,
|
||||
title: `Edit file outside working directory: ${filePath}`,
|
||||
metadata: {
|
||||
filepath: filePath,
|
||||
parentDir,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const agent = await Agent.get(ctx.agent)
|
||||
|
||||
@@ -54,7 +54,19 @@ export const PatchTool = Tool.define("patch", {
|
||||
const filePath = path.resolve(Instance.directory, hunk.path)
|
||||
|
||||
if (!Filesystem.contains(Instance.directory, filePath)) {
|
||||
throw new Error(`File ${filePath} is not in the current working directory`)
|
||||
const parentDir = path.dirname(filePath)
|
||||
await Permission.ask({
|
||||
type: "external-directory",
|
||||
pattern: parentDir,
|
||||
sessionID: ctx.sessionID,
|
||||
messageID: ctx.messageID,
|
||||
callID: ctx.callID,
|
||||
title: `Patch file outside working directory: ${filePath}`,
|
||||
metadata: {
|
||||
filepath: filePath,
|
||||
parentDir,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
switch (hunk.type) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { Filesystem } from "../util/filesystem"
|
||||
import { Instance } from "../project/instance"
|
||||
import { Provider } from "../provider/provider"
|
||||
import { Identifier } from "../id/id"
|
||||
import { Permission } from "../permission"
|
||||
|
||||
const DEFAULT_READ_LIMIT = 2000
|
||||
const MAX_LINE_LENGTH = 2000
|
||||
@@ -28,7 +29,19 @@ export const ReadTool = Tool.define("read", {
|
||||
const title = path.relative(Instance.worktree, filepath)
|
||||
|
||||
if (!ctx.extra?.["bypassCwdCheck"] && !Filesystem.contains(Instance.directory, filepath)) {
|
||||
throw new Error(`File ${filepath} is not in the current working directory`)
|
||||
const parentDir = path.dirname(filepath)
|
||||
await Permission.ask({
|
||||
type: "external-directory",
|
||||
pattern: parentDir,
|
||||
sessionID: ctx.sessionID,
|
||||
messageID: ctx.messageID,
|
||||
callID: ctx.callID,
|
||||
title: `Access file outside working directory: ${filepath}`,
|
||||
metadata: {
|
||||
filepath,
|
||||
parentDir,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const file = Bun.file(filepath)
|
||||
|
||||
@@ -20,7 +20,19 @@ export const WriteTool = Tool.define("write", {
|
||||
async execute(params, ctx) {
|
||||
const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)
|
||||
if (!Filesystem.contains(Instance.directory, filepath)) {
|
||||
throw new Error(`File ${filepath} is not in the current working directory`)
|
||||
const parentDir = path.dirname(filepath)
|
||||
await Permission.ask({
|
||||
type: "external-directory",
|
||||
pattern: parentDir,
|
||||
sessionID: ctx.sessionID,
|
||||
messageID: ctx.messageID,
|
||||
callID: ctx.callID,
|
||||
title: `Write file outside working directory: ${filepath}`,
|
||||
metadata: {
|
||||
filepath,
|
||||
parentDir,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const file = Bun.file(filepath)
|
||||
|
||||
Reference in New Issue
Block a user