Skip to content

Commit de0de9c

Browse files
committed
fix(ai): keep tools with a throwing execute runnable
1 parent 653650d commit de0de9c

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎packages/ai/src/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import {
3535
type TranscriptionRequest,
3636
type TranscriptionRequestInput,
3737
} from "./transcription.js"
38-
import { ToolFailure, build, type ToolExecuteContext, type ToolOptions, type ToolSchema } from "./tool.js"
38+
import { ToolFailure, build } from "./tool.js"
39+
import type { NoExecute, ToolExecuteContext, ToolOptions, ToolSchema } from "./tool.js"
3940
import { fileMediaType } from "./utils/media-type.js"
4041
import { Video, type VideoModel, type VideoRequest, type VideoRequestInput } from "./video.js"
4142

@@ -282,7 +283,7 @@ const fileError = (message: string, cause: unknown) =>
282283
export const tool = <
283284
Parameters extends ToolSchema<any> = typeof Schema.Unknown,
284285
Success extends ToolSchema<any> = typeof Schema.Unknown,
285-
Result extends Schema.Schema.Type<Success> | PromiseLike<Schema.Schema.Type<Success>> = never,
286+
Result extends Schema.Schema.Type<Success> | PromiseLike<Schema.Schema.Type<Success>> | NoExecute = NoExecute,
286287
>(
287288
config: ToolOptions<
288289
Parameters,

‎packages/ai/src/tool.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export type ToolOptions<
130130
export const tool = <
131131
Parameters extends ToolSchema<any> = typeof Schema.Unknown,
132132
Success extends ToolSchema<any> = typeof Schema.Unknown,
133-
Result extends Effect.Effect<Schema.Schema.Type<Success>, ToolFailure> = never,
133+
Result extends Effect.Effect<Schema.Schema.Type<Success>, ToolFailure> | NoExecute = NoExecute,
134134
>(
135135
config: ToolOptions<
136136
Parameters,
@@ -139,14 +139,22 @@ export const tool = <
139139
>,
140140
) => build(config, config.execute)
141141

142-
/** @internal Builds a tool for both clients from its options and an Effect `execute`; no `execute` infers `never`. */
143-
export function build<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>, Execute>(
144-
config: ToolOptions<Parameters, Success, Execute>,
145-
execute: ToolExecute<Parameters, ToolSchema<unknown>> | undefined,
146-
): Execute extends (...args: never) => never ? Tool<Parameters, Success> : ExecutableTool<Parameters, Success>
142+
declare const noExecute: unique symbol
143+
/** @internal The inferred `execute` result when `execute` is omitted; no function can return it. */
144+
export type NoExecute = { readonly [noExecute]: never }
145+
146+
/** @internal Builds a tool for both clients from its options and an Effect `execute`. */
147+
export function build<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>, Result>(
148+
config: ToolOptions<Parameters, Success, (...args: never) => Result>,
149+
execute: ((params: Schema.Schema.Type<Parameters>, context?: ToolExecuteContext) => unknown) | undefined,
150+
): 0 extends 1 & Result // `any`
151+
? ExecutableTool<Parameters, Success>
152+
: [Result, NoExecute] extends [NoExecute, Result]
153+
? Tool<Parameters, Success>
154+
: ExecutableTool<Parameters, Success>
147155
export function build(
148156
config: ToolOptions<ToolSchema<any>, ToolSchema<any>, unknown>,
149-
execute: ToolExecute<ToolSchema<any>, ToolSchema<any>> | undefined,
157+
execute: ((params: any, context?: ToolExecuteContext) => any) | undefined,
150158
): Tool {
151159
const shared = {
152160
description: config.description,

‎packages/ai/test/loop-tools.types.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ LLM.loop({ model, prompt: "Use any tool.", tools: { add, echo, weather, shout }
4747

4848
// @ts-expect-error The loop runs tools, so each one needs an execute handler.
4949
LLM.loop({ model, prompt: "Get weather.", tools: { schemaOnly } })
50+
51+
const unavailable = () => {
52+
throw new Error("Unavailable.")
53+
}
54+
const fail = AI.tool({ description: "Fail.", parameters: Schema.String, execute: unavailable })
55+
const parse = AI.tool({ description: "Parse.", parameters: Schema.String, execute: (text) => JSON.parse(text) })
56+
const inspect = tool({ description: "Inspect.", parameters: Schema.String, execute: () => Effect.succeed<unknown>(1) })
57+
ai.llm.loop({ model, prompt: "Fail.", tools: { fail, parse, inspect } })

0 commit comments

Comments
 (0)