|
| 1 | +/* Copyright 2024 Marimo. All rights reserved. */ |
| 2 | + |
| 3 | +import type { components } from "@marimo-team/marimo-api"; |
| 4 | +import { Memoize } from "typescript-memoize"; |
| 5 | +import { type ZodObject, z } from "zod"; |
| 6 | +import type { AiTool } from "./base"; |
| 7 | +import { TestFrontendTool } from "./sample-tool"; |
| 8 | + |
| 9 | +export type AnyZodObject = ZodObject<z.ZodRawShape>; |
| 10 | + |
| 11 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 12 | +type StoredTool = AiTool<any, any>; |
| 13 | + |
| 14 | +/** should be the same as marimo/_config/config.py > CopilotMode */ |
| 15 | + |
| 16 | +type ToolDefinition = components["schemas"]["ToolDefinition"]; |
| 17 | +export type CopilotMode = ToolDefinition["mode"][number]; |
| 18 | + |
| 19 | +export interface FrontendToolDefinition extends ToolDefinition { |
| 20 | + source: "frontend"; |
| 21 | +} |
| 22 | + |
| 23 | +export class FrontendToolRegistry { |
| 24 | + /** All registered tools */ |
| 25 | + private tools = new Map<string, StoredTool>(); |
| 26 | + |
| 27 | + constructor(tools: StoredTool[] = []) { |
| 28 | + this.tools = new Map(tools.map((tool) => [tool.name, tool])); |
| 29 | + } |
| 30 | + |
| 31 | + has(toolName: string) { |
| 32 | + return this.tools.has(toolName); |
| 33 | + } |
| 34 | + |
| 35 | + private getToolOrThrow(toolName: string): StoredTool { |
| 36 | + const tool = this.tools.get(toolName); |
| 37 | + if (!tool) { |
| 38 | + throw new Error(`Tool ${toolName} not found`); |
| 39 | + } |
| 40 | + return tool; |
| 41 | + } |
| 42 | + |
| 43 | + async invoke<TName extends string>( |
| 44 | + toolName: TName, |
| 45 | + rawArgs: unknown, |
| 46 | + ): Promise<unknown> { |
| 47 | + const tool = this.getToolOrThrow(toolName); |
| 48 | + const handler = tool.handler; |
| 49 | + const inputSchema = tool.schema; |
| 50 | + const outputSchema = tool.outputSchema; |
| 51 | + |
| 52 | + try { |
| 53 | + // Parse input args |
| 54 | + const inputResponse = await inputSchema.safeParseAsync(rawArgs); |
| 55 | + if (inputResponse.error) { |
| 56 | + const strError = z.prettifyError(inputResponse.error); |
| 57 | + throw new Error(`Tool ${toolName} returned invalid input: ${strError}`); |
| 58 | + } |
| 59 | + const args = inputResponse.data; |
| 60 | + |
| 61 | + // Call the handler |
| 62 | + const rawOutput = await handler(args); |
| 63 | + |
| 64 | + // Parse output |
| 65 | + const response = await outputSchema.safeParseAsync(rawOutput); |
| 66 | + if (response.error) { |
| 67 | + const strError = z.prettifyError(response.error); |
| 68 | + throw new Error( |
| 69 | + `Tool ${toolName} returned invalid output: ${strError}`, |
| 70 | + ); |
| 71 | + } |
| 72 | + const output = response.data; |
| 73 | + return output; |
| 74 | + } catch (error) { |
| 75 | + return { |
| 76 | + status: "error", |
| 77 | + code: "TOOL_ERROR", |
| 78 | + message: error instanceof Error ? error.message : String(error), |
| 79 | + suggestedFix: "Try again with valid arguments.", |
| 80 | + meta: { |
| 81 | + args: rawArgs, |
| 82 | + }, |
| 83 | + }; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Memoize() |
| 88 | + getToolSchemas(): FrontendToolDefinition[] { |
| 89 | + return [...this.tools.values()].map((tool) => ({ |
| 90 | + name: tool.name, |
| 91 | + description: tool.description, |
| 92 | + parameters: z.toJSONSchema(tool.schema), |
| 93 | + source: "frontend", |
| 94 | + mode: tool.mode, |
| 95 | + })); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export const FRONTEND_TOOL_REGISTRY = new FrontendToolRegistry([ |
| 100 | + ...(import.meta.env.DEV ? [new TestFrontendTool()] : []), |
| 101 | + // ADD MORE TOOLS HERE |
| 102 | +]); |
0 commit comments