telecoder 0.0.0-stage → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +375 -0
- package/CHANGELOG.md +1867 -0
- package/LICENSE +22 -0
- package/README.md +130 -2
- package/dist/audio/transcribe.js +41 -0
- package/dist/bin/mcp-server.js +363 -0
- package/dist/bin/tool-result.js +22 -0
- package/dist/bot/access/group-access.js +147 -0
- package/dist/bot/bot.js +573 -0
- package/dist/bot/command-catalog.js +263 -0
- package/dist/bot/deleted-threads.js +103 -0
- package/dist/bot/file-access.js +150 -0
- package/dist/bot/handlers/album.handler.js +128 -0
- package/dist/bot/handlers/branch.handler.js +1096 -0
- package/dist/bot/handlers/command/access.js +407 -0
- package/dist/bot/handlers/command/admin.js +803 -0
- package/dist/bot/handlers/command/all-view.js +33 -0
- package/dist/bot/handlers/command/auto-approve.js +138 -0
- package/dist/bot/handlers/command/background.js +37 -0
- package/dist/bot/handlers/command/botname.js +76 -0
- package/dist/bot/handlers/command/btw.js +175 -0
- package/dist/bot/handlers/command/cleanup.js +329 -0
- package/dist/bot/handlers/command/model.js +226 -0
- package/dist/bot/handlers/command/prefs-scope.js +362 -0
- package/dist/bot/handlers/command/project.js +458 -0
- package/dist/bot/handlers/command/resume.js +325 -0
- package/dist/bot/handlers/command/session.js +299 -0
- package/dist/bot/handlers/command/settings.js +657 -0
- package/dist/bot/handlers/command/shared.js +207 -0
- package/dist/bot/handlers/command/tasks.js +681 -0
- package/dist/bot/handlers/command/thread.js +128 -0
- package/dist/bot/handlers/command/topic-store.js +74 -0
- package/dist/bot/handlers/command/topic.js +27 -0
- package/dist/bot/handlers/command/usage.js +382 -0
- package/dist/bot/handlers/command-fallback.js +179 -0
- package/dist/bot/handlers/command.handler.js +446 -0
- package/dist/bot/handlers/loop.handler.js +389 -0
- package/dist/bot/handlers/media-fallback.js +126 -0
- package/dist/bot/handlers/message/replies.js +56 -0
- package/dist/bot/handlers/message/shared.js +95 -0
- package/dist/bot/handlers/message/turn-notify.js +135 -0
- package/dist/bot/handlers/message/turn-runner.js +56 -0
- package/dist/bot/handlers/message.handler.js +114 -0
- package/dist/bot/handlers/newbot.handler.js +230 -0
- package/dist/bot/handlers/photo.handler.js +286 -0
- package/dist/bot/handlers/prompts.handler.js +343 -0
- package/dist/bot/handlers/running-note.js +44 -0
- package/dist/bot/handlers/session-guard.js +78 -0
- package/dist/bot/handlers/suggestion.handler.js +84 -0
- package/dist/bot/handlers/tree.handler.js +480 -0
- package/dist/bot/handlers/view.js +41 -0
- package/dist/bot/handlers/voice.handler.js +147 -0
- package/dist/bot/kept-threads.js +51 -0
- package/dist/bot/middleware/admin-only.js +49 -0
- package/dist/bot/middleware/album-collector.js +69 -0
- package/dist/bot/middleware/auth.middleware.js +122 -0
- package/dist/bot/middleware/group-mention.middleware.js +145 -0
- package/dist/bot/middleware/group-role.middleware.js +75 -0
- package/dist/bot/middleware/message-batcher.js +87 -0
- package/dist/bot/middleware/queued-messages.js +1082 -0
- package/dist/bot/middleware/stale-filter.js +9 -0
- package/dist/build-info.json +8 -0
- package/dist/claude/ask-user-form.js +517 -0
- package/dist/claude/ask-user.js +363 -0
- package/dist/claude/auto-approve.js +225 -0
- package/dist/claude/auto-topic-haiku.js +143 -0
- package/dist/claude/available-commands.js +156 -0
- package/dist/claude/background-hint.js +91 -0
- package/dist/claude/branch-tree.js +393 -0
- package/dist/claude/carry-over.js +25 -0
- package/dist/claude/ccr-shim.js +48 -0
- package/dist/claude/charter-judge.js +214 -0
- package/dist/claude/claude-auth.js +117 -0
- package/dist/claude/command-parser.js +206 -0
- package/dist/claude/cost-overlay-scraper.js +134 -0
- package/dist/claude/effort-level.js +5 -0
- package/dist/claude/enabled-plugins.js +52 -0
- package/dist/claude/in-flight-tracker.js +82 -0
- package/dist/claude/ipc-server.js +192 -0
- package/dist/claude/mcp-bridge.js +481 -0
- package/dist/claude/memory-guard.js +400 -0
- package/dist/claude/message-offsets.js +88 -0
- package/dist/claude/modal-relay.js +817 -0
- package/dist/claude/model-catalog.js +102 -0
- package/dist/claude/monitor-relay.js +712 -0
- package/dist/claude/native-commands.js +40 -0
- package/dist/claude/pending-branches.js +93 -0
- package/dist/claude/pending-suggestions.js +50 -0
- package/dist/claude/permission-gate.js +338 -0
- package/dist/claude/permission-mode.js +152 -0
- package/dist/claude/poll-user.js +51 -0
- package/dist/claude/prewarm.js +70 -0
- package/dist/claude/project-commands.js +69 -0
- package/dist/claude/prompt-hold.js +124 -0
- package/dist/claude/prompt-suggestion-scraper.js +89 -0
- package/dist/claude/pty-idle-reaper.js +96 -0
- package/dist/claude/pty-provider.js +2499 -0
- package/dist/claude/pty-spawn-config.js +343 -0
- package/dist/claude/pty-stream.js +363 -0
- package/dist/claude/pty-watch.js +201 -0
- package/dist/claude/request-queue.js +107 -0
- package/dist/claude/rewind.js +111 -0
- package/dist/claude/scheduled-runner.js +89 -0
- package/dist/claude/scheduler.js +241 -0
- package/dist/claude/scope-guard.js +279 -0
- package/dist/claude/screen-marks.js +83 -0
- package/dist/claude/screen-render.js +96 -0
- package/dist/claude/secret-request.js +576 -0
- package/dist/claude/session-history.js +288 -0
- package/dist/claude/session-jsonl.js +1067 -0
- package/dist/claude/session-manager.js +216 -0
- package/dist/claude/session-move.js +70 -0
- package/dist/claude/session-scan.js +270 -0
- package/dist/claude/setup-token.js +285 -0
- package/dist/claude/shell-mode-gate.js +60 -0
- package/dist/claude/side-call-options.js +45 -0
- package/dist/claude/side-question.js +252 -0
- package/dist/claude/stage-images.js +62 -0
- package/dist/claude/subagent-tools.js +12 -0
- package/dist/claude/synthetic-ctx.js +51 -0
- package/dist/claude/tasks-overlay-scraper.js +217 -0
- package/dist/claude/thread-judge-haiku.js +88 -0
- package/dist/claude/tip-scraper.js +77 -0
- package/dist/claude/tui-modal.js +746 -0
- package/dist/claude/tui-state.js +456 -0
- package/dist/claude/turn-end.js +105 -0
- package/dist/claude/turn-prose.js +64 -0
- package/dist/claude/typed-answer.js +104 -0
- package/dist/claude/update-banner-relay.js +70 -0
- package/dist/cli/commands/doctor.js +338 -0
- package/dist/cli/commands/help.js +34 -0
- package/dist/cli/commands/init.js +126 -0
- package/dist/cli/commands/service.js +710 -0
- package/dist/cli/commands/start.js +63 -0
- package/dist/cli/commands/version.js +11 -0
- package/dist/cli/commands/watch.js +322 -0
- package/dist/cli/dispatch.js +84 -0
- package/dist/cli/init/env-file.js +99 -0
- package/dist/cli/init/flow.js +705 -0
- package/dist/cli/init/prompter.js +124 -0
- package/dist/cli/init/telegram.js +120 -0
- package/dist/cli/service/render.js +212 -0
- package/dist/cli.js +18 -0
- package/dist/config-report.js +107 -0
- package/dist/config.js +525 -0
- package/dist/index.js +443 -0
- package/dist/launcher.js +960 -0
- package/dist/node-version-check.js +27 -0
- package/dist/providers/ccr-health.js +119 -0
- package/dist/providers/ccr-introspect.js +142 -0
- package/dist/providers/ccr-provider.js +59 -0
- package/dist/providers/claude-provider.js +10 -0
- package/dist/providers/prefs-sync.js +121 -0
- package/dist/providers/project-favorites.js +74 -0
- package/dist/providers/provider-router.js +128 -0
- package/dist/providers/provider-switch.js +91 -0
- package/dist/providers/spawn-settings.js +45 -0
- package/dist/providers/types.js +8 -0
- package/dist/providers/user-preferences.js +322 -0
- package/dist/restart-when-idle.js +173 -0
- package/dist/startup/auto-continue.js +67 -0
- package/dist/startup/auto-resume.js +161 -0
- package/dist/startup/notices.js +120 -0
- package/dist/startup/shared.js +44 -0
- package/dist/startup/verify-token.js +31 -0
- package/dist/telegram/access-request.js +153 -0
- package/dist/telegram/action-logger.js +522 -0
- package/dist/telegram/admin-mention.js +107 -0
- package/dist/telegram/background-activity-card.js +502 -0
- package/dist/telegram/botname-settings.js +282 -0
- package/dist/telegram/button-text.js +26 -0
- package/dist/telegram/client-options.js +26 -0
- package/dist/telegram/deduplication.js +33 -0
- package/dist/telegram/entities.js +295 -0
- package/dist/telegram/force-reply-tracker.js +96 -0
- package/dist/telegram/lettered-options.js +87 -0
- package/dist/telegram/markdown.js +362 -0
- package/dist/telegram/message-sender.js +1019 -0
- package/dist/telegram/prompt-label.js +148 -0
- package/dist/telegram/prompt-suggestion.js +54 -0
- package/dist/telegram/send.js +123 -0
- package/dist/telegram/streaming-turn.js +103 -0
- package/dist/telegram/suggestions-settings.js +30 -0
- package/dist/telegram/table-lists.js +56 -0
- package/dist/telegram/task-list.js +367 -0
- package/dist/telegram/task-tracker.js +128 -0
- package/dist/telegram/terminal-renderer.js +667 -0
- package/dist/telegram/thread-titles.js +262 -0
- package/dist/telegram/threads-setup.js +117 -0
- package/dist/utils/admins.js +143 -0
- package/dist/utils/agent-timer.js +55 -0
- package/dist/utils/atomic-write.js +24 -0
- package/dist/utils/bot-icon.js +248 -0
- package/dist/utils/bot-token.js +77 -0
- package/dist/utils/bounded-map.js +55 -0
- package/dist/utils/build-info.js +57 -0
- package/dist/utils/caffeinate.js +53 -0
- package/dist/utils/claude-env.js +53 -0
- package/dist/utils/claude-version.js +48 -0
- package/dist/utils/config-home.js +62 -0
- package/dist/utils/download.js +137 -0
- package/dist/utils/exit-codes.js +59 -0
- package/dist/utils/file-type.js +149 -0
- package/dist/utils/format.js +78 -0
- package/dist/utils/home-project.js +30 -0
- package/dist/utils/host-stall.js +51 -0
- package/dist/utils/id-list.js +44 -0
- package/dist/utils/idle-restart.js +92 -0
- package/dist/utils/install-mode.js +63 -0
- package/dist/utils/instance-config.js +148 -0
- package/dist/utils/instances.js +95 -0
- package/dist/utils/json-store.js +140 -0
- package/dist/utils/keyed-settings.js +62 -0
- package/dist/utils/legacy-env.js +35 -0
- package/dist/utils/memory-pressure.js +60 -0
- package/dist/utils/npm-version.js +134 -0
- package/dist/utils/prefs-relay.js +107 -0
- package/dist/utils/proc-children.js +102 -0
- package/dist/utils/resolve-bin.js +86 -0
- package/dist/utils/resolve-claude-bin.js +52 -0
- package/dist/utils/respawn-backoff.js +28 -0
- package/dist/utils/sanitize.js +91 -0
- package/dist/utils/session-key.js +37 -0
- package/dist/utils/stale-launcher.js +109 -0
- package/dist/utils/state-dir-migration.js +92 -0
- package/dist/utils/systemd.js +126 -0
- package/dist/utils/telegram-proxy.js +72 -0
- package/dist/utils/uploads-dir.js +27 -0
- package/dist/utils/user-roster.js +275 -0
- package/dist/utils/verbosity.js +118 -0
- package/dist/utils/version-check.js +166 -0
- package/dist/utils/workspace-guard.js +44 -0
- package/dist/worker-restart.js +141 -0
- package/instances.json.example +41 -0
- package/package.json +75 -4
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where TeleCoder's configuration lives: the .env and instances.json. Every
|
|
3
|
+
* reader goes through here.
|
|
4
|
+
*
|
|
5
|
+
* In order:
|
|
6
|
+
* 1. TELECODER_HOME, when set — an explicit choice always wins.
|
|
7
|
+
* 2. The package root, when a .env or instances.json sits next to
|
|
8
|
+
* package.json. That is a git checkout set up the way it always has been,
|
|
9
|
+
* and it keeps working exactly as before.
|
|
10
|
+
* 3. ~/.telecoder — an npm install, whose package directory is no place for
|
|
11
|
+
* config (an update replaces it).
|
|
12
|
+
*
|
|
13
|
+
* ~/.telecoder is also the state directory (utils/json-store.ts). The two share
|
|
14
|
+
* it without colliding: state is per-bot `*-<botId>.json` files and the like,
|
|
15
|
+
* config is `.env`, `.env.*` and `instances.json`.
|
|
16
|
+
* TELECODER_HOME moves only the config; state stays in ~/.telecoder.
|
|
17
|
+
*
|
|
18
|
+
* ENV_PATH and INSTANCES_CONFIG still point at a single file directly, ahead
|
|
19
|
+
* of all this; their readers check them first.
|
|
20
|
+
*/
|
|
21
|
+
import * as fs from 'fs';
|
|
22
|
+
import * as os from 'os';
|
|
23
|
+
import * as path from 'path';
|
|
24
|
+
import { PACKAGE_ROOT } from './install-mode.js';
|
|
25
|
+
/** The files whose presence next to package.json marks a checkout-style setup. */
|
|
26
|
+
export const CHECKOUT_CONFIG_FILES = ['.env', 'instances.json'];
|
|
27
|
+
/** The pure decision, for tests and for `telecoder doctor` to explain itself. */
|
|
28
|
+
export function resolveConfigHome({ packageRoot, env, homedir, exists }) {
|
|
29
|
+
const override = env.TELECODER_HOME?.trim();
|
|
30
|
+
if (override)
|
|
31
|
+
return { dir: path.resolve(override), source: 'override' };
|
|
32
|
+
if (CHECKOUT_CONFIG_FILES.some((name) => exists(path.join(packageRoot, name)))) {
|
|
33
|
+
return { dir: packageRoot, source: 'package-root' };
|
|
34
|
+
}
|
|
35
|
+
return { dir: path.join(homedir, '.telecoder'), source: 'home' };
|
|
36
|
+
}
|
|
37
|
+
let cached;
|
|
38
|
+
/**
|
|
39
|
+
* The config home for this process. Decided once: a .env created while the bot
|
|
40
|
+
* runs (by `telecoder init`, say) must not move it halfway through.
|
|
41
|
+
*/
|
|
42
|
+
export function getConfigHomeInfo() {
|
|
43
|
+
cached ??= resolveConfigHome({
|
|
44
|
+
packageRoot: PACKAGE_ROOT,
|
|
45
|
+
env: process.env,
|
|
46
|
+
homedir: os.homedir(),
|
|
47
|
+
exists: fs.existsSync,
|
|
48
|
+
});
|
|
49
|
+
return cached;
|
|
50
|
+
}
|
|
51
|
+
export function getConfigHome() {
|
|
52
|
+
return getConfigHomeInfo().dir;
|
|
53
|
+
}
|
|
54
|
+
/** A file in the config home, e.g. `configFile('.env')`. */
|
|
55
|
+
export function configFile(name) {
|
|
56
|
+
return path.join(getConfigHome(), name);
|
|
57
|
+
}
|
|
58
|
+
/** Test seam: forget the cached decision. */
|
|
59
|
+
export function resetConfigHomeCache() {
|
|
60
|
+
cached = undefined;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=config-home.js.map
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as http from 'http';
|
|
3
|
+
import * as https from 'https';
|
|
4
|
+
import { Readable } from 'stream';
|
|
5
|
+
import { pipeline } from 'stream/promises';
|
|
6
|
+
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
7
|
+
import { redactSecrets } from './sanitize.js';
|
|
8
|
+
/** Attempts per download: one try plus three retries, as curl's `--retry 3` did. */
|
|
9
|
+
const MAX_ATTEMPTS = 4;
|
|
10
|
+
/** Pause between attempts. */
|
|
11
|
+
const RETRY_DELAY_MS = 2_000;
|
|
12
|
+
/** Ceiling on a single attempt, connect through last byte. */
|
|
13
|
+
const ATTEMPT_TIMEOUT_MS = 30_000;
|
|
14
|
+
/** Ceiling on the whole download, retries included. */
|
|
15
|
+
const TOTAL_TIMEOUT_MS = 60_000;
|
|
16
|
+
/**
|
|
17
|
+
* Is this URL safe to download from?
|
|
18
|
+
*
|
|
19
|
+
* Only http(s), and nothing that could smuggle a second line into a request or
|
|
20
|
+
* a log: no CR, LF or NUL. Telegram file URLs are https; plain http is kept
|
|
21
|
+
* for a local Bot API server, which listens on localhost.
|
|
22
|
+
*/
|
|
23
|
+
export function isValidDownloadUrl(url) {
|
|
24
|
+
if (typeof url !== 'string' || /[\r\n\0]/.test(url))
|
|
25
|
+
return false;
|
|
26
|
+
let parsed;
|
|
27
|
+
try {
|
|
28
|
+
parsed = new URL(url);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return parsed.protocol === 'https:' || parsed.protocol === 'http:';
|
|
34
|
+
}
|
|
35
|
+
/** An HTTP status worth retrying: server trouble, rate limits, timeouts. */
|
|
36
|
+
function isRetryableStatus(status) {
|
|
37
|
+
return status >= 500 || status === 429 || status === 408;
|
|
38
|
+
}
|
|
39
|
+
class DownloadError extends Error {
|
|
40
|
+
retryable;
|
|
41
|
+
constructor(message, retryable) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.retryable = retryable;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function sleep(ms) {
|
|
47
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
48
|
+
}
|
|
49
|
+
async function openDirect(fileUrl, signal) {
|
|
50
|
+
const res = await fetch(fileUrl, { signal, redirect: 'follow' });
|
|
51
|
+
return {
|
|
52
|
+
status: res.status,
|
|
53
|
+
body: res.body ? Readable.fromWeb(res.body) : null,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The same request through a proxy. Node's fetch takes no proxy agent, so
|
|
58
|
+
* this goes out over `http(s).get` with the agent the bot's own requests to
|
|
59
|
+
* Telegram use. A redirect isn't followed: Telegram's file endpoint answers
|
|
60
|
+
* with the file.
|
|
61
|
+
*/
|
|
62
|
+
function openThroughProxy(fileUrl, proxyUrl, signal) {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
const get = new URL(fileUrl).protocol === 'https:' ? https.get : http.get;
|
|
65
|
+
get(fileUrl, { agent: new HttpsProxyAgent(proxyUrl), signal }, (res) => {
|
|
66
|
+
resolve({ status: res.statusCode ?? 0, body: res });
|
|
67
|
+
}).on('error', reject);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async function attemptDownload(fileUrl, destPath, signal, proxyUrl) {
|
|
71
|
+
let res;
|
|
72
|
+
try {
|
|
73
|
+
res = proxyUrl ? await openThroughProxy(fileUrl, proxyUrl, signal) : await openDirect(fileUrl, signal);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
// Network failure or timeout. Neither client's message carries the URL,
|
|
77
|
+
// but redact anyway: this is the one place the token is in play, and a
|
|
78
|
+
// proxy URL can hold a password.
|
|
79
|
+
const msg = error instanceof Error ? (error.cause instanceof Error ? error.cause.message : error.message) : String(error);
|
|
80
|
+
throw new DownloadError(redactSecrets(msg), true);
|
|
81
|
+
}
|
|
82
|
+
if (res.status < 200 || res.status >= 300 || !res.body) {
|
|
83
|
+
res.body?.destroy();
|
|
84
|
+
throw new DownloadError(`HTTP ${res.status}`, isRetryableStatus(res.status));
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
await pipeline(res.body, fs.createWriteStream(destPath), { signal });
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
fs.rmSync(destPath, { force: true });
|
|
91
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
92
|
+
throw new DownloadError(redactSecrets(msg), true);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Download a file from a URL to `destPath`, through `options.proxyUrl` when
|
|
97
|
+
* one is given.
|
|
98
|
+
*
|
|
99
|
+
* The request is made in this process, so the URL — which for Telegram files
|
|
100
|
+
* embeds the bot token — never appears in a process argument list (`ps`) or in
|
|
101
|
+
* an error message. Retries transient failures up to three times, two seconds
|
|
102
|
+
* apart, within an overall one-minute budget; a 4xx other than 408/429 fails
|
|
103
|
+
* at once.
|
|
104
|
+
*/
|
|
105
|
+
export async function downloadFileSecure(fileUrl, destPath, options = {}) {
|
|
106
|
+
if (!isValidDownloadUrl(fileUrl)) {
|
|
107
|
+
throw new Error('Invalid URL for download');
|
|
108
|
+
}
|
|
109
|
+
const deadline = Date.now() + TOTAL_TIMEOUT_MS;
|
|
110
|
+
let lastError;
|
|
111
|
+
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
|
112
|
+
const remaining = deadline - Date.now();
|
|
113
|
+
if (remaining <= 0)
|
|
114
|
+
break;
|
|
115
|
+
try {
|
|
116
|
+
await attemptDownload(fileUrl, destPath, AbortSignal.timeout(Math.min(ATTEMPT_TIMEOUT_MS, remaining)), options.proxyUrl);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
lastError = error instanceof DownloadError ? error : new DownloadError(redactSecrets(String(error)), false);
|
|
121
|
+
if (!lastError.retryable || attempt === MAX_ATTEMPTS)
|
|
122
|
+
break;
|
|
123
|
+
if (deadline - Date.now() <= RETRY_DELAY_MS)
|
|
124
|
+
break;
|
|
125
|
+
await sleep(RETRY_DELAY_MS);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
throw new Error(`Failed to download file: ${lastError?.message ?? 'timed out'}`);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Build a Telegram file download URL from the bot token and file path.
|
|
132
|
+
* The result contains the token: pass it to downloadFileSecure, never log it.
|
|
133
|
+
*/
|
|
134
|
+
export function getTelegramFileUrl(botToken, filePath) {
|
|
135
|
+
return `https://api.telegram.org/file/bot${botToken}/${filePath}`;
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=download.js.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { isMainThread } from 'worker_threads';
|
|
2
|
+
/**
|
|
3
|
+
* The exit code a launcher worker uses when its configuration stops it — a
|
|
4
|
+
* missing setting, or a token Telegram rejected. 78 is EX_CONFIG from
|
|
5
|
+
* sysexits.h.
|
|
6
|
+
*
|
|
7
|
+
* The launcher reads it as "restarting won't help": it stops the bot with the
|
|
8
|
+
* message the bot printed, instead of respawning it into the same error four
|
|
9
|
+
* times. A bot run on its own exits 1, which is what process managers and
|
|
10
|
+
* shells expect from a failed start — unless a unit from `telecoder service
|
|
11
|
+
* install` runs it, see configErrorExitCode().
|
|
12
|
+
*/
|
|
13
|
+
export const EXIT_CONFIG = 78;
|
|
14
|
+
/**
|
|
15
|
+
* The exit code a bot uses when Telegram itself is the problem: the startup
|
|
16
|
+
* getMe or the heartbeat couldn't get an answer (a 502, a timeout, no
|
|
17
|
+
* network). 69 is EX_UNAVAILABLE from sysexits.h.
|
|
18
|
+
*
|
|
19
|
+
* The launcher reads it as "try again later" rather than as a crash: an
|
|
20
|
+
* outage can outlast every crash retry, and a bot given up on during one
|
|
21
|
+
* stayed offline until someone restarted the launcher. Anything else that
|
|
22
|
+
* restarts the bot sees an ordinary non-zero exit.
|
|
23
|
+
*/
|
|
24
|
+
export const EXIT_TELEGRAM_UNAVAILABLE = 69;
|
|
25
|
+
/**
|
|
26
|
+
* Set in the environment by the units `telecoder service install` writes, so
|
|
27
|
+
* the bot knows which service manager answers its exit code.
|
|
28
|
+
*/
|
|
29
|
+
export const SERVICE_MANAGER_ENV = 'TELECODER_SERVICE';
|
|
30
|
+
export function getServiceManager(env = process.env) {
|
|
31
|
+
const value = env[SERVICE_MANAGER_ENV];
|
|
32
|
+
return value === 'systemd' || value === 'launchd' ? value : null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The code to exit with on a configuration problem — whichever code makes the
|
|
36
|
+
* thing that started us stand down instead of starting us into the same error
|
|
37
|
+
* again.
|
|
38
|
+
*
|
|
39
|
+
* - A launcher worker: 78, which the launcher reads as "don't respawn".
|
|
40
|
+
* - Under our systemd unit: 78, named in its RestartPreventExitStatus=.
|
|
41
|
+
* - Under our launchd agent: 0. KeepAlive can only tell zero from non-zero
|
|
42
|
+
* (SuccessfulExit), not one code from another, so a clean exit is the only
|
|
43
|
+
* way to keep launchd from restarting a bot that can't start.
|
|
44
|
+
* - Anything else (a terminal, pm2): 1, as for any failed start.
|
|
45
|
+
*/
|
|
46
|
+
export function configErrorExitCode(mainThread = isMainThread, manager = getServiceManager()) {
|
|
47
|
+
if (!mainThread)
|
|
48
|
+
return EXIT_CONFIG;
|
|
49
|
+
if (manager === 'systemd')
|
|
50
|
+
return EXIT_CONFIG;
|
|
51
|
+
if (manager === 'launchd')
|
|
52
|
+
return 0;
|
|
53
|
+
return 1;
|
|
54
|
+
}
|
|
55
|
+
/** Exit for a configuration problem, with the code that fits how we were started. */
|
|
56
|
+
export function exitForConfigError() {
|
|
57
|
+
process.exit(configErrorExitCode());
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=exit-codes.js.map
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
/**
|
|
3
|
+
* Common image file magic bytes (signatures)
|
|
4
|
+
*/
|
|
5
|
+
const IMAGE_SIGNATURES = [
|
|
6
|
+
// JPEG
|
|
7
|
+
{ bytes: [0xFF, 0xD8, 0xFF], extension: '.jpg', mimeType: 'image/jpeg' },
|
|
8
|
+
// PNG
|
|
9
|
+
{ bytes: [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], extension: '.png', mimeType: 'image/png' },
|
|
10
|
+
// GIF87a
|
|
11
|
+
{ bytes: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61], extension: '.gif', mimeType: 'image/gif' },
|
|
12
|
+
// GIF89a
|
|
13
|
+
{ bytes: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61], extension: '.gif', mimeType: 'image/gif' },
|
|
14
|
+
// NOTE: BMP is handled by dedicated isBMP() check, not in this array.
|
|
15
|
+
// TIFF (little endian)
|
|
16
|
+
{ bytes: [0x49, 0x49, 0x2A, 0x00], extension: '.tiff', mimeType: 'image/tiff' },
|
|
17
|
+
// TIFF (big endian)
|
|
18
|
+
{ bytes: [0x4D, 0x4D, 0x00, 0x2A], extension: '.tiff', mimeType: 'image/tiff' },
|
|
19
|
+
// NOTE: WebP is handled by dedicated isWebP() check, not in this array.
|
|
20
|
+
// NOTE: HEIC/HEIF removed — the old [0x00,0x00,0x00] signature was a false-positive
|
|
21
|
+
// magnet (matches any file starting with null bytes). Proper HEIC detection requires
|
|
22
|
+
// checking for "ftyp" at offset 4 and brand codes at offset 8.
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* Check if a buffer starts with the given signature bytes.
|
|
26
|
+
*/
|
|
27
|
+
function matchesSignature(buffer, signature) {
|
|
28
|
+
if (buffer.length < signature.length)
|
|
29
|
+
return false;
|
|
30
|
+
for (let i = 0; i < signature.length; i++) {
|
|
31
|
+
if (buffer[i] !== signature[i])
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Special check for WebP which has RIFF header followed by WEBP at bytes 8-11.
|
|
38
|
+
*/
|
|
39
|
+
function isWebP(buffer) {
|
|
40
|
+
if (buffer.length < 12)
|
|
41
|
+
return false;
|
|
42
|
+
// Check RIFF header
|
|
43
|
+
if (!matchesSignature(buffer, [0x52, 0x49, 0x46, 0x46]))
|
|
44
|
+
return false;
|
|
45
|
+
// Check WEBP signature at offset 8
|
|
46
|
+
return buffer[8] === 0x57 && buffer[9] === 0x45 && buffer[10] === 0x42 && buffer[11] === 0x50;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Detect HEIF container brand from ISO BMFF ftyp box.
|
|
50
|
+
* Returns 'heic' for HEVC-based brands, 'heif' for generic HEIF brands, or null.
|
|
51
|
+
*/
|
|
52
|
+
function getHeifBrand(buffer) {
|
|
53
|
+
if (buffer.length < 12)
|
|
54
|
+
return null;
|
|
55
|
+
if (buffer[4] !== 0x66 || buffer[5] !== 0x74 || buffer[6] !== 0x79 || buffer[7] !== 0x70)
|
|
56
|
+
return null;
|
|
57
|
+
const brand = buffer.slice(8, 12).toString('ascii');
|
|
58
|
+
if (['heic', 'heix', 'hevc', 'hevx'].includes(brand))
|
|
59
|
+
return 'heic';
|
|
60
|
+
if (['mif1', 'msf1', 'heif'].includes(brand))
|
|
61
|
+
return 'heif';
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Additional BMP validation: reserved bytes at offset 6-9 should be zero.
|
|
66
|
+
*/
|
|
67
|
+
function isBMP(buffer) {
|
|
68
|
+
if (buffer.length < 10)
|
|
69
|
+
return false;
|
|
70
|
+
if (buffer[0] !== 0x42 || buffer[1] !== 0x4D)
|
|
71
|
+
return false;
|
|
72
|
+
// Reserved fields at bytes 6-9 must be zero in valid BMP files
|
|
73
|
+
return buffer[6] === 0 && buffer[7] === 0 && buffer[8] === 0 && buffer[9] === 0;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Detect file type from magic bytes.
|
|
77
|
+
* Returns null if the file type is not recognized as an image.
|
|
78
|
+
*/
|
|
79
|
+
export function detectImageType(buffer) {
|
|
80
|
+
// Check WebP first (special case — shares RIFF header with WAV/AVI)
|
|
81
|
+
if (isWebP(buffer)) {
|
|
82
|
+
return { extension: '.webp', mimeType: 'image/webp' };
|
|
83
|
+
}
|
|
84
|
+
// Check HEIC/HEIF (proper ftyp box validation)
|
|
85
|
+
const heifBrand = getHeifBrand(buffer);
|
|
86
|
+
if (heifBrand === 'heic') {
|
|
87
|
+
return { extension: '.heic', mimeType: 'image/heic' };
|
|
88
|
+
}
|
|
89
|
+
if (heifBrand === 'heif') {
|
|
90
|
+
return { extension: '.heif', mimeType: 'image/heif' };
|
|
91
|
+
}
|
|
92
|
+
// Check BMP with reserved-byte validation (avoids 2-byte false positives)
|
|
93
|
+
if (isBMP(buffer)) {
|
|
94
|
+
return { extension: '.bmp', mimeType: 'image/bmp' };
|
|
95
|
+
}
|
|
96
|
+
// Check other signatures
|
|
97
|
+
for (const sig of IMAGE_SIGNATURES) {
|
|
98
|
+
if (sig.extension === '.bmp')
|
|
99
|
+
continue; // Already checked above
|
|
100
|
+
if (matchesSignature(buffer, sig.bytes)) {
|
|
101
|
+
return { extension: sig.extension, mimeType: sig.mimeType };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
function readHeaderBytes(filePath, length = 16) {
|
|
107
|
+
const fd = fs.openSync(filePath, 'r');
|
|
108
|
+
try {
|
|
109
|
+
const buffer = Buffer.alloc(length);
|
|
110
|
+
const bytesRead = fs.readSync(fd, buffer, 0, length, 0);
|
|
111
|
+
return { buffer: buffer.subarray(0, bytesRead), bytesRead };
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
fs.closeSync(fd);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Validate that a file is actually an image by checking magic bytes.
|
|
119
|
+
* @param filePath Path to the file to validate
|
|
120
|
+
* @returns true if the file is a valid image, false otherwise
|
|
121
|
+
*/
|
|
122
|
+
export function isValidImageFile(filePath) {
|
|
123
|
+
try {
|
|
124
|
+
const { buffer, bytesRead } = readHeaderBytes(filePath);
|
|
125
|
+
if (bytesRead < 2)
|
|
126
|
+
return false;
|
|
127
|
+
return detectImageType(buffer) !== null;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get the actual file type from magic bytes.
|
|
135
|
+
* @param filePath Path to the file
|
|
136
|
+
* @returns FileTypeResult or null if not recognized
|
|
137
|
+
*/
|
|
138
|
+
export function getFileType(filePath) {
|
|
139
|
+
try {
|
|
140
|
+
const { buffer, bytesRead } = readHeaderBytes(filePath);
|
|
141
|
+
if (bytesRead < 2)
|
|
142
|
+
return null;
|
|
143
|
+
return detectImageType(buffer);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=file-type.js.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure display formatters for user-facing numbers and times.
|
|
3
|
+
*
|
|
4
|
+
* These live in `utils/` rather than next to their first caller because both
|
|
5
|
+
* the bot handler layer and the provider layer need them, and the providers
|
|
6
|
+
* cannot import from the handlers — the handlers import the providers back.
|
|
7
|
+
* `pty-provider.ts` used to keep its own copy of `fmtTokens` for exactly that
|
|
8
|
+
* reason; a leaf module with no imports of its own removes the cycle instead
|
|
9
|
+
* of working around it.
|
|
10
|
+
*/
|
|
11
|
+
/** Compact token count for user-facing messages: `999`, `12.5k`, `1.2M`. */
|
|
12
|
+
export function fmtTokens(n) {
|
|
13
|
+
if (n >= 1_000_000)
|
|
14
|
+
return (n / 1_000_000).toFixed(1) + 'M';
|
|
15
|
+
if (n >= 1_000)
|
|
16
|
+
return (n / 1_000).toFixed(1) + 'k';
|
|
17
|
+
return String(n);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Ten-cell usage bar, colour-coded green / amber / red at 60% and 80%. Pass
|
|
21
|
+
* `color` to colour it by something other than the fill, as the context bar
|
|
22
|
+
* does with `contextHealthColor`.
|
|
23
|
+
*/
|
|
24
|
+
export function getProgressBar(pct, color) {
|
|
25
|
+
const clamped = Math.max(0, Math.min(100, pct));
|
|
26
|
+
const filled = Math.round(clamped / 10);
|
|
27
|
+
const empty = 10 - filled;
|
|
28
|
+
const shade = color ?? (clamped >= 80 ? '🔴' : clamped >= 60 ? '🟡' : '🟢');
|
|
29
|
+
return shade + ' [' + '█'.repeat(filled) + '░'.repeat(empty) + ']';
|
|
30
|
+
}
|
|
31
|
+
/** Context size from which answers get slower, costlier and less sharp. */
|
|
32
|
+
export const CONTEXT_WARN_TOKENS = 150_000;
|
|
33
|
+
/** Context size from which it's time to /clear, branch or /compact. */
|
|
34
|
+
export const CONTEXT_HIGH_TOKENS = 300_000;
|
|
35
|
+
/**
|
|
36
|
+
* How healthy a context of `used` tokens is, as 🟢 / 🟡 / 🔴.
|
|
37
|
+
*
|
|
38
|
+
* The share of the window alone doesn't say it: with the 1M window a context
|
|
39
|
+
* at 30% is already 300k tokens, where every turn costs more and recall gets
|
|
40
|
+
* patchier, long before auto-compact would step in. So whichever comes first
|
|
41
|
+
* counts — the absolute size, or 60% / 80% of the window.
|
|
42
|
+
*/
|
|
43
|
+
export function contextHealthColor(used, contextWindow) {
|
|
44
|
+
const pct = contextWindow > 0 ? (used / contextWindow) * 100 : 0;
|
|
45
|
+
if (used >= CONTEXT_HIGH_TOKENS || pct >= 80)
|
|
46
|
+
return '🔴';
|
|
47
|
+
if (used >= CONTEXT_WARN_TOKENS || pct >= 60)
|
|
48
|
+
return '🟡';
|
|
49
|
+
return '🟢';
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* One-line manual `/compact` confirmation with the token reduction, which the
|
|
53
|
+
* provider reads back out of the session JSONL.
|
|
54
|
+
*/
|
|
55
|
+
export function formatCompactionConfirmation(c) {
|
|
56
|
+
const before = fmtTokens(c.preTokens);
|
|
57
|
+
// postTokens is absent on older Claude Code builds — omit the arrow then.
|
|
58
|
+
return c.postTokens && c.postTokens > 0
|
|
59
|
+
? `🗜️ Context compacted — ${before} → ${fmtTokens(c.postTokens)} tokens.`
|
|
60
|
+
: `🗜️ Context compacted — was ${before} tokens.`;
|
|
61
|
+
}
|
|
62
|
+
export function formatTimeAgo(date) {
|
|
63
|
+
const now = new Date();
|
|
64
|
+
const diffMs = now.getTime() - date.getTime();
|
|
65
|
+
const diffMins = Math.floor(diffMs / 60000);
|
|
66
|
+
const diffHours = Math.floor(diffMs / 3600000);
|
|
67
|
+
const diffDays = Math.floor(diffMs / 86400000);
|
|
68
|
+
if (diffMins < 1)
|
|
69
|
+
return 'just now';
|
|
70
|
+
if (diffMins < 60)
|
|
71
|
+
return `${diffMins}m ago`;
|
|
72
|
+
if (diffHours < 24)
|
|
73
|
+
return `${diffHours}h ago`;
|
|
74
|
+
if (diffDays < 7)
|
|
75
|
+
return `${diffDays}d ago`;
|
|
76
|
+
return date.toLocaleDateString();
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=format.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project a bot is for (HOME_PROJECT), set per bot in instances.json by
|
|
3
|
+
* /newbot. A bot made for one project names itself after it, so it neither
|
|
4
|
+
* needs a /dir before the first message nor the project in front of
|
|
5
|
+
* every thread's name.
|
|
6
|
+
*/
|
|
7
|
+
import * as fs from 'fs';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
import { config } from '../config.js';
|
|
10
|
+
import { getWorkspaceRoot } from './workspace-guard.js';
|
|
11
|
+
let warned = false;
|
|
12
|
+
/** The bot's home project as an absolute path, or undefined when it has none or it's gone. */
|
|
13
|
+
export function homeProject() {
|
|
14
|
+
if (!config.HOME_PROJECT)
|
|
15
|
+
return undefined;
|
|
16
|
+
const dir = path.resolve(getWorkspaceRoot(), config.HOME_PROJECT);
|
|
17
|
+
if (fs.existsSync(dir) && fs.statSync(dir).isDirectory())
|
|
18
|
+
return dir;
|
|
19
|
+
if (!warned) {
|
|
20
|
+
warned = true;
|
|
21
|
+
console.warn(`[HomeProject] HOME_PROJECT ${dir} is not a directory; ignoring it`);
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Whether `dir` is the bot's home project. */
|
|
26
|
+
export function isHomeProject(dir) {
|
|
27
|
+
const home = homeProject();
|
|
28
|
+
return home !== undefined && dir !== undefined && path.resolve(dir) === home;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=home-project.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telling a frozen host apart from a broken bot.
|
|
3
|
+
*
|
|
4
|
+
* Every watchdog in the bot — the launcher's heartbeat monitor, its
|
|
5
|
+
* wedged-worker escalation, the worker's own Telegram liveness check — reads
|
|
6
|
+
* the same symptom when the machine runs out of memory: something that should
|
|
7
|
+
* have happened on time didn't. Acting on that reads a starved process as a
|
|
8
|
+
* dead one and restarts it, which costs a live Claude session and adds respawn
|
|
9
|
+
* load to a machine already thrashing.
|
|
10
|
+
*
|
|
11
|
+
* These are the decisions those watchdogs share. They live here, away from the
|
|
12
|
+
* launcher's worker-thread spawning and the bot's module-level config load, so
|
|
13
|
+
* they can be tested directly.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Did our own timer fire so late that the thread plainly wasn't running?
|
|
17
|
+
*
|
|
18
|
+
* This is the one first-hand piece of evidence a watchdog has about the host:
|
|
19
|
+
* a timer is late because nothing got scheduled, and whatever the round would
|
|
20
|
+
* have measured was not being measured either.
|
|
21
|
+
*/
|
|
22
|
+
export function tickWasStalled({ sinceLastTickMs, intervalMs, slackMs }) {
|
|
23
|
+
return sinceLastTickMs > intervalMs + slackMs;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Should a watchdog keep its hands off because the host froze recently?
|
|
27
|
+
*
|
|
28
|
+
* `tickWasStalled` only covers the tick that fired late. A freeze that starves
|
|
29
|
+
* the workers without delaying the watchdog's own loop — or one that lifts
|
|
30
|
+
* just before its next tick — leaves them looking silent through no fault of
|
|
31
|
+
* their own, so the suspicion has to outlive the stall that raised it.
|
|
32
|
+
*/
|
|
33
|
+
export function withinStallCooldown({ stalledAgoMs, cooldownMs }) {
|
|
34
|
+
return stalledAgoMs < cooldownMs;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Is a worker that won't exit genuinely wedged, or just starved?
|
|
38
|
+
*
|
|
39
|
+
* They look identical from outside: both ignore terminate(), neither reaches
|
|
40
|
+
* the exit handler. Only the wedged one is worth the cure — killing the
|
|
41
|
+
* launcher so every instance restarts — so defer while the host is still
|
|
42
|
+
* freezing. The deferral is bounded: a machine that stalls every few minutes
|
|
43
|
+
* for hours would otherwise defer forever, leaving the instance offline for
|
|
44
|
+
* good, which is the outcome the escalation exists to prevent.
|
|
45
|
+
*/
|
|
46
|
+
export function shouldEscalateWedged({ stalledAgoMs, cooldownMs, deferredForMs, maxDeferMs, }) {
|
|
47
|
+
if (deferredForMs >= maxDeferMs)
|
|
48
|
+
return true;
|
|
49
|
+
return !withinStallCooldown({ stalledAgoMs, cooldownMs });
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=host-stall.js.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a comma-separated list of numeric Telegram ids from the environment.
|
|
3
|
+
*
|
|
4
|
+
* Kept free of config imports so config.ts can use it and tests can call it
|
|
5
|
+
* directly. The two mistakes it exists to catch both used to start the bot
|
|
6
|
+
* quietly broken: `@alice` parsed to NaN ("Allowed users: NaN", nobody let
|
|
7
|
+
* in), and the .env.example placeholder 123456789 was accepted as a real
|
|
8
|
+
* admin — someone else's account, for all anyone could tell.
|
|
9
|
+
*/
|
|
10
|
+
/** The user id .env.example ships with. Never a real install's id. */
|
|
11
|
+
export const PLACEHOLDER_USER_ID = 123456789;
|
|
12
|
+
/**
|
|
13
|
+
* `kind` decides the hint: user ids are positive, and group ids are negative
|
|
14
|
+
* (supergroups start with -100).
|
|
15
|
+
*/
|
|
16
|
+
export function parseIdList(name, raw, kind = 'user') {
|
|
17
|
+
const entries = raw.split(',').map((s) => s.trim()).filter(Boolean);
|
|
18
|
+
const ids = [];
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
if (!/^-?\d+$/.test(entry)) {
|
|
21
|
+
const hint = entry.startsWith('@')
|
|
22
|
+
? ` Telegram usernames can't be used here: the bot can't look a username up, and usernames can change.`
|
|
23
|
+
: '';
|
|
24
|
+
const how = kind === 'user'
|
|
25
|
+
? 'Each entry must be a numeric Telegram user id, like 412345678 — message @userinfobot on Telegram to get yours.'
|
|
26
|
+
: 'Each entry must be a numeric Telegram chat id, like -1001234567890 — add @RawDataBot to the group to see it.';
|
|
27
|
+
return { ok: false, error: `${name} contains "${entry}", which is not a number.${hint} ${how}` };
|
|
28
|
+
}
|
|
29
|
+
const id = Number(entry);
|
|
30
|
+
if (!Number.isSafeInteger(id)) {
|
|
31
|
+
return { ok: false, error: `${name} contains "${entry}", which is too large to be a Telegram id.` };
|
|
32
|
+
}
|
|
33
|
+
if (kind === 'user' && id === PLACEHOLDER_USER_ID) {
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
error: `${name} still contains ${PLACEHOLDER_USER_ID}, the example value from .env.example. ` +
|
|
37
|
+
'Replace it with your own numeric Telegram user id — message @userinfobot on Telegram to get it.',
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
ids.push(id);
|
|
41
|
+
}
|
|
42
|
+
return { ok: true, ids };
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=id-list.js.map
|