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