Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadHistoryController.ts

109 lines3.9 KB

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: Effect.fn("ThreadHistoryController.register")(
53 function (environmentId, threadId, handler) {
54 return Ref.update(handlers, (current) => {
55 const next = new Map(current);
56 next.set(threadKey({ environmentId, threadId }), handler);
57 return next;
58 }).pipe(
59 Effect.as({
60 environmentId,
61 threadId,
62 handler,
63 } satisfies ThreadHistoryRegistration),
64 );
65 },
66 ),
67 unregister: Effect.fn("ThreadHistoryController.unregister")(function (registration) {
68 return Ref.update(handlers, (current) => {
69 const key = threadKey({
70 environmentId: registration.environmentId,
71 threadId: registration.threadId,
72 });
73 // Only remove when this registration still owns the slot. An older
74 // fiber finalizer must not delete a newer handler for the same key.
75 if (current.get(key) !== registration.handler) {
76 return current;
77 }
78 const next = new Map(current);
79 next.delete(key);
80 return next;
81 });
82 }),
83 loadEarlier: Effect.fn("ThreadHistoryController.loadEarlier")(
84 function (environmentId, threadId, throughEntryId) {
85 return Ref.get(handlers).pipe(
86 Effect.flatMap((current) => {
87 const handler = current.get(threadKey({ environmentId, threadId }));
88 if (handler === undefined) {
89 return Effect.succeed({ _tag: "noop" } satisfies ThreadHistoryLoadEarlierResult);
90 }
91 return handler.loadEarlier(throughEntryId);
92 }),
93 );
94 },
95 ),
96 reconcilePendingWork: Effect.fn("ThreadHistoryController.reconcilePendingWork")(
97 function (environmentId, threadId) {
98 return Ref.get(handlers).pipe(
99 Effect.flatMap(
100 (current) =>
101 current.get(threadKey({ environmentId, threadId }))?.reconcilePendingWork() ??
102 Effect.void,
103 ),
104 );
105 },
106 ),
107 });
108 }),
109);