summaryrefslogtreecommitdiffhomepage
path: root/packages/transport-ws/src/router.ts
blob: 7e9ba773991f5311fc452fb883e453577a5fc242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
 * Pure core — routes a client WS message into an effect description.
 *
 * Zero I/O, zero ambient state. Every function is `input → output`:
 * it decides what to do but does NOT do it. The shell (extension.ts)
 * interprets the result: sends WS messages, mutates connSubs, calls
 * provider.invoke, drives the orchestrator.
 */

import type { SurfaceContext, SurfaceRegistry } from "@dispatch/surface-registry";
import type {
	ChatQueueMessage,
	ChatSendMessage,
	ChatSubscribeMessage,
	ChatUnsubscribeMessage,
	ReasoningEffort,
	WsClientMessage,
} from "@dispatch/transport-contract";
import type { SurfaceServerMessage } from "@dispatch/ui-contract";

// ── Result types ────────────────────────────────────────────────────────────

/** The effect a surface client message should produce. */
export interface SurfaceRouteResult {
	readonly kind: "surface";
	/** Server messages to send back to this connection. */
	readonly replies: readonly SurfaceServerMessage[];
	/** Whether to add or remove the surface id from connSubs. */
	readonly subChange?: {
		readonly op: "add" | "remove";
		readonly surfaceId: string;
		readonly conversationId?: string;
	};
	/** If set, the shell must call `provider.invoke(actionId, payload, context)`. */
	readonly invoke?: {
		readonly surfaceId: string;
		readonly actionId: string;
		readonly payload?: unknown;
		readonly conversationId?: string;
	};
}

/** The effect a validated chat.send should produce. */
export interface ChatRouteResult {
	readonly kind: "chat";
	readonly conversationId: string | undefined;
	readonly message: string;
	readonly model: string | undefined;
	readonly cwd: string | undefined;
	readonly reasoningEffort?: ReasoningEffort;
	readonly workspaceId?: string;
	/**
	 * The computer (SSH config alias) to run this turn's tools on — forwarded
	 * verbatim to the orchestrator's `startTurn` (which resolves it via
	 * `getEffectiveComputer`). Mirrors `cwd`/`workspaceId`: an opaque per-turn
	 * override, unvalidated here (validation happens at SSH connect time).
	 * Absent when the client omits it (the orchestrator then inherits the
	 * conversation → workspace → local chain).
	 */
	readonly computerId?: string;
}

/** A malformed chat.send that should yield a chat.error reply. */
export interface ChatRouteError {
	readonly kind: "chat-error";
	readonly conversationId: string | undefined;
	readonly errorMessage: string;
}

/** The effect a chat.subscribe should produce. */
export interface ChatSubscribeRouteResult {
	readonly kind: "chat-subscribe";
	readonly conversationId: string;
}

/** The effect a chat.unsubscribe should produce. */
export interface ChatUnsubscribeRouteResult {
	readonly kind: "chat-unsubscribe";
	readonly conversationId: string;
}

/**
 * The effect a validated chat.queue should produce. The shell calls
 * `orchestrator.enqueue({ conversationId, text })` and emits NOTHING back on
 * either path (fire-and-forget): success is confirmed by the message-queue
 * SURFACE updating (startedTurn:false) or by streaming chat.deltas
 * (startedTurn:true — the shell auto-subscribes the sender, same as chat.send).
 */
export interface ChatQueueRouteResult {
	readonly kind: "chat-queue";
	readonly conversationId: string;
	readonly text: string;
	readonly workspaceId?: string;
}

/** The effect any client WS message should produce. */
export type RouteResult =
	| SurfaceRouteResult
	| ChatRouteResult
	| ChatRouteError
	| ChatSubscribeRouteResult
	| ChatUnsubscribeRouteResult
	| ChatQueueRouteResult;

// ── Helpers ─────────────────────────────────────────────────────────────────

/**
 * Build a subscription key from a surface id and optional conversation id.
 * The shell uses this same function so both layers agree on key format.
 */
export function subKey(surfaceId: string, conversationId?: string): string {
	return conversationId !== undefined ? `${surfaceId}::${conversationId}` : `${surfaceId}::`;
}

/** Build the catalog `SurfaceServerMessage` from the registry. */
export function catalogMessage(registry: SurfaceRegistry): SurfaceServerMessage {
	return { type: "catalog", catalog: registry.getCatalog() };
}

// ── Router ──────────────────────────────────────────────────────────────────

/**
 * Route a single client message into a pure effect description.
 *
 * @param registry  The surface registry (looked up once, injected).
 * @param connSubs  This connection's current subscription keys (via `subKey`).
 * @param msg       The parsed client message (surface or chat).
 */
export function routeClientMessage(
	registry: SurfaceRegistry,
	connSubs: ReadonlySet<string>,
	msg: WsClientMessage,
): RouteResult {
	switch (msg.type) {
		case "subscribe":
			return handleSubscribe(registry, connSubs, msg.surfaceId, msg.conversationId);
		case "unsubscribe":
			return handleUnsubscribe(msg.surfaceId, msg.conversationId);
		case "invoke":
			return handleInvoke(registry, msg.surfaceId, msg.actionId, msg.payload, msg.conversationId);
		case "chat.send":
			return handleChatSend(msg);
		case "chat.subscribe":
			return handleChatSubscribe(msg);
		case "chat.unsubscribe":
			return handleChatUnsubscribe(msg);
		case "chat.queue":
			return handleChatQueue(msg);
	}
}

// ── Chat validation ─────────────────────────────────────────────────────────

const VALID_REASONING_EFFORT: ReadonlySet<string> = new Set<ReasoningEffort>([
	"low",
	"medium",
	"high",
	"xhigh",
	"max",
]);

function handleChatSend(msg: ChatSendMessage): ChatRouteResult | ChatRouteError {
	if (typeof msg.message !== "string" || msg.message.length === 0) {
		return {
			kind: "chat-error",
			conversationId: msg.conversationId,
			errorMessage: "chat.send requires a non-empty string `message`",
		};
	}
	if (msg.reasoningEffort !== undefined && !VALID_REASONING_EFFORT.has(msg.reasoningEffort)) {
		return {
			kind: "chat-error",
			conversationId: msg.conversationId,
			errorMessage: `chat.send: invalid reasoningEffort "${msg.reasoningEffort}" — must be one of: low, medium, high, xhigh, max`,
		};
	}
	return {
		kind: "chat",
		conversationId: msg.conversationId,
		message: msg.message,
		model: msg.model,
		cwd: msg.cwd,
		...(msg.reasoningEffort !== undefined ? { reasoningEffort: msg.reasoningEffort } : {}),
		...(msg.workspaceId !== undefined ? { workspaceId: msg.workspaceId } : {}),
		...(msg.computerId !== undefined ? { computerId: msg.computerId } : {}),
	};
}

function handleChatSubscribe(msg: ChatSubscribeMessage): ChatSubscribeRouteResult {
	return { kind: "chat-subscribe", conversationId: msg.conversationId };
}

function handleChatUnsubscribe(msg: ChatUnsubscribeMessage): ChatUnsubscribeRouteResult {
	return { kind: "chat-unsubscribe", conversationId: msg.conversationId };
}

/**
 * Validate a chat.queue: `text` must be a non-empty string AFTER TRIM (matches
 * the HTTP `QueueRequest` rule). Invalid → `chat-error` (the shell replies with
 * `chat.error`, same style as a malformed `chat.send`; the orchestrator is never
 * called). Valid → `chat-queue` (the shell calls `orchestrator.enqueue`).
 */
function handleChatQueue(msg: ChatQueueMessage): ChatQueueRouteResult | ChatRouteError {
	if (typeof msg.text !== "string" || msg.text.trim().length === 0) {
		return {
			kind: "chat-error",
			conversationId: msg.conversationId,
			errorMessage: "chat.queue requires a non-empty string `text`",
		};
	}
	return {
		kind: "chat-queue",
		conversationId: msg.conversationId,
		text: msg.text,
		...(msg.workspaceId !== undefined ? { workspaceId: msg.workspaceId } : {}),
	};
}

// ── Per-message handlers ────────────────────────────────────────────────────

function handleSubscribe(
	registry: SurfaceRegistry,
	connSubs: ReadonlySet<string>,
	surfaceId: string,
	conversationId?: string,
): SurfaceRouteResult {
	const provider = registry.getSurface(surfaceId);
	if (!provider) {
		return {
			kind: "surface",
			replies: [{ type: "error", surfaceId, message: `Unknown surface: ${surfaceId}` }],
		};
	}

	const context: SurfaceContext | undefined =
		conversationId !== undefined ? { conversationId } : undefined;
	const spec = provider.getSpec(context);

	// getSpec may be sync or async — the pure core treats it as a value the
	// shell will resolve. We return the spec directly (it's a SurfaceSpec).
	// If it's a Promise the shell awaits it; if it's sync it's already the value.
	// For the pure core we just pass it through — the shell handles the resolution.
	const specValue = spec as import("@dispatch/ui-contract").SurfaceSpec;

	const replies: import("@dispatch/ui-contract").SurfaceServerMessage[] = [
		{
			type: "surface",
			spec: specValue,
			...(conversationId !== undefined ? { conversationId } : {}),
		},
	];

	// Idempotent: only emit subChange if not already subscribed.
	const key = subKey(surfaceId, conversationId);
	if (!connSubs.has(key)) {
		return {
			kind: "surface",
			replies,
			subChange: {
				op: "add",
				surfaceId,
				...(conversationId !== undefined ? { conversationId } : {}),
			},
		};
	}
	return { kind: "surface", replies };
}

function handleUnsubscribe(surfaceId: string, conversationId?: string): SurfaceRouteResult {
	return {
		kind: "surface",
		replies: [],
		subChange: {
			op: "remove",
			surfaceId,
			...(conversationId !== undefined ? { conversationId } : {}),
		},
	};
}

function handleInvoke(
	registry: SurfaceRegistry,
	surfaceId: string,
	actionId: string,
	payload?: unknown,
	conversationId?: string,
): SurfaceRouteResult {
	const provider = registry.getSurface(surfaceId);
	if (!provider) {
		return {
			kind: "surface",
			replies: [{ type: "error", surfaceId, message: `Unknown surface: ${surfaceId}` }],
		};
	}
	return {
		kind: "surface",
		replies: [],
		invoke: {
			surfaceId,
			actionId,
			payload,
			...(conversationId !== undefined ? { conversationId } : {}),
		},
	};
}