@convex-dev/agent
Version:
A agent component for Convex.
66 lines (65 loc) • 3.01 kB
TypeScript
import type { FlexibleSchema } from "@ai-sdk/provider-utils";
import type { Tool, ToolCallOptions, ToolSet } from "ai";
import type { Agent } from "./index.js";
import type { GenericActionCtx, GenericDataModel } from "convex/server";
import type { ProviderOptions } from "../validators.js";
export type ToolCtx<DataModel extends GenericDataModel = GenericDataModel> = GenericActionCtx<DataModel> & {
agent?: Agent;
userId?: string;
threadId?: string;
messageId?: string;
};
/**
* This is a wrapper around the ai.tool function that adds extra context to the
* tool call, including the action context, userId, threadId, and messageId.
* @param tool The tool. See https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling
* but swap parameters for args and handler for execute.
* @returns A tool to be used with the AI SDK.
*/
export declare function createTool<INPUT, OUTPUT, Ctx extends ToolCtx = ToolCtx>(def: {
/**
An optional description of what the tool does.
Will be used by the language model to decide whether to use the tool.
Not used for provider-defined tools.
*/
description?: string;
/**
The schema of the input that the tool expects. The language model will use this to generate the input.
It is also used to validate the output of the language model.
Use descriptions to make the input understandable for the language model.
*/
args: FlexibleSchema<INPUT>;
/**
An async function that is called with the arguments from the tool call and produces a result.
If not provided, the tool will not be executed automatically.
@args is the input of the tool call.
@options.abortSignal is a signal that can be used to abort the tool call.
*/
handler: (ctx: Ctx, args: INPUT, options: ToolCallOptions) => PromiseLike<OUTPUT> | AsyncIterable<OUTPUT>;
/**
* Provide the context to use, e.g. when defining the tool at runtime.
*/
ctx?: Ctx;
/**
* Optional function that is called when the argument streaming starts.
* Only called when the tool is used in a streaming context.
*/
onInputStart?: (ctx: Ctx, options: ToolCallOptions) => void | PromiseLike<void>;
/**
* Optional function that is called when an argument streaming delta is available.
* Only called when the tool is used in a streaming context.
*/
onInputDelta?: (ctx: Ctx, options: {
inputTextDelta: string;
} & ToolCallOptions) => void | PromiseLike<void>;
/**
* Optional function that is called when a tool call can be started,
* even if the execute function is not provided.
*/
onInputAvailable?: (ctx: Ctx, options: {
input: [INPUT] extends [never] ? undefined : INPUT;
} & ToolCallOptions) => void | PromiseLike<void>;
providerOptions?: ProviderOptions;
}): Tool<INPUT, OUTPUT>;
export declare function wrapTools(ctx: ToolCtx, ...toolSets: (ToolSet | undefined)[]): ToolSet;
//# sourceMappingURL=createTool.d.ts.map