import React, { useState } from "react"; import { LLMContent } from "../types"; interface ScreenshotToolProps { // For tool_use (pending state) toolInput?: unknown; isRunning?: boolean; // For tool_result (completed state) toolResult?: LLMContent[]; hasError?: boolean; executionTime?: string; display?: unknown; // Display data from the tool_result Content } function ScreenshotTool({ toolInput, isRunning, toolResult, hasError, executionTime, display, }: ScreenshotToolProps) { const [isExpanded, setIsExpanded] = useState(true); // Default to expanded // Extract display info from toolInput const getPath = (input: unknown): string | undefined => { if ( typeof input === "object" && input !== null && "path" in input && typeof input.path === "string" ) { return input.path; } return undefined; }; const getId = (input: unknown): string | undefined => { if ( typeof input === "object" && input !== null && "id" in input && typeof input.id === "string" ) { return input.id; } return undefined; }; const getSelector = (input: unknown): string | undefined => { if ( typeof input === "object" && input !== null && "selector" in input && typeof input.selector === "string" ) { return input.selector; } return undefined; }; const filename = getPath(toolInput) || getId(toolInput) || getSelector(toolInput) || "screenshot"; // Use display data passed as prop (from tool_result Content.Display) const displayData = display; // Construct image URL let imageUrl: string | undefined = undefined; if (displayData && typeof displayData === "object" && displayData !== null) { const url = "url" in displayData && typeof displayData.url === "string" ? displayData.url : undefined; const path = "path" in displayData && typeof displayData.path === "string" ? displayData.path : undefined; const id = "id" in displayData && typeof displayData.id === "string" ? displayData.id : undefined; imageUrl = url || (path ? `/api/read?path=${encodeURIComponent(path)}` : id ? `/api/read?path=${encodeURIComponent(id)}` : undefined); } const isComplete = !isRunning && toolResult !== undefined; return (
setIsExpanded(!isExpanded)}>
📷 {filename} {isComplete && hasError && ✗} {isComplete && !hasError && ✓}
{isExpanded && (
{isComplete && !hasError && imageUrl && (
{executionTime && (
Screenshot: {executionTime}
)}
{`Screenshot:
)} {isComplete && hasError && (
Error: {executionTime && {executionTime}}
                {toolResult && toolResult[0]?.Text
                  ? toolResult[0].Text
                  : "Screenshot capture failed"}
              
)} {isRunning && (
Capturing screenshot...
)}
)}
); } export default ScreenshotTool;