import { ChatBubbleIcon, CheckIcon, ChevronRightIcon, ClipboardCopyIcon, CubeIcon, ExclamationTriangleIcon, FileTextIcon, GearIcon, LightningBoltIcon, PersonIcon, StackIcon, } from "@radix-ui/react-icons"; import { Badge, Button, Flex } from "@radix-ui/themes"; import { useEffect, useRef, useState } from "react"; import type { Capture, Json } from "../model.js"; import { group, output } from "./format.js"; import { JsonTree } from "./json-tree.js"; export function Glyph({ kind }: { kind: string }) { const Icon = kind === "toolResult" ? StackIcon : kind === "toolCall" ? GearIcon : kind === "assistant" ? ChatBubbleIcon : kind === "user" ? PersonIcon : kind === "thinking_level_change" ? GearIcon : group(kind) === "Model" ? CubeIcon : group(kind) === "Tool" ? LightningBoltIcon : kind === "system" ? FileTextIcon : CubeIcon; return ( ); } export function Status({ value = "recorded" }: { value?: string }) { const label = value === "ok" || value === "success" ? "Success" : value === "error" ? "Error" : value === "running" ? "Running" : value === "cancelled" ? "Cancelled" : value === "unfinished" ? "Unfinished" : "Recorded"; return ( {label === "Success" ? ( ) : label === "Error" ? ( ) : ( )} {label} ); } function images(value: Json, found: { data: string; mimeType: string }[] = []): typeof found { if (!value || typeof value !== "object" || found.length >= 4) return found; if ( !Array.isArray(value) && value.type === "image" && typeof value.data === "string" && typeof value.mimeType === "string" && /^(image\/(png|jpeg|gif|webp))$/.test(value.mimeType) && value.data.length <= 16384 && /^[A-Za-z0-9+/]*={0,2}$/.test(value.data) ) { if (!found.some((image) => image.data === value.data && image.mimeType === value.mimeType)) found.push({ data: value.data, mimeType: value.mimeType }); } else for (const child of Object.values(value)) images(child, found); return found; } export function Copy({ value }: { value: string }) { const mounted = useRef(false); const [feedback, setFeedback] = useState(""); const [pending, setPending] = useState(false); useEffect(() => { mounted.current = true; return () => { mounted.current = false; }; }, []); async function copy() { setPending(true); try { await navigator.clipboard.writeText(value); if (mounted.current) setFeedback("Copied"); } catch { if (mounted.current) setFeedback("Copy failed"); } finally { if (mounted.current) setPending(false); } } return ( ); } function colored(line: string, budget: { remaining: number }) { if (line.length > 2000 || budget.remaining <= 0) return line; const fragments = []; let cursor = 0; for (const match of line.matchAll(/("(?:[^"\\]|\\.)*"|\b(?:true|false|null|\d+(?:\.\d+)?)\b)/g)) { if (--budget.remaining < 0) return line; if (match.index > cursor) fragments.push(line.slice(cursor, match.index)); const part = match[0]; fragments.push( {part} , ); cursor = match.index + part.length; } fragments.push(line.slice(cursor)); return fragments; } export interface DataState { open: boolean; format: "tree" | "text"; } export function Data({ data, label, defaultOpen = true, scope = label, state, changeState, }: { data?: Capture; label: string; defaultOpen?: boolean; scope?: string; state?: DataState; changeState?(state: DataState): void; }) { const [localState, setLocalState] = useState({ open: defaultOpen, format: "tree" }); const { open, format } = state ?? localState; const change = changeState ?? setLocalState; const value = data ? output(data.value) : "unavailable"; const lines = value.split("\n"); const budget = { remaining: 512 }; let sourceOffset = 0; const structured = data?.value !== null && typeof data?.value === "object"; return (
{data?.truncated && truncated} {data && }
{open && (
{structured && (
{(["tree", "text"] as const).map((mode) => ( ))} Redacted display copy
)} {structured && format === "tree" && data ? ( ) : (
              {lines.slice(0, 1000).map((line, index) => {
                const start = sourceOffset;
                sourceOffset += line.length + 1;
                return (
                  
                    
                    {colored(line, budget)}
                  
                );
              })}
              {lines.length > 1000 && (
                
                  Preview limited to 1,000 lines; Copy includes the bounded display value.
                
              )}
            
)} {data && images(data.value).map((image) => ( Captured raster tool output ))}
)}
); } export function Metadata({ rows }: { rows: [string, string | undefined][] }) { return (
{rows.map(([key, value]) => (
{key}
{value || "unavailable"}
))}
); }