import React, { useState } from "react"; import { LLMContent } from "../types"; interface SubagentToolProps { // For tool_use (pending state) toolInput?: unknown; // { slug: string, prompt: string, timeout_seconds?: number, wait?: boolean } isRunning?: boolean; // For tool_result (completed state) toolResult?: LLMContent[]; hasError?: boolean; executionTime?: string; displayData?: { slug?: string; conversation_id?: string }; } function SubagentTool({ toolInput, isRunning, toolResult, hasError, executionTime, displayData, }: SubagentToolProps) { const [isExpanded, setIsExpanded] = useState(false); // Extract fields from toolInput const input = typeof toolInput === "object" && toolInput !== null ? (toolInput as { slug?: string; prompt?: string; timeout_seconds?: number; wait?: boolean }) : {}; const slug = input.slug || displayData?.slug || "subagent"; const prompt = input.prompt || ""; const wait = input.wait !== false; const timeout = input.timeout_seconds || 60; // Extract result text const resultText = toolResult ?.filter((r) => r.Type === 2) // ContentTypeText .map((r) => r.Text) .join("\n") || ""; // Truncate prompt for display const truncateText = (text: string, maxLen: number = 60) => { if (!text) return ""; const firstLine = text.split("\n")[0]; if (firstLine.length <= maxLen) return firstLine; return firstLine.substring(0, maxLen) + "..."; }; const displayPrompt = truncateText(prompt); const isComplete = !isRunning && toolResult !== undefined; return (
setIsExpanded(!isExpanded)}>
subagent {isComplete && hasError && } {isComplete && !hasError && } Subagent '{slug}' {isRunning ? (wait ? "running..." : "started") : ""} {displayPrompt && !isRunning && ` ${displayPrompt}`}
{isExpanded && (
Prompt to '{slug}': {!wait && fire-and-forget} {timeout !== 60 && timeout: {timeout}s}
{prompt || "(no prompt)"}
{isComplete && (
Response: {executionTime && {executionTime}}
{resultText || "(no response)"}
)} {displayData?.conversation_id && (
Conversation:
{ e.preventDefault(); // Navigate to the subagent conversation window.history.pushState({}, "", `/c/${slug}`); window.dispatchEvent(new PopStateEvent("popstate")); }} style={{ color: "var(--link-color)", textDecoration: "underline" }} > View subagent conversation →
)}
)}
); } export default SubagentTool;