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

WorkflowOpeningBlock.tsx

238 lines8.5 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​
31function scriptMarkdown(script: string) {
32 const longestRun = Math.max(2, ...(script.match(/`+/g) ?? []).map((run) => run.length));
33 const fence = "`".repeat(longestRun + 1);
34 return `${fence}javascript\n${script.trimEnd()}\n${fence}`;
36​
37export function WorkflowOpeningBlock(props: {
38 readonly opening: WorkflowOpening;
39 readonly environmentId: EnvironmentId;
40 readonly onOpenThread: (threadId: string) => void;
41}) {
42 const { opening } = props;
43 const providers = useAtomValue(providersIgnoringCheckedAtAtom(props.environmentId));
44 const headline = workflowOpeningHeadline(opening);
45 const phases = opening.group?.phases.filter((phase) => phase.members.length > 0) ?? [];
46 const memberRow = (member: RuntimeSubagent) =>
47 opening.providerInstanceId === null ? null : (
48 <WorkflowMemberRow
49 key={member.id}
50 member={member}
51 environmentId={props.environmentId}
52 providerInstanceId={opening.providerInstanceId}
53 providers={providers}
54 driver={opening.driver ?? undefined}
55 onOpenThread={props.onOpenThread}
56 />
57 );
58 return (
59 <section
60 aria-label={`Workflow started: ${headline.title}`}
61 className="flex min-w-0 flex-col gap-3 px-1 py-0.5"
62 data-workflow-opening
63 >
64 <header className="flex min-w-0 items-start gap-2">
65 <WorkflowIcon aria-hidden className="mt-0.5 size-3.5 shrink-0 text-muted-foreground" />
66 <div className="flex min-w-0 flex-1 flex-col gap-0.5">
67 <p className="text-2xs font-medium text-muted-foreground">Workflow started</p>
68 <h3 className="truncate text-sm font-medium text-foreground" data-thread-find-text="true">
69 {headline.title}
70 </h3>
71 {headline.description ? (
72 <p className="text-xs text-muted-foreground" data-thread-find-text="true">
73 {headline.description}
74 </p>
75 ) : null}
76 </div>
77 {headline.agents ? (
78 <span aria-live="polite" className="shrink-0 text-2xs tabular-nums text-muted-foreground">
79 {headline.agents}
80 </span>
81 ) : null}
82 </header>
83 {phases.map((phase) => (
84 <section key={phase.index} aria-label={`${phase.title} phase`}>
85 <h4 className="flex items-baseline gap-2 px-2 pb-0.5 text-2xs font-medium text-muted-foreground">
86 <span className="min-w-0 truncate" data-thread-find-text="true">
87 {phase.title}
88 </span>
89 <span className="shrink-0 font-normal tabular-nums">
90 {phase.settledCount}/{phase.members.length}
91 </span>
92 </h4>
93 <ul className="m-0 list-none p-0">{phase.members.map(memberRow)}</ul>
94 </section>
95 ))}
96 {opening.group && opening.group.unphasedMembers.length > 0 ? (
97 <ul className="m-0 list-none p-0">{opening.group.unphasedMembers.map(memberRow)}</ul>
98 ) : null}
99 <WorkflowScript script={opening.script} />
100 </section>
101 );
103​
104function WorkflowMemberRow(props: {
105 readonly member: RuntimeSubagent;
106 readonly environmentId: EnvironmentId;
107 readonly providerInstanceId: ProviderInstanceId;
108 readonly providers: ReadonlyArray<ServerProvider>;
109 readonly driver: ProviderDriverKind | undefined;
110 readonly onOpenThread: (threadId: string) => void;
111}) {
112 const { member } = props;
113 const threadId = member.childThreadId;
114 const failed = member.status === "failed";
115 const detail = (
116 isTerminalSubagentStatus(member.status)
117 ? (member.error ?? member.result ?? member.progress)
118 : member.progress
119 )
120 ?.replace(/`/g, "")
121 .replace(/\s+/g, " ")
122 .trim();
123 const className =
124 "group/subagent flex w-full min-w-0 items-center gap-2.5 rounded-md px-2 py-1.5 text-left";
125 return (
126 <li>
127 <Tooltip>
128 <TooltipTrigger
129 delay={200}
130 render={
131 threadId === null ? (
132 <div aria-description={member.status} className={className} />
133 ) : (
134 <button
135 type="button"
136 aria-label={`Open ${member.title}`}
137 aria-description={member.status}
138 onClick={() => props.onOpenThread(threadId)}
139 className={cn(
140 className,
141 "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",
142 )}
143 />
144 )
145 }
146 >
147 <EnvironmentSubagentAvatar
148 environmentId={props.environmentId}
149 providerInstanceId={props.providerInstanceId}
150 driver={props.driver}
151 status={member.status}
152 />
153 <span className="min-w-0 flex-1">
154 <span className="block truncate text-xs font-medium text-foreground">
155 {member.title}
156 </span>
157 {detail ? (
158 <span
159 className={cn(
160 "block truncate text-2xs leading-relaxed",
161 failed ? "text-destructive" : "text-muted-foreground",
162 )}
163 >
164 {detail}
165 </span>
166 ) : null}
167 </span>
168 {member.startedAt ? (
169 <span className="shrink-0 text-xs text-muted-foreground tabular-nums">
170 <AgentElapsed agent={member} />
171 </span>
172 ) : null}
173 {threadId !== null ? (
174 <ChevronRightIcon
175 aria-hidden
176 className="size-3.5 shrink-0 text-muted-foreground/60 transition-colors group-hover/subagent:text-foreground"
177 />
178 ) : null}
179 </TooltipTrigger>
180 <ThreadHoverCardPopup>
181 <SubagentTooltipContent
182 title={member.title}
183 model={member.model}
184 providerInstanceId={props.providerInstanceId}
185 origin="provider_native"
186 provider={props.providers.find(
187 (provider) => provider.instanceId === props.providerInstanceId,
188 )}
189 providers={props.providers}
190 driver={props.driver}
191 elapsed={<AgentElapsed agent={member} />}
192 status={member.status}
193 result={member.result ?? member.error}
194 progress={member.progress}
195 />
196 </ThreadHoverCardPopup>
197 </Tooltip>
198 </li>
199 );
201​
202function WorkflowScript(props: { readonly script: string }) {
203 const [expanded, setExpanded] = useState(false);
204 const revealRef = findRevealRef(() => setExpanded(true));
205 const canFold = shouldCollapseUserMessage(props.script);
206 const folded = canFold && !expanded;
207 return (
208 <div>
209 <div
210 ref={revealRef}
211 className={cn("relative", folded && "max-h-44 overflow-hidden")}
212 data-thread-find-text="true"
213 data-thread-find-fold={folded ? "" : undefined}
214 style={
215 folded
216 ? { WebkitMaskImage: FOLDED_SCRIPT_MASK, maskImage: FOLDED_SCRIPT_MASK }
217 : undefined
218 }
219 >
220 <ChatMarkdown text={scriptMarkdown(props.script)} cwd={undefined} />
221 </div>
222 {canFold ? (
223 <div className="-ms-1">
224 <Button
225 type="button"
226 size="xs"
227 variant="ghost-muted"
228 aria-expanded={expanded}
229 data-scroll-anchor-ignore
230 onClick={() => setExpanded((value) => !value)}
231 >
232 {expanded ? "Show less" : "Show full script"}
233 </Button>
234 </div>
235 ) : null}
236 </div>
237 );