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

V2LifecycleRow.tsx

790 lines27.5 KB

1import { T, Var, msg, useGT, useMessages, type Msg } from "gt-react";
2import { runStopReasonLabel } from "@t3tools/client-runtime/contract-labels";
3import { ThreadHoverCardPopup } from "../ThreadHoverCard";
4import { AgentElapsed } from "./AgentElapsed";
5import { projectedSubagentsToRuntime } from "@t3tools/client-runtime/state/subagentRuntime";
6import { use, type ReactNode } from "react";
7import { useThreadShell, useProject } from "../../state/entities";
8import type { SideThreadOpenIntent } from "@t3tools/client-runtime/side-thread";
9import { SideThreadsOpenContext, useSideThreadTrigger } from "../../hooks/useSideThreadTrigger";
10import { useLinkedThreadOpener } from "../../state/linkedThreadView";
11import { SubagentTooltipContent } from "./SubagentTooltipContent";
12import { useAtomValue } from "@effect/atom-react";
13import { scopeThreadRef, scopeProjectRef } from "@t3tools/client-runtime/environment";
14import { environmentThreadDetails } from "../../state/threads";
15import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server";
16import { MiddleTruncate } from "../ui/middle-truncate";
17import * as DateTime from "effect/DateTime";
18import { WorkLogRow } from "./WorkLog";
19import {
20 resolveHandoffEndpoints,
21 type HandoffEndpoints,
22 type HandoffTimelineRun,
23 resolveModelSwitchChange,
24} from "@t3tools/client-runtime/handoff";
25import {
26 formatSubagentDisplayTitle,
27 subagentCardDetail,
28} from "@t3tools/client-runtime/state/subagent-display";
29import {
30 ProviderDriverKind,
31 type ModelSelection,
32 type OrchestrationV2ModelSwitch,
33 type OrchestrationV2Notification,
34 type OrchestrationV2TurnItem,
35 type OrchestrationV2RemoteTaskChild,
36 type OrchestrationV2Subagent,
37 type ProviderInstanceId,
38 type ServerProvider,
39 type ThreadId,
40 type EnvironmentId,
41 type NodeId,
42 type ScopedThreadRef,
43} from "@t3tools/contracts";
44import type { TimestampFormat } from "@t3tools/contracts/settings";
45import {
46 BotIcon,
47 ChevronRightIcon,
48 CircleStopIcon,
49 ArrowRightLeftIcon,
50 ArrowRightIcon,
51 EraserIcon,
52 GitForkIcon,
53 MessageSquareIcon,
54} from "lucide-react";
55​
56import { Tooltip, TooltipTrigger } from "../ui/tooltip";
57import { SharedTooltipTrigger } from "../ui/shared-tooltip";
58import {
59 applyProviderInstanceSettings,
60 brandProviderInstanceEntries,
61 deriveProviderInstanceEntries,
62 hasInstanceBadge,
63 shouldShowInstanceBadge,
64 type ProviderInstanceEntry,
65} from "../../providerInstances";
66import { formatShortTimestamp } from "../../timestampFormat";
67import { getTriggerDisplayModelName } from "./providerIconUtils";
68import { ProviderInstanceIcon, providerTextColor } from "./ProviderInstanceIcon";
69import { cn } from "~/lib/utils";
70import { TimelineSystemDivider } from "./TimelineSystemDivider";
71import { subagentResumedSince } from "./V2LifecycleRow.logic";
72import { Button, InlineButton } from "../ui/button";
73import { T3Wordmark } from "../T3Wordmark";
74​
75export type { HandoffTimelineRun } from "@t3tools/client-runtime/handoff";
76​
77export function V2LifecycleRow(props: {
78 readonly item: OrchestrationV2TurnItem;
79 readonly environmentId: EnvironmentId;
80 readonly resourceSummary?: boolean | undefined;
81 readonly runs: ReadonlyArray<HandoffTimelineRun>;
82 readonly onRetryWorkspacePreparation?: (() => void) | undefined;
83 readonly onOpenThread: (
84 threadId: ThreadId,
85 environmentId?: EnvironmentId,
86 intent?: SideThreadOpenIntent,
87 ) => void;
88}) {
89 const { item } = props;
90 const gt = useGT();
91 const m = useMessages();
92 if (item.type === "run_interrupt_result") {
93 return (
94 <>
95 <TimelineSystemDivider
96 label={gt`Run interrupted`}
97 detail={runStopReasonLabel(item.message, m)}
98 icon={CircleStopIcon}
99 wrapDetail
100 />
101 {props.onRetryWorkspacePreparation ? (
102 <div className="flex justify-center pb-2">
103 <Button size="xs" variant="outline" onClick={props.onRetryWorkspacePreparation}>
104 <T>Retry</T>
105 </Button>
106 </div>
107 ) : null}
108 </>
109 );
110 }
111 if (item.type === "handoff") {
112 return (
113 <EnvironmentHandoffRow
114 environmentId={props.environmentId}
115 endpoints={resolveHandoffEndpoints(item, props.runs)}
116 failed={item.status === "failed"}
117 />
118 );
119 }
120 if (item.type === "fork") {
121 const relatedThreadId = item.source.type === "run" ? item.source.threadId : item.targetThreadId;
122 return (
123 <TimelineSystemDivider
124 label={item.source.type === "run" ? gt`Forked from conversation` : gt`Conversation fork`}
125 icon={GitForkIcon}
126 actionLabel={item.source.type === "run" ? gt`Open source conversation` : gt`Open fork`}
127 onAction={() => props.onOpenThread(relatedThreadId)}
128 />
129 );
130 }
131 if (item.type === "thread_created") {
132 if (props.resourceSummary) {
133 return (
134 <div
135 data-v2-item-type={item.type}
136 className="flex min-w-0 items-center gap-3 rounded-lg border border-border/60 p-3"
137 >
138 <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-muted/60">
139 <MessageSquareIcon className="size-4 text-secondary-label" aria-hidden />
140 </span>
141 <span className="min-w-0 flex-1 truncate text-sm font-medium">
142 {item.title ?? gt`Created thread`}
143 </span>
144 <Button
145 size="xs"
146 variant="outline"
147 aria-label={
148 item.title != null ? gt`Open ${{ title: item.title }}` : gt`Open created thread`
149 }
150 onClick={() => props.onOpenThread(item.targetThreadId)}
151 >
152 <T>Open chat</T>
153 </Button>
154 </div>
155 );
156 }
157 return (
158 <WorkLogRow
159 data-v2-item-type={item.type}
160 icon={<T3Wordmark className="size-4 text-icon-muted" aria-hidden />}
161 label={
162 item.title ? (
163 <T>
164 Created thread · <Var>{item.title}</Var>
165 </T>
166 ) : (
167 <T>Created thread</T>
168 )
169 }
170 trailing={
171 <InlineButton
172 aria-label={
173 item.title != null ? gt`Open ${{ title: item.title }}` : gt`Open created thread`
174 }
175 onClick={() => props.onOpenThread(item.targetThreadId)}
176 >
177 <T>Open chat</T>
178 </InlineButton>
179 }
180 />
181 );
182 }
183 if (item.type === "subagent") {
184 return (
185 <SubagentTimelineLink
186 parentRef={scopeThreadRef(props.environmentId, item.threadId)}
187 subagentId={item.subagentId}
188 providerInstanceId={item.providerInstanceId}
189 origin={item.origin}
190 status={item.status}
191 driver={item.driver}
192 title={formatSubagentDisplayTitle(item.title ?? gt`Subagent`)}
193 result={item.result}
194 progress={item.progress}
195 startedAt={item.startedAt}
196 completedAt={item.completedAt}
197 threadId={item.childThreadId}
198 remote={item.remoteChild}
199 onOpenThread={props.onOpenThread}
200 />
201 );
202 }
203 return null;
205​
206const STATUS_VISUALS: Record<
207 OrchestrationV2TurnItem["status"],
208 { dotClass: string; label: string | Msg }
209> = {
210 pending: { dotClass: "bg-info", label: msg({ context: "subagent status" })`Queued` },
211 running: { dotClass: "bg-info", label: msg({ context: "subagent status" })`Running` },
212 waiting: { dotClass: "bg-info", label: msg({ context: "subagent status" })`Waiting` },
213 idle: {
214 dotClass: "bg-muted-foreground/50",
215 label: msg({ context: "subagent status" })`Idle · resumable`,
216 },
217 completed: { dotClass: "bg-success", label: msg({ context: "subagent status" })`Completed` },
218 failed: { dotClass: "bg-destructive", label: msg({ context: "subagent status" })`Failed` },
219 cancelled: {
220 dotClass: "bg-muted-foreground/60",
221 label: msg({ context: "subagent status" })`Stopped`,
222 },
223 interrupted: {
224 dotClass: "bg-muted-foreground/60",
225 label: msg({ context: "subagent status" })`Stopped`,
226 },
227};
228​
229function subagentStatusVisual(status: OrchestrationV2TurnItem["status"]) {
230 return STATUS_VISUALS[status];
232​
233const SETTLED_SUBAGENT_STATUSES = new Set<OrchestrationV2TurnItem["status"]>([
234 "completed",
235 "failed",
236 "cancelled",
237 "interrupted",
238]);
239​
240function isoOrNull(value: DateTime.Utc | null | undefined): string | null {
241 return value ? DateTime.formatIso(value) : null;
243​
244export function SubagentElapsed({ agent }: { agent: Parameters<typeof AgentElapsed>[0]["agent"] }) {
245 return <AgentElapsed agent={agent} />;
247​
248function SubagentAvatar({
249 driver,
250 displayName,
251 iconUrl,
252 accentColor,
253 showBadge,
254 anyInstanceBadge,
255 status,
256 className,
257}: {
258 driver?: ProviderDriverKind | undefined;
259 displayName?: string | undefined;
260 iconUrl?: string | undefined;
261 accentColor?: string | undefined;
262 showBadge: boolean;
263 anyInstanceBadge: boolean;
264 status?: OrchestrationV2TurnItem["status"] | undefined;
265 className?: string;
266}) {
267 return (
268 <span
269 aria-hidden
270 className={cn(
271 "relative inline-flex size-6 shrink-0 items-center justify-center rounded-full border border-border/70 bg-muted ring-2 ring-background",
272 className,
273 )}
274 >
275 {driver ? (
276 <ProviderInstanceIcon
277 driverKind={driver}
278 displayName={displayName ?? driver}
279 accentColor={accentColor}
280 acpRegistryIconUrl={iconUrl}
281 showBadge={showBadge}
282 size="compact"
283 className="z-auto"
284 iconClassName="size-3.5"
285 />
286 ) : (
287 <BotIcon className="size-3.5 text-muted-foreground" />
288 )}
289 {status ? (
290 <span
291 className={cn(
292 "absolute -right-px size-2 rounded-full ring-2 ring-background",
293 anyInstanceBadge ? "-top-px" : "-bottom-px",
294 subagentStatusVisual(status).dotClass,
295 )}
296 />
297 ) : null}
298 </span>
299 );
301​
302// Select strings; status pushes replace providers even when avatar fields are unchanged.
303export function EnvironmentSubagentAvatar({
304 environmentId,
305 providerInstanceId,
306 driver,
307 remote,
308 ...props
309}: {
310 environmentId: EnvironmentId;
311 providerInstanceId: ProviderInstanceId;
312 driver?: ProviderDriverKind | undefined;
313 /** A task in a linked environment runs a provider instance there; its driver here is only a label. */
314 remote?: OrchestrationV2RemoteTaskChild | undefined;
315 status?: OrchestrationV2TurnItem["status"] | undefined;
316}) {
317 const providerEnvironmentId = remote?.environmentId ?? environmentId;
318 const providersAtom = serverEnvironment.providersValueAtom(providerEnvironmentId);
319 const settings = useAtomValue(serverEnvironment.settingsValueAtom(providerEnvironmentId));
320 const entries = (providers: ReadonlyArray<ServerProvider> | null | undefined) => {
321 const derived = deriveProviderInstanceEntries(providers ?? []);
322 return settings ? brandProviderInstanceEntries(derived, settings) : derived;
323 };
324 const providerDriver = useAtomValue(
325 providersAtom,
326 (providers) =>
327 providers?.find((provider) => provider.instanceId === providerInstanceId)?.driver,
328 );
329 const displayName = useAtomValue(
330 providersAtom,
331 (providers) =>
332 entries(providers).find((entry) => entry.instanceId === providerInstanceId)?.displayName,
333 );
334 const accentColor = useAtomValue(
335 providersAtom,
336 (providers) =>
337 entries(providers).find((entry) => entry.instanceId === providerInstanceId)?.accentColor,
338 );
339 const showBadge = useAtomValue(providersAtom, (providers) => {
340 const all = entries(providers);
341 const entry = all.find((candidate) => candidate.instanceId === providerInstanceId);
342 return entry !== undefined && shouldShowInstanceBadge(entry, all);
343 });
344 const anyInstanceBadge = useAtomValue(providersAtom, (providers) =>
345 hasInstanceBadge(entries(providers)),
346 );
347 const iconUrl = useAtomValue(
348 providersAtom,
349 (providers) =>
350 providers?.find((provider) => provider.instanceId === providerInstanceId)?.iconUrl,
351 );
352 return (
353 <SubagentAvatar
354 {...props}
355 driver={providerDriver ?? (remote === undefined ? driver : undefined)}
356 displayName={displayName}
357 iconUrl={iconUrl}
358 accentColor={accentColor}
359 showBadge={showBadge}
360 anyInstanceBadge={anyInstanceBadge}
361 />
362 );
364​
365const NOTIFICATION_OUTCOME_STATUS: Record<
366 OrchestrationV2Notification["outcome"],
367 OrchestrationV2TurnItem["status"] | null
368> = {
369 completed: "completed",
370 failed: "failed",
371 cancelled: "cancelled",
372 updated: null,
373 unknown: null,
374};
375​
376const NOTIFICATION_OUTCOME_LABEL: Record<OrchestrationV2Notification["outcome"], string | Msg> = {
377 completed: msg({ context: "subagent task outcome" })`Finished`,
378 failed: msg({ context: "subagent task outcome" })`Failed`,
379 cancelled: msg({ context: "subagent task outcome" })`Stopped`,
380 updated: msg({ context: "subagent task outcome" })`Updated`,
381 unknown: msg({ context: "subagent task outcome" })`Finished`,
382};
383​
384export function SubagentNotificationLink(props: {
385 readonly parentRef: ScopedThreadRef;
386 readonly childThreadId: ThreadId;
387 readonly taskIds?: ReadonlyArray<NodeId> | undefined;
388 readonly outcome: OrchestrationV2Notification["outcome"];
389 readonly createdAt: string;
390 readonly timestampFormat: TimestampFormat;
391 readonly onOpenThread: (
392 threadId: ThreadId,
393 environmentId?: EnvironmentId,
394 intent?: SideThreadOpenIntent,
395 ) => void;
396 readonly fallback: ReactNode;
397}) {
398 const gt = useGT();
399 const m = useMessages();
400 // Follow-up tasks share a child conversation; this notice reports a particular task result.
401 const agent = useAtomValue(
402 environmentThreadDetails.threadAtom(props.parentRef),
403 (thread) =>
404 thread?.projection.subagents.find((agent) =>
405 props.taskIds === undefined
406 ? agent.childThreadId === props.childThreadId
407 : props.taskIds.includes(agent.id),
408 ) ?? null,
409 );
410 if (agent === null) return props.fallback;
411 const resumed =
412 NOTIFICATION_OUTCOME_STATUS[props.outcome] !== null &&
413 subagentResumedSince(agent, props.createdAt);
414 return (
415 <SubagentTimelineLink
416 parentRef={props.parentRef}
417 subagentId={agent.id}
418 providerInstanceId={agent.providerInstanceId}
419 origin={agent.origin}
420 status={agent.status}
421 driver={agent.driver}
422 title={formatSubagentDisplayTitle(agent.title ?? gt`Subagent`)}
423 result={agent.result}
424 progress={agent.progress}
425 startedAt={agent.startedAt}
426 completedAt={agent.completedAt}
427 threadId={props.childThreadId}
428 onOpenThread={props.onOpenThread}
429 {...(resumed
430 ? {}
431 : {
432 event: {
433 status: NOTIFICATION_OUTCOME_STATUS[props.outcome],
434 label: m(NOTIFICATION_OUTCOME_LABEL[props.outcome]),
435 timestamp: formatShortTimestamp(props.createdAt, props.timestampFormat),
436 },
437 })}
438 />
439 );
441​
442function SubagentTimelineLink(props: {
443 readonly parentRef: ScopedThreadRef;
444 readonly subagentId: NodeId;
445 readonly providerInstanceId: ProviderInstanceId;
446 readonly origin: OrchestrationV2Subagent["origin"];
447 readonly driver: ProviderDriverKind;
448 readonly title: string;
449 readonly result: string | null;
450 readonly progress: string | undefined;
451 readonly status: OrchestrationV2TurnItem["status"];
452 readonly startedAt: DateTime.Utc | null;
453 readonly completedAt: DateTime.Utc | null;
454 readonly threadId: ThreadId | null;
455 /** The thread in a linked environment that runs this task, when it runs there. */
456 readonly remote?: OrchestrationV2RemoteTaskChild | undefined;
457 readonly onOpenThread: (
458 threadId: ThreadId,
459 environmentId?: EnvironmentId,
460 intent?: SideThreadOpenIntent,
461 ) => void;
462 readonly event?: {
463 readonly status: OrchestrationV2TurnItem["status"] | null;
464 readonly label: string;
465 readonly timestamp: string;
466 };
467}) {
468 const agent = useAtomValue(
469 environmentThreadDetails.threadAtom(props.parentRef),
470 (thread) => thread?.projection.subagents.find((agent) => agent.id === props.subagentId) ?? null,
471 );
472 const remote = props.remote ?? agent?.remoteChild;
473 const gt = useGT();
474 const m = useMessages();
475 const threadId = props.threadId;
476 // A task in a linked environment opens there, or read-only through this one.
477 const remoteOpener = useLinkedThreadOpener(props.parentRef, remote, props.title);
478 const trigger = useSideThreadTrigger();
479 const canOpen = use(SideThreadsOpenContext);
480 const open = !canOpen
481 ? null
482 : threadId !== null
483 ? (intent: SideThreadOpenIntent) => props.onOpenThread(threadId, undefined, intent)
484 : (remoteOpener?.open ?? null);
485 // A task on a connected linked environment opens there, on the main screen only.
486 const panel = threadId !== null || remoteOpener?.mode === "linked";
487 const liveStatus = agent?.status ?? props.status;
488 const status = props.event ? props.event.status : liveStatus;
489 const statusLabel = props.event?.label ?? m(subagentStatusVisual(liveStatus).label);
490 const result = (agent?.result ?? props.result)?.trim();
491 const progress = (agent?.progress ?? props.progress)?.trim();
492 const settled = SETTLED_SUBAGENT_STATUSES.has(status ?? liveStatus);
493 const rawDetail = settled ? result || progress : progress || result;
494 const detail = subagentCardDetail(rawDetail);
495 const failed = status === "failed";
496 const timing = {
497 status: liveStatus,
498 startedAt: isoOrNull(agent?.startedAt ?? props.startedAt),
499 completedAt: isoOrNull(agent?.completedAt ?? props.completedAt),
500 };
501 const content = (
502 <>
503 <EnvironmentSubagentAvatar
504 environmentId={props.parentRef.environmentId}
505 providerInstanceId={props.providerInstanceId}
506 driver={props.driver}
507 remote={remote}
508 status={status ?? undefined}
509 />
510 <span className="min-w-0 flex-1">
511 <span className="flex items-baseline gap-2">
512 <span className="min-w-0 truncate text-xs font-medium text-foreground">
513 {props.title}
514 </span>
515 {remote !== undefined ? (
516 <span className="shrink-0 text-xs text-muted-foreground">
517 {gt`on ${{ environment: remote.label }}`}
518 </span>
519 ) : null}
520 </span>
521 <span
522 className={cn(
523 "block text-2xs leading-relaxed",
524 failed ? "text-destructive" : "text-muted-foreground",
525 )}
526 >
527 {detail === null ? (
528 statusLabel
529 ) : detail.includes("/") && !detail.includes(" ") ? (
530 <MiddleTruncate value={detail} showTitle={false} className="flex" />
531 ) : (
532 <span className="block truncate">{detail}</span>
533 )}
534 </span>
535 </span>
536 {detail !== null && (props.event !== undefined || status !== "completed") ? (
537 <span
538 className={cn("shrink-0 text-xs", failed ? "text-destructive" : "text-muted-foreground")}
539 >
540 {statusLabel}
541 </span>
542 ) : null}
543 <span className="shrink-0 text-xs text-muted-foreground tabular-nums">
544 {props.event ? props.event.timestamp : <SubagentElapsed agent={timing} />}
545 </span>
546 {open !== null ? (
547 <ChevronRightIcon
548 aria-hidden
549 className="size-3.5 shrink-0 text-muted-foreground/60 transition-colors group-hover/subagent:text-foreground"
550 />
551 ) : null}
552 </>
553 );
554 const className =
555 "group/subagent flex w-full min-w-0 items-center gap-2.5 rounded-md px-2 py-1.5 text-left";
556 return (
557 <Tooltip>
558 <TooltipTrigger
559 delay={200}
560 render={
561 open === null ? (
562 <div data-v2-item-type="subagent" aria-description={statusLabel} className={className}>
563 {content}
564 </div>
565 ) : (
566 <button
567 type="button"
568 data-v2-item-type="subagent"
569 aria-label={gt`Open ${{ title: props.title }}`}
570 aria-description={statusLabel}
571 {...trigger(open, { panel })}
572 className={cn(
573 className,
574 "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",
575 )}
576 >
577 {content}
578 </button>
579 )
580 }
581 />
582 <ThreadHoverCardPopup>
583 <SubagentTimelineTooltip
584 {...props}
585 remote={remote}
586 elapsed={agent ? <AgentElapsed agent={projectedSubagentsToRuntime([agent])[0]!} /> : null}
587 model={agent?.model ?? null}
588 modelSelection={agent?.modelSelection}
589 status={liveStatus}
590 result={agent?.result ?? props.result}
591 progress={agent?.progress ?? props.progress}
592 />
593 </ThreadHoverCardPopup>
594 </Tooltip>
595 );
597​
598function SubagentTimelineTooltip(
599 props: Parameters<typeof SubagentTimelineLink>[0] & {
600 model: string | null;
601 modelSelection: OrchestrationV2Subagent["modelSelection"];
602 elapsed: ReactNode;
603 },
604) {
605 const environmentId = props.parentRef.environmentId;
606 // Read providers only in the open popup to avoid per-row subscriptions.
607 const providers =
608 useAtomValue(
609 serverEnvironment.providersValueAtom(props.remote?.environmentId ?? environmentId),
610 ) ?? EMPTY_SERVER_PROVIDERS;
611 const parent = useThreadShell(props.parentRef)?.source;
612 const childRef = props.threadId
613 ? scopeThreadRef(environmentId, props.threadId)
614 : props.remote
615 ? scopeThreadRef(props.remote.environmentId, props.remote.threadId)
616 : null;
617 const child = useThreadShell(childRef)?.source;
618 const parentProject = useProject(
619 parent ? scopeProjectRef(environmentId, parent.projectId) : null,
620 );
621 const childProject = useProject(
622 child && childRef ? scopeProjectRef(childRef.environmentId, child.projectId) : null,
623 );
624 return (
625 <SubagentTooltipContent
626 title={formatSubagentDisplayTitle(child?.title ?? props.title)}
627 model={props.model}
628 providerInstanceId={props.providerInstanceId}
629 origin={props.origin}
630 modelSelection={props.modelSelection}
631 provider={providers.find((provider) => provider.instanceId === props.providerInstanceId)}
632 providers={providers}
633 driver={props.remote === undefined ? props.driver : undefined}
634 elapsed={props.elapsed}
635 status={props.status}
636 result={props.result}
637 progress={props.progress}
638 parentThread={parent}
639 childThread={child}
640 parentProject={parentProject ?? undefined}
641 childProject={childProject ?? undefined}
642 />
643 );
645​
646function EnvironmentHandoffRow({
647 environmentId,
648 ...props
649}: {
650 readonly environmentId: EnvironmentId;
651 readonly endpoints: HandoffEndpoints;
652 readonly failed: boolean;
653}) {
654 const providers =
655 useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS;
656 return <HandoffRow {...props} environmentId={environmentId} providers={providers} />;
658​
659/** Marks where `/clear` or an automation started a new provider session in this thread. */
660export function FreshSessionRow() {
661 const gt = useGT();
662 return <TimelineSystemDivider label={gt`New session`} icon={EraserIcon} />;
664​
665/** A same-harness model or effort change. Shows only what changed. */
666export function ModelSwitchRow(props: {
667 readonly environmentId: EnvironmentId;
668 readonly modelSwitch: OrchestrationV2ModelSwitch;
669}) {
670 const providers =
671 useAtomValue(serverEnvironment.providersValueAtom(props.environmentId)) ??
672 EMPTY_SERVER_PROVIDERS;
673 const change = resolveModelSwitchChange(props.modelSwitch, providers);
674 if (change === null) return null;
675 const describe = (selection: ModelSelection, effort: string | undefined) => {
676 const provider = providers.find((candidate) => candidate.instanceId === selection.instanceId);
677 const model = provider?.models.find((candidate) => candidate.slug === selection.model);
678 const modelName = model ? getTriggerDisplayModelName(model, provider?.driver) : selection.model;
679 return [change.modelChanged ? modelName : null, effort].filter(Boolean).join(" ");
680 };
681 const { from, to } = props.modelSwitch;
682 return (
683 <TimelineSystemDivider
684 label={`${describe(from, change.effort?.from)} → ${describe(to, change.effort?.to)}`}
685 />
686 );
688​
689export function HandoffRow(props: {
690 readonly environmentId: EnvironmentId;
691 readonly endpoints: HandoffEndpoints;
692 readonly providers: ReadonlyArray<ServerProvider>;
693 readonly pending?: boolean;
694 readonly failed?: boolean;
695}) {
696 const gt = useGT();
697 const { from: fromEndpoints, to } = props.endpoints;
698 const settings = useAtomValue(serverEnvironment.settingsValueAtom(props.environmentId));
699 const derived = deriveProviderInstanceEntries(props.providers);
700 const entries = settings ? applyProviderInstanceSettings(derived, settings, null) : derived;
701 return (
702 <TimelineSystemDivider
703 label={props.pending ? gt`Context handoff pending` : gt`Context handoff`}
704 icon={ArrowRightLeftIcon}
705 showDetailSeparator={false}
706 tone={props.failed ? "danger" : "neutral"}
707 detail={
708 <span className="inline-flex min-w-0 flex-wrap items-center justify-center gap-1.5">
709 {fromEndpoints.map((endpoint, index) => (
710 <span
711 key={`${endpoint.instanceId}:${endpoint.model ?? ""}`}
712 className="inline-flex min-w-0 items-center"
713 >
714 <HandoffEndpoint
715 entries={entries}
716 instanceId={endpoint.instanceId}
717 model={endpoint.model}
718 />
719 {index < fromEndpoints.length - 1 ? (
720 <span aria-hidden="true" className="ml-0.5">
721 ,
722 </span>
723 ) : null}
724 </span>
725 ))}
726 {fromEndpoints.length > 0 ? (
727 <ArrowRightIcon aria-hidden="true" className="size-3 shrink-0" />
728 ) : null}
729 <HandoffEndpoint entries={entries} instanceId={to.instanceId} model={to.model} />
730 </span>
731 }
732 />
733 );
735​
736function HandoffEndpoint(props: {
737 readonly entries: ReadonlyArray<ProviderInstanceEntry>;
738 readonly instanceId: ProviderInstanceId;
739 readonly model?: string | undefined;
740}) {
741 const entry = props.entries.find((candidate) => candidate.instanceId === props.instanceId);
742 const showBadge = entry !== undefined && shouldShowInstanceBadge(entry, props.entries);
743 const model = props.model?.trim();
744 const providerModel =
745 model === undefined || model.length === 0
746 ? undefined
747 : entry?.models.find((candidate) => candidate.slug === model);
748 const label =
749 providerModel !== undefined
750 ? getTriggerDisplayModelName(providerModel, entry?.driverKind)
751 : model !== undefined && model.length > 0
752 ? model
753 : (entry?.displayName ?? props.instanceId);
754 const labelColor = providerTextColor(
755 entry?.driverKind ?? ProviderDriverKind.make(props.instanceId),
756 );
757 return (
758 <SharedTooltipTrigger
759 render={
760 <span
761 tabIndex={0}
762 className="inline-flex min-w-0 items-center gap-1.5 rounded-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
763 >
764 <ProviderInstanceIcon
765 driverKind={entry?.driverKind ?? ProviderDriverKind.make(props.instanceId)}
766 displayName={entry?.displayName ?? props.instanceId}
767 accentColor={entry?.accentColor}
768 acpRegistryAgentId={entry?.acpRegistryAgentId}
769 acpRegistryIconUrl={entry?.acpRegistryIconUrl}
770 showBadge={showBadge}
771 size="compact"
772 iconClassName={cn(showBadge && "opacity-60")}
773 indicatorBackground="var(--background)"
774 />
775 <span
776 className={cn("truncate font-medium", labelColor.className)}
777 style={labelColor.style}
778 >
779 {label}
780 </span>
781 </span>
782 }
783 label={
784 <>
785 {entry?.displayName ?? props.instanceId} · {label}
786 </>
787 }
788 />
789 );