telecoder 0.0.0-stage → 1.2.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 +363 -0
- package/CHANGELOG.md +1186 -0
- package/LICENSE +22 -0
- package/README.md +126 -2
- package/dist/audio/transcribe.js +50 -0
- package/dist/bin/mcp-server.js +378 -0
- package/dist/bin/tool-result.js +22 -0
- package/dist/bot/access/group-access.js +147 -0
- package/dist/bot/bot.js +592 -0
- package/dist/bot/file-access.js +141 -0
- package/dist/bot/handlers/command/access.js +407 -0
- package/dist/bot/handlers/command/admin.js +827 -0
- package/dist/bot/handlers/command/auto-approve.js +138 -0
- package/dist/bot/handlers/command/btw.js +224 -0
- package/dist/bot/handlers/command/cost.js +409 -0
- package/dist/bot/handlers/command/media.js +197 -0
- package/dist/bot/handlers/command/model.js +305 -0
- package/dist/bot/handlers/command/prefs-scope.js +197 -0
- package/dist/bot/handlers/command/project.js +536 -0
- package/dist/bot/handlers/command/resume.js +321 -0
- package/dist/bot/handlers/command/session.js +438 -0
- package/dist/bot/handlers/command/settings.js +1012 -0
- package/dist/bot/handlers/command/shared.js +210 -0
- package/dist/bot/handlers/command/streaming-mode.js +16 -0
- package/dist/bot/handlers/command/tasks.js +513 -0
- package/dist/bot/handlers/command/topic-store.js +113 -0
- package/dist/bot/handlers/command/topic.js +80 -0
- package/dist/bot/handlers/command.handler.js +772 -0
- package/dist/bot/handlers/fork.handler.js +1109 -0
- package/dist/bot/handlers/media-fallback.js +121 -0
- package/dist/bot/handlers/message/replies.js +133 -0
- package/dist/bot/handlers/message/shared.js +117 -0
- package/dist/bot/handlers/message/throttle.js +49 -0
- package/dist/bot/handlers/message/turn-notify.js +169 -0
- package/dist/bot/handlers/message/turn-runner.js +141 -0
- package/dist/bot/handlers/message.handler.js +107 -0
- package/dist/bot/handlers/newbot.handler.js +221 -0
- package/dist/bot/handlers/photo.handler.js +327 -0
- package/dist/bot/handlers/prompts.handler.js +343 -0
- package/dist/bot/handlers/running-note.js +44 -0
- package/dist/bot/handlers/schedule.handler.js +303 -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 +209 -0
- package/dist/bot/middleware/admin-only.js +49 -0
- package/dist/bot/middleware/auth.middleware.js +122 -0
- package/dist/bot/middleware/group-mention.middleware.js +139 -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 +971 -0
- package/dist/bot/middleware/stale-filter.js +31 -0
- package/dist/build-info.json +8 -0
- package/dist/claude/agent-watchdog.js +162 -0
- package/dist/claude/agent.js +1458 -0
- package/dist/claude/ask-user.js +295 -0
- package/dist/claude/auto-approve.js +225 -0
- package/dist/claude/auto-topic-haiku.js +126 -0
- package/dist/claude/available-commands.js +153 -0
- package/dist/claude/carry-over.js +27 -0
- package/dist/claude/ccr-shim.js +46 -0
- package/dist/claude/charter-judge.js +214 -0
- package/dist/claude/claude-auth.js +121 -0
- package/dist/claude/command-parser.js +200 -0
- package/dist/claude/cost-overlay-scraper.js +129 -0
- package/dist/claude/enabled-plugins.js +56 -0
- package/dist/claude/fork-tree.js +393 -0
- package/dist/claude/held-query.js +190 -0
- package/dist/claude/in-flight-tracker.js +61 -0
- package/dist/claude/ipc-server.js +192 -0
- package/dist/claude/mcp-bridge.js +453 -0
- package/dist/claude/mcp-tools.js +354 -0
- package/dist/claude/message-offsets.js +88 -0
- package/dist/claude/modal-relay.js +450 -0
- package/dist/claude/model-catalog.js +85 -0
- package/dist/claude/monitor-relay.js +597 -0
- package/dist/claude/pending-forks.js +93 -0
- package/dist/claude/pending-suggestions.js +50 -0
- package/dist/claude/permission-gate.js +337 -0
- package/dist/claude/permission-mode.js +178 -0
- package/dist/claude/poll-user.js +51 -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-provider.js +2214 -0
- package/dist/claude/pty-spawn-config.js +296 -0
- package/dist/claude/pty-stream.js +363 -0
- package/dist/claude/request-queue.js +135 -0
- package/dist/claude/scheduled-runner.js +91 -0
- package/dist/claude/scheduler.js +237 -0
- package/dist/claude/scope-guard.js +274 -0
- package/dist/claude/sdk-permission-gate.js +77 -0
- package/dist/claude/secret-request.js +576 -0
- package/dist/claude/session-cost.js +41 -0
- package/dist/claude/session-history.js +286 -0
- package/dist/claude/session-jsonl.js +920 -0
- package/dist/claude/session-manager.js +195 -0
- package/dist/claude/session-scan.js +270 -0
- package/dist/claude/setup-token.js +285 -0
- package/dist/claude/side-call-options.js +45 -0
- package/dist/claude/side-question.js +249 -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/system-prompt.js +162 -0
- package/dist/claude/tasks-overlay-scraper.js +94 -0
- package/dist/claude/tip-scraper.js +78 -0
- package/dist/claude/tui-modal.js +501 -0
- package/dist/claude/tui-state.js +384 -0
- package/dist/claude/turn-end.js +91 -0
- package/dist/claude/turn-prose.js +64 -0
- package/dist/claude/update-banner-relay.js +70 -0
- package/dist/cli/commands/doctor.js +305 -0
- package/dist/cli/commands/help.js +25 -0
- package/dist/cli/commands/init.js +123 -0
- package/dist/cli/commands/service.js +678 -0
- package/dist/cli/commands/start.js +53 -0
- package/dist/cli/commands/version.js +11 -0
- package/dist/cli/dispatch.js +79 -0
- package/dist/cli/init/env-file.js +99 -0
- package/dist/cli/init/flow.js +638 -0
- package/dist/cli/init/prompter.js +116 -0
- package/dist/cli/init/telegram.js +94 -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 +500 -0
- package/dist/index.js +367 -0
- package/dist/launcher.js +933 -0
- package/dist/node-version-check.js +27 -0
- package/dist/providers/ccr-health.js +119 -0
- package/dist/providers/ccr-introspect.js +149 -0
- package/dist/providers/ccr-provider.js +60 -0
- package/dist/providers/claude-provider.js +63 -0
- package/dist/providers/prefs-sync.js +164 -0
- package/dist/providers/project-favorites.js +74 -0
- package/dist/providers/provider-router.js +127 -0
- package/dist/providers/provider-switch.js +91 -0
- package/dist/providers/sdk-provider.js +37 -0
- package/dist/providers/types.js +8 -0
- package/dist/providers/user-preferences.js +214 -0
- package/dist/restart-when-idle.js +153 -0
- package/dist/startup/auto-continue.js +67 -0
- package/dist/startup/auto-resume.js +173 -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 +684 -0
- package/dist/telegram/admin-mention.js +107 -0
- package/dist/telegram/background-activity-card.js +502 -0
- package/dist/telegram/botname-settings.js +336 -0
- package/dist/telegram/button-text.js +26 -0
- package/dist/telegram/client-options.js +27 -0
- package/dist/telegram/deduplication.js +33 -0
- package/dist/telegram/entities.js +295 -0
- package/dist/telegram/force-reply-tracker.js +57 -0
- package/dist/telegram/lettered-options.js +87 -0
- package/dist/telegram/markdown.js +362 -0
- package/dist/telegram/message-sender.js +1287 -0
- package/dist/telegram/prompt-label.js +142 -0
- package/dist/telegram/prompt-suggestion.js +54 -0
- package/dist/telegram/send.js +123 -0
- package/dist/telegram/streaming-turn.js +105 -0
- package/dist/telegram/suggestions-settings.js +30 -0
- package/dist/telegram/task-list.js +367 -0
- package/dist/telegram/task-tracker.js +209 -0
- package/dist/telegram/telegraph-settings.js +31 -0
- package/dist/telegram/telegraph.js +453 -0
- package/dist/telegram/terminal-renderer.js +671 -0
- package/dist/telegram/terminal-settings.js +25 -0
- package/dist/telegram/thread-titles.js +190 -0
- package/dist/telegram/threads-setup.js +117 -0
- package/dist/tts/tts-settings.js +62 -0
- package/dist/tts/tts.js +245 -0
- package/dist/tts/voice-reply.js +78 -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-token.js +74 -0
- package/dist/utils/bounded-map.js +55 -0
- package/dist/utils/build-info.js +57 -0
- package/dist/utils/caffeinate.js +50 -0
- package/dist/utils/claude-env.js +54 -0
- package/dist/utils/claude-version.js +48 -0
- package/dist/utils/config-home.js +62 -0
- package/dist/utils/debug-agent.js +117 -0
- package/dist/utils/debug-watchdog.js +121 -0
- package/dist/utils/download.js +111 -0
- package/dist/utils/exit-codes.js +48 -0
- package/dist/utils/file-type.js +149 -0
- package/dist/utils/format.js +81 -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/npm-version.js +134 -0
- package/dist/utils/prefs-relay.js +107 -0
- package/dist/utils/proc-children.js +102 -0
- package/dist/utils/project-data-dir.js +33 -0
- package/dist/utils/reasoning-summary.js +12 -0
- package/dist/utils/resolve-bin.js +86 -0
- package/dist/utils/resolve-claude-bin.js +63 -0
- package/dist/utils/respawn-backoff.js +19 -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/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 +76 -4
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `telecoder init` wizard, free of the terminal and the network: the
|
|
3
|
+
* prompts come through a Prompter and Telegram through a TelegramClient, so the
|
|
4
|
+
* tests drive the whole flow with fakes. commands/init.ts wires up the real ones.
|
|
5
|
+
*
|
|
6
|
+
* The steps: check the machine (doctor's checks), decide what to do with an
|
|
7
|
+
* existing .env, get and check the bot token, find the owner's id by having
|
|
8
|
+
* them message the bot, pick a workspace and a transport, two optional
|
|
9
|
+
* features, write the file, and have the bot say hello.
|
|
10
|
+
*
|
|
11
|
+
* Secrets are never printed: not the token, not the Groq key, not in errors
|
|
12
|
+
* (telegram.ts redacts those) and not in the summary before writing.
|
|
13
|
+
*/
|
|
14
|
+
import * as fs from 'fs';
|
|
15
|
+
import * as path from 'path';
|
|
16
|
+
import { parse as parseEnv } from 'dotenv';
|
|
17
|
+
import { isWellFormedBotToken, MALFORMED_TOKEN_MESSAGE, REJECTED_TOKEN_MESSAGE } from '../../utils/bot-token.js';
|
|
18
|
+
import { parseIdList } from '../../utils/id-list.js';
|
|
19
|
+
import { redactSecrets } from '../../utils/sanitize.js';
|
|
20
|
+
import { backupFile, renderEnv, updateEnvText, writeEnvFile } from './env-file.js';
|
|
21
|
+
import { isAbort } from './prompter.js';
|
|
22
|
+
import { TelegramApiError, TelegramNetworkError, } from './telegram.js';
|
|
23
|
+
/** Why the wizard stopped without finishing. The message is for the user. */
|
|
24
|
+
class Stop extends Error {
|
|
25
|
+
code;
|
|
26
|
+
constructor(message, code = 1) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.code = code;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const ICON = { ok: '✓', warn: '!', fail: '✗' };
|
|
32
|
+
/** What the bot answers a /start while the wizard waits for the owner's. */
|
|
33
|
+
export const OWNER_CAPTURED_REPLY = '👋 Got it. Back in your terminal, confirm it\'s you to finish setting up.';
|
|
34
|
+
/** The hello the bot sends at the end: the start command set as code. */
|
|
35
|
+
export function testMessage(startCommand) {
|
|
36
|
+
const before = '✅ TeleCoder is set up. Start it with ';
|
|
37
|
+
return {
|
|
38
|
+
text: `${before}${startCommand}.`,
|
|
39
|
+
// String length is UTF-16 units, which is what Telegram's offsets count.
|
|
40
|
+
entities: [{ type: 'code', offset: before.length, length: startCommand.length }],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export async function runInit(opts, rawDeps) {
|
|
44
|
+
const deps = { ...rawDeps, prompter: collapseBlankLines(rawDeps.prompter) };
|
|
45
|
+
const p = deps.prompter;
|
|
46
|
+
const interactive = p.interactive && !opts.yes;
|
|
47
|
+
let wrote = false;
|
|
48
|
+
try {
|
|
49
|
+
await wizard(opts, deps, interactive, () => { wrote = true; });
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (isAbort(error)) {
|
|
54
|
+
p.say('');
|
|
55
|
+
p.say(wrote ? 'Cancelled.' : 'Cancelled. Nothing was written.');
|
|
56
|
+
return 130;
|
|
57
|
+
}
|
|
58
|
+
if (error instanceof Stop) {
|
|
59
|
+
if (error.message)
|
|
60
|
+
p.say(error.message);
|
|
61
|
+
return error.code;
|
|
62
|
+
}
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The steps end with a blank line each; when one asked nothing (a flag or a
|
|
68
|
+
* kept value answered it) that would leave a stack of them.
|
|
69
|
+
*/
|
|
70
|
+
function collapseBlankLines(p) {
|
|
71
|
+
let blank = true;
|
|
72
|
+
const answered = (pending) => { blank = false; return pending; };
|
|
73
|
+
return {
|
|
74
|
+
interactive: p.interactive,
|
|
75
|
+
signal: p.signal,
|
|
76
|
+
say: (line = '') => {
|
|
77
|
+
if (line === '' && blank)
|
|
78
|
+
return;
|
|
79
|
+
blank = line === '';
|
|
80
|
+
p.say(line);
|
|
81
|
+
},
|
|
82
|
+
ask: (q, o) => answered(p.ask(q, o)),
|
|
83
|
+
secret: (q) => answered(p.secret(q)),
|
|
84
|
+
confirm: (q, d) => answered(p.confirm(q, d)),
|
|
85
|
+
choose: (q, c, d) => answered(p.choose(q, c, d)),
|
|
86
|
+
runAttached: (cmd, args) => { blank = false; return p.runAttached(cmd, args); },
|
|
87
|
+
close: () => p.close(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
async function wizard(opts, deps, interactive, markWritten) {
|
|
91
|
+
const p = deps.prompter;
|
|
92
|
+
const startCommand = deps.isCheckout ? 'npm start' : 'telecoder start';
|
|
93
|
+
p.say('TeleCoder setup');
|
|
94
|
+
p.say('');
|
|
95
|
+
// ── 1. The machine ──────────────────────────────────────────
|
|
96
|
+
const pre = await deps.preflight();
|
|
97
|
+
const show = (r) => {
|
|
98
|
+
p.say(`${ICON[r.status]} ${r.label}`);
|
|
99
|
+
for (const line of r.detail ?? [])
|
|
100
|
+
p.say(` ${line}`);
|
|
101
|
+
};
|
|
102
|
+
show(pre.node);
|
|
103
|
+
if (pre.node.status === 'fail')
|
|
104
|
+
throw new Stop('\nInstall a newer Node.js, then run "telecoder init" again.');
|
|
105
|
+
for (const r of pre.claude)
|
|
106
|
+
show(r);
|
|
107
|
+
if (pre.claude[0]?.status === 'fail') {
|
|
108
|
+
throw new Stop('\nTeleCoder drives Claude Code, so it has to be installed first. Then run "telecoder init" again.');
|
|
109
|
+
}
|
|
110
|
+
if (pre.claude[1]?.status === 'fail')
|
|
111
|
+
await offerLogin(deps, interactive);
|
|
112
|
+
const claudeLogin = await stepEnvLogin(deps, interactive, pre.envLogin ?? []);
|
|
113
|
+
for (const r of pre.nodePty)
|
|
114
|
+
show(r);
|
|
115
|
+
const ptyWorks = pre.nodePty.every((r) => r.status === 'ok');
|
|
116
|
+
if (!ptyWorks)
|
|
117
|
+
p.say(' Only the PTY transport needs it; the default SDK transport works without.');
|
|
118
|
+
p.say('');
|
|
119
|
+
// ── 2. An existing .env ─────────────────────────────────────
|
|
120
|
+
let existing = {};
|
|
121
|
+
let existingText;
|
|
122
|
+
if (fs.existsSync(deps.envPath)) {
|
|
123
|
+
existingText = fs.readFileSync(deps.envPath, 'utf-8');
|
|
124
|
+
if (!interactive) {
|
|
125
|
+
if (!opts.yes) {
|
|
126
|
+
throw new Stop(`${deps.envPath} already exists. Run "telecoder init" in a terminal to edit it, ` +
|
|
127
|
+
'or pass --yes to replace it (the old one is kept as a backup).');
|
|
128
|
+
}
|
|
129
|
+
existingText = undefined;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
p.say(`There is already a config at ${deps.envPath}.`);
|
|
133
|
+
const choice = await p.choose('What do you want to do?', [
|
|
134
|
+
{ key: 'keep', label: 'Keep it as it is, and stop here' },
|
|
135
|
+
{ key: 'edit', label: 'Go through the setup with its values as the defaults' },
|
|
136
|
+
{ key: 'over', label: 'Start over with a new file' },
|
|
137
|
+
], 'keep');
|
|
138
|
+
if (choice === 'keep')
|
|
139
|
+
throw new Stop(`Kept ${deps.envPath} as it is. "telecoder doctor" checks it.`, 0);
|
|
140
|
+
if (choice === 'edit')
|
|
141
|
+
existing = parseEnv(existingText);
|
|
142
|
+
else
|
|
143
|
+
existingText = undefined;
|
|
144
|
+
}
|
|
145
|
+
const backup = backupFile(deps.envPath, deps.now?.());
|
|
146
|
+
p.say(`Backed up the current file to ${backup}`);
|
|
147
|
+
p.say('');
|
|
148
|
+
}
|
|
149
|
+
const editing = existingText !== undefined;
|
|
150
|
+
// ── 3. Bot token ────────────────────────────────────────────
|
|
151
|
+
const { token, bot, client } = await stepToken(opts, deps, interactive, editing ? existing.TELEGRAM_BOT_TOKEN : undefined);
|
|
152
|
+
p.say(`Connected to @${bot.username}`);
|
|
153
|
+
p.say('');
|
|
154
|
+
// ── 4. Owner ────────────────────────────────────────────────
|
|
155
|
+
const owner = await stepOwner(opts, deps, interactive, client, bot, editing ? existing.ALLOWED_USER_IDS : undefined, Boolean(existing.ADMIN_USER_IDS || existing.APPROVER_USER_IDS));
|
|
156
|
+
p.say('');
|
|
157
|
+
// ── 5. Workspace ────────────────────────────────────────────
|
|
158
|
+
const workspace = await stepWorkspace(opts, deps, interactive, editing ? existing.WORKSPACE_DIR : undefined);
|
|
159
|
+
p.say('');
|
|
160
|
+
// ── 6. Transport ────────────────────────────────────────────
|
|
161
|
+
const currentTransport = existing.CLAUDE_METHOD_DEFAULT === 'pty' ? 'pty' : 'sdk';
|
|
162
|
+
let transport = opts.transport ?? currentTransport;
|
|
163
|
+
if (interactive && !opts.transport) {
|
|
164
|
+
transport = await p.choose('How should TeleCoder run Claude Code? (/method switches it per chat later)', [
|
|
165
|
+
{ key: 'sdk', label: 'SDK: the Claude Agent SDK. Simplest, and it tracks cost per turn.' },
|
|
166
|
+
{ key: 'pty', label: 'PTY: your own `claude` CLI in a terminal, its dialogs relayed to Telegram. More moving parts; needs node-pty.' },
|
|
167
|
+
], currentTransport);
|
|
168
|
+
}
|
|
169
|
+
if (transport === 'pty' && !ptyWorks)
|
|
170
|
+
p.say('! node-pty did not load (see above): PTY chats will fail until it does.');
|
|
171
|
+
p.say('');
|
|
172
|
+
// ── 7. Optional features ────────────────────────────────────
|
|
173
|
+
const groqKey = await stepVoice(opts, deps, interactive, editing ? existing.GROQ_API_KEY : undefined);
|
|
174
|
+
const wasTelegraph = /^(true|1|yes)$/i.test(existing.TELEGRAPH_ENABLED ?? '');
|
|
175
|
+
let telegraph = opts.telegraph ?? wasTelegraph;
|
|
176
|
+
if (interactive && opts.telegraph === undefined) {
|
|
177
|
+
telegraph = await p.confirm('Publish long replies as Telegraph (Instant View) pages too? The pages are public to anyone with the link, and can\'t be deleted.', wasTelegraph);
|
|
178
|
+
}
|
|
179
|
+
p.say('');
|
|
180
|
+
// ── 8. Write ────────────────────────────────────────────────
|
|
181
|
+
const values = {
|
|
182
|
+
TELEGRAM_BOT_TOKEN: token,
|
|
183
|
+
ALLOWED_USER_IDS: owner.idList,
|
|
184
|
+
WORKSPACE_DIR: workspace,
|
|
185
|
+
CLAUDE_METHOD_DEFAULT: transport,
|
|
186
|
+
};
|
|
187
|
+
Object.assign(values, claudeLogin);
|
|
188
|
+
if (groqKey)
|
|
189
|
+
values.GROQ_API_KEY = groqKey;
|
|
190
|
+
if (telegraph)
|
|
191
|
+
values.TELEGRAPH_ENABLED = 'true';
|
|
192
|
+
p.say(`About to write ${deps.envPath}:`);
|
|
193
|
+
p.say(` Bot @${bot.username} (token hidden)`);
|
|
194
|
+
p.say(` Allowed users ${owner.idList}`);
|
|
195
|
+
p.say(` Workspace ${workspace}`);
|
|
196
|
+
p.say(` Transport ${transport.toUpperCase()}`);
|
|
197
|
+
for (const key of Object.keys(claudeLogin))
|
|
198
|
+
p.say(` Claude login ${key}=<hidden>`);
|
|
199
|
+
p.say(` Voice ${groqKey ? 'on (Groq key hidden)' : 'off'}`);
|
|
200
|
+
p.say(` Telegraph ${telegraph ? 'on' : 'off'}`);
|
|
201
|
+
if (interactive && !await p.confirm('Write it?', true))
|
|
202
|
+
throw new Stop('Nothing was written.', 1);
|
|
203
|
+
let content;
|
|
204
|
+
if (existingText !== undefined) {
|
|
205
|
+
const updates = {};
|
|
206
|
+
for (const [key, value] of Object.entries(values))
|
|
207
|
+
if (existing[key] !== value)
|
|
208
|
+
updates[key] = value;
|
|
209
|
+
if (!groqKey && existing.GROQ_API_KEY)
|
|
210
|
+
updates.GROQ_API_KEY = undefined;
|
|
211
|
+
if (!telegraph && wasTelegraph)
|
|
212
|
+
updates.TELEGRAPH_ENABLED = 'false';
|
|
213
|
+
content = updateEnvText(existingText, updates);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
content = renderEnv(values);
|
|
217
|
+
}
|
|
218
|
+
writeEnvFile(deps.envPath, content, { ownDir: deps.ownDir });
|
|
219
|
+
markWritten();
|
|
220
|
+
p.say(`✓ Wrote ${deps.envPath} (readable by you only)`);
|
|
221
|
+
if (owner.otherRoles) {
|
|
222
|
+
p.say('! ADMIN_USER_IDS / APPROVER_USER_IDS are set in this file: check they are still in ALLOWED_USER_IDS.');
|
|
223
|
+
}
|
|
224
|
+
// ── 9. Hello, and what next ─────────────────────────────────
|
|
225
|
+
if (!opts.noTestMessage) {
|
|
226
|
+
try {
|
|
227
|
+
const hello = testMessage(startCommand);
|
|
228
|
+
await client.sendMessage(owner.id, hello.text, hello.entities);
|
|
229
|
+
p.say('✓ Sent you a test message on Telegram');
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
p.say(`! Couldn't send you a test message: ${describeTelegramError(error)}`);
|
|
233
|
+
if (error instanceof TelegramApiError && error.code === 403) {
|
|
234
|
+
p.say(` Open https://t.me/${bot.username} and press Start, so the bot is allowed to message you.`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
p.say('');
|
|
239
|
+
p.say('Now:');
|
|
240
|
+
p.say(` ${startCommand.padEnd(18)}run the bot, then message it on Telegram`);
|
|
241
|
+
p.say(` ${'telecoder doctor'.padEnd(18)}check the setup again any time`);
|
|
242
|
+
// A checkout runs under pm2 or botctl; the service is for npm installs.
|
|
243
|
+
if (interactive && deps.requestService && !deps.isCheckout
|
|
244
|
+
&& await p.confirm('Keep it running in the background now (telecoder service install)?', true)) {
|
|
245
|
+
deps.requestService();
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
p.say('Next: run `telecoder service install` to keep it running in the background.');
|
|
249
|
+
}
|
|
250
|
+
// ── Steps ─────────────────────────────────────────────────────
|
|
251
|
+
async function offerLogin(deps, interactive) {
|
|
252
|
+
const p = deps.prompter;
|
|
253
|
+
p.say(' TeleCoder answers as your Claude Code login, so it needs one. Either:');
|
|
254
|
+
p.say(' - run "claude auth login" (or "claude", then /login) as the user the bot runs as, or');
|
|
255
|
+
p.say(' - on a machine you won\'t sign in on, run "claude setup-token" and put the token it');
|
|
256
|
+
p.say(' prints in CLAUDE_CODE_OAUTH_TOKEN; or set ANTHROPIC_API_KEY (billed per use).');
|
|
257
|
+
if (!interactive) {
|
|
258
|
+
p.say(' Carrying on: the bot will start without it, but can\'t answer until it has one.');
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (await p.confirm('Log in now? (runs "claude auth login")', true)) {
|
|
262
|
+
const status = p.runAttached(deps.claudeBin, ['auth', 'login']);
|
|
263
|
+
p.say(status === 0 ? '✓ Logged in' : '! The login didn\'t finish; carrying on. Run "claude auth login" later.');
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
p.say(' Carrying on without it.');
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* A login in the shell's environment works for the wizard and for
|
|
271
|
+
* `telecoder start` from this shell, but a service starts without it. Offer
|
|
272
|
+
* to keep the variables in the .env, which the bot loads. The values are
|
|
273
|
+
* never printed.
|
|
274
|
+
*/
|
|
275
|
+
async function stepEnvLogin(deps, interactive, vars) {
|
|
276
|
+
const p = deps.prompter;
|
|
277
|
+
const found = vars.filter((key) => deps.env[key]?.trim());
|
|
278
|
+
if (!found.length)
|
|
279
|
+
return {};
|
|
280
|
+
const names = found.join(' and ');
|
|
281
|
+
p.say(`! Claude Code is logged in through ${names} in this shell's environment, not stored credentials.`);
|
|
282
|
+
p.say(' A background service doesn\'t see your shell\'s environment, so the bot would start logged out there.');
|
|
283
|
+
const save = interactive
|
|
284
|
+
? await p.confirm(`Save ${names} in the .env too (readable by you only)?`, true)
|
|
285
|
+
: true;
|
|
286
|
+
if (!save) {
|
|
287
|
+
p.say(` Not saving it: the bot only logs in where ${names} is set.`);
|
|
288
|
+
return {};
|
|
289
|
+
}
|
|
290
|
+
if (!interactive)
|
|
291
|
+
p.say(` ${names} will go in the .env too (readable by you only), so a service can log in.`);
|
|
292
|
+
return Object.fromEntries(found.map((key) => [key, deps.env[key].trim()]));
|
|
293
|
+
}
|
|
294
|
+
/** Pasted tokens come with stray whitespace or quotes more often than not. */
|
|
295
|
+
export function cleanToken(raw) {
|
|
296
|
+
return raw.trim().replace(/^(['"])(.*)\1$/, '$2').trim();
|
|
297
|
+
}
|
|
298
|
+
async function stepToken(opts, deps, interactive, current) {
|
|
299
|
+
const p = deps.prompter;
|
|
300
|
+
const check = async (token) => {
|
|
301
|
+
const client = deps.makeClient(token);
|
|
302
|
+
const bot = await client.getMe();
|
|
303
|
+
return { token, bot, client };
|
|
304
|
+
};
|
|
305
|
+
if (opts.tokenEnv) {
|
|
306
|
+
const token = cleanToken(deps.env[opts.tokenEnv] ?? '');
|
|
307
|
+
if (!token)
|
|
308
|
+
throw new Stop(`--token-env ${opts.tokenEnv}: that environment variable is empty or not set.`);
|
|
309
|
+
if (!isWellFormedBotToken(token))
|
|
310
|
+
throw new Stop(MALFORMED_TOKEN_MESSAGE);
|
|
311
|
+
try {
|
|
312
|
+
return await check(token);
|
|
313
|
+
}
|
|
314
|
+
catch (error) {
|
|
315
|
+
throw new Stop(describeTelegramError(error));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (!interactive) {
|
|
319
|
+
throw new Stop('No bot token: pass --token-env VAR, naming an environment variable that holds it ' +
|
|
320
|
+
'(not the token itself: arguments show up in ps). Or run "telecoder init" in a terminal.');
|
|
321
|
+
}
|
|
322
|
+
if (current && isWellFormedBotToken(current)) {
|
|
323
|
+
try {
|
|
324
|
+
const found = await check(current);
|
|
325
|
+
if (await p.confirm(`Keep the current token, for @${found.bot.username}?`, true))
|
|
326
|
+
return found;
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
p.say(`The current token doesn't work: ${describeTelegramError(error)}`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
p.say('Bot token. If you don\'t have a bot yet:');
|
|
333
|
+
p.say(' 1. In Telegram, open @BotFather (https://t.me/BotFather) and send /newbot.');
|
|
334
|
+
p.say(' 2. Pick a display name, then a username ending in "bot".');
|
|
335
|
+
p.say(' 3. It replies with a token like 1234567890:AAH…: copy it and paste it here.');
|
|
336
|
+
for (;;) {
|
|
337
|
+
const token = cleanToken(await p.secret('Bot token:'));
|
|
338
|
+
if (!token)
|
|
339
|
+
continue;
|
|
340
|
+
if (!isWellFormedBotToken(token)) {
|
|
341
|
+
p.say(MALFORMED_TOKEN_MESSAGE);
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
for (;;) {
|
|
345
|
+
try {
|
|
346
|
+
return await check(token);
|
|
347
|
+
}
|
|
348
|
+
catch (error) {
|
|
349
|
+
p.say(describeTelegramError(error));
|
|
350
|
+
if (error instanceof TelegramNetworkError && await p.confirm('Try again?', true))
|
|
351
|
+
continue;
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async function stepOwner(opts, deps, interactive, client, bot, current, hasRoles) {
|
|
358
|
+
const p = deps.prompter;
|
|
359
|
+
// Telegram doesn't deliver updates to getUpdates — or to the bot — while a
|
|
360
|
+
// webhook is set, so look first.
|
|
361
|
+
const webhookOk = await checkWebhook(deps, interactive, client, opts.yes ?? false);
|
|
362
|
+
if (opts.ownerId !== undefined) {
|
|
363
|
+
const id = parseOwnerId(opts.ownerId);
|
|
364
|
+
if (typeof id === 'string')
|
|
365
|
+
throw new Stop(`--owner-id: ${id}`);
|
|
366
|
+
return { id, idList: String(id) };
|
|
367
|
+
}
|
|
368
|
+
if (!interactive) {
|
|
369
|
+
throw new Stop('No owner: pass --owner-id with your numeric Telegram user id. Or run "telecoder init" in a terminal, ' +
|
|
370
|
+
'which finds it when you message the bot.');
|
|
371
|
+
}
|
|
372
|
+
if (current) {
|
|
373
|
+
const parsed = parseIdList('ALLOWED_USER_IDS', current);
|
|
374
|
+
if (parsed.ok && parsed.ids.length && await p.confirm(`Keep the allowed users, ${parsed.ids.join(', ')}?`, true)) {
|
|
375
|
+
return { id: parsed.ids[0], idList: parsed.ids.join(',') };
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const otherRoles = hasRoles;
|
|
379
|
+
p.say('Who owns this bot: TeleCoder only answers the Telegram accounts you allow.');
|
|
380
|
+
if (webhookOk) {
|
|
381
|
+
const captured = await captureOwner(deps, client, bot);
|
|
382
|
+
if (captured)
|
|
383
|
+
return { id: captured, idList: String(captured), otherRoles };
|
|
384
|
+
}
|
|
385
|
+
for (;;) {
|
|
386
|
+
const answer = await p.ask('Your numeric Telegram user id (message @userinfobot to get it):');
|
|
387
|
+
const id = parseOwnerId(answer);
|
|
388
|
+
if (typeof id === 'number')
|
|
389
|
+
return { id, idList: String(id), otherRoles };
|
|
390
|
+
p.say(id);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
/** A user id, or why it isn't one. */
|
|
394
|
+
export function parseOwnerId(raw) {
|
|
395
|
+
const trimmed = raw.trim();
|
|
396
|
+
if (!trimmed)
|
|
397
|
+
return 'Type the number, like 412345678.';
|
|
398
|
+
const parsed = parseIdList('ALLOWED_USER_IDS', trimmed);
|
|
399
|
+
if (!parsed.ok)
|
|
400
|
+
return parsed.error;
|
|
401
|
+
if (parsed.ids.length !== 1 || parsed.ids[0] <= 0)
|
|
402
|
+
return 'One user id, a positive number like 412345678.';
|
|
403
|
+
return parsed.ids[0];
|
|
404
|
+
}
|
|
405
|
+
async function checkWebhook(deps, interactive, client, yes) {
|
|
406
|
+
const p = deps.prompter;
|
|
407
|
+
let info;
|
|
408
|
+
try {
|
|
409
|
+
info = await client.getWebhookInfo();
|
|
410
|
+
}
|
|
411
|
+
catch (error) {
|
|
412
|
+
p.say(`! Couldn't check the bot's webhook: ${describeTelegramError(error)}`);
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
if (!info.url)
|
|
416
|
+
return true;
|
|
417
|
+
p.say(`! This bot has a webhook set, to ${redactSecrets(info.url)}: Telegram is sending its messages to`);
|
|
418
|
+
p.say(' another program. TeleCoder fetches them itself, which only works once the webhook is removed.');
|
|
419
|
+
const remove = interactive ? await p.confirm('Remove the webhook?', false) : yes;
|
|
420
|
+
if (!remove) {
|
|
421
|
+
p.say(' Left it in place. TeleCoder won\'t receive messages until it is removed.');
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
await client.deleteWebhook();
|
|
426
|
+
p.say('✓ Removed the webhook');
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
p.say(`! Couldn't remove it: ${describeTelegramError(error)}`);
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
function describeUser(user) {
|
|
435
|
+
const name = [user.first_name, user.last_name].filter(Boolean).join(' ') || 'someone';
|
|
436
|
+
return `${name}${user.username ? ` (@${user.username})` : ''}`;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Wait for the owner to message the bot, and take their id from it. Typing an
|
|
440
|
+
* id instead works at any point. Returns null when nothing usable arrived, for
|
|
441
|
+
* the caller to ask for the id outright.
|
|
442
|
+
*/
|
|
443
|
+
async function captureOwner(deps, client, bot) {
|
|
444
|
+
const p = deps.prompter;
|
|
445
|
+
const waitMs = deps.ownerWaitMs ?? 5 * 60_000;
|
|
446
|
+
const pollTimeout = deps.pollTimeoutSec ?? 30;
|
|
447
|
+
const now = () => (deps.now?.() ?? new Date()).getTime();
|
|
448
|
+
// Whatever is already queued predates this run, and could be anyone's:
|
|
449
|
+
// skip it, so only a message sent from here on counts.
|
|
450
|
+
let offset;
|
|
451
|
+
try {
|
|
452
|
+
const queued = await client.getUpdates({ timeout: 0, signal: p.signal });
|
|
453
|
+
if (queued.length)
|
|
454
|
+
offset = queued[queued.length - 1].update_id + 1;
|
|
455
|
+
}
|
|
456
|
+
catch (error) {
|
|
457
|
+
if (isAbort(error))
|
|
458
|
+
throw error;
|
|
459
|
+
p.say(`! ${describePollError(error)}`);
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
p.say(`Now open https://t.me/${bot.username} and send /start, from your own account.`);
|
|
463
|
+
p.say('The bot replies when it gets it, and this asks you to confirm it was you.');
|
|
464
|
+
p.say(`Waiting for it, up to ${Math.round(waitMs / 60_000)} minutes. Or type your numeric id and press Enter. Ctrl+C cancels.`);
|
|
465
|
+
const deadline = now() + waitMs;
|
|
466
|
+
let found = null;
|
|
467
|
+
while (found === null && now() < deadline) {
|
|
468
|
+
const stopPoll = new AbortController();
|
|
469
|
+
const stopAsk = new AbortController();
|
|
470
|
+
const poll = (async () => {
|
|
471
|
+
while (now() < deadline) {
|
|
472
|
+
const timeout = Math.max(0, Math.min(pollTimeout, Math.ceil((deadline - now()) / 1000)));
|
|
473
|
+
const updates = await client.getUpdates({
|
|
474
|
+
offset, timeout, signal: AbortSignal.any([p.signal, stopPoll.signal]),
|
|
475
|
+
});
|
|
476
|
+
let candidate;
|
|
477
|
+
let chatId = 0;
|
|
478
|
+
for (const u of updates) {
|
|
479
|
+
offset = u.update_id + 1;
|
|
480
|
+
const m = u.message;
|
|
481
|
+
if (!candidate && m?.from && !m.from.is_bot && m.chat.type === 'private') {
|
|
482
|
+
candidate = m.from;
|
|
483
|
+
chatId = m.chat.id;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
if (candidate) {
|
|
487
|
+
// Otherwise Telegram stays silent and the /start looks lost. Best
|
|
488
|
+
// effort: the answer that counts is the one in the terminal.
|
|
489
|
+
await client.sendMessage(chatId, OWNER_CAPTURED_REPLY).catch(() => { });
|
|
490
|
+
return { kind: 'message', user: candidate };
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return { kind: 'timeout' };
|
|
494
|
+
})();
|
|
495
|
+
const typed = p.ask('>', { signal: stopAsk.signal }).then((text) => ({ kind: 'typed', text }));
|
|
496
|
+
let outcome;
|
|
497
|
+
try {
|
|
498
|
+
outcome = await Promise.race([poll, typed]);
|
|
499
|
+
}
|
|
500
|
+
catch (error) {
|
|
501
|
+
stopPoll.abort();
|
|
502
|
+
stopAsk.abort();
|
|
503
|
+
await Promise.allSettled([poll, typed]);
|
|
504
|
+
if (isAbort(error) || p.signal.aborted)
|
|
505
|
+
throw error;
|
|
506
|
+
p.say(`! ${describePollError(error)}`);
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
// Whichever lost is cancelled, and its rejection swallowed.
|
|
510
|
+
if (outcome.kind === 'typed')
|
|
511
|
+
stopPoll.abort();
|
|
512
|
+
else
|
|
513
|
+
stopAsk.abort();
|
|
514
|
+
await Promise.allSettled([poll, typed]);
|
|
515
|
+
if (p.signal.aborted)
|
|
516
|
+
throw p.signal.reason;
|
|
517
|
+
if (outcome.kind === 'typed') {
|
|
518
|
+
const id = parseOwnerId(outcome.text);
|
|
519
|
+
if (typeof id === 'number')
|
|
520
|
+
found = id;
|
|
521
|
+
else
|
|
522
|
+
p.say(id);
|
|
523
|
+
}
|
|
524
|
+
else if (outcome.kind === 'message') {
|
|
525
|
+
const user = outcome.user;
|
|
526
|
+
if (await p.confirm(`Is this you: ${describeUser(user)}, id ${user.id}?`, true))
|
|
527
|
+
found = user.id;
|
|
528
|
+
else
|
|
529
|
+
p.say('Waiting for another message. Or type your id.');
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
p.say('No message arrived in time.');
|
|
533
|
+
break;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
// Mark what was read as handled, or the bot's first poll replays the /start.
|
|
537
|
+
if (offset !== undefined) {
|
|
538
|
+
try {
|
|
539
|
+
await client.getUpdates({ offset, timeout: 0 });
|
|
540
|
+
}
|
|
541
|
+
catch {
|
|
542
|
+
// Harmless: the bot answers one /start twice, at worst.
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return found;
|
|
546
|
+
}
|
|
547
|
+
function describePollError(error) {
|
|
548
|
+
if (error instanceof TelegramApiError && error.code === 409) {
|
|
549
|
+
return 'Something else is already reading this bot\'s messages: probably TeleCoder or another program ' +
|
|
550
|
+
'running with the same token. Stop it, or type your id below.';
|
|
551
|
+
}
|
|
552
|
+
return `Couldn't wait for your message: ${describeTelegramError(error)}`;
|
|
553
|
+
}
|
|
554
|
+
async function stepWorkspace(opts, deps, interactive, current) {
|
|
555
|
+
const p = deps.prompter;
|
|
556
|
+
if (opts.workspace !== undefined) {
|
|
557
|
+
const resolved = resolveDir(opts.workspace, deps.homedir);
|
|
558
|
+
if (typeof resolved !== 'string' || !isDir(resolved))
|
|
559
|
+
throw new Stop(`--workspace: ${opts.workspace} is not a directory.`);
|
|
560
|
+
return resolved;
|
|
561
|
+
}
|
|
562
|
+
const def = current ?? suggestWorkspace(deps.homedir);
|
|
563
|
+
if (!interactive)
|
|
564
|
+
return def;
|
|
565
|
+
p.say('Workspace: the folder your projects are in. /project picks one of them for Claude to work in,');
|
|
566
|
+
p.say('and admins can fetch files from anywhere inside it with /file.');
|
|
567
|
+
for (;;) {
|
|
568
|
+
const answer = await p.ask('Workspace directory:', { default: tildify(def, deps.homedir) });
|
|
569
|
+
const resolved = resolveDir(answer, deps.homedir);
|
|
570
|
+
if (isDir(resolved))
|
|
571
|
+
return resolved;
|
|
572
|
+
p.say(`${resolved} is not a directory. Create it first, or pick another.`);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
/** ~/projects or the like when there is one, else the home directory. */
|
|
576
|
+
export function suggestWorkspace(homedir, exists = isDir) {
|
|
577
|
+
for (const name of ['projects', 'Projects', 'dev', 'code', 'src', 'repos']) {
|
|
578
|
+
const dir = path.join(homedir, name);
|
|
579
|
+
if (exists(dir))
|
|
580
|
+
return dir;
|
|
581
|
+
}
|
|
582
|
+
return homedir;
|
|
583
|
+
}
|
|
584
|
+
function resolveDir(input, homedir) {
|
|
585
|
+
const trimmed = input.trim();
|
|
586
|
+
const expanded = trimmed === '~' ? homedir : trimmed.startsWith('~/') ? path.join(homedir, trimmed.slice(2)) : trimmed;
|
|
587
|
+
return path.resolve(expanded);
|
|
588
|
+
}
|
|
589
|
+
function tildify(dir, homedir) {
|
|
590
|
+
if (dir === homedir)
|
|
591
|
+
return '~';
|
|
592
|
+
return dir.startsWith(homedir + path.sep) ? `~/${dir.slice(homedir.length + 1)}` : dir;
|
|
593
|
+
}
|
|
594
|
+
function isDir(dir) {
|
|
595
|
+
try {
|
|
596
|
+
return fs.statSync(dir).isDirectory();
|
|
597
|
+
}
|
|
598
|
+
catch {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
async function stepVoice(opts, deps, interactive, current) {
|
|
603
|
+
const p = deps.prompter;
|
|
604
|
+
if (opts.groqKeyEnv) {
|
|
605
|
+
const key = deps.env[opts.groqKeyEnv]?.trim();
|
|
606
|
+
if (!key)
|
|
607
|
+
throw new Stop(`--groq-key-env ${opts.groqKeyEnv}: that environment variable is empty or not set.`);
|
|
608
|
+
return key;
|
|
609
|
+
}
|
|
610
|
+
if (!interactive)
|
|
611
|
+
return current;
|
|
612
|
+
const on = await p.confirm('Transcribe voice messages? Needs a Groq API key (https://console.groq.com/keys; it has a free tier).', Boolean(current));
|
|
613
|
+
if (!on)
|
|
614
|
+
return undefined;
|
|
615
|
+
if (current && await p.confirm('Keep the current Groq key?', true))
|
|
616
|
+
return current;
|
|
617
|
+
for (;;) {
|
|
618
|
+
const key = (await p.secret('Groq API key:')).trim();
|
|
619
|
+
if (key) {
|
|
620
|
+
if (!key.startsWith('gsk_'))
|
|
621
|
+
p.say('! Groq keys usually start with gsk_; using it anyway.');
|
|
622
|
+
return key;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
/** One sentence about a failed Bot API call, in the wording the bot itself uses. */
|
|
627
|
+
export function describeTelegramError(error) {
|
|
628
|
+
if (error instanceof TelegramApiError) {
|
|
629
|
+
if (error.method === 'getMe' && (error.code === 401 || error.code === 404))
|
|
630
|
+
return REJECTED_TOKEN_MESSAGE;
|
|
631
|
+
return `Telegram said: ${error.description}`;
|
|
632
|
+
}
|
|
633
|
+
if (error instanceof TelegramNetworkError) {
|
|
634
|
+
return `Couldn't reach api.telegram.org (${error.message}). Check the network connection, and try again.`;
|
|
635
|
+
}
|
|
636
|
+
return redactSecrets(error instanceof Error ? error.message : String(error));
|
|
637
|
+
}
|
|
638
|
+
//# sourceMappingURL=flow.js.map
|