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