refactor(tools): unify tool APIs and result handling (#38367)

This commit is contained in:
Kit Langton 2026-07-23 17:13:31 -04:00 committed by GitHub
commit 79c1544072
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
133 changed files with 3602 additions and 2770 deletions

View file

@ -6,6 +6,69 @@ import { Session } from "@opencode-ai/schema/session"
import { SessionMessage } from "@opencode-ai/schema/session-message"
import { Effect, Schema } from "effect"
const context = {
sessionID: Session.ID.make("ses_execute"),
agent: Agent.ID.make("build"),
messageID: SessionMessage.ID.make("msg_execute"),
callID: "call_execute",
progress: () => Effect.void,
}
test("canonical execution distinguishes declared, model-only, and raw schema outputs", async () => {
const declared = Tool.make({
description: "Declared",
input: Schema.Struct({ value: Schema.String }),
output: Schema.Struct({ value: Schema.String }),
execute: ({ value }) => Effect.succeed({ output: { value } }),
})
const modelOnly = Tool.make({
description: "Model only",
input: Schema.Struct({}),
execute: () => Effect.succeed({ content: "visible only", metadata: { kind: "model" } }),
})
const raw = Tool.make({
description: "Raw",
input: {},
output: {},
execute: (input) => Effect.succeed({ output: input, content: "raw" }),
})
expect(await Effect.runPromise(Tool.execute(declared, { value: "encoded" }, context))).toEqual({
output: { value: "encoded" },
content: [{ type: "text", text: '{"value":"encoded"}' }],
})
expect(await Effect.runPromise(Tool.execute(modelOnly, {}, context))).toEqual({
content: [{ type: "text", text: "visible only" }],
metadata: { kind: "model" },
})
expect(await Effect.runPromise(Tool.execute(raw, { unchecked: true }, context))).toEqual({
output: { unchecked: true },
content: [{ type: "text", text: "raw" }],
})
})
test("declared outputs cannot bypass validation and raw outputs stay JSON-compatible", async () => {
const missing: Tool.Any = {
description: "Missing output",
input: Schema.Struct({}),
output: Schema.String,
execute: () => Effect.succeed({ content: "not an output" }),
}
const invalid: Tool.Any = {
description: "Invalid raw output",
input: {},
output: {},
execute: () => Effect.succeed({ output: 1n, content: "not JSON" }),
}
expect((await Effect.runPromiseExit(Tool.execute(missing, {}, context))).toString()).toContain(
"Tool did not return its declared output",
)
expect((await Effect.runPromiseExit(Tool.execute(invalid, {}, context))).toString()).toContain(
"Tool returned a non-JSON value",
)
})
test("execute preserves successful results with visible unhandled rejections", async () => {
const child = Tool.make({
description: "Always fail",
@ -13,27 +76,10 @@ test("execute preserves successful results with visible unhandled rejections", a
output: Schema.String,
execute: () => Effect.fail(new Tool.Failure({ message: "Lookup refused" })),
})
const execute = ExecuteTool.create(new Map([["fail", { tool: child, name: "fail" }]]))
const result = await Effect.runPromise(
Tool.settle(
execute,
{
type: "tool-call",
id: "call_execute",
name: "execute",
input: { code: `tools.fail({}); return "done"` },
},
{
sessionID: Session.ID.make("ses_execute"),
agent: Agent.ID.make("build"),
messageID: SessionMessage.ID.make("msg_execute"),
callID: "call_execute",
progress: () => Effect.void,
},
),
)
const execute = ExecuteTool.create(new Map([["fail", { tool: child, name: "fail", permission: "fail" }]]))
const result = await Effect.runPromise(Tool.execute(execute, { code: `tools.fail({}); return "done"` }, context))
expect(result.structured).toEqual({ toolCalls: [{ tool: "fail", status: "error" }] })
expect(result.metadata).toEqual({ toolCalls: [{ tool: "fail", status: "error" }] })
expect(result.content).toEqual([
{
type: "text",
@ -52,40 +98,32 @@ test("execute supports callable namespace tools", async () => {
description: "Administer Slack",
input: Schema.Struct({}),
output: Schema.String,
execute: () => Effect.succeed("admin"),
execute: () => Effect.succeed({ output: "admin" }),
})
const child = Tool.make({
description: "Create a Slack resource",
input: Schema.Struct({}),
output: Schema.String,
execute: () => Effect.succeed("created"),
execute: () => Effect.succeed({ output: "created" }),
})
const execute = ExecuteTool.create(
new Map([
["slack_admin", { tool: callable, name: "admin", namespace: "slack" }],
["slack_admin_create", { tool: child, name: "create", namespace: "slack.admin" }],
["slack_admin", { tool: callable, name: "admin", namespace: "slack", permission: "slack_admin" }],
[
"slack_admin_create",
{ tool: child, name: "create", namespace: "slack.admin", permission: "slack_admin_create" },
],
]),
)
const result = await Effect.runPromise(
Tool.settle(
Tool.execute(
execute,
{
type: "tool-call",
id: "call_execute",
name: "execute",
input: { code: "return [await tools.slack.admin({}), await tools.slack.admin.create({})]" },
},
{
sessionID: Session.ID.make("ses_execute"),
agent: Agent.ID.make("build"),
messageID: SessionMessage.ID.make("msg_execute"),
callID: "call_execute",
progress: () => Effect.void,
},
{ code: "return [await tools.slack.admin({}), await tools.slack.admin.create({})]" },
context,
),
)
expect(result.structured).toEqual({
expect(result.metadata).toEqual({
toolCalls: [
{ tool: "slack.admin", status: "completed" },
{ tool: "slack.admin.create", status: "completed" },