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