"use client"; import { makeAssistantToolUI } from "@assistant-ui/react"; import { AlertCircleIcon, ImageIcon } from "lucide-react"; import { z } from "zod"; import { Image, ImageErrorBoundary, ImageLoading, parseSerializableImage, } from "@/components/tool-ui/image"; // ============================================================================ // Zod Schemas // ============================================================================ /** * Schema for display_image tool arguments */ const DisplayImageArgsSchema = z.object({ src: z.string(), alt: z.string().nullish(), title: z.string().nullish(), description: z.string().nullish(), }); /** * Schema for display_image tool result */ const DisplayImageResultSchema = z.object({ id: z.string(), assetId: z.string(), src: z.string(), alt: z.string().nullish(), title: z.string().nullish(), description: z.string().nullish(), domain: z.string().nullish(), ratio: z.string().nullish(), error: z.string().nullish(), }); // ============================================================================ // Types // ============================================================================ type DisplayImageArgs = z.infer; type DisplayImageResult = z.infer; /** * Error state component shown when image display fails */ function ImageErrorState({ src, error }: { src: string; error: string }) { return (

Failed to display image

{src}

{error}

); } /** * Cancelled state component */ function ImageCancelledState({ src }: { src: string }) { return (

Image: {src}

); } /** * Parsed Image component with error handling * Note: Image component has built-in click handling via href/src, * so no additional responseActions needed. */ function ParsedImage({ result }: { result: unknown }) { const image = parseSerializableImage(result); return ; } /** * Display Image Tool UI Component * * This component is registered with assistant-ui to render an image * when the display_image tool is called by the agent. * * It displays images with: * - Title and description * - Source attribution * - Hover overlay effects * - Click to open full size */ export const DisplayImageToolUI = makeAssistantToolUI({ toolName: "display_image", render: function DisplayImageUI({ args, result, status }) { const src = args.src || "Unknown"; // Loading state - tool is still running if (status.type === "running" || status.type === "requires-action") { return (
); } // Incomplete/cancelled state if (status.type === "incomplete") { if (status.reason === "cancelled") { return ; } if (status.reason === "error") { return ( ); } } // No result yet if (!result) { return (
); } // Error result from the tool if (result.error) { return ; } // Success - render the image return (
); }, }); export { DisplayImageArgsSchema, DisplayImageResultSchema, type DisplayImageArgs, type DisplayImageResult, };