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