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

orchestration.ts

131 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: (input: { readonly threadId: ThreadId }) =>
31 Effect.gen(function* () {
32 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
33 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
34 if (Option.isNone(prepared)) {
35 return yield* new EnvironmentRpcUnavailableError({
36 environmentId: supervisor.target.environmentId,
37 message: "The source environment is not connected.",
38 });
39 }
40 return yield* fetchEnvironmentThreadTranscript({
41 prepared: prepared.value,
42 threadId: input.threadId,
43 signer: yield* Effect.serviceOption(ManagedRelayDpopSigner.ManagedRelayDpopSigner),
44 remoteAuthorization: yield* Effect.serviceOption(
45 RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
46 ),
47 });
48 }),
49 }),
50 v2: {
51 dispatchCommand: createEnvironmentRpcCommand(runtime, {
52 label: "environment-data:orchestration-v2:dispatch-command",
53 tag: ORCHESTRATION_V2_WS_METHODS.dispatchCommand,
54 }),
55 threadProjection: createEnvironmentRpcQueryAtomFamily(runtime, {
56 label: "environment-data:orchestration-v2:thread-projection",
57 tag: ORCHESTRATION_V2_WS_METHODS.getThreadProjection,
58 staleTimeMs: 0,
59 idleTtlMs: 0,
60 }),
61 shell: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
62 label: "environment-data:orchestration-v2:shell",
63 tag: ORCHESTRATION_V2_WS_METHODS.subscribeShell,
64 }),
65 thread: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
66 label: "environment-data:orchestration-v2:thread",
67 tag: ORCHESTRATION_V2_WS_METHODS.subscribeThread,
68 idleTtlMs: 0,
69 }),
70 // One expanded command row's output. Mounted only while the row is open,
71 // and released as soon as it closes, so hidden rows receive nothing.
72 commandOutput: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
73 label: "environment-data:orchestration-v2:command-output",
74 tag: ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput,
75 idleTtlMs: 0,
76 // `scan` emits its seed first; skipping it keeps the row "loading" until the first frame.
77 transform: (frames) =>
78 frames.pipe(
79 Stream.scan(() => EMPTY_COMMAND_OUTPUT, applyCommandOutputFrame),
80 Stream.filter((view) => view !== EMPTY_COMMAND_OUTPUT),
81 ),
82 }),
83 },
84 turnDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
85 label: "environment-data:orchestration:turn-diff",
86 tag: ORCHESTRATION_V2_WS_METHODS.getTurnDiff,
87 }),
88 workflowScript: createEnvironmentRpcQueryAtomFamily(runtime, {
89 label: "environment-data:orchestration:workflow-script",
90 tag: ORCHESTRATION_V2_WS_METHODS.getWorkflowScript,
91 staleTimeMs: 300_000,
92 idleTtlMs: 300_000,
93 }),
94 stopWorkflow: createEnvironmentRpcCommand(runtime, {
95 label: "environment-data:orchestration:stop-workflow",
96 tag: ORCHESTRATION_V2_WS_METHODS.stopWorkflow,
97 }),
98 turnItem: createEnvironmentRpcQueryAtomFamily(runtime, {
99 label: "environment-data:orchestration:turn-item",
100 tag: ORCHESTRATION_V2_WS_METHODS.getTurnItem,
101 staleTimeMs: 60_000,
102 idleTtlMs: 60_000,
103 }),
104 fullThreadDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
105 label: "environment-data:orchestration:full-thread-diff",
106 tag: ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff,
107 }),
108 threadFind: createEnvironmentRpcQueryAtomFamily(runtime, {
109 label: "environment-data:orchestration:thread-find",
110 tag: ORCHESTRATION_V2_WS_METHODS.searchThread,
111 staleTimeMs: 0,
112 idleTtlMs: 0,
113 }),
114 threadFindProgressive: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
115 label: "environment-data:orchestration:thread-find-progressive",
116 tag: ORCHESTRATION_V2_WS_METHODS.searchThreadStream,
117 completeWhen: (result) => result.complete !== false,
118 idleTtlMs: 0,
119 }),
120 threadSearch: createEnvironmentRpcQueryAtomFamily(runtime, {
121 label: "environment-data:orchestration:thread-search",
122 tag: ORCHESTRATION_V2_WS_METHODS.searchThreads,
123 staleTimeMs: 30_000,
124 idleTtlMs: 60_000,
125 }),
126 archivedShellSnapshot: createEnvironmentRpcQueryAtomFamily(runtime, {
127 label: "environment-data:orchestration:archived-shell-snapshot",
128 tag: ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot,
129 }),
130 };