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

wsRpcClient.ts

207 lines9.4 KB

1import {
2 type GitActionProgressEvent,
3 type GitRunStackedActionInput,
4 type GitRunStackedActionResult,
5 type NativeApi,
6 ORCHESTRATION_WS_METHODS,
7 type ServerSettingsPatch,
8 WS_METHODS,
9} from "@t3tools/contracts";
10import { Effect, Stream } from "effect";
11​
12import { type WsRpcProtocolClient } from "./rpc/protocol";
13import { WsTransport } from "./wsTransport";
14​
15type RpcTag = keyof WsRpcProtocolClient & string;
16type RpcMethod<TTag extends RpcTag> = WsRpcProtocolClient[TTag];
17type RpcInput<TTag extends RpcTag> = Parameters<RpcMethod<TTag>>[0];
18​
19type RpcUnaryMethod<TTag extends RpcTag> =
20 RpcMethod<TTag> extends (input: any, options?: any) => Effect.Effect<infer TSuccess, any, any>
21 ? (input: RpcInput<TTag>) => Promise<TSuccess>
22 : never;
23​
24type RpcUnaryNoArgMethod<TTag extends RpcTag> =
25 RpcMethod<TTag> extends (input: any, options?: any) => Effect.Effect<infer TSuccess, any, any>
26 ? () => Promise<TSuccess>
27 : never;
28​
29type RpcStreamMethod<TTag extends RpcTag> =
30 RpcMethod<TTag> extends (input: any, options?: any) => Stream.Stream<infer TEvent, any, any>
31 ? (listener: (event: TEvent) => void) => () => void
32 : never;
33​
34interface GitRunStackedActionOptions {
35 readonly onProgress?: (event: GitActionProgressEvent) => void;
37​
38export interface WsRpcClient {
39 readonly dispose: () => Promise<void>;
40 readonly terminal: {
41 readonly open: RpcUnaryMethod<typeof WS_METHODS.terminalOpen>;
42 readonly write: RpcUnaryMethod<typeof WS_METHODS.terminalWrite>;
43 readonly resize: RpcUnaryMethod<typeof WS_METHODS.terminalResize>;
44 readonly clear: RpcUnaryMethod<typeof WS_METHODS.terminalClear>;
45 readonly restart: RpcUnaryMethod<typeof WS_METHODS.terminalRestart>;
46 readonly close: RpcUnaryMethod<typeof WS_METHODS.terminalClose>;
47 readonly onEvent: RpcStreamMethod<typeof WS_METHODS.subscribeTerminalEvents>;
48 };
49 readonly projects: {
50 readonly searchEntries: RpcUnaryMethod<typeof WS_METHODS.projectsSearchEntries>;
51 readonly writeFile: RpcUnaryMethod<typeof WS_METHODS.projectsWriteFile>;
52 };
53 readonly shell: {
54 readonly openInEditor: (input: {
55 readonly cwd: Parameters<NativeApi["shell"]["openInEditor"]>[0];
56 readonly editor: Parameters<NativeApi["shell"]["openInEditor"]>[1];
57 }) => ReturnType<NativeApi["shell"]["openInEditor"]>;
58 };
59 readonly git: {
60 readonly pull: RpcUnaryMethod<typeof WS_METHODS.gitPull>;
61 readonly status: RpcUnaryMethod<typeof WS_METHODS.gitStatus>;
62 readonly runStackedAction: (
63 input: GitRunStackedActionInput,
64 options?: GitRunStackedActionOptions,
65 ) => Promise<GitRunStackedActionResult>;
66 readonly listBranches: RpcUnaryMethod<typeof WS_METHODS.gitListBranches>;
67 readonly createWorktree: RpcUnaryMethod<typeof WS_METHODS.gitCreateWorktree>;
68 readonly removeWorktree: RpcUnaryMethod<typeof WS_METHODS.gitRemoveWorktree>;
69 readonly createBranch: RpcUnaryMethod<typeof WS_METHODS.gitCreateBranch>;
70 readonly checkout: RpcUnaryMethod<typeof WS_METHODS.gitCheckout>;
71 readonly init: RpcUnaryMethod<typeof WS_METHODS.gitInit>;
72 readonly resolvePullRequest: RpcUnaryMethod<typeof WS_METHODS.gitResolvePullRequest>;
73 readonly preparePullRequestThread: RpcUnaryMethod<
74 typeof WS_METHODS.gitPreparePullRequestThread
75 >;
76 };
77 readonly server: {
78 readonly getConfig: RpcUnaryNoArgMethod<typeof WS_METHODS.serverGetConfig>;
79 readonly refreshProviders: RpcUnaryNoArgMethod<typeof WS_METHODS.serverRefreshProviders>;
80 readonly upsertKeybinding: RpcUnaryMethod<typeof WS_METHODS.serverUpsertKeybinding>;
81 readonly getSettings: RpcUnaryNoArgMethod<typeof WS_METHODS.serverGetSettings>;
82 readonly updateSettings: (
83 patch: ServerSettingsPatch,
84 ) => ReturnType<RpcUnaryMethod<typeof WS_METHODS.serverUpdateSettings>>;
85 readonly subscribeConfig: RpcStreamMethod<typeof WS_METHODS.subscribeServerConfig>;
86 readonly subscribeLifecycle: RpcStreamMethod<typeof WS_METHODS.subscribeServerLifecycle>;
87 };
88 readonly orchestration: {
89 readonly getSnapshot: RpcUnaryNoArgMethod<typeof ORCHESTRATION_WS_METHODS.getSnapshot>;
90 readonly dispatchCommand: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.dispatchCommand>;
91 readonly getTurnDiff: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.getTurnDiff>;
92 readonly getFullThreadDiff: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.getFullThreadDiff>;
93 readonly replayEvents: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.replayEvents>;
94 readonly onDomainEvent: RpcStreamMethod<typeof WS_METHODS.subscribeOrchestrationDomainEvents>;
95 };
97​
98let sharedWsRpcClient: WsRpcClient | null = null;
99​
100export function getWsRpcClient(): WsRpcClient {
101 if (sharedWsRpcClient) {
102 return sharedWsRpcClient;
103 }
104 sharedWsRpcClient = createWsRpcClient();
105 return sharedWsRpcClient;
107​
108export async function __resetWsRpcClientForTests() {
109 await sharedWsRpcClient?.dispose();
110 sharedWsRpcClient = null;
112​
113export function createWsRpcClient(transport = new WsTransport()): WsRpcClient {
114 return {
115 dispose: () => transport.dispose(),
116 terminal: {
117 open: (input) => transport.request((client) => client[WS_METHODS.terminalOpen](input)),
118 write: (input) => transport.request((client) => client[WS_METHODS.terminalWrite](input)),
119 resize: (input) => transport.request((client) => client[WS_METHODS.terminalResize](input)),
120 clear: (input) => transport.request((client) => client[WS_METHODS.terminalClear](input)),
121 restart: (input) => transport.request((client) => client[WS_METHODS.terminalRestart](input)),
122 close: (input) => transport.request((client) => client[WS_METHODS.terminalClose](input)),
123 onEvent: (listener) =>
124 transport.subscribe((client) => client[WS_METHODS.subscribeTerminalEvents]({}), listener),
125 },
126 projects: {
127 searchEntries: (input) =>
128 transport.request((client) => client[WS_METHODS.projectsSearchEntries](input)),
129 writeFile: (input) =>
130 transport.request((client) => client[WS_METHODS.projectsWriteFile](input)),
131 },
132 shell: {
133 openInEditor: (input) =>
134 transport.request((client) => client[WS_METHODS.shellOpenInEditor](input)),
135 },
136 git: {
137 pull: (input) => transport.request((client) => client[WS_METHODS.gitPull](input)),
138 status: (input) => transport.request((client) => client[WS_METHODS.gitStatus](input)),
139 runStackedAction: async (input, options) => {
140 let result: GitRunStackedActionResult | null = null;
141​
142 await transport.requestStream(
143 (client) => client[WS_METHODS.gitRunStackedAction](input),
144 (event) => {
145 options?.onProgress?.(event);
146 if (event.kind === "action_finished") {
147 result = event.result;
148 }
149 },
150 );
151​
152 if (result) {
153 return result;
154 }
155​
156 throw new Error("Git action stream completed without a final result.");
157 },
158 listBranches: (input) =>
159 transport.request((client) => client[WS_METHODS.gitListBranches](input)),
160 createWorktree: (input) =>
161 transport.request((client) => client[WS_METHODS.gitCreateWorktree](input)),
162 removeWorktree: (input) =>
163 transport.request((client) => client[WS_METHODS.gitRemoveWorktree](input)),
164 createBranch: (input) =>
165 transport.request((client) => client[WS_METHODS.gitCreateBranch](input)),
166 checkout: (input) => transport.request((client) => client[WS_METHODS.gitCheckout](input)),
167 init: (input) => transport.request((client) => client[WS_METHODS.gitInit](input)),
168 resolvePullRequest: (input) =>
169 transport.request((client) => client[WS_METHODS.gitResolvePullRequest](input)),
170 preparePullRequestThread: (input) =>
171 transport.request((client) => client[WS_METHODS.gitPreparePullRequestThread](input)),
172 },
173 server: {
174 getConfig: () => transport.request((client) => client[WS_METHODS.serverGetConfig]({})),
175 refreshProviders: () =>
176 transport.request((client) => client[WS_METHODS.serverRefreshProviders]({})),
177 upsertKeybinding: (input) =>
178 transport.request((client) => client[WS_METHODS.serverUpsertKeybinding](input)),
179 getSettings: () => transport.request((client) => client[WS_METHODS.serverGetSettings]({})),
180 updateSettings: (patch) =>
181 transport.request((client) => client[WS_METHODS.serverUpdateSettings]({ patch })),
182 subscribeConfig: (listener) =>
183 transport.subscribe((client) => client[WS_METHODS.subscribeServerConfig]({}), listener),
184 subscribeLifecycle: (listener) =>
185 transport.subscribe((client) => client[WS_METHODS.subscribeServerLifecycle]({}), listener),
186 },
187 orchestration: {
188 getSnapshot: () =>
189 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getSnapshot]({})),
190 dispatchCommand: (input) =>
191 transport.request((client) => client[ORCHESTRATION_WS_METHODS.dispatchCommand](input)),
192 getTurnDiff: (input) =>
193 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getTurnDiff](input)),
194 getFullThreadDiff: (input) =>
195 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getFullThreadDiff](input)),
196 replayEvents: (input) =>
197 transport
198 .request((client) => client[ORCHESTRATION_WS_METHODS.replayEvents](input))
199 .then((events) => [...events]),
200 onDomainEvent: (listener) =>
201 transport.subscribe(
202 (client) => client[WS_METHODS.subscribeOrchestrationDomainEvents]({}),
203 listener,
204 ),
205 },
206 };