Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkflowOpeningBlock.tsx

242 lines8.9 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 isTerminalSubagentStatus,
4 workflowOpeningHeadline,
5 type RuntimeSubagent,
6 type WorkflowOpening,
7} from "@t3tools/client-runtime/state/subagentRuntime";
8import type {
9 EnvironmentId,
10 ProviderDriverKind,
11 ProviderInstanceId,
12 ServerProvider,
13} from "@t3tools/contracts";
14import { ChevronRightIcon, WorkflowIcon } from "lucide-react";
15import { useState } from "react";
16​
17import { cn } from "../../lib/utils";
18import { providersIgnoringCheckedAtAtom } from "../../state/server";
19import ChatMarkdown from "../ChatMarkdown";
20import { ThreadHoverCardPopup } from "../ThreadHoverCard";
21import { Button } from "../ui/button";
22import { Tooltip, TooltipTrigger } from "../ui/tooltip";
23import { AgentElapsed } from "./AgentElapsed";
24import { findRevealRef } from "./markdownFindContext";
25import { shouldCollapseUserMessage } from "./MessagesTimeline.logic";
26import { SubagentTooltipContent } from "./SubagentTooltipContent";
27import { EnvironmentSubagentAvatar } from "./V2LifecycleRow";
28​
29const FOLDED_SCRIPT_MASK = "linear-gradient(to bottom, black calc(100% - 1.75rem), transparent)";
30​
31/** The script as a fenced block, so it renders like any other code in the thread. */
32function scriptMarkdown(script: string) {
33 const longestRun = Math.max(2, ...(script.match(/`+/g) ?? []).map((run) => run.length));
34 const fence = "`".repeat(longestRun + 1);
35 return `${fence}javascript\n${script.trimEnd()}\n${fence}`;
37​
38/** Opens a workflow coordinator's thread in place of its script: the plan, its agents, then the script. */
39export function WorkflowOpeningBlock(props: {
40 readonly opening: WorkflowOpening;
41 readonly environmentId: EnvironmentId;
42 readonly onOpenThread: (threadId: string) => void;
43}) {
44 const { opening } = props;
45 const providers = useAtomValue(providersIgnoringCheckedAtAtom(props.environmentId));
46 const headline = workflowOpeningHeadline(opening);
47 const phases = opening.group?.phases.filter((phase) => phase.members.length > 0) ?? [];
48 const memberRow = (member: RuntimeSubagent) =>
49 opening.providerInstanceId === null ? null : (
50 <WorkflowMemberRow
51 key={member.id}
52 member={member}
53 environmentId={props.environmentId}
54 providerInstanceId={opening.providerInstanceId}
55 providers={providers}
56 driver={opening.driver ?? undefined}
57 onOpenThread={props.onOpenThread}
58 />
59 );
60 return (
61 <section
62 aria-label={`Workflow started: ${headline.title}`}
63 className="flex min-w-0 flex-col gap-3 px-1 py-0.5"
64 data-workflow-opening
65 >
66 <header className="flex min-w-0 items-start gap-2">
67 <WorkflowIcon aria-hidden className="mt-0.5 size-3.5 shrink-0 text-muted-foreground" />
68 <div className="flex min-w-0 flex-1 flex-col gap-0.5">
69 <p className="text-2xs font-medium text-muted-foreground">Workflow started</p>
70 <h3 className="truncate text-sm font-medium text-foreground" data-thread-find-text="true">
71 {headline.title}
72 </h3>
73 {headline.description ? (
74 <p className="text-xs text-muted-foreground" data-thread-find-text="true">
75 {headline.description}
76 </p>
77 ) : null}
78 </div>
79 {headline.agents ? (
80 <span aria-live="polite" className="shrink-0 text-2xs tabular-nums text-muted-foreground">
81 {headline.agents}
82 </span>
83 ) : null}
84 </header>
85 {phases.map((phase) => (
86 <section key={phase.index} aria-label={`${phase.title} phase`}>
87 <h4 className="flex items-baseline gap-2 px-2 pb-0.5 text-2xs font-medium text-muted-foreground">
88 <span className="min-w-0 truncate" data-thread-find-text="true">
89 {phase.title}
90 </span>
91 <span className="shrink-0 font-normal tabular-nums">
92 {phase.settledCount}/{phase.members.length}
93 </span>
94 </h4>
95 <ul className="m-0 list-none p-0">{phase.members.map(memberRow)}</ul>
96 </section>
97 ))}
98 {opening.group && opening.group.unphasedMembers.length > 0 ? (
99 <ul className="m-0 list-none p-0">{opening.group.unphasedMembers.map(memberRow)}</ul>
100 ) : null}
101 <WorkflowScript script={opening.script} />
102 </section>
103 );
105​
106/** One member agent, drawn like the timeline's subagent rows; it opens the member's thread. */
107function WorkflowMemberRow(props: {
108 readonly member: RuntimeSubagent;
109 readonly environmentId: EnvironmentId;
110 readonly providerInstanceId: ProviderInstanceId;
111 readonly providers: ReadonlyArray<ServerProvider>;
112 readonly driver: ProviderDriverKind | undefined;
113 readonly onOpenThread: (threadId: string) => void;
114}) {
115 const { member } = props;
116 const threadId = member.childThreadId;
117 const failed = member.status === "failed";
118 const detail = (
119 isTerminalSubagentStatus(member.status)
120 ? (member.error ?? member.result ?? member.progress)
121 : member.progress
122 )
123 ?.replace(/`/g, "")
124 .replace(/\s+/g, " ")
125 .trim();
126 const className =
127 "group/subagent flex w-full min-w-0 items-center gap-2.5 rounded-md px-2 py-1.5 text-left";
128 return (
129 <li>
130 <Tooltip>
131 <TooltipTrigger
132 delay={200}
133 render={
134 threadId === null ? (
135 <div aria-description={member.status} className={className} />
136 ) : (
137 <button
138 type="button"
139 aria-label={`Open ${member.title}`}
140 aria-description={member.status}
141 onClick={() => props.onOpenThread(threadId)}
142 className={cn(
143 className,
144 "cursor-pointer transition-colors hover:bg-accent/20 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70",
145 )}
146 />
147 )
148 }
149 >
150 <EnvironmentSubagentAvatar
151 environmentId={props.environmentId}
152 providerInstanceId={props.providerInstanceId}
153 driver={props.driver}
154 status={member.status}
155 />
156 <span className="min-w-0 flex-1">
157 <span className="block truncate text-xs font-medium text-foreground">
158 {member.title}
159 </span>
160 {detail ? (
161 <span
162 className={cn(
163 "block truncate text-2xs leading-relaxed",
164 failed ? "text-destructive" : "text-muted-foreground",
165 )}
166 >
167 {detail}
168 </span>
169 ) : null}
170 </span>
171 {member.startedAt ? (
172 <span className="shrink-0 text-xs text-muted-foreground tabular-nums">
173 <AgentElapsed agent={member} />
174 </span>
175 ) : null}
176 {threadId !== null ? (
177 <ChevronRightIcon
178 aria-hidden
179 className="size-3.5 shrink-0 text-muted-foreground/60 transition-colors group-hover/subagent:text-foreground"
180 />
181 ) : null}
182 </TooltipTrigger>
183 <ThreadHoverCardPopup>
184 <SubagentTooltipContent
185 title={member.title}
186 model={member.model}
187 providerInstanceId={props.providerInstanceId}
188 origin="provider_native"
189 provider={props.providers.find(
190 (provider) => provider.instanceId === props.providerInstanceId,
191 )}
192 providers={props.providers}
193 driver={props.driver}
194 elapsed={<AgentElapsed agent={member} />}
195 status={member.status}
196 result={member.result ?? member.error}
197 progress={member.progress}
198 />
199 </ThreadHoverCardPopup>
200 </Tooltip>
201 </li>
202 );
204​
205/** The script as highlighted code, folded like a long message until opened or found. */
206function WorkflowScript(props: { readonly script: string }) {
207 const [expanded, setExpanded] = useState(false);
208 const revealRef = findRevealRef(() => setExpanded(true));
209 const canFold = shouldCollapseUserMessage(props.script);
210 const folded = canFold && !expanded;
211 return (
212 <div>
213 <div
214 ref={revealRef}
215 className={cn("relative", folded && "max-h-44 overflow-hidden")}
216 data-thread-find-text="true"
217 data-thread-find-fold={folded ? "" : undefined}
218 style={
219 folded
220 ? { WebkitMaskImage: FOLDED_SCRIPT_MASK, maskImage: FOLDED_SCRIPT_MASK }
221 : undefined
222 }
223 >
224 <ChatMarkdown text={scriptMarkdown(props.script)} cwd={undefined} />
225 </div>
226 {canFold ? (
227 <div className="-ms-1">
228 <Button
229 type="button"
230 size="xs"
231 variant="ghost-muted"
232 aria-expanded={expanded}
233 data-scroll-anchor-ignore
234 onClick={() => setExpanded((value) => !value)}
235 >
236 {expanded ? "Show less" : "Show full script"}
237 </Button>
238 </div>
239 ) : null}
240 </div>
241 );