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

orchestration.ts

132 lines5.7 KB

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