mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-18 07:12:47 +03:00
fix(ai): accept typed tools in loop (#60390)
This commit is contained in:
committed by
GitHub
Unverified
parent
b5c223182c
commit
4d9e4de387
@@ -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 */
|
||||
|
||||
@@ -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 } })
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user