"use client"; import { ChevronDown } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { type ReactNode, useId, useLayoutEffect, useRef, useState } from "react"; import { AgentDisclosure } from "@/components/agents/agent-disclosure"; import { ThinkingShimmer } from "@/components/agents/loading-states/thinking-shimmer"; import { EASE_OUT, SPRING_LAYOUT, SPRING_SWAP, } from "@/lib/ease"; import { useStatusOpen } from "@/lib/hooks/use-status-open"; import { useMessages } from "@/lib/i18n"; import { cn } from "@/lib/utils"; import { ActivityRow } from "./activity-row"; import { type AgentActivityTranslate, agentActivityMessages, } from "./messages"; import type { AgentActivityContentType, AgentActivityItem, AgentActivityProps, } from "./types"; export { type AgentActivityMessages, agentActivityMessages, } from "./messages"; export type { AgentActivityContentType, AgentActivityItem, AgentActivityProps, AgentActivitySearch, AgentActivityStatus, AgentActivityStep, AgentActivityText, AgentActivityTool, AgentActivityTrace, AgentSearchResult, AgentStepStatus, AgentTraceKind, } from "./types"; function formatDuration(duration: number, t: AgentActivityTranslate) { const seconds = Math.max(0, Math.round(duration)); if (seconds < 60) return t("seconds", { seconds }); const minutes = Math.floor(seconds / 60); const remainder = seconds % 60; return remainder === 0 ? t("minutes", { minutes }) : t("minutesSeconds", { minutes, seconds: remainder }); } function getContentType(items: AgentActivityItem[]): AgentActivityContentType { const first = items[0]?.type; return first && items.every((item) => item.type === first) ? first : "mixed"; } function getActiveLabel( type: AgentActivityContentType, t: AgentActivityTranslate, ) { if (type === "search") return t("searching"); if (type === "tool") return t("runningTools"); if (type === "trace") return t("workingTrace"); if (type === "mixed") return t("workingMixed"); return t("thinking"); } function getSummary( type: AgentActivityContentType, items: AgentActivityItem[], duration: number, t: AgentActivityTranslate, ): ReactNode { const count = items.length; if (type === "step" || type === "text") { // Raw template, split around the placeholder so the duration keeps its tabular figures. const [before, after = ""] = t("thoughtFor").split("{duration}"); return ( <> {before} {formatDuration(duration, t)} {after} ); } if (type === "search") return t("searched"); if (type === "tool") { return t(count === 1 ? "ranToolsOne" : "ranToolsOther", { count }); } if (type === "trace") { const messages = items.filter( (item) => item.type === "trace" && (item.kind === "thinking" || item.kind === "message"), ).length; const tools = count - messages; return t("traceSummary", { toolCalls: t(tools === 1 ? "toolCallsOne" : "toolCallsOther", { count: tools, }), messages: t(messages === 1 ? "messagesOne" : "messagesOther", { count: messages, }), }); } return t(count === 1 ? "completedStepsOne" : "completedStepsOther", { count, }); } export function AgentActivity({ items, contentType: initialContentType, status = "working", duration = 0, open, defaultOpen = false, onOpenChange, collapseOnComplete = true, activeLabel, summary, renderWorkingStatus, renderCompletedStatus, maxHeight = 208, labels, className, contentClassName, }: AgentActivityProps) { const reduce = useReducedMotion() ?? false; const t = useMessages("agentActivity", agentActivityMessages, labels); const baseId = useId(); const triggerId = `${baseId}-trigger`; const contentId = `${baseId}-content`; const contentRef = useRef(null); const viewportRef = useRef(null); const [contentHeight, setContentHeight] = useState(0); // When the work completes, the panel stays open or folds as asked. const [currentOpen, setOpen] = useStatusOpen({ status, open, defaultOpen, onOpenChange, decide: (previous, next) => previous === "working" && next === "complete" ? !collapseOnComplete : undefined, }); const working = status === "working"; const expanded = working || currentOpen; const contentType = items.length ? getContentType(items) : (initialContentType ?? "mixed"); const cappedHeight = Math.min(contentHeight, Math.max(0, maxHeight)); const viewportHeight = working ? Math.max(0, maxHeight) : cappedHeight; const capped = contentHeight > maxHeight; const streamOffset = working ? Math.min(0, viewportHeight - contentHeight) : 0; useLayoutEffect(() => { const node = contentRef.current; if (!node) return; const measure = () => setContentHeight(node.offsetHeight); measure(); if (typeof ResizeObserver === "undefined") return; const observer = new ResizeObserver(measure); observer.observe(node); return () => observer.disconnect(); }, []); const toggle = () => { const next = !currentOpen; setOpen(next); if (next) requestAnimationFrame(() => viewportRef.current?.scrollTo({ top: 0 })); }; const liveLabel = activeLabel ?? getActiveLabel(contentType, t); const completedSummary = summary ?? getSummary(contentType, items, duration, t); const maskImage = capped ? working ? "linear-gradient(to bottom, transparent, black 12px)" : "linear-gradient(to bottom, transparent, black 12px, black calc(100% - 12px), transparent)" : undefined; return (
{working ? (
{renderWorkingStatus ? renderWorkingStatus({ label: liveLabel, duration }) : {liveLabel}}
) : ( )}
{items.map((item) => ( ))}
); }