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

ws.ts

257 lines9.3 KB

1import { Schema, Struct } from "effect";
2import { NonNegativeInt, ProjectId, ThreadId, TrimmedNonEmptyString } from "./baseSchemas";
3​
4import {
5 ClientOrchestrationCommand,
6 OrchestrationEvent,
7 ORCHESTRATION_WS_CHANNELS,
8 OrchestrationGetFullThreadDiffInput,
9 ORCHESTRATION_WS_METHODS,
10 OrchestrationGetSnapshotInput,
11 OrchestrationGetTurnDiffInput,
12 OrchestrationReplayEventsInput,
13} from "./orchestration";
14import {
15 GitActionProgressEvent,
16 GitCheckoutInput,
17 GitCreateBranchInput,
18 GitPreparePullRequestThreadInput,
19 GitCreateWorktreeInput,
20 GitInitInput,
21 GitListBranchesInput,
22 GitPullInput,
23 GitPullRequestRefInput,
24 GitRemoveWorktreeInput,
25 GitRunStackedActionInput,
26 GitStatusInput,
27} from "./git";
28import {
29 TerminalClearInput,
30 TerminalCloseInput,
31 TerminalEvent,
32 TerminalOpenInput,
33 TerminalResizeInput,
34 TerminalRestartInput,
35 TerminalWriteInput,
36} from "./terminal";
37import { KeybindingRule } from "./keybindings";
38import { ProjectSearchEntriesInput, ProjectWriteFileInput } from "./project";
39import { OpenInEditorInput } from "./editor";
40import { ServerConfigUpdatedPayload, ServerProviderUpdatedPayload } from "./server";
41import { ServerSettingsPatch } from "./settings";
42​
43// ── WebSocket RPC Method Names ───────────────────────────────────────
44​
45export const WS_METHODS = {
46 // Project registry methods
47 projectsList: "projects.list",
48 projectsAdd: "projects.add",
49 projectsRemove: "projects.remove",
50 projectsSearchEntries: "projects.searchEntries",
51 projectsWriteFile: "projects.writeFile",
52​
53 // Shell methods
54 shellOpenInEditor: "shell.openInEditor",
55​
56 // Git methods
57 gitPull: "git.pull",
58 gitStatus: "git.status",
59 gitRunStackedAction: "git.runStackedAction",
60 gitListBranches: "git.listBranches",
61 gitCreateWorktree: "git.createWorktree",
62 gitRemoveWorktree: "git.removeWorktree",
63 gitCreateBranch: "git.createBranch",
64 gitCheckout: "git.checkout",
65 gitInit: "git.init",
66 gitResolvePullRequest: "git.resolvePullRequest",
67 gitPreparePullRequestThread: "git.preparePullRequestThread",
68​
69 // Terminal methods
70 terminalOpen: "terminal.open",
71 terminalWrite: "terminal.write",
72 terminalResize: "terminal.resize",
73 terminalClear: "terminal.clear",
74 terminalRestart: "terminal.restart",
75 terminalClose: "terminal.close",
76​
77 // Server meta
78 serverGetConfig: "server.getConfig",
79 serverRefreshProviders: "server.refreshProviders",
80 serverUpsertKeybinding: "server.upsertKeybinding",
81 serverGetSettings: "server.getSettings",
82 serverUpdateSettings: "server.updateSettings",
83} as const;
84​
85// ── Push Event Channels ──────────────────────────────────────────────
86​
87export const WS_CHANNELS = {
88 gitActionProgress: "git.actionProgress",
89 terminalEvent: "terminal.event",
90 serverWelcome: "server.welcome",
91 serverConfigUpdated: "server.configUpdated",
92 serverProvidersUpdated: "server.providersUpdated",
93} as const;
94​
95// -- Tagged Union of all request body schemas ─────────────────────────
96​
97const tagRequestBody = <const Tag extends string, const Fields extends Schema.Struct.Fields>(
98 tag: Tag,
99 schema: Schema.Struct<Fields>,
100) =>
101 schema.mapFields(
102 Struct.assign({ _tag: Schema.tag(tag) }),
103 // PreserveChecks is safe here. No existing schema should have checks depending on the tag
104 { unsafePreserveChecks: true },
105 );
106​
107const WebSocketRequestBody = Schema.Union([
108 // Orchestration methods
109 tagRequestBody(
110 ORCHESTRATION_WS_METHODS.dispatchCommand,
111 Schema.Struct({ command: ClientOrchestrationCommand }),
112 ),
113 tagRequestBody(ORCHESTRATION_WS_METHODS.getSnapshot, OrchestrationGetSnapshotInput),
114 tagRequestBody(ORCHESTRATION_WS_METHODS.getTurnDiff, OrchestrationGetTurnDiffInput),
115 tagRequestBody(ORCHESTRATION_WS_METHODS.getFullThreadDiff, OrchestrationGetFullThreadDiffInput),
116 tagRequestBody(ORCHESTRATION_WS_METHODS.replayEvents, OrchestrationReplayEventsInput),
117​
118 // Project Search
119 tagRequestBody(WS_METHODS.projectsSearchEntries, ProjectSearchEntriesInput),
120 tagRequestBody(WS_METHODS.projectsWriteFile, ProjectWriteFileInput),
121​
122 // Shell methods
123 tagRequestBody(WS_METHODS.shellOpenInEditor, OpenInEditorInput),
124​
125 // Git methods
126 tagRequestBody(WS_METHODS.gitPull, GitPullInput),
127 tagRequestBody(WS_METHODS.gitStatus, GitStatusInput),
128 tagRequestBody(WS_METHODS.gitRunStackedAction, GitRunStackedActionInput),
129 tagRequestBody(WS_METHODS.gitListBranches, GitListBranchesInput),
130 tagRequestBody(WS_METHODS.gitCreateWorktree, GitCreateWorktreeInput),
131 tagRequestBody(WS_METHODS.gitRemoveWorktree, GitRemoveWorktreeInput),
132 tagRequestBody(WS_METHODS.gitCreateBranch, GitCreateBranchInput),
133 tagRequestBody(WS_METHODS.gitCheckout, GitCheckoutInput),
134 tagRequestBody(WS_METHODS.gitInit, GitInitInput),
135 tagRequestBody(WS_METHODS.gitResolvePullRequest, GitPullRequestRefInput),
136 tagRequestBody(WS_METHODS.gitPreparePullRequestThread, GitPreparePullRequestThreadInput),
137​
138 // Terminal methods
139 tagRequestBody(WS_METHODS.terminalOpen, TerminalOpenInput),
140 tagRequestBody(WS_METHODS.terminalWrite, TerminalWriteInput),
141 tagRequestBody(WS_METHODS.terminalResize, TerminalResizeInput),
142 tagRequestBody(WS_METHODS.terminalClear, TerminalClearInput),
143 tagRequestBody(WS_METHODS.terminalRestart, TerminalRestartInput),
144 tagRequestBody(WS_METHODS.terminalClose, TerminalCloseInput),
145​
146 // Server meta
147 tagRequestBody(WS_METHODS.serverGetConfig, Schema.Struct({})),
148 tagRequestBody(WS_METHODS.serverRefreshProviders, Schema.Struct({})),
149 tagRequestBody(WS_METHODS.serverUpsertKeybinding, KeybindingRule),
150 tagRequestBody(WS_METHODS.serverGetSettings, Schema.Struct({})),
151 tagRequestBody(WS_METHODS.serverUpdateSettings, Schema.Struct({ patch: ServerSettingsPatch })),
152]);
153​
154export const WebSocketRequest = Schema.Struct({
155 id: TrimmedNonEmptyString,
156 body: WebSocketRequestBody,
157});
158export type WebSocketRequest = typeof WebSocketRequest.Type;
159​
160export const WebSocketResponse = Schema.Struct({
161 id: TrimmedNonEmptyString,
162 result: Schema.optional(Schema.Unknown),
163 error: Schema.optional(
164 Schema.Struct({
165 message: Schema.String,
166 }),
167 ),
168});
169export type WebSocketResponse = typeof WebSocketResponse.Type;
170​
171export const WsPushSequence = NonNegativeInt;
172export type WsPushSequence = typeof WsPushSequence.Type;
173​
174export const WsWelcomePayload = Schema.Struct({
175 cwd: TrimmedNonEmptyString,
176 projectName: TrimmedNonEmptyString,
177 bootstrapProjectId: Schema.optional(ProjectId),
178 bootstrapThreadId: Schema.optional(ThreadId),
179});
180export type WsWelcomePayload = typeof WsWelcomePayload.Type;
181​
182export interface WsPushPayloadByChannel {
183 readonly [WS_CHANNELS.serverWelcome]: WsWelcomePayload;
184 readonly [WS_CHANNELS.serverConfigUpdated]: typeof ServerConfigUpdatedPayload.Type;
185 readonly [WS_CHANNELS.serverProvidersUpdated]: typeof ServerProviderUpdatedPayload.Type;
186 readonly [WS_CHANNELS.gitActionProgress]: typeof GitActionProgressEvent.Type;
187 readonly [WS_CHANNELS.terminalEvent]: typeof TerminalEvent.Type;
188 readonly [ORCHESTRATION_WS_CHANNELS.domainEvent]: OrchestrationEvent;
190​
191export type WsPushChannel = keyof WsPushPayloadByChannel;
192export type WsPushData<C extends WsPushChannel> = WsPushPayloadByChannel[C];
193​
194const makeWsPushSchema = <const Channel extends string, Payload extends Schema.Schema<any>>(
195 channel: Channel,
196 payload: Payload,
197) =>
198 Schema.Struct({
199 type: Schema.Literal("push"),
200 sequence: WsPushSequence,
201 channel: Schema.Literal(channel),
202 data: payload,
203 });
204​
205export const WsPushServerWelcome = makeWsPushSchema(WS_CHANNELS.serverWelcome, WsWelcomePayload);
206export const WsPushServerConfigUpdated = makeWsPushSchema(
207 WS_CHANNELS.serverConfigUpdated,
208 ServerConfigUpdatedPayload,
209);
210export const WsPushServerProvidersUpdated = makeWsPushSchema(
211 WS_CHANNELS.serverProvidersUpdated,
212 ServerProviderUpdatedPayload,
213);
214export const WsPushGitActionProgress = makeWsPushSchema(
215 WS_CHANNELS.gitActionProgress,
216 GitActionProgressEvent,
217);
218export const WsPushTerminalEvent = makeWsPushSchema(WS_CHANNELS.terminalEvent, TerminalEvent);
219export const WsPushOrchestrationDomainEvent = makeWsPushSchema(
220 ORCHESTRATION_WS_CHANNELS.domainEvent,
221 OrchestrationEvent,
222);
223​
224export const WsPushChannelSchema = Schema.Literals([
225 WS_CHANNELS.gitActionProgress,
226 WS_CHANNELS.serverWelcome,
227 WS_CHANNELS.serverConfigUpdated,
228 WS_CHANNELS.serverProvidersUpdated,
229 WS_CHANNELS.terminalEvent,
230 ORCHESTRATION_WS_CHANNELS.domainEvent,
231]);
232export type WsPushChannelSchema = typeof WsPushChannelSchema.Type;
233​
234export const WsPush = Schema.Union([
235 WsPushServerWelcome,
236 WsPushServerConfigUpdated,
237 WsPushServerProvidersUpdated,
238 WsPushGitActionProgress,
239 WsPushTerminalEvent,
240 WsPushOrchestrationDomainEvent,
241]);
242export type WsPush = typeof WsPush.Type;
243​
244export type WsPushMessage<C extends WsPushChannel> = Extract<WsPush, { channel: C }>;
245​
246export const WsPushEnvelopeBase = Schema.Struct({
247 type: Schema.Literal("push"),
248 sequence: WsPushSequence,
249 channel: WsPushChannelSchema,
250 data: Schema.Unknown,
251});
252export type WsPushEnvelopeBase = typeof WsPushEnvelopeBase.Type;
253​
254// ── Union of all server → client messages ─────────────────────────────
255​
256export const WsResponse = Schema.Union([WebSocketResponse, WsPush]);
257export type WsResponse = typeof WsResponse.Type;