@deepseekharness-mobile/companion 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +25 -2
- package/cordis.patch.yml +20 -0
- package/dist/core/devices.d.ts +35 -0
- package/dist/core/devices.d.ts.map +1 -0
- package/dist/core/devices.js +90 -0
- package/dist/core/devices.js.map +1 -0
- package/dist/core/pairing.d.ts +21 -0
- package/dist/core/pairing.d.ts.map +1 -0
- package/dist/core/pairing.js +74 -0
- package/dist/core/pairing.js.map +1 -0
- package/dist/dsh/errors.d.ts +3 -0
- package/dist/dsh/errors.d.ts.map +1 -0
- package/dist/dsh/errors.js +15 -0
- package/dist/dsh/errors.js.map +1 -0
- package/dist/dsh/lifecycle.d.ts +37 -0
- package/dist/dsh/lifecycle.d.ts.map +1 -0
- package/dist/dsh/lifecycle.js +95 -0
- package/dist/dsh/lifecycle.js.map +1 -0
- package/dist/dsh/models.d.ts +39 -0
- package/dist/dsh/models.d.ts.map +1 -0
- package/dist/dsh/models.js +171 -0
- package/dist/dsh/models.js.map +1 -0
- package/dist/dsh/prompt.d.ts +8 -0
- package/dist/dsh/prompt.d.ts.map +1 -0
- package/dist/dsh/prompt.js +25 -0
- package/dist/dsh/prompt.js.map +1 -0
- package/dist/dsh/providers.d.ts +25 -0
- package/dist/dsh/providers.d.ts.map +1 -0
- package/dist/dsh/providers.js +315 -0
- package/dist/dsh/providers.js.map +1 -0
- package/dist/dsh/question-bridge.d.ts +25 -0
- package/dist/dsh/question-bridge.d.ts.map +1 -0
- package/dist/dsh/question-bridge.js +140 -0
- package/dist/dsh/question-bridge.js.map +1 -0
- package/dist/dsh/questions.d.ts +15 -0
- package/dist/dsh/questions.d.ts.map +1 -0
- package/dist/dsh/questions.js +126 -0
- package/dist/dsh/questions.js.map +1 -0
- package/dist/dsh/session-activity.d.ts +5 -0
- package/dist/dsh/session-activity.d.ts.map +1 -0
- package/dist/dsh/session-activity.js +100 -0
- package/dist/dsh/session-activity.js.map +1 -0
- package/dist/dsh/session-source.d.ts +19 -0
- package/dist/dsh/session-source.d.ts.map +1 -0
- package/dist/dsh/session-source.js +104 -0
- package/dist/dsh/session-source.js.map +1 -0
- package/dist/dsh/sessions.d.ts +10 -0
- package/dist/dsh/sessions.d.ts.map +1 -0
- package/dist/dsh/sessions.js +51 -0
- package/dist/dsh/sessions.js.map +1 -0
- package/dist/dsh/slash-commands.d.ts +15 -0
- package/dist/dsh/slash-commands.d.ts.map +1 -0
- package/dist/dsh/slash-commands.js +60 -0
- package/dist/dsh/slash-commands.js.map +1 -0
- package/dist/dsh/transcript.d.ts +28 -0
- package/dist/dsh/transcript.d.ts.map +1 -0
- package/dist/dsh/transcript.js +211 -0
- package/dist/dsh/transcript.js.map +1 -0
- package/dist/dsh/workspaces.d.ts +20 -0
- package/dist/dsh/workspaces.d.ts.map +1 -0
- package/dist/dsh/workspaces.js +123 -0
- package/dist/dsh/workspaces.js.map +1 -0
- package/dist/host-api.d.ts +148 -0
- package/dist/host-api.d.ts.map +1 -0
- package/dist/host-api.js +54 -0
- package/dist/host-api.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +268 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +5 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +40 -0
- package/dist/log.js.map +1 -0
- package/dist/open-browser.d.ts +3 -0
- package/dist/open-browser.d.ts.map +1 -0
- package/dist/open-browser.js +46 -0
- package/dist/open-browser.js.map +1 -0
- package/dist/pairing-message.d.ts +16 -0
- package/dist/pairing-message.d.ts.map +1 -0
- package/dist/pairing-message.js +38 -0
- package/dist/pairing-message.js.map +1 -0
- package/dist/persist/devices-file.d.ts +8 -0
- package/dist/persist/devices-file.d.ts.map +1 -0
- package/dist/persist/devices-file.js +47 -0
- package/dist/persist/devices-file.js.map +1 -0
- package/dist/probe.d.ts +25 -0
- package/dist/probe.d.ts.map +1 -0
- package/dist/probe.js +143 -0
- package/dist/probe.js.map +1 -0
- package/dist/public-origin.d.ts +11 -0
- package/dist/public-origin.d.ts.map +1 -0
- package/dist/public-origin.js +79 -0
- package/dist/public-origin.js.map +1 -0
- package/dist/qr-text.d.ts +2 -0
- package/dist/qr-text.d.ts.map +1 -0
- package/dist/qr-text.js +31 -0
- package/dist/qr-text.js.map +1 -0
- package/dist/routes/origin.d.ts +17 -0
- package/dist/routes/origin.d.ts.map +1 -0
- package/dist/routes/origin.js +85 -0
- package/dist/routes/origin.js.map +1 -0
- package/dist/routes/static.d.ts +9 -0
- package/dist/routes/static.d.ts.map +1 -0
- package/dist/routes/static.js +129 -0
- package/dist/routes/static.js.map +1 -0
- package/dist/routes/web-root.d.ts +2 -0
- package/dist/routes/web-root.d.ts.map +1 -0
- package/dist/routes/web-root.js +21 -0
- package/dist/routes/web-root.js.map +1 -0
- package/dist/server.d.ts +140 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +829 -0
- package/dist/server.js.map +1 -0
- package/dist/web/apple-touch-icon-precomposed.png +0 -0
- package/dist/web/apple-touch-icon.png +0 -0
- package/dist/web/assets/index-NOGXMIsS.js +65 -0
- package/dist/web/assets/index-NOGXMIsS.js.map +1 -0
- package/dist/web/assets/index-s7zoNGmx.css +1 -0
- package/dist/web/fonts/Montserrat-OFL.txt +93 -0
- package/dist/web/fonts/montserrat-medium.woff2 +0 -0
- package/dist/web/fonts/montserrat-regular.woff2 +0 -0
- package/dist/web/icon-192.png +0 -0
- package/dist/web/icon-512.png +0 -0
- package/dist/web/icon-maskable-512.png +0 -0
- package/dist/web/icon-maskable.svg +15 -0
- package/dist/web/icon.svg +6 -0
- package/dist/web/index.html +25 -0
- package/dist/web/manifest.webmanifest +37 -0
- package/package.json +60 -3
package/dist/server.js
ADDED
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
import { hostname } from 'node:os';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import QRCode from 'qrcode';
|
|
4
|
+
import { WebSocketServer } from 'ws';
|
|
5
|
+
import { HEARTBEAT_INTERVAL_MS, MAX_FRAME_BYTES, MAX_FRAMES_PER_MINUTE, PROTOCOL_VERSION, WEBSOCKET_PATH, buildPairingUrl, isSupportedVersion, newMessageId, parseClientMessage, } from '@deepseekharness-mobile/protocol';
|
|
6
|
+
import { log } from './log.js';
|
|
7
|
+
import { serveStatic } from './routes/static.js';
|
|
8
|
+
import { noticeHeadline, originNotice, publicOriginFor, } from './routes/origin.js';
|
|
9
|
+
import { toWireSession } from './dsh/session-source.js';
|
|
10
|
+
export const DEFAULT_PORT = 6768;
|
|
11
|
+
const HOST = '127.0.0.1';
|
|
12
|
+
const DEFAULT_LIMITS = {
|
|
13
|
+
maxConnections: 50,
|
|
14
|
+
maxDevices: 32,
|
|
15
|
+
handshakeTimeoutMs: 10_000,
|
|
16
|
+
heartbeatIntervalMs: HEARTBEAT_INTERVAL_MS,
|
|
17
|
+
flushIntervalMs: 30_000,
|
|
18
|
+
};
|
|
19
|
+
export function reapIdleClients(clients, alive) {
|
|
20
|
+
for (const client of clients) {
|
|
21
|
+
if (!alive.has(client)) {
|
|
22
|
+
client.terminate();
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
alive.delete(client);
|
|
26
|
+
try {
|
|
27
|
+
client.ping();
|
|
28
|
+
}
|
|
29
|
+
catch { }
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function settleProvider(socket, id, run, fallback, type) {
|
|
33
|
+
const outcome = await run();
|
|
34
|
+
if (socket.readyState !== socket.OPEN)
|
|
35
|
+
return;
|
|
36
|
+
if (!outcome.ok) {
|
|
37
|
+
send(socket, errorMessage(id, outcome.code ?? 'internal', outcome.message ?? 'The provider could not be configured.'));
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const frame = {
|
|
41
|
+
v: PROTOCOL_VERSION,
|
|
42
|
+
id,
|
|
43
|
+
at: Date.now(),
|
|
44
|
+
...(outcome.connection ?? fallback),
|
|
45
|
+
};
|
|
46
|
+
send(socket, type === 'provider.connected'
|
|
47
|
+
? { ...frame, type: 'provider.connected' }
|
|
48
|
+
: { ...frame, type: 'provider.disconnected' });
|
|
49
|
+
}
|
|
50
|
+
function send(socket, message) {
|
|
51
|
+
socket.send(JSON.stringify(message));
|
|
52
|
+
}
|
|
53
|
+
function errorMessage(id, code, message, capabilities) {
|
|
54
|
+
return {
|
|
55
|
+
v: PROTOCOL_VERSION,
|
|
56
|
+
id,
|
|
57
|
+
at: Date.now(),
|
|
58
|
+
type: 'error',
|
|
59
|
+
code,
|
|
60
|
+
message,
|
|
61
|
+
...(capabilities ? { capabilities } : {}),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function toDeviceInfo(device) {
|
|
65
|
+
return {
|
|
66
|
+
id: device.id,
|
|
67
|
+
name: device.name,
|
|
68
|
+
createdAt: device.createdAt,
|
|
69
|
+
lastSeenAt: device.lastSeenAt,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function escapeHtml(value) {
|
|
73
|
+
return value
|
|
74
|
+
.replace(/&/g, '&')
|
|
75
|
+
.replace(/</g, '<')
|
|
76
|
+
.replace(/>/g, '>')
|
|
77
|
+
.replace(/"/g, '"');
|
|
78
|
+
}
|
|
79
|
+
function pairingPage(link, code, expiresAt, qrDataUrl, notice) {
|
|
80
|
+
const banner = notice
|
|
81
|
+
? `<div class="warn ${notice.severity}"><strong>${escapeHtml(noticeHeadline(notice))}</strong><br>${escapeHtml(notice.message)}</div>`
|
|
82
|
+
: '';
|
|
83
|
+
return `<!doctype html>
|
|
84
|
+
<html lang="en">
|
|
85
|
+
<meta charset="utf-8">
|
|
86
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
87
|
+
<title>Pair DSH Mobile</title>
|
|
88
|
+
<style>
|
|
89
|
+
@font-face {
|
|
90
|
+
font-family: 'Montserrat';
|
|
91
|
+
font-style: normal;
|
|
92
|
+
font-weight: 400;
|
|
93
|
+
font-display: swap;
|
|
94
|
+
src: url(/fonts/montserrat-regular.woff2) format('woff2');
|
|
95
|
+
}
|
|
96
|
+
:root { color-scheme: dark; }
|
|
97
|
+
body { margin:0; min-height:100dvh; display:grid; place-items:center; background:#151517; color:#f9fafb;
|
|
98
|
+
font-family: 'Montserrat', system-ui, -apple-system, "Segoe UI", sans-serif; font-weight:400; }
|
|
99
|
+
main { text-align:center; padding:32px 24px; max-width:560px; }
|
|
100
|
+
h1 { font-size:20px; font-weight:500; margin:0 0 8px; }
|
|
101
|
+
p { color:#adb2b8; margin:0 0 24px; font-size:14px; }
|
|
102
|
+
.warn { text-align:left; margin:0 0 20px; padding:12px 14px; border-radius:12px;
|
|
103
|
+
border:1px solid color-mix(in srgb, #f25a5a 55%, transparent); background:#1f1416; color:#f25a5a;
|
|
104
|
+
font-size:13px; line-height:1.5; }
|
|
105
|
+
.warn.warning { border-color: color-mix(in srgb, #f59e0b 55%, transparent); background:#1c1508; color:#f59e0b; }
|
|
106
|
+
img { width:min(320px,80vw); height:auto; background:#fff; border-radius:16px; padding:12px; }
|
|
107
|
+
code { display:block; margin:20px auto 0; font-size:15px; letter-spacing:2px; color:#f9fafb;
|
|
108
|
+
font-family: ui-monospace, 'SFMono-Regular', Menlo, monospace; word-break:break-all; }
|
|
109
|
+
small { display:block; margin-top:12px; color:#adb2b8; }
|
|
110
|
+
small.link { word-break:break-all; }
|
|
111
|
+
</style>
|
|
112
|
+
<main>
|
|
113
|
+
${banner}
|
|
114
|
+
<h1>Pair DSH Mobile</h1>
|
|
115
|
+
<p>Scan this code with your phone camera.</p>
|
|
116
|
+
<img src="${qrDataUrl}" alt="Pairing QR code">
|
|
117
|
+
<code>${escapeHtml(code)}</code>
|
|
118
|
+
<small>Expires at ${escapeHtml(new Date(expiresAt).toLocaleTimeString())} · single use</small>
|
|
119
|
+
<small class="link">${escapeHtml(link)}</small>
|
|
120
|
+
</main>
|
|
121
|
+
</html>`;
|
|
122
|
+
}
|
|
123
|
+
export function startCompanionServer(deps) {
|
|
124
|
+
const { pairing, devices } = deps;
|
|
125
|
+
const limits = { ...DEFAULT_LIMITS, ...deps.limits };
|
|
126
|
+
const capabilities = () => typeof deps.capabilities === 'function' ? deps.capabilities() : (deps.capabilities ?? []);
|
|
127
|
+
const requestedPort = deps.port ?? DEFAULT_PORT;
|
|
128
|
+
const baseHostInfo = {
|
|
129
|
+
id: hostname(),
|
|
130
|
+
name: 'DSH Mobile',
|
|
131
|
+
version: '0.1.0',
|
|
132
|
+
platform: process.platform,
|
|
133
|
+
};
|
|
134
|
+
let listeningPort = requestedPort;
|
|
135
|
+
const originFor = (req) => publicOriginFor({
|
|
136
|
+
host: req.headers.host,
|
|
137
|
+
forwardedProto: req.headers['x-forwarded-proto'],
|
|
138
|
+
fallbackPort: listeningPort,
|
|
139
|
+
configured: deps.publicUrl,
|
|
140
|
+
});
|
|
141
|
+
const localOrigin = () => deps.publicUrl ?? `http://${HOST}:${listeningPort}`;
|
|
142
|
+
const http = createServer((req, res) => {
|
|
143
|
+
const url = req.url ?? '/';
|
|
144
|
+
const json = { 'content-type': 'application/json; charset=utf-8' };
|
|
145
|
+
if (url === '/health') {
|
|
146
|
+
const origin = originFor(req);
|
|
147
|
+
const notice = originNotice(origin);
|
|
148
|
+
res.writeHead(200, json);
|
|
149
|
+
res.end(JSON.stringify({
|
|
150
|
+
ok: true,
|
|
151
|
+
name: 'dsh-mobile-companion',
|
|
152
|
+
proto: PROTOCOL_VERSION,
|
|
153
|
+
capabilities: capabilities(),
|
|
154
|
+
devices: devices.list().length,
|
|
155
|
+
origin,
|
|
156
|
+
...(notice ? { warning: notice.message, warningSeverity: notice.severity } : {}),
|
|
157
|
+
}));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (url === '/probe' && deps.report) {
|
|
161
|
+
res.writeHead(200, json);
|
|
162
|
+
res.end(JSON.stringify(deps.report(), null, 2));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (url === '/pair') {
|
|
166
|
+
try {
|
|
167
|
+
const existing = pairing.pending()[0] ?? pairing.issue();
|
|
168
|
+
const origin = originFor(req);
|
|
169
|
+
const link = buildPairingUrl(origin, existing.code);
|
|
170
|
+
const notice = originNotice(origin);
|
|
171
|
+
void QRCode.toDataURL(link, { margin: 1, width: 320, errorCorrectionLevel: 'M' })
|
|
172
|
+
.then((dataUrl) => {
|
|
173
|
+
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
174
|
+
res.end(pairingPage(link, existing.code, existing.expiresAt, dataUrl, notice));
|
|
175
|
+
})
|
|
176
|
+
.catch((error) => {
|
|
177
|
+
log(`could not generate the QR: ${String(error)}`);
|
|
178
|
+
res.writeHead(500, json);
|
|
179
|
+
res.end(JSON.stringify({ error: 'qr-failed' }));
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
log(`/pair failed: ${String(error)}`);
|
|
184
|
+
res.writeHead(400, json);
|
|
185
|
+
res.end(JSON.stringify({ error: 'bad-request' }));
|
|
186
|
+
}
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const asset = serveStatic(deps.webRoot, url);
|
|
190
|
+
if (asset.status === 200 && asset.body) {
|
|
191
|
+
res.writeHead(200, asset.headers);
|
|
192
|
+
res.end(asset.body);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
res.writeHead(404, json);
|
|
196
|
+
res.end(JSON.stringify({ error: 'not-found', path: url }));
|
|
197
|
+
});
|
|
198
|
+
const wss = new WebSocketServer({
|
|
199
|
+
server: http,
|
|
200
|
+
path: WEBSOCKET_PATH,
|
|
201
|
+
maxPayload: MAX_FRAME_BYTES,
|
|
202
|
+
});
|
|
203
|
+
wss.on('error', (error) => log(`WebSocket server error: ${error.message}`));
|
|
204
|
+
const clients = new Set();
|
|
205
|
+
const deviceSockets = new Map();
|
|
206
|
+
const alive = new WeakSet();
|
|
207
|
+
function trackDevice(socket, device) {
|
|
208
|
+
clients.add(socket);
|
|
209
|
+
const peers = deviceSockets.get(device.id) ?? new Set();
|
|
210
|
+
peers.add(socket);
|
|
211
|
+
deviceSockets.set(device.id, peers);
|
|
212
|
+
}
|
|
213
|
+
function untrackDevice(socket) {
|
|
214
|
+
clients.delete(socket);
|
|
215
|
+
for (const [id, peers] of deviceSockets) {
|
|
216
|
+
if (!peers.delete(socket))
|
|
217
|
+
continue;
|
|
218
|
+
if (peers.size === 0)
|
|
219
|
+
deviceSockets.delete(id);
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function broadcast(message) {
|
|
224
|
+
for (const client of clients) {
|
|
225
|
+
if (client.readyState === client.OPEN)
|
|
226
|
+
send(client, message);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
wss.on('connection', (socket) => {
|
|
230
|
+
if (wss.clients.size > limits.maxConnections) {
|
|
231
|
+
socket.close(1013, 'too many connections');
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const connection = {
|
|
235
|
+
device: undefined,
|
|
236
|
+
sawHello: false,
|
|
237
|
+
frameCount: 0,
|
|
238
|
+
windowStart: Date.now(),
|
|
239
|
+
};
|
|
240
|
+
alive.add(socket);
|
|
241
|
+
socket.on('pong', () => alive.add(socket));
|
|
242
|
+
const handshakeTimer = setTimeout(() => {
|
|
243
|
+
if (!connection.sawHello)
|
|
244
|
+
socket.close(1008, 'hello timeout');
|
|
245
|
+
}, limits.handshakeTimeoutMs);
|
|
246
|
+
log('connection open');
|
|
247
|
+
const subscriptions = new Map();
|
|
248
|
+
socket.on('message', (data) => {
|
|
249
|
+
void handleFrame(data).catch((error) => {
|
|
250
|
+
log(`failed frame: ${String(error)}`);
|
|
251
|
+
if (socket.readyState === socket.OPEN) {
|
|
252
|
+
send(socket, errorMessage(newMessageId(), 'internal', 'Internal host failure.'));
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
async function handleFrame(data) {
|
|
257
|
+
const now = Date.now();
|
|
258
|
+
if (now - connection.windowStart > 60_000) {
|
|
259
|
+
connection.windowStart = now;
|
|
260
|
+
connection.frameCount = 0;
|
|
261
|
+
}
|
|
262
|
+
connection.frameCount += 1;
|
|
263
|
+
if (connection.frameCount > MAX_FRAMES_PER_MINUTE) {
|
|
264
|
+
send(socket, errorMessage(newMessageId(), 'rate-limited', 'Too many frames per minute.'));
|
|
265
|
+
socket.close();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const message = parseClientMessage(data.toString('utf8'));
|
|
269
|
+
if (!message) {
|
|
270
|
+
send(socket, errorMessage(newMessageId(), 'bad-request', 'The frame does not validate against the schema.'));
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (message.type === 'ping') {
|
|
274
|
+
send(socket, { v: PROTOCOL_VERSION, id: message.id, at: Date.now(), type: 'pong' });
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (message.type === 'hello') {
|
|
278
|
+
if (!isSupportedVersion(message.proto)) {
|
|
279
|
+
send(socket, errorMessage(message.id, 'proto-unsupported', `This host speaks dsh-mobile/${PROTOCOL_VERSION}; you asked for ${message.proto}.`, capabilities()));
|
|
280
|
+
socket.close();
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
connection.sawHello = true;
|
|
284
|
+
clearTimeout(handshakeTimer);
|
|
285
|
+
if (message.deviceToken) {
|
|
286
|
+
const device = devices.authenticate(message.deviceToken);
|
|
287
|
+
if (!device) {
|
|
288
|
+
send(socket, errorMessage(message.id, 'auth-invalid', 'The device token is not valid.'));
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
connection.device = device;
|
|
292
|
+
trackDevice(socket, device);
|
|
293
|
+
}
|
|
294
|
+
send(socket, {
|
|
295
|
+
v: PROTOCOL_VERSION,
|
|
296
|
+
id: message.id,
|
|
297
|
+
at: Date.now(),
|
|
298
|
+
type: 'hello.ok',
|
|
299
|
+
host: { ...baseHostInfo, capabilities: capabilities() },
|
|
300
|
+
...(connection.device ? { device: toDeviceInfo(connection.device) } : {}),
|
|
301
|
+
});
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (message.type === 'pair.redeem') {
|
|
305
|
+
if (!connection.sawHello) {
|
|
306
|
+
send(socket, errorMessage(message.id, 'auth-required', 'Send `hello` before pairing.'));
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (devices.list().length >= limits.maxDevices) {
|
|
310
|
+
send(socket, errorMessage(message.id, 'denied', 'The device limit was reached. Revoke a device and try again.'));
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const outcome = pairing.redeem(message.code);
|
|
314
|
+
if (outcome !== 'ok') {
|
|
315
|
+
const reason = outcome === 'expired'
|
|
316
|
+
? 'That code expired. Generate a new one on the desktop.'
|
|
317
|
+
: outcome === 'consumed'
|
|
318
|
+
? 'That code was already used. Generate a new one.'
|
|
319
|
+
: 'Unknown code.';
|
|
320
|
+
send(socket, errorMessage(message.id, 'auth-invalid', reason));
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const enrolled = devices.enroll(message.deviceName, message.platform);
|
|
324
|
+
connection.device = enrolled.device;
|
|
325
|
+
trackDevice(socket, enrolled.device);
|
|
326
|
+
log(`device paired: ${enrolled.device.name} (${enrolled.device.platform})`);
|
|
327
|
+
send(socket, {
|
|
328
|
+
v: PROTOCOL_VERSION,
|
|
329
|
+
id: message.id,
|
|
330
|
+
at: Date.now(),
|
|
331
|
+
type: 'pair.ok',
|
|
332
|
+
deviceToken: enrolled.token,
|
|
333
|
+
host: { ...baseHostInfo, capabilities: capabilities() },
|
|
334
|
+
device: toDeviceInfo(enrolled.device),
|
|
335
|
+
});
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (!connection.device) {
|
|
339
|
+
send(socket, errorMessage(message.id, 'auth-required', `Without a paired device, ${message.type} is not available.`));
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (message.type === 'devices.list') {
|
|
343
|
+
send(socket, {
|
|
344
|
+
v: PROTOCOL_VERSION,
|
|
345
|
+
id: message.id,
|
|
346
|
+
at: Date.now(),
|
|
347
|
+
type: 'devices.ok',
|
|
348
|
+
devices: devices.list().map(toDeviceInfo),
|
|
349
|
+
});
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (message.type === 'devices.revoke') {
|
|
353
|
+
const target = devices.list().find((device) => device.id === message.deviceId);
|
|
354
|
+
if (!target) {
|
|
355
|
+
send(socket, errorMessage(message.id, 'not-found', 'Unknown device.'));
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
devices.revoke(target.id);
|
|
359
|
+
const updated = devices.list().map(toDeviceInfo);
|
|
360
|
+
const peers = [...(deviceSockets.get(target.id) ?? [])];
|
|
361
|
+
send(socket, {
|
|
362
|
+
v: PROTOCOL_VERSION,
|
|
363
|
+
id: message.id,
|
|
364
|
+
at: Date.now(),
|
|
365
|
+
type: 'devices.revoked',
|
|
366
|
+
deviceId: target.id,
|
|
367
|
+
});
|
|
368
|
+
for (const peer of peers) {
|
|
369
|
+
if (peer !== socket) {
|
|
370
|
+
send(peer, {
|
|
371
|
+
v: PROTOCOL_VERSION,
|
|
372
|
+
id: newMessageId(),
|
|
373
|
+
at: Date.now(),
|
|
374
|
+
type: 'devices.revoked',
|
|
375
|
+
deviceId: target.id,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
untrackDevice(peer);
|
|
379
|
+
peer.close(1008, 'revoked');
|
|
380
|
+
}
|
|
381
|
+
broadcast({
|
|
382
|
+
v: PROTOCOL_VERSION,
|
|
383
|
+
id: newMessageId(),
|
|
384
|
+
at: Date.now(),
|
|
385
|
+
type: 'devices.changed',
|
|
386
|
+
devices: updated,
|
|
387
|
+
});
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (message.type === 'providers.list') {
|
|
391
|
+
if (!deps.listProviders) {
|
|
392
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot list providers.'));
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
const providers = await deps.listProviders();
|
|
396
|
+
if (socket.readyState !== socket.OPEN)
|
|
397
|
+
return;
|
|
398
|
+
send(socket, {
|
|
399
|
+
v: PROTOCOL_VERSION,
|
|
400
|
+
id: message.id,
|
|
401
|
+
at: Date.now(),
|
|
402
|
+
type: 'providers.ok',
|
|
403
|
+
providers,
|
|
404
|
+
});
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (message.type === 'provider.test') {
|
|
408
|
+
if (!deps.testProvider) {
|
|
409
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot test providers.'));
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
const result = await deps.testProvider({
|
|
413
|
+
provider: message.provider,
|
|
414
|
+
apiKey: message.apiKey,
|
|
415
|
+
});
|
|
416
|
+
if (socket.readyState !== socket.OPEN)
|
|
417
|
+
return;
|
|
418
|
+
send(socket, {
|
|
419
|
+
v: PROTOCOL_VERSION,
|
|
420
|
+
id: message.id,
|
|
421
|
+
at: Date.now(),
|
|
422
|
+
type: 'provider.tested',
|
|
423
|
+
...result,
|
|
424
|
+
});
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
if (message.type === 'provider.connect') {
|
|
428
|
+
const connect = deps.connectProvider;
|
|
429
|
+
if (!connect) {
|
|
430
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot connect providers.'));
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
await settleProvider(socket, message.id, () => connect({ provider: message.provider, apiKey: message.apiKey }), { provider: message.provider, credential: 'set' }, 'provider.connected');
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (message.type === 'provider.disconnect') {
|
|
437
|
+
const disconnect = deps.disconnectProvider;
|
|
438
|
+
if (!disconnect) {
|
|
439
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot disconnect providers.'));
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
await settleProvider(socket, message.id, () => disconnect({ provider: message.provider }), { provider: message.provider, credential: 'unknown' }, 'provider.disconnected');
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (message.type === 'workspace.list') {
|
|
446
|
+
const workspaces = deps.listWorkspaces ? await deps.listWorkspaces() : [];
|
|
447
|
+
if (socket.readyState !== socket.OPEN)
|
|
448
|
+
return;
|
|
449
|
+
send(socket, {
|
|
450
|
+
v: PROTOCOL_VERSION,
|
|
451
|
+
id: message.id,
|
|
452
|
+
at: Date.now(),
|
|
453
|
+
type: 'workspace.ok',
|
|
454
|
+
workspaces,
|
|
455
|
+
});
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (message.type === 'models.list') {
|
|
459
|
+
if (!deps.listModels) {
|
|
460
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot list models.'));
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const catalog = await deps.listModels();
|
|
464
|
+
if (socket.readyState !== socket.OPEN)
|
|
465
|
+
return;
|
|
466
|
+
send(socket, {
|
|
467
|
+
v: PROTOCOL_VERSION,
|
|
468
|
+
id: message.id,
|
|
469
|
+
at: Date.now(),
|
|
470
|
+
type: 'models.ok',
|
|
471
|
+
catalog,
|
|
472
|
+
});
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (message.type === 'model.select') {
|
|
476
|
+
if (!deps.selectModel) {
|
|
477
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot select models.'));
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const selected = await deps.selectModel({
|
|
481
|
+
sessionId: message.sessionId,
|
|
482
|
+
provider: message.provider,
|
|
483
|
+
model: message.model,
|
|
484
|
+
...(message.reasoningEffort ? { reasoningEffort: message.reasoningEffort } : {}),
|
|
485
|
+
});
|
|
486
|
+
if (socket.readyState !== socket.OPEN)
|
|
487
|
+
return;
|
|
488
|
+
if (!selected.ok || !selected.selection) {
|
|
489
|
+
send(socket, errorMessage(message.id, selected.code === 'unavailable' || selected.code === 'no-service'
|
|
490
|
+
? 'bad-request'
|
|
491
|
+
: selected.code === 'not-found'
|
|
492
|
+
? 'not-found'
|
|
493
|
+
: 'internal', selected.message ?? 'The model could not be changed.'));
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
send(socket, {
|
|
497
|
+
v: PROTOCOL_VERSION,
|
|
498
|
+
id: message.id,
|
|
499
|
+
at: Date.now(),
|
|
500
|
+
type: 'model.selected',
|
|
501
|
+
sessionId: message.sessionId,
|
|
502
|
+
selection: selected.selection,
|
|
503
|
+
});
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (message.type === 'session.create') {
|
|
507
|
+
if (!deps.createSession) {
|
|
508
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot create sessions.'));
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const created = await deps.createSession({ workspaceId: message.workspaceId });
|
|
512
|
+
if (socket.readyState !== socket.OPEN)
|
|
513
|
+
return;
|
|
514
|
+
if (!created.ok || !created.sessionId) {
|
|
515
|
+
send(socket, errorMessage(message.id, created.code === 'no-service'
|
|
516
|
+
? 'bad-request'
|
|
517
|
+
: created.code === 'no-folder'
|
|
518
|
+
? 'not-found'
|
|
519
|
+
: 'internal', created.message ?? 'The session could not be created.'));
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
send(socket, {
|
|
523
|
+
v: PROTOCOL_VERSION,
|
|
524
|
+
id: message.id,
|
|
525
|
+
at: Date.now(),
|
|
526
|
+
type: 'session.created',
|
|
527
|
+
sessionId: created.sessionId,
|
|
528
|
+
});
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
if (message.type === 'workspace.archive') {
|
|
532
|
+
if (!deps.archiveSession) {
|
|
533
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot archive sessions.'));
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
const archived = await deps.archiveSession({
|
|
537
|
+
sessionId: message.sessionId,
|
|
538
|
+
...(message.stopActivity === true ? { stopActivity: true } : {}),
|
|
539
|
+
});
|
|
540
|
+
if (socket.readyState !== socket.OPEN)
|
|
541
|
+
return;
|
|
542
|
+
if (!archived.ok) {
|
|
543
|
+
send(socket, errorMessage(message.id, archived.code === 'active'
|
|
544
|
+
? 'denied'
|
|
545
|
+
: archived.code === 'not-found'
|
|
546
|
+
? 'not-found'
|
|
547
|
+
: archived.code === 'no-service'
|
|
548
|
+
? 'bad-request'
|
|
549
|
+
: 'internal', archived.message ?? 'The session could not be archived.'));
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
send(socket, {
|
|
553
|
+
v: PROTOCOL_VERSION,
|
|
554
|
+
id: message.id,
|
|
555
|
+
at: Date.now(),
|
|
556
|
+
type: 'workspace.archived',
|
|
557
|
+
sessionId: message.sessionId,
|
|
558
|
+
});
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
if (message.type === 'workspace.unarchive') {
|
|
562
|
+
if (!deps.unarchiveSession) {
|
|
563
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot unarchive sessions.'));
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
const restored = await deps.unarchiveSession({ sessionId: message.sessionId });
|
|
567
|
+
if (socket.readyState !== socket.OPEN)
|
|
568
|
+
return;
|
|
569
|
+
if (!restored.ok) {
|
|
570
|
+
send(socket, errorMessage(message.id, restored.code === 'no-service'
|
|
571
|
+
? 'bad-request'
|
|
572
|
+
: restored.code === 'not-found'
|
|
573
|
+
? 'not-found'
|
|
574
|
+
: 'internal', restored.message ?? 'The session could not be restored.'));
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
send(socket, {
|
|
578
|
+
v: PROTOCOL_VERSION,
|
|
579
|
+
id: message.id,
|
|
580
|
+
at: Date.now(),
|
|
581
|
+
type: 'workspace.unarchived',
|
|
582
|
+
sessionId: message.sessionId,
|
|
583
|
+
});
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
if (message.type === 'sessions.list') {
|
|
587
|
+
const records = deps.listSessions
|
|
588
|
+
? await deps.listSessions({
|
|
589
|
+
...(message.includeArchived === true ? { includeArchived: true } : {}),
|
|
590
|
+
})
|
|
591
|
+
: [];
|
|
592
|
+
if (socket.readyState !== socket.OPEN)
|
|
593
|
+
return;
|
|
594
|
+
send(socket, {
|
|
595
|
+
v: PROTOCOL_VERSION,
|
|
596
|
+
id: message.id,
|
|
597
|
+
at: Date.now(),
|
|
598
|
+
type: 'sessions.ok',
|
|
599
|
+
sessions: records.map(toWireSession),
|
|
600
|
+
});
|
|
601
|
+
for (const batch of deps.listQuestions?.() ?? []) {
|
|
602
|
+
if (socket.readyState !== socket.OPEN)
|
|
603
|
+
return;
|
|
604
|
+
send(socket, {
|
|
605
|
+
v: PROTOCOL_VERSION,
|
|
606
|
+
id: newMessageId(),
|
|
607
|
+
at: Date.now(),
|
|
608
|
+
type: 'question.pending',
|
|
609
|
+
question: batch,
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
if (message.type === 'question.answer') {
|
|
615
|
+
if (!deps.answerQuestion) {
|
|
616
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot answer questions.'));
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
const outcome = deps.answerQuestion({
|
|
620
|
+
callId: message.callId,
|
|
621
|
+
sessionId: message.sessionId,
|
|
622
|
+
answers: message.answers,
|
|
623
|
+
});
|
|
624
|
+
if (!outcome.ok) {
|
|
625
|
+
send(socket, errorMessage(message.id, outcome.code ?? 'internal', outcome.message ?? 'Could not answer.'));
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
send(socket, {
|
|
629
|
+
v: PROTOCOL_VERSION,
|
|
630
|
+
id: message.id,
|
|
631
|
+
at: Date.now(),
|
|
632
|
+
type: 'answer.ok',
|
|
633
|
+
callId: message.callId,
|
|
634
|
+
});
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
if (message.type === 'commands.list') {
|
|
638
|
+
if (!deps.listCommands) {
|
|
639
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot list the commands.'));
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
const commands = await deps.listCommands(message.sessionId);
|
|
643
|
+
if (socket.readyState !== socket.OPEN)
|
|
644
|
+
return;
|
|
645
|
+
send(socket, {
|
|
646
|
+
v: PROTOCOL_VERSION,
|
|
647
|
+
id: message.id,
|
|
648
|
+
at: Date.now(),
|
|
649
|
+
type: 'commands.ok',
|
|
650
|
+
commands,
|
|
651
|
+
});
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
if (message.type === 'command.run') {
|
|
655
|
+
if (!deps.runCommand) {
|
|
656
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot run commands.'));
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
const outcome = await deps.runCommand({
|
|
660
|
+
sessionId: message.sessionId,
|
|
661
|
+
line: message.line,
|
|
662
|
+
});
|
|
663
|
+
if (socket.readyState !== socket.OPEN)
|
|
664
|
+
return;
|
|
665
|
+
if (!outcome.ok) {
|
|
666
|
+
send(socket, errorMessage(message.id, outcome.code ?? 'internal', outcome.message ?? 'The command could not run.'));
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
const result = outcome.result ?? { kind: 'success' };
|
|
670
|
+
send(socket, {
|
|
671
|
+
v: PROTOCOL_VERSION,
|
|
672
|
+
id: message.id,
|
|
673
|
+
at: Date.now(),
|
|
674
|
+
type: 'command.ok',
|
|
675
|
+
kind: result.kind,
|
|
676
|
+
...(result.text !== undefined ? { text: result.text } : {}),
|
|
677
|
+
});
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
if (message.type === 'session.activity') {
|
|
681
|
+
if (!deps.readActivity) {
|
|
682
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot read the activity.'));
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
const activity = await deps.readActivity(message.sessionId);
|
|
686
|
+
if (socket.readyState !== socket.OPEN)
|
|
687
|
+
return;
|
|
688
|
+
send(socket, {
|
|
689
|
+
v: PROTOCOL_VERSION,
|
|
690
|
+
id: message.id,
|
|
691
|
+
at: Date.now(),
|
|
692
|
+
type: 'activity.ok',
|
|
693
|
+
sessionId: message.sessionId,
|
|
694
|
+
activity,
|
|
695
|
+
});
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
if (message.type === 'reply.send') {
|
|
699
|
+
if (!deps.sendReply) {
|
|
700
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot write.'));
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
const outcome = await deps.sendReply(message.sessionId, message.text);
|
|
704
|
+
if (socket.readyState !== socket.OPEN)
|
|
705
|
+
return;
|
|
706
|
+
if (!outcome.ok) {
|
|
707
|
+
send(socket, errorMessage(message.id, outcome.code ?? 'internal', outcome.message ?? 'Could not send the message.'));
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
send(socket, {
|
|
711
|
+
v: PROTOCOL_VERSION,
|
|
712
|
+
id: message.id,
|
|
713
|
+
at: Date.now(),
|
|
714
|
+
type: 'reply.ok',
|
|
715
|
+
sessionId: message.sessionId,
|
|
716
|
+
});
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
if (message.type === 'session.history') {
|
|
720
|
+
const page = deps.readHistory
|
|
721
|
+
? await deps.readHistory(message.sessionId, {
|
|
722
|
+
...(message.before !== undefined ? { before: message.before } : {}),
|
|
723
|
+
...(message.limit !== undefined ? { limit: message.limit } : {}),
|
|
724
|
+
})
|
|
725
|
+
: { entries: [], hasMore: false };
|
|
726
|
+
if (socket.readyState !== socket.OPEN)
|
|
727
|
+
return;
|
|
728
|
+
send(socket, {
|
|
729
|
+
v: PROTOCOL_VERSION,
|
|
730
|
+
id: message.id,
|
|
731
|
+
at: Date.now(),
|
|
732
|
+
type: 'history.ok',
|
|
733
|
+
sessionId: message.sessionId,
|
|
734
|
+
entries: page.entries,
|
|
735
|
+
hasMore: page.hasMore,
|
|
736
|
+
});
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
if (message.type === 'session.subscribe') {
|
|
740
|
+
if (!deps.subscribeSession) {
|
|
741
|
+
send(socket, errorMessage(message.id, 'bad-request', 'This host cannot subscribe to sessions.'));
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
subscriptions.get(message.sessionId)?.stop();
|
|
745
|
+
const subscription = deps.subscribeSession(message.sessionId, {
|
|
746
|
+
...(message.since !== undefined ? { since: message.since } : {}),
|
|
747
|
+
onEvent: (entry) => {
|
|
748
|
+
if (socket.readyState !== socket.OPEN)
|
|
749
|
+
return;
|
|
750
|
+
send(socket, {
|
|
751
|
+
v: PROTOCOL_VERSION,
|
|
752
|
+
id: newMessageId(),
|
|
753
|
+
at: Date.now(),
|
|
754
|
+
type: 'session.event',
|
|
755
|
+
sessionId: message.sessionId,
|
|
756
|
+
seq: entry.seq,
|
|
757
|
+
event: entry.event,
|
|
758
|
+
});
|
|
759
|
+
},
|
|
760
|
+
onEnd: () => subscriptions.delete(message.sessionId),
|
|
761
|
+
});
|
|
762
|
+
subscriptions.set(message.sessionId, subscription);
|
|
763
|
+
send(socket, {
|
|
764
|
+
v: PROTOCOL_VERSION,
|
|
765
|
+
id: message.id,
|
|
766
|
+
at: Date.now(),
|
|
767
|
+
type: 'session.ok',
|
|
768
|
+
sessionId: message.sessionId,
|
|
769
|
+
seq: message.since ?? 0,
|
|
770
|
+
});
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
if (message.type === 'run.cancel') {
|
|
774
|
+
const accepted = deps.cancelRun?.(message.sessionId) ?? false;
|
|
775
|
+
if (!accepted) {
|
|
776
|
+
send(socket, errorMessage(message.id, 'not-found', 'No turn is running in that session.'));
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
send(socket, {
|
|
780
|
+
v: PROTOCOL_VERSION,
|
|
781
|
+
id: message.id,
|
|
782
|
+
at: Date.now(),
|
|
783
|
+
type: 'run.ok',
|
|
784
|
+
sessionId: message.sessionId,
|
|
785
|
+
state: 'idle',
|
|
786
|
+
});
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
send(socket, errorMessage(message.id, 'bad-request', `${message.type} is not implemented in this build yet.`));
|
|
790
|
+
}
|
|
791
|
+
socket.on('error', (error) => log(`WebSocket error: ${error.message}`));
|
|
792
|
+
socket.on('close', () => {
|
|
793
|
+
clearTimeout(handshakeTimer);
|
|
794
|
+
untrackDevice(socket);
|
|
795
|
+
for (const [, subscription] of subscriptions)
|
|
796
|
+
subscription.stop();
|
|
797
|
+
subscriptions.clear();
|
|
798
|
+
log('connection closed');
|
|
799
|
+
});
|
|
800
|
+
});
|
|
801
|
+
return new Promise((resolve, reject) => {
|
|
802
|
+
http.once('error', reject);
|
|
803
|
+
http.listen(requestedPort, HOST, () => {
|
|
804
|
+
const address = http.address();
|
|
805
|
+
listeningPort =
|
|
806
|
+
typeof address === 'object' && address !== null ? address.port : requestedPort;
|
|
807
|
+
log(`listener on ${localOrigin()} (ws ${WEBSOCKET_PATH})`);
|
|
808
|
+
const heartbeatTimer = setInterval(() => reapIdleClients(wss.clients, alive), limits.heartbeatIntervalMs);
|
|
809
|
+
const flushTimer = setInterval(() => devices.flush(), limits.flushIntervalMs);
|
|
810
|
+
resolve({
|
|
811
|
+
port: listeningPort,
|
|
812
|
+
publicUrl: localOrigin(),
|
|
813
|
+
broadcast,
|
|
814
|
+
clientCount() {
|
|
815
|
+
return clients.size;
|
|
816
|
+
},
|
|
817
|
+
async close() {
|
|
818
|
+
clearInterval(heartbeatTimer);
|
|
819
|
+
clearInterval(flushTimer);
|
|
820
|
+
for (const client of wss.clients)
|
|
821
|
+
client.terminate();
|
|
822
|
+
await new Promise((done) => wss.close(() => done()));
|
|
823
|
+
await new Promise((done) => http.close(() => done()));
|
|
824
|
+
},
|
|
825
|
+
});
|
|
826
|
+
});
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
//# sourceMappingURL=server.js.map
|