Open History to see the last commit
1import { Schema, Struct } from "effect";
2import { ProjectId, ThreadId, TrimmedNonEmptyString } from "./baseSchemas";
3
4import {
5 ClientOrchestrationCommand,
6 OrchestrationGetFullThreadDiffInput,
7 ORCHESTRATION_WS_METHODS,
8 OrchestrationGetSnapshotInput,
9 OrchestrationGetTurnDiffInput,
10 OrchestrationReplayEventsInput,
11} from "./orchestration";
12import {
13 GitCheckoutInput,
14 GitCreateBranchInput,
15 GitCreateWorktreeInput,
16 GitInitInput,
17 GitListBranchesInput,
18 GitPullInput,
19 GitRemoveWorktreeInput,
20 GitRunStackedActionInput,
21 GitStatusInput,
22} from "./git";
23import {
24 TerminalClearInput,
25 TerminalCloseInput,
26 TerminalOpenInput,
27 TerminalResizeInput,
28 TerminalRestartInput,
29 TerminalWriteInput,
30} from "./terminal";
31import { KeybindingRule } from "./keybindings";
32import { ProjectSearchEntriesInput, ProjectWriteFileInput } from "./project";
33import { OpenInEditorInput } from "./editor";
34
35// ── WebSocket RPC Method Names ───────────────────────────────────────
36
37export const WS_METHODS = {
38 // Project registry methods
39 projectsList: "projects.list",
40 projectsAdd: "projects.add",
41 projectsRemove: "projects.remove",
42 projectsSearchEntries: "projects.searchEntries",
43 projectsWriteFile: "projects.writeFile",
44
45 // Shell methods
46 shellOpenInEditor: "shell.openInEditor",
47
48 // Git methods
49 gitPull: "git.pull",
50 gitStatus: "git.status",
51 gitRunStackedAction: "git.runStackedAction",
52 gitListBranches: "git.listBranches",
53 gitCreateWorktree: "git.createWorktree",
54 gitRemoveWorktree: "git.removeWorktree",
55 gitCreateBranch: "git.createBranch",
56 gitCheckout: "git.checkout",
57 gitInit: "git.init",
58
59 // Terminal methods
60 terminalOpen: "terminal.open",
61 terminalWrite: "terminal.write",
62 terminalResize: "terminal.resize",
63 terminalClear: "terminal.clear",
64 terminalRestart: "terminal.restart",
65 terminalClose: "terminal.close",
66
67 // Server meta
68 serverGetConfig: "server.getConfig",
69 serverUpsertKeybinding: "server.upsertKeybinding",
70} as const;
71
72// ── Push Event Channels ──────────────────────────────────────────────
73
74export const WS_CHANNELS = {
75 terminalEvent: "terminal.event",
76 serverWelcome: "server.welcome",
77 serverConfigUpdated: "server.configUpdated",
78} as const;
79
80// -- Tagged Union of all request body schemas ─────────────────────────
81
82const tagRequestBody = <const Tag extends string, const Fields extends Schema.Struct.Fields>(
83 tag: Tag,
84 schema: Schema.Struct<Fields>,
85) =>
86 schema.mapFields(
87 Struct.assign({ _tag: Schema.tag(tag) }),
88 // PreserveChecks is safe here. No existing schema should have checks depending on the tag
89 { unsafePreserveChecks: true },
90 );
91
92const WebSocketRequestBody = Schema.Union([
93 // Orchestration methods
94 tagRequestBody(
95 ORCHESTRATION_WS_METHODS.dispatchCommand,
96 Schema.Struct({ command: ClientOrchestrationCommand }),
97 ),
98 tagRequestBody(ORCHESTRATION_WS_METHODS.getSnapshot, OrchestrationGetSnapshotInput),
99 tagRequestBody(ORCHESTRATION_WS_METHODS.getTurnDiff, OrchestrationGetTurnDiffInput),
100 tagRequestBody(ORCHESTRATION_WS_METHODS.getFullThreadDiff, OrchestrationGetFullThreadDiffInput),
101 tagRequestBody(ORCHESTRATION_WS_METHODS.replayEvents, OrchestrationReplayEventsInput),
102
103 // Project Search
104 tagRequestBody(WS_METHODS.projectsSearchEntries, ProjectSearchEntriesInput),
105 tagRequestBody(WS_METHODS.projectsWriteFile, ProjectWriteFileInput),
106
107 // Shell methods
108 tagRequestBody(WS_METHODS.shellOpenInEditor, OpenInEditorInput),
109
110 // Git methods
111 tagRequestBody(WS_METHODS.gitPull, GitPullInput),
112 tagRequestBody(WS_METHODS.gitStatus, GitStatusInput),
113 tagRequestBody(WS_METHODS.gitRunStackedAction, GitRunStackedActionInput),
114 tagRequestBody(WS_METHODS.gitListBranches, GitListBranchesInput),
115 tagRequestBody(WS_METHODS.gitCreateWorktree, GitCreateWorktreeInput),
116 tagRequestBody(WS_METHODS.gitRemoveWorktree, GitRemoveWorktreeInput),
117 tagRequestBody(WS_METHODS.gitCreateBranch, GitCreateBranchInput),
118 tagRequestBody(WS_METHODS.gitCheckout, GitCheckoutInput),
119 tagRequestBody(WS_METHODS.gitInit, GitInitInput),
120
121 // Terminal methods
122 tagRequestBody(WS_METHODS.terminalOpen, TerminalOpenInput),
123 tagRequestBody(WS_METHODS.terminalWrite, TerminalWriteInput),
124 tagRequestBody(WS_METHODS.terminalResize, TerminalResizeInput),
125 tagRequestBody(WS_METHODS.terminalClear, TerminalClearInput),
126 tagRequestBody(WS_METHODS.terminalRestart, TerminalRestartInput),
127 tagRequestBody(WS_METHODS.terminalClose, TerminalCloseInput),
128
129 // Server meta
130 tagRequestBody(WS_METHODS.serverGetConfig, Schema.Struct({})),
131 tagRequestBody(WS_METHODS.serverUpsertKeybinding, KeybindingRule),
132]);
133
134export const WebSocketRequest = Schema.Struct({
135 id: TrimmedNonEmptyString,
136 body: WebSocketRequestBody,
137});
138export type WebSocketRequest = typeof WebSocketRequest.Type;
139
140export const WebSocketResponse = Schema.Struct({
141 id: TrimmedNonEmptyString,
142 result: Schema.optional(Schema.Unknown),
143 error: Schema.optional(
144 Schema.Struct({
145 message: Schema.String,
146 }),
147 ),
148});
149export type WebSocketResponse = typeof WebSocketResponse.Type;
150
151export const WsPush = Schema.Struct({
152 type: Schema.Literal("push"),
153 channel: TrimmedNonEmptyString,
154 data: Schema.Unknown,
155});
156export type WsPush = typeof WsPush.Type;
157
158// ── Union of all server → client messages ─────────────────────────────
159
160export const WsResponse = Schema.Union([WebSocketResponse, WsPush]);
161export type WsResponse = typeof WsResponse.Type;
162
163// ── Server welcome payload ───────────────────────────────────────────
164
165export const WsWelcomePayload = Schema.Struct({
166 cwd: TrimmedNonEmptyString,
167 projectName: TrimmedNonEmptyString,
168 bootstrapProjectId: Schema.optional(ProjectId),
169 bootstrapThreadId: Schema.optional(ThreadId),
170});
171export type WsWelcomePayload = typeof WsWelcomePayload.Type;