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

orchestration.ts

117 lines5.1 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 SubscriptionRef from "effect/SubscriptionRef";
5import type { HttpClient } from "effect/http";
6import { Atom } from "effect/reactivity";
7​
8import {
9 createEnvironmentRpcCommand,
10 createEnvironmentRpcQueryAtomFamily,
11 createEnvironmentRpcSubscriptionAtomFamily,
12 createEnvironmentCommand,
13} from "./runtime.ts";
14import type { EnvironmentRegistry } from "../connection/registry.ts";
15import * as EnvironmentSupervisor from "../connection/supervisor.ts";
16import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
17import * as ManagedRelayDpopSigner from "../relay/managedRelay.ts";
18import { EnvironmentRpcUnavailableError } from "../rpc/client.ts";
19import { fetchEnvironmentThreadTranscript } from "./threadTranscriptHttp.ts";
20​
21export function createOrchestrationEnvironmentAtoms<R, E>(
22 runtime: Atom.AtomRuntime<EnvironmentRegistry | HttpClient.HttpClient | R, E>,
23) {
24 return {
25 threadTranscript: createEnvironmentCommand(runtime, {
26 label: "environment-data:orchestration:thread-transcript",
27 execute: (input: { readonly threadId: ThreadId }) =>
28 Effect.gen(function* () {
29 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
30 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
31 if (Option.isNone(prepared)) {
32 return yield* new EnvironmentRpcUnavailableError({
33 environmentId: supervisor.target.environmentId,
34 message: "The source environment is not connected.",
35 });
36 }
37 return yield* fetchEnvironmentThreadTranscript({
38 prepared: prepared.value,
39 threadId: input.threadId,
40 signer: yield* Effect.serviceOption(ManagedRelayDpopSigner.ManagedRelayDpopSigner),
41 remoteAuthorization: yield* Effect.serviceOption(
42 RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
43 ),
44 });
45 }),
46 }),
47 v2: {
48 dispatchCommand: createEnvironmentRpcCommand(runtime, {
49 label: "environment-data:orchestration-v2:dispatch-command",
50 tag: ORCHESTRATION_V2_WS_METHODS.dispatchCommand,
51 }),
52 threadProjection: createEnvironmentRpcQueryAtomFamily(runtime, {
53 label: "environment-data:orchestration-v2:thread-projection",
54 tag: ORCHESTRATION_V2_WS_METHODS.getThreadProjection,
55 staleTimeMs: 0,
56 idleTtlMs: 0,
57 }),
58 shell: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
59 label: "environment-data:orchestration-v2:shell",
60 tag: ORCHESTRATION_V2_WS_METHODS.subscribeShell,
61 }),
62 thread: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
63 label: "environment-data:orchestration-v2:thread",
64 tag: ORCHESTRATION_V2_WS_METHODS.subscribeThread,
65 idleTtlMs: 0,
66 }),
67 },
68 turnDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
69 label: "environment-data:orchestration:turn-diff",
70 tag: ORCHESTRATION_V2_WS_METHODS.getTurnDiff,
71 }),
72 workflowScript: createEnvironmentRpcQueryAtomFamily(runtime, {
73 label: "environment-data:orchestration:workflow-script",
74 tag: ORCHESTRATION_V2_WS_METHODS.getWorkflowScript,
75 // Scripts are immutable per run: cache generously.
76 staleTimeMs: 300_000,
77 idleTtlMs: 300_000,
78 }),
79 stopWorkflow: createEnvironmentRpcCommand(runtime, {
80 label: "environment-data:orchestration:stop-workflow",
81 tag: ORCHESTRATION_V2_WS_METHODS.stopWorkflow,
82 }),
83 // Keyed by the item revision, so a live row refetches as its output grows.
84 turnItem: createEnvironmentRpcQueryAtomFamily(runtime, {
85 label: "environment-data:orchestration:turn-item",
86 tag: ORCHESTRATION_V2_WS_METHODS.getTurnItem,
87 staleTimeMs: 60_000,
88 idleTtlMs: 60_000,
89 }),
90 fullThreadDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
91 label: "environment-data:orchestration:full-thread-diff",
92 tag: ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff,
93 }),
94 threadFind: createEnvironmentRpcQueryAtomFamily(runtime, {
95 label: "environment-data:orchestration:thread-find",
96 tag: ORCHESTRATION_V2_WS_METHODS.searchThread,
97 staleTimeMs: 0,
98 idleTtlMs: 0,
99 }),
100 threadFindProgressive: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
101 label: "environment-data:orchestration:thread-find-progressive",
102 tag: ORCHESTRATION_V2_WS_METHODS.searchThreadStream,
103 completeWhen: (result) => result.complete !== false,
104 idleTtlMs: 0,
105 }),
106 threadSearch: createEnvironmentRpcQueryAtomFamily(runtime, {
107 label: "environment-data:orchestration:thread-search",
108 tag: ORCHESTRATION_V2_WS_METHODS.searchThreads,
109 staleTimeMs: 30_000,
110 idleTtlMs: 60_000,
111 }),
112 archivedShellSnapshot: createEnvironmentRpcQueryAtomFamily(runtime, {
113 label: "environment-data:orchestration:archived-shell-snapshot",
114 tag: ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot,
115 }),
116 };