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 WS_ERROR_CODES = {
151 invalidRequest: "INVALID_REQUEST",
152 projectDirNotExist: "PROJECT_DIR_NOT_EXIST",
153 projectPathNotDirectory: "PROJECT_PATH_NOT_DIRECTORY",
154 routeRequest: "ROUTE_REQUEST_ERROR",
155 unknown: "UNKNOWN",
156} as const;
157export type WsErrorCode = (typeof WS_ERROR_CODES)[keyof typeof WS_ERROR_CODES];
158
159export const WsError = Schema.Struct({
160 code: Schema.String,
161 message: Schema.String,
162 path: Schema.optional(Schema.String),
163});
164export type WsError = typeof WsError.Type;
165
166export const WebSocketResponse = Schema.Struct({
167 id: TrimmedNonEmptyString,
168 result: Schema.optional(Schema.Unknown),
169 error: Schema.optional(WsError),
170});
171export type WebSocketResponse = typeof WebSocketResponse.Type;
172
173export const WsPushSequence = NonNegativeInt;
174export type WsPushSequence = typeof WsPushSequence.Type;
175
176export const WsWelcomePayload = Schema.Struct({
177 cwd: TrimmedNonEmptyString,
178 projectName: TrimmedNonEmptyString,
179 bootstrapProjectId: Schema.optional(ProjectId),
180 bootstrapThreadId: Schema.optional(ThreadId),
181});
182export type WsWelcomePayload = typeof WsWelcomePayload.Type;
183
184export interface WsPushPayloadByChannel {
185 readonly [WS_CHANNELS.serverWelcome]: WsWelcomePayload;
186 readonly [WS_CHANNELS.serverConfigUpdated]: typeof ServerConfigUpdatedPayload.Type;
187 readonly [WS_CHANNELS.terminalEvent]: typeof TerminalEvent.Type;
188 readonly [ORCHESTRATION_WS_CHANNELS.domainEvent]: OrchestrationEvent;
189}
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 WsPushTerminalEvent = makeWsPushSchema(WS_CHANNELS.terminalEvent, TerminalEvent);
211export const WsPushOrchestrationDomainEvent = makeWsPushSchema(
212 ORCHESTRATION_WS_CHANNELS.domainEvent,
213 OrchestrationEvent,
214);
215
216export const WsPushChannelSchema = Schema.Literals([
217 WS_CHANNELS.serverWelcome,
218 WS_CHANNELS.serverConfigUpdated,
219 WS_CHANNELS.terminalEvent,
220 ORCHESTRATION_WS_CHANNELS.domainEvent,
221]);
222export type WsPushChannelSchema = typeof WsPushChannelSchema.Type;
223
224export const WsPush = Schema.Union([
225 WsPushServerWelcome,
226 WsPushServerConfigUpdated,
227 WsPushTerminalEvent,
228 WsPushOrchestrationDomainEvent,
229]);
230export type WsPush = typeof WsPush.Type;
231
232export type WsPushMessage<C extends WsPushChannel> = Extract<WsPush, { channel: C }>;
233
234export const WsPushEnvelopeBase = Schema.Struct({
235 type: Schema.Literal("push"),
236 sequence: WsPushSequence,
237 channel: WsPushChannelSchema,
238 data: Schema.Unknown,
239});
240export type WsPushEnvelopeBase = typeof WsPushEnvelopeBase.Type;
241
242// ── Union of all server → client messages ─────────────────────────────
243
244export const WsResponse = Schema.Union([WebSocketResponse, WsPush]);
245export type WsResponse = typeof WsResponse.Type;