The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type * as DateTime from "effect/DateTime"; |
| 2 | import { msg } from "gt-react"; |
| 3 | import { useAtomValue } from "@effect/atom-react"; |
| 4 | import type { |
| 5 | EnvironmentProject, |
| 6 | EnvironmentThread, |
| 7 | EnvironmentThreadShell, |
| 8 | EnvironmentThreadStructure, |
| 9 | ThreadStructure, |
| 10 | ThreadStructureVisibleTurnItem, |
| 11 | } from "@t3tools/client-runtime/state/shell"; |
| 12 | import { |
| 13 | EMPTY_THREAD_HISTORY_META, |
| 14 | type EnvironmentThreadStatus, |
| 15 | type ThreadHistoryMeta, |
| 16 | } from "@t3tools/client-runtime/state/threads"; |
| 17 | import { |
| 18 | arrayElementsEqual, |
| 19 | parseThreadKey, |
| 20 | threadKey, |
| 21 | } from "@t3tools/client-runtime/state/entities"; |
| 22 | import type { ScopedProjectRef, ScopedThreadRef } from "@t3tools/contracts"; |
| 23 | import type { |
| 24 | EnvironmentId, |
| 25 | OrchestrationV2ProjectedTurnItem, |
| 26 | OrchestrationV2RemoteTaskChild, |
| 27 | OrchestrationV2ThreadShell, |
| 28 | NodeId, |
| 29 | ThreadId, |
| 30 | TurnItemId, |
| 31 | } from "@t3tools/contracts"; |
| 32 | import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints"; |
| 33 | import { Atom } from "effect/reactivity"; |
| 34 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 35 | import { environmentProjects } from "./projects"; |
| 36 | import { environmentPresentations } from "./presentation"; |
| 37 | import { environmentServerConfigsAtom } from "./server"; |
| 38 | import { |
| 39 | allEnvironmentProjectSnapshotsReadyAtom, |
| 40 | allEnvironmentShellsBootstrappedAtom, |
| 41 | } from "./shell"; |
| 42 | import { environmentThreadDetails, environmentThreadShells } from "./threads"; |
| 43 | import { waitForAtomValue } from "./waitForAtomValue"; |
| 44 | import { m } from "../i18n/messages"; |
| 45 | |
| 46 | const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]); |
| 47 | const EMPTY_VISIBLE_TURN_ITEMS: ReadonlyArray<OrchestrationV2ProjectedTurnItem> = Object.freeze([]); |
| 48 | |
| 49 | const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe( |
| 50 | Atom.withLabel("web-project:empty"), |
| 51 | ); |
| 52 | const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe( |
| 53 | Atom.withLabel("web-thread-refs:empty"), |
| 54 | ); |
| 55 | const EMPTY_THREAD_SHELLS_ATOM = Atom.make<ReadonlyArray<EnvironmentThreadShell>>( |
| 56 | Object.freeze([]), |
| 57 | ).pipe(Atom.withLabel("web-thread-shells:empty")); |
| 58 | const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe( |
| 59 | Atom.withLabel("web-thread-shell:empty"), |
| 60 | ); |
| 61 | const EMPTY_THREAD_PROJECTION_ATOM = Atom.make<EnvironmentThread | null>(null).pipe( |
| 62 | Atom.withLabel("web-thread-projection:empty"), |
| 63 | ); |
| 64 | const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe( |
| 65 | Atom.withLabel("web-thread-status:empty"), |
| 66 | ); |
| 67 | const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe( |
| 68 | Atom.withLabel("web-thread-visible-turn-items:empty"), |
| 69 | ); |
| 70 | const EMPTY_CHECKPOINT_SUMMARIES_ATOM = Atom.make<ReadonlyArray<ThreadCheckpointSummary>>( |
| 71 | Object.freeze([]), |
| 72 | ).pipe(Atom.withLabel("web-thread-checkpoint-summaries:empty")); |
| 73 | const EMPTY_ASSISTANT_MESSAGE_TEXT_ATOM = Atom.make<string | null>(null).pipe( |
| 74 | Atom.withLabel("web-assistant-message-text:empty"), |
| 75 | ); |
| 76 | const EMPTY_LAST_VISITED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe( |
| 77 | Atom.withLabel("web-thread-last-visited:empty"), |
| 78 | ); |
| 79 | const EMPTY_UPDATED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe( |
| 80 | Atom.withLabel("web-thread-updated-at:empty"), |
| 81 | ); |
| 82 | const EMPTY_THREAD_HISTORY_ATOM = Atom.make<ThreadHistoryMeta>(EMPTY_THREAD_HISTORY_META).pipe( |
| 83 | Atom.withLabel("web-thread-history:empty"), |
| 84 | ); |
| 85 | |
| 86 | export function useThreadRefs(enabled = true): ReadonlyArray<ScopedThreadRef> { |
| 87 | return useAtomValue(enabled ? environmentThreadShells.threadRefsAtom : EMPTY_THREAD_REFS_ATOM); |
| 88 | } |
| 89 | |
| 90 | export function useEnvironmentThreadRefs( |
| 91 | environmentId: EnvironmentId | null, |
| 92 | ): ReadonlyArray<ScopedThreadRef> { |
| 93 | return useAtomValue( |
| 94 | environmentId === null |
| 95 | ? EMPTY_THREAD_REFS_ATOM |
| 96 | : environmentThreadShells.environmentThreadRefsAtom(environmentId), |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | export function useProjects(): ReadonlyArray<EnvironmentProject> { |
| 101 | return useAtomValue(environmentProjects.projectsAtom); |
| 102 | } |
| 103 | |
| 104 | /** Providers are left out; read them with `useEnvironmentProviders()`. */ |
| 105 | export function useServerConfigs() { |
| 106 | return useAtomValue(environmentPresentations.serverConfigsAtom); |
| 107 | } |
| 108 | |
| 109 | export function useThreadShells(enabled = true): ReadonlyArray<EnvironmentThreadShell> { |
| 110 | return useAtomValue( |
| 111 | enabled ? environmentThreadShells.threadShellsAtom : EMPTY_THREAD_SHELLS_ATOM, |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** Whether any connected environment is hiding tagged threads right now. */ |
| 116 | export function useAnyEnvironmentHiding(): boolean { |
| 117 | return useAtomValue(environmentThreadShells.anyEnvironmentHidingAtom); |
| 118 | } |
| 119 | |
| 120 | /** Keys of threads the tag filter hides from display, by `threadKey`. */ |
| 121 | export function useHiddenThreadKeys(): ReadonlySet<string> { |
| 122 | return useAtomValue(environmentThreadShells.hiddenThreadKeysAtom); |
| 123 | } |
| 124 | |
| 125 | /** Thread shells minus those the tag filter hides, for lists that show threads to the user. */ |
| 126 | export function useVisibleThreadShells(enabled = true): ReadonlyArray<EnvironmentThreadShell> { |
| 127 | return useAtomValue( |
| 128 | enabled ? environmentThreadShells.visibleThreadShellsAtom : EMPTY_THREAD_SHELLS_ATOM, |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | export function useAllEnvironmentShellsBootstrapped(): boolean { |
| 133 | return useAtomValue(allEnvironmentShellsBootstrappedAtom); |
| 134 | } |
| 135 | |
| 136 | export function useAllEnvironmentProjectSnapshotsReady(): boolean { |
| 137 | return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom); |
| 138 | } |
| 139 | |
| 140 | /** Tasks linked environments run for a listed thread, by thread key; lists show them under it. */ |
| 141 | export function useDelegatedUnderParent(): ReadonlySet<string> { |
| 142 | return useAtomValue(environmentThreadShells.delegatedUnderParentAtom); |
| 143 | } |
| 144 | |
| 145 | export function useThreadShellsForProjectRefs( |
| 146 | refs: ReadonlyArray<ScopedProjectRef>, |
| 147 | ): ReadonlyArray<EnvironmentThreadShell> { |
| 148 | return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs)); |
| 149 | } |
| 150 | |
| 151 | export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null { |
| 152 | return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref)); |
| 153 | } |
| 154 | |
| 155 | export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null { |
| 156 | return useAtomValue( |
| 157 | ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref), |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | /** The shell without its streaming timestamps; see quietThreadShellAtom. */ |
| 162 | export function useQuietThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null { |
| 163 | return useAtomValue( |
| 164 | ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.quietThreadShellAtom(ref), |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | /** Every thread shell, held through streaming ticks; see quietThreadShellAtom. */ |
| 169 | export function useQuietThreadShells(): ReadonlyArray<EnvironmentThreadShell> { |
| 170 | return useAtomValue(environmentThreadShells.quietThreadShellsAtom); |
| 171 | } |
| 172 | |
| 173 | export function useChildThreadInputs(ref: ScopedThreadRef | null) { |
| 174 | return useAtomValue( |
| 175 | ref === null ? EMPTY_THREAD_SHELLS_ATOM : environmentThreadShells.childThreadInputsAtom(ref), |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | export function useThreadProjection(ref: ScopedThreadRef | null): EnvironmentThread | null { |
| 180 | return useAtomValue( |
| 181 | ref === null ? EMPTY_THREAD_PROJECTION_ATOM : environmentThreadDetails.threadAtom(ref), |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | /** The thread's structure, held through streamed assistant and reasoning text; see ThreadStructure. */ |
| 186 | export function useQuietThreadProjection( |
| 187 | ref: ScopedThreadRef | null, |
| 188 | ): EnvironmentThreadStructure | null { |
| 189 | return useAtomValue( |
| 190 | ref === null ? EMPTY_THREAD_PROJECTION_ATOM : environmentThreadDetails.quietThreadAtom(ref), |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | /** The detail's live visit mark, which the thread structure leaves out. */ |
| 195 | export function useThreadLastVisitedAt(ref: ScopedThreadRef | null): DateTime.Utc | null { |
| 196 | return useAtomValue( |
| 197 | ref === null ? EMPTY_LAST_VISITED_AT_ATOM : environmentThreadDetails.lastVisitedAtAtom(ref), |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** The detail's live `updatedAt`, which the thread structure leaves out. */ |
| 202 | export function useThreadUpdatedAt(ref: ScopedThreadRef | null): DateTime.Utc | null { |
| 203 | return useAtomValue( |
| 204 | ref === null ? EMPTY_UPDATED_AT_ATOM : environmentThreadDetails.updatedAtAtom(ref), |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus { |
| 209 | return useAtomValue( |
| 210 | ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref), |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | export function useThreadHistory(ref: ScopedThreadRef | null): ThreadHistoryMeta { |
| 215 | return useAtomValue( |
| 216 | ref === null ? EMPTY_THREAD_HISTORY_ATOM : environmentThreadDetails.historyAtom(ref), |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | export function resolveThreadDetailRef( |
| 221 | ref: ScopedThreadRef | null, |
| 222 | options: { |
| 223 | shellExists: boolean; |
| 224 | waitForShell: boolean; |
| 225 | }, |
| 226 | ): ScopedThreadRef | null { |
| 227 | return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null; |
| 228 | } |
| 229 | |
| 230 | export function useThreadVisibleTurnItems( |
| 231 | ref: ScopedThreadRef | null, |
| 232 | ): ReadonlyArray<OrchestrationV2ProjectedTurnItem> { |
| 233 | return useAtomValue( |
| 234 | ref === null |
| 235 | ? EMPTY_VISIBLE_TURN_ITEMS_ATOM |
| 236 | : environmentThreadDetails.visibleTurnItemsAtom(ref), |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** Visible turn items held through streamed assistant text; see useAssistantMessageText. */ |
| 241 | export function useTimelineVisibleTurnItems( |
| 242 | ref: ScopedThreadRef | null, |
| 243 | ): ReadonlyArray<OrchestrationV2ProjectedTurnItem> { |
| 244 | return useAtomValue( |
| 245 | ref === null |
| 246 | ? EMPTY_VISIBLE_TURN_ITEMS_ATOM |
| 247 | : environmentThreadDetails.timelineVisibleTurnItemsAtom(ref), |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | /** One assistant message's live text, or null when the thread does not hold the item. */ |
| 252 | export function useAssistantMessageText( |
| 253 | ref: ScopedThreadRef | null, |
| 254 | itemId: string | undefined, |
| 255 | ): string | null { |
| 256 | return useAtomValue( |
| 257 | ref === null || itemId === undefined |
| 258 | ? EMPTY_ASSISTANT_MESSAGE_TEXT_ATOM |
| 259 | : environmentThreadDetails.assistantMessageTextAtom(ref, itemId), |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | /** Visible turn items without their streamed text; see ThreadStructure. */ |
| 264 | export function useQuietThreadVisibleTurnItems( |
| 265 | ref: ScopedThreadRef | null, |
| 266 | ): ReadonlyArray<ThreadStructureVisibleTurnItem> { |
| 267 | return useAtomValue( |
| 268 | ref === null |
| 269 | ? EMPTY_VISIBLE_TURN_ITEMS_ATOM |
| 270 | : environmentThreadDetails.quietVisibleTurnItemsAtom(ref), |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | export function useThreadCheckpointSummaries( |
| 275 | ref: ScopedThreadRef | null, |
| 276 | ): ReadonlyArray<ThreadCheckpointSummary> { |
| 277 | return useAtomValue( |
| 278 | ref === null |
| 279 | ? EMPTY_CHECKPOINT_SUMMARIES_ATOM |
| 280 | : environmentThreadDetails.checkpointSummariesAtom(ref), |
| 281 | ); |
| 282 | } |
| 283 | |
| 284 | export function readProject(ref: ScopedProjectRef): EnvironmentProject | null { |
| 285 | return appAtomRegistry.get(environmentProjects.projectAtom(ref)); |
| 286 | } |
| 287 | |
| 288 | export function subscribeProject( |
| 289 | ref: ScopedProjectRef, |
| 290 | listener: (project: EnvironmentProject | null) => void, |
| 291 | ): () => void { |
| 292 | return appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), listener); |
| 293 | } |
| 294 | |
| 295 | export function readProjects(): ReadonlyArray<EnvironmentProject> { |
| 296 | return appAtomRegistry.get(environmentProjects.projectsAtom); |
| 297 | } |
| 298 | |
| 299 | export function waitForProject( |
| 300 | ref: ScopedProjectRef, |
| 301 | timeoutMs = 10_000, |
| 302 | ): Promise<EnvironmentProject> { |
| 303 | const current = readProject(ref); |
| 304 | if (current !== null) return Promise.resolve(current); |
| 305 | |
| 306 | return new Promise((resolve, reject) => { |
| 307 | let unsubscribe: (() => void) | null = null; |
| 308 | const timeout = setTimeout(() => { |
| 309 | unsubscribe?.(); |
| 310 | reject(new Error(m(msg("The project did not appear in the desktop app.")))); |
| 311 | }, timeoutMs); |
| 312 | const finish = (project: EnvironmentProject | null) => { |
| 313 | if (project === null) return; |
| 314 | clearTimeout(timeout); |
| 315 | unsubscribe?.(); |
| 316 | resolve(project); |
| 317 | }; |
| 318 | unsubscribe = appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), finish); |
| 319 | finish(readProject(ref)); |
| 320 | }); |
| 321 | } |
| 322 | |
| 323 | /** Waits for removal from the live store; delete replies arrive before the shell stream. */ |
| 324 | export function waitForProjectRemoval(ref: ScopedProjectRef, timeoutMs = 5_000): Promise<boolean> { |
| 325 | return waitForAtomValue({ |
| 326 | registry: appAtomRegistry, |
| 327 | atom: environmentProjects.projectAtom(ref), |
| 328 | predicate: (project) => project === null, |
| 329 | timeoutMs, |
| 330 | }); |
| 331 | } |
| 332 | |
| 333 | /** The thread's loaded timeline, for a handler that reads it once instead of subscribing. */ |
| 334 | export function readThreadTurnItems(ref: ScopedThreadRef) { |
| 335 | return ( |
| 336 | appAtomRegistry.get(environmentThreadDetails.quietThreadAtom(ref))?.projection |
| 337 | .visibleTurnItems ?? [] |
| 338 | ); |
| 339 | } |
| 340 | |
| 341 | export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null { |
| 342 | return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref)); |
| 343 | } |
| 344 | |
| 345 | export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> { |
| 346 | return waitForAtomValue({ |
| 347 | registry: appAtomRegistry, |
| 348 | atom: environmentThreadShells.threadShellAtom(ref), |
| 349 | predicate: (thread) => thread !== null, |
| 350 | timeoutMs, |
| 351 | }); |
| 352 | } |
| 353 | |
| 354 | export function useEnvironmentSupportsServerBrowser(environmentId: EnvironmentId | null): boolean { |
| 355 | const configs = useServerConfigs(); |
| 356 | return ( |
| 357 | environmentId !== null && |
| 358 | configs.get(environmentId)?.environment.capabilities.serverBrowser === true |
| 359 | ); |
| 360 | } |
| 361 | |
| 362 | export function readEnvironmentSupportsServerBrowser(environmentId: EnvironmentId): boolean { |
| 363 | return ( |
| 364 | appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities |
| 365 | .serverBrowser === true |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | export function readEnvironmentServerConfigLoaded(environmentId: EnvironmentId): boolean { |
| 370 | return appAtomRegistry.get(environmentServerConfigsAtom).has(environmentId); |
| 371 | } |
| 372 | |
| 373 | export function readEnvironmentSupportsAutoSettleOptOut(environmentId: EnvironmentId): boolean { |
| 374 | return ( |
| 375 | appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities |
| 376 | .threadAutoSettleOptOut === true |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | export function readEnvironmentThreadRefs( |
| 381 | environmentId: EnvironmentId, |
| 382 | ): ReadonlyArray<ScopedThreadRef> { |
| 383 | return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId)); |
| 384 | } |
| 385 | |
| 386 | export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> { |
| 387 | return appAtomRegistry.get(environmentThreadShells.threadShellsAtom); |
| 388 | } |
| 389 | |
| 390 | export function readDelegatedUnderParent(): ReadonlySet<string> { |
| 391 | return appAtomRegistry.get(environmentThreadShells.delegatedUnderParentAtom); |
| 392 | } |
| 393 | |
| 394 | function shellParentThreadId( |
| 395 | thread: Pick<OrchestrationV2ThreadShell, "forkedFrom" | "lineage">, |
| 396 | ): ThreadId | null { |
| 397 | return thread.forkedFrom?.type === "run" |
| 398 | ? thread.forkedFrom.threadId |
| 399 | : thread.lineage.parentThreadId; |
| 400 | } |
| 401 | |
| 402 | /** Retains unchanged reachable shells so unrelated streams do not re-render the panel. */ |
| 403 | export function selectThreadRelationshipShells( |
| 404 | threadId: ThreadId, |
| 405 | projection: Pick<ThreadStructure, "thread" | "subagents" | "contextTransfers"> | null, |
| 406 | threads: ReadonlyArray<OrchestrationV2ThreadShell>, |
| 407 | ): ReadonlyArray<OrchestrationV2ThreadShell> { |
| 408 | const named = new Set<string>([threadId]); |
| 409 | if (projection !== null) { |
| 410 | const parentThreadId = shellParentThreadId(projection.thread); |
| 411 | if (parentThreadId !== null) named.add(parentThreadId); |
| 412 | for (const subagent of projection.subagents) { |
| 413 | if (subagent.childThreadId !== null) named.add(subagent.childThreadId); |
| 414 | for (const agent of subagent.workflow?.agents ?? []) { |
| 415 | if (agent.childThreadId !== undefined) named.add(agent.childThreadId); |
| 416 | } |
| 417 | } |
| 418 | for (const transfer of projection.contextTransfers) { |
| 419 | named.add(transfer.sourceThreadId); |
| 420 | named.add(transfer.targetThreadId); |
| 421 | } |
| 422 | } |
| 423 | const current = threads.find((thread) => thread.id === threadId); |
| 424 | const currentParentId = current === undefined ? null : shellParentThreadId(current); |
| 425 | if (currentParentId !== null) named.add(currentParentId); |
| 426 | return threads.filter( |
| 427 | (thread) => named.has(thread.id) || shellParentThreadId(thread) === threadId, |
| 428 | ); |
| 429 | } |
| 430 | |
| 431 | const relationshipShellsAtomFamily = Atom.family((key: string) => { |
| 432 | const ref = parseThreadKey(key); |
| 433 | let previous: ReadonlyArray<OrchestrationV2ThreadShell> = []; |
| 434 | return Atom.make((get) => { |
| 435 | const next = selectThreadRelationshipShells( |
| 436 | ref.threadId, |
| 437 | get(environmentThreadDetails.quietThreadAtom(ref))?.projection ?? null, |
| 438 | get(environmentThreadShells.environmentThreadsAtom(ref.environmentId)), |
| 439 | ).map( |
| 440 | (thread) => |
| 441 | get( |
| 442 | environmentThreadShells.quietThreadShellAtom({ |
| 443 | environmentId: ref.environmentId, |
| 444 | threadId: thread.id, |
| 445 | }), |
| 446 | )?.source ?? thread, |
| 447 | ); |
| 448 | if (arrayElementsEqual(previous, next)) return previous; |
| 449 | previous = next; |
| 450 | return next; |
| 451 | }).pipe(Atom.withLabel(`web-thread-relationship-shells:${key}`)); |
| 452 | }); |
| 453 | |
| 454 | export function useThreadRelationshipShells( |
| 455 | ref: ScopedThreadRef, |
| 456 | ): ReadonlyArray<OrchestrationV2ThreadShell> { |
| 457 | return useAtomValue(relationshipShellsAtomFamily(threadKey(ref))); |
| 458 | } |
| 459 | |
| 460 | interface SubagentChild { |
| 461 | readonly threadId: ThreadId; |
| 462 | /** Set when the task ran on a linked environment, which holds its thread. */ |
| 463 | readonly environmentId?: EnvironmentId; |
| 464 | readonly title: string | null | undefined; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * The subagents a response reported on, read once on demand so a timeline row does not subscribe |
| 469 | * to the thread for it. A result reaches the thread as a message from the subagent's thread or as |
| 470 | * a completion notice, at the start of a run or steered into one, so the inputs just before the |
| 471 | * response decide it. Turn items are read because older history loads nothing else. |
| 472 | */ |
| 473 | export function readResponseSubagents( |
| 474 | ref: ScopedThreadRef, |
| 475 | itemId: TurnItemId, |
| 476 | ): ReadonlyArray<Omit<SubagentChild, "title"> & { readonly title: string }> { |
| 477 | const projection = appAtomRegistry.get(environmentThreadDetails.quietThreadAtom(ref))?.projection; |
| 478 | if (!projection) return []; |
| 479 | const items = projection.visibleTurnItems; |
| 480 | const isInput = (index: number) => { |
| 481 | const type = items[index]?.item.type; |
| 482 | return type === "user_message" || type === "notification"; |
| 483 | }; |
| 484 | let index = items.findIndex((projected) => projected.sourceItemId === itemId) - 1; |
| 485 | while (index >= 0 && !isInput(index)) index -= 1; |
| 486 | |
| 487 | const byTask = new Map<NodeId, SubagentChild>(); |
| 488 | const local = new Map<ThreadId, SubagentChild>(); |
| 489 | const remember = ( |
| 490 | taskId: NodeId, |
| 491 | childThreadId: ThreadId | null, |
| 492 | remote: OrchestrationV2RemoteTaskChild | undefined, |
| 493 | title: string | null, |
| 494 | ) => { |
| 495 | if (childThreadId !== null) { |
| 496 | const child = { threadId: childThreadId, title }; |
| 497 | byTask.set(taskId, child); |
| 498 | local.set(childThreadId, child); |
| 499 | } else if (remote) { |
| 500 | // A task sent to a linked environment has no thread here; its thread lives there. |
| 501 | const { threadId, environmentId } = remote; |
| 502 | byTask.set(taskId, { threadId, environmentId, title: title ?? remote.title }); |
| 503 | } |
| 504 | }; |
| 505 | for (const { item } of items) { |
| 506 | if (item.type === "subagent") { |
| 507 | remember(item.subagentId, item.childThreadId, item.remoteChild, null); |
| 508 | } |
| 509 | } |
| 510 | for (const subagent of projection.subagents) { |
| 511 | remember(subagent.id, subagent.childThreadId, subagent.remoteChild, subagent.title); |
| 512 | } |
| 513 | |
| 514 | const found = new Map<string, SubagentChild>(); |
| 515 | const add = (child: SubagentChild | undefined) => { |
| 516 | if (child) found.set(`${child.environmentId ?? ""}:${child.threadId}`, child); |
| 517 | }; |
| 518 | for (; index >= 0 && isInput(index); index -= 1) { |
| 519 | const item = items[index]!.item; |
| 520 | if (item.type === "notification") { |
| 521 | // A notice names its child itself, so it counts even when the task is no longer loaded. |
| 522 | if ("childThreadId" in item.source && item.source.childThreadId !== undefined) { |
| 523 | const threadId = item.source.childThreadId; |
| 524 | add(local.get(threadId) ?? { threadId, title: null }); |
| 525 | } |
| 526 | for (const taskId of "taskIds" in item.source ? item.source.taskIds : []) { |
| 527 | add(byTask.get(taskId)); |
| 528 | } |
| 529 | } else if (item.type === "user_message") { |
| 530 | const message = projection.messages.find(({ id }) => id === item.messageId); |
| 531 | for (const taskId of message?.delegatedCompletion?.taskIds ?? []) add(byTask.get(taskId)); |
| 532 | // A sender counts only as this thread's subagent: a child's first message comes from its parent. |
| 533 | if (item.senderThreadId !== undefined) add(local.get(item.senderThreadId)); |
| 534 | } |
| 535 | } |
| 536 | return [...found.values()].map(({ title, ...child }) => ({ |
| 537 | ...child, |
| 538 | title: |
| 539 | title?.trim() || |
| 540 | appAtomRegistry |
| 541 | .get( |
| 542 | environmentThreadShells.quietThreadShellAtom({ |
| 543 | environmentId: child.environmentId ?? ref.environmentId, |
| 544 | threadId: child.threadId, |
| 545 | }), |
| 546 | ) |
| 547 | ?.title.trim() || |
| 548 | // oxlint-disable-next-line t3code/no-untranslated-copy -- subagent title stored in the reply record sent to the agent |
| 549 | "Agent", |
| 550 | })); |
| 551 | } |