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
42interface GitRunStackedActionOptions {
43 readonly onProgress?: (event: GitActionProgressEvent) => void;
44}
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<LocalApi["shell"]["openInEditor"]>[0];
65 readonly editor: Parameters<LocalApi["shell"]["openInEditor"]>[1];
66 }) => ReturnType<LocalApi["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 readonly subscribeAuthAccess: RpcStreamMethod<typeof WS_METHODS.subscribeAuthAccess>;
102 };
103 readonly orchestration: {
104 readonly getSnapshot: RpcUnaryNoArgMethod<typeof ORCHESTRATION_WS_METHODS.getSnapshot>;
105 readonly dispatchCommand: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.dispatchCommand>;
106 readonly getTurnDiff: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.getTurnDiff>;
107 readonly getFullThreadDiff: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.getFullThreadDiff>;
108 readonly replayEvents: RpcUnaryMethod<typeof ORCHESTRATION_WS_METHODS.replayEvents>;
109 readonly onDomainEvent: RpcStreamMethod<typeof WS_METHODS.subscribeOrchestrationDomainEvents>;
110 };
111}
112
113export function createWsRpcClient(transport: WsTransport): WsRpcClient {
114 return {
115 dispose: () => transport.dispose(),
116 reconnect: async () => {
117 resetWsReconnectBackoff();
118 await transport.reconnect();
119 },
120 terminal: {
121 open: (input) => transport.request((client) => client[WS_METHODS.terminalOpen](input)),
122 write: (input) => transport.request((client) => client[WS_METHODS.terminalWrite](input)),
123 resize: (input) => transport.request((client) => client[WS_METHODS.terminalResize](input)),
124 clear: (input) => transport.request((client) => client[WS_METHODS.terminalClear](input)),
125 restart: (input) => transport.request((client) => client[WS_METHODS.terminalRestart](input)),
126 close: (input) => transport.request((client) => client[WS_METHODS.terminalClose](input)),
127 onEvent: (listener, options) =>
128 transport.subscribe(
129 (client) => client[WS_METHODS.subscribeTerminalEvents]({}),
130 listener,
131 options,
132 ),
133 },
134 projects: {
135 searchEntries: (input) =>
136 transport.request((client) => client[WS_METHODS.projectsSearchEntries](input)),
137 writeFile: (input) =>
138 transport.request((client) => client[WS_METHODS.projectsWriteFile](input)),
139 },
140 shell: {
141 openInEditor: (input) =>
142 transport.request((client) => client[WS_METHODS.shellOpenInEditor](input)),
143 },
144 git: {
145 pull: (input) => transport.request((client) => client[WS_METHODS.gitPull](input)),
146 refreshStatus: (input) =>
147 transport.request((client) => client[WS_METHODS.gitRefreshStatus](input)),
148 onStatus: (input, listener, options) => {
149 let current: GitStatusResult | null = null;
150 return transport.subscribe(
151 (client) => client[WS_METHODS.subscribeGitStatus](input),
152 (event: GitStatusStreamEvent) => {
153 current = applyGitStatusStreamEvent(current, event);
154 listener(current);
155 },
156 options,
157 );
158 },
159 runStackedAction: async (input, options) => {
160 let result: GitRunStackedActionResult | null = null;
161
162 await transport.requestStream(
163 (client) => client[WS_METHODS.gitRunStackedAction](input),
164 (event) => {
165 options?.onProgress?.(event);
166 if (event.kind === "action_finished") {
167 result = event.result;
168 }
169 },
170 );
171
172 if (result) {
173 return result;
174 }
175
176 throw new Error("Git action stream completed without a final result.");
177 },
178 listBranches: (input) =>
179 transport.request((client) => client[WS_METHODS.gitListBranches](input)),
180 createWorktree: (input) =>
181 transport.request((client) => client[WS_METHODS.gitCreateWorktree](input)),
182 removeWorktree: (input) =>
183 transport.request((client) => client[WS_METHODS.gitRemoveWorktree](input)),
184 createBranch: (input) =>
185 transport.request((client) => client[WS_METHODS.gitCreateBranch](input)),
186 checkout: (input) => transport.request((client) => client[WS_METHODS.gitCheckout](input)),
187 init: (input) => transport.request((client) => client[WS_METHODS.gitInit](input)),
188 resolvePullRequest: (input) =>
189 transport.request((client) => client[WS_METHODS.gitResolvePullRequest](input)),
190 preparePullRequestThread: (input) =>
191 transport.request((client) => client[WS_METHODS.gitPreparePullRequestThread](input)),
192 },
193 server: {
194 getConfig: () => transport.request((client) => client[WS_METHODS.serverGetConfig]({})),
195 refreshProviders: () =>
196 transport.request((client) => client[WS_METHODS.serverRefreshProviders]({})),
197 upsertKeybinding: (input) =>
198 transport.request((client) => client[WS_METHODS.serverUpsertKeybinding](input)),
199 getSettings: () => transport.request((client) => client[WS_METHODS.serverGetSettings]({})),
200 updateSettings: (patch) =>
201 transport.request((client) => client[WS_METHODS.serverUpdateSettings]({ patch })),
202 subscribeConfig: (listener, options) =>
203 transport.subscribe(
204 (client) => client[WS_METHODS.subscribeServerConfig]({}),
205 listener,
206 options,
207 ),
208 subscribeLifecycle: (listener, options) =>
209 transport.subscribe(
210 (client) => client[WS_METHODS.subscribeServerLifecycle]({}),
211 listener,
212 options,
213 ),
214 subscribeAuthAccess: (listener, options) =>
215 transport.subscribe(
216 (client) => client[WS_METHODS.subscribeAuthAccess]({}),
217 listener,
218 options,
219 ),
220 },
221 orchestration: {
222 getSnapshot: () =>
223 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getSnapshot]({})),
224 dispatchCommand: (input) =>
225 transport.request((client) => client[ORCHESTRATION_WS_METHODS.dispatchCommand](input)),
226 getTurnDiff: (input) =>
227 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getTurnDiff](input)),
228 getFullThreadDiff: (input) =>
229 transport.request((client) => client[ORCHESTRATION_WS_METHODS.getFullThreadDiff](input)),
230 replayEvents: (input) =>
231 transport
232 .request((client) => client[ORCHESTRATION_WS_METHODS.replayEvents](input))
233 .then((events) => [...events]),
234 onDomainEvent: (listener, options) =>
235 transport.subscribe(
236 (client) => client[WS_METHODS.subscribeOrchestrationDomainEvents]({}),
237 listener,
238 options,
239 ),
240 },
241 };
242}