fix(ai): accept typed tools in loop (#60390)

This commit is contained in:
Aiden Cline
2026-10-29 20:34:48 -05:00
committed by GitHub
Unverified
parent b5c223182c
commit 4d9e4de387
3 changed files with 55 additions and 6 deletions
+3 -6
View File
@@ -74,14 +74,11 @@ export interface Definition<Parameters extends ToolSchema<any>, Success extends
readonly toStructuredOutput?: (output: Success["Encoded"]) => unknown
/** @internal */
readonly _decode: (input: unknown) => Effect.Effect<Schema.Schema.Type<Parameters>, Schema.SchemaError>
// Method syntax keeps these parameters bivariant, so a concrete tool stays assignable to `AnyExecutableTool`.
/** @internal */
readonly _encode: (value: Schema.Schema.Type<Success>) => Effect.Effect<unknown, Schema.SchemaError>
_encode(value: Schema.Schema.Type<Success>): Effect.Effect<unknown, Schema.SchemaError>
/** @internal */
readonly _project: (
parameters: Schema.Schema.Type<Parameters>,
id: ToolCallPart["id"],
output: unknown,
) => ToolOutputType
_project(parameters: Schema.Schema.Type<Parameters>, id: ToolCallPart["id"], output: unknown): ToolOutputType
/** @internal */
readonly _legacyResult: boolean
/** @internal */
+50
View File
@@ -0,0 +1,50 @@
import { Effect, Schema } from "effect"
import { LLM } from "../src/effect.js"
import * as OpenAIChat from "../src/protocols/openai-chat.js"
import { Auth } from "../src/route.js"
import { Tool } from "../src/tool.js"
import { AI, ai } from "../src/index.js"
const model = OpenAIChat.route.with({ auth: Auth.bearer("fixture") }).model({ id: "gpt-4o-mini" })
const add = AI.tool({
description: "Add two numbers.",
parameters: Schema.Struct({ a: Schema.Number, b: Schema.Number }),
execute: (input) => input.a + input.b,
})
const echo = AI.tool({
description: "Echo text.",
parameters: Schema.String,
execute: (input) => input.toUpperCase(),
})
const weather = Tool.make({
description: "Get weather.",
parameters: Schema.Struct({ city: Schema.String }),
success: Schema.Struct({ forecast: Schema.String }),
execute: (input) => Effect.succeed({ forecast: input.city }),
})
const shout = Tool.make({
description: "Shout text.",
parameters: Schema.String,
success: Schema.String,
execute: (input) => Effect.succeed(input.toUpperCase()),
})
const schemaOnly = Tool.make({
description: "Get weather.",
parameters: Schema.Struct({ city: Schema.String }),
success: Schema.Struct({ forecast: Schema.String }),
})
ai.llm.loop({ model, prompt: "Add 2 and 3.", tools: { add } })
ai.llm.loop({ model, prompt: "Get weather.", tools: { weather } })
ai.llm.loop({ model, prompt: "Use any tool.", tools: { add, echo, weather, shout } })
LLM.loop({ model, prompt: "Add 2 and 3.", tools: { add } })
LLM.loop({ model, prompt: "Get weather.", tools: { weather } })
LLM.loop({ model, prompt: "Use any tool.", tools: { add, echo, weather, shout } })
// @ts-expect-error The loop runs tools, so each one needs an execute handler.
LLM.loop({ model, prompt: "Get weather.", tools: { schemaOnly } })
+2
View File
@@ -8,6 +8,8 @@
"include": [
"example/**/*.ts",
"test/**/*.types.ts",
"test/loop.test.ts",
"test/promise.test.ts",
"test/testing.test.ts",
"test/testing-media.test.ts",
"test/provider/checkpoint.test.ts"