@drisp/cli 0.3.39

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.
@@ -0,0 +1,3998 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ CHANNEL_REQUEST_ID_REGEX,
4
+ createUdsServerTransport,
5
+ generateChannelRequestId,
6
+ isLoopbackHost,
7
+ isValidChannelRequestId,
8
+ loadOrCreateToken,
9
+ refreshDashboardAccessToken,
10
+ requireTokenForBind,
11
+ resolveGatewayPaths,
12
+ resolveListenSpec,
13
+ timingSafeTokenEqual,
14
+ traceGatewayFrame,
15
+ trackGatewayRuntimeExpired,
16
+ trackGatewayRuntimeRebind,
17
+ trackGatewayTransportConnect,
18
+ trackGatewayTransportDisconnect,
19
+ writeGatewayTrace
20
+ } from "./chunk-3FVULBV4.js";
21
+
22
+ // src/gateway/daemon.ts
23
+ import crypto from "crypto";
24
+ import fs4 from "fs";
25
+
26
+ // src/infra/config/channels.ts
27
+ import fs from "fs";
28
+ import os from "os";
29
+ import path from "path";
30
+ function channelSidecarDir(home = os.homedir()) {
31
+ return path.join(home, ".config", "athena", "channels");
32
+ }
33
+ function loadChannelSidecars(home = os.homedir()) {
34
+ const dir = channelSidecarDir(home);
35
+ const sidecars = [];
36
+ const errors = [];
37
+ let entries;
38
+ try {
39
+ entries = fs.readdirSync(dir);
40
+ } catch (err) {
41
+ const code = err.code;
42
+ if (code === "ENOENT") return { sidecars, errors };
43
+ errors.push({
44
+ path: dir,
45
+ reason: `read dir failed: ${err instanceof Error ? err.message : String(err)}`
46
+ });
47
+ return { sidecars, errors };
48
+ }
49
+ for (const entry of entries) {
50
+ if (!entry.endsWith(".json")) continue;
51
+ const full = path.join(dir, entry);
52
+ const name = entry.slice(0, -".json".length);
53
+ const result = loadOne(name, full);
54
+ if (result.ok) sidecars.push(result.sidecar);
55
+ else errors.push({ path: full, reason: result.reason });
56
+ }
57
+ return { sidecars, errors };
58
+ }
59
+ function loadOne(name, filePath) {
60
+ let raw;
61
+ try {
62
+ if (process.platform !== "win32") {
63
+ const stat = fs.statSync(filePath);
64
+ if ((stat.mode & 63) !== 0) {
65
+ return {
66
+ ok: false,
67
+ reason: `file ${filePath} is too permissive (mode ${(stat.mode & 511).toString(8)}); chmod 600`
68
+ };
69
+ }
70
+ }
71
+ raw = JSON.parse(fs.readFileSync(filePath, "utf-8"));
72
+ } catch (err) {
73
+ return {
74
+ ok: false,
75
+ reason: err instanceof Error ? err.message : String(err)
76
+ };
77
+ }
78
+ if (typeof raw !== "object" || raw === null) {
79
+ return { ok: false, reason: "config root must be an object" };
80
+ }
81
+ const obj = raw;
82
+ const userIdsRaw = obj["allowed_user_ids"];
83
+ const allowedUserIds = [];
84
+ if (userIdsRaw !== void 0) {
85
+ if (!Array.isArray(userIdsRaw)) {
86
+ return { ok: false, reason: "allowed_user_ids must be an array" };
87
+ }
88
+ for (const id of userIdsRaw) {
89
+ if (typeof id === "string") allowedUserIds.push(id);
90
+ else if (typeof id === "number") allowedUserIds.push(String(id));
91
+ else
92
+ return {
93
+ ok: false,
94
+ reason: "allowed_user_ids entries must be string or number"
95
+ };
96
+ }
97
+ }
98
+ const options = {};
99
+ for (const [key, value] of Object.entries(obj)) {
100
+ if (key === "allowed_user_ids") continue;
101
+ options[key] = value;
102
+ }
103
+ return {
104
+ ok: true,
105
+ sidecar: { name, path: filePath, allowedUserIds, options }
106
+ };
107
+ }
108
+
109
+ // src/gateway/adapters/console/adapter.ts
110
+ import { readFileSync as readFileSync2 } from "fs";
111
+
112
+ // src/gateway/adapters/console/client.ts
113
+ import { readFileSync } from "fs";
114
+ import { WebSocket } from "ws";
115
+ var DEFAULT_CONNECT_TIMEOUT_MS = 1e4;
116
+ var DEFAULT_INITIAL_RECONNECT_MS = 1e3;
117
+ var DEFAULT_MAX_RECONNECT_MS = 3e4;
118
+ function createConsoleBrokerClient(opts) {
119
+ const hasStatic = typeof opts.pairingToken === "string" && opts.pairingToken.length > 0;
120
+ const hasProvider = typeof opts.pairingTokenProvider === "function";
121
+ if (hasStatic === hasProvider) {
122
+ throw new Error(
123
+ "console broker client: exactly one of pairingToken or pairingTokenProvider is required"
124
+ );
125
+ }
126
+ const provider = hasStatic ? async () => opts.pairingToken : opts.pairingTokenProvider;
127
+ const initialDelay = opts.reconnect?.initialDelayMs ?? DEFAULT_INITIAL_RECONNECT_MS;
128
+ const maxDelay = opts.reconnect?.maxDelayMs ?? DEFAULT_MAX_RECONNECT_MS;
129
+ let ws = null;
130
+ let ready = null;
131
+ let closeRequested = false;
132
+ let reconnectAttempt = 0;
133
+ let reconnectTimer = null;
134
+ let lastHello = null;
135
+ let currentToken = null;
136
+ const frameHandlers = /* @__PURE__ */ new Set();
137
+ const closeHandlers = /* @__PURE__ */ new Set();
138
+ const readyHandlers = /* @__PURE__ */ new Set();
139
+ const tokenRedacted = "<redacted>";
140
+ function redact(message) {
141
+ if (!currentToken) return message;
142
+ return message.split(currentToken).join(tokenRedacted);
143
+ }
144
+ function emitClose(reason) {
145
+ for (const h of [...closeHandlers]) {
146
+ try {
147
+ h(reason);
148
+ } catch {
149
+ }
150
+ }
151
+ }
152
+ function scheduleReconnect() {
153
+ if (closeRequested || !lastHello) return;
154
+ const exp = Math.min(maxDelay, initialDelay * 2 ** reconnectAttempt);
155
+ const delay = Math.floor(Math.random() * exp);
156
+ reconnectAttempt++;
157
+ const hello = lastHello;
158
+ reconnectTimer = setTimeout(() => {
159
+ reconnectTimer = null;
160
+ void attemptConnect(hello).then(
161
+ () => {
162
+ reconnectAttempt = 0;
163
+ },
164
+ (err) => {
165
+ opts.log(
166
+ "warn",
167
+ `console broker reconnect failed: ${redact(err instanceof Error ? err.message : String(err))}`
168
+ );
169
+ scheduleReconnect();
170
+ }
171
+ );
172
+ }, delay);
173
+ }
174
+ async function attemptConnect(hello) {
175
+ const timeoutMs = opts.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS;
176
+ let token;
177
+ try {
178
+ token = await provider();
179
+ } catch (err) {
180
+ throw new Error(
181
+ `console broker connect failed: pairing token provider threw: ${err instanceof Error ? err.message : String(err)}`
182
+ );
183
+ }
184
+ if (typeof token !== "string" || token.length === 0) {
185
+ throw new Error(
186
+ "console broker connect failed: pairing token provider returned empty token"
187
+ );
188
+ }
189
+ currentToken = token;
190
+ const headers = { Authorization: `Bearer ${token}` };
191
+ const wsOpts = opts.tlsCaPath ? { headers, ca: readFileSync(opts.tlsCaPath) } : { headers };
192
+ const next = new WebSocket(opts.brokerUrl, wsOpts);
193
+ ws = next;
194
+ ready = null;
195
+ try {
196
+ await new Promise((resolve, reject) => {
197
+ let settled = false;
198
+ const finishOk = () => {
199
+ if (settled) return;
200
+ settled = true;
201
+ clearTimeout(timer);
202
+ resolve();
203
+ };
204
+ const finishErr = (err) => {
205
+ if (settled) return;
206
+ settled = true;
207
+ clearTimeout(timer);
208
+ reject(err);
209
+ };
210
+ const timer = setTimeout(() => {
211
+ finishErr(
212
+ new Error(`console broker connect timed out after ${timeoutMs}ms`)
213
+ );
214
+ }, timeoutMs);
215
+ next.once("open", () => {
216
+ try {
217
+ const helloFrame = {
218
+ kind: "console.hello",
219
+ frameId: makeFrameId(),
220
+ sentAt: Date.now(),
221
+ protocolVersion: 1,
222
+ clientName: hello.clientName,
223
+ clientVersion: hello.clientVersion,
224
+ address: {
225
+ runnerId: hello.runnerId,
226
+ ...hello.workspaceId !== void 0 ? { workspaceId: hello.workspaceId } : {}
227
+ }
228
+ };
229
+ next.send(JSON.stringify(helloFrame));
230
+ } catch (err) {
231
+ finishErr(err instanceof Error ? err : new Error(String(err)));
232
+ }
233
+ });
234
+ next.once("error", (err) => {
235
+ finishErr(
236
+ new Error(`console broker connect failed: ${redact(err.message)}`)
237
+ );
238
+ });
239
+ const earlyCloseListener = (code, reasonBuf) => {
240
+ if (!ready) {
241
+ const reason = reasonBuf.toString();
242
+ finishErr(
243
+ new Error(
244
+ `console broker closed before ready (code=${code}${reason ? ` reason=${reason}` : ""})`
245
+ )
246
+ );
247
+ }
248
+ };
249
+ next.once("close", earlyCloseListener);
250
+ next.on("message", (data) => {
251
+ let parsed;
252
+ try {
253
+ parsed = JSON.parse(String(data));
254
+ } catch (err) {
255
+ opts.log(
256
+ "warn",
257
+ `console broker frame parse failed: ${redact(String(err))}`
258
+ );
259
+ return;
260
+ }
261
+ if (!ready) {
262
+ if (parsed.kind === "console.ready") {
263
+ const claimedRunnerId = hello.runnerId;
264
+ const readyRunnerId = parsed.address.runnerId;
265
+ if (readyRunnerId !== claimedRunnerId) {
266
+ finishErr(
267
+ new Error(
268
+ `console broker ready runnerId mismatch: claimed ${claimedRunnerId}, got ${readyRunnerId}`
269
+ )
270
+ );
271
+ return;
272
+ }
273
+ ready = parsed;
274
+ next.removeListener("close", earlyCloseListener);
275
+ const address = parsed.address;
276
+ for (const h of [...readyHandlers]) {
277
+ try {
278
+ h(address);
279
+ } catch {
280
+ }
281
+ }
282
+ finishOk();
283
+ return;
284
+ }
285
+ if (parsed.kind === "console.error") {
286
+ finishErr(
287
+ new Error(
288
+ `console broker rejected hello: ${parsed.code} ${parsed.message}`
289
+ )
290
+ );
291
+ return;
292
+ }
293
+ opts.log(
294
+ "warn",
295
+ `console broker pre-ready frame ignored: ${parsed.kind}`
296
+ );
297
+ return;
298
+ }
299
+ for (const h of [...frameHandlers]) {
300
+ try {
301
+ h(parsed);
302
+ } catch (err) {
303
+ opts.log(
304
+ "warn",
305
+ `console frame handler threw: ${redact(err instanceof Error ? err.message : String(err))}`
306
+ );
307
+ }
308
+ }
309
+ });
310
+ });
311
+ } catch (err) {
312
+ try {
313
+ next.terminate();
314
+ } catch {
315
+ }
316
+ if (ws === next) {
317
+ ws = null;
318
+ ready = null;
319
+ }
320
+ throw err;
321
+ }
322
+ next.on("close", (_code, reasonBuf) => {
323
+ if (next !== ws) return;
324
+ ws = null;
325
+ ready = null;
326
+ emitClose(reasonBuf.toString() || "closed");
327
+ if (!closeRequested) scheduleReconnect();
328
+ });
329
+ }
330
+ async function connect(hello) {
331
+ if (ws) throw new Error("console broker client already connected");
332
+ closeRequested = false;
333
+ lastHello = hello;
334
+ await attemptConnect(hello);
335
+ }
336
+ function close(reason) {
337
+ closeRequested = true;
338
+ if (reconnectTimer) {
339
+ clearTimeout(reconnectTimer);
340
+ reconnectTimer = null;
341
+ }
342
+ if (ws) {
343
+ try {
344
+ ws.close(1e3, reason);
345
+ } catch {
346
+ ws.terminate();
347
+ }
348
+ }
349
+ ws = null;
350
+ ready = null;
351
+ emitClose(reason);
352
+ }
353
+ function sendFrame(frame) {
354
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
355
+ throw new Error("console broker client not connected");
356
+ }
357
+ ws.send(JSON.stringify(frame));
358
+ }
359
+ function onFrame(handler) {
360
+ frameHandlers.add(handler);
361
+ }
362
+ function onClose(handler) {
363
+ closeHandlers.add(handler);
364
+ }
365
+ function onReady(handler) {
366
+ readyHandlers.add(handler);
367
+ }
368
+ return {
369
+ connect,
370
+ close,
371
+ sendFrame,
372
+ onFrame,
373
+ onReady,
374
+ onClose,
375
+ getReadyAddress: () => ready?.address ?? null,
376
+ isReady: () => ready !== null
377
+ };
378
+ }
379
+ var frameCounter = 0;
380
+ function makeFrameId() {
381
+ frameCounter = (frameCounter + 1) % 1e6;
382
+ return `f${Date.now().toString(36)}-${frameCounter.toString(36)}`;
383
+ }
384
+
385
+ // src/gateway/adapters/console/adapter.ts
386
+ var CONSOLE_ID = "console";
387
+ var CLIENT_NAME = "athena-cli";
388
+ var CLIENT_VERSION = "0.0.0";
389
+ var ConsoleAdapter = class {
390
+ id = CONSOLE_ID;
391
+ capabilities = {
392
+ chat: true,
393
+ threads: true,
394
+ relayPermission: true,
395
+ relayQuestion: true
396
+ };
397
+ opts;
398
+ client = null;
399
+ ctx = null;
400
+ pendingPermissions = /* @__PURE__ */ new Map();
401
+ pendingQuestions = /* @__PURE__ */ new Map();
402
+ constructor(opts) {
403
+ this.opts = opts;
404
+ }
405
+ async start(ctx) {
406
+ if (this.client) {
407
+ throw new Error("console adapter already started");
408
+ }
409
+ this.ctx = ctx;
410
+ const tokenSource = resolvePairingTokenSource(this.opts);
411
+ const factory = this.opts.brokerClientFactory ?? ((input) => createConsoleBrokerClient(input));
412
+ const client = factory({
413
+ brokerUrl: this.opts.brokerUrl,
414
+ ...tokenSource.kind === "static" ? { pairingToken: tokenSource.token } : { pairingTokenProvider: tokenSource.provider },
415
+ ...this.opts.tlsCaPath !== void 0 ? { tlsCaPath: this.opts.tlsCaPath } : {},
416
+ log: ctx.log
417
+ });
418
+ client.onFrame((frame) => this.handleInboundFrame(frame));
419
+ client.onReady(() => {
420
+ ctx.emitHealth({ at: Date.now(), transportOk: true });
421
+ });
422
+ client.onClose((reason) => {
423
+ this.disposePermissions("connection_lost");
424
+ this.disposeQuestions("connection_lost");
425
+ ctx.emitHealth({
426
+ at: Date.now(),
427
+ transportOk: false,
428
+ note: `broker connection closed: ${reason}`
429
+ });
430
+ });
431
+ await client.connect({
432
+ runnerId: this.opts.runnerId,
433
+ ...this.opts.workspaceId !== void 0 ? { workspaceId: this.opts.workspaceId } : {},
434
+ clientName: CLIENT_NAME,
435
+ clientVersion: CLIENT_VERSION
436
+ });
437
+ this.client = client;
438
+ ctx.signal.addEventListener("abort", () => {
439
+ this.client?.close("manager abort");
440
+ });
441
+ }
442
+ async stop(_reason) {
443
+ this.disposePermissions();
444
+ this.disposeQuestions();
445
+ this.client?.close("shutdown");
446
+ this.client = null;
447
+ this.ctx = null;
448
+ }
449
+ async send(msg) {
450
+ const client = this.client;
451
+ if (!client || !client.isReady()) {
452
+ throw new Error("console adapter: send called before broker is ready");
453
+ }
454
+ const messageId = makeOutboundMessageId();
455
+ const workspaceId = this.opts.workspaceId ?? msg.location.accountId;
456
+ const frame = {
457
+ kind: "console.message.out",
458
+ frameId: makeFrameId2(),
459
+ sentAt: Date.now(),
460
+ address: {
461
+ runnerId: this.opts.runnerId,
462
+ ...workspaceId.length > 0 ? { workspaceId } : {},
463
+ ...msg.location.peer?.id !== void 0 ? { userId: msg.location.peer.id } : {},
464
+ ...msg.location.thread?.id !== void 0 ? {
465
+ conversationId: msg.location.thread.id,
466
+ threadId: msg.location.thread.id
467
+ } : {}
468
+ },
469
+ messageId,
470
+ idempotencyKey: msg.idempotencyKey,
471
+ text: msg.text
472
+ };
473
+ client.sendFrame(frame);
474
+ return {
475
+ providerMessageId: messageId,
476
+ deliveredAt: Date.now()
477
+ };
478
+ }
479
+ async probe() {
480
+ const ok2 = this.client?.isReady() ?? false;
481
+ return {
482
+ ok: ok2,
483
+ detail: ok2 ? "broker connected" : "broker not connected",
484
+ checkedAt: Date.now()
485
+ };
486
+ }
487
+ requestPermissionVerdict(req, signal) {
488
+ const client = this.client;
489
+ if (!client || !client.isReady()) {
490
+ return Promise.resolve({ kind: "no_relay" });
491
+ }
492
+ if (signal.aborted) {
493
+ return Promise.resolve({ kind: "cancelled", reason: "auto_resolved" });
494
+ }
495
+ client.sendFrame({
496
+ kind: "console.permission.request",
497
+ frameId: makeFrameId2(),
498
+ sentAt: Date.now(),
499
+ address: {
500
+ runnerId: this.opts.runnerId,
501
+ ...this.opts.workspaceId !== void 0 ? { workspaceId: this.opts.workspaceId } : {}
502
+ },
503
+ channelRequestId: req.channelRequestId,
504
+ toolName: req.toolName,
505
+ description: req.description,
506
+ inputPreview: req.inputPreview
507
+ });
508
+ return new Promise((resolve) => {
509
+ const abortListener = () => {
510
+ const entry = this.pendingPermissions.get(req.channelRequestId);
511
+ if (!entry) return;
512
+ this.pendingPermissions.delete(req.channelRequestId);
513
+ try {
514
+ this.client?.sendFrame({
515
+ kind: "console.permission.cancel",
516
+ frameId: makeFrameId2(),
517
+ sentAt: Date.now(),
518
+ channelRequestId: req.channelRequestId,
519
+ reason: "resolved_by_other_channel"
520
+ });
521
+ } catch {
522
+ }
523
+ resolve({ kind: "cancelled", reason: "resolved_by_other_channel" });
524
+ };
525
+ signal.addEventListener("abort", abortListener);
526
+ this.pendingPermissions.set(req.channelRequestId, {
527
+ resolve,
528
+ abortListener,
529
+ signal
530
+ });
531
+ });
532
+ }
533
+ settlePermissionResponse(channelRequestId, decision) {
534
+ const entry = this.pendingPermissions.get(channelRequestId);
535
+ if (!entry) return;
536
+ this.pendingPermissions.delete(channelRequestId);
537
+ entry.signal.removeEventListener("abort", entry.abortListener);
538
+ entry.resolve({ kind: "verdict", behavior: decision, channelId: CONSOLE_ID });
539
+ }
540
+ disposePermissions(reason = "auto_resolved") {
541
+ for (const [id, entry] of [...this.pendingPermissions.entries()]) {
542
+ this.pendingPermissions.delete(id);
543
+ entry.signal.removeEventListener("abort", entry.abortListener);
544
+ entry.resolve({ kind: "cancelled", reason });
545
+ }
546
+ }
547
+ requestQuestionAnswer(req, signal) {
548
+ const client = this.client;
549
+ if (!client || !client.isReady()) {
550
+ return Promise.resolve({ kind: "no_relay" });
551
+ }
552
+ if (signal.aborted) {
553
+ return Promise.resolve({ kind: "cancelled", reason: "auto_resolved" });
554
+ }
555
+ client.sendFrame({
556
+ kind: "console.question.request",
557
+ frameId: makeFrameId2(),
558
+ sentAt: Date.now(),
559
+ address: {
560
+ runnerId: this.opts.runnerId,
561
+ ...this.opts.workspaceId !== void 0 ? { workspaceId: this.opts.workspaceId } : {}
562
+ },
563
+ channelRequestId: req.channelRequestId,
564
+ title: req.title,
565
+ questions: req.questions
566
+ });
567
+ return new Promise((resolve) => {
568
+ const abortListener = () => {
569
+ const entry = this.pendingQuestions.get(req.channelRequestId);
570
+ if (!entry) return;
571
+ this.pendingQuestions.delete(req.channelRequestId);
572
+ try {
573
+ this.client?.sendFrame({
574
+ kind: "console.question.cancel",
575
+ frameId: makeFrameId2(),
576
+ sentAt: Date.now(),
577
+ channelRequestId: req.channelRequestId,
578
+ reason: "resolved_by_other_channel"
579
+ });
580
+ } catch {
581
+ }
582
+ resolve({ kind: "cancelled", reason: "resolved_by_other_channel" });
583
+ };
584
+ signal.addEventListener("abort", abortListener);
585
+ this.pendingQuestions.set(req.channelRequestId, {
586
+ questionKeys: req.questions.map((q) => q.key),
587
+ resolve,
588
+ abortListener,
589
+ signal
590
+ });
591
+ });
592
+ }
593
+ settleQuestionResponse(channelRequestId, answers) {
594
+ const entry = this.pendingQuestions.get(channelRequestId);
595
+ if (!entry) return;
596
+ const filtered = {};
597
+ for (const key of entry.questionKeys) {
598
+ const value = answers[key];
599
+ if (typeof value === "string") filtered[key] = value;
600
+ }
601
+ this.pendingQuestions.delete(channelRequestId);
602
+ entry.signal.removeEventListener("abort", entry.abortListener);
603
+ if (Object.keys(filtered).length === 0) {
604
+ entry.resolve({ kind: "cancelled", reason: "auto_resolved" });
605
+ return;
606
+ }
607
+ entry.resolve({ kind: "answer", answers: filtered, channelId: CONSOLE_ID });
608
+ }
609
+ disposeQuestions(reason = "auto_resolved") {
610
+ for (const [id, entry] of [...this.pendingQuestions.entries()]) {
611
+ this.pendingQuestions.delete(id);
612
+ entry.signal.removeEventListener("abort", entry.abortListener);
613
+ entry.resolve({ kind: "cancelled", reason });
614
+ }
615
+ }
616
+ handleInboundFrame(frame) {
617
+ switch (frame.kind) {
618
+ case "console.message.in":
619
+ this.handleInboundMessage(frame);
620
+ return;
621
+ case "console.permission.response":
622
+ this.handlePermissionResponse(frame);
623
+ return;
624
+ case "console.question.response":
625
+ this.handleQuestionResponse(frame);
626
+ return;
627
+ default:
628
+ return;
629
+ }
630
+ }
631
+ handleInboundMessage(frame) {
632
+ if (frame.kind !== "console.message.in") return;
633
+ if (!isValidConsoleAddress(frame.address)) {
634
+ this.ctx?.log("warn", "console.message.in dropped: invalid address");
635
+ return;
636
+ }
637
+ if (frame.address.runnerId !== this.opts.runnerId) {
638
+ this.ctx?.log(
639
+ "warn",
640
+ `console.message.in dropped: runner mismatch (claimed ${frame.address.runnerId}, expected ${this.opts.runnerId})`
641
+ );
642
+ return;
643
+ }
644
+ if (typeof frame.messageId !== "string" || frame.messageId.length === 0) {
645
+ this.ctx?.log("warn", "console.message.in dropped: missing messageId");
646
+ return;
647
+ }
648
+ const inbound = normalizeInbound(frame, this.opts.runnerId);
649
+ if (!inbound || !this.ctx) return;
650
+ try {
651
+ this.ctx.emitInbound(inbound);
652
+ } catch (err) {
653
+ this.ctx.log(
654
+ "warn",
655
+ `console emitInbound threw: ${err instanceof Error ? err.message : String(err)}`
656
+ );
657
+ }
658
+ }
659
+ handlePermissionResponse(frame) {
660
+ if (frame.kind !== "console.permission.response") return;
661
+ if (typeof frame.channelRequestId !== "string" || frame.channelRequestId.length === 0) {
662
+ this.ctx?.log(
663
+ "warn",
664
+ "console.permission.response dropped: missing channelRequestId"
665
+ );
666
+ return;
667
+ }
668
+ if (frame.decision !== "allow" && frame.decision !== "deny") {
669
+ this.ctx?.log(
670
+ "warn",
671
+ `console.permission.response dropped: invalid decision ${String(frame.decision)}`
672
+ );
673
+ return;
674
+ }
675
+ this.settlePermissionResponse(frame.channelRequestId, frame.decision);
676
+ }
677
+ handleQuestionResponse(frame) {
678
+ if (frame.kind !== "console.question.response") return;
679
+ if (typeof frame.channelRequestId !== "string" || frame.channelRequestId.length === 0) {
680
+ this.ctx?.log(
681
+ "warn",
682
+ "console.question.response dropped: missing channelRequestId"
683
+ );
684
+ return;
685
+ }
686
+ if (typeof frame.answers !== "object" || frame.answers === null || Array.isArray(frame.answers)) {
687
+ this.ctx?.log(
688
+ "warn",
689
+ "console.question.response dropped: answers must be a record"
690
+ );
691
+ return;
692
+ }
693
+ const cleaned = {};
694
+ for (const [key, value] of Object.entries(frame.answers)) {
695
+ if (typeof key !== "string" || key.length === 0) continue;
696
+ if (typeof value !== "string") continue;
697
+ cleaned[key] = value;
698
+ }
699
+ this.settleQuestionResponse(frame.channelRequestId, cleaned);
700
+ }
701
+ };
702
+ function resolvePairingTokenSource(opts) {
703
+ if (opts.dashboardConfig === true) {
704
+ const provider = opts.pairingTokenProvider ?? (async () => {
705
+ const result = await refreshDashboardAccessToken();
706
+ return result.accessToken;
707
+ });
708
+ return { kind: "provider", provider };
709
+ }
710
+ if (opts.pairingToken !== void 0 && opts.pairingToken.length > 0) {
711
+ return { kind: "static", token: opts.pairingToken };
712
+ }
713
+ if (opts.tokenPath !== void 0 && opts.tokenPath.length > 0) {
714
+ try {
715
+ const value = readFileSync2(opts.tokenPath, "utf-8").trim();
716
+ if (value.length === 0) {
717
+ throw new Error(
718
+ `console adapter: token_path ${opts.tokenPath} is empty`
719
+ );
720
+ }
721
+ return { kind: "static", token: value };
722
+ } catch (err) {
723
+ const code = err.code;
724
+ throw new Error(
725
+ `console adapter: failed to read token_path ${opts.tokenPath}` + (code ? ` (${code})` : "") + (err instanceof Error ? `: ${err.message}` : "")
726
+ );
727
+ }
728
+ }
729
+ throw new Error(
730
+ "console adapter: no pairing_token, token_path, or dashboard_config configured"
731
+ );
732
+ }
733
+ var outboundCounter = 0;
734
+ function makeOutboundMessageId() {
735
+ outboundCounter = (outboundCounter + 1) % 1e6;
736
+ return `console-out-${Date.now().toString(36)}-${outboundCounter.toString(36)}`;
737
+ }
738
+ var frameCounter2 = 0;
739
+ function makeFrameId2() {
740
+ frameCounter2 = (frameCounter2 + 1) % 1e6;
741
+ return `f${Date.now().toString(36)}-${frameCounter2.toString(36)}`;
742
+ }
743
+ function isValidConsoleAddress(value) {
744
+ if (typeof value !== "object" || value === null) return false;
745
+ const v = value;
746
+ if (typeof v["runnerId"] !== "string" || v["runnerId"].length === 0) {
747
+ return false;
748
+ }
749
+ for (const key of [
750
+ "workspaceId",
751
+ "conversationId",
752
+ "threadId",
753
+ "userId"
754
+ ]) {
755
+ if (v[key] !== void 0 && typeof v[key] !== "string") return false;
756
+ }
757
+ return true;
758
+ }
759
+ function normalizeInbound(frame, runnerId) {
760
+ if (typeof frame.text !== "string" || frame.text.length === 0) return null;
761
+ const userId = frame.address.userId ?? "console-user";
762
+ const idempotencyKey = typeof frame.idempotencyKey === "string" && frame.idempotencyKey.length > 0 ? frame.idempotencyKey : `console:${runnerId}:${frame.messageId}`;
763
+ return {
764
+ location: {
765
+ channelId: CONSOLE_ID,
766
+ accountId: frame.address.workspaceId ?? runnerId,
767
+ peer: { id: userId, kind: "user" },
768
+ ...frame.address.threadId !== void 0 ? { thread: { id: frame.address.threadId } } : frame.address.conversationId !== void 0 ? { thread: { id: frame.address.conversationId } } : {}
769
+ },
770
+ sender: { id: userId },
771
+ text: frame.text,
772
+ receivedAt: frame.sentAt,
773
+ idempotencyKey,
774
+ providerMessageId: frame.messageId
775
+ };
776
+ }
777
+
778
+ // src/gateway/adapters/console/module.ts
779
+ var consoleModule = {
780
+ name: "console",
781
+ parseConfig({ options }) {
782
+ const brokerUrl = options["broker_url"];
783
+ if (typeof brokerUrl !== "string" || brokerUrl.length === 0) {
784
+ return { ok: false, reason: "broker_url missing" };
785
+ }
786
+ if (!/^wss?:\/\//.test(brokerUrl)) {
787
+ return { ok: false, reason: "broker_url must start with ws:// or wss://" };
788
+ }
789
+ if (brokerUrl.startsWith("ws://") && !isLoopbackUrl(brokerUrl)) {
790
+ return {
791
+ ok: false,
792
+ reason: "broker_url must use wss:// for non-loopback hosts"
793
+ };
794
+ }
795
+ const runnerId = options["runner_id"];
796
+ if (typeof runnerId !== "string" || runnerId.length === 0) {
797
+ return { ok: false, reason: "runner_id missing" };
798
+ }
799
+ const pairingToken = options["pairing_token"];
800
+ const tokenPath = options["token_path"];
801
+ const dashboardConfig = options["dashboard_config"];
802
+ if (dashboardConfig !== void 0 && typeof dashboardConfig !== "boolean") {
803
+ return { ok: false, reason: "dashboard_config must be a boolean" };
804
+ }
805
+ const useDashboardConfig = dashboardConfig === true;
806
+ const hasInline = typeof pairingToken === "string" && pairingToken.length > 0;
807
+ const hasTokenPath = typeof tokenPath === "string" && tokenPath.length > 0;
808
+ if (useDashboardConfig && (hasInline || hasTokenPath)) {
809
+ return {
810
+ ok: false,
811
+ reason: "dashboard_config is mutually exclusive with pairing_token and token_path"
812
+ };
813
+ }
814
+ if (!useDashboardConfig && !hasInline && !hasTokenPath) {
815
+ return {
816
+ ok: false,
817
+ reason: "either pairing_token, token_path, or dashboard_config is required"
818
+ };
819
+ }
820
+ if (pairingToken !== void 0 && typeof pairingToken !== "string") {
821
+ return { ok: false, reason: "pairing_token must be a string" };
822
+ }
823
+ if (tokenPath !== void 0 && typeof tokenPath !== "string") {
824
+ return { ok: false, reason: "token_path must be a string" };
825
+ }
826
+ const workspaceId = options["workspace_id"];
827
+ if (workspaceId !== void 0 && typeof workspaceId !== "string") {
828
+ return { ok: false, reason: "workspace_id must be a string" };
829
+ }
830
+ const tlsCaPath = options["tls_ca_path"];
831
+ if (tlsCaPath !== void 0 && typeof tlsCaPath !== "string") {
832
+ return { ok: false, reason: "tls_ca_path must be a string" };
833
+ }
834
+ const config = {
835
+ brokerUrl,
836
+ runnerId,
837
+ ...workspaceId !== void 0 ? { workspaceId } : {},
838
+ ...useDashboardConfig ? { dashboardConfig: true } : {
839
+ ...pairingToken !== void 0 ? { pairingToken } : {},
840
+ ...tokenPath !== void 0 ? { tokenPath } : {}
841
+ },
842
+ ...tlsCaPath !== void 0 ? { tlsCaPath } : {}
843
+ };
844
+ return { ok: true, config };
845
+ },
846
+ create(config) {
847
+ return new ConsoleAdapter(config);
848
+ }
849
+ };
850
+ function isLoopbackUrl(url) {
851
+ try {
852
+ const parsed = new URL(url);
853
+ const host = parsed.hostname;
854
+ return host === "localhost" || host === "127.0.0.1" || host === "::1";
855
+ } catch {
856
+ return false;
857
+ }
858
+ }
859
+
860
+ // src/shared/utils/errorMessage.ts
861
+ function errorMessage(err) {
862
+ return err instanceof Error ? err.message : String(err);
863
+ }
864
+
865
+ // src/shared/telegram/bot.ts
866
+ var TelegramApiError = class extends Error {
867
+ constructor(status, message, retryAfterMs) {
868
+ super(message);
869
+ this.status = status;
870
+ this.retryAfterMs = retryAfterMs;
871
+ this.name = "TelegramApiError";
872
+ }
873
+ };
874
+ var TelegramBot = class {
875
+ token;
876
+ apiBase;
877
+ pollTimeoutSec;
878
+ offset = 0;
879
+ stopped = false;
880
+ log;
881
+ consecutiveAuthFailures = 0;
882
+ consecutiveErrors = 0;
883
+ constructor(opts, log) {
884
+ this.token = opts.token;
885
+ this.apiBase = opts.apiBase ?? "https://api.telegram.org";
886
+ this.pollTimeoutSec = opts.pollTimeoutSec ?? 25;
887
+ this.log = log;
888
+ }
889
+ /** Strip the bot token from any string before logging or surfacing. */
890
+ redact(text) {
891
+ if (!this.token) return text;
892
+ return text.split(this.token).join("<redacted>");
893
+ }
894
+ stop() {
895
+ this.stopped = true;
896
+ }
897
+ isStopped() {
898
+ return this.stopped;
899
+ }
900
+ /** Current update offset; persist to skip already-seen updates on restart. */
901
+ getOffset() {
902
+ return this.offset;
903
+ }
904
+ setOffset(offset) {
905
+ if (offset > this.offset) this.offset = offset;
906
+ }
907
+ async *poll() {
908
+ while (!this.isStopped()) {
909
+ try {
910
+ const updates = await this.getUpdates();
911
+ this.consecutiveAuthFailures = 0;
912
+ this.consecutiveErrors = 0;
913
+ for (const update of updates) {
914
+ if (this.isStopped()) return;
915
+ if (update.update_id >= this.offset) {
916
+ this.offset = update.update_id + 1;
917
+ }
918
+ yield update;
919
+ }
920
+ } catch (err) {
921
+ const message = this.redact(errorMessage(err));
922
+ const apiErr = err instanceof TelegramApiError ? err : void 0;
923
+ const status = apiErr?.status;
924
+ const retryAfterMs = apiErr?.retryAfterMs;
925
+ if (status === 401 || status === 409) {
926
+ this.consecutiveAuthFailures++;
927
+ this.log(
928
+ "error",
929
+ `getUpdates failed (HTTP ${status}, attempt ${this.consecutiveAuthFailures}): ${message}`
930
+ );
931
+ if (this.consecutiveAuthFailures >= 3) {
932
+ this.stopped = true;
933
+ throw new Error(
934
+ `telegram channel: persistent HTTP ${status} from getUpdates after 3 attempts`
935
+ );
936
+ }
937
+ } else if (status === 429 && retryAfterMs) {
938
+ this.log(
939
+ "warn",
940
+ `getUpdates rate-limited; sleeping ${retryAfterMs}ms`
941
+ );
942
+ await sleep(retryAfterMs);
943
+ continue;
944
+ } else {
945
+ this.consecutiveErrors++;
946
+ this.log("warn", `getUpdates failed: ${message}`);
947
+ }
948
+ const backoffMs = Math.min(
949
+ 1500 * Math.pow(2, Math.max(0, this.consecutiveErrors - 1)),
950
+ 3e4
951
+ );
952
+ await sleep(backoffMs);
953
+ }
954
+ }
955
+ }
956
+ async sendMessage(chatId, text, options = {}) {
957
+ try {
958
+ const params = { chat_id: chatId, text };
959
+ if (options.parse_mode) params["parse_mode"] = options.parse_mode;
960
+ if (options.reply_markup) params["reply_markup"] = options.reply_markup;
961
+ if (options.message_thread_id !== void 0)
962
+ params["message_thread_id"] = options.message_thread_id;
963
+ const result = await this.call("sendMessage", params);
964
+ return result;
965
+ } catch (err) {
966
+ this.log("warn", `sendMessage failed: ${this.redact(errorMessage(err))}`);
967
+ return null;
968
+ }
969
+ }
970
+ async createForumTopic(chatId, name) {
971
+ try {
972
+ return await this.call("createForumTopic", {
973
+ chat_id: chatId,
974
+ name
975
+ });
976
+ } catch (err) {
977
+ this.log(
978
+ "warn",
979
+ `createForumTopic failed: ${this.redact(errorMessage(err))}`
980
+ );
981
+ return null;
982
+ }
983
+ }
984
+ async editForumTopic(chatId, messageThreadId, name) {
985
+ try {
986
+ await this.call("editForumTopic", {
987
+ chat_id: chatId,
988
+ message_thread_id: messageThreadId,
989
+ name
990
+ });
991
+ } catch (err) {
992
+ this.log(
993
+ "debug",
994
+ `editForumTopic failed: ${this.redact(errorMessage(err))}`
995
+ );
996
+ }
997
+ }
998
+ async closeForumTopic(chatId, messageThreadId) {
999
+ try {
1000
+ await this.call("closeForumTopic", {
1001
+ chat_id: chatId,
1002
+ message_thread_id: messageThreadId
1003
+ });
1004
+ } catch (err) {
1005
+ this.log(
1006
+ "debug",
1007
+ `closeForumTopic failed: ${this.redact(errorMessage(err))}`
1008
+ );
1009
+ }
1010
+ }
1011
+ async editMessageText(chatId, messageId, text, options = {}) {
1012
+ try {
1013
+ const params = {
1014
+ chat_id: chatId,
1015
+ message_id: messageId,
1016
+ text
1017
+ };
1018
+ if (options.parse_mode) params["parse_mode"] = options.parse_mode;
1019
+ if (options.reply_markup) params["reply_markup"] = options.reply_markup;
1020
+ await this.call("editMessageText", params);
1021
+ } catch (err) {
1022
+ this.log(
1023
+ "debug",
1024
+ `editMessageText failed: ${this.redact(errorMessage(err))}`
1025
+ );
1026
+ }
1027
+ }
1028
+ async editMessageReplyMarkup(chatId, messageId, replyMarkup) {
1029
+ try {
1030
+ await this.call("editMessageReplyMarkup", {
1031
+ chat_id: chatId,
1032
+ message_id: messageId,
1033
+ reply_markup: replyMarkup ?? { inline_keyboard: [] }
1034
+ });
1035
+ } catch (err) {
1036
+ this.log(
1037
+ "debug",
1038
+ `editMessageReplyMarkup failed: ${this.redact(errorMessage(err))}`
1039
+ );
1040
+ }
1041
+ }
1042
+ async answerCallbackQuery(callbackQueryId, text) {
1043
+ try {
1044
+ const params = {
1045
+ callback_query_id: callbackQueryId
1046
+ };
1047
+ if (text) params["text"] = text;
1048
+ await this.call("answerCallbackQuery", params);
1049
+ } catch (err) {
1050
+ this.log(
1051
+ "debug",
1052
+ `answerCallbackQuery failed: ${this.redact(errorMessage(err))}`
1053
+ );
1054
+ }
1055
+ }
1056
+ async setMyCommands(commands) {
1057
+ try {
1058
+ await this.call("setMyCommands", { commands });
1059
+ } catch (err) {
1060
+ this.log(
1061
+ "debug",
1062
+ `setMyCommands failed: ${this.redact(errorMessage(err))}`
1063
+ );
1064
+ }
1065
+ }
1066
+ async getUpdates() {
1067
+ const result = await this.call("getUpdates", {
1068
+ offset: this.offset,
1069
+ timeout: this.pollTimeoutSec,
1070
+ allowed_updates: ["message", "callback_query"]
1071
+ });
1072
+ return result;
1073
+ }
1074
+ async call(method, params) {
1075
+ const url = `${this.apiBase}/bot${this.token}/${method}`;
1076
+ const ctrl = new AbortController();
1077
+ const timeoutMs = (this.pollTimeoutSec + 5) * 1e3 * 2;
1078
+ const timer = setTimeout(() => ctrl.abort(), timeoutMs);
1079
+ let res;
1080
+ try {
1081
+ res = await fetch(url, {
1082
+ method: "POST",
1083
+ headers: { "Content-Type": "application/json" },
1084
+ body: JSON.stringify(params),
1085
+ signal: ctrl.signal
1086
+ });
1087
+ } finally {
1088
+ clearTimeout(timer);
1089
+ }
1090
+ if (!res.ok) {
1091
+ let retryAfterSec;
1092
+ try {
1093
+ const body = await res.json();
1094
+ retryAfterSec = body.parameters?.retry_after;
1095
+ } catch {
1096
+ }
1097
+ throw new TelegramApiError(
1098
+ res.status,
1099
+ `HTTP ${res.status}`,
1100
+ typeof retryAfterSec === "number" ? retryAfterSec * 1e3 : void 0
1101
+ );
1102
+ }
1103
+ const json = await res.json();
1104
+ if (!json.ok) {
1105
+ throw new Error(json.description ?? "telegram api returned ok=false");
1106
+ }
1107
+ return json.result;
1108
+ }
1109
+ };
1110
+ function sleep(ms) {
1111
+ return new Promise((resolve) => setTimeout(resolve, ms));
1112
+ }
1113
+
1114
+ // src/shared/telegram/markdown.ts
1115
+ function escapeMarkdownV2(text) {
1116
+ return text.replace(/([_*[\]()~`>#+\-=|{}.!\\])/g, "\\$1");
1117
+ }
1118
+ function escapeMarkdownV2CodeBlock(text) {
1119
+ return text.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
1120
+ }
1121
+
1122
+ // src/gateway/adapters/telegram/verdict.ts
1123
+ var CB_PERMISSION = "v";
1124
+ var CB_QUESTION = "q";
1125
+ var CB_ALLOW = "a";
1126
+ var CB_DENY = "d";
1127
+ var CB_VERDICT = {
1128
+ allow: CB_ALLOW,
1129
+ deny: CB_DENY
1130
+ };
1131
+ var ID_PATTERN = CHANNEL_REQUEST_ID_REGEX.source.replace(/^\^|\$$/g, "");
1132
+ var VERDICT_RE = new RegExp(`^\\s*(y|yes|n|no)\\s+(${ID_PATTERN})\\s*$`, "i");
1133
+ var ANSWER_RE = new RegExp(
1134
+ `^\\s*(a|answer)\\s+(${ID_PATTERN})\\s+([\\s\\S]+?)\\s*$`,
1135
+ "i"
1136
+ );
1137
+ var ANSWER_ID_RE = new RegExp(`^\\s*(a|answer)\\s+(${ID_PATTERN})\\s+`, "i");
1138
+ var NON_NEG_INT_RE = /^\d+$/;
1139
+ var MAX_JSON_ANSWER_BYTES = 8 * 1024;
1140
+ function parseVerdict(text) {
1141
+ const m = VERDICT_RE.exec(text);
1142
+ if (!m) return null;
1143
+ const verdictWord = m[1].toLowerCase();
1144
+ const id = m[2].toLowerCase();
1145
+ return {
1146
+ channelRequestId: id,
1147
+ behavior: verdictWord.startsWith("y") ? "allow" : "deny"
1148
+ };
1149
+ }
1150
+ function parseQuestionAnswer(text, questionKeys) {
1151
+ const m = ANSWER_RE.exec(text);
1152
+ if (!m) return null;
1153
+ const channelRequestId = m[2].toLowerCase();
1154
+ const rawAnswer = m[3].trim();
1155
+ if (rawAnswer.length === 0) return null;
1156
+ if (rawAnswer.startsWith("{")) {
1157
+ if (rawAnswer.length > MAX_JSON_ANSWER_BYTES) return null;
1158
+ try {
1159
+ const parsed = JSON.parse(rawAnswer);
1160
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
1161
+ return null;
1162
+ }
1163
+ const answers = {};
1164
+ for (const [key, value] of Object.entries(parsed)) {
1165
+ if (typeof value !== "string") return null;
1166
+ answers[key] = value;
1167
+ }
1168
+ return { channelRequestId, answers };
1169
+ } catch {
1170
+ return null;
1171
+ }
1172
+ }
1173
+ const firstKey = questionKeys[0];
1174
+ if (!firstKey) return null;
1175
+ return {
1176
+ channelRequestId,
1177
+ answers: { [firstKey]: rawAnswer }
1178
+ };
1179
+ }
1180
+ function parseQuestionAnswerId(text) {
1181
+ const m = ANSWER_ID_RE.exec(text);
1182
+ return m ? m[2].toLowerCase() : null;
1183
+ }
1184
+ function buildPlainTextQuestionAnswer(channelRequestId, text, questionKeys) {
1185
+ const answer = text.trim();
1186
+ const firstKey = questionKeys[0];
1187
+ if (!firstKey || answer.length === 0) return null;
1188
+ return {
1189
+ channelRequestId,
1190
+ answers: { [firstKey]: answer }
1191
+ };
1192
+ }
1193
+ function parseCallbackData(data) {
1194
+ const parts = data.split(":");
1195
+ const kind = parts[0];
1196
+ const id = parts[1];
1197
+ if (!id || !isValidChannelRequestId(id)) return null;
1198
+ if (kind === CB_PERMISSION && parts.length === 3) {
1199
+ const verb = parts[2];
1200
+ if (verb !== CB_ALLOW && verb !== CB_DENY) return null;
1201
+ return {
1202
+ kind: "permission",
1203
+ channelRequestId: id,
1204
+ behavior: verb === CB_ALLOW ? "allow" : "deny"
1205
+ };
1206
+ }
1207
+ if (kind === CB_QUESTION && parts.length === 3) {
1208
+ const idxStr = parts[2];
1209
+ if (!NON_NEG_INT_RE.test(idxStr)) return null;
1210
+ const optionIndex = Number.parseInt(idxStr, 10);
1211
+ return { kind: "question", channelRequestId: id, optionIndex };
1212
+ }
1213
+ return null;
1214
+ }
1215
+ function buildPermissionCallbackData(channelRequestId, behavior) {
1216
+ return `${CB_PERMISSION}:${channelRequestId}:${CB_VERDICT[behavior]}`;
1217
+ }
1218
+ function buildQuestionCallbackData(channelRequestId, optionIndex) {
1219
+ return `${CB_QUESTION}:${channelRequestId}:${optionIndex}`;
1220
+ }
1221
+
1222
+ // src/gateway/adapters/telegram/relay.ts
1223
+ var MD_OPTIONS = {
1224
+ parse_mode: "MarkdownV2"
1225
+ };
1226
+ var TELEGRAM_MAX_TEXT = 4096;
1227
+ var TELEGRAM_TEXT_SAFE_MARGIN = 96;
1228
+ var TelegramRelay = class {
1229
+ pending = /* @__PURE__ */ new Map();
1230
+ resolveTarget;
1231
+ log;
1232
+ bot = null;
1233
+ constructor(opts) {
1234
+ this.resolveTarget = opts.resolveTarget;
1235
+ this.log = opts.log;
1236
+ }
1237
+ bindBot(bot) {
1238
+ this.bot = bot;
1239
+ }
1240
+ async requestPermission(req, signal) {
1241
+ const bot = this.bot;
1242
+ const target = this.resolveTarget();
1243
+ if (!bot || !target) {
1244
+ return { kind: "no_relay" };
1245
+ }
1246
+ if (signal.aborted) {
1247
+ return { kind: "cancelled", reason: "auto_resolved" };
1248
+ }
1249
+ const headline = `${req.toolName} \u2014 ${req.description}`;
1250
+ const text = buildPromptMarkdown(
1251
+ req.toolName,
1252
+ req.description,
1253
+ req.inputPreview,
1254
+ req.channelRequestId
1255
+ );
1256
+ const reply_markup = buildPermissionKeyboard(req.channelRequestId);
1257
+ const sent = await bot.sendMessage(target.chatId, text, {
1258
+ ...MD_OPTIONS,
1259
+ reply_markup,
1260
+ ...target.threadId !== void 0 ? { message_thread_id: target.threadId } : {}
1261
+ });
1262
+ if (!sent) {
1263
+ return { kind: "no_relay" };
1264
+ }
1265
+ return new Promise((resolve) => {
1266
+ const abortListener = () => {
1267
+ const entry = this.pending.get(req.channelRequestId);
1268
+ if (!entry || entry.kind !== "permission") return;
1269
+ this.pending.delete(req.channelRequestId);
1270
+ void this.editToCancelled(entry);
1271
+ resolve({ kind: "cancelled", reason: "resolved_by_other_channel" });
1272
+ };
1273
+ signal.addEventListener("abort", abortListener);
1274
+ this.pending.set(req.channelRequestId, {
1275
+ kind: "permission",
1276
+ channelRequestId: req.channelRequestId,
1277
+ chatId: sent.chat.id,
1278
+ messageId: sent.message_id,
1279
+ headline,
1280
+ resolve,
1281
+ abortListener,
1282
+ signal
1283
+ });
1284
+ });
1285
+ }
1286
+ async requestQuestion(req, signal) {
1287
+ const bot = this.bot;
1288
+ const target = this.resolveTarget();
1289
+ if (!bot || !target) {
1290
+ return { kind: "no_relay" };
1291
+ }
1292
+ if (signal.aborted) {
1293
+ return { kind: "cancelled", reason: "auto_resolved" };
1294
+ }
1295
+ const headline = req.title.trim() || "Question";
1296
+ const keyboard = buildQuestionKeyboard(req.channelRequestId, req.questions);
1297
+ const text = buildQuestionMarkdown(
1298
+ headline,
1299
+ req.questions,
1300
+ req.channelRequestId,
1301
+ keyboard !== null
1302
+ );
1303
+ const sent = await bot.sendMessage(target.chatId, text, {
1304
+ ...MD_OPTIONS,
1305
+ ...keyboard !== null ? { reply_markup: keyboard.markup } : {},
1306
+ ...target.threadId !== void 0 ? { message_thread_id: target.threadId } : {}
1307
+ });
1308
+ if (!sent) {
1309
+ return { kind: "no_relay" };
1310
+ }
1311
+ return new Promise((resolve) => {
1312
+ const abortListener = () => {
1313
+ const entry = this.pending.get(req.channelRequestId);
1314
+ if (!entry || entry.kind !== "question") return;
1315
+ this.pending.delete(req.channelRequestId);
1316
+ void this.editToCancelled(entry);
1317
+ resolve({ kind: "cancelled", reason: "resolved_by_other_channel" });
1318
+ };
1319
+ signal.addEventListener("abort", abortListener);
1320
+ this.pending.set(req.channelRequestId, {
1321
+ kind: "question",
1322
+ channelRequestId: req.channelRequestId,
1323
+ chatId: sent.chat.id,
1324
+ messageId: sent.message_id,
1325
+ headline,
1326
+ questionKeys: req.questions.map((q) => q.key),
1327
+ buttonOptions: keyboard?.options ?? null,
1328
+ resolve,
1329
+ abortListener,
1330
+ signal
1331
+ });
1332
+ });
1333
+ }
1334
+ /** Returns true if the update was consumed by a relay (verdict/answer). */
1335
+ handleUpdate(update) {
1336
+ const bot = this.bot;
1337
+ if (!bot) return false;
1338
+ const cb = update.callback_query;
1339
+ if (cb?.data) {
1340
+ const parsed = parseCallbackData(cb.data);
1341
+ if (!parsed) return false;
1342
+ const entry = this.pending.get(parsed.channelRequestId);
1343
+ if (!entry) {
1344
+ void bot.answerCallbackQuery(cb.id, "request expired");
1345
+ return true;
1346
+ }
1347
+ if (parsed.kind === "permission" && entry.kind === "permission") {
1348
+ this.settlePermission(entry, parsed.behavior);
1349
+ void bot.answerCallbackQuery(cb.id, parsed.behavior);
1350
+ return true;
1351
+ }
1352
+ if (parsed.kind === "question" && entry.kind === "question") {
1353
+ const opt = entry.buttonOptions?.[parsed.optionIndex];
1354
+ if (!opt) {
1355
+ void bot.answerCallbackQuery(cb.id, "unknown option");
1356
+ return true;
1357
+ }
1358
+ this.settleQuestion(entry, { [opt.key]: opt.label });
1359
+ void bot.answerCallbackQuery(cb.id, opt.label);
1360
+ return true;
1361
+ }
1362
+ return false;
1363
+ }
1364
+ const message = update.message ?? update.edited_message;
1365
+ const text = message?.text;
1366
+ if (typeof text !== "string" || text.length === 0) return false;
1367
+ const verdict = parseVerdict(text);
1368
+ if (verdict) {
1369
+ const entry = this.pending.get(verdict.channelRequestId);
1370
+ if (entry?.kind === "permission") {
1371
+ this.settlePermission(entry, verdict.behavior);
1372
+ return true;
1373
+ }
1374
+ return false;
1375
+ }
1376
+ const answerId = parseQuestionAnswerId(text);
1377
+ if (answerId) {
1378
+ const entry = this.pending.get(answerId);
1379
+ if (entry?.kind === "question") {
1380
+ const parsed = parseQuestionAnswer(text, entry.questionKeys);
1381
+ if (!parsed) return false;
1382
+ this.settleQuestion(entry, parsed.answers);
1383
+ return true;
1384
+ }
1385
+ return false;
1386
+ }
1387
+ if (message && "reply_to_message" in message && message.reply_to_message) {
1388
+ const replyTo = message.reply_to_message.message_id;
1389
+ for (const entry of this.pending.values()) {
1390
+ if (entry.kind !== "question") continue;
1391
+ if (entry.messageId !== replyTo) continue;
1392
+ const parsed = buildPlainTextQuestionAnswer(
1393
+ entry.channelRequestId,
1394
+ text,
1395
+ entry.questionKeys
1396
+ );
1397
+ if (!parsed) return false;
1398
+ this.settleQuestion(entry, parsed.answers);
1399
+ return true;
1400
+ }
1401
+ }
1402
+ return false;
1403
+ }
1404
+ disposeAll() {
1405
+ for (const entry of [...this.pending.values()]) {
1406
+ this.pending.delete(entry.channelRequestId);
1407
+ entry.signal.removeEventListener("abort", entry.abortListener);
1408
+ if (entry.kind === "permission") {
1409
+ entry.resolve({ kind: "cancelled", reason: "auto_resolved" });
1410
+ } else {
1411
+ entry.resolve({ kind: "cancelled", reason: "auto_resolved" });
1412
+ }
1413
+ }
1414
+ }
1415
+ settlePermission(entry, behavior) {
1416
+ this.pending.delete(entry.channelRequestId);
1417
+ entry.signal.removeEventListener("abort", entry.abortListener);
1418
+ void this.editToResolved(
1419
+ entry,
1420
+ behavior === "allow" ? "Allowed" : "Denied"
1421
+ );
1422
+ entry.resolve({ kind: "verdict", behavior, channelId: "telegram" });
1423
+ }
1424
+ settleQuestion(entry, answers) {
1425
+ this.pending.delete(entry.channelRequestId);
1426
+ entry.signal.removeEventListener("abort", entry.abortListener);
1427
+ const summary = Object.values(answers).join(", ").slice(0, 120);
1428
+ void this.editToResolved(entry, summary || "Answered");
1429
+ entry.resolve({ kind: "answer", answers, channelId: "telegram" });
1430
+ }
1431
+ async editToResolved(entry, label) {
1432
+ const bot = this.bot;
1433
+ if (!bot) return;
1434
+ try {
1435
+ await bot.editMessageText(
1436
+ entry.chatId,
1437
+ entry.messageId,
1438
+ buildResolvedText(entry.headline, label),
1439
+ MD_OPTIONS
1440
+ );
1441
+ } catch (err) {
1442
+ this.log(
1443
+ "debug",
1444
+ `telegram relay: edit-to-resolved failed: ${err instanceof Error ? err.message : String(err)}`
1445
+ );
1446
+ }
1447
+ }
1448
+ async editToCancelled(entry) {
1449
+ const bot = this.bot;
1450
+ if (!bot) return;
1451
+ try {
1452
+ await bot.editMessageText(
1453
+ entry.chatId,
1454
+ entry.messageId,
1455
+ buildCancelText("resolved elsewhere"),
1456
+ MD_OPTIONS
1457
+ );
1458
+ } catch (err) {
1459
+ this.log(
1460
+ "debug",
1461
+ `telegram relay: edit-to-cancelled failed: ${err instanceof Error ? err.message : String(err)}`
1462
+ );
1463
+ }
1464
+ }
1465
+ };
1466
+ function clampToTelegramLimit(text) {
1467
+ if (text.length <= TELEGRAM_MAX_TEXT) return text;
1468
+ return text.slice(0, TELEGRAM_MAX_TEXT - 1) + "\u2026";
1469
+ }
1470
+ function buildPromptMarkdown(toolName, description, inputPreview, channelRequestId) {
1471
+ const trimmedPreview = inputPreview.trim();
1472
+ const render = (preview) => {
1473
+ const lines = [
1474
+ `*${escapeMarkdownV2(toolName)}* \u2014 ${escapeMarkdownV2(description)}`
1475
+ ];
1476
+ if (preview.length > 0) {
1477
+ lines.push("", "```", escapeMarkdownV2CodeBlock(preview), "```");
1478
+ }
1479
+ lines.push(
1480
+ "",
1481
+ escapeMarkdownV2(
1482
+ `Tap a button below, or reply "yes ${channelRequestId}" / "no ${channelRequestId}".`
1483
+ )
1484
+ );
1485
+ return lines.join("\n");
1486
+ };
1487
+ const first = render(trimmedPreview);
1488
+ const budget = TELEGRAM_MAX_TEXT - TELEGRAM_TEXT_SAFE_MARGIN;
1489
+ if (first.length <= budget) return first;
1490
+ const overflow = first.length - budget;
1491
+ const safePreview = trimmedPreview.length > overflow ? trimmedPreview.slice(0, Math.max(0, trimmedPreview.length - overflow)) + "\u2026" : "";
1492
+ return clampToTelegramLimit(render(safePreview));
1493
+ }
1494
+ function buildPermissionKeyboard(channelRequestId) {
1495
+ return {
1496
+ inline_keyboard: [
1497
+ [
1498
+ {
1499
+ text: "\u2705 Allow",
1500
+ callback_data: buildPermissionCallbackData(channelRequestId, "allow")
1501
+ },
1502
+ {
1503
+ text: "\u274C Deny",
1504
+ callback_data: buildPermissionCallbackData(channelRequestId, "deny")
1505
+ }
1506
+ ]
1507
+ ]
1508
+ };
1509
+ }
1510
+ function buildQuestionMarkdown(title, questions, channelRequestId, hasButtons) {
1511
+ const lines = [`*${escapeMarkdownV2(title)}*`];
1512
+ for (const [index, q] of questions.entries()) {
1513
+ lines.push("");
1514
+ lines.push(
1515
+ `${index + 1}\\. *${escapeMarkdownV2(q.header)}*: ${escapeMarkdownV2(q.question)}`
1516
+ );
1517
+ if (q.options.length > 0 && !hasButtons) {
1518
+ for (const option of q.options) {
1519
+ const suffix = option.description ? ` \u2014 ${option.description}` : "";
1520
+ lines.push(` \u2022 ${escapeMarkdownV2(option.label + suffix)}`);
1521
+ }
1522
+ }
1523
+ }
1524
+ const trailer = hasButtons ? escapeMarkdownV2("Tap an option below.") : questions.length <= 1 ? escapeMarkdownV2(
1525
+ `Reply with your answer, or "answer ${channelRequestId} your response".`
1526
+ ) : escapeMarkdownV2(
1527
+ `Reply 'answer ${channelRequestId} {"Question":"Answer"}' to respond.`
1528
+ );
1529
+ lines.push("", trailer);
1530
+ return clampToTelegramLimit(lines.join("\n"));
1531
+ }
1532
+ function buildQuestionKeyboard(channelRequestId, questions) {
1533
+ if (questions.length !== 1) return null;
1534
+ const q = questions[0];
1535
+ if (q.multi_select || q.options.length === 0) return null;
1536
+ const rows = [];
1537
+ const options = [];
1538
+ for (const [optIdx, option] of q.options.entries()) {
1539
+ const data = buildQuestionCallbackData(channelRequestId, optIdx);
1540
+ if (Buffer.byteLength(data, "utf8") > 64) return null;
1541
+ rows.push([{ text: option.label, callback_data: data }]);
1542
+ options.push({ key: q.key, label: option.label });
1543
+ }
1544
+ return { markup: { inline_keyboard: rows }, options };
1545
+ }
1546
+ function buildResolvedText(headline, label) {
1547
+ return `*${escapeMarkdownV2(headline)}*
1548
+
1549
+ ${escapeMarkdownV2(`\u2713 ${label}`)}`;
1550
+ }
1551
+ function buildCancelText(reason) {
1552
+ return escapeMarkdownV2(`~ resolved (${reason}) ~`);
1553
+ }
1554
+
1555
+ // src/gateway/adapters/telegram/adapter.ts
1556
+ var TELEGRAM_ID = "telegram";
1557
+ var TelegramAdapter = class {
1558
+ id = TELEGRAM_ID;
1559
+ capabilities = {
1560
+ chat: true,
1561
+ threads: true,
1562
+ relayPermission: true,
1563
+ relayQuestion: true,
1564
+ // Telegram caps text at 4096 chars; we leave chunking to the manager.
1565
+ maxMessageBytes: 4096
1566
+ };
1567
+ bot = null;
1568
+ opts;
1569
+ relay;
1570
+ pollTask = null;
1571
+ lastInboundAt;
1572
+ lastTransportOk = true;
1573
+ ctx = null;
1574
+ constructor(opts) {
1575
+ this.opts = opts;
1576
+ this.relay = new TelegramRelay({
1577
+ resolveTarget: () => {
1578
+ if (this.opts.defaultChatId === void 0) return null;
1579
+ return {
1580
+ chatId: this.opts.defaultChatId,
1581
+ ...this.opts.defaultThreadId !== void 0 ? { threadId: this.opts.defaultThreadId } : {}
1582
+ };
1583
+ },
1584
+ log: (level, msg) => this.ctx?.log(level, msg)
1585
+ });
1586
+ }
1587
+ requestPermissionVerdict(req, signal) {
1588
+ return this.relay.requestPermission(req, signal);
1589
+ }
1590
+ requestQuestionAnswer(req, signal) {
1591
+ return this.relay.requestQuestion(req, signal);
1592
+ }
1593
+ async start(ctx) {
1594
+ if (this.bot) {
1595
+ throw new Error("telegram adapter already started");
1596
+ }
1597
+ this.ctx = ctx;
1598
+ const factory = this.opts.botFactory ?? ((o) => new TelegramBot(
1599
+ {
1600
+ token: o.token,
1601
+ apiBase: o.apiBase,
1602
+ pollTimeoutSec: o.pollTimeoutSec
1603
+ },
1604
+ o.log
1605
+ ));
1606
+ this.bot = factory({
1607
+ token: this.opts.token,
1608
+ apiBase: this.opts.apiBase,
1609
+ pollTimeoutSec: this.opts.pollTimeoutSec,
1610
+ log: ctx.log
1611
+ });
1612
+ this.relay.bindBot(this.bot);
1613
+ this.pollTask = this.runPollLoop();
1614
+ ctx.signal.addEventListener("abort", () => {
1615
+ this.bot?.stop();
1616
+ });
1617
+ }
1618
+ async stop(_reason) {
1619
+ this.relay.disposeAll();
1620
+ this.bot?.stop();
1621
+ const task = this.pollTask;
1622
+ this.pollTask = null;
1623
+ if (task) {
1624
+ try {
1625
+ await task;
1626
+ } catch {
1627
+ }
1628
+ }
1629
+ this.relay.bindBot(null);
1630
+ this.bot = null;
1631
+ this.ctx = null;
1632
+ }
1633
+ async send(msg) {
1634
+ const bot = this.bot;
1635
+ if (!bot) {
1636
+ throw new Error("telegram adapter: send called before start");
1637
+ }
1638
+ const chatId = msg.location.peer?.id ?? msg.location.room?.id;
1639
+ if (!chatId) {
1640
+ throw new Error(
1641
+ "telegram adapter: outbound location has no peer or room"
1642
+ );
1643
+ }
1644
+ const threadId = msg.location.thread?.id ? Number(msg.location.thread.id) : void 0;
1645
+ const result = await bot.sendMessage(chatId, msg.text, {
1646
+ ...threadId !== void 0 && Number.isFinite(threadId) ? { message_thread_id: threadId } : {}
1647
+ });
1648
+ if (!result) {
1649
+ throw new Error("telegram adapter: sendMessage returned null");
1650
+ }
1651
+ return {
1652
+ providerMessageId: String(result.message_id),
1653
+ deliveredAt: Date.now()
1654
+ };
1655
+ }
1656
+ async probe() {
1657
+ return {
1658
+ ok: this.lastTransportOk,
1659
+ detail: this.lastTransportOk ? "long-poll healthy" : "long-poll erroring",
1660
+ checkedAt: Date.now()
1661
+ };
1662
+ }
1663
+ async runPollLoop() {
1664
+ const bot = this.bot;
1665
+ const ctx = this.ctx;
1666
+ if (!bot || !ctx) return;
1667
+ const allow = this.opts.allowedUserIds.length === 0 ? null : new Set(this.opts.allowedUserIds.map(String));
1668
+ try {
1669
+ for await (const update of bot.poll()) {
1670
+ if (this.relay.handleUpdate(update)) {
1671
+ this.markHealth(true);
1672
+ continue;
1673
+ }
1674
+ const inbound = normalizeInbound2(update, allow);
1675
+ if (!inbound) continue;
1676
+ this.lastInboundAt = inbound.receivedAt;
1677
+ this.markHealth(true);
1678
+ try {
1679
+ ctx.emitInbound(inbound);
1680
+ } catch (err) {
1681
+ ctx.log(
1682
+ "warn",
1683
+ `telegram emitInbound threw: ${err instanceof Error ? err.message : String(err)}`
1684
+ );
1685
+ }
1686
+ }
1687
+ } catch (err) {
1688
+ this.markHealth(false, err instanceof Error ? err.message : String(err));
1689
+ ctx.log(
1690
+ "error",
1691
+ `telegram poll loop terminated: ${err instanceof Error ? err.message : String(err)}`
1692
+ );
1693
+ throw err;
1694
+ }
1695
+ }
1696
+ markHealth(ok2, note) {
1697
+ this.lastTransportOk = ok2;
1698
+ const ctx = this.ctx;
1699
+ if (!ctx) return;
1700
+ const sample = {
1701
+ at: Date.now(),
1702
+ transportOk: ok2,
1703
+ ...this.lastInboundAt !== void 0 ? { lastInboundAt: this.lastInboundAt } : {},
1704
+ ...note !== void 0 ? { note } : {}
1705
+ };
1706
+ try {
1707
+ ctx.emitHealth(sample);
1708
+ } catch {
1709
+ }
1710
+ }
1711
+ };
1712
+ function normalizeInbound2(update, allow) {
1713
+ const message = update.message ?? update.edited_message;
1714
+ if (!message) return null;
1715
+ const text = message.text;
1716
+ if (typeof text !== "string" || text.length === 0) return null;
1717
+ const sender = message.from;
1718
+ if (!sender) return null;
1719
+ const senderId = String(sender.id);
1720
+ if (allow && !allow.has(senderId)) return null;
1721
+ const accountId = String(sender.is_bot ? `bot:${sender.id}` : "user");
1722
+ const chatId = String(message.chat.id);
1723
+ const isPrivate = message.chat.type === "private";
1724
+ const threadId = typeof message.message_thread_id === "number" ? String(message.message_thread_id) : void 0;
1725
+ return {
1726
+ location: {
1727
+ channelId: TELEGRAM_ID,
1728
+ accountId,
1729
+ ...isPrivate ? { peer: { id: chatId, kind: "user" } } : {
1730
+ room: {
1731
+ id: chatId,
1732
+ kind: message.chat.type === "channel" ? "channel" : "group"
1733
+ }
1734
+ },
1735
+ ...threadId !== void 0 ? { thread: { id: threadId } } : {}
1736
+ },
1737
+ sender: {
1738
+ id: senderId,
1739
+ ...sender.username !== void 0 ? { displayName: sender.username } : sender.first_name !== void 0 ? { displayName: sender.first_name } : {}
1740
+ },
1741
+ text,
1742
+ receivedAt: Date.now(),
1743
+ idempotencyKey: `tg:${update.update_id}`,
1744
+ providerMessageId: String(message.message_id)
1745
+ };
1746
+ }
1747
+
1748
+ // src/gateway/adapters/telegram/module.ts
1749
+ var telegramModule = {
1750
+ name: "telegram",
1751
+ parseConfig({ options, allowedUserIds }) {
1752
+ const token = options["bot_token"];
1753
+ if (typeof token !== "string" || token.length === 0) {
1754
+ return { ok: false, reason: "bot_token missing" };
1755
+ }
1756
+ const defaultChatRaw = options["default_chat_id"];
1757
+ const defaultThreadRaw = options["default_thread_id"];
1758
+ const apiBaseRaw = options["api_base"];
1759
+ const pollTimeoutRaw = options["poll_timeout_sec"];
1760
+ if (defaultChatRaw !== void 0 && typeof defaultChatRaw !== "string" && typeof defaultChatRaw !== "number") {
1761
+ return { ok: false, reason: "default_chat_id must be string or number" };
1762
+ }
1763
+ if (defaultThreadRaw !== void 0 && typeof defaultThreadRaw !== "number") {
1764
+ return { ok: false, reason: "default_thread_id must be number" };
1765
+ }
1766
+ if (apiBaseRaw !== void 0 && typeof apiBaseRaw !== "string") {
1767
+ return { ok: false, reason: "api_base must be string" };
1768
+ }
1769
+ if (pollTimeoutRaw !== void 0 && typeof pollTimeoutRaw !== "number") {
1770
+ return { ok: false, reason: "poll_timeout_sec must be number" };
1771
+ }
1772
+ const config = {
1773
+ token,
1774
+ allowedUserIds,
1775
+ ...defaultChatRaw !== void 0 ? { defaultChatId: defaultChatRaw } : {},
1776
+ ...defaultThreadRaw !== void 0 ? { defaultThreadId: defaultThreadRaw } : {},
1777
+ ...apiBaseRaw !== void 0 ? { apiBase: apiBaseRaw } : {},
1778
+ ...pollTimeoutRaw !== void 0 ? { pollTimeoutSec: pollTimeoutRaw } : {}
1779
+ };
1780
+ return { ok: true, config };
1781
+ },
1782
+ create(config) {
1783
+ return new TelegramAdapter(config);
1784
+ }
1785
+ };
1786
+
1787
+ // src/gateway/adapters/registry.ts
1788
+ var BUILTIN_MODULES = [
1789
+ telegramModule,
1790
+ consoleModule
1791
+ ];
1792
+ function findAdapterModule(name) {
1793
+ return BUILTIN_MODULES.find((m) => m.name === name);
1794
+ }
1795
+
1796
+ // src/gateway/adapters/factory.ts
1797
+ function instantiateAdapter(sidecar) {
1798
+ const module = findAdapterModule(sidecar.name);
1799
+ if (!module) {
1800
+ return { ok: false, reason: `unknown channel: ${sidecar.name}` };
1801
+ }
1802
+ const parsed = module.parseConfig({
1803
+ options: sidecar.options,
1804
+ allowedUserIds: sidecar.allowedUserIds
1805
+ });
1806
+ if (!parsed.ok) {
1807
+ return { ok: false, reason: `${sidecar.name}: ${parsed.reason}` };
1808
+ }
1809
+ return { ok: true, adapter: module.create(parsed.config) };
1810
+ }
1811
+
1812
+ // src/gateway/channelManager.ts
1813
+ var DEFAULT_DEDUP_WINDOW = 1024;
1814
+ var DuplicateChannelError = class extends Error {
1815
+ constructor(id) {
1816
+ super(`channel ${id} already registered`);
1817
+ this.name = "DuplicateChannelError";
1818
+ }
1819
+ };
1820
+ var UnknownChannelError = class extends Error {
1821
+ constructor(id) {
1822
+ super(`channel ${id} not registered`);
1823
+ this.name = "UnknownChannelError";
1824
+ }
1825
+ };
1826
+ var ChannelManager = class {
1827
+ entries = /* @__PURE__ */ new Map();
1828
+ dedup = [];
1829
+ dedupSet = /* @__PURE__ */ new Set();
1830
+ dedupMax;
1831
+ log;
1832
+ inboundSink = null;
1833
+ healthSink = null;
1834
+ stopped = false;
1835
+ constructor(opts = {}) {
1836
+ this.dedupMax = opts.dedupWindow ?? DEFAULT_DEDUP_WINDOW;
1837
+ this.log = opts.log;
1838
+ }
1839
+ /** Register the single inbound dispatch target. M5 wires the router here. */
1840
+ setInboundSink(sink) {
1841
+ this.inboundSink = sink;
1842
+ }
1843
+ setHealthSink(sink) {
1844
+ this.healthSink = sink;
1845
+ }
1846
+ listChannels() {
1847
+ return [...this.entries.values()].map((e) => ({
1848
+ id: e.adapter.id,
1849
+ health: e.lastHealth
1850
+ }));
1851
+ }
1852
+ /** Snapshot of currently registered adapters; used by the relay coordinator. */
1853
+ listAdapters() {
1854
+ return [...this.entries.values()].map((e) => e.adapter);
1855
+ }
1856
+ async register(adapter) {
1857
+ if (this.stopped) {
1858
+ throw new Error("channel manager already stopped");
1859
+ }
1860
+ if (this.entries.has(adapter.id)) {
1861
+ throw new DuplicateChannelError(adapter.id);
1862
+ }
1863
+ const abort = new AbortController();
1864
+ const inboundListener = (msg) => this.handleInbound(msg);
1865
+ const healthListener = (sample) => {
1866
+ const entry = this.entries.get(adapter.id);
1867
+ if (entry) entry.lastHealth = sample;
1868
+ this.healthSink?.(sample);
1869
+ };
1870
+ const startPromise = adapter.start({
1871
+ log: (level, msg) => this.log?.(level, `[${adapter.id}] ${msg}`),
1872
+ signal: abort.signal,
1873
+ emitInbound: inboundListener,
1874
+ emitHealth: healthListener
1875
+ });
1876
+ this.entries.set(adapter.id, {
1877
+ adapter,
1878
+ abort,
1879
+ startPromise
1880
+ });
1881
+ try {
1882
+ await startPromise;
1883
+ } catch (err) {
1884
+ this.entries.delete(adapter.id);
1885
+ throw err;
1886
+ }
1887
+ }
1888
+ async unregister(id, reason) {
1889
+ const entry = this.entries.get(id);
1890
+ if (!entry) {
1891
+ throw new UnknownChannelError(id);
1892
+ }
1893
+ this.entries.delete(id);
1894
+ entry.abort.abort();
1895
+ await entry.adapter.stop(reason);
1896
+ }
1897
+ async send(channelId, msg) {
1898
+ const entry = this.entries.get(channelId);
1899
+ if (!entry) {
1900
+ throw new UnknownChannelError(channelId);
1901
+ }
1902
+ return entry.adapter.send(msg);
1903
+ }
1904
+ async probe(channelId) {
1905
+ const entry = this.entries.get(channelId);
1906
+ if (!entry) {
1907
+ throw new UnknownChannelError(channelId);
1908
+ }
1909
+ return entry.adapter.probe();
1910
+ }
1911
+ async stop(reason = "shutdown") {
1912
+ if (this.stopped) return;
1913
+ this.stopped = true;
1914
+ const ids = [...this.entries.keys()];
1915
+ for (const id of ids.reverse()) {
1916
+ try {
1917
+ await this.unregister(id, reason);
1918
+ } catch (err) {
1919
+ this.log?.(
1920
+ "warn",
1921
+ `channel ${id} stop failed: ${err instanceof Error ? err.message : String(err)}`
1922
+ );
1923
+ }
1924
+ }
1925
+ }
1926
+ handleInbound(msg) {
1927
+ if (this.dedupSet.has(msg.idempotencyKey)) {
1928
+ this.log?.("debug", `dropping duplicate inbound ${msg.idempotencyKey}`);
1929
+ return;
1930
+ }
1931
+ this.dedupSet.add(msg.idempotencyKey);
1932
+ this.dedup.push(msg.idempotencyKey);
1933
+ while (this.dedup.length > this.dedupMax) {
1934
+ const evicted = this.dedup.shift();
1935
+ if (evicted !== void 0) this.dedupSet.delete(evicted);
1936
+ }
1937
+ const sink = this.inboundSink;
1938
+ if (!sink) {
1939
+ this.log?.(
1940
+ "debug",
1941
+ `no inbound sink registered; dropping ${msg.idempotencyKey}`
1942
+ );
1943
+ return;
1944
+ }
1945
+ try {
1946
+ sink(msg);
1947
+ } catch (err) {
1948
+ this.log?.(
1949
+ "warn",
1950
+ `inbound sink threw: ${err instanceof Error ? err.message : String(err)}`
1951
+ );
1952
+ }
1953
+ }
1954
+ };
1955
+
1956
+ // src/gateway/control/handlers.ts
1957
+ import { createRequire } from "module";
1958
+
1959
+ // src/gateway/sessionRegistry.ts
1960
+ import { randomUUID } from "crypto";
1961
+ var AlreadyRegisteredError = class extends Error {
1962
+ code = "already_registered";
1963
+ constructor(existing) {
1964
+ super(
1965
+ `gateway already has a registered runtime (pid=${existing.pid}, runtimeId=${existing.runtimeId})`
1966
+ );
1967
+ this.name = "AlreadyRegisteredError";
1968
+ }
1969
+ };
1970
+ var NotRegisteredError = class extends Error {
1971
+ code = "not_registered";
1972
+ constructor() {
1973
+ super("no runtime registered with gateway");
1974
+ this.name = "NotRegisteredError";
1975
+ }
1976
+ };
1977
+ var UnknownDispatchError = class extends Error {
1978
+ code = "unknown_dispatch";
1979
+ constructor(id) {
1980
+ super(`unknown dispatchId: ${id}`);
1981
+ this.name = "UnknownDispatchError";
1982
+ }
1983
+ };
1984
+ function maybeLastRebindAt(value) {
1985
+ return value !== void 0 ? { lastRebindAt: value } : {};
1986
+ }
1987
+ var SessionRegistry = class {
1988
+ current = null;
1989
+ binding = null;
1990
+ dispatches = /* @__PURE__ */ new Map();
1991
+ idFactory;
1992
+ now;
1993
+ constructor(opts = {}) {
1994
+ this.idFactory = opts.idFactory ?? randomUUID;
1995
+ this.now = opts.now ?? Date.now;
1996
+ }
1997
+ register(input) {
1998
+ if (this.current) {
1999
+ if (this.current.runtimeId === input.runtimeId) {
2000
+ this.current = {
2001
+ ...this.current,
2002
+ defaultAgentId: input.defaultAgentId,
2003
+ pid: input.pid
2004
+ };
2005
+ return this.current;
2006
+ }
2007
+ throw new AlreadyRegisteredError(this.current);
2008
+ }
2009
+ this.current = { ...input, registeredAt: this.now() };
2010
+ return this.current;
2011
+ }
2012
+ bindConnection(runtimeId, connectionId) {
2013
+ if (!this.current || this.current.runtimeId !== runtimeId) {
2014
+ throw new NotRegisteredError();
2015
+ }
2016
+ const previous = this.binding;
2017
+ const now = this.now();
2018
+ const isRebind = previous !== null && (previous.state === "stale" || previous.connectionId !== connectionId);
2019
+ const lastRebindAt = isRebind ? now : previous?.lastRebindAt;
2020
+ const epoch = previous ? previous.epoch + (isRebind ? 1 : 0) : 1;
2021
+ this.binding = {
2022
+ state: "active",
2023
+ connectionId,
2024
+ boundAt: now,
2025
+ epoch,
2026
+ ...maybeLastRebindAt(lastRebindAt)
2027
+ };
2028
+ }
2029
+ markConnectionStale(connectionId) {
2030
+ if (!this.current || !this.binding || this.binding.connectionId !== connectionId || this.binding.state !== "active") {
2031
+ return null;
2032
+ }
2033
+ this.binding = {
2034
+ state: "stale",
2035
+ connectionId,
2036
+ staleSince: this.now(),
2037
+ epoch: this.binding.epoch,
2038
+ ...maybeLastRebindAt(this.binding.lastRebindAt)
2039
+ };
2040
+ return this.current.runtimeId;
2041
+ }
2042
+ hasActiveBinding(runtimeId) {
2043
+ if (!this.current || !this.binding || this.binding.state !== "active") {
2044
+ return false;
2045
+ }
2046
+ return runtimeId === void 0 || this.current.runtimeId === runtimeId;
2047
+ }
2048
+ getBinding() {
2049
+ return this.binding;
2050
+ }
2051
+ getRuntimeIdByConnection(connectionId) {
2052
+ if (!this.current || !this.binding) return null;
2053
+ return this.binding.connectionId === connectionId ? this.current.runtimeId : null;
2054
+ }
2055
+ unregister(runtimeId) {
2056
+ if (!this.current || this.current.runtimeId !== runtimeId) {
2057
+ throw new NotRegisteredError();
2058
+ }
2059
+ this.current = null;
2060
+ this.binding = null;
2061
+ this.dispatches.clear();
2062
+ }
2063
+ getCurrent() {
2064
+ return this.current;
2065
+ }
2066
+ beginDispatch(input) {
2067
+ if (!this.current) {
2068
+ throw new NotRegisteredError();
2069
+ }
2070
+ const dispatchId = this.idFactory();
2071
+ const entry = {
2072
+ dispatchId,
2073
+ sessionKey: input.sessionKey,
2074
+ agentId: input.agentId,
2075
+ location: input.location,
2076
+ createdAt: this.now()
2077
+ };
2078
+ this.dispatches.set(dispatchId, entry);
2079
+ return entry;
2080
+ }
2081
+ completeDispatch(dispatchId) {
2082
+ const entry = this.dispatches.get(dispatchId);
2083
+ if (!entry) {
2084
+ throw new UnknownDispatchError(dispatchId);
2085
+ }
2086
+ this.dispatches.delete(dispatchId);
2087
+ return entry;
2088
+ }
2089
+ pendingDispatchCount() {
2090
+ return this.dispatches.size;
2091
+ }
2092
+ };
2093
+
2094
+ // src/gateway/control/handlers.ts
2095
+ var require_ = createRequire(import.meta.url);
2096
+ var cachedVersion = null;
2097
+ function readVersion() {
2098
+ if (cachedVersion !== null) return cachedVersion;
2099
+ try {
2100
+ const injected = "0.3.39";
2101
+ if (typeof injected === "string" && injected.length > 0) {
2102
+ cachedVersion = injected;
2103
+ return cachedVersion;
2104
+ }
2105
+ } catch {
2106
+ }
2107
+ try {
2108
+ const pkg = require_("../../../package.json");
2109
+ cachedVersion = pkg.version ?? "0.0.0";
2110
+ } catch {
2111
+ cachedVersion = "0.0.0";
2112
+ }
2113
+ return cachedVersion;
2114
+ }
2115
+ function createDispatcher(deps) {
2116
+ const handle = async (envelope, connection) => {
2117
+ const ts = Date.now();
2118
+ switch (envelope.kind) {
2119
+ case "ping": {
2120
+ const payload = {
2121
+ pong: true,
2122
+ daemonPid: process.pid,
2123
+ uptimeMs: ts - deps.startedAt
2124
+ };
2125
+ return ok(envelope, ts, payload);
2126
+ }
2127
+ case "status": {
2128
+ const channels = (deps.channelManager?.listChannels() ?? []).map((c) => ({
2129
+ id: c.id,
2130
+ state: c.health?.transportOk === false ? "degraded" : "running",
2131
+ ...c.health?.at !== void 0 ? { lastHealthAt: c.health.at } : {},
2132
+ ...c.health?.note !== void 0 ? { note: c.health.note } : {}
2133
+ }));
2134
+ const payload = {
2135
+ daemonPid: process.pid,
2136
+ startedAt: deps.startedAt,
2137
+ uptimeMs: ts - deps.startedAt,
2138
+ version: readVersion(),
2139
+ listener: deps.getListener?.() ?? {
2140
+ kind: "uds",
2141
+ socketPath: "<unknown>"
2142
+ },
2143
+ channels,
2144
+ runtimes: runtimeStatusEntries(deps.registry)
2145
+ };
2146
+ return ok(envelope, ts, payload);
2147
+ }
2148
+ case "channels.reload": {
2149
+ if (!deps.reloadChannels) {
2150
+ return error(
2151
+ envelope,
2152
+ ts,
2153
+ "unsupported",
2154
+ "channel reload not configured"
2155
+ );
2156
+ }
2157
+ const payload = await deps.reloadChannels();
2158
+ return ok(envelope, ts, payload);
2159
+ }
2160
+ case "session.register": {
2161
+ if (!deps.registry)
2162
+ return error(
2163
+ envelope,
2164
+ ts,
2165
+ "unsupported",
2166
+ "session.register not configured"
2167
+ );
2168
+ const req = envelope.payload;
2169
+ try {
2170
+ const reg = deps.registry.register({
2171
+ runtimeId: req.runtimeId,
2172
+ defaultAgentId: req.defaultAgentId,
2173
+ pid: req.pid
2174
+ });
2175
+ deps.registerRuntimeConnection?.(req.runtimeId, connection);
2176
+ try {
2177
+ deps.dispatcher?.drainPending();
2178
+ } catch (err) {
2179
+ process.stderr.write(
2180
+ `gateway: drainPending failed: ${err instanceof Error ? err.message : String(err)}
2181
+ `
2182
+ );
2183
+ }
2184
+ const payload = {
2185
+ registeredAt: reg.registeredAt,
2186
+ gatewayStartedAt: deps.startedAt
2187
+ };
2188
+ return ok(envelope, ts, payload);
2189
+ } catch (err) {
2190
+ if (err instanceof AlreadyRegisteredError) {
2191
+ return error(envelope, ts, err.code, err.message);
2192
+ }
2193
+ throw err;
2194
+ }
2195
+ }
2196
+ case "session.unregister": {
2197
+ if (!deps.registry)
2198
+ return error(
2199
+ envelope,
2200
+ ts,
2201
+ "unsupported",
2202
+ "session.unregister not configured"
2203
+ );
2204
+ const req = envelope.payload;
2205
+ try {
2206
+ deps.registry.unregister(req.runtimeId);
2207
+ deps.unregisterRuntimeConnection?.(req.runtimeId);
2208
+ const payload = {
2209
+ unregisteredAt: ts
2210
+ };
2211
+ return ok(envelope, ts, payload);
2212
+ } catch (err) {
2213
+ if (err instanceof NotRegisteredError) {
2214
+ return error(envelope, ts, err.code, err.message);
2215
+ }
2216
+ throw err;
2217
+ }
2218
+ }
2219
+ case "session.turn.complete": {
2220
+ if (!deps.dispatcher)
2221
+ return error(
2222
+ envelope,
2223
+ ts,
2224
+ "unsupported",
2225
+ "dispatcher not configured"
2226
+ );
2227
+ const req = envelope.payload;
2228
+ const result = await deps.dispatcher.handleTurnComplete(req);
2229
+ return ok(envelope, ts, result);
2230
+ }
2231
+ case "channel.send": {
2232
+ if (!deps.channelManager)
2233
+ return error(
2234
+ envelope,
2235
+ ts,
2236
+ "unsupported",
2237
+ "channel manager not configured"
2238
+ );
2239
+ const req = envelope.payload;
2240
+ const result = await deps.channelManager.send(
2241
+ req.message.location.channelId,
2242
+ req.message
2243
+ );
2244
+ const payload = {
2245
+ providerMessageId: result.providerMessageId,
2246
+ deliveredAt: result.deliveredAt
2247
+ };
2248
+ return ok(envelope, ts, payload);
2249
+ }
2250
+ case "relay.permission.request": {
2251
+ if (!deps.relayCoordinator)
2252
+ return error(
2253
+ envelope,
2254
+ ts,
2255
+ "unsupported",
2256
+ "relay coordinator not configured"
2257
+ );
2258
+ const req = envelope.payload;
2259
+ const callerRuntimeId = deps.registry?.getRuntimeIdByConnection(connection.connectionId) ?? void 0;
2260
+ if (deps.registry && callerRuntimeId === void 0) {
2261
+ return error(
2262
+ envelope,
2263
+ ts,
2264
+ "not_registered",
2265
+ "relay.permission.request requires a registered runtime connection"
2266
+ );
2267
+ }
2268
+ const broadcast = deps.relayCoordinator.requestPermission({
2269
+ ...req.channelRequestId !== void 0 ? { channelRequestId: req.channelRequestId } : {},
2270
+ toolName: req.toolName,
2271
+ description: req.description,
2272
+ inputPreview: req.inputPreview,
2273
+ ...req.ttlMs !== void 0 ? { ttlMs: req.ttlMs } : {},
2274
+ ...callerRuntimeId !== void 0 ? { runtimeId: callerRuntimeId } : {}
2275
+ });
2276
+ const result = await broadcast.result;
2277
+ const payload = {
2278
+ channelRequestId: broadcast.channelRequestId,
2279
+ result
2280
+ };
2281
+ return ok(envelope, Date.now(), payload);
2282
+ }
2283
+ case "relay.permission.cancel": {
2284
+ if (!deps.relayCoordinator)
2285
+ return error(
2286
+ envelope,
2287
+ ts,
2288
+ "unsupported",
2289
+ "relay coordinator not configured"
2290
+ );
2291
+ const req = envelope.payload;
2292
+ const callerRuntimeId = deps.registry?.getRuntimeIdByConnection(connection.connectionId) ?? void 0;
2293
+ if (deps.registry && callerRuntimeId === void 0) {
2294
+ return error(
2295
+ envelope,
2296
+ ts,
2297
+ "not_registered",
2298
+ "relay.permission.cancel requires a registered runtime connection"
2299
+ );
2300
+ }
2301
+ const cancelled = deps.relayCoordinator.cancel(
2302
+ req.channelRequestId,
2303
+ req.reason,
2304
+ callerRuntimeId
2305
+ );
2306
+ const payload = { cancelled };
2307
+ return ok(envelope, ts, payload);
2308
+ }
2309
+ case "relay.question.request": {
2310
+ if (!deps.relayCoordinator)
2311
+ return error(
2312
+ envelope,
2313
+ ts,
2314
+ "unsupported",
2315
+ "relay coordinator not configured"
2316
+ );
2317
+ const req = envelope.payload;
2318
+ const callerRuntimeId = deps.registry?.getRuntimeIdByConnection(connection.connectionId) ?? void 0;
2319
+ if (deps.registry && callerRuntimeId === void 0) {
2320
+ return error(
2321
+ envelope,
2322
+ ts,
2323
+ "not_registered",
2324
+ "relay.question.request requires a registered runtime connection"
2325
+ );
2326
+ }
2327
+ const broadcast = deps.relayCoordinator.requestQuestion({
2328
+ ...req.channelRequestId !== void 0 ? { channelRequestId: req.channelRequestId } : {},
2329
+ title: req.title,
2330
+ questions: req.questions,
2331
+ ...req.ttlMs !== void 0 ? { ttlMs: req.ttlMs } : {},
2332
+ ...callerRuntimeId !== void 0 ? { runtimeId: callerRuntimeId } : {}
2333
+ });
2334
+ const result = await broadcast.result;
2335
+ const payload = {
2336
+ channelRequestId: broadcast.channelRequestId,
2337
+ result
2338
+ };
2339
+ return ok(envelope, Date.now(), payload);
2340
+ }
2341
+ case "relay.question.cancel": {
2342
+ if (!deps.relayCoordinator)
2343
+ return error(
2344
+ envelope,
2345
+ ts,
2346
+ "unsupported",
2347
+ "relay coordinator not configured"
2348
+ );
2349
+ const req = envelope.payload;
2350
+ const callerRuntimeId = deps.registry?.getRuntimeIdByConnection(connection.connectionId) ?? void 0;
2351
+ if (deps.registry && callerRuntimeId === void 0) {
2352
+ return error(
2353
+ envelope,
2354
+ ts,
2355
+ "not_registered",
2356
+ "relay.question.cancel requires a registered runtime connection"
2357
+ );
2358
+ }
2359
+ const cancelled = deps.relayCoordinator.cancel(
2360
+ req.channelRequestId,
2361
+ req.reason,
2362
+ callerRuntimeId
2363
+ );
2364
+ const payload = { cancelled };
2365
+ return ok(envelope, ts, payload);
2366
+ }
2367
+ default:
2368
+ return error(
2369
+ envelope,
2370
+ ts,
2371
+ "unknown_kind",
2372
+ `unknown kind: ${envelope.kind}`
2373
+ );
2374
+ }
2375
+ };
2376
+ return handle;
2377
+ }
2378
+ function runtimeStatusEntries(registry) {
2379
+ const runtime = registry?.getCurrent();
2380
+ if (!runtime || !registry) return [];
2381
+ const binding = registry.getBinding();
2382
+ return [
2383
+ {
2384
+ runtimeId: runtime.runtimeId,
2385
+ defaultAgentId: runtime.defaultAgentId,
2386
+ pid: runtime.pid,
2387
+ registeredAt: runtime.registeredAt,
2388
+ binding: binding?.state === "active" ? {
2389
+ state: "active",
2390
+ boundAt: binding.boundAt,
2391
+ epoch: binding.epoch,
2392
+ ...maybeLastRebindAt(binding.lastRebindAt)
2393
+ } : binding?.state === "stale" ? {
2394
+ state: "stale",
2395
+ staleSince: binding.staleSince,
2396
+ epoch: binding.epoch,
2397
+ ...maybeLastRebindAt(binding.lastRebindAt)
2398
+ } : { state: "none" },
2399
+ pendingDispatchCount: registry.pendingDispatchCount()
2400
+ }
2401
+ ];
2402
+ }
2403
+ function ok(envelope, ts, payload) {
2404
+ return { request_id: envelope.request_id, ts, ok: true, payload };
2405
+ }
2406
+ function error(envelope, ts, code, message) {
2407
+ return {
2408
+ request_id: envelope.request_id,
2409
+ ts,
2410
+ ok: false,
2411
+ error: { code, message }
2412
+ };
2413
+ }
2414
+
2415
+ // src/gateway/control/server.ts
2416
+ var CONNECT_TIMEOUT_MS = 2e3;
2417
+ function isStringRecord(v) {
2418
+ return typeof v === "object" && v !== null && !Array.isArray(v);
2419
+ }
2420
+ function nowError(requestId, code, message) {
2421
+ return {
2422
+ request_id: requestId,
2423
+ ts: Date.now(),
2424
+ ok: false,
2425
+ error: { code, message }
2426
+ };
2427
+ }
2428
+ var connectionCounter = 0;
2429
+ function nextConnectionId() {
2430
+ connectionCounter = connectionCounter + 1 >>> 0;
2431
+ return `c${connectionCounter}-${process.pid}`;
2432
+ }
2433
+ async function startControlServer(opts) {
2434
+ const { socketPath, token, startedAt, handler } = opts;
2435
+ const logError = opts.logError ?? ((m) => process.stderr.write(m + "\n"));
2436
+ const transport = opts.transport ?? createUdsServerTransport({
2437
+ socketPath,
2438
+ logError
2439
+ });
2440
+ const listener = await transport.listen((connection) => {
2441
+ handleConnection(
2442
+ connection,
2443
+ token,
2444
+ startedAt,
2445
+ handler,
2446
+ logError,
2447
+ opts.onConnect,
2448
+ opts.onDisconnect
2449
+ );
2450
+ });
2451
+ return {
2452
+ close: () => listener.close()
2453
+ };
2454
+ }
2455
+ function handleConnection(connection, expectedToken, startedAt, handler, logError, onConnect, onDisconnect) {
2456
+ let authed = false;
2457
+ const connectTimer = setTimeout(() => {
2458
+ connection.close();
2459
+ }, CONNECT_TIMEOUT_MS);
2460
+ const ctx = {
2461
+ connectionId: nextConnectionId(),
2462
+ push: (env) => connection.send(env),
2463
+ disconnect: () => connection.close()
2464
+ };
2465
+ connection.onFrame((parsed) => {
2466
+ if (!isStringRecord(parsed)) {
2467
+ connection.close();
2468
+ return;
2469
+ }
2470
+ if (!authed) {
2471
+ if (parsed["kind"] !== "connect") {
2472
+ connection.close();
2473
+ return;
2474
+ }
2475
+ const tok = parsed["token"];
2476
+ if (typeof tok !== "string" || !timingSafeTokenEqual(tok, expectedToken)) {
2477
+ connection.send({
2478
+ ok: false,
2479
+ error: { code: "unauthorized", message: "invalid token" }
2480
+ });
2481
+ connection.close();
2482
+ return;
2483
+ }
2484
+ authed = true;
2485
+ clearTimeout(connectTimer);
2486
+ connection.send({
2487
+ ok: true,
2488
+ hello: { daemonPid: process.pid, startedAt }
2489
+ });
2490
+ onConnect?.(ctx);
2491
+ return;
2492
+ }
2493
+ const requestId = typeof parsed["request_id"] === "string" ? parsed["request_id"] : "";
2494
+ if (typeof parsed["kind"] !== "string" || typeof parsed["ts"] !== "number" || !("payload" in parsed) || requestId.length === 0) {
2495
+ connection.send(nowError(requestId, "bad_request", "malformed envelope"));
2496
+ return;
2497
+ }
2498
+ void Promise.resolve().then(() => handler(parsed, ctx)).then((res) => connection.send(res)).catch(
2499
+ (err) => connection.send(
2500
+ nowError(
2501
+ requestId,
2502
+ "handler_error",
2503
+ err instanceof Error ? err.message : String(err)
2504
+ )
2505
+ )
2506
+ );
2507
+ });
2508
+ connection.onError((err) => {
2509
+ const code = err.code;
2510
+ if (code !== "EPIPE" && code !== "ECONNRESET") {
2511
+ logError(`gateway: socket error: ${err.message}`);
2512
+ }
2513
+ });
2514
+ connection.onClose(() => {
2515
+ clearTimeout(connectTimer);
2516
+ if (authed) {
2517
+ onDisconnect?.(ctx);
2518
+ }
2519
+ });
2520
+ }
2521
+
2522
+ // src/gateway/router/sessionKey.ts
2523
+ function deriveSessionKey(loc) {
2524
+ const c = loc.channelId;
2525
+ const a = loc.accountId;
2526
+ if (loc.peer?.id) {
2527
+ const peer = loc.peer.id;
2528
+ if (loc.thread?.id) {
2529
+ return `peer:${c}:${a}:${peer}:${loc.thread.id}`;
2530
+ }
2531
+ return `peer:${c}:${a}:${peer}`;
2532
+ }
2533
+ if (loc.room?.id) {
2534
+ const room = loc.room.id;
2535
+ if (loc.thread?.id) {
2536
+ return `room:${c}:${a}:${room}:${loc.thread.id}`;
2537
+ }
2538
+ return `room:${c}:${a}:${room}`;
2539
+ }
2540
+ return `default:${c}:${a}`;
2541
+ }
2542
+
2543
+ // src/gateway/dispatcher.ts
2544
+ var Dispatcher = class {
2545
+ registry;
2546
+ pushDispatch;
2547
+ sendOutbound;
2548
+ resolveAgent;
2549
+ inboundQueue;
2550
+ canDispatch;
2551
+ log;
2552
+ constructor(opts) {
2553
+ this.registry = opts.registry;
2554
+ this.pushDispatch = opts.pushDispatch;
2555
+ this.sendOutbound = opts.sendOutbound;
2556
+ this.resolveAgent = opts.resolveAgent ?? ((input) => input.defaultAgentId);
2557
+ this.inboundQueue = opts.inboundQueue;
2558
+ this.canDispatch = opts.canDispatch ?? (() => true);
2559
+ this.log = opts.log;
2560
+ }
2561
+ handleInbound(inbound) {
2562
+ const current = this.registry.getCurrent();
2563
+ if (!current || !this.canDispatch()) {
2564
+ if (!this.inboundQueue) {
2565
+ this.log?.(
2566
+ "debug",
2567
+ `no runtime registered; dropping inbound ${inbound.idempotencyKey}`
2568
+ );
2569
+ return { kind: "dropped", reason: "no_runtime" };
2570
+ }
2571
+ const result = this.inboundQueue.enqueue(inbound);
2572
+ if (result.kind === "queued") {
2573
+ this.log?.(
2574
+ "info",
2575
+ `no runtime registered; parked inbound ${inbound.idempotencyKey} as queue#${result.id}`
2576
+ );
2577
+ return { kind: "queued", queueId: result.id };
2578
+ }
2579
+ if (result.kind === "duplicate") {
2580
+ this.log?.(
2581
+ "debug",
2582
+ `inbound ${inbound.idempotencyKey} already parked; ignoring duplicate`
2583
+ );
2584
+ return { kind: "dropped", reason: "no_runtime" };
2585
+ }
2586
+ this.log?.(
2587
+ "warn",
2588
+ `inbound queue full (>=${this.inboundQueue.size()}); dropping ${inbound.idempotencyKey}`
2589
+ );
2590
+ return { kind: "dropped", reason: "queue_full" };
2591
+ }
2592
+ const sessionKey = deriveSessionKey(inbound.location);
2593
+ const agentId = this.resolveAgent({
2594
+ sessionKey,
2595
+ channelId: inbound.location.channelId,
2596
+ defaultAgentId: current.defaultAgentId
2597
+ });
2598
+ if (!agentId) {
2599
+ this.log?.(
2600
+ "warn",
2601
+ `agent resolution returned empty for sessionKey=${sessionKey}`
2602
+ );
2603
+ return { kind: "dropped", reason: "no_default_agent" };
2604
+ }
2605
+ const entry = this.registry.beginDispatch({
2606
+ sessionKey,
2607
+ agentId,
2608
+ location: inbound.location
2609
+ });
2610
+ this.pushDispatch({
2611
+ dispatchId: entry.dispatchId,
2612
+ sessionKey,
2613
+ agentId,
2614
+ inbound
2615
+ });
2616
+ return {
2617
+ kind: "dispatched",
2618
+ dispatchId: entry.dispatchId,
2619
+ sessionKey
2620
+ };
2621
+ }
2622
+ /**
2623
+ * Drain parked inbound messages and dispatch them in FIFO order. Called by
2624
+ * the session.register handler after a runtime attaches. Safe to call when
2625
+ * no queue is configured (no-op).
2626
+ */
2627
+ drainPending() {
2628
+ if (!this.inboundQueue) return { dispatched: 0, dropped: 0 };
2629
+ const current = this.registry.getCurrent();
2630
+ if (!current || !this.canDispatch()) return { dispatched: 0, dropped: 0 };
2631
+ const parked = this.inboundQueue.drain();
2632
+ let dispatched = 0;
2633
+ let dropped = 0;
2634
+ for (const { inbound } of parked) {
2635
+ const sessionKey = deriveSessionKey(inbound.location);
2636
+ const agentId = this.resolveAgent({
2637
+ sessionKey,
2638
+ channelId: inbound.location.channelId,
2639
+ defaultAgentId: current.defaultAgentId
2640
+ });
2641
+ if (!agentId) {
2642
+ dropped += 1;
2643
+ this.log?.(
2644
+ "warn",
2645
+ `drainPending: no agent for ${sessionKey}; dropping ${inbound.idempotencyKey}`
2646
+ );
2647
+ continue;
2648
+ }
2649
+ const entry = this.registry.beginDispatch({
2650
+ sessionKey,
2651
+ agentId,
2652
+ location: inbound.location
2653
+ });
2654
+ this.pushDispatch({
2655
+ dispatchId: entry.dispatchId,
2656
+ sessionKey,
2657
+ agentId,
2658
+ inbound
2659
+ });
2660
+ dispatched += 1;
2661
+ }
2662
+ if (dispatched > 0 || dropped > 0) {
2663
+ this.log?.(
2664
+ "info",
2665
+ `drainPending: dispatched=${dispatched} dropped=${dropped}`
2666
+ );
2667
+ }
2668
+ return { dispatched, dropped };
2669
+ }
2670
+ async handleTurnComplete(payload) {
2671
+ const current = this.registry.getCurrent();
2672
+ if (!current || current.runtimeId !== payload.runtimeId) {
2673
+ throw new Error("runtime mismatch on session.turn.complete");
2674
+ }
2675
+ let entry;
2676
+ try {
2677
+ entry = this.registry.completeDispatch(payload.dispatchId);
2678
+ } catch (err) {
2679
+ if (err instanceof UnknownDispatchError) {
2680
+ return { delivered: false };
2681
+ }
2682
+ throw err;
2683
+ }
2684
+ const result = await this.sendOutbound(entry.location.channelId, {
2685
+ location: payload.location,
2686
+ text: payload.text,
2687
+ idempotencyKey: payload.idempotencyKey
2688
+ });
2689
+ return {
2690
+ delivered: true,
2691
+ providerMessageId: result.providerMessageId
2692
+ };
2693
+ }
2694
+ };
2695
+
2696
+ // src/gateway/lock.ts
2697
+ import fs2 from "fs";
2698
+ import path2 from "path";
2699
+ var GatewayAlreadyRunningError = class extends Error {
2700
+ otherPid;
2701
+ constructor(otherPid) {
2702
+ super(`gateway already running (pid=${otherPid})`);
2703
+ this.name = "GatewayAlreadyRunningError";
2704
+ this.otherPid = otherPid;
2705
+ }
2706
+ };
2707
+ function isProcessAlive(pid) {
2708
+ try {
2709
+ process.kill(pid, 0);
2710
+ return true;
2711
+ } catch (err) {
2712
+ const code = err.code;
2713
+ return code === "EPERM";
2714
+ }
2715
+ }
2716
+ function readPidFile(p) {
2717
+ try {
2718
+ const text = fs2.readFileSync(p, "utf-8").trim();
2719
+ const pid = Number.parseInt(text, 10);
2720
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
2721
+ } catch {
2722
+ return null;
2723
+ }
2724
+ }
2725
+ function acquireLock(lockPath) {
2726
+ fs2.mkdirSync(path2.dirname(lockPath), { recursive: true, mode: 448 });
2727
+ for (let attempt = 0; attempt < 2; attempt++) {
2728
+ try {
2729
+ const fd = fs2.openSync(lockPath, "wx", 384);
2730
+ fs2.writeSync(fd, String(process.pid) + "\n");
2731
+ fs2.closeSync(fd);
2732
+ return {
2733
+ path: lockPath,
2734
+ pid: process.pid,
2735
+ release: () => {
2736
+ try {
2737
+ const pidNow = readPidFile(lockPath);
2738
+ if (pidNow === process.pid) {
2739
+ fs2.unlinkSync(lockPath);
2740
+ }
2741
+ } catch {
2742
+ }
2743
+ }
2744
+ };
2745
+ } catch (err) {
2746
+ const code = err.code;
2747
+ if (code !== "EEXIST") throw err;
2748
+ const otherPid = readPidFile(lockPath);
2749
+ if (otherPid !== null && isProcessAlive(otherPid)) {
2750
+ throw new GatewayAlreadyRunningError(otherPid);
2751
+ }
2752
+ try {
2753
+ fs2.unlinkSync(lockPath);
2754
+ } catch {
2755
+ }
2756
+ }
2757
+ }
2758
+ throw new Error(`failed to acquire gateway lock at ${lockPath}`);
2759
+ }
2760
+
2761
+ // src/gateway/outboundDispatcher.ts
2762
+ var DEFAULT_BACKOFF = [
2763
+ 1e3,
2764
+ // 1s
2765
+ 2e3,
2766
+ // 2s
2767
+ 4e3,
2768
+ // 4s
2769
+ 8e3,
2770
+ // 8s
2771
+ 16e3,
2772
+ // 16s
2773
+ 3e4
2774
+ // 30s
2775
+ ];
2776
+ var DEFAULT_MAX_ATTEMPTS = 10;
2777
+ var DEFAULT_TICK_MS = 1e3;
2778
+ var DEFAULT_BATCH = 16;
2779
+ var OutboundDispatcher = class {
2780
+ outbox;
2781
+ send;
2782
+ backoff;
2783
+ maxAttempts;
2784
+ tickMs;
2785
+ batchSize;
2786
+ now;
2787
+ log;
2788
+ timer = null;
2789
+ draining = false;
2790
+ stopped = false;
2791
+ constructor(opts) {
2792
+ this.outbox = opts.outbox;
2793
+ this.send = opts.send;
2794
+ this.backoff = opts.backoffSchedule ?? DEFAULT_BACKOFF;
2795
+ this.maxAttempts = opts.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
2796
+ this.tickMs = opts.tickIntervalMs ?? DEFAULT_TICK_MS;
2797
+ this.batchSize = opts.drainBatchSize ?? DEFAULT_BATCH;
2798
+ this.now = opts.now ?? Date.now;
2799
+ this.log = opts.log;
2800
+ }
2801
+ start() {
2802
+ if (this.timer) return;
2803
+ this.timer = setInterval(() => {
2804
+ void this.drain();
2805
+ }, this.tickMs);
2806
+ this.timer.unref();
2807
+ }
2808
+ stop() {
2809
+ this.stopped = true;
2810
+ if (this.timer) {
2811
+ clearInterval(this.timer);
2812
+ this.timer = null;
2813
+ }
2814
+ }
2815
+ async dispatch(channelId, msg) {
2816
+ try {
2817
+ const result = await this.send(channelId, msg);
2818
+ return { kind: "sent", result };
2819
+ } catch (err) {
2820
+ const error2 = err instanceof Error ? err.message : String(err);
2821
+ const nextAttemptAt = this.now() + this.backoffFor(0);
2822
+ const id = this.outbox.enqueue({
2823
+ channelId,
2824
+ message: msg,
2825
+ nextAttemptAt,
2826
+ lastError: error2
2827
+ });
2828
+ this.log?.(
2829
+ "warn",
2830
+ `send to ${channelId} failed; parked as outbox#${id}: ${error2}`
2831
+ );
2832
+ return { kind: "queued", outboxId: id, error: error2 };
2833
+ }
2834
+ }
2835
+ /**
2836
+ * Drain due entries. Exposed for tests; the timer also calls this. Safe
2837
+ * to call concurrently — a re-entry guard short-circuits.
2838
+ */
2839
+ async drain() {
2840
+ if (this.draining || this.stopped) {
2841
+ return { retried: 0, succeeded: 0, dropped: 0 };
2842
+ }
2843
+ this.draining = true;
2844
+ let retried = 0;
2845
+ let succeeded = 0;
2846
+ let dropped = 0;
2847
+ try {
2848
+ const due = this.outbox.peekDue(this.now(), this.batchSize);
2849
+ for (const row of due) {
2850
+ retried += 1;
2851
+ const outcome = await this.attempt(row);
2852
+ if (outcome === "succeeded") succeeded += 1;
2853
+ else if (outcome === "dropped") dropped += 1;
2854
+ }
2855
+ } finally {
2856
+ this.draining = false;
2857
+ }
2858
+ return { retried, succeeded, dropped };
2859
+ }
2860
+ async attempt(row) {
2861
+ try {
2862
+ await this.send(row.channelId, row.message);
2863
+ this.outbox.delete(row.id);
2864
+ this.log?.(
2865
+ "info",
2866
+ `outbox#${row.id} delivered to ${row.channelId} on attempt ${row.attempt + 1}`
2867
+ );
2868
+ return "succeeded";
2869
+ } catch (err) {
2870
+ const error2 = err instanceof Error ? err.message : String(err);
2871
+ const nextAttempt = row.attempt + 1;
2872
+ if (nextAttempt >= this.maxAttempts) {
2873
+ this.outbox.delete(row.id);
2874
+ this.log?.(
2875
+ "error",
2876
+ `outbox#${row.id} dropped after ${nextAttempt} attempts: ${error2}`
2877
+ );
2878
+ return "dropped";
2879
+ }
2880
+ const nextAttemptAt = this.now() + this.backoffFor(nextAttempt);
2881
+ this.outbox.recordFailure({
2882
+ id: row.id,
2883
+ nextAttemptAt,
2884
+ lastError: error2
2885
+ });
2886
+ return "requeued";
2887
+ }
2888
+ }
2889
+ backoffFor(attempt) {
2890
+ if (this.backoff.length === 0) return 1e3;
2891
+ const idx = Math.min(attempt, this.backoff.length - 1);
2892
+ return this.backoff[idx];
2893
+ }
2894
+ };
2895
+
2896
+ // src/gateway/relay/coordinator.ts
2897
+ var DEFAULT_RELAY_TTL_MS = 5 * 6e4;
2898
+ var RelayCoordinator = class {
2899
+ adapters;
2900
+ defaultTtlMs;
2901
+ idFactory;
2902
+ log;
2903
+ pending = /* @__PURE__ */ new Map();
2904
+ constructor(opts) {
2905
+ this.adapters = opts.adapters;
2906
+ this.defaultTtlMs = opts.defaultTtlMs ?? DEFAULT_RELAY_TTL_MS;
2907
+ this.idFactory = opts.idFactory ?? generateChannelRequestId;
2908
+ this.log = opts.log;
2909
+ }
2910
+ requestPermission(req) {
2911
+ const channelRequestId = req.channelRequestId ?? this.idFactory();
2912
+ const ttlMs = req.ttlMs ?? this.defaultTtlMs;
2913
+ const targets = this.adapters().filter(
2914
+ (a) => a.capabilities.relayPermission && typeof a.requestPermissionVerdict === "function"
2915
+ );
2916
+ if (targets.length === 0) {
2917
+ return {
2918
+ channelRequestId,
2919
+ result: Promise.resolve({ kind: "no_relay" })
2920
+ };
2921
+ }
2922
+ const fingerprint = permissionFingerprint(req);
2923
+ const existing = this.pending.get(channelRequestId);
2924
+ if (existing) {
2925
+ if (existing.kind !== "permission") {
2926
+ throw new Error(
2927
+ `channel_request_id_collision: ${channelRequestId} is bound to a question relay`
2928
+ );
2929
+ }
2930
+ if (existing.fingerprint !== fingerprint) {
2931
+ throw new Error(
2932
+ `channel_request_id_collision: ${channelRequestId} payload mismatch`
2933
+ );
2934
+ }
2935
+ if (existing.runtimeId !== req.runtimeId) {
2936
+ throw new Error(
2937
+ `channel_request_owner_mismatch: ${channelRequestId} owned by a different runtime`
2938
+ );
2939
+ }
2940
+ return { channelRequestId, result: existing.result };
2941
+ }
2942
+ const controllers = targets.map(() => new AbortController());
2943
+ let resolveFn;
2944
+ const result = new Promise((resolve) => {
2945
+ resolveFn = resolve;
2946
+ });
2947
+ const timer = setTimeout(() => {
2948
+ this.settlePermission(channelRequestId, {
2949
+ kind: "cancelled",
2950
+ reason: "timeout"
2951
+ });
2952
+ }, ttlMs);
2953
+ if (typeof timer.unref === "function") timer.unref();
2954
+ const entry = {
2955
+ kind: "permission",
2956
+ channelRequestId,
2957
+ fingerprint,
2958
+ ...req.runtimeId !== void 0 ? { runtimeId: req.runtimeId } : {},
2959
+ controllers,
2960
+ timer,
2961
+ resolve: resolveFn,
2962
+ result,
2963
+ settled: false
2964
+ };
2965
+ this.pending.set(channelRequestId, entry);
2966
+ const fullReq = {
2967
+ channelRequestId,
2968
+ toolName: req.toolName,
2969
+ description: req.description,
2970
+ inputPreview: req.inputPreview
2971
+ };
2972
+ targets.forEach((adapter, idx) => {
2973
+ const ctrl = controllers[idx];
2974
+ Promise.resolve().then(() => adapter.requestPermissionVerdict(fullReq, ctrl.signal)).then((res) => {
2975
+ if (res.kind === "verdict") {
2976
+ this.settlePermission(channelRequestId, {
2977
+ ...res,
2978
+ channelId: adapter.id
2979
+ });
2980
+ }
2981
+ }).catch((err) => {
2982
+ this.log?.(
2983
+ "warn",
2984
+ `adapter ${adapter.id} permission relay failed: ${err instanceof Error ? err.message : String(err)}`
2985
+ );
2986
+ });
2987
+ });
2988
+ return { channelRequestId, result };
2989
+ }
2990
+ requestQuestion(req) {
2991
+ const channelRequestId = req.channelRequestId ?? this.idFactory();
2992
+ const ttlMs = req.ttlMs ?? this.defaultTtlMs;
2993
+ const targets = this.adapters().filter(
2994
+ (a) => a.capabilities.relayQuestion && typeof a.requestQuestionAnswer === "function"
2995
+ );
2996
+ if (targets.length === 0) {
2997
+ return {
2998
+ channelRequestId,
2999
+ result: Promise.resolve({ kind: "no_relay" })
3000
+ };
3001
+ }
3002
+ const fingerprint = questionFingerprint(req);
3003
+ const existing = this.pending.get(channelRequestId);
3004
+ if (existing) {
3005
+ if (existing.kind !== "question") {
3006
+ throw new Error(
3007
+ `channel_request_id_collision: ${channelRequestId} is bound to a permission relay`
3008
+ );
3009
+ }
3010
+ if (existing.fingerprint !== fingerprint) {
3011
+ throw new Error(
3012
+ `channel_request_id_collision: ${channelRequestId} payload mismatch`
3013
+ );
3014
+ }
3015
+ if (existing.runtimeId !== req.runtimeId) {
3016
+ throw new Error(
3017
+ `channel_request_owner_mismatch: ${channelRequestId} owned by a different runtime`
3018
+ );
3019
+ }
3020
+ return { channelRequestId, result: existing.result };
3021
+ }
3022
+ const controllers = targets.map(() => new AbortController());
3023
+ let resolveFn;
3024
+ const result = new Promise((resolve) => {
3025
+ resolveFn = resolve;
3026
+ });
3027
+ const timer = setTimeout(() => {
3028
+ this.settleQuestion(channelRequestId, {
3029
+ kind: "cancelled",
3030
+ reason: "timeout"
3031
+ });
3032
+ }, ttlMs);
3033
+ if (typeof timer.unref === "function") timer.unref();
3034
+ const entry = {
3035
+ kind: "question",
3036
+ channelRequestId,
3037
+ fingerprint,
3038
+ ...req.runtimeId !== void 0 ? { runtimeId: req.runtimeId } : {},
3039
+ controllers,
3040
+ timer,
3041
+ resolve: resolveFn,
3042
+ result,
3043
+ settled: false
3044
+ };
3045
+ this.pending.set(channelRequestId, entry);
3046
+ const fullReq = {
3047
+ channelRequestId,
3048
+ title: req.title,
3049
+ questions: req.questions
3050
+ };
3051
+ targets.forEach((adapter, idx) => {
3052
+ const ctrl = controllers[idx];
3053
+ Promise.resolve().then(() => adapter.requestQuestionAnswer(fullReq, ctrl.signal)).then((res) => {
3054
+ if (res.kind === "answer") {
3055
+ this.settleQuestion(channelRequestId, {
3056
+ ...res,
3057
+ channelId: adapter.id
3058
+ });
3059
+ }
3060
+ }).catch((err) => {
3061
+ this.log?.(
3062
+ "warn",
3063
+ `adapter ${adapter.id} question relay failed: ${err instanceof Error ? err.message : String(err)}`
3064
+ );
3065
+ });
3066
+ });
3067
+ return { channelRequestId, result };
3068
+ }
3069
+ cancel(channelRequestId, reason, expectedRuntimeId) {
3070
+ const entry = this.pending.get(channelRequestId);
3071
+ if (!entry) return false;
3072
+ if (expectedRuntimeId !== void 0 && entry.runtimeId !== void 0 && entry.runtimeId !== expectedRuntimeId) {
3073
+ return false;
3074
+ }
3075
+ if (entry.kind === "permission") {
3076
+ this.settlePermission(channelRequestId, { kind: "cancelled", reason });
3077
+ } else {
3078
+ this.settleQuestion(channelRequestId, { kind: "cancelled", reason });
3079
+ }
3080
+ return true;
3081
+ }
3082
+ pendingCount() {
3083
+ return this.pending.size;
3084
+ }
3085
+ disposeAll(reason = "auto_resolved") {
3086
+ for (const id of [...this.pending.keys()]) {
3087
+ this.cancel(id, reason);
3088
+ }
3089
+ }
3090
+ settlePermission(channelRequestId, result) {
3091
+ const entry = this.pending.get(channelRequestId);
3092
+ if (!entry || entry.kind !== "permission" || entry.settled) return;
3093
+ entry.settled = true;
3094
+ this.pending.delete(channelRequestId);
3095
+ clearTimeout(entry.timer);
3096
+ for (const ctrl of entry.controllers) {
3097
+ if (!ctrl.signal.aborted) ctrl.abort();
3098
+ }
3099
+ entry.resolve(result);
3100
+ }
3101
+ settleQuestion(channelRequestId, result) {
3102
+ const entry = this.pending.get(channelRequestId);
3103
+ if (!entry || entry.kind !== "question" || entry.settled) return;
3104
+ entry.settled = true;
3105
+ this.pending.delete(channelRequestId);
3106
+ clearTimeout(entry.timer);
3107
+ for (const ctrl of entry.controllers) {
3108
+ if (!ctrl.signal.aborted) ctrl.abort();
3109
+ }
3110
+ entry.resolve(result);
3111
+ }
3112
+ };
3113
+ function permissionFingerprint(req) {
3114
+ return JSON.stringify([req.toolName, req.description, req.inputPreview]);
3115
+ }
3116
+ function questionFingerprint(req) {
3117
+ return JSON.stringify([req.title, req.questions]);
3118
+ }
3119
+
3120
+ // src/gateway/state/db.ts
3121
+ import fs3 from "fs";
3122
+ import path3 from "path";
3123
+ import Database from "better-sqlite3";
3124
+ var GATEWAY_STATE_VERSION = 1;
3125
+ function openGatewayState(dbPath) {
3126
+ if (dbPath !== ":memory:") {
3127
+ fs3.mkdirSync(path3.dirname(dbPath), { recursive: true, mode: 448 });
3128
+ }
3129
+ const db = new Database(dbPath);
3130
+ db.exec("PRAGMA journal_mode = WAL");
3131
+ db.exec("PRAGMA foreign_keys = ON");
3132
+ initGatewayStateSchema(db);
3133
+ if (dbPath !== ":memory:" && process.platform !== "win32") {
3134
+ try {
3135
+ fs3.chmodSync(dbPath, 384);
3136
+ } catch {
3137
+ }
3138
+ }
3139
+ return db;
3140
+ }
3141
+ function initGatewayStateSchema(db) {
3142
+ db.exec(`
3143
+ CREATE TABLE IF NOT EXISTS schema_version (
3144
+ version INTEGER NOT NULL
3145
+ );
3146
+
3147
+ -- Inbound chat messages parked while no runtime is registered. Drained
3148
+ -- in FIFO id order on session.register. Idempotency key prevents the
3149
+ -- same provider message from being parked twice if an adapter retries.
3150
+ CREATE TABLE IF NOT EXISTS inbound_queue (
3151
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
3152
+ channel_id TEXT NOT NULL,
3153
+ account_id TEXT NOT NULL,
3154
+ idempotency_key TEXT NOT NULL,
3155
+ payload_json TEXT NOT NULL,
3156
+ created_at INTEGER NOT NULL
3157
+ );
3158
+ CREATE UNIQUE INDEX IF NOT EXISTS ix_inbound_queue_idem
3159
+ ON inbound_queue(channel_id, account_id, idempotency_key);
3160
+
3161
+ -- Outbound messages whose adapter send() failed transiently. Drained
3162
+ -- by a periodic retry loop with exponential backoff. Idempotency key
3163
+ -- on the OutboundMessage prevents double-delivery if the adapter
3164
+ -- partially succeeded before throwing.
3165
+ CREATE TABLE IF NOT EXISTS channel_outbox (
3166
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
3167
+ channel_id TEXT NOT NULL,
3168
+ payload_json TEXT NOT NULL,
3169
+ attempt INTEGER NOT NULL DEFAULT 0,
3170
+ next_attempt_at INTEGER NOT NULL,
3171
+ last_error TEXT,
3172
+ created_at INTEGER NOT NULL
3173
+ );
3174
+ CREATE INDEX IF NOT EXISTS ix_channel_outbox_due
3175
+ ON channel_outbox(next_attempt_at);
3176
+ `);
3177
+ const existing = db.prepare("SELECT version FROM schema_version").get();
3178
+ if (existing && existing.version > GATEWAY_STATE_VERSION) {
3179
+ throw new Error(
3180
+ `Gateway state DB has newer schema version ${existing.version} (expected <= ${GATEWAY_STATE_VERSION}). Update athena-cli.`
3181
+ );
3182
+ }
3183
+ if (!existing) {
3184
+ db.prepare("INSERT INTO schema_version (version) VALUES (?)").run(
3185
+ GATEWAY_STATE_VERSION
3186
+ );
3187
+ }
3188
+ }
3189
+
3190
+ // src/gateway/state/inboundQueue.ts
3191
+ var DEFAULT_MAX_ENTRIES = 1e3;
3192
+ var InboundQueue = class {
3193
+ db;
3194
+ maxEntries;
3195
+ constructor(db, opts = {}) {
3196
+ this.db = db;
3197
+ this.maxEntries = opts.maxEntries ?? DEFAULT_MAX_ENTRIES;
3198
+ }
3199
+ size() {
3200
+ const row = this.db.prepare("SELECT COUNT(*) as n FROM inbound_queue").get();
3201
+ return row.n;
3202
+ }
3203
+ enqueue(inbound) {
3204
+ if (this.size() >= this.maxEntries) {
3205
+ return { kind: "rejected", reason: "queue_full" };
3206
+ }
3207
+ const stmt = this.db.prepare(
3208
+ `INSERT INTO inbound_queue
3209
+ (channel_id, account_id, idempotency_key, payload_json, created_at)
3210
+ VALUES (?, ?, ?, ?, ?)
3211
+ ON CONFLICT(channel_id, account_id, idempotency_key) DO NOTHING`
3212
+ );
3213
+ const result = stmt.run(
3214
+ inbound.location.channelId,
3215
+ inbound.location.accountId,
3216
+ inbound.idempotencyKey,
3217
+ JSON.stringify(inbound),
3218
+ Date.now()
3219
+ );
3220
+ if (result.changes === 0) {
3221
+ return { kind: "duplicate" };
3222
+ }
3223
+ return { kind: "queued", id: Number(result.lastInsertRowid) };
3224
+ }
3225
+ /** Atomically read and remove all parked entries in FIFO order. */
3226
+ drain() {
3227
+ return this.db.transaction(() => {
3228
+ const rows = this.db.prepare("SELECT id, payload_json FROM inbound_queue ORDER BY id ASC").all();
3229
+ if (rows.length > 0) {
3230
+ this.db.prepare("DELETE FROM inbound_queue").run();
3231
+ }
3232
+ return rows.map((r) => ({
3233
+ id: r.id,
3234
+ inbound: JSON.parse(r.payload_json)
3235
+ }));
3236
+ })();
3237
+ }
3238
+ };
3239
+
3240
+ // src/gateway/state/outbox.ts
3241
+ var Outbox = class {
3242
+ db;
3243
+ constructor(db) {
3244
+ this.db = db;
3245
+ }
3246
+ size() {
3247
+ const row = this.db.prepare("SELECT COUNT(*) as n FROM channel_outbox").get();
3248
+ return row.n;
3249
+ }
3250
+ enqueue(input) {
3251
+ const result = this.db.prepare(
3252
+ `INSERT INTO channel_outbox
3253
+ (channel_id, payload_json, attempt, next_attempt_at, last_error, created_at)
3254
+ VALUES (?, ?, 0, ?, ?, ?)`
3255
+ ).run(
3256
+ input.channelId,
3257
+ JSON.stringify(input.message),
3258
+ input.nextAttemptAt,
3259
+ input.lastError ?? null,
3260
+ Date.now()
3261
+ );
3262
+ return Number(result.lastInsertRowid);
3263
+ }
3264
+ /** Rows whose `next_attempt_at` is at or before `now`, oldest first. */
3265
+ peekDue(now, limit) {
3266
+ const rows = this.db.prepare(
3267
+ `SELECT id, channel_id, payload_json, attempt, next_attempt_at, last_error
3268
+ FROM channel_outbox
3269
+ WHERE next_attempt_at <= ?
3270
+ ORDER BY next_attempt_at ASC, id ASC
3271
+ LIMIT ?`
3272
+ ).all(now, limit);
3273
+ return rows.map((r) => ({
3274
+ id: r.id,
3275
+ channelId: r.channel_id,
3276
+ message: JSON.parse(r.payload_json),
3277
+ attempt: r.attempt,
3278
+ nextAttemptAt: r.next_attempt_at,
3279
+ lastError: r.last_error
3280
+ }));
3281
+ }
3282
+ delete(id) {
3283
+ this.db.prepare("DELETE FROM channel_outbox WHERE id = ?").run(id);
3284
+ }
3285
+ recordFailure(input) {
3286
+ this.db.prepare(
3287
+ `UPDATE channel_outbox
3288
+ SET attempt = attempt + 1,
3289
+ next_attempt_at = ?,
3290
+ last_error = ?
3291
+ WHERE id = ?`
3292
+ ).run(input.nextAttemptAt, input.lastError, input.id);
3293
+ }
3294
+ };
3295
+
3296
+ // src/gateway/transport/tlsWs.ts
3297
+ import { WebSocketServer } from "ws";
3298
+ import { createServer as createHttpsServer } from "https";
3299
+ import { readFileSync as readFileSync3 } from "fs";
3300
+ function createWsServerTransport(opts) {
3301
+ if (!opts.allowNonLoopback && !isLoopbackHost(opts.host)) {
3302
+ throw new Error(`gateway: refusing non-loopback bind without --insecure`);
3303
+ }
3304
+ let endpoint = null;
3305
+ const scheme = opts.tls ? "wss" : "ws";
3306
+ const rateLimit = createConnectRateLimiter(opts.rateLimitPerMin ?? 10);
3307
+ return {
3308
+ kind: "ws",
3309
+ endpoint: () => {
3310
+ if (!endpoint) {
3311
+ throw new Error("gateway: WS transport has not started listening");
3312
+ }
3313
+ return endpoint;
3314
+ },
3315
+ listen: (onConnection) => new Promise((resolve, reject) => {
3316
+ const verifyClient = (info) => rateLimit.allow(info.req.socket.remoteAddress ?? "unknown");
3317
+ const { wss, httpsServer } = createWss({
3318
+ host: opts.host,
3319
+ port: opts.port,
3320
+ tls: opts.tls,
3321
+ verifyClient
3322
+ });
3323
+ const onError = (err) => reject(err);
3324
+ wss.once("error", onError);
3325
+ if (httpsServer) httpsServer.once("error", onError);
3326
+ const onListening = () => {
3327
+ wss.off("error", onError);
3328
+ if (httpsServer) httpsServer.off("error", onError);
3329
+ const addr = httpsServer ? httpsServer.address() : wss.address();
3330
+ if (typeof addr === "string" || addr === null) {
3331
+ wss.close();
3332
+ httpsServer?.close();
3333
+ reject(
3334
+ new Error("gateway: WS listener did not expose TCP address")
3335
+ );
3336
+ return;
3337
+ }
3338
+ endpoint = {
3339
+ host: opts.host,
3340
+ port: addr.port,
3341
+ url: `${scheme}://${opts.host}:${addr.port}`
3342
+ };
3343
+ resolve({
3344
+ close: () => new Promise((closeResolve) => {
3345
+ for (const client of wss.clients) client.terminate();
3346
+ wss.close(() => {
3347
+ if (httpsServer) httpsServer.close(() => closeResolve());
3348
+ else closeResolve();
3349
+ });
3350
+ })
3351
+ });
3352
+ };
3353
+ if (httpsServer) httpsServer.once("listening", onListening);
3354
+ else wss.once("listening", onListening);
3355
+ const pingIntervalMs = opts.pingIntervalMs ?? 15e3;
3356
+ const pongTimeoutMs = opts.pongTimeoutMs ?? 3e4;
3357
+ wss.on("connection", (ws) => {
3358
+ attachHeartbeat(ws, pingIntervalMs, pongTimeoutMs);
3359
+ onConnection(createWsConnection(ws, `${scheme}:${opts.host}`));
3360
+ });
3361
+ })
3362
+ };
3363
+ }
3364
+ function loadTlsOptions(tls) {
3365
+ return {
3366
+ cert: readFileSync3(tls.certPath),
3367
+ key: readFileSync3(tls.keyPath)
3368
+ };
3369
+ }
3370
+ function createWss(input) {
3371
+ if (input.tls) {
3372
+ const httpsServer = createHttpsServer(loadTlsOptions(input.tls));
3373
+ httpsServer.listen({ host: input.host, port: input.port });
3374
+ return {
3375
+ wss: new WebSocketServer({
3376
+ server: httpsServer,
3377
+ verifyClient: input.verifyClient
3378
+ }),
3379
+ httpsServer
3380
+ };
3381
+ }
3382
+ return {
3383
+ wss: new WebSocketServer({
3384
+ host: input.host,
3385
+ port: input.port,
3386
+ verifyClient: input.verifyClient
3387
+ }),
3388
+ httpsServer: null
3389
+ };
3390
+ }
3391
+ function createConnectRateLimiter(maxPerMin) {
3392
+ if (maxPerMin <= 0) return { allow: () => true };
3393
+ const buckets = /* @__PURE__ */ new Map();
3394
+ let pruneCountdown = 256;
3395
+ const prune = (cutoff) => {
3396
+ for (const [ip, arr] of buckets) {
3397
+ const fresh = arr.filter((t) => t > cutoff);
3398
+ if (fresh.length === 0) buckets.delete(ip);
3399
+ else if (fresh.length !== arr.length) buckets.set(ip, fresh);
3400
+ }
3401
+ };
3402
+ return {
3403
+ allow(ip) {
3404
+ const now = Date.now();
3405
+ const cutoff = now - 6e4;
3406
+ pruneCountdown -= 1;
3407
+ if (pruneCountdown <= 0) {
3408
+ prune(cutoff);
3409
+ pruneCountdown = 256;
3410
+ }
3411
+ const recent = (buckets.get(ip) ?? []).filter((t) => t > cutoff);
3412
+ if (recent.length >= maxPerMin) {
3413
+ buckets.set(ip, recent);
3414
+ return false;
3415
+ }
3416
+ recent.push(now);
3417
+ buckets.set(ip, recent);
3418
+ return true;
3419
+ }
3420
+ };
3421
+ }
3422
+ function attachHeartbeat(ws, pingIntervalMs, pongTimeoutMs) {
3423
+ if (pingIntervalMs <= 0) return;
3424
+ let pongTimer = null;
3425
+ const clearPongTimer = () => {
3426
+ if (pongTimer) {
3427
+ clearTimeout(pongTimer);
3428
+ pongTimer = null;
3429
+ }
3430
+ };
3431
+ ws.on("pong", clearPongTimer);
3432
+ const interval = setInterval(() => {
3433
+ if (ws.readyState !== ws.OPEN) return;
3434
+ try {
3435
+ ws.ping();
3436
+ } catch {
3437
+ return;
3438
+ }
3439
+ if (!pongTimer) {
3440
+ pongTimer = setTimeout(() => ws.terminate(), pongTimeoutMs);
3441
+ }
3442
+ }, pingIntervalMs);
3443
+ const stop = () => {
3444
+ clearInterval(interval);
3445
+ clearPongTimer();
3446
+ };
3447
+ ws.on("close", stop);
3448
+ ws.on("error", stop);
3449
+ }
3450
+ function createWsConnection(ws, peer) {
3451
+ const frameHandlers = /* @__PURE__ */ new Set();
3452
+ const closeHandlers = /* @__PURE__ */ new Set();
3453
+ const errorHandlers = /* @__PURE__ */ new Set();
3454
+ ws.on("message", (data) => {
3455
+ let parsed;
3456
+ try {
3457
+ parsed = JSON.parse(data.toString());
3458
+ } catch {
3459
+ ws.close();
3460
+ return;
3461
+ }
3462
+ traceGatewayFrame("ws", peer, "in", parsed);
3463
+ for (const handler of frameHandlers) handler(parsed);
3464
+ });
3465
+ ws.on("error", (err) => {
3466
+ for (const handler of errorHandlers) handler(err);
3467
+ });
3468
+ ws.on("close", () => {
3469
+ for (const handler of closeHandlers) handler();
3470
+ });
3471
+ return {
3472
+ kind: "ws",
3473
+ peer,
3474
+ send: (frame) => {
3475
+ if (ws.readyState !== ws.OPEN) return;
3476
+ traceGatewayFrame("ws", peer, "out", frame);
3477
+ ws.send(JSON.stringify(frame));
3478
+ },
3479
+ close: () => ws.close(),
3480
+ onFrame: (cb) => {
3481
+ frameHandlers.add(cb);
3482
+ return () => frameHandlers.delete(cb);
3483
+ },
3484
+ onClose: (cb) => {
3485
+ closeHandlers.add(cb);
3486
+ return () => closeHandlers.delete(cb);
3487
+ },
3488
+ onError: (cb) => {
3489
+ errorHandlers.add(cb);
3490
+ return () => errorHandlers.delete(cb);
3491
+ }
3492
+ };
3493
+ }
3494
+
3495
+ // src/gateway/daemon.ts
3496
+ function buildListenerStatus(spec, resolvedPort) {
3497
+ if (spec.kind === "uds") {
3498
+ return { kind: "uds", socketPath: spec.socketPath };
3499
+ }
3500
+ const port = resolvedPort ?? spec.port;
3501
+ const tls = Boolean(spec.tls);
3502
+ const scheme = tls ? "wss" : "ws";
3503
+ return {
3504
+ kind: "tcp",
3505
+ host: spec.host,
3506
+ port,
3507
+ url: `${scheme}://${spec.host}:${port}`,
3508
+ tls,
3509
+ insecure: spec.insecure,
3510
+ loopback: isLoopbackHost(spec.host)
3511
+ };
3512
+ }
3513
+ function pathIdFromSidecarPath(filePath) {
3514
+ const base = filePath.split(/[\\/]/).pop() ?? filePath;
3515
+ return base.endsWith(".json") ? base.slice(0, -".json".length) : base;
3516
+ }
3517
+ async function startDaemon(opts) {
3518
+ const startedAt = Date.now();
3519
+ const pid = process.pid;
3520
+ const paths = opts.paths ?? resolveGatewayPaths(opts.env);
3521
+ fs4.mkdirSync(paths.runDir, { recursive: true, mode: 448 });
3522
+ fs4.mkdirSync(paths.configDir, { recursive: true, mode: 448 });
3523
+ if (process.platform !== "win32") {
3524
+ try {
3525
+ fs4.chmodSync(paths.runDir, 448);
3526
+ fs4.chmodSync(paths.configDir, 448);
3527
+ } catch {
3528
+ }
3529
+ }
3530
+ const lock = acquireLock(paths.lockPath);
3531
+ const token = loadOrCreateToken(paths.tokenPath);
3532
+ const listenSpec = opts.listenSpec ?? resolveListenSpec({ paths });
3533
+ requireTokenForBind(listenSpec, token);
3534
+ const listenerHints = {
3535
+ transport: listenSpec.kind === "tcp" ? "ws" : "uds",
3536
+ tls: listenSpec.kind === "tcp" && Boolean(listenSpec.tls),
3537
+ loopback: listenSpec.kind === "uds" || isLoopbackHost(listenSpec.host)
3538
+ };
3539
+ const stateDb = openGatewayState(paths.statePath);
3540
+ const inboundQueue = new InboundQueue(stateDb);
3541
+ const outbox = new Outbox(stateDb);
3542
+ const registry = new SessionRegistry();
3543
+ const channelManager = new ChannelManager();
3544
+ const relayCoordinator = new RelayCoordinator({
3545
+ adapters: () => channelManager.listAdapters()
3546
+ });
3547
+ const runtimeConnections = /* @__PURE__ */ new Map();
3548
+ const staleRuntimeTimers = /* @__PURE__ */ new Map();
3549
+ const connectionOpenedAt = /* @__PURE__ */ new Map();
3550
+ const disconnectGracePeriodMs = opts.disconnectGracePeriodMs ?? 0;
3551
+ let listenerStatus = null;
3552
+ const pushDispatch = (payload) => {
3553
+ const current = registry.getCurrent();
3554
+ if (!current) return;
3555
+ const ctx = runtimeConnections.get(current.runtimeId);
3556
+ if (!ctx) return;
3557
+ ctx.push({
3558
+ push_id: crypto.randomUUID(),
3559
+ ts: Date.now(),
3560
+ kind: "session.dispatch.turn",
3561
+ payload
3562
+ });
3563
+ };
3564
+ const log = (level, message) => {
3565
+ if (opts.silent) return;
3566
+ const stream = level === "error" || level === "warn" ? "stderr" : "stdout";
3567
+ process[stream].write(`athena-gateway: [${level}] ${message}
3568
+ `);
3569
+ };
3570
+ const outboundDispatcher = new OutboundDispatcher({
3571
+ outbox,
3572
+ send: (channelId, msg) => channelManager.send(channelId, msg),
3573
+ log
3574
+ });
3575
+ outboundDispatcher.start();
3576
+ const dispatcher = new Dispatcher({
3577
+ registry,
3578
+ pushDispatch,
3579
+ canDispatch: () => {
3580
+ const current = registry.getCurrent();
3581
+ return current ? registry.hasActiveBinding(current.runtimeId) : false;
3582
+ },
3583
+ sendOutbound: async (channelId, msg) => {
3584
+ const result = await outboundDispatcher.dispatch(channelId, msg);
3585
+ if (result.kind === "sent") return result.result;
3586
+ return {
3587
+ providerMessageId: `outbox:${result.outboxId}`,
3588
+ deliveredAt: Date.now()
3589
+ };
3590
+ },
3591
+ inboundQueue,
3592
+ log
3593
+ });
3594
+ channelManager.setInboundSink((inbound) => {
3595
+ dispatcher.handleInbound(inbound);
3596
+ });
3597
+ const channelConfigHome = opts.env?.HOME;
3598
+ const reloadChannels = async () => {
3599
+ const results = [];
3600
+ const { sidecars, errors } = loadChannelSidecars(channelConfigHome);
3601
+ for (const err of errors) {
3602
+ const id = pathIdFromSidecarPath(err.path);
3603
+ results.push({
3604
+ id,
3605
+ ok: false,
3606
+ action: "failed",
3607
+ reason: err.reason
3608
+ });
3609
+ }
3610
+ const sidecarIds = new Set(sidecars.map((s) => s.name));
3611
+ for (const channel of channelManager.listChannels()) {
3612
+ if (sidecarIds.has(channel.id)) continue;
3613
+ try {
3614
+ await channelManager.unregister(channel.id, "shutdown");
3615
+ results.push({
3616
+ id: channel.id,
3617
+ ok: true,
3618
+ action: "unregistered"
3619
+ });
3620
+ } catch (err) {
3621
+ results.push({
3622
+ id: channel.id,
3623
+ ok: false,
3624
+ action: "failed",
3625
+ reason: err instanceof Error ? err.message : String(err)
3626
+ });
3627
+ }
3628
+ }
3629
+ for (const sidecar of sidecars) {
3630
+ const existed = channelManager.listChannels().some((channel) => channel.id === sidecar.name);
3631
+ if (existed) {
3632
+ try {
3633
+ await channelManager.unregister(sidecar.name, "shutdown");
3634
+ } catch (err) {
3635
+ results.push({
3636
+ id: sidecar.name,
3637
+ ok: false,
3638
+ action: "failed",
3639
+ reason: err instanceof Error ? err.message : String(err)
3640
+ });
3641
+ continue;
3642
+ }
3643
+ }
3644
+ const built = instantiateAdapter(sidecar);
3645
+ if (!built.ok) {
3646
+ results.push({
3647
+ id: sidecar.name,
3648
+ ok: false,
3649
+ action: "failed",
3650
+ reason: built.reason
3651
+ });
3652
+ continue;
3653
+ }
3654
+ try {
3655
+ await channelManager.register(built.adapter);
3656
+ results.push({
3657
+ id: sidecar.name,
3658
+ ok: true,
3659
+ action: existed ? "replaced" : "registered"
3660
+ });
3661
+ if (!opts.silent) {
3662
+ process.stdout.write(`athena-gateway: registered ${sidecar.name}
3663
+ `);
3664
+ }
3665
+ } catch (err) {
3666
+ results.push({
3667
+ id: sidecar.name,
3668
+ ok: false,
3669
+ action: "failed",
3670
+ reason: err instanceof Error ? err.message : String(err)
3671
+ });
3672
+ }
3673
+ }
3674
+ return { results };
3675
+ };
3676
+ if (!opts.skipChannelLoad) {
3677
+ const { sidecars, errors } = loadChannelSidecars(channelConfigHome);
3678
+ for (const err of errors) {
3679
+ process.stderr.write(
3680
+ `athena-gateway: skipping ${err.path}: ${err.reason}
3681
+ `
3682
+ );
3683
+ }
3684
+ for (const sidecar of sidecars) {
3685
+ const built = instantiateAdapter(sidecar);
3686
+ if (!built.ok) {
3687
+ process.stderr.write(
3688
+ `athena-gateway: ${sidecar.name}: ${built.reason}
3689
+ `
3690
+ );
3691
+ continue;
3692
+ }
3693
+ try {
3694
+ await channelManager.register(built.adapter);
3695
+ if (!opts.silent) {
3696
+ process.stdout.write(`athena-gateway: registered ${sidecar.name}
3697
+ `);
3698
+ }
3699
+ } catch (err) {
3700
+ process.stderr.write(
3701
+ `athena-gateway: register ${sidecar.name} failed: ${err instanceof Error ? err.message : String(err)}
3702
+ `
3703
+ );
3704
+ }
3705
+ }
3706
+ }
3707
+ const handler = createDispatcher({
3708
+ startedAt,
3709
+ registry,
3710
+ dispatcher,
3711
+ channelManager,
3712
+ relayCoordinator,
3713
+ getListener: () => listenerStatus ?? buildListenerStatus(listenSpec, null),
3714
+ reloadChannels,
3715
+ registerRuntimeConnection: (runtimeId, ctx) => {
3716
+ const timer = staleRuntimeTimers.get(runtimeId);
3717
+ if (timer) {
3718
+ clearTimeout(timer);
3719
+ staleRuntimeTimers.delete(runtimeId);
3720
+ }
3721
+ const previousBinding = registry.getBinding();
3722
+ const wasStale = previousBinding?.state === "stale";
3723
+ const staleSince = wasStale ? previousBinding.staleSince : null;
3724
+ registry.bindConnection(runtimeId, ctx.connectionId);
3725
+ runtimeConnections.set(runtimeId, ctx);
3726
+ writeGatewayTrace(
3727
+ `daemon registered runtime runtimeId=${runtimeId} connectionId=${ctx.connectionId}`
3728
+ );
3729
+ if (wasStale && staleSince !== null) {
3730
+ const newBinding = registry.getBinding();
3731
+ if (newBinding?.state === "active") {
3732
+ trackGatewayRuntimeRebind({
3733
+ gapMs: Date.now() - staleSince,
3734
+ epoch: newBinding.epoch
3735
+ });
3736
+ }
3737
+ }
3738
+ },
3739
+ unregisterRuntimeConnection: (runtimeId) => {
3740
+ const timer = staleRuntimeTimers.get(runtimeId);
3741
+ if (timer) {
3742
+ clearTimeout(timer);
3743
+ staleRuntimeTimers.delete(runtimeId);
3744
+ }
3745
+ runtimeConnections.delete(runtimeId);
3746
+ writeGatewayTrace(`daemon unregistered runtime runtimeId=${runtimeId}`);
3747
+ }
3748
+ });
3749
+ let server;
3750
+ let listener;
3751
+ try {
3752
+ const transport = listenSpec.kind === "tcp" ? createWsServerTransport({
3753
+ host: listenSpec.host,
3754
+ port: listenSpec.port,
3755
+ allowNonLoopback: listenSpec.insecure || Boolean(listenSpec.tls),
3756
+ ...listenSpec.tls ? { tls: listenSpec.tls } : {}
3757
+ }) : void 0;
3758
+ server = await startControlServer({
3759
+ socketPath: paths.socketPath,
3760
+ token,
3761
+ startedAt,
3762
+ handler,
3763
+ ...transport !== void 0 ? { transport } : {},
3764
+ onConnect: (ctx) => {
3765
+ connectionOpenedAt.set(ctx.connectionId, Date.now());
3766
+ trackGatewayTransportConnect({
3767
+ transport: listenerHints.transport,
3768
+ tls: listenerHints.tls,
3769
+ loopback: listenerHints.loopback
3770
+ });
3771
+ },
3772
+ onDisconnect: (ctx) => {
3773
+ const openedAt = connectionOpenedAt.get(ctx.connectionId);
3774
+ connectionOpenedAt.delete(ctx.connectionId);
3775
+ const durationMs = openedAt !== void 0 ? Date.now() - openedAt : 0;
3776
+ trackGatewayTransportDisconnect({
3777
+ transport: listenerHints.transport,
3778
+ reason: "closed",
3779
+ durationMs
3780
+ });
3781
+ const current = registry.getCurrent();
3782
+ if (current && runtimeConnections.get(current.runtimeId)?.connectionId === ctx.connectionId) {
3783
+ writeGatewayTrace(
3784
+ `daemon runtime connection disconnected runtimeId=${current.runtimeId} connectionId=${ctx.connectionId}`
3785
+ );
3786
+ runtimeConnections.delete(current.runtimeId);
3787
+ const staleAt = Date.now();
3788
+ registry.markConnectionStale(ctx.connectionId);
3789
+ if (disconnectGracePeriodMs <= 0) {
3790
+ try {
3791
+ registry.unregister(current.runtimeId);
3792
+ } catch {
3793
+ }
3794
+ relayCoordinator.disposeAll("connection_lost");
3795
+ return;
3796
+ }
3797
+ const runtimeId = current.runtimeId;
3798
+ const timer = setTimeout(() => {
3799
+ staleRuntimeTimers.delete(runtimeId);
3800
+ const latest = registry.getCurrent();
3801
+ if (latest?.runtimeId === runtimeId && !registry.hasActiveBinding(runtimeId)) {
3802
+ try {
3803
+ registry.unregister(runtimeId);
3804
+ } catch {
3805
+ }
3806
+ relayCoordinator.disposeAll("connection_lost");
3807
+ trackGatewayRuntimeExpired({
3808
+ gapMs: Date.now() - staleAt
3809
+ });
3810
+ }
3811
+ }, disconnectGracePeriodMs);
3812
+ staleRuntimeTimers.set(runtimeId, timer);
3813
+ }
3814
+ }
3815
+ });
3816
+ if (listenSpec.kind === "tcp") {
3817
+ const endpoint = transport.endpoint();
3818
+ listener = {
3819
+ kind: "tcp",
3820
+ host: endpoint.host,
3821
+ port: endpoint.port,
3822
+ url: endpoint.url
3823
+ };
3824
+ listenerStatus = buildListenerStatus(listenSpec, endpoint.port);
3825
+ } else {
3826
+ listener = { kind: "uds", socketPath: listenSpec.socketPath };
3827
+ listenerStatus = buildListenerStatus(listenSpec, null);
3828
+ }
3829
+ } catch (err) {
3830
+ lock.release();
3831
+ throw err;
3832
+ }
3833
+ if (!opts.silent) {
3834
+ const target = listener.kind === "tcp" ? listener.url : `socket=${paths.socketPath}`;
3835
+ process.stdout.write(`athena-gateway: ok pid=${pid} ${target}
3836
+ `);
3837
+ }
3838
+ if (listenSpec.kind === "tcp" && listenSpec.insecure && !listenSpec.tls && !isLoopbackHost(listenSpec.host)) {
3839
+ process.stderr.write(
3840
+ `athena-gateway: WARNING --insecure is set on a non-loopback bind (${listenSpec.host}:${listenSpec.port}); token travels in plaintext. Use only behind TLS-terminating reverse proxy or Tailscale/WireGuard tunnel.
3841
+ `
3842
+ );
3843
+ }
3844
+ let stopping = false;
3845
+ const stop = async () => {
3846
+ if (stopping) return;
3847
+ stopping = true;
3848
+ try {
3849
+ outboundDispatcher.stop();
3850
+ for (const timer of staleRuntimeTimers.values()) {
3851
+ clearTimeout(timer);
3852
+ }
3853
+ staleRuntimeTimers.clear();
3854
+ relayCoordinator.disposeAll("auto_resolved");
3855
+ await channelManager.stop();
3856
+ await server.close();
3857
+ } finally {
3858
+ try {
3859
+ stateDb.close();
3860
+ } catch {
3861
+ }
3862
+ lock.release();
3863
+ }
3864
+ };
3865
+ if (!opts.skipSignalHandlers) {
3866
+ const onSignal = (signal) => {
3867
+ process.stderr.write(`athena-gateway: received ${signal}, stopping
3868
+ `);
3869
+ void stop().then(() => process.exit(0));
3870
+ };
3871
+ process.once("SIGINT", onSignal);
3872
+ process.once("SIGTERM", onSignal);
3873
+ }
3874
+ return {
3875
+ startedAt,
3876
+ pid,
3877
+ paths,
3878
+ registry,
3879
+ dispatcher,
3880
+ channelManager,
3881
+ relayCoordinator,
3882
+ inboundQueue,
3883
+ outbox,
3884
+ outboundDispatcher,
3885
+ listener,
3886
+ stop
3887
+ };
3888
+ }
3889
+
3890
+ // src/gateway/entryArgs.ts
3891
+ function parseGatewayDaemonArgs(argv) {
3892
+ const parsed = { silent: false, insecure: false };
3893
+ for (let i = 0; i < argv.length; i += 1) {
3894
+ const arg = argv[i];
3895
+ if (arg === "--silent") {
3896
+ parsed.silent = true;
3897
+ continue;
3898
+ }
3899
+ if (arg === "--insecure") {
3900
+ parsed.insecure = true;
3901
+ continue;
3902
+ }
3903
+ if (arg === "--bind") {
3904
+ parsed.bind = requireValue(argv, i, "--bind");
3905
+ i += 1;
3906
+ continue;
3907
+ }
3908
+ if (arg.startsWith("--bind=")) {
3909
+ parsed.bind = arg.slice("--bind=".length);
3910
+ continue;
3911
+ }
3912
+ if (arg === "--grace-period-ms") {
3913
+ parsed.gracePeriodMs = parseGracePeriod(
3914
+ requireValue(argv, i, "--grace-period-ms")
3915
+ );
3916
+ i += 1;
3917
+ continue;
3918
+ }
3919
+ if (arg.startsWith("--grace-period-ms=")) {
3920
+ parsed.gracePeriodMs = parseGracePeriod(
3921
+ arg.slice("--grace-period-ms=".length)
3922
+ );
3923
+ continue;
3924
+ }
3925
+ const tls = matchPathFlag(arg, argv, i, [
3926
+ ["--tls-cert", "tlsCertPath"],
3927
+ ["--tls-key", "tlsKeyPath"]
3928
+ ]);
3929
+ if (tls) {
3930
+ parsed[tls.key] = tls.value;
3931
+ i += tls.consumed;
3932
+ continue;
3933
+ }
3934
+ throw new Error(`gateway: unknown daemon option ${arg}`);
3935
+ }
3936
+ if (parsed.tlsCertPath && !parsed.tlsKeyPath || !parsed.tlsCertPath && parsed.tlsKeyPath) {
3937
+ throw new Error("gateway: --tls-cert and --tls-key must be used together");
3938
+ }
3939
+ return parsed;
3940
+ }
3941
+ function matchPathFlag(arg, argv, index, specs) {
3942
+ for (const [flag, key] of specs) {
3943
+ if (arg === flag) {
3944
+ return { key, value: requireValue(argv, index, flag), consumed: 1 };
3945
+ }
3946
+ const prefix = `${flag}=`;
3947
+ if (arg.startsWith(prefix)) {
3948
+ return { key, value: arg.slice(prefix.length), consumed: 0 };
3949
+ }
3950
+ }
3951
+ return null;
3952
+ }
3953
+ function requireValue(argv, index, flag) {
3954
+ const value = argv[index + 1];
3955
+ if (!value || value.startsWith("--")) {
3956
+ throw new Error(`gateway: ${flag} requires a value`);
3957
+ }
3958
+ return value;
3959
+ }
3960
+ function parseGracePeriod(value) {
3961
+ const n = Number(value);
3962
+ if (!Number.isInteger(n) || n < 0) {
3963
+ throw new Error(
3964
+ `gateway: --grace-period-ms must be a non-negative integer`
3965
+ );
3966
+ }
3967
+ return n;
3968
+ }
3969
+
3970
+ // src/gateway/entry.ts
3971
+ async function main() {
3972
+ const args = parseGatewayDaemonArgs(process.argv.slice(2));
3973
+ const paths = resolveGatewayPaths();
3974
+ const tls = args.tlsCertPath && args.tlsKeyPath ? { certPath: args.tlsCertPath, keyPath: args.tlsKeyPath } : void 0;
3975
+ const listenSpec = resolveListenSpec({
3976
+ paths,
3977
+ ...args.bind !== void 0 ? { bind: args.bind } : {},
3978
+ insecure: args.insecure,
3979
+ ...tls ? { tls } : {}
3980
+ });
3981
+ await startDaemon({
3982
+ foreground: true,
3983
+ silent: args.silent,
3984
+ paths,
3985
+ listenSpec,
3986
+ disconnectGracePeriodMs: args.gracePeriodMs ?? (listenSpec.kind === "tcp" ? 6e4 : void 0)
3987
+ });
3988
+ setInterval(() => {
3989
+ }, 1 << 30);
3990
+ }
3991
+ main().catch((err) => {
3992
+ process.stderr.write(
3993
+ `athena-gateway: startup failed: ${err instanceof Error ? err.message : String(err)}
3994
+ `
3995
+ );
3996
+ process.exit(1);
3997
+ });
3998
+ //# sourceMappingURL=athena-gateway.js.map