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

orchestration.ts

162 lines6.8 KB

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 // Mounted only while a subagent inspector is open; running children change between opens.
107 nativeChildWork: createEnvironmentRpcQueryAtomFamily(runtime, {
108 label: "environment-data:orchestration:native-child-work",
109 tag: ORCHESTRATION_V2_WS_METHODS.getNativeChildWork,
110 staleTimeMs: 0,
111 idleTtlMs: 0,
112 }),
113 stopWorkflow: createEnvironmentRpcCommand(runtime, {
114 label: "environment-data:orchestration:stop-workflow",
115 tag: ORCHESTRATION_V2_WS_METHODS.stopWorkflow,
116 }),
117 forceStopThread: createEnvironmentRpcCommand(runtime, {
118 label: "environment-data:orchestration:force-stop-thread",
119 tag: ORCHESTRATION_V2_WS_METHODS.forceStopThread,
120 }),
121 turnItem: createEnvironmentRpcQueryAtomFamily(runtime, {
122 label: "environment-data:orchestration:turn-item",
123 tag: ORCHESTRATION_V2_WS_METHODS.getTurnItem,
124 staleTimeMs: 60_000,
125 idleTtlMs: 60_000,
126 }),
127 fullThreadDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
128 label: "environment-data:orchestration:full-thread-diff",
129 tag: ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff,
130 ...onCheckpointDiffAnswer,
131 }),
132 threadFind: createEnvironmentRpcQueryAtomFamily(runtime, {
133 label: "environment-data:orchestration:thread-find",
134 tag: ORCHESTRATION_V2_WS_METHODS.searchThread,
135 staleTimeMs: 0,
136 idleTtlMs: 0,
137 }),
138 threadFindProgressive: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
139 label: "environment-data:orchestration:thread-find-progressive",
140 tag: ORCHESTRATION_V2_WS_METHODS.searchThreadStream,
141 completeWhen: (result) => result.complete !== false,
142 idleTtlMs: 0,
143 }),
144 threadSearch: createEnvironmentRpcQueryAtomFamily(runtime, {
145 label: "environment-data:orchestration:thread-search",
146 tag: ORCHESTRATION_V2_WS_METHODS.searchThreads,
147 staleTimeMs: 30_000,
148 idleTtlMs: 60_000,
149 }),
150 archivedShellSnapshot: createEnvironmentRpcQueryAtomFamily(runtime, {
151 label: "environment-data:orchestration:archived-shell-snapshot",
152 tag: ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot,
153 }),
154 // Read right before deleting a worktree, so never served from cache.
155 worktreeThreadIds: createEnvironmentRpcQueryAtomFamily(runtime, {
156 label: "environment-data:orchestration:worktree-thread-ids",
157 tag: ORCHESTRATION_V2_WS_METHODS.getWorktreeThreadIds,
158 staleTimeMs: 0,
159 idleTtlMs: 0,
160 }),
161 };