UNPKG

@convex-dev/agent

Version:

A agent component for Convex.

46 lines 1.59 kB
import { tool } from "ai"; import { z } from "zod"; /** * 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 function createTool(t) { const args = { __acceptsCtx: true, ctx: t.ctx, description: t.description, parameters: t.args, async execute(args, options) { if (!this.ctx) { throw new Error("To use a Convex tool, you must either provide the ctx" + " at definition time (dynamically in an action), or use the Agent to" + " call it (which injects the ctx, userId and threadId)"); } return t.handler(this.ctx, args, options); }, }; return tool(args); } export function wrapTools(ctx, ...toolSets) { const output = {}; for (const toolSet of toolSets) { if (!toolSet) { continue; } for (const [name, tool] of Object.entries(toolSet)) { // eslint-disable-next-line @typescript-eslint/no-explicit-any if (!tool.__acceptsCtx) { output[name] = tool; } else { const out = { ...tool, ctx }; output[name] = out; } } } return output; } //# sourceMappingURL=createTool.js.map