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 "@workbench/contracts";
12import { applyGitStatusStreamEvent } from "@workbench/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 listEntries: RpcUnaryMethod<typeof WS_METHODS.projectsListEntries>;
69 readonly searchEntries: RpcUnaryMethod<typeof WS_METHODS.projectsSearchEntries>;
70 readonly readFile: RpcUnaryMethod<typeof WS_METHODS.projectsReadFile>;
71 readonly writeFile: RpcUnaryMethod<typeof WS_METHODS.projectsWriteFile>;
72 };
73 readonly filesystem: {
74 readonly browse: RpcUnaryMethod<typeof WS_METHODS.filesystemBrowse>;
75 };
76 readonly shell: {
77 readonly openInEditor: (input: {
78 readonly cwd: Parameters<LocalApi["shell"]["openInEditor"]>[0];
79 readonly editor: Parameters<LocalApi["shell"]["openInEditor"]>[1];
80 }) => ReturnType<LocalApi["shell"]["openInEditor"]>;
81 };
82 readonly git: {
83 readonly pull: RpcUnaryMethod<typeof WS_METHODS.gitPull>;
84 readonly refreshStatus: RpcUnaryMethod<typeof WS_METHODS.gitRefreshStatus>;
85 readonly onStatus: (
86 input: RpcInput<typeof WS_METHODS.subscribeGitStatus>,
87 listener: (status: GitStatusResult) => void,
88 options?: StreamSubscriptionOptions,
89 ) => () => void;
90 readonly runStackedAction: (
91 input: GitRunStackedActionInput,
92 options?: GitRunStackedActionOptions,
93 ) => Promise<GitRunStackedActionResult>;
94 readonly listBranches: RpcUnaryMethod<typeof WS_METHODS.gitListBranches>;
95 readonly createWorktree: RpcUnaryMethod<typeof WS_METHODS.gitCreateWorktree>;
96 readonly removeWorktree: RpcUnaryMethod<typeof WS_METHODS.gitRemoveWorktree>;
97 readonly createBranch: RpcUnaryMethod<typeof WS_METHODS.gitCreateBranch>;
98 readonly checkout: RpcUnaryMethod<typeof WS_METHODS.gitCheckout>;
99 readonly init: RpcUnaryMethod<typeof WS_METHODS.gitInit>;
100 readonly resolvePullRequest: RpcUnaryMethod<typeof WS_METHODS.gitResolvePullRequest>;
101 readonly preparePullRequestThread: RpcUnaryMethod<
102 typeof WS_METHODS.gitPreparePullRequestThread
103 >;
104 };
105 readonly server: {
106 readonly getConfig: RpcUnaryNoArgMethod<typeof WS_METHODS.serverGetConfig>;
107 readonly refreshProviders: RpcUnaryNoArgMethod<typeof WS_METHODS.serverRefreshProviders>;
108 readonly upsertKeybinding: RpcUnaryMethod<typeof WS_METHODS.serverUpsertKeybinding>;
109 readonly getSettings: RpcUnaryNoArgMethod<typeof WS_METHODS.serverGetSettings>;
110 readonly updateSettings: (
111 patch: ServerSettingsPatch,
112 ) => ReturnType<RpcUnaryMethod<typeof WS_METHODS.serverUpdateSettings>>;
113 readonly subscribeConfig: RpcStreamMethod<typeof WS_METHODS.subscribeServerConfig>;
114 readonly subscribeLifecycle: RpcStreamMethod<typeof WS_METHODS.subscribeServerLifecycle>;
115 readonly subscribeAuthAccess: RpcStreamMethod<typeof WS_METHODS.subscribeAuthAccess>;
116 };
117 readonly orchestration: {
118 readonly dispatchCommand: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.dispatchCommand>;
119 readonly getTurnDiff: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.getTurnDiff>;
120 readonly getFullThreadDiff: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.getFullThreadDiff>;
121 readonly subscribeShell: RpcStreamMethod<typeof ORCHESTRATION_WS_METHODS.subscribeShell>;
122 readonly subscribeThread: RpcInputStreamMethod<typeof ORCHESTRATION_WS_METHODS.subscribeThread>;
123 };
124}
125
126export function createWsRpcClient(transport: WsTransport): WsRpcClient {
127 return {
128 dispose: () => transport.dispose(),
129 reconnect: async () => {
130 resetWsReconnectBackoff();
131 await transport.reconnect();
132 },
133 terminal: {
134 open: (input) => transport.request((client) => client[WS_METHODS.terminalOpen](input)),
135 write: (input) => transport.request((client) => client[WS_METHODS.terminalWrite](input)),
136 resize: (input) => transport.request((client) => client[WS_METHODS.terminalResize](input)),
137 clear: (input) => transport.request((client) => client[WS_METHODS.terminalClear](input)),
138 restart: (input) => transport.request((client) => client[WS_METHODS.terminalRestart](input)),
139 close: (input) => transport.request((client) => client[WS_METHODS.terminalClose](input)),
140 onEvent: (listener, options) =>
141 transport.subscribe(
142 (client) => client[WS_METHODS.subscribeTerminalEvents]({}),
143 listener,
144 options,
145 ),
146 },
147 projects: {
148 listEntries: (input) =>
149 transport.request((client) => client[WS_METHODS.projectsListEntries](input)),
150 searchEntries: (input) =>
151 transport.request((client) => client[WS_METHODS.projectsSearchEntries](input)),
152 readFile: (input) =>
153 transport.request((client) => client[WS_METHODS.projectsReadFile](input)),
154 writeFile: (input) =>
155 transport.request((client) => client[WS_METHODS.projectsWriteFile](input)),
156 },
157 filesystem: {
158 browse: (input) => transport.request((client) => client[WS_METHODS.filesystemBrowse](input)),
159 },
160 shell: {
161 openInEditor: (input) =>
162 transport.request((client) => client[WS_METHODS.shellOpenInEditor](input)),
163 },
164 git: {
165 pull: (input) => transport.request((client) => client[WS_METHODS.gitPull](input)),
166 refreshStatus: (input) =>
167 transport.request((client) => client[WS_METHODS.gitRefreshStatus](input)),
168 onStatus: (input, listener, options) => {
169 let current: GitStatusResult | null = null;
170 return transport.subscribe(
171 (client) => client[WS_METHODS.subscribeGitStatus](input),
172 (event: GitStatusStreamEvent) => {
173 current = applyGitStatusStreamEvent(current, event);
174 listener(current);
175 },
176 options,
177 );
178 },
179 runStackedAction: async (input, options) => {
180 let result: GitRunStackedActionResult | null = null;
181
182 await transport.requestStream(
183 (client) => client[WS_METHODS.gitRunStackedAction](input),
184 (event) => {
185 options?.onProgress?.(event);
186 if (event.kind === "action_finished") {
187 result = event.result;
188 }
189 },
190 );
191
192 if (result) {
193 return result;
194 }
195
196 throw new Error("Git action stream completed without a final result.");
197 },
198 listBranches: (input) =>
199 transport.request((client) => client[WS_METHODS.gitListBranches](input)),
200 createWorktree: (input) =>
201 transport.request((client) => client[WS_METHODS.gitCreateWorktree](input)),
202 removeWorktree: (input) =>
203 transport.request((client) => client[WS_METHODS.gitRemoveWorktree](input)),
204 createBranch: (input) =>
205 transport.request((client) => client[WS_METHODS.gitCreateBranch](input)),
206 checkout: (input) => transport.request((client) => client[WS_METHODS.gitCheckout](input)),
207 init: (input) => transport.request((client) => client[WS_METHODS.gitInit](input)),
208 resolvePullRequest: (input) =>
209 transport.request((client) => client[WS_METHODS.gitResolvePullRequest](input)),
210 preparePullRequestThread: (input) =>
211 transport.request((client) => client[WS_METHODS.gitPreparePullRequestThread](input)),
212 },
213 server: {
214 getConfig: () => transport.request((client) => client[WS_METHODS.serverGetConfig]({})),
215 refreshProviders: () =>
216 transport.request((client) => client[WS_METHODS.serverRefreshProviders]({})),
217 upsertKeybinding: (input) =>
218 transport.request((client) => client[WS_METHODS.serverUpsertKeybinding](input)),
219 getSettings: () => transport.request((client) => client[WS_METHODS.serverGetSettings]({})),
220 updateSettings: (patch) =>
221 transport.request((client) => client[WS_METHODS.serverUpdateSettings]({ patch })),
222 subscribeConfig: (listener, options) =>
223 transport.subscribe(
224 (client) => client[WS_METHODS.subscribeServerConfig]({}),
225 listener,
226 options,
227 ),
228 subscribeLifecycle: (listener, options) =>
229 transport.subscribe(
230 (client) => client[WS_METHODS.subscribeServerLifecycle]({}),
231 listener,
232 options,
233 ),
234 subscribeAuthAccess: (listener, options) =>
235 transport.subscribe(
236 (client) => client[WS_METHODS.subscribeAuthAccess]({}),
237 listener,
238 options,
239 ),
240 },
241 orchestration: {
242 dispatchCommand: (input) =>
243 transport.request((client) => client[ORCHESTRATION_WS_METHODS.dispatchCommand](input)),
244 getTurnDiff: (input) =>
245 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getTurnDiff](input)),
246 getFullThreadDiff: (input) =>
247 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getFullThreadDiff](input)),
248 subscribeShell: (listener, options) =>
249 transport.subscribe(
250 (client) => client[ORCHESTRATION_WS_METHODS.subscribeShell]({}),
251 listener,
252 options,
253 ),
254 subscribeThread: (input, listener, options) =>
255 transport.subscribe(
256 (client) => client[ORCHESTRATION_WS_METHODS.subscribeThread](input),
257 listener,
258 options,
259 ),
260 },
261 };
262}