The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | EnvironmentId, |
| 3 | type OrchestrationV2ArchivedShellSnapshot, |
| 4 | type ScopedThreadRef, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import * as Arr from "effect/Array"; |
| 7 | import { pipe } from "effect/Function"; |
| 8 | import * as Order from "effect/Order"; |
| 9 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 10 | |
| 11 | import { threadKey } from "./entities.ts"; |
| 12 | import { hiddenThreadKeys, type TagFilteredEnvironment } from "./threadTagFilter.ts"; |
| 13 | |
| 14 | export interface ArchivedSnapshotEntry { |
| 15 | readonly environmentId: EnvironmentId; |
| 16 | readonly snapshot: OrchestrationV2ArchivedShellSnapshot; |
| 17 | } |
| 18 | |
| 19 | export interface ArchivedThreadSnapshotsState { |
| 20 | readonly snapshots: ReadonlyArray<ArchivedSnapshotEntry>; |
| 21 | readonly error: string | null; |
| 22 | readonly isLoading: boolean; |
| 23 | } |
| 24 | |
| 25 | const ARCHIVED_THREADS_ENVIRONMENT_KEY_SEPARATOR = "\u001f"; |
| 26 | const environmentIdOrder = Order.String as Order.Order<EnvironmentId>; |
| 27 | |
| 28 | export function makeArchivedThreadsEnvironmentKey( |
| 29 | environmentIds: ReadonlyArray<EnvironmentId>, |
| 30 | ): string { |
| 31 | return pipe(environmentIds, Arr.sort(environmentIdOrder), (sortedEnvironmentIds) => |
| 32 | sortedEnvironmentIds.join(ARCHIVED_THREADS_ENVIRONMENT_KEY_SEPARATOR), |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | function parseArchivedThreadsEnvironmentKey(key: string): ReadonlyArray<EnvironmentId> { |
| 37 | if (key.length === 0) { |
| 38 | return []; |
| 39 | } |
| 40 | return pipe( |
| 41 | key.split(ARCHIVED_THREADS_ENVIRONMENT_KEY_SEPARATOR), |
| 42 | Arr.map((environmentId) => EnvironmentId.make(environmentId)), |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | export function createArchivedThreadSnapshotsAtomFamily<E>(options: { |
| 47 | readonly getSnapshotAtom: ( |
| 48 | environmentId: EnvironmentId, |
| 49 | ) => Atom.Atom<AsyncResult.AsyncResult<OrchestrationV2ArchivedShellSnapshot, E>>; |
| 50 | readonly hiddenThreadTagsAtom: ( |
| 51 | environmentId: EnvironmentId, |
| 52 | ) => Atom.Atom<ReadonlyArray<string> | null>; |
| 53 | /** Live threads the tag filter hides, so archived children of hidden parents stay hidden. */ |
| 54 | readonly hiddenThreadKeysAtom: Atom.Atom<ReadonlySet<string>>; |
| 55 | /** Live threads, so archived children of loaded, visible parents stay visible. */ |
| 56 | readonly liveThreadRefsAtom: Atom.Atom<ReadonlyArray<ScopedThreadRef>>; |
| 57 | /** Whether any connected environment hides tags, including ones outside this archive's scope. */ |
| 58 | readonly anyEnvironmentHidingAtom: Atom.Atom<boolean>; |
| 59 | readonly labelPrefix: string; |
| 60 | }) { |
| 61 | return Atom.family((environmentKey: string) => |
| 62 | Atom.make((get): ArchivedThreadSnapshotsState => { |
| 63 | const snapshots: ArchivedSnapshotEntry[] = []; |
| 64 | const environments: TagFilteredEnvironment[] = []; |
| 65 | let error: string | null = null; |
| 66 | let isLoading = false; |
| 67 | |
| 68 | for (const environmentId of parseArchivedThreadsEnvironmentKey(environmentKey)) { |
| 69 | const result = get(options.getSnapshotAtom(environmentId)); |
| 70 | isLoading ||= result.waiting; |
| 71 | |
| 72 | const snapshot = AsyncResult.getOrElse(result, () => null); |
| 73 | if (snapshot !== null) { |
| 74 | snapshots.push({ environmentId, snapshot }); |
| 75 | } |
| 76 | // A page that has not arrived still counts, so its tags apply to children elsewhere. |
| 77 | environments.push({ |
| 78 | environmentId, |
| 79 | threads: snapshot?.threads ?? [], |
| 80 | hiddenTags: get(options.hiddenThreadTagsAtom(environmentId)), |
| 81 | }); |
| 82 | |
| 83 | if (error === null && result._tag === "Failure") { |
| 84 | error = "Failed to load archived threads."; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // One pass over every environment's page, so archived tasks follow parents archived elsewhere. |
| 89 | // Any connected machine may hide an unloaded parent, so the global flag decides, not the scope. |
| 90 | const hidingAnywhere = get(options.anyEnvironmentHidingAtom); |
| 91 | const hidden = hiddenThreadKeys( |
| 92 | environments, |
| 93 | get(options.hiddenThreadKeysAtom), |
| 94 | hidingAnywhere ? new Set(get(options.liveThreadRefsAtom).map(threadKey)) : undefined, |
| 95 | hidingAnywhere, |
| 96 | ); |
| 97 | if (hidden.size === 0) return { snapshots, error, isLoading }; |
| 98 | return { |
| 99 | snapshots: snapshots.map(({ environmentId, snapshot }) => ({ |
| 100 | environmentId, |
| 101 | snapshot: { |
| 102 | ...snapshot, |
| 103 | threads: snapshot.threads.filter( |
| 104 | (thread) => !hidden.has(threadKey({ environmentId, threadId: thread.id })), |
| 105 | ), |
| 106 | }, |
| 107 | })), |
| 108 | error, |
| 109 | isLoading, |
| 110 | }; |
| 111 | }).pipe(Atom.withLabel(`${options.labelPrefix}:${environmentKey}`)), |
| 112 | ); |
| 113 | } |