Open History to see the last commit
1import {
2 type GitActionProgressEvent,
3 type GitRunStackedActionInput,
4 type GitRunStackedActionResult,
5 type GitStatusResult,
6 type GitStatusStreamEvent,
7 type LocalApi,
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 "./protocol";
16import { resetWsReconnectBackoff } from "./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;
25}
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
42type RpcInputStreamMethod<TTag extends RpcTag> =
43 RpcMethod<TTag> extends (input: any, options?: any) => Stream.Stream<infer TEvent, any, any>
44 ? (
45 input: RpcInput<TTag>,
46 listener: (event: TEvent) => void,
47 options?: StreamSubscriptionOptions,
48 ) => () => void
49 : never;
50
51interface GitRunStackedActionOptions {
52 readonly onProgress?: (event: GitActionProgressEvent) => void;
53}
54
55export interface WsRpcClient {
56 readonly dispose: () => Promise<void>;
57 readonly reconnect: () => Promise<void>;
58 readonly terminal: {
59 readonly open: RpcUnaryMethod<typeof WS_METHODS.terminalOpen>;
60 readonly write: RpcUnaryMethod<typeof WS_METHODS.terminalWrite>;
61 readonly resize: RpcUnaryMethod<typeof WS_METHODS.terminalResize>;
62 readonly clear: RpcUnaryMethod<typeof WS_METHODS.terminalClear>;
63 readonly restart: RpcUnaryMethod<typeof WS_METHODS.terminalRestart>;
64 readonly close: RpcUnaryMethod<typeof WS_METHODS.terminalClose>;
65 readonly onEvent: RpcStreamMethod<typeof WS_METHODS.subscribeTerminalEvents>;
66 };
67 readonly projects: {
68 readonly searchEntries: RpcUnaryMethod<typeof WS_METHODS.projectsSearchEntries>;
69 readonly writeFile: RpcUnaryMethod<typeof WS_METHODS.projectsWriteFile>;
70 };
71 readonly filesystem: {
72 readonly browse: RpcUnaryMethod<typeof WS_METHODS.filesystemBrowse>;
73 };
74 readonly shell: {
75 readonly openInEditor: (input: {
76 readonly cwd: Parameters<LocalApi["shell"]["openInEditor"]>[0];
77 readonly editor: Parameters<LocalApi["shell"]["openInEditor"]>[1];
78 }) => ReturnType<LocalApi["shell"]["openInEditor"]>;
79 };
80 readonly git: {
81 readonly pull: RpcUnaryMethod<typeof WS_METHODS.gitPull>;
82 readonly refreshStatus: RpcUnaryMethod<typeof WS_METHODS.gitRefreshStatus>;
83 readonly onStatus: (
84 input: RpcInput<typeof WS_METHODS.subscribeGitStatus>,
85 listener: (status: GitStatusResult) => void,
86 options?: StreamSubscriptionOptions,
87 ) => () => void;
88 readonly runStackedAction: (
89 input: GitRunStackedActionInput,
90 options?: GitRunStackedActionOptions,
91 ) => Promise<GitRunStackedActionResult>;
92 readonly listBranches: RpcUnaryMethod<typeof WS_METHODS.gitListBranches>;
93 readonly createWorktree: RpcUnaryMethod<typeof WS_METHODS.gitCreateWorktree>;
94 readonly removeWorktree: RpcUnaryMethod<typeof WS_METHODS.gitRemoveWorktree>;
95 readonly createBranch: RpcUnaryMethod<typeof WS_METHODS.gitCreateBranch>;
96 readonly checkout: RpcUnaryMethod<typeof WS_METHODS.gitCheckout>;
97 readonly init: RpcUnaryMethod<typeof WS_METHODS.gitInit>;
98 readonly resolvePullRequest: RpcUnaryMethod<typeof WS_METHODS.gitResolvePullRequest>;
99 readonly preparePullRequestThread: RpcUnaryMethod<
100 typeof WS_METHODS.gitPreparePullRequestThread
101 >;
102 };
103 readonly server: {
104 readonly getConfig: RpcUnaryNoArgMethod<typeof WS_METHODS.serverGetConfig>;
105 readonly refreshProviders: RpcUnaryNoArgMethod<typeof WS_METHODS.serverRefreshProviders>;
106 readonly upsertKeybinding: RpcUnaryMethod<typeof WS_METHODS.serverUpsertKeybinding>;
107 readonly getSettings: RpcUnaryNoArgMethod<typeof WS_METHODS.serverGetSettings>;
108 readonly updateSettings: (
109 patch: ServerSettingsPatch,
110 ) => ReturnType<RpcUnaryMethod<typeof WS_METHODS.serverUpdateSettings>>;
111 readonly subscribeConfig: RpcStreamMethod<typeof WS_METHODS.subscribeServerConfig>;
112 readonly subscribeLifecycle: RpcStreamMethod<typeof WS_METHODS.subscribeServerLifecycle>;
113 readonly subscribeAuthAccess: RpcStreamMethod<typeof WS_METHODS.subscribeAuthAccess>;
114 };
115 readonly orchestration: {
116 readonly dispatchCommand: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.dispatchCommand>;
117 readonly getTurnDiff: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.getTurnDiff>;
118 readonly getFullThreadDiff: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.getFullThreadDiff>;
119 readonly subscribeShell: RpcStreamMethod<typeof ORCHESTRATION_WS_METHODS.subscribeShell>;
120 readonly subscribeThread: RpcInputStreamMethod<typeof ORCHESTRATION_WS_METHODS.subscribeThread>;
121 };
122}
123
124export function createWsRpcClient(transport: WsTransport): WsRpcClient {
125 return {
126 dispose: () => transport.dispose(),
127 reconnect: async () => {
128 resetWsReconnectBackoff();
129 await transport.reconnect();
130 },
131 terminal: {
132 open: (input) => transport.request((client) => client[WS_METHODS.terminalOpen](input)),
133 write: (input) => transport.request((client) => client[WS_METHODS.terminalWrite](input)),
134 resize: (input) => transport.request((client) => client[WS_METHODS.terminalResize](input)),
135 clear: (input) => transport.request((client) => client[WS_METHODS.terminalClear](input)),
136 restart: (input) => transport.request((client) => client[WS_METHODS.terminalRestart](input)),
137 close: (input) => transport.request((client) => client[WS_METHODS.terminalClose](input)),
138 onEvent: (listener, options) =>
139 transport.subscribe(
140 (client) => client[WS_METHODS.subscribeTerminalEvents]({}),
141 listener,
142 options,
143 ),
144 },
145 projects: {
146 searchEntries: (input) =>
147 transport.request((client) => client[WS_METHODS.projectsSearchEntries](input)),
148 writeFile: (input) =>
149 transport.request((client) => client[WS_METHODS.projectsWriteFile](input)),
150 },
151 filesystem: {
152 browse: (input) => transport.request((client) => client[WS_METHODS.filesystemBrowse](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 subscribeAuthAccess: (listener, options) =>
229 transport.subscribe(
230 (client) => client[WS_METHODS.subscribeAuthAccess]({}),
231 listener,
232 options,
233 ),
234 },
235 orchestration: {
236 dispatchCommand: (input) =>
237 transport.request((client) => client[ORCHESTRATION_WS_METHODS.dispatchCommand](input)),
238 getTurnDiff: (input) =>
239 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getTurnDiff](input)),
240 getFullThreadDiff: (input) =>
241 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getFullThreadDiff](input)),
242 subscribeShell: (listener, options) =>
243 transport.subscribe(
244 (client) => client[ORCHESTRATION_WS_METHODS.subscribeShell]({}),
245 listener,
246 options,
247 ),
248 subscribeThread: (input, listener, options) =>
249 transport.subscribe(
250 (client) => client[ORCHESTRATION_WS_METHODS.subscribeThread](input),
251 listener,
252 options,
253 ),
254 },
255 };
256}