Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ws.ts

98 lines3.2 KB

1import { z } from "zod";
2​
3// ── WebSocket RPC Method Names ───────────────────────────────────────
4​
5export const WS_METHODS = {
6 // Provider methods (mirrors NativeApi.providers)
7 providersStartSession: "providers.startSession",
8 providersSendTurn: "providers.sendTurn",
9 providersInterruptTurn: "providers.interruptTurn",
10 providersRespondToRequest: "providers.respondToRequest",
11 providersStopSession: "providers.stopSession",
12 providersListSessions: "providers.listSessions",
13​
14 // Project registry methods
15 projectsList: "projects.list",
16 projectsAdd: "projects.add",
17 projectsRemove: "projects.remove",
18​
19 // Shell methods
20 shellOpenInEditor: "shell.openInEditor",
21​
22 // Git methods
23 gitPull: "git.pull",
24 gitStatus: "git.status",
25 gitRunStackedAction: "git.runStackedAction",
26 gitListBranches: "git.listBranches",
27 gitCreateWorktree: "git.createWorktree",
28 gitRemoveWorktree: "git.removeWorktree",
29 gitCreateBranch: "git.createBranch",
30 gitCheckout: "git.checkout",
31 gitInit: "git.init",
32​
33 // Terminal methods
34 terminalOpen: "terminal.open",
35 terminalWrite: "terminal.write",
36 terminalResize: "terminal.resize",
37 terminalClear: "terminal.clear",
38 terminalRestart: "terminal.restart",
39 terminalClose: "terminal.close",
40​
41 // Server meta
42 serverGetConfig: "server.getConfig",
43} as const;
44​
45// ── Push Event Channels ──────────────────────────────────────────────
46​
47export const WS_CHANNELS = {
48 providerEvent: "providers.event",
49 terminalEvent: "terminal.event",
50 serverWelcome: "server.welcome",
51} as const;
52​
53// ── Client → Server (request) ────────────────────────────────────────
54​
55export const wsRequestSchema = z.object({
56 id: z.string().min(1),
57 method: z.string().min(1),
58 params: z.unknown().optional(),
59});
60​
61export type WsRequest = z.infer<typeof wsRequestSchema>;
62​
63// ── Server → Client (response to request) ────────────────────────────
64​
65export const wsResponseSchema = z.object({
66 id: z.string().min(1),
67 result: z.unknown().optional(),
68 error: z
69 .object({
70 message: z.string(),
71 })
72 .optional(),
73});
74​
75export type WsResponse = z.infer<typeof wsResponseSchema>;
76​
77// ── Server → Client (push event) ─────────────────────────────────────
78​
79export const wsPushSchema = z.object({
80 type: z.literal("push"),
81 channel: z.string().min(1),
82 data: z.unknown(),
83});
84​
85export type WsPush = z.infer<typeof wsPushSchema>;
86​
87// ── Union of all server → client messages ─────────────────────────────
88​
89export type WsServerMessage = WsResponse | WsPush;
90​
91// ── Server welcome payload ───────────────────────────────────────────
92​
93export const wsWelcomePayloadSchema = z.object({
94 cwd: z.string().min(1),
95 projectName: z.string().min(1),
96});
97​
98export type WsWelcomePayload = z.infer<typeof wsWelcomePayloadSchema>;