+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
137 lines
5.7 KiB
TypeScript
137 lines
5.7 KiB
TypeScript
import { useAtomValue } from "@effect/atom-react";
|
|
import { enabledEnvironmentIds } from "@t3tools/client-runtime/state/connections";
|
|
import { arrayElementsEqual } from "@t3tools/client-runtime/state/entities";
|
|
import {
|
|
createEnvironmentThreadDetailAtoms,
|
|
createEnvironmentThreadShellAtoms,
|
|
createEnvironmentThreadStateAtoms,
|
|
EMPTY_ENVIRONMENT_THREAD_STATE,
|
|
type EnvironmentThreadState,
|
|
createThreadEnvironmentAtoms,
|
|
} from "@t3tools/client-runtime/state/threads";
|
|
import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts";
|
|
import * as Option from "effect/Option";
|
|
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
|
|
|
import { environmentCatalog } from "../connection/catalog";
|
|
import { connectionAtomRuntime } from "../connection/runtime";
|
|
import { environmentSnapshotAtom } from "./shell";
|
|
|
|
export const threadEnvironment = createThreadEnvironmentAtoms(
|
|
connectionAtomRuntime,
|
|
environmentSnapshotAtom,
|
|
);
|
|
const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
|
|
export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
|
|
environmentThreads.stateAtom,
|
|
);
|
|
export const environmentThreadShells = createEnvironmentThreadShellAtoms({
|
|
catalogValueAtom: environmentCatalog.catalogValueAtom,
|
|
snapshotAtom: threadEnvironment.snapshotAtom,
|
|
});
|
|
|
|
const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
|
|
Atom.withLabel("web-environment-thread:empty"),
|
|
);
|
|
|
|
export function useEnvironmentThread(
|
|
environmentId: EnvironmentId | null,
|
|
threadId: ThreadId | null,
|
|
): EnvironmentThreadState {
|
|
const result = useAtomValue(
|
|
environmentId !== null && threadId !== null
|
|
? environmentThreads.stateAtom(environmentId, threadId)
|
|
: EMPTY_THREAD_STATE_ATOM,
|
|
);
|
|
const state = Option.getOrElse(
|
|
AsyncResult.value(result),
|
|
() => EMPTY_ENVIRONMENT_THREAD_STATE,
|
|
) as EnvironmentThreadState;
|
|
return state;
|
|
}
|
|
|
|
const isRunning = (status: string) =>
|
|
status === "preparing" || status === "starting" || status === "running";
|
|
|
|
type KeptThreads = ReadonlyMap<EnvironmentId, ReadonlySet<ThreadId>>;
|
|
|
|
// True once a thread's own stream no longer needs to stay open: it is in sync
|
|
// and shows a settled session, or it cannot progress (deleted or failed). A
|
|
// stream that is still loading or reconnecting keeps waiting for the stop.
|
|
function isDetailDone<E>(result: AsyncResult.AsyncResult<EnvironmentThreadState, E>): boolean {
|
|
if (!AsyncResult.isSuccess(result)) return true;
|
|
const { status, data, error } = result.value;
|
|
if (status === "deleted" || Option.isSome(error)) return true;
|
|
return (
|
|
status === "live" &&
|
|
!Option.exists(data, (thread) => thread.runs.some((run) => isRunning(run.status)))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Keeps the thread state atom mounted for each running thread in the listed
|
|
* environments. Mount the result; its value is only bookkeeping.
|
|
*
|
|
* The shell and detail streams are independent, so the shell can report a
|
|
* stop before the detail loads or catches up. A stopped thread stays mounted
|
|
* until its own detail is live and shows the stop too. Then the stream closes
|
|
* and saves the settled state to disk.
|
|
*/
|
|
export function createRunningThreadKeepAliveAtom<E>(input: {
|
|
readonly environmentIdsAtom: Atom.Atom<ReadonlyArray<EnvironmentId>>;
|
|
readonly threadsAtom: (
|
|
environmentId: EnvironmentId,
|
|
) => Atom.Atom<ReadonlyArray<Pick<OrchestrationV2ThreadShell, "id" | "status">>>;
|
|
readonly stateAtom: (
|
|
environmentId: EnvironmentId,
|
|
threadId: ThreadId,
|
|
) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentThreadState, E>>;
|
|
}) {
|
|
// Keeps its identity until a thread starts or stops, so ordinary shell
|
|
// updates do not rebuild the keep-alive set.
|
|
const runningThreadIdsAtom = Atom.family((environmentId: EnvironmentId) => {
|
|
let previous: ReadonlyArray<ThreadId> = [];
|
|
return Atom.make((get) => {
|
|
const running = get(input.threadsAtom(environmentId)).flatMap((thread) =>
|
|
isRunning(thread.status) ? [thread.id] : [],
|
|
);
|
|
if (arrayElementsEqual(previous, running)) return previous;
|
|
previous = running;
|
|
return running;
|
|
}).pipe(Atom.withLabel(`web-running-thread-ids:${environmentId}`));
|
|
});
|
|
|
|
return Atom.make((get): KeptThreads => {
|
|
const previous = Option.getOrUndefined(get.self<KeptThreads>());
|
|
const kept = new Map<EnvironmentId, ReadonlySet<ThreadId>>();
|
|
// An environment that leaves the list is not visited, so its mounts drop.
|
|
for (const environmentId of get(input.environmentIdsAtom)) {
|
|
const threadIds = new Set(get(runningThreadIdsAtom(environmentId)));
|
|
for (const threadId of previous?.get(environmentId) ?? []) {
|
|
if (threadIds.has(threadId)) continue;
|
|
const stateAtom = input.stateAtom(environmentId, threadId);
|
|
// `once`, not `get`: a dependency on a stopped thread would hold its
|
|
// stream open until some other change rebuilds this atom.
|
|
if (isDetailDone(get.once(stateAtom))) continue;
|
|
threadIds.add(threadId);
|
|
// Rebuild when this detail is done, not on each update.
|
|
get.subscribe(stateAtom, (state) => {
|
|
if (isDetailDone(state)) get.refreshSelf();
|
|
});
|
|
}
|
|
for (const threadId of threadIds) get.mount(input.stateAtom(environmentId, threadId));
|
|
kept.set(environmentId, threadIds);
|
|
}
|
|
return kept;
|
|
}).pipe(Atom.withLabel("web-running-thread-keep-alive"));
|
|
}
|
|
|
|
/** Mounted by `RunningThreadKeepAlive` on desktop, for every enabled environment. */
|
|
export const runningThreadKeepAliveAtom = createRunningThreadKeepAliveAtom({
|
|
environmentIdsAtom: Atom.map(environmentCatalog.catalogValueAtom, (catalog) => [
|
|
...enabledEnvironmentIds(catalog),
|
|
]),
|
|
threadsAtom: environmentThreadShells.environmentThreadsAtom,
|
|
stateAtom: environmentThreads.stateAtom,
|
|
});
|