mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
refactor(core): share tool permission invocation and registry steps (#61169)
This commit is contained in:
committed by
GitHub
Unverified
parent
ff5be41bf7
commit
c930ef1f08
@@ -78,11 +78,6 @@ export const resolvePath = (directory: string, input: string, home = Global.Path
|
||||
}
|
||||
|
||||
const slash = (value: string) => value.replaceAll("\\", "/")
|
||||
const invocation = (context: Invocation) => ({
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool" as const, messageID: context.messageID, id: context.id },
|
||||
})
|
||||
|
||||
const layer = Layer.effect(
|
||||
Service,
|
||||
@@ -137,7 +132,7 @@ const layer = Layer.effect(
|
||||
resources: external.map((item) => item.resource),
|
||||
save: external.map((item) => item.save),
|
||||
...(metadata === undefined ? {} : { metadata }),
|
||||
...invocation(context),
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
})
|
||||
|
||||
@@ -155,7 +150,7 @@ const layer = Layer.effect(
|
||||
action: "read",
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
...invocation(context),
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
return target
|
||||
})
|
||||
|
||||
@@ -76,9 +76,7 @@ const layer = Layer.effect(
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
metadata: preview ? { files: [preview] } : undefined,
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ export * as Permission from "./permission.js"
|
||||
import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import { Context, Deferred, Effect, Layer, Schema } from "effect"
|
||||
import { Permission } from "@opencode/schema/permission"
|
||||
import type { Tool } from "@opencode/schema/tool"
|
||||
import { Bus } from "./bus.js"
|
||||
import { Location } from "./location.js"
|
||||
import { Agent } from "./agent.js"
|
||||
@@ -35,6 +36,13 @@ export interface AssertInput extends Omit<Request, "id" | "message"> {
|
||||
readonly agent?: Agent.ID
|
||||
}
|
||||
|
||||
/** The requester fields of a permission request made by a tool call. */
|
||||
export const invocation = (context: Pick<Tool.Context, "sessionID" | "agent" | "messageID" | "id">) => ({
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool" as const, messageID: context.messageID, id: context.id },
|
||||
})
|
||||
|
||||
export interface ReplyInput {
|
||||
readonly requestID: ID
|
||||
readonly reply: Reply
|
||||
|
||||
+23
-30
@@ -178,6 +178,9 @@ const layer = Layer.effect(
|
||||
}
|
||||
})
|
||||
|
||||
const run = (tool: Tool.Info, name: string, input: unknown, context: Tool.Context) =>
|
||||
beforeExecute(name, input, context).pipe(Effect.flatMap((event) => executeTool(tool, name, event.input, context)))
|
||||
|
||||
let catalog:
|
||||
| {
|
||||
data: Data
|
||||
@@ -207,19 +210,8 @@ const layer = Layer.effect(
|
||||
editor.namespaces.set(namespace.name, { ...namespace })
|
||||
},
|
||||
add: (tool) => {
|
||||
const registered = register(tool)
|
||||
if (Result.isFailure(registered)) {
|
||||
editor.errors.push({
|
||||
kind: "tool",
|
||||
name: tool.name,
|
||||
namespace: tool.options?.namespace,
|
||||
error: registered.failure,
|
||||
})
|
||||
return
|
||||
}
|
||||
const id = effectiveName(tool)
|
||||
editor.tools.set(id, { ...tool, id, options: tool.options && { ...tool.options } })
|
||||
editor.definitions.set(id, registered.success)
|
||||
commit(editor, id, { ...tool, id, options: tool.options && { ...tool.options } })
|
||||
},
|
||||
update: (id, update) => {
|
||||
const current = editor.tools.get(id)
|
||||
@@ -230,18 +222,7 @@ const layer = Layer.effect(
|
||||
tool.id = id
|
||||
if (tool.options?.namespace !== current.options?.namespace)
|
||||
tool.options = { ...tool.options, namespace: current.options?.namespace }
|
||||
const registered = register(tool)
|
||||
if (Result.isFailure(registered)) {
|
||||
editor.errors.push({
|
||||
kind: "tool",
|
||||
name: tool.name,
|
||||
namespace: tool.options?.namespace,
|
||||
error: registered.failure,
|
||||
})
|
||||
return
|
||||
}
|
||||
editor.tools.set(id, tool)
|
||||
editor.definitions.set(id, registered.success)
|
||||
commit(editor, id, tool)
|
||||
},
|
||||
remove: (id) => {
|
||||
editor.tools.delete(id)
|
||||
@@ -291,10 +272,7 @@ const layer = Layer.effect(
|
||||
const codeModeTool = codeModeEnabled
|
||||
? CodeModeTool.create(
|
||||
codeModeInventory,
|
||||
(name, tool, input, context) =>
|
||||
beforeExecute(name, input, context).pipe(
|
||||
Effect.flatMap((event) => executeTool(tool, name, event.input, context)),
|
||||
),
|
||||
(name, tool, input, context) => run(tool, name, input, context),
|
||||
permission,
|
||||
)
|
||||
: undefined
|
||||
@@ -374,8 +352,7 @@ const layer = Layer.effect(
|
||||
...input.context,
|
||||
id: Tool.CallID.make(`call_${input.context.messageID}_${index}`),
|
||||
}
|
||||
const event = yield* beforeExecute(name, toolInput, context)
|
||||
return yield* executeTool(tool, name, event.input, context)
|
||||
return yield* run(tool, name, toolInput, context)
|
||||
}),
|
||||
)
|
||||
}),
|
||||
@@ -383,6 +360,22 @@ const layer = Layer.effect(
|
||||
}),
|
||||
)
|
||||
|
||||
/** Store a valid tool under its effective name, or record why it was skipped. */
|
||||
function commit(editor: Data, id: string, tool: Tool.Info & { readonly id: string }) {
|
||||
const registered = register(tool)
|
||||
if (Result.isFailure(registered)) {
|
||||
editor.errors.push({
|
||||
kind: "tool",
|
||||
name: tool.name,
|
||||
namespace: tool.options?.namespace,
|
||||
error: registered.failure,
|
||||
})
|
||||
return
|
||||
}
|
||||
editor.tools.set(id, tool)
|
||||
editor.definitions.set(id, registered.success)
|
||||
}
|
||||
|
||||
const whollyDisabled = (action: string, rules: Permission.Ruleset) => {
|
||||
const rule = rules.findLast((rule) => Wildcard.match(action, rule.action))
|
||||
return rule?.resource === "*" && rule.effect === "deny"
|
||||
|
||||
@@ -16,14 +16,15 @@ Do not add a second executable entry type, registry-owned executor, authorizatio
|
||||
|
||||
Tool schemas use `input` and `output` terminology. Each tool carries its name, options, schemas, and executable behavior in one object.
|
||||
|
||||
Location-scoped built-in layers acquire `Permission.Service` and every other required Location service while the layer is constructed. The executor captures those services. Permission sources are always constructed from the canonical invocation context:
|
||||
Location-scoped built-in layers acquire `Permission.Service` and every other required Location service while the layer is constructed. The executor captures those services. Permission requests always take their requester fields from the canonical invocation context through `Permission.invocation`:
|
||||
|
||||
```ts
|
||||
const source = {
|
||||
type: "tool" as const,
|
||||
messageID: context.messageID,
|
||||
id: context.id,
|
||||
}
|
||||
yield* permission.assert({
|
||||
action: "read",
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
```
|
||||
|
||||
Leaves own resolution, permission, and side-effect ordering. Translate only expected typed errors into `ToolFailure`; do not use `catchCause`, because interruption and defects must survive. User declines from `Permission.assert` and question dismissals travel as defects beneath leaf `mapError` blankets and resurface as typed failures at `SessionModelRequest.executeTool`; leaves must never catch or convert them. A decline with feedback (`Permission.CorrectedError`) stays typed so the leaf converts it into `ToolFailure` and the model continues.
|
||||
|
||||
@@ -13,14 +13,16 @@ import { Permission } from "../permission.js"
|
||||
import { Tool } from "../tool.js"
|
||||
import { ToolOutput } from "../tool-output.js"
|
||||
import { ReadToolFileSystem } from "./read-filesystem.js"
|
||||
import { effectiveName, normalizedName } from "./runtime.js"
|
||||
|
||||
const decodeJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown))
|
||||
|
||||
/**
|
||||
* Registry namespace and permission action names for MCP tools.
|
||||
*/
|
||||
export const namespace = (server: string) => server.replace(/[^a-zA-Z0-9_-]/g, "_")
|
||||
export const name = (server: string, tool: string) => `${namespace(server)}_${tool.replace(/[^a-zA-Z0-9_-]/g, "_")}`
|
||||
export const namespace = (server: string) => normalizedName({ name: server })
|
||||
export const name = (server: string, tool: string) =>
|
||||
effectiveName({ name: tool, options: { namespace: namespace(server) } })
|
||||
|
||||
export interface Interface {
|
||||
/**
|
||||
@@ -63,13 +65,7 @@ export const layer = Layer.effect(
|
||||
resources: ["*"],
|
||||
save: ["*"],
|
||||
metadata: { input: input ?? {} },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: {
|
||||
type: "tool",
|
||||
messageID: context.messageID,
|
||||
id: context.id,
|
||||
},
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const result = yield* mcp
|
||||
.callTool({
|
||||
|
||||
@@ -277,11 +277,6 @@ export const Plugin = {
|
||||
output: Output,
|
||||
execute: (input, context) => {
|
||||
return Effect.gen(function* () {
|
||||
const permissionSource = {
|
||||
type: "tool" as const,
|
||||
messageID: context.messageID,
|
||||
id: context.id,
|
||||
}
|
||||
if (input.oldString === input.newString) {
|
||||
return yield* new ToolFailure({
|
||||
message: "No changes to apply: oldString and newString are identical.",
|
||||
@@ -330,9 +325,7 @@ export const Plugin = {
|
||||
action: "read",
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: permissionSource,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const hint = readable ? missHint(source, input.oldString, input.newString) : undefined
|
||||
return yield* new ToolFailure({
|
||||
|
||||
@@ -64,7 +64,6 @@ export const Plugin = {
|
||||
execute: (input, context) =>
|
||||
Effect.gen(function* () {
|
||||
const searchPath = input.path === "undefined" || input.path === "null" ? undefined : input.path
|
||||
const source = { type: "tool" as const, messageID: context.messageID, id: context.id }
|
||||
const target = yield* access.resolve({ path: searchPath ?? ".", kind: "directory" })
|
||||
yield* access.authorizeExternal([target], context)
|
||||
yield* permission.assert({
|
||||
@@ -77,9 +76,7 @@ export const Plugin = {
|
||||
hidden: input.hidden,
|
||||
limit: input.limit,
|
||||
},
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const type = yield* ToolSearch.pathType(environment.files, target.absolute, searchPath ?? ".")
|
||||
if (type !== "directory")
|
||||
|
||||
@@ -82,7 +82,6 @@ export const Plugin = {
|
||||
output: Output,
|
||||
execute: (input, context) =>
|
||||
Effect.gen(function* () {
|
||||
const source = { type: "tool" as const, messageID: context.messageID, id: context.id }
|
||||
const target = yield* access.resolve({ path: input.path ?? "." })
|
||||
yield* access.authorizeExternal([target], context)
|
||||
yield* permission.assert({
|
||||
@@ -97,9 +96,7 @@ export const Plugin = {
|
||||
caseSensitive: input.caseSensitive,
|
||||
limit: input.limit,
|
||||
},
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const root = target.absolute
|
||||
const type = yield* ToolSearch.pathType(environment.files, root, input.path ?? ".")
|
||||
|
||||
@@ -165,9 +165,7 @@ export const Plugin = {
|
||||
resources: [source],
|
||||
save: ["*"],
|
||||
metadata: { url: source },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
return yield* Effect.gen(function* () {
|
||||
const response = yield* http
|
||||
@@ -264,7 +262,6 @@ export const Plugin = {
|
||||
output: Output,
|
||||
execute: (input, context) =>
|
||||
Effect.gen(function* () {
|
||||
const source = { type: "tool" as const, messageID: context.messageID, id: context.id }
|
||||
const resolved = yield* resolveModel(input.model, context.sessionID, input.images !== undefined)
|
||||
const model = resolved.model
|
||||
const variant = resolved.variant
|
||||
@@ -280,9 +277,7 @@ export const Plugin = {
|
||||
resources: [ref],
|
||||
save: ["*"],
|
||||
metadata: { ...input, model: ref },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
if (target) {
|
||||
yield* access.authorizeExternal([target], context)
|
||||
@@ -290,9 +285,7 @@ export const Plugin = {
|
||||
action: "edit",
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
}
|
||||
const images = input.images
|
||||
@@ -333,9 +326,7 @@ export const Plugin = {
|
||||
const request = {
|
||||
action: "edit",
|
||||
resources: [actual.resource],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
}
|
||||
yield* replaces ? permission.assert({ ...request, save: ["*"] }) : permission.assertNotDenied(request)
|
||||
}
|
||||
|
||||
@@ -39,9 +39,7 @@ export const Plugin = {
|
||||
resources: servers,
|
||||
save: servers,
|
||||
metadata: {},
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
if (input.server === undefined) return { output: yield* mcp.resourceCatalog() }
|
||||
return { output: yield* mcp.resources({ server: input.server }) }
|
||||
@@ -68,9 +66,7 @@ export const Plugin = {
|
||||
resources: [`${input.server}:${input.uri}`],
|
||||
save: [`${input.server}:*`],
|
||||
metadata: { server: input.server, uri: input.uri },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const resource = yield* mcp.readResource(input)
|
||||
if (!resource)
|
||||
|
||||
@@ -201,9 +201,7 @@ export const Plugin = {
|
||||
action: "edit",
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
yield* ctx.worktree
|
||||
.remove({
|
||||
|
||||
@@ -101,11 +101,6 @@ export const Plugin = {
|
||||
})
|
||||
}
|
||||
return Effect.gen(function* () {
|
||||
const source = {
|
||||
type: "tool" as const,
|
||||
messageID: context.messageID,
|
||||
id: context.id,
|
||||
}
|
||||
if (!input.patchText) return yield* new ToolFailure({ message: "patchText is required" })
|
||||
const hunks = yield* Effect.fromResult(parsed).pipe(
|
||||
Effect.mapError((error) => new ToolFailure({ message: `patch verification failed: ${error.message}` })),
|
||||
@@ -222,9 +217,7 @@ export const Plugin = {
|
||||
diff: patchFiles.map((file) => `${file.patch}\n`).join(""),
|
||||
files: patchFiles,
|
||||
},
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
|
||||
yield* Effect.forEach(
|
||||
|
||||
@@ -64,9 +64,7 @@ export const Plugin = {
|
||||
.assert({
|
||||
action: "question",
|
||||
resources: ["*"],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError((error) => new ToolFailure({ message: "Permission denied: question", error })),
|
||||
|
||||
@@ -59,22 +59,17 @@ export const Plugin = {
|
||||
if (!skill || !Skill.isUri(skill.path))
|
||||
return yield* new ToolFailure({ message: `File not found: ${input.path}` })
|
||||
const extra = yield* skills.files(id)
|
||||
const source = { type: "tool" as const, messageID: context.messageID, id: context.id }
|
||||
yield* permission.assert({
|
||||
action: "skill",
|
||||
resources: [skill.id],
|
||||
save: [skill.id],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
yield* permission.assert({
|
||||
action: "read",
|
||||
resources: [parsed.uri],
|
||||
save: ["*"],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const virtualFs = Environment.makeFiles(Environment.makeMemoryDriver())
|
||||
const encoder = new TextEncoder()
|
||||
|
||||
@@ -112,11 +112,6 @@ export const Plugin = {
|
||||
const permission = yield* Permission.Service
|
||||
|
||||
const prepare = Effect.fn("ShellTool.prepare")(function* (invocation: ShellCreateBefore, context: Tool.Context) {
|
||||
const source = {
|
||||
type: "tool" as const,
|
||||
messageID: context.messageID,
|
||||
id: context.id,
|
||||
}
|
||||
const target = yield* access.resolve({ path: invocation.cwd, kind: "directory" })
|
||||
invocation.cwd = target.absolute
|
||||
const timeout = invocation.timeout
|
||||
@@ -133,9 +128,7 @@ export const Plugin = {
|
||||
action: name,
|
||||
resources: parsed.commands.map((command) => command.resource),
|
||||
save: parsed.commands.map((command) => command.save),
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
// Approval can outlive the directory, so validate immediately before spawning.
|
||||
const workdir = yield* Environment.typeFollowing(environment.files, target.absolute).pipe(
|
||||
|
||||
@@ -52,9 +52,7 @@ export const Plugin = {
|
||||
action: name,
|
||||
resources: [skill.id],
|
||||
save: [skill.id],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const files = yield* skills.files(skill.id)
|
||||
return { name: skill.name, ...(yield* Skill.prepare(fs, skill, files)) }
|
||||
|
||||
@@ -146,13 +146,7 @@ export const Plugin = {
|
||||
action: name,
|
||||
resources: [agent.id],
|
||||
save: [agent.id],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: {
|
||||
type: "tool",
|
||||
messageID: context.messageID,
|
||||
id: context.id,
|
||||
},
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
.pipe(Effect.mapError((error) => new ToolFailure({ message: `Subagent denied: ${agent.id}`, error })))
|
||||
|
||||
|
||||
@@ -64,9 +64,7 @@ export const Plugin = {
|
||||
action: name,
|
||||
resources: ["*"],
|
||||
save: ["*"],
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError((error) => new ToolFailure({ message: "Unable to update todos", error })),
|
||||
|
||||
@@ -154,9 +154,7 @@ export const Plugin = {
|
||||
resources: [url],
|
||||
save: ["*"],
|
||||
metadata: input,
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
|
||||
const { body, contentType } = yield* Effect.gen(function* () {
|
||||
@@ -165,9 +163,7 @@ export const Plugin = {
|
||||
action: name,
|
||||
resources: [next],
|
||||
metadata: { ...input, url: next },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const response = yield* execute(http, check, url, input.format).pipe(
|
||||
Effect.catchIf(isCloudflareChallenge, () => execute(http, check, url, input.format, "opencode")),
|
||||
|
||||
@@ -51,9 +51,7 @@ export const Plugin = {
|
||||
resources: [input.query],
|
||||
save: ["*"],
|
||||
metadata: input,
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
...Permission.invocation(context),
|
||||
})
|
||||
const search = (providerID?: WebSearch.ID) =>
|
||||
websearch.query(
|
||||
|
||||
@@ -285,9 +285,9 @@ export const stringify = (value: unknown) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const normalizedName = (tool: Tool.Info) => tool.name.replace(/[^a-zA-Z0-9_-]/g, "_")
|
||||
export const normalizedName = (tool: Pick<Tool.Info, "name">) => tool.name.replace(/[^a-zA-Z0-9_-]/g, "_")
|
||||
|
||||
export const effectiveName = (tool: Tool.Info) =>
|
||||
export const effectiveName = (tool: Pick<Tool.Info, "name" | "options">) =>
|
||||
tool.options?.namespace === undefined
|
||||
? normalizedName(tool)
|
||||
: `${tool.options.namespace.replaceAll(".", "_")}_${normalizedName(tool)}`
|
||||
|
||||
Reference in New Issue
Block a user