diff --git a/packages/core/src/codemode/tool.ts b/packages/core/src/codemode/tool.ts index 5c947af5f73..c8ba658ecd0 100644 --- a/packages/core/src/codemode/tool.ts +++ b/packages/core/src/codemode/tool.ts @@ -325,7 +325,9 @@ function inventoryTools(inventory: Inventory) { description: child.description, input: child.inputSchema, freeform: registration.freeform !== undefined, - output: child.outputSchema ?? Schema.NullOr(Schema.String), + // JSON Schema, which Code Mode renders but does not check: Core has validated the result of the tool that ran, + // and a plugin may have renamed the call to a tool with another output. + output: child.outputSchema ?? { type: ["string", "null"] }, execute: (input) => Run.use((run) => run.executeTool(name, registration, input)), }) } diff --git a/packages/core/test/tool-registry.test.ts b/packages/core/test/tool-registry.test.ts index e1d2321120d..755ad4e7872 100644 --- a/packages/core/test/tool-registry.test.ts +++ b/packages/core/test/tool-registry.test.ts @@ -1008,6 +1008,37 @@ describe("Tool", () => { }), ) + it.effect("a call renamed inside a Code Mode program returns the renamed tool's structured output", () => + Effect.gen(function* () { + const service = yield* Tool.Service + const hooks = yield* PluginHooks.Service + const plain: Info = { + name: "plain", + description: "Return text", + input: Schema.Struct({ text: Schema.String }), + execute: () => Effect.succeed({ content: "plain" }), + } + yield* transform(service, { plain, constant: constant("structured") }) + yield* hooks.register("tool", "execute.before", (event) => + Effect.sync(() => { + if (event.tool === "plain") event.tool = "constant" + }), + ) + const snapshot = yield* service.snapshot() + const result = yield* snapshot.execute({ + sessionID, + ...identity, + call: { + type: "tool-call", + id: "call-program", + name: "execute", + input: { code: 'return await tools.plain({ text: "x" })' }, + }, + }) + expect(result.output).toMatchObject({ output: JSON.stringify({ text: "structured" }) }) + }), + ) + it.effect("hooks see one ID per call inside a Code Mode program", () => Effect.gen(function* () { const service = yield* Tool.Service