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: (throughEntryId?: string) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
17 readonly reconcilePendingWork: () => Effect.Effect<void>;
18};
19
20export type ThreadHistoryRegistration = {
21 readonly environmentId: EnvironmentId;
22 readonly threadId: ThreadId;
23 readonly handler: ThreadHistoryHandler;
24};
25
26export class ThreadHistoryController extends Context.Service<
27 ThreadHistoryController,
28 {
29 readonly register: (
30 environmentId: EnvironmentId,
31 threadId: ThreadId,
32 handler: ThreadHistoryHandler,
33 ) => Effect.Effect<ThreadHistoryRegistration>;
34 readonly unregister: (registration: ThreadHistoryRegistration) => Effect.Effect<void>;
35 readonly loadEarlier: (
36 environmentId: EnvironmentId,
37 threadId: ThreadId,
38 throughEntryId?: string,
39 ) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
40 readonly reconcilePendingWork: (
41 environmentId: EnvironmentId,
42 threadId: ThreadId,
43 ) => Effect.Effect<void>;
44 }
45>()("@t3tools/client-runtime/state/threadHistoryController") {}
46
47export const layer: Layer.Layer<ThreadHistoryController> = Layer.effect(
48 ThreadHistoryController,
49 Effect.gen(function* () {
50 const handlers = yield* Ref.make(new Map<string, ThreadHistoryHandler>());
51 return ThreadHistoryController.of({
52 register: (environmentId, threadId, handler) =>
53 Ref.update(handlers, (current) => {
54 const next = new Map(current);
55 next.set(threadKey({ environmentId, threadId }), handler);
56 return next;
57 }).pipe(
58 Effect.as({
59 environmentId,
60 threadId,
61 handler,
62 } satisfies ThreadHistoryRegistration),
63 ),
64 unregister: (registration) =>
65 Ref.update(handlers, (current) => {
66 const key = threadKey({
67 environmentId: registration.environmentId,
68 threadId: registration.threadId,
69 });
70 // Only remove when this registration still owns the slot. An older
71 // fiber finalizer must not delete a newer handler for the same key.
72 if (current.get(key) !== registration.handler) {
73 return current;
74 }
75 const next = new Map(current);
76 next.delete(key);
77 return next;
78 }),
79 loadEarlier: (environmentId, threadId, throughEntryId) =>
80 Ref.get(handlers).pipe(
81 Effect.flatMap((current) => {
82 const handler = current.get(threadKey({ environmentId, threadId }));
83 if (handler === undefined) {
84 return Effect.succeed({ _tag: "noop" } satisfies ThreadHistoryLoadEarlierResult);
85 }
86 return handler.loadEarlier(throughEntryId);
87 }),
88 ),
89 reconcilePendingWork: (environmentId, threadId) =>
90 Ref.get(handlers).pipe(
91 Effect.flatMap(
92 (current) =>
93 current.get(threadKey({ environmentId, threadId }))?.reconcilePendingWork() ??
94 Effect.void,
95 ),
96 ),
97 });
98 }),
99);