Open History to see the last commit
1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Ref from "effect/Ref";
6
7import { threadKey } from "./entities.ts";
8
9export type ThreadHistoryLoadEarlierResult =
10 | { readonly _tag: "loaded" }
11 | { readonly _tag: "noop" }
12 | { readonly _tag: "busy" }
13 | { readonly _tag: "error"; readonly message: string };
14
15export type ThreadHistoryHandler = {
16 readonly loadEarlier: () => Effect.Effect<ThreadHistoryLoadEarlierResult>;
17};
18
19/**
20 * Opaque registration token returned by `register`. Finalizers must pass the
21 * same token so an older fiber cannot delete a newer handler for the key.
22 */
23export type ThreadHistoryRegistration = {
24 readonly environmentId: EnvironmentId;
25 readonly threadId: ThreadId;
26 readonly handler: ThreadHistoryHandler;
27};
28
29/**
30 * Per-thread registration for progressive history loads. Thread state fibers
31 * register while active; mobile UI dispatches through `loadEarlier`.
32 */
33export class ThreadHistoryController extends Context.Service<
34 ThreadHistoryController,
35 {
36 readonly register: (
37 environmentId: EnvironmentId,
38 threadId: ThreadId,
39 handler: ThreadHistoryHandler,
40 ) => Effect.Effect<ThreadHistoryRegistration>;
41 readonly unregister: (registration: ThreadHistoryRegistration) => Effect.Effect<void>;
42 readonly loadEarlier: (
43 environmentId: EnvironmentId,
44 threadId: ThreadId,
45 ) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
46 }
47>()("@t3tools/client-runtime/state/threadHistoryController") {}
48
49export const layer: Layer.Layer<ThreadHistoryController> = Layer.effect(
50 ThreadHistoryController,
51 Effect.gen(function* () {
52 const handlers = yield* Ref.make(new Map<string, ThreadHistoryHandler>());
53 return ThreadHistoryController.of({
54 register: (environmentId, threadId, handler) =>
55 Ref.update(handlers, (current) => {
56 const next = new Map(current);
57 next.set(threadKey({ environmentId, threadId }), handler);
58 return next;
59 }).pipe(
60 Effect.as({
61 environmentId,
62 threadId,
63 handler,
64 } satisfies ThreadHistoryRegistration),
65 ),
66 unregister: (registration) =>
67 Ref.update(handlers, (current) => {
68 const key = threadKey({
69 environmentId: registration.environmentId,
70 threadId: registration.threadId,
71 });
72 // Only remove when this registration still owns the slot. An older
73 // fiber finalizer must not delete a newer handler for the same key.
74 if (current.get(key) !== registration.handler) {
75 return current;
76 }
77 const next = new Map(current);
78 next.delete(key);
79 return next;
80 }),
81 loadEarlier: (environmentId, threadId) =>
82 Ref.get(handlers).pipe(
83 Effect.flatMap((current) => {
84 const handler = current.get(threadKey({ environmentId, threadId }));
85 if (handler === undefined) {
86 return Effect.succeed({ _tag: "noop" } satisfies ThreadHistoryLoadEarlierResult);
87 }
88 return handler.loadEarlier();
89 }),
90 ),
91 });
92 }),
93);