Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

1.8 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { z } from "zod";
import {
  type AiTool,
  type ToolDescription,
  ToolExecutionError,
  type ToolOutputBase,
  toolOutputBaseSchema,
} from "./base";
import type { CopilotMode } from "./registry";

const description: ToolDescription = {
  baseDescription: "Test frontend tool that returns a greeting message.",
  additionalInfo: `
  Args:
    - name (string): The name to include in the greeting.

  Returns:
    - Output with data containing the greeting message.
  `,
};

interface Input {
  name: string;
}

interface GreetingData {
  greeting: string;
  timestamp: string;
}

interface Output extends ToolOutputBase {
  data: GreetingData;
}

/** A sample frontend tool that demonstrates real tool output structure */
export class TestFrontendTool implements AiTool<Input, Output> {
  public readonly name = "test_frontend_tool";
  public readonly description = description;
  public readonly schema = z.object({ name: z.string() });
  public readonly outputSchema = toolOutputBaseSchema.extend({
    data: z.object({
      greeting: z.string(),
      timestamp: z.string(),
    }),
  });
  public readonly mode: CopilotMode[] = ["ask"];

  public async handler({ name }: Input): Promise<Output> {
    // Example: Validate input and throw ToolExecutionError on invalid data
    if (!name.trim()) {
      throw new ToolExecutionError("Name cannot be empty", {
        code: "INVALID_INPUT",
        isRetryable: false,
        suggestedFix: "Please provide a non-empty name",
        meta: { field: "name", received: name },
      });
    }

    return {
      status: "success",
      data: {
        greeting: `Hello: ${name}`,
        timestamp: new Date().toISOString(),
      },
      next_steps: [
        "You can now proceed with your next task",
        "Try calling another tool if needed",
      ],
    };
  }
}