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 } 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
44 // Shell methods
45 shellOpenInEditor: "shell.openInEditor",
46
47 // Git methods
48 gitPull: "git.pull",
49 gitStatus: "git.status",
50 gitRunStackedAction: "git.runStackedAction",
51 gitListBranches: "git.listBranches",
52 gitCreateWorktree: "git.createWorktree",
53 gitRemoveWorktree: "git.removeWorktree",
54 gitCreateBranch: "git.createBranch",
55 gitCheckout: "git.checkout",
56 gitInit: "git.init",
57
58 // Terminal methods
59 terminalOpen: "terminal.open",
60 terminalWrite: "terminal.write",
61 terminalResize: "terminal.resize",
62 terminalClear: "terminal.clear",
63 terminalRestart: "terminal.restart",
64 terminalClose: "terminal.close",
65
66 // Server meta
67 serverGetConfig: "server.getConfig",
68 serverUpsertKeybinding: "server.upsertKeybinding",
69} as const;
70
71// ── Push Event Channels ──────────────────────────────────────────────
72
73export const WS_CHANNELS = {
74 terminalEvent: "terminal.event",
75 serverWelcome: "server.welcome",
76 serverConfigUpdated: "server.configUpdated",
77} as const;
78
79// -- Tagged Union of all request body schemas ─────────────────────────
80
81const tagRequestBody = <const Tag extends string, const Fields extends Schema.Struct.Fields>(
82 tag: Tag,
83 schema: Schema.Struct<Fields>,
84) =>
85 schema.mapFields(
86 Struct.assign({ _tag: Schema.tag(tag) }),
87 // PreserveChecks is safe here. No existing schema should have checks depending on the tag
88 { unsafePreserveChecks: true },
89 );
90
91export const WebSocketRequestBody = Schema.Union([
92 // Orchestration methods
93 tagRequestBody(
94 ORCHESTRATION_WS_METHODS.dispatchCommand,
95 Schema.Struct({ command: ClientOrchestrationCommand }),
96 ),
97 tagRequestBody(ORCHESTRATION_WS_METHODS.getSnapshot, OrchestrationGetSnapshotInput),
98 tagRequestBody(ORCHESTRATION_WS_METHODS.getTurnDiff, OrchestrationGetTurnDiffInput),
99 tagRequestBody(ORCHESTRATION_WS_METHODS.getFullThreadDiff, OrchestrationGetFullThreadDiffInput),
100 tagRequestBody(ORCHESTRATION_WS_METHODS.replayEvents, OrchestrationReplayEventsInput),
101
102 // Project Search
103 tagRequestBody(WS_METHODS.projectsSearchEntries, ProjectSearchEntriesInput),
104
105 // Shell methods
106 tagRequestBody(WS_METHODS.shellOpenInEditor, OpenInEditorInput),
107
108 // Git methods
109 tagRequestBody(WS_METHODS.gitPull, GitPullInput),
110 tagRequestBody(WS_METHODS.gitStatus, GitStatusInput),
111 tagRequestBody(WS_METHODS.gitRunStackedAction, GitRunStackedActionInput),
112 tagRequestBody(WS_METHODS.gitListBranches, GitListBranchesInput),
113 tagRequestBody(WS_METHODS.gitCreateWorktree, GitCreateWorktreeInput),
114 tagRequestBody(WS_METHODS.gitRemoveWorktree, GitRemoveWorktreeInput),
115 tagRequestBody(WS_METHODS.gitCreateBranch, GitCreateBranchInput),
116 tagRequestBody(WS_METHODS.gitCheckout, GitCheckoutInput),
117 tagRequestBody(WS_METHODS.gitInit, GitInitInput),
118
119 // Terminal methods
120 tagRequestBody(WS_METHODS.terminalOpen, TerminalOpenInput),
121 tagRequestBody(WS_METHODS.terminalWrite, TerminalWriteInput),
122 tagRequestBody(WS_METHODS.terminalResize, TerminalResizeInput),
123 tagRequestBody(WS_METHODS.terminalClear, TerminalClearInput),
124 tagRequestBody(WS_METHODS.terminalRestart, TerminalRestartInput),
125 tagRequestBody(WS_METHODS.terminalClose, TerminalCloseInput),
126
127 // Server meta
128 tagRequestBody(WS_METHODS.serverGetConfig, Schema.Struct({})),
129 tagRequestBody(WS_METHODS.serverUpsertKeybinding, KeybindingRule),
130]);
131
132export const WebSocketRequest = Schema.Struct({
133 id: TrimmedNonEmptyString,
134 body: WebSocketRequestBody,
135});
136export type WebSocketRequest = typeof WebSocketRequest.Type;
137
138export const WebSocketResponse = Schema.Struct({
139 id: TrimmedNonEmptyString,
140 result: Schema.optional(Schema.Unknown),
141 error: Schema.optional(
142 Schema.Struct({
143 message: Schema.String,
144 }),
145 ),
146});
147export type WebSocketResponse = typeof WebSocketResponse.Type;
148
149export const WsPush = Schema.Struct({
150 type: Schema.Literal("push"),
151 channel: TrimmedNonEmptyString,
152 data: Schema.Unknown,
153});
154export type WsPush = typeof WsPush.Type;
155
156// ── Union of all server → client messages ─────────────────────────────
157
158export const WsResponse = Schema.Union([WebSocketResponse, WsPush]);
159export type WsResponse = typeof WsResponse.Type;
160
161// ── Server welcome payload ───────────────────────────────────────────
162
163export const WsWelcomePayload = Schema.Struct({
164 cwd: TrimmedNonEmptyString,
165 projectName: TrimmedNonEmptyString,
166 bootstrapProjectId: Schema.optional(ProjectId),
167 bootstrapThreadId: Schema.optional(ThreadId),
168});
169export type WsWelcomePayload = typeof WsWelcomePayload.Type;