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

orchestration.ts

157 lines6.6 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 threadProjection: createEnvironmentRpcQueryAtomFamily(runtime, {
73 label: "environment-data:orchestration-v2:thread-projection",
74 tag: ORCHESTRATION_V2_WS_METHODS.getThreadProjection,
75 staleTimeMs: 0,
76 idleTtlMs: 0,
77 }),
78 shell: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
79 label: "environment-data:orchestration-v2:shell",
80 tag: ORCHESTRATION_V2_WS_METHODS.subscribeShell,
81 }),
82 thread: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
83 label: "environment-data:orchestration-v2:thread",
84 tag: ORCHESTRATION_V2_WS_METHODS.subscribeThread,
85 idleTtlMs: 0,
86 }),
87 // One expanded command row's output. Mounted only while the row is open,
88 // and released as soon as it closes, so hidden rows receive nothing.
89 commandOutput: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
90 label: "environment-data:orchestration-v2:command-output",
91 tag: ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput,
92 idleTtlMs: 0,
93 // `scan` emits its seed first; skipping it keeps the row "loading" until the first frame.
94 transform: (frames) =>
95 frames.pipe(
96 Stream.scan(() => EMPTY_COMMAND_OUTPUT, applyCommandOutputFrame),
97 Stream.filter((view) => view !== EMPTY_COMMAND_OUTPUT),
98 ),
99 }),
100 },
101 turnDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
102 label: "environment-data:orchestration:turn-diff",
103 tag: ORCHESTRATION_V2_WS_METHODS.getTurnDiff,
104 ...onCheckpointDiffAnswer,
105 }),
106 workflowScript: createEnvironmentRpcQueryAtomFamily(runtime, {
107 label: "environment-data:orchestration:workflow-script",
108 tag: ORCHESTRATION_V2_WS_METHODS.getWorkflowScript,
109 staleTimeMs: 300_000,
110 idleTtlMs: 300_000,
111 }),
112 stopWorkflow: createEnvironmentRpcCommand(runtime, {
113 label: "environment-data:orchestration:stop-workflow",
114 tag: ORCHESTRATION_V2_WS_METHODS.stopWorkflow,
115 }),
116 turnItem: createEnvironmentRpcQueryAtomFamily(runtime, {
117 label: "environment-data:orchestration:turn-item",
118 tag: ORCHESTRATION_V2_WS_METHODS.getTurnItem,
119 staleTimeMs: 60_000,
120 idleTtlMs: 60_000,
121 }),
122 fullThreadDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
123 label: "environment-data:orchestration:full-thread-diff",
124 tag: ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff,
125 ...onCheckpointDiffAnswer,
126 }),
127 threadFind: createEnvironmentRpcQueryAtomFamily(runtime, {
128 label: "environment-data:orchestration:thread-find",
129 tag: ORCHESTRATION_V2_WS_METHODS.searchThread,
130 staleTimeMs: 0,
131 idleTtlMs: 0,
132 }),
133 threadFindProgressive: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
134 label: "environment-data:orchestration:thread-find-progressive",
135 tag: ORCHESTRATION_V2_WS_METHODS.searchThreadStream,
136 completeWhen: (result) => result.complete !== false,
137 idleTtlMs: 0,
138 }),
139 threadSearch: createEnvironmentRpcQueryAtomFamily(runtime, {
140 label: "environment-data:orchestration:thread-search",
141 tag: ORCHESTRATION_V2_WS_METHODS.searchThreads,
142 staleTimeMs: 30_000,
143 idleTtlMs: 60_000,
144 }),
145 archivedShellSnapshot: createEnvironmentRpcQueryAtomFamily(runtime, {
146 label: "environment-data:orchestration:archived-shell-snapshot",
147 tag: ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot,
148 }),
149 // Read right before deleting a worktree, so never served from cache.
150 worktreeThreadIds: createEnvironmentRpcQueryAtomFamily(runtime, {
151 label: "environment-data:orchestration:worktree-thread-ids",
152 tag: ORCHESTRATION_V2_WS_METHODS.getWorktreeThreadIds,
153 staleTimeMs: 0,
154 idleTtlMs: 0,
155 }),
156 };