The best way to control Claude Code, Codex and more from any computer or phone

main
ts 113 lines 4.05 KB
Raw
1 import type {
2 EnvironmentId,
3 OrchestrationV2ShellSnapshot,
4 OrchestrationV2ThreadShell,
5 ThreadId,
6 } from "@t3tools/contracts";
7 import * as DateTime from "effect/DateTime";
8 import * as Effect from "effect/Effect";
9 import * as Exit from "effect/Exit";
10 import { Atom } from "effect/reactivity";
11
12 import type { AtomCommand } from "./runtime.ts";
13
14 interface PendingThreadUpdate {
15 readonly threadId: ThreadId;
16 readonly apply: (thread: OrchestrationV2ThreadShell) => OrchestrationV2ThreadShell;
17 sequence?: number;
18 }
19
20 export function createOptimisticThreadLifecycle(
21 sourceSnapshotAtom: (
22 environmentId: EnvironmentId,
23 ) => Atom.Atom<OrchestrationV2ShellSnapshot | null>,
24 ) {
25 const pendingAtom = Atom.family((_environmentId: EnvironmentId) =>
26 Atom.make<ReadonlyArray<PendingThreadUpdate>>([]).pipe(Atom.keepAlive),
27 );
28 const snapshotAtom = Atom.family((environmentId: EnvironmentId) =>
29 Atom.make((get) => {
30 const snapshot = get(sourceSnapshotAtom(environmentId));
31 const pending = get(pendingAtom(environmentId));
32 if (snapshot === null || pending.length === 0) return snapshot;
33 const byThread = new Map<ThreadId, PendingThreadUpdate[]>();
34 for (const update of pending) {
35 if (update.sequence !== undefined && update.sequence <= snapshot.snapshotSequence) continue;
36 const updates = byThread.get(update.threadId) ?? [];
37 updates.push(update);
38 byThread.set(update.threadId, updates);
39 }
40 if (byThread.size === 0) return snapshot;
41 return {
42 ...snapshot,
43 threads: snapshot.threads.map((thread) =>
44 (byThread.get(thread.id) ?? []).reduce(
45 (current, update) => update.apply(current),
46 thread,
47 ),
48 ),
49 };
50 }),
51 );
52
53 // Until the durable snapshot catches up, an older pending update can still replace what shows.
54 const pendingThreadIdsAtom = Atom.family((environmentId: EnvironmentId) =>
55 Atom.make(
56 (get): ReadonlySet<ThreadId> =>
57 new Set(get(pendingAtom(environmentId)).map((update) => update.threadId)),
58 ),
59 );
60
61 function wrap<Input extends { readonly threadId: ThreadId }, E>(
62 command: AtomCommand<
63 { readonly environmentId: EnvironmentId; readonly input: Input },
64 { readonly sequence: number },
65 E
66 >,
67 apply: (
68 thread: OrchestrationV2ThreadShell,
69 input: Input,
70 now: DateTime.Utc,
71 accepted: boolean,
72 ) => OrchestrationV2ThreadShell,
73 ): typeof command {
74 return {
75 label: command.label,
76 run: (registry, target) =>
77 Effect.suspend(() => {
78 const now = DateTime.nowUnsafe();
79 const pending = pendingAtom(target.environmentId);
80 const source = sourceSnapshotAtom(target.environmentId);
81 const update: PendingThreadUpdate = {
82 threadId: target.input.threadId,
83 apply: (thread) => apply(thread, target.input, now, update.sequence !== undefined),
84 };
85 const remove = () =>
86 registry.update(pending, (current) => current.filter((item) => item !== update));
87 // Stays applied after the reply until a snapshot at or past its sequence replaces it.
88 const confirm = (sequence: number) => {
89 update.sequence = sequence;
90 registry.update(pending, (current) => [...current]);
91 const reconcile = (snapshot: OrchestrationV2ShellSnapshot | null) => {
92 if (snapshot === null || snapshot.snapshotSequence >= sequence) {
93 remove();
94 unsubscribe();
95 }
96 };
97 const unsubscribe = registry.subscribe(source, reconcile);
98 reconcile(registry.get(source));
99 };
100 registry.update(pending, (current) => [...current, update]);
101 return command
102 .run(registry, target)
103 .pipe(
104 Effect.onExit((exit) =>
105 Effect.sync(() => (Exit.isSuccess(exit) ? confirm(exit.value.sequence) : remove())),
106 ),
107 );
108 }),
109 };
110 }
111
112 return { snapshotAtom, pendingThreadIdsAtom, wrap };
113 }