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

threadDetail.ts

84 lines3.6 KB

1import type { OrchestrationV2ThreadProjection, ScopedThreadRef } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import { AsyncResult, Atom } from "effect/unstable/reactivity";
4​
5import type { EnvironmentThread } from "./models.ts";
6import { EMPTY_ENVIRONMENT_THREAD_STATE, type EnvironmentThreadState } from "./threadState.ts";
7import { parseThreadKey, threadKey } from "./entities.ts";
8import { THREAD_STATE_IDLE_TTL_MS } from "./threadRetention.ts";
9​
10const EMPTY_VISIBLE_TURN_ITEMS: OrchestrationV2ThreadProjection["visibleTurnItems"] = Object.freeze(
11 [],
12);
13​
14export function createEnvironmentThreadDetailAtoms<E>(
15 threadStateAtom: (
16 environmentId: ScopedThreadRef["environmentId"],
17 threadId: ScopedThreadRef["threadId"],
18 ) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentThreadState, E>>,
19) {
20 const threadStateValueAtomFamily = Atom.family((key: string) => {
21 const ref = parseThreadKey(key);
22 return Atom.make((get) =>
23 Option.getOrElse(
24 AsyncResult.value(get(threadStateAtom(ref.environmentId, ref.threadId))),
25 () => EMPTY_ENVIRONMENT_THREAD_STATE,
26 ),
27 ).pipe(
28 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
29 Atom.withLabel(`environment-thread-state-value:${key}`),
30 );
31 });
32​
33 const threadAtomFamily = Atom.family((key: string) => {
34 const ref = parseThreadKey(key);
35 let previousProjection: OrchestrationV2ThreadProjection | null = null;
36 let previousValue: EnvironmentThread | null = null;
37 return Atom.make((get) => {
38 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
39 if (projection === previousProjection) return previousValue;
40 previousProjection = projection;
41 previousValue = projection === null ? null : { environmentId: ref.environmentId, projection };
42 return previousValue;
43 }).pipe(Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS), Atom.withLabel(`environment-thread:${key}`));
44 });
45​
46 const visibleTurnItemsAtomFamily = Atom.family((key: string) =>
47 Atom.make(
48 (get): OrchestrationV2ThreadProjection["visibleTurnItems"] =>
49 Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.visibleTurnItems ??
50 EMPTY_VISIBLE_TURN_ITEMS,
51 ).pipe(
52 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
53 Atom.withLabel(`environment-thread-visible-turn-items:${key}`),
54 ),
55 );
56​
57 const statusAtomFamily = Atom.family((key: string) =>
58 Atom.make((get) => get(threadStateValueAtomFamily(key)).status).pipe(
59 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
60 Atom.withLabel(`environment-thread-status:${key}`),
61 ),
62 );
63 const errorAtomFamily = Atom.family((key: string) =>
64 Atom.make((get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).error)).pipe(
65 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
66 Atom.withLabel(`environment-thread-error:${key}`),
67 ),
68 );
69 const historyAtomFamily = Atom.family((key: string) =>
70 Atom.make((get) => get(threadStateValueAtomFamily(key)).history).pipe(
71 Atom.setIdleTTL(THREAD_STATE_IDLE_TTL_MS),
72 Atom.withLabel(`environment-thread-history:${key}`),
73 ),
74 );
75​
76 return {
77 stateAtom: (ref: ScopedThreadRef) => threadStateValueAtomFamily(threadKey(ref)),
78 threadAtom: (ref: ScopedThreadRef) => threadAtomFamily(threadKey(ref)),
79 visibleTurnItemsAtom: (ref: ScopedThreadRef) => visibleTurnItemsAtomFamily(threadKey(ref)),
80 statusAtom: (ref: ScopedThreadRef) => statusAtomFamily(threadKey(ref)),
81 errorAtom: (ref: ScopedThreadRef) => errorAtomFamily(threadKey(ref)),
82 historyAtom: (ref: ScopedThreadRef) => historyAtomFamily(threadKey(ref)),
83 };