Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { deriveReportedModelSelection } from "@t3tools/client-runtime/state/thread-execution";
3
4import { appAtomRegistry } from "./atom-registry";
5import type {
6 EnvironmentProject,
7 EnvironmentThread,
8 EnvironmentThreadShell,
9} from "@t3tools/client-runtime/state/shell";
10import type {
11 EnvironmentId,
12 ScopedProjectRef,
13 ScopedThreadRef,
14 ServerConfig,
15 ThreadId,
16} from "@t3tools/contracts";
17import { hasThreadLinks, relabelThreadLinks } from "@t3tools/shared/threadLinks";
18import { Atom } from "effect/reactivity";
19import { useMemo } from "react";
20
21import { environmentProjects } from "./projects";
22import { environmentServerConfigsAtom, serverEnvironment } from "./server";
23import { environmentThreadDetails, environmentThreadShells } from "./threads";
24
25const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
26 Atom.withLabel("mobile-project:empty"),
27);
28const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
29 Atom.withLabel("mobile-thread-shell:empty"),
30);
31const EMPTY_SERVER_CONFIG_ATOM = Atom.make<ServerConfig | null>(null).pipe(
32 Atom.withLabel("mobile-server-config:empty"),
33);
34const EMPTY_THREAD_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
35 Atom.withLabel("mobile-thread:empty"),
36);
37const EMPTY_THREAD_TITLES: ReadonlyMap<ThreadId, string> = new Map();
38const EMPTY_THREAD_TITLES_ATOM = Atom.make(EMPTY_THREAD_TITLES).pipe(
39 Atom.withLabel("mobile-thread-titles:empty"),
40);
41
42const threadTitlesAtom = Atom.family((environmentId: EnvironmentId) => {
43 let previous = EMPTY_THREAD_TITLES;
44 return Atom.make((get) => {
45 const index = get(environmentThreadShells.environmentThreadIndexAtom(environmentId));
46 const unchanged =
47 index.size === previous.size &&
48 Array.from(index).every(([threadId, shell]) => previous.get(threadId) === shell.title);
49 if (!unchanged) {
50 previous = new Map(Array.from(index, ([threadId, shell]) => [threadId, shell.title]));
51 }
52 return previous;
53 }).pipe(Atom.withLabel(`mobile-thread-titles:${environmentId}`));
54});
55
56export function waitForProject(
57 ref: ScopedProjectRef,
58 timeoutMs = 10_000,
59): Promise<EnvironmentProject | null> {
60 const atom = environmentProjects.projectAtom(ref);
61 const current = appAtomRegistry.get(atom);
62 if (current !== null) return Promise.resolve(current);
63 return new Promise((resolve) => {
64 let unsubscribe: (() => void) | null = null;
65 const timeout = setTimeout(() => {
66 unsubscribe?.();
67 resolve(null);
68 }, timeoutMs);
69 const finish = (project: EnvironmentProject | null) => {
70 if (project === null) return;
71 clearTimeout(timeout);
72 unsubscribe?.();
73 resolve(project);
74 };
75 unsubscribe = appAtomRegistry.subscribe(atom, finish);
76 finish(appAtomRegistry.get(atom));
77 });
78}
79
80export function useProjects(): ReadonlyArray<EnvironmentProject> {
81 return useAtomValue(environmentProjects.projectsAtom);
82}
83
84export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
85 return useAtomValue(environmentThreadShells.threadShellsAtom);
86}
87
88export function useNavigationThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
89 return useAtomValue(environmentThreadShells.navigationThreadShellsAtom);
90}
91
92export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
93 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
94}
95
96export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
97 return useAtomValue(
98 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
99 );
100}
101
102/** Streaming timestamps, item counts, and the latest message can be stale during a run. */
103export function useQuietThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
104 return useAtomValue(
105 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.quietThreadShellAtom(ref),
106 );
107}
108
109export function useChildThreadInputs(ref: ScopedThreadRef) {
110 return useAtomValue(environmentThreadShells.childThreadInputsAtom(ref));
111}
112
113export function useLiveThreadLinkLabels(markdown: string, environmentId: EnvironmentId): string {
114 const titles = useAtomValue(
115 hasThreadLinks(markdown) ? threadTitlesAtom(environmentId) : EMPTY_THREAD_TITLES_ATOM,
116 );
117 return useMemo(
118 () =>
119 titles.size === 0
120 ? markdown
121 : relabelThreadLinks(markdown, (threadId) => titles.get(threadId)),
122 [markdown, titles],
123 );
124}
125
126export function useEnvironmentServerConfig(
127 environmentId: EnvironmentId | null,
128): ServerConfig | null {
129 return useAtomValue(
130 environmentId === null
131 ? EMPTY_SERVER_CONFIG_ATOM
132 : serverEnvironment.configValueAtom(environmentId),
133 );
134}
135
136export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
137 return useAtomValue(environmentServerConfigsAtom);
138}
139
140const selectReportedModelSelection = (thread: EnvironmentThread | null) =>
141 thread === null ? null : deriveReportedModelSelection(thread.projection);
142
143export function useThreadReportedModelSelection(ref: ScopedThreadRef | null) {
144 return useAtomValue(
145 ref === null ? EMPTY_THREAD_ATOM : environmentThreadDetails.threadAtom(ref),
146 selectReportedModelSelection,
147 );
148}