Open History to see the last commit
1import type { EnvironmentId, EnvironmentApi } from "@t3tools/contracts";
2
3import type { WsRpcClient } from "./rpc/wsRpcClient";
4import { readEnvironmentConnection } from "./environments/runtime";
5
6export function createEnvironmentApi(rpcClient: WsRpcClient): EnvironmentApi {
7 return {
8 terminal: {
9 open: (input) => rpcClient.terminal.open(input as never),
10 write: (input) => rpcClient.terminal.write(input as never),
11 resize: (input) => rpcClient.terminal.resize(input as never),
12 clear: (input) => rpcClient.terminal.clear(input as never),
13 restart: (input) => rpcClient.terminal.restart(input as never),
14 close: (input) => rpcClient.terminal.close(input as never),
15 onEvent: (callback) => rpcClient.terminal.onEvent(callback),
16 },
17 projects: {
18 searchEntries: rpcClient.projects.searchEntries,
19 writeFile: rpcClient.projects.writeFile,
20 },
21 git: {
22 pull: rpcClient.git.pull,
23 refreshStatus: rpcClient.git.refreshStatus,
24 onStatus: (input, callback, options) => rpcClient.git.onStatus(input, callback, options),
25 listBranches: rpcClient.git.listBranches,
26 createWorktree: rpcClient.git.createWorktree,
27 removeWorktree: rpcClient.git.removeWorktree,
28 createBranch: rpcClient.git.createBranch,
29 checkout: rpcClient.git.checkout,
30 init: rpcClient.git.init,
31 resolvePullRequest: rpcClient.git.resolvePullRequest,
32 preparePullRequestThread: rpcClient.git.preparePullRequestThread,
33 },
34 orchestration: {
35 getSnapshot: rpcClient.orchestration.getSnapshot,
36 dispatchCommand: rpcClient.orchestration.dispatchCommand,
37 getTurnDiff: rpcClient.orchestration.getTurnDiff,
38 getFullThreadDiff: rpcClient.orchestration.getFullThreadDiff,
39 replayEvents: (fromSequenceExclusive) =>
40 rpcClient.orchestration
41 .replayEvents({ fromSequenceExclusive })
42 .then((events) => [...events]),
43 onDomainEvent: (callback, options) =>
44 rpcClient.orchestration.onDomainEvent(callback, options),
45 },
46 };
47}
48
49export function readEnvironmentApi(environmentId: EnvironmentId): EnvironmentApi | undefined {
50 if (typeof window === "undefined") {
51 return undefined;
52 }
53
54 if (!environmentId) {
55 return undefined;
56 }
57
58 const connection = readEnvironmentConnection(environmentId);
59 return connection ? createEnvironmentApi(connection.client) : undefined;
60}
61
62export function ensureEnvironmentApi(environmentId: EnvironmentId): EnvironmentApi {
63 const api = readEnvironmentApi(environmentId);
64 if (!api) {
65 throw new Error(`Environment API not found for environment ${environmentId}`);
66 }
67 return api;
68}