+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
155 lines
5.1 KiB
TypeScript
155 lines
5.1 KiB
TypeScript
import { describe, expect } from "bun:test"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { Cause, Effect, Exit, Schema } from "effect"
|
|
import { Agent } from "../../src/agent/agent"
|
|
import { MessageID, SessionID } from "../../src/session/schema"
|
|
import { Tool } from "@/tool/tool"
|
|
import { Truncate } from "@/tool/truncate"
|
|
import { testEffect } from "../lib/effect"
|
|
|
|
const it = testEffect(LayerNode.compile(LayerNode.group([Truncate.node, Agent.node])))
|
|
|
|
const params = Schema.Struct({ input: Schema.String })
|
|
|
|
function makeCtx(): Tool.Context {
|
|
return {
|
|
sessionID: SessionID.descending(),
|
|
messageID: MessageID.ascending(),
|
|
agent: "build",
|
|
abort: new AbortController().signal,
|
|
messages: [],
|
|
metadata() {
|
|
return Effect.void
|
|
},
|
|
ask() {
|
|
return Effect.void
|
|
},
|
|
}
|
|
}
|
|
|
|
function makeTool(id: string, executeFn?: () => void) {
|
|
return {
|
|
description: "test tool",
|
|
parameters: params,
|
|
execute() {
|
|
executeFn?.()
|
|
return Effect.succeed({ title: "test", output: "ok", metadata: {} })
|
|
},
|
|
}
|
|
}
|
|
|
|
describe("Tool.define", () => {
|
|
it.effect("object-defined tool does not mutate the original init object", () =>
|
|
Effect.gen(function* () {
|
|
const original = makeTool("test")
|
|
const originalExecute = original.execute
|
|
|
|
const info = yield* Tool.define("test-tool", Effect.succeed(original))
|
|
|
|
yield* info.init()
|
|
yield* info.init()
|
|
yield* info.init()
|
|
|
|
expect(original.execute).toBe(originalExecute)
|
|
}),
|
|
)
|
|
|
|
it.effect("effect-defined tool returns fresh objects and is unaffected", () =>
|
|
Effect.gen(function* () {
|
|
const info = yield* Tool.define(
|
|
"test-fn-tool",
|
|
Effect.succeed(() => Effect.succeed(makeTool("test"))),
|
|
)
|
|
|
|
const first = yield* info.init()
|
|
const second = yield* info.init()
|
|
|
|
expect(first).not.toBe(second)
|
|
}),
|
|
)
|
|
|
|
it.effect("object-defined tool returns distinct objects per init() call", () =>
|
|
Effect.gen(function* () {
|
|
const info = yield* Tool.define("test-copy", Effect.succeed(makeTool("test")))
|
|
|
|
const first = yield* info.init()
|
|
const second = yield* info.init()
|
|
|
|
expect(first).not.toBe(second)
|
|
}),
|
|
)
|
|
|
|
it.effect("execute receives decoded parameters", () =>
|
|
Effect.gen(function* () {
|
|
const parameters = Schema.Struct({
|
|
count: Schema.NumberFromString.pipe(Schema.optional, Schema.withDecodingDefaultType(Effect.succeed(5))),
|
|
})
|
|
const calls: Array<Schema.Schema.Type<typeof parameters>> = []
|
|
const info = yield* Tool.define(
|
|
"test-decoded",
|
|
Effect.succeed({
|
|
description: "test tool",
|
|
parameters,
|
|
execute(args: Schema.Schema.Type<typeof parameters>) {
|
|
calls.push(args)
|
|
return Effect.succeed({ title: "test", output: "ok", metadata: { truncated: false } })
|
|
},
|
|
}),
|
|
)
|
|
const ctx = makeCtx()
|
|
const tool = yield* info.init()
|
|
const execute = tool.execute as unknown as (args: unknown, ctx: Tool.Context) => ReturnType<typeof tool.execute>
|
|
|
|
yield* execute({}, ctx)
|
|
yield* execute({ count: "7" }, ctx)
|
|
|
|
expect(calls).toEqual([{ count: 5 }, { count: 7 }])
|
|
}),
|
|
)
|
|
|
|
// Regression for #28438: the wrap is the canonical "untyped → typed" boundary.
|
|
// When the LLM emits a tool call with a payload that fails the parameter
|
|
// schema, the wrap must surface a typed `Tool.InvalidArgumentsError` whose
|
|
// `.message` is the actionable prose the AI SDK feeds back to the model.
|
|
it.effect("invalid args surface as Tool.InvalidArgumentsError with friendly message and JSON path", () =>
|
|
Effect.gen(function* () {
|
|
const parameters = Schema.Struct({
|
|
questions: Schema.Array(
|
|
Schema.Struct({
|
|
question: Schema.String,
|
|
options: Schema.Array(Schema.String),
|
|
}),
|
|
),
|
|
})
|
|
const info = yield* Tool.define(
|
|
"qtest",
|
|
Effect.succeed({
|
|
description: "test tool",
|
|
parameters,
|
|
execute() {
|
|
return Effect.succeed({ title: "ok", output: "ok", metadata: { truncated: false } })
|
|
},
|
|
}),
|
|
)
|
|
const tool = yield* info.init()
|
|
const execute = tool.execute as unknown as (args: unknown, ctx: Tool.Context) => ReturnType<typeof tool.execute>
|
|
|
|
// Missing required `question` field on the first questions[] entry.
|
|
const exit = yield* execute({ questions: [{ options: ["a"] }] }, makeCtx()).pipe(Effect.exit)
|
|
expect(Exit.isFailure(exit)).toBe(true)
|
|
if (!Exit.isFailure(exit)) return
|
|
|
|
// The wrap ends with Effect.orDie, so the failure lives in the cause as a
|
|
// defect. Recover the typed instance from there.
|
|
const die = exit.cause.reasons.find(Cause.isDieReason)
|
|
const error = die?.defect
|
|
expect(error).toBeInstanceOf(Tool.InvalidArgumentsError)
|
|
const args = error as Tool.InvalidArgumentsError
|
|
expect(args.tool).toBe("qtest")
|
|
expect(args.message).toContain("qtest tool was called with invalid arguments")
|
|
expect(args.message).toContain("Please rewrite the input")
|
|
expect(args.message).toContain(`["questions"][0]["question"]`)
|
|
}),
|
|
)
|
|
})
|