Open History to see the last commit
1import {
2 ORCHESTRATION_V2_WS_METHODS,
3 type EnvironmentId,
4 type ThreadId,
5 type OrchestrationGetTurnDiffResult,
6} from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import * as Option from "effect/Option";
9import * as Stream from "effect/Stream";
10import * as SubscriptionRef from "effect/SubscriptionRef";
11import type { HttpClient } from "effect/http";
12import { Atom } from "effect/reactivity";
13
14import { applyCommandOutputFrame, EMPTY_COMMAND_OUTPUT } from "./commandOutput.ts";
15
16import {
17 createEnvironmentRpcCommand,
18 createEnvironmentRpcQueryAtomFamily,
19 createEnvironmentRpcSubscriptionAtomFamily,
20 createEnvironmentCommand,
21 type QueryAnswerHandler,
22} from "./runtime.ts";
23import type { EnvironmentRegistry } from "../connection/registry.ts";
24import * as EnvironmentSupervisor from "../connection/supervisor.ts";
25import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
26import * as ManagedRelayDpopSigner from "../relay/managedRelay.ts";
27import { EnvironmentRpcUnavailableError } from "../rpc/client.ts";
28import type { EnvironmentShellState } from "./shell.ts";
29import { fetchEnvironmentThreadTranscript } from "./threadTranscriptHttp.ts";
30
31export function createOrchestrationEnvironmentAtoms<R, E>(
32 runtime: Atom.AtomRuntime<EnvironmentRegistry | HttpClient.HttpClient | R, E>,
33 options: {
34 /** Re-checks `threadTitles` when one of their threads leaves the shell. */
35 readonly shellState?: (
36 environmentId: EnvironmentId,
37 ) => Atom.Atom<Pick<EnvironmentShellState, "removedAt">>;
38 /** Runs on each turn-range and full-thread diff the server answers. */
39 readonly onCheckpointDiffAnswer?: QueryAnswerHandler<
40 { readonly ignoreWhitespace?: boolean },
41 OrchestrationGetTurnDiffResult
42 >;
43 } = {},
44) {
45 const onCheckpointDiffAnswer =
46 options.onCheckpointDiffAnswer === undefined
47 ? {}
48 : { onAnswer: options.onCheckpointDiffAnswer };
49 const shellState = options.shellState;
50 return {
51 threadTitles: createEnvironmentRpcQueryAtomFamily(runtime, {
52 label: "environment-data:orchestration:thread-titles",
53 tag: ORCHESTRATION_V2_WS_METHODS.getThreadTitles,
54 staleTimeMs: 0,
55 idleTtlMs: 0,
56 // Deleting a thread the shell already left out keeps these ids, so the query would not rerun.
57 // A removal can also be an archive, so it only prompts a re-check.
58 refreshTrigger: ({ environmentId, input }) => {
59 if (shellState === undefined) return undefined;
60 const state = shellState(environmentId);
61 return Atom.make((get) => {
62 const { removedAt } = get(state);
63 return Math.max(-1, ...input.threadIds.map((threadId) => removedAt.get(threadId) ?? -1));
64 });
65 },
66 }),
67 threadTranscript: createEnvironmentCommand(runtime, {
68 label: "environment-data:orchestration:thread-transcript",
69 execute: Effect.fn("orchestration.execute")(function* (input: {
70 readonly threadId: ThreadId;
71 }) {
72 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
73 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
74 if (Option.isNone(prepared)) {
75 return yield* new EnvironmentRpcUnavailableError({
76 environmentId: supervisor.target.environmentId,
77 message: "The source environment is not connected.",
78 });
79 }
80 return yield* fetchEnvironmentThreadTranscript({
81 prepared: prepared.value,
82 threadId: input.threadId,
83 signer: yield* Effect.serviceOption(ManagedRelayDpopSigner.ManagedRelayDpopSigner),
84 remoteAuthorization: yield* Effect.serviceOption(
85 RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
86 ),
87 });
88 }),
89 }),
90 v2: {
91 dispatchCommand: createEnvironmentRpcCommand(runtime, {
92 label: "environment-data:orchestration-v2:dispatch-command",
93 tag: ORCHESTRATION_V2_WS_METHODS.dispatchCommand,
94 }),
95 shell: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
96 label: "environment-data:orchestration-v2:shell",
97 tag: ORCHESTRATION_V2_WS_METHODS.subscribeShell,
98 }),
99 thread: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
100 label: "environment-data:orchestration-v2:thread",
101 tag: ORCHESTRATION_V2_WS_METHODS.subscribeThread,
102 idleTtlMs: 0,
103 }),
104 // One expanded command row's output. Mounted only while the row is open,
105 // and released as soon as it closes, so hidden rows receive nothing.
106 commandOutput: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
107 label: "environment-data:orchestration-v2:command-output",
108 tag: ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput,
109 idleTtlMs: 0,
110 // `scan` emits its seed first; skipping it keeps the row "loading" until the first frame.
111 transform: (frames) =>
112 frames.pipe(
113 Stream.scan(() => EMPTY_COMMAND_OUTPUT, applyCommandOutputFrame),
114 Stream.filter((view) => view !== EMPTY_COMMAND_OUTPUT),
115 ),
116 }),
117 },
118 turnDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
119 label: "environment-data:orchestration:turn-diff",
120 tag: ORCHESTRATION_V2_WS_METHODS.getTurnDiff,
121 ...onCheckpointDiffAnswer,
122 }),
123 workflowScript: createEnvironmentRpcQueryAtomFamily(runtime, {
124 label: "environment-data:orchestration:workflow-script",
125 tag: ORCHESTRATION_V2_WS_METHODS.getWorkflowScript,
126 staleTimeMs: 300_000,
127 idleTtlMs: 300_000,
128 }),
129 // Mounted only while a subagent inspector is open; running children change between opens.
130 nativeChildWork: createEnvironmentRpcQueryAtomFamily(runtime, {
131 label: "environment-data:orchestration:native-child-work",
132 tag: ORCHESTRATION_V2_WS_METHODS.getNativeChildWork,
133 staleTimeMs: 0,
134 idleTtlMs: 0,
135 }),
136 stopWorkflow: createEnvironmentRpcCommand(runtime, {
137 label: "environment-data:orchestration:stop-workflow",
138 tag: ORCHESTRATION_V2_WS_METHODS.stopWorkflow,
139 }),
140 forceStopThread: createEnvironmentRpcCommand(runtime, {
141 label: "environment-data:orchestration:force-stop-thread",
142 tag: ORCHESTRATION_V2_WS_METHODS.forceStopThread,
143 }),
144 turnItem: createEnvironmentRpcQueryAtomFamily(runtime, {
145 label: "environment-data:orchestration:turn-item",
146 tag: ORCHESTRATION_V2_WS_METHODS.getTurnItem,
147 staleTimeMs: 60_000,
148 idleTtlMs: 60_000,
149 }),
150 fullThreadDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
151 label: "environment-data:orchestration:full-thread-diff",
152 tag: ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff,
153 ...onCheckpointDiffAnswer,
154 }),
155 threadFind: createEnvironmentRpcQueryAtomFamily(runtime, {
156 label: "environment-data:orchestration:thread-find",
157 tag: ORCHESTRATION_V2_WS_METHODS.searchThread,
158 staleTimeMs: 0,
159 idleTtlMs: 0,
160 }),
161 threadFindProgressive: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
162 label: "environment-data:orchestration:thread-find-progressive",
163 tag: ORCHESTRATION_V2_WS_METHODS.searchThreadStream,
164 completeWhen: (result) => result.complete !== false,
165 idleTtlMs: 0,
166 }),
167 threadSearch: createEnvironmentRpcQueryAtomFamily(runtime, {
168 label: "environment-data:orchestration:thread-search",
169 tag: ORCHESTRATION_V2_WS_METHODS.searchThreads,
170 staleTimeMs: 30_000,
171 idleTtlMs: 60_000,
172 }),
173 archivedShellSnapshot: createEnvironmentRpcQueryAtomFamily(runtime, {
174 label: "environment-data:orchestration:archived-shell-snapshot",
175 tag: ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot,
176 }),
177 // Read right before deleting a worktree, so never served from cache.
178 worktreeThreadIds: createEnvironmentRpcQueryAtomFamily(runtime, {
179 label: "environment-data:orchestration:worktree-thread-ids",
180 tag: ORCHESTRATION_V2_WS_METHODS.getWorktreeThreadIds,
181 staleTimeMs: 0,
182 idleTtlMs: 0,
183 }),
184 };
185}