@drisp/cli 0.4.5 → 0.5.0

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