import { useId, useMemo, useState, type ComponentProps } from "react"; import { ChevronRight } from "lucide-react"; import { useTranslation } from "react-i18next"; import { SubagentTaskButton, useSubagentTasks } from "@/components/thread/SubagentTasks"; import { ThreadMessages } from "@/components/thread/ThreadMessages"; import { isActiveSubagentTask, type ObservedSubagentTask } from "@/lib/subagent-tasks"; import type { UIMessage } from "@/lib/types"; /** Exact initiating prompts win; replayed IDs fall back to the turn's first user message. */ function groupResults(messages: UIMessage[], tasks: ObservedSubagentTask[]) { const promptsById = new Map(); const firstPromptByTurn = new Map(); for (const message of messages) { if (message.role !== "user") continue; promptsById.set(message.id, message); if (message.turnId && !firstPromptByTurn.has(message.turnId)) { firstPromptByTurn.set(message.turnId, message); } } const byMessage = new Map(); for (const task of tasks) { if (isActiveSubagentTask(task)) continue; const prompt = (task.origin_message_id ? promptsById.get(task.origin_message_id) : undefined) ?? (task.origin_turn_id ? firstPromptByTurn.get(task.origin_turn_id) : undefined); if (!prompt) continue; const group = byMessage.get(prompt.id) ?? []; group.push(task); byMessage.set(prompt.id, group); } return byMessage; } export function SubagentThreadMessages(props: ComponentProps) { const context = useSubagentTasks(); const sessionKey = context?.sessionKey; const groups = useMemo(() => groupResults(props.messages, context?.tasks ?? []), [props.messages, context?.tasks]); const [archive, setArchive] = useState(() => ({ sessionKey, expanded: new Set() })); // Reset only the disclosures; the outgoing transcript must not remount during handoff. if (archive.sessionKey !== sessionKey) { setArchive({ sessionKey, expanded: new Set() }); } const expanded = (results: ObservedSubagentTask[]) => results.some(task => archive.expanded.has(task.task_id)); const toggle = (results: ObservedSubagentTask[]) => { props.onActivityToggle?.(); setArchive(current => { const next = new Set(current.expanded); const close = results.some(task => current.expanded.has(task.task_id)); for (const task of results) { if (close) next.delete(task.task_id); else next.add(task.task_id); } return { sessionKey, expanded: next }; }); }; return { const results = groups.get(message.id); return <> {props.afterUserMessage?.(message)} {results ? toggle(results)} /> : null} ; }} />; } function SubagentResults({ tasks, expanded, onToggle }: { tasks: ObservedSubagentTask[]; expanded: boolean; onToggle: () => void; }) { const { t } = useTranslation("common"); const rowsId = useId(); const completed = tasks.filter((task) => task.state === "done").length; const attention = tasks.filter((task) => task.state === "error" || task.state === "incomplete" || task.state === "interrupted").length; const title = t("thread.subagents.workTitle"); const summary = completed === tasks.length ? t("thread.subagents.completed", { count: completed }) : t("thread.subagents.finished", { count: tasks.length }); return
; }