Open History to see the last commit
1import type { OrchestrationV2PlanStep } from "@t3tools/contracts";
2import { CheckIcon, ChevronDownIcon, CircleDotIcon, CircleIcon } from "lucide-react";
3import { msg, useGT, useMessages, type Msg } from "gt-react";
4import { useState } from "react";
5
6import { cn } from "../../lib/utils";
7import { formatDuration } from "@t3tools/shared/orchestrationTimingCopy";
8import { type TaskListPlan } from "../../session-logic";
9import { Button } from "../ui/button";
10import { ThreadDetailsSection } from "./ThreadDetailsSection";
11
12const STATUS_LABEL = {
13 pending: msg({ context: "task step that has not started" })`Pending`,
14 running: msg("Running"),
15 completed: msg("Completed"),
16} satisfies Record<OrchestrationV2PlanStep["status"], string | Msg>;
17
18/** The agent's latest task list; collapsed, or in a short card, it keeps only the current step. */
19export function ThreadDetailsTasks({
20 plan,
21 compact,
22}: {
23 readonly plan: TaskListPlan;
24 readonly compact: boolean;
25}) {
26 const gt = useGT();
27 const m = useMessages();
28 const [expanded, setExpanded] = useState(false);
29 const done = plan.steps.filter((step) => step.status === "completed").length;
30 const current =
31 plan.steps.find((step) => step.status === "running") ??
32 plan.steps.find((step) => step.status === "pending");
33 const count = (
34 <span
35 className={cn(
36 "text-2xs font-medium tabular-nums",
37 done === plan.steps.length ? "text-success-foreground" : "text-muted-foreground",
38 )}
39 >
40 {done}/{plan.steps.length}
41 </span>
42 );
43
44 if (compact) {
45 return (
46 <ThreadDetailsSection
47 headingId="thread-details-tasks-heading"
48 title={gt`Tasks`}
49 showHeading={false}
50 >
51 <div className="flex h-6 min-w-0 items-center gap-2 px-2.5 text-xs">
52 <span className="shrink-0 text-muted-foreground">{gt`Tasks`}</span>
53 <span className="min-w-0 flex-1 truncate text-foreground/80">
54 {current?.text ?? gt`All done`}
55 </span>
56 {count}
57 </div>
58 </ThreadDetailsSection>
59 );
60 }
61
62 return (
63 <ThreadDetailsSection
64 headingId="thread-details-tasks-heading"
65 title={gt`Tasks`}
66 actions={
67 <Button
68 variant="ghost-muted"
69 size="micro"
70 aria-expanded={expanded}
71 aria-label={
72 expanded
73 ? gt`Collapse tasks, ${{ done }} of ${{ total: plan.steps.length }} completed`
74 : gt`Expand tasks, ${{ done }} of ${{ total: plan.steps.length }} completed`
75 }
76 onClick={() => setExpanded((value) => !value)}
77 >
78 {count}
79 <ChevronDownIcon
80 aria-hidden
81 className={cn("size-3 shrink-0 transition-transform", !expanded && "-rotate-90")}
82 />
83 </Button>
84 }
85 >
86 <ul data-thread-details-task-list className="flex max-h-48 flex-col overflow-y-auto text-xs">
87 {!expanded && !current ? (
88 <li className="px-2.5 py-1 text-muted-foreground/60">{gt`All done`}</li>
89 ) : null}
90 {(expanded ? plan.steps : current ? [current] : []).map((step) => (
91 <li key={step.id} className="flex min-w-0 items-start gap-2 px-2.5 py-1">
92 <span
93 aria-hidden
94 className={cn(
95 "flex h-4 shrink-0 items-center [&>svg]:size-3.5",
96 step.status === "completed"
97 ? "text-success"
98 : step.status === "running"
99 ? "text-primary"
100 : "text-muted-foreground/40",
101 )}
102 >
103 {step.status === "completed" ? (
104 <CheckIcon />
105 ) : step.status === "running" ? (
106 <CircleDotIcon />
107 ) : (
108 <CircleIcon />
109 )}
110 </span>
111 <span
112 className={cn(
113 "min-w-0 flex-1 wrap-anywhere",
114 step.status === "completed"
115 ? "text-muted-foreground/60"
116 : step.status === "running"
117 ? "text-foreground/90"
118 : "text-muted-foreground",
119 )}
120 >
121 <span className="sr-only">{m(STATUS_LABEL[step.status])}: </span>
122 {step.text}
123 </span>
124 {step.durationMs !== undefined ? (
125 <span className="shrink-0 text-3xs/4 text-muted-foreground/50 tabular-nums">
126 {formatDuration(step.durationMs, m)}
127 </span>
128 ) : null}
129 </li>
130 ))}
131 </ul>
132 </ThreadDetailsSection>
133 );
134}