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
15/** How a load may leave or fill a gap in the timeline. Only a client that draws gaps asks. */
16export type ThreadHistoryRangeRequest = {
17 /** With a target: take the page around it when it is far back, leaving a gap. */
18 readonly detach?: boolean;
19 /** Load the newest unloaded rows of the gap before this row. */
20 readonly gapSeamItemId?: string;
21};
22
23export type ThreadHistoryHandler = {
24 readonly loadEarlier: (
25 throughEntryId?: string,
26 workAnchorId?: string,
27 range?: ThreadHistoryRangeRequest,
28 ) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
29 readonly reconcilePendingWork: () => Effect.Effect<void>;
30};
31
32export type ThreadHistoryRegistration = {
33 readonly environmentId: EnvironmentId;
34 readonly threadId: ThreadId;
35 readonly handler: ThreadHistoryHandler;
36};
37
38export class ThreadHistoryController extends Context.Service<
39 ThreadHistoryController,
40 {
41 readonly register: (
42 environmentId: EnvironmentId,
43 threadId: ThreadId,
44 handler: ThreadHistoryHandler,
45 ) => Effect.Effect<ThreadHistoryRegistration>;
46 readonly unregister: (registration: ThreadHistoryRegistration) => Effect.Effect<void>;
47 readonly loadEarlier: (
48 environmentId: EnvironmentId,
49 threadId: ThreadId,
50 throughEntryId?: string,
51 workAnchorId?: string,
52 range?: ThreadHistoryRangeRequest,
53 ) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
54 readonly reconcilePendingWork: (
55 environmentId: EnvironmentId,
56 threadId: ThreadId,
57 ) => Effect.Effect<void>;
58 }
59>()("@t3tools/client-runtime/state/threadHistoryController") {}
60
61export const layer: Layer.Layer<ThreadHistoryController> = Layer.effect(
62 ThreadHistoryController,
63 Effect.gen(function* () {
64 const handlers = yield* Ref.make(new Map<string, ThreadHistoryHandler>());
65 return ThreadHistoryController.of({
66 register: Effect.fn("ThreadHistoryController.register")(
67 function (environmentId, threadId, handler) {
68 return Ref.update(handlers, (current) => {
69 const next = new Map(current);
70 next.set(threadKey({ environmentId, threadId }), handler);
71 return next;
72 }).pipe(
73 Effect.as({
74 environmentId,
75 threadId,
76 handler,
77 } satisfies ThreadHistoryRegistration),
78 );
79 },
80 ),
81 unregister: Effect.fn("ThreadHistoryController.unregister")(function (registration) {
82 return Ref.update(handlers, (current) => {
83 const key = threadKey({
84 environmentId: registration.environmentId,
85 threadId: registration.threadId,
86 });
87 // Only remove when this registration still owns the slot. An older
88 // fiber finalizer must not delete a newer handler for the same key.
89 if (current.get(key) !== registration.handler) {
90 return current;
91 }
92 const next = new Map(current);
93 next.delete(key);
94 return next;
95 });
96 }),
97 loadEarlier: Effect.fn("ThreadHistoryController.loadEarlier")(
98 function (environmentId, threadId, throughEntryId, workAnchorId, range) {
99 return Ref.get(handlers).pipe(
100 Effect.flatMap((current) => {
101 const handler = current.get(threadKey({ environmentId, threadId }));
102 if (handler === undefined) {
103 return Effect.succeed({ _tag: "noop" } satisfies ThreadHistoryLoadEarlierResult);
104 }
105 return handler.loadEarlier(throughEntryId, workAnchorId, range);
106 }),
107 );
108 },
109 ),
110 reconcilePendingWork: Effect.fn("ThreadHistoryController.reconcilePendingWork")(
111 function (environmentId, threadId) {
112 return Ref.get(handlers).pipe(
113 Effect.flatMap(
114 (current) =>
115 current.get(threadKey({ environmentId, threadId }))?.reconcilePendingWork() ??
116 Effect.void,
117 ),
118 );
119 },
120 ),
121 });
122 }),
123);