import * as Context from "../../Context.ts"; import * as Effect from "../../Effect.ts"; import * as Layer from "../../Layer.ts"; import type * as Scope from "../../Scope.ts"; import * as Stream from "../../Stream.ts"; import * as AiError from "./AiError.ts"; import type * as Tool from "./Tool.ts"; declare const TypeId: "~effect/ai/Toolkit"; /** * Represents a collection of tools which can be used to enhance the * capabilities of a large language model. * * @example * ```ts * import { Effect, Schema } from "effect" * import { Tool, Toolkit } from "effect/unstable/ai" * * // Create individual tools * const GetCurrentTime = Tool.make("GetCurrentTime", { * description: "Get the current timestamp", * success: Schema.Number * }) * * const GetWeather = Tool.make("GetWeather", { * description: "Get weather for a location", * parameters: Schema.Struct({ location: Schema.String }), * success: Schema.Struct({ * temperature: Schema.Number, * condition: Schema.String * }) * }) * * // Create a toolkit with multiple tools * const MyToolkit = Toolkit.make(GetCurrentTime, GetWeather) * * const MyToolkitLayer = MyToolkit.toLayer({ * GetCurrentTime: () => Effect.succeed(Date.now()), * GetWeather: ({ location }) => * Effect.succeed({ * temperature: 72, * condition: "sunny" * }) * }) * ``` * * @since 1.0.0 * @category models */ export interface Toolkit> extends Effect.Effect, never, Tool.HandlersFor> { new (_: never): {}; readonly [TypeId]: typeof TypeId; /** * A record containing all tools in this toolkit. */ readonly tools: Tools; /** * A helper method which can be used for type-safe handler declarations. */ of>(handlers: Handlers): Handlers; /** * Converts a toolkit into a `Context` containing handlers for each tool * in the toolkit. */ toHandlers, EX = never, RX = never>(build: Handlers | Effect.Effect): Effect.Effect>, EX, RX>; /** * Converts a toolkit into a `Layer` containing handlers for each tool in the * toolkit. */ toLayer, EX = never, RX = never>( /** * Handler functions or Effect that produces handlers. */ build: Handlers | Effect.Effect): Layer.Layer, EX, Exclude>; } /** * Context provided to tool handlers during execution. * * @since 1.0.0 * @category models */ export interface HandlerContext { /** * Emit a preliminary result during long-running tool calls. * * Preliminary results are streamed to the caller before the handler completes, * enabling real-time progress updates for lengthy operations. */ readonly preliminary: (result: Tool.Success) => Effect.Effect; } /** * Represents any `Toolkit` instance, used for generic constraints. * * @since 1.0.0 * @category utility types */ export interface Any { readonly [TypeId]: typeof TypeId; readonly tools: Record; } /** * A utility type which can be used to extract the tool definitions from a * toolkit. * * @since 1.0.0 * @category utility types */ export type Tools = T extends Toolkit ? Tools : never; /** * A utility type which transforms either a record or an array of tools into * a record where keys are tool names and values are the tool instances. * * @since 1.0.0 * @category utility types */ export type ToolsByName = Tools extends Record ? { readonly [Name in keyof Tools]: Tools[Name]; } : Tools extends ReadonlyArray ? { readonly [Tool in Tools[number] as Tool["name"]]: Tool; } : never; /** * A utility type that maps tool names to their required handler functions. * * Handlers can return either the tool's custom failure type, an `AiErrorReason` * (which will be wrapped in `AiError`), or a full `AiError`. * * @since 1.0.0 * @category utility types */ export type HandlersFrom> = { readonly [Name in keyof Tools as Tool.RequiresHandler extends true ? Name : never]: (params: Tool.Parameters, context: HandlerContext) => Effect.Effect, Tool.Failure | AiError.AiError | AiError.AiErrorReason, Tool.HandlerServices>; }; /** * A toolkit instance with registered handlers ready for tool execution. * * @since 1.0.0 * @category models */ export interface WithHandler> { /** * The tools available in this toolkit instance. */ readonly tools: Tools; /** * Executes a tool call by name. * * Validates the input parameters, executes the corresponding handler, and * streams back both the typed result and encoded result. Streaming allows * handlers to emit preliminary results before completion. */ readonly handle: ( /** * The name of the tool to execute. */ name: Name, /** * Parameters to pass to the tool handler. */ params: Tool.Parameters) => Effect.Effect, Tool.HandlerError, Tool.HandlerServices>, AiError.AiError>; } /** * A utility type which can be used to extract the tools from a toolkit with * handlers. * * @since 1.0.0 * @category utility types */ export type WithHandlerTools = T extends WithHandler ? Tools : never; /** * An empty toolkit with no tools. * * Useful as a starting point for building toolkits or as a default value. Can * be extended using the merge function to add tools. * * @since 1.0.0 * @category constructors */ export declare const empty: Toolkit<{}>; /** * Creates a new toolkit from the specified tools. * * This is the primary constructor for creating toolkits. It accepts multiple * tools and organizes them into a toolkit that can be provided to AI language * models. * * @example * ```ts * import { Schema } from "effect" * import { Tool, Toolkit } from "effect/unstable/ai" * * const GetCurrentTime = Tool.make("GetCurrentTime", { * description: "Get the current timestamp", * success: Schema.Number * }) * * const GetWeather = Tool.make("get_weather", { * description: "Get weather information", * parameters: Schema.Struct({ location: Schema.String }), * success: Schema.Struct({ * temperature: Schema.Number, * condition: Schema.String * }) * }) * * const toolkit = Toolkit.make(GetCurrentTime, GetWeather) * ``` * * @since 1.0.0 * @category constructors */ export declare const make: >(...tools: Tools) => Toolkit>; /** * A utility type which flattens a record type for improved IDE display. * * @since 1.0.0 * @category utility types */ export type SimplifyRecord = { [K in keyof T]: T[K]; } & {}; /** * A utility type which merges a union of tool records into a single record. * * @since 1.0.0 * @category utility types */ export type MergeRecords = { readonly [K in Extract]: Extract ? V : never, Tool.Any>; }; /** * A utility type which merges the tools from multiple toolkits into a single * record. * * @since 1.0.0 * @category utility types */ export type MergedTools> = SimplifyRecord>>; /** * Merges multiple toolkits into a single toolkit. * * Combines all tools from the provided toolkits into one unified toolkit. * If there are naming conflicts, tools from later toolkits will override * tools from earlier ones. * * @example * ```ts * import { Schema } from "effect" * import { Tool, Toolkit } from "effect/unstable/ai" * * const mathToolkit = Toolkit.make( * Tool.make("add", { success: Schema.Number }), * Tool.make("subtract", { success: Schema.Number }) * ) * * const utilityToolkit = Toolkit.make( * Tool.make("get_time", { success: Schema.Number }), * Tool.make("get_weather", { success: Schema.String }) * ) * * const combined = Toolkit.merge(mathToolkit, utilityToolkit) * ``` * * @since 1.0.0 * @category constructors */ export declare const merge: >( /** * The toolkits to merge together. */ ...toolkits: Toolkits) => Toolkit>; export {}; //# sourceMappingURL=Toolkit.d.ts.map