mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(core): normalize and validate tool input schemas at registration (#61537)
This commit is contained in:
committed by
GitHub
Unverified
parent
644d41f37a
commit
d10c993904
@@ -361,7 +361,9 @@ 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 }) {
|
||||
function commit(editor: Data, id: string, input: Tool.Info & { readonly id: string }) {
|
||||
// JavaScript plugins may leave out `input` for a tool that takes none.
|
||||
const tool = input.input === undefined || input.input === null ? { ...input, input: {} } : input
|
||||
const registered = register(tool)
|
||||
if (Result.isFailure(registered)) {
|
||||
editor.errors.push({
|
||||
@@ -400,6 +402,11 @@ function register(tool: Tool.Info): Result.Result<ToolDefinition, RegistrationEr
|
||||
const id = effectiveName(tool)
|
||||
if (tool.options?.codemode === false && id === "execute")
|
||||
return Result.fail(new RegistrationError({ name: id, message: 'Tool name "execute" is reserved for CodeMode' }))
|
||||
// Standard Schema libraries such as ArkType make schemas that are functions.
|
||||
if (typeof tool.input !== "object" && typeof tool.input !== "function")
|
||||
return Result.fail(
|
||||
new RegistrationError({ name: id, message: `Invalid tool definition ${id}: input must be a schema` }),
|
||||
)
|
||||
return Result.try({
|
||||
try: () => ToolDefinition.make(definition(tool)),
|
||||
catch: (error) =>
|
||||
|
||||
@@ -609,6 +609,7 @@ describe("Tool", () => {
|
||||
execute: () => Effect.succeed({ content: "ok" }),
|
||||
options: { codemode: false },
|
||||
} as unknown as Info)
|
||||
editor.add({ ...make(), name: "boolean_input", input: true, options: { codemode: false } } as unknown as Info)
|
||||
editor.add({ ...make(), name: "codemode" })
|
||||
})
|
||||
|
||||
@@ -621,6 +622,14 @@ describe("Tool", () => {
|
||||
error: expect.stringContaining('Expected string\n at ["description"]'),
|
||||
},
|
||||
],
|
||||
[
|
||||
"Skipping invalid tool registration",
|
||||
{
|
||||
name: "boolean_input",
|
||||
namespace: undefined,
|
||||
error: "Invalid tool definition boolean_input: input must be a schema",
|
||||
},
|
||||
],
|
||||
])
|
||||
const snapshot = yield* service.snapshot()
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["healthy", "execute"])
|
||||
@@ -688,10 +697,14 @@ describe("Tool", () => {
|
||||
input: undefined,
|
||||
} as unknown as Info),
|
||||
)
|
||||
yield* service.transform((editor) =>
|
||||
editor.add({ ...make(), name: "direct", input: null, options: { codemode: false } } as unknown as Info),
|
||||
)
|
||||
|
||||
const snapshot = yield* service.snapshot()
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["execute"])
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["direct", "execute"])
|
||||
expect(codeModeListings(snapshot.codeModeCatalog!)[0]?.line).toContain("tools.echo")
|
||||
expect((yield* snapshot.execute(call("direct"))).output).toEqual({ text: "direct" })
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user