@deepseek-insight/telegram-bot 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ /**
2
+ * status-listener — the bot's identity endpoint (ADR-0091 D2). node:http on
3
+ * 127.0.0.1 ONLY; `GET /` answers who the bot is; every other path or method
4
+ * is 404. It is a status endpoint, never a control channel: nothing can be
5
+ * started, stopped or prompted through it.
6
+ */
7
+ import { createServer, type Server } from 'node:http';
8
+
9
+ /** What the bot says about itself. `ok: true` is added by the listener. */
10
+ export interface BotStatus {
11
+ /** The floor's slot from DSI_SLOT — reported verbatim, null when absent. */
12
+ slot: number | null;
13
+ pid: number;
14
+ /** The DSI this bot serves = the DSI_UI stamp it was spawned with. */
15
+ upstream: string;
16
+ startedAt: string;
17
+ allowCount: number;
18
+ }
19
+
20
+ export interface StatusListener {
21
+ /** null when no listener runs (port 0/unset). */
22
+ port: number | null;
23
+ close(): Promise<void>;
24
+ }
25
+
26
+ export interface StatusListenerOptions {
27
+ /** 0, unset or non-finite → no listener (ADR-0090 bots run exactly as before). */
28
+ port?: number | string | undefined;
29
+ status: BotStatus;
30
+ /** Seam for tests / the boot log. */
31
+ log?: (line: string) => void;
32
+ }
33
+
34
+ export function startStatusListener(options: StatusListenerOptions): StatusListener {
35
+ const port = typeof options.port === 'string' ? Number(options.port) : (options.port ?? 0);
36
+ if (!Number.isInteger(port) || port <= 0) {
37
+ return { port: null, close: async () => {} };
38
+ }
39
+
40
+ const body = JSON.stringify({ ok: true, ...options.status });
41
+ let server: Server | undefined;
42
+ server = createServer((req, res) => {
43
+ if (req.method === 'GET' && req.url === '/') {
44
+ res.writeHead(200, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) });
45
+ res.end(body);
46
+ return;
47
+ }
48
+ // No other route exists — status only, no control (D2).
49
+ res.writeHead(404, { 'content-type': 'text/plain' });
50
+ res.end('not found');
51
+ });
52
+ server.on('error', (err) => {
53
+ (options.log ?? console.error)(`[telegram-bot] status listener on 127.0.0.1:${port} failed: ${err instanceof Error ? err.message : String(err)}`);
54
+ });
55
+ server.listen(port, '127.0.0.1');
56
+
57
+ return {
58
+ port,
59
+ close: () =>
60
+ new Promise<void>((resolve) => {
61
+ if (!server) return resolve();
62
+ server.close(() => resolve());
63
+ server.closeAllConnections();
64
+ server = undefined;
65
+ })
66
+ };
67
+ }