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

entities.ts

266 lines9.6 KB

1import { useAtomValue } from "@effect/atom-react";
2import type {
3 EnvironmentProject,
4 EnvironmentThread,
5 EnvironmentThreadShell,
6} from "@t3tools/client-runtime/state/shell";
7import {
8 EMPTY_THREAD_HISTORY_META,
9 type EnvironmentThreadStatus,
10 type ThreadHistoryMeta,
11} from "@t3tools/client-runtime/state/threads";
12import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/contracts";
13import type { EnvironmentId, OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts";
14import { Atom } from "effect/unstable/reactivity";
15import { appAtomRegistry } from "../rpc/atomRegistry";
16import { environmentProjects } from "./projects";
17import { environmentServerConfigsAtom } from "./server";
18import {
19 allEnvironmentProjectSnapshotsReadyAtom,
20 allEnvironmentShellsBootstrappedAtom,
21} from "./shell";
22import { environmentThreadDetails, environmentThreadShells } from "./threads";
23import { waitForAtomValue } from "./waitForAtomValue";
24​
25const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
26const EMPTY_VISIBLE_TURN_ITEMS: ReadonlyArray<OrchestrationV2ProjectedTurnItem> = Object.freeze([]);
27​
28const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
29 Atom.withLabel("web-project:empty"),
30);
31const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
32 Atom.withLabel("web-thread-refs:empty"),
33);
34const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
35 Atom.withLabel("web-thread-shell:empty"),
36);
37const EMPTY_THREAD_PROJECTION_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
38 Atom.withLabel("web-thread-projection:empty"),
39);
40const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe(
41 Atom.withLabel("web-thread-status:empty"),
42);
43const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
44 Atom.withLabel("web-thread-visible-turn-items:empty"),
45);
46const EMPTY_THREAD_HISTORY_ATOM = Atom.make<ThreadHistoryMeta>(EMPTY_THREAD_HISTORY_META).pipe(
47 Atom.withLabel("web-thread-history:empty"),
48);
49​
50export const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
51 Atom.keepAlive,
52 Atom.withLabel("web-active-environment-id"),
53);
54​
55export function useActiveEnvironmentId(): EnvironmentId | null {
56 return useAtomValue(activeEnvironmentIdAtom);
58​
59export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
60 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
62​
63export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
64 return useAtomValue(environmentThreadShells.threadRefsAtom);
66​
67export function useEnvironmentThreadRefs(
68 environmentId: EnvironmentId | null,
69): ReadonlyArray<ScopedThreadRef> {
70 return useAtomValue(
71 environmentId === null
72 ? EMPTY_THREAD_REFS_ATOM
73 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
74 );
76​
77export function useProjects(): ReadonlyArray<EnvironmentProject> {
78 return useAtomValue(environmentProjects.projectsAtom);
80​
81export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
82 return useAtomValue(environmentServerConfigsAtom);
84​
85export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
86 return useAtomValue(environmentThreadShells.threadShellsAtom);
88​
89export function useAllEnvironmentShellsBootstrapped(): boolean {
90 return useAtomValue(allEnvironmentShellsBootstrappedAtom);
92​
93export function useAllEnvironmentProjectSnapshotsReady(): boolean {
94 return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom);
96​
97export function useThreadShellsForProjectRefs(
98 refs: ReadonlyArray<ScopedProjectRef>,
99): ReadonlyArray<EnvironmentThreadShell> {
100 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
102​
103export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
104 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
106​
107export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
108 return useAtomValue(
109 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
110 );
112​
113export function useThreadProjection(ref: ScopedThreadRef | null): EnvironmentThread | null {
114 return useAtomValue(
115 ref === null ? EMPTY_THREAD_PROJECTION_ATOM : environmentThreadDetails.threadAtom(ref),
116 );
118​
119export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
120 return useAtomValue(
121 ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),
122 );
124​
125export function useThreadHistory(ref: ScopedThreadRef | null): ThreadHistoryMeta {
126 return useAtomValue(
127 ref === null ? EMPTY_THREAD_HISTORY_ATOM : environmentThreadDetails.historyAtom(ref),
128 );
130​
131export function resolveThreadDetailRef(
132 ref: ScopedThreadRef | null,
133 options: {
134 shellExists: boolean;
135 waitForShell: boolean;
136 },
137): ScopedThreadRef | null {
138 return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
140​
141export function useThreadVisibleTurnItems(
142 ref: ScopedThreadRef | null,
143): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
144 return useAtomValue(
145 ref === null
146 ? EMPTY_VISIBLE_TURN_ITEMS_ATOM
147 : environmentThreadDetails.visibleTurnItemsAtom(ref),
148 );
150​
151export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
152 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
154​
155export function readProjects(): ReadonlyArray<EnvironmentProject> {
156 return appAtomRegistry.get(environmentProjects.projectsAtom);
158​
159/** Resolves when the project event reaches the live client store. */
160export function waitForProject(
161 ref: ScopedProjectRef,
162 timeoutMs = 10_000,
163): Promise<EnvironmentProject> {
164 const current = readProject(ref);
165 if (current !== null) return Promise.resolve(current);
166​
167 return new Promise((resolve, reject) => {
168 let unsubscribe: (() => void) | null = null;
169 const timeout = setTimeout(() => {
170 unsubscribe?.();
171 reject(new Error("The project did not appear in the desktop app."));
172 }, timeoutMs);
173 const finish = (project: EnvironmentProject | null) => {
174 if (project === null) return;
175 clearTimeout(timeout);
176 unsubscribe?.();
177 resolve(project);
178 };
179 unsubscribe = appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), finish);
180 finish(readProject(ref));
181 });
183​
184export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
185 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
187​
188export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> {
189 return waitForAtomValue({
190 registry: appAtomRegistry,
191 atom: environmentThreadShells.threadShellAtom(ref),
192 predicate: (thread) => thread !== null,
193 timeoutMs,
194 });
196​
197/** Whether the environment's server understands thread.pin/unpin.
198 Same version-skew contract as settlement. */
199export function readEnvironmentSupportsPinning(environmentId: EnvironmentId): boolean {
200 return (
201 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
202 .threadPinning === true
203 );
205​
206/** Whether the environment's server understands thread title regeneration.
207 Same version-skew contract as settlement. */
208export function readEnvironmentSupportsTitleRegeneration(environmentId: EnvironmentId): boolean {
209 return (
210 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
211 .threadTitleRegeneration === true
212 );
214​
215/** Whether the environment's server understands thread.pin.reorder (and
216 orderKey on thread.pin). Same version-skew contract as settlement. */
217export function readEnvironmentSupportsPinReorder(environmentId: EnvironmentId): boolean {
218 return (
219 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
220 .threadPinReorder === true
221 );
223​
224export function readThreadProjection(ref: ScopedThreadRef): EnvironmentThread | null {
225 return appAtomRegistry.get(environmentThreadDetails.threadAtom(ref));
227​
228/** Whether the environment's server understands thread.settle/unsettle.
229 False for pre-settlement servers (capability defaults false on decode),
230 so clients under version skew fall back instead of erroring. */
231export function readEnvironmentSupportsSettlement(environmentId: EnvironmentId): boolean {
232 return (
233 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
234 .threadSettlement === true
235 );
237​
238/** Whether the environment's server understands thread.snooze/unsnooze.
239 Same version-skew contract as settlement. */
240export function readEnvironmentSupportsSnooze(environmentId: EnvironmentId): boolean {
241 return (
242 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
243 .threadSnooze === true
244 );
246​
247/** Whether the environment's server understands thread.visit/mark-unread and
248 projects lastVisitedAt on thread shells. Same version-skew contract as
249 settlement: against older servers, clients keep the browser-local visited
250 state instead. */
251export function readEnvironmentSupportsVisitedTracking(environmentId: EnvironmentId): boolean {
252 return (
253 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
254 .threadVisitedTracking === true
255 );
257​
258export function readEnvironmentThreadRefs(
259 environmentId: EnvironmentId,
260): ReadonlyArray<ScopedThreadRef> {
261 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
263​
264export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
265 return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);