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

wsRpcClient.ts

250 lines10.6 KB

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