mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(core): honor plugin tool renames in Code Mode (#63633)
This commit is contained in:
committed by
GitHub
Unverified
parent
86755334b4
commit
e0ab9b0ebc
@@ -181,8 +181,19 @@ 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)))
|
||||
// A plugin may rename a call in a Code Mode program, as it may a direct call, to another of the program's tools.
|
||||
const run = Effect.fnUntraced(function* (
|
||||
tools: ReadonlyMap<string, Tool.Info>,
|
||||
tool: Tool.Info,
|
||||
name: string,
|
||||
input: unknown,
|
||||
context: Tool.Context,
|
||||
) {
|
||||
const event = yield* beforeExecute(name, input, context)
|
||||
const selected = event.tool === name ? tool : tools.get(event.tool)
|
||||
if (!selected) return yield* new Tool.Error({ message: `Tool is not available for this request: ${event.tool}` })
|
||||
return yield* executeTool(selected, event.tool, event.input, context)
|
||||
})
|
||||
|
||||
let catalog:
|
||||
| {
|
||||
@@ -275,7 +286,7 @@ const layer = Layer.effect(
|
||||
const codeModeTool = codeModeEnabled
|
||||
? CodeModeTool.create(
|
||||
codeModeInventory,
|
||||
(name, tool, input, context) => run(tool, name, input, context),
|
||||
(name, tool, input, context) => run(codeModeInventory.tools, tool, name, input, context),
|
||||
permission,
|
||||
)
|
||||
: undefined
|
||||
@@ -356,7 +367,7 @@ const layer = Layer.effect(
|
||||
...input.context,
|
||||
id: Tool.CallID.make(`call_${input.context.messageID}_${index}`),
|
||||
}
|
||||
return yield* run(tool, name, toolInput, context)
|
||||
return yield* run(tools, tool, name, toolInput, context)
|
||||
}),
|
||||
)
|
||||
}),
|
||||
|
||||
@@ -962,6 +962,52 @@ describe("Tool", () => {
|
||||
)
|
||||
})
|
||||
|
||||
it.effect("a hook can rename a call inside a Code Mode program to another of its tools", () =>
|
||||
Effect.gen(function* () {
|
||||
const service = yield* Tool.Service
|
||||
const hooks = yield* PluginHooks.Service
|
||||
yield* transform(service, { echo: make(), constant: constant("renamed") })
|
||||
yield* transform(service, { direct: constant("direct") }, { codemode: false })
|
||||
const seen: string[] = []
|
||||
yield* hooks.register("tool", "execute.before", (event) =>
|
||||
Effect.sync(() => {
|
||||
if (event.tool !== "echo") return
|
||||
event.tool = Schema.is(Schema.Struct({ text: Schema.String }))(event.input) ? event.input.text : event.tool
|
||||
}),
|
||||
)
|
||||
yield* hooks.register("tool", "execute.after", (event) => Effect.sync(() => void seen.push(event.tool)))
|
||||
const snapshot = yield* service.snapshot()
|
||||
const program = (code: string) =>
|
||||
snapshot.execute({
|
||||
sessionID,
|
||||
...identity,
|
||||
call: { type: "tool-call", id: "call-program", name: "execute", input: { code } },
|
||||
})
|
||||
|
||||
expect((yield* program('return await tools.echo({ text: "constant" })')).output).toMatchObject({
|
||||
output: JSON.stringify({ text: "renamed" }),
|
||||
})
|
||||
expect(seen).toContain("constant")
|
||||
// A program can only reach its own tools, however the call is renamed.
|
||||
for (const name of ["direct", "missing"])
|
||||
expect(
|
||||
(yield* program(`try { await tools.echo({ text: "${name}" }) } catch (e) { return e.message }`)).output,
|
||||
).toMatchObject({ output: `Tool is not available for this request: ${name}` })
|
||||
|
||||
const check = (tools: string[]) =>
|
||||
service.check({
|
||||
code: 'return await tools.echo({ text: "constant" })',
|
||||
tools,
|
||||
bindings: {},
|
||||
permissions: [],
|
||||
limits: {},
|
||||
context: { sessionID, ...identity, progress: () => Effect.void },
|
||||
})
|
||||
expect(yield* check(["echo"])).toMatchObject({ ok: false })
|
||||
expect(yield* check(["echo", "constant"])).toMatchObject({ ok: true, value: { text: "renamed" } })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("hooks see one ID per call inside a Code Mode program", () =>
|
||||
Effect.gen(function* () {
|
||||
const service = yield* Tool.Service
|
||||
|
||||
Reference in New Issue
Block a user