mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-05 00:23:10 +00:00
core: extract external directory validation to shared utility to reduce code duplication across tools
This commit is contained in:
33
packages/opencode/src/tool/external-directory.ts
Normal file
33
packages/opencode/src/tool/external-directory.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import path from "path"
|
||||
import type { Tool } from "./tool"
|
||||
import { Filesystem } from "../util/filesystem"
|
||||
import { Instance } from "../project/instance"
|
||||
|
||||
type Kind = "file" | "directory"
|
||||
|
||||
type Options = {
|
||||
bypass?: boolean
|
||||
kind?: Kind
|
||||
}
|
||||
|
||||
export async function assertExternalDirectory(ctx: Tool.Context, target?: string, options?: Options) {
|
||||
if (!target) return
|
||||
|
||||
if (options?.bypass) return
|
||||
|
||||
if (Filesystem.contains(Instance.directory, target)) return
|
||||
|
||||
const kind = options?.kind ?? "file"
|
||||
const parentDir = kind === "directory" ? target : path.dirname(target)
|
||||
const glob = path.join(parentDir, "*")
|
||||
|
||||
await ctx.ask({
|
||||
permission: "external_directory",
|
||||
patterns: [glob],
|
||||
always: [glob],
|
||||
metadata: {
|
||||
filepath: target,
|
||||
parentDir,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user