Open History to see the last commit
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";
8
9const EMPTY_VISIBLE_TURN_ITEMS: OrchestrationV2ThreadProjection["visibleTurnItems"] = Object.freeze(
10 [],
11);
12
13export function createEnvironmentThreadDetailAtoms<E>(
14 threadStateAtom: (
15 environmentId: ScopedThreadRef["environmentId"],
16 threadId: ScopedThreadRef["threadId"],
17 ) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentThreadState, E>>,
18) {
19 const threadStateValueAtomFamily = Atom.family((key: string) => {
20 const ref = parseThreadKey(key);
21 return Atom.make((get) =>
22 Option.getOrElse(
23 AsyncResult.value(get(threadStateAtom(ref.environmentId, ref.threadId))),
24 () => EMPTY_ENVIRONMENT_THREAD_STATE,
25 ),
26 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-state-value:${key}`));
27 });
28
29 const threadAtomFamily = Atom.family((key: string) => {
30 const ref = parseThreadKey(key);
31 let previousProjection: OrchestrationV2ThreadProjection | null = null;
32 let previousValue: EnvironmentThread | null = null;
33 return Atom.make((get) => {
34 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
35 if (projection === previousProjection) return previousValue;
36 previousProjection = projection;
37 previousValue = projection === null ? null : { environmentId: ref.environmentId, projection };
38 return previousValue;
39 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread:${key}`));
40 });
41
42 const visibleTurnItemsAtomFamily = Atom.family((key: string) =>
43 Atom.make(
44 (get): OrchestrationV2ThreadProjection["visibleTurnItems"] =>
45 Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.visibleTurnItems ??
46 EMPTY_VISIBLE_TURN_ITEMS,
47 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-visible-turn-items:${key}`)),
48 );
49
50 const statusAtomFamily = Atom.family((key: string) =>
51 Atom.make((get) => get(threadStateValueAtomFamily(key)).status).pipe(
52 Atom.setIdleTTL(0),
53 Atom.withLabel(`environment-thread-status:${key}`),
54 ),
55 );
56 const errorAtomFamily = Atom.family((key: string) =>
57 Atom.make((get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).error)).pipe(
58 Atom.setIdleTTL(0),
59 Atom.withLabel(`environment-thread-error:${key}`),
60 ),
61 );
62 const historyAtomFamily = Atom.family((key: string) =>
63 Atom.make((get) => get(threadStateValueAtomFamily(key)).history).pipe(
64 Atom.setIdleTTL(0),
65 Atom.withLabel(`environment-thread-history:${key}`),
66 ),
67 );
68
69 return {
70 stateAtom: (ref: ScopedThreadRef) => threadStateValueAtomFamily(threadKey(ref)),
71 threadAtom: (ref: ScopedThreadRef) => threadAtomFamily(threadKey(ref)),
72 visibleTurnItemsAtom: (ref: ScopedThreadRef) => visibleTurnItemsAtomFamily(threadKey(ref)),
73 statusAtom: (ref: ScopedThreadRef) => statusAtomFamily(threadKey(ref)),
74 errorAtom: (ref: ScopedThreadRef) => errorAtomFamily(threadKey(ref)),
75 historyAtom: (ref: ScopedThreadRef) => historyAtomFamily(threadKey(ref)),
76 };
77}