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,450 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Putting a Claude Code dialog in the chat, and its answer back in the pty.
|
|
3
|
+
*
|
|
4
|
+
* The pty has no keyboard, so a dialog claude opens blocks forever unless
|
|
5
|
+
* someone answers it. Guessing the answer is not an option — the readiness
|
|
6
|
+
* check used to guess by accident, and pressing Enter at a dialog it had
|
|
7
|
+
* mistaken for the input box is enough to change a setting. So the dialog goes
|
|
8
|
+
* to the person the session belongs to, as buttons, and their tap becomes the
|
|
9
|
+
* keystrokes.
|
|
10
|
+
*
|
|
11
|
+
* Three rules the flow is built around:
|
|
12
|
+
*
|
|
13
|
+
* - Only offer what the dialog offers. Buttons come from its numbered rows
|
|
14
|
+
* and from the keys its own footer names; nothing is inferred. A dialog we
|
|
15
|
+
* cannot parse gets relayed as text with no buttons at all.
|
|
16
|
+
* - Confirm the cursor before committing. Arrow keys are written first and
|
|
17
|
+
* the screen re-read; Enter follows only once the glyph is on the intended
|
|
18
|
+
* row. A dialog that redrew under us, or moved in a way we did not model,
|
|
19
|
+
* stops here instead of committing the wrong row.
|
|
20
|
+
* - Silence presses nothing. On timeout the dialog is left exactly as it
|
|
21
|
+
* was. An unanswered dialog is a prompt that fails; a guessed one is a
|
|
22
|
+
* setting that changed, and only the second is unrecoverable.
|
|
23
|
+
*
|
|
24
|
+
* A dialog can also take more than one answer. `/mcp` is a menu several
|
|
25
|
+
* screens deep — its servers, then one server's actions, then that server's
|
|
26
|
+
* tools — and a tap that opens the next screen hasn't finished with the
|
|
27
|
+
* dialog, it has moved it on. So the relay follows it ({@link relayDialog})
|
|
28
|
+
* until claude is back at its input box, offering each screen as it offered
|
|
29
|
+
* the first.
|
|
30
|
+
*/
|
|
31
|
+
import * as path from 'path';
|
|
32
|
+
import { closeExpiredQuestion, createPendingQuestion, questionTimeoutMinutes } from './ask-user.js';
|
|
33
|
+
import { arrowsTo, keystrokeFor, parseModal } from './tui-modal.js';
|
|
34
|
+
import { hasInputBox, screenSignature } from './tui-state.js';
|
|
35
|
+
import { EntityText } from '../telegram/entities.js';
|
|
36
|
+
import { parseSessionKey } from '../utils/session-key.js';
|
|
37
|
+
import { sanitizeError } from '../utils/sanitize.js';
|
|
38
|
+
/** Settle time after writing a key, before re-reading the screen. */
|
|
39
|
+
const KEY_SETTLE_MS = 400;
|
|
40
|
+
/**
|
|
41
|
+
* Gap between the arrow keys of one walk, so each arrives as a keystroke of
|
|
42
|
+
* its own. Short, because a walk can be long: a list that scrolls is a page of
|
|
43
|
+
* eighteen rows, and a page scroll is that many arrows twice. Measured against
|
|
44
|
+
* a live 2.1.310 pty, eighteen arrows sent back to back, and ten in a single
|
|
45
|
+
* write, all landed. The screen is re-read for where the cursor ended up
|
|
46
|
+
* either way.
|
|
47
|
+
*/
|
|
48
|
+
const ARROW_GAP_MS = 20;
|
|
49
|
+
/** How often to look at the screen while waiting to see what a press did. */
|
|
50
|
+
const PRESS_POLL_MS = 100;
|
|
51
|
+
/** How long one arrow of a scroll gets to show up on screen before the scroll gives up. */
|
|
52
|
+
const REDRAW_MS = 500;
|
|
53
|
+
/**
|
|
54
|
+
* Most rows offered as buttons: a whole screenful, which at 40 rows is about
|
|
55
|
+
* twenty. A list longer than that scrolls, and its scroll buttons reach the
|
|
56
|
+
* rest.
|
|
57
|
+
*/
|
|
58
|
+
const MAX_ROW_BUTTONS = 20;
|
|
59
|
+
/**
|
|
60
|
+
* The buttons a dialog earns. Numbered rows first, in the dialog's own order,
|
|
61
|
+
* then the footer keys that are answers rather than navigation — `Esc to
|
|
62
|
+
* cancel` is worth a button, `↑/↓ to select` is not, because the relay does
|
|
63
|
+
* the navigating. Enter is dropped when there are rows to pick, since picking
|
|
64
|
+
* a row already presses it.
|
|
65
|
+
*
|
|
66
|
+
* Doing the navigating includes scrolling. A list that runs past the screen
|
|
67
|
+
* gets a scroll button at each end it continues beyond, placed at that end,
|
|
68
|
+
* and each row it draws gets a button of its own. The cap of seven this used
|
|
69
|
+
* to have left `/mcp`'s 111-tool list reachable as far as its seventh tool.
|
|
70
|
+
*/
|
|
71
|
+
export function buildChoices(modal) {
|
|
72
|
+
const rows = [];
|
|
73
|
+
if (modal.highlighted >= 0) {
|
|
74
|
+
for (const option of modal.options.slice(0, MAX_ROW_BUTTONS)) {
|
|
75
|
+
const prefix = option.number === undefined ? '' : `${option.number}. `;
|
|
76
|
+
rows.push({ kind: 'row', label: `${prefix}${option.label}`, index: option.index });
|
|
77
|
+
}
|
|
78
|
+
if (modal.more.above > 0) {
|
|
79
|
+
rows.unshift({ kind: 'page', label: `↑ Scroll up (${modal.more.above} more)`, direction: 'up' });
|
|
80
|
+
}
|
|
81
|
+
if (modal.more.below > 0) {
|
|
82
|
+
rows.push({ kind: 'page', label: `↓ Scroll down (${modal.more.below} more)`, direction: 'down' });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const keys = [];
|
|
86
|
+
for (const hint of modal.hints) {
|
|
87
|
+
if (rows.length > 0 && /^Enter/.test(hint.key))
|
|
88
|
+
continue;
|
|
89
|
+
const key = keystrokeFor(hint.key);
|
|
90
|
+
if (key === null)
|
|
91
|
+
continue;
|
|
92
|
+
keys.push({ kind: 'key', label: `${hint.key} — ${hint.action}`, key });
|
|
93
|
+
}
|
|
94
|
+
return [...rows, ...keys];
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The message above the buttons: claude's dialog, laid out for a phone.
|
|
98
|
+
*
|
|
99
|
+
* The screen does not travel well as it is. Claude hard-wraps it at 120
|
|
100
|
+
* columns, which a phone then wraps again; the indentation and the gutter that
|
|
101
|
+
* set a command apart mean nothing in a proportional font; and the rows repeat
|
|
102
|
+
* the buttons with a cursor glyph nobody can move. So the body goes out as the
|
|
103
|
+
* parts {@link parseModal} read it into — title and question in bold, drawn
|
|
104
|
+
* content in a monospace block, each row once in full, since its button is
|
|
105
|
+
* clipped.
|
|
106
|
+
*
|
|
107
|
+
* Entities rather than a parse mode: the body is claude's own render, and the
|
|
108
|
+
* underscores and asterisks of whatever command it is asking about would be
|
|
109
|
+
* read as Markdown and 400 the send.
|
|
110
|
+
*/
|
|
111
|
+
export function buildModalMessage(modal, cwd, stage) {
|
|
112
|
+
const b = new EntityText();
|
|
113
|
+
b.add('⌨️ ').bold('Claude Code needs an answer').newline();
|
|
114
|
+
b.add('Project: ').bold(path.basename(cwd) || cwd).newline(2);
|
|
115
|
+
appendDialog(b, modal);
|
|
116
|
+
return b.newline(2).italic(closingLine(modal, stage)).build();
|
|
117
|
+
}
|
|
118
|
+
function appendDialog(b, modal) {
|
|
119
|
+
const { parts } = modal;
|
|
120
|
+
// The line the rows answer — "Do you want to proceed?" — sits right above them.
|
|
121
|
+
const question = parts.findIndex((p) => p.kind === 'options') - 1;
|
|
122
|
+
parts.forEach((part, i) => {
|
|
123
|
+
if (i > 0)
|
|
124
|
+
b.newline(part.gapBefore ? 2 : 1);
|
|
125
|
+
if (part.kind === 'verbatim') {
|
|
126
|
+
b.pre(part.lines.join('\n'));
|
|
127
|
+
}
|
|
128
|
+
else if (part.kind === 'options') {
|
|
129
|
+
const { above, below } = modal.more;
|
|
130
|
+
[
|
|
131
|
+
...(above > 0 ? [`↑ ${above} more`] : []),
|
|
132
|
+
...modal.options.map((o) => {
|
|
133
|
+
const key = o.number === undefined ? '•' : `${o.number}.`;
|
|
134
|
+
return `${key} ${o.text}${o.detail ? ` — ${o.detail}` : ''}`;
|
|
135
|
+
}),
|
|
136
|
+
...(below > 0 ? [`↓ ${below} more`] : []),
|
|
137
|
+
].forEach((row, n) => b.newline(n > 0 ? 1 : 0).add(row));
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
part.lines.forEach((line, n) => {
|
|
141
|
+
const heading = (i === 0 && n === 0)
|
|
142
|
+
|| (i === question && n === part.lines.length - 1 && line.endsWith('?'));
|
|
143
|
+
b.newline(n > 0 ? 1 : 0);
|
|
144
|
+
if (heading)
|
|
145
|
+
b.bold(line);
|
|
146
|
+
else
|
|
147
|
+
b.add(line);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Why the turn stops at a dialog that is still open, for the chat.
|
|
154
|
+
*
|
|
155
|
+
* Where the turn stands decides what's true. Before a prompt, the user's
|
|
156
|
+
* message is what didn't happen: it was never typed in, and sending it again
|
|
157
|
+
* is the fix. Mid-turn it was delivered long before, and saying otherwise sent
|
|
158
|
+
* people off to resend a message claude already had. What stops there is the
|
|
159
|
+
* bot waiting on the turn, while claude stays paused on the dialog — and the
|
|
160
|
+
* next message meets it before anything else, with its buttons.
|
|
161
|
+
*/
|
|
162
|
+
export function openDialogError(dialog, stage) {
|
|
163
|
+
if (stage === 'before-prompt') {
|
|
164
|
+
switch (dialog.kind) {
|
|
165
|
+
case 'still-open':
|
|
166
|
+
return `I pressed "${dialog.label}", but Claude Code hasn't returned to its input box, `
|
|
167
|
+
+ "so your message wasn't delivered. Send it again in a moment.";
|
|
168
|
+
case 'timeout':
|
|
169
|
+
return "Nobody answered Claude Code's dialog, so I left it alone and your message wasn't "
|
|
170
|
+
+ "delivered. Send it again to get the dialog's buttons back.";
|
|
171
|
+
default:
|
|
172
|
+
return `Your message wasn't delivered — ${dialog.why}. Claude Code is still waiting on that dialog.`;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
switch (dialog.kind) {
|
|
176
|
+
case 'still-open':
|
|
177
|
+
return `I pressed "${dialog.label}", but Claude Code's dialog is still open, so I stopped `
|
|
178
|
+
+ 'waiting on this turn. Your next message brings its buttons back.';
|
|
179
|
+
case 'timeout':
|
|
180
|
+
return "Nobody answered Claude Code's dialog, so I left it alone and stopped waiting on "
|
|
181
|
+
+ 'this turn. Claude Code is paused on it until your next message, which brings its buttons back.';
|
|
182
|
+
default:
|
|
183
|
+
return `I stopped waiting on this turn — ${dialog.why}. Claude Code is still paused on that dialog.`;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/** What a tap does, and what happens once it has. */
|
|
187
|
+
function closingLine(modal, stage) {
|
|
188
|
+
if (buildChoices(modal).length === 0) {
|
|
189
|
+
return 'It offers no key I can press from here, so it needs someone at the terminal.';
|
|
190
|
+
}
|
|
191
|
+
return stage === 'before-prompt'
|
|
192
|
+
? "Tap your answer and I'll press it in Claude Code, then send your message."
|
|
193
|
+
: "Tap your answer and I'll press it in Claude Code so it can carry on.";
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Relay the dialog on screen to the chat and press whatever comes back — one
|
|
197
|
+
* screen of it; {@link relayDialog} follows it through the rest.
|
|
198
|
+
*
|
|
199
|
+
* Returns without pressing anything when the dialog cannot be parsed, when it
|
|
200
|
+
* offers nothing pressable, or when nobody answers — the caller turns each of
|
|
201
|
+
* those into a message saying the prompt was not delivered, which is the
|
|
202
|
+
* honest outcome and leaves the dialog for a person to deal with.
|
|
203
|
+
*/
|
|
204
|
+
export async function relayModal(pty, sessionKey, ctx, cwd, stage) {
|
|
205
|
+
const modal = parseModal(pty.screen());
|
|
206
|
+
if (!modal)
|
|
207
|
+
return { kind: 'declined', why: 'the screen could not be read as a dialog' };
|
|
208
|
+
const choices = buildChoices(modal);
|
|
209
|
+
const labels = [...choices.map((c) => c.label), '✋ Leave it alone'];
|
|
210
|
+
const message = buildModalMessage(modal, cwd, stage);
|
|
211
|
+
if (choices.length === 0) {
|
|
212
|
+
await send(ctx, sessionKey, message, undefined);
|
|
213
|
+
return { kind: 'declined', why: 'the dialog offers no keys this bot can press' };
|
|
214
|
+
}
|
|
215
|
+
// Waits QUESTION_TIMEOUT_MINUTES, like any other question the turn asks.
|
|
216
|
+
const { id, promise } = createPendingQuestion(labels, undefined, sessionKey);
|
|
217
|
+
const keyboard = labels.map((label, idx) => [{ text: label, callback_data: `q:${id}:${idx}` }]);
|
|
218
|
+
const { sent, entities } = await send(ctx, sessionKey, message, keyboard);
|
|
219
|
+
const answer = await promise;
|
|
220
|
+
if (!answer) {
|
|
221
|
+
await closeExpiredQuestion(ctx.api, sent, message.text, `No answer within ${questionTimeoutMinutes()} min, so I left the dialog alone.`, entities);
|
|
222
|
+
return { kind: 'timeout' };
|
|
223
|
+
}
|
|
224
|
+
const choice = choices[answer.index];
|
|
225
|
+
if (!choice)
|
|
226
|
+
return { kind: 'declined', why: 'you chose to leave it alone' };
|
|
227
|
+
// The tap can come minutes after the message went out. A key meant for this
|
|
228
|
+
// dialog means something else anywhere else — at the input box, `s` is a
|
|
229
|
+
// letter typed into the next message — so look again before pressing it.
|
|
230
|
+
const screen = pty.screen();
|
|
231
|
+
const current = hasInputBox(screen) ? null : parseModal(screen);
|
|
232
|
+
if (!current || !sameDialog(current, modal)) {
|
|
233
|
+
return { kind: 'failed', why: 'the dialog changed before your answer reached it' };
|
|
234
|
+
}
|
|
235
|
+
if (choice.kind === 'key') {
|
|
236
|
+
pty.write(choice.key);
|
|
237
|
+
return { kind: 'answered', label: choice.label, dialog: current };
|
|
238
|
+
}
|
|
239
|
+
if (choice.kind === 'page')
|
|
240
|
+
return scroll(pty, current, choice.direction, choice.label);
|
|
241
|
+
return pressOption(pty, current, choice.index, choice.label);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* True when two reads are of the same dialog, whatever has changed inside it.
|
|
245
|
+
* A row can be redrawn while the dialog waits for its answer without changing
|
|
246
|
+
* what that answer presses. A different heading, a different number of rows
|
|
247
|
+
* or different keys in the footer would.
|
|
248
|
+
*/
|
|
249
|
+
function sameDialog(a, b) {
|
|
250
|
+
return a.title === b.title && a.options.length === b.options.length && sameHints(a, b);
|
|
251
|
+
}
|
|
252
|
+
/** True when two reads show the dialog exactly as it was: same rows, same cursor, same keys. */
|
|
253
|
+
function drawnAlike(a, b) {
|
|
254
|
+
return screenSignature(a.body) === screenSignature(b.body) && sameHints(a, b);
|
|
255
|
+
}
|
|
256
|
+
function sameHints(a, b) {
|
|
257
|
+
return a.hints.length === b.hints.length
|
|
258
|
+
&& a.hints.every((hint, i) => hint.key === b.hints[i].key && hint.action === b.hints[i].action);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Walk the cursor to `index` and commit — but only after seeing it arrive.
|
|
262
|
+
*
|
|
263
|
+
* The re-read is the whole point. Arrow keys are a bet that the dialog moves
|
|
264
|
+
* its cursor the way every dialog we measured moves it, and a bet that is
|
|
265
|
+
* wrong must not end in Enter: a dialog whose cursor did not land where we
|
|
266
|
+
* asked is one where Enter commits an option nobody chose.
|
|
267
|
+
*/
|
|
268
|
+
async function pressOption(pty, modal, index, label) {
|
|
269
|
+
const arrows = arrowsTo(modal, index);
|
|
270
|
+
if (arrows === null)
|
|
271
|
+
return { kind: 'failed', why: 'the dialog\'s cursor position could not be read' };
|
|
272
|
+
for (const arrow of arrows) {
|
|
273
|
+
pty.write(arrow);
|
|
274
|
+
await delay(ARROW_GAP_MS);
|
|
275
|
+
}
|
|
276
|
+
await delay(KEY_SETTLE_MS);
|
|
277
|
+
const landed = parseModal(pty.screen());
|
|
278
|
+
if (!landed)
|
|
279
|
+
return { kind: 'failed', why: 'the dialog changed shape while being answered' };
|
|
280
|
+
// Which row the cursor is on is read off what the row says. A list that
|
|
281
|
+
// scrolls moves its rows under the cursor: past its first screen, `/mcp`'s
|
|
282
|
+
// tool list keeps the cursor on its bottom row and shifts the rest, so the
|
|
283
|
+
// row asked for is on screen but never at the index it was asked for at. A
|
|
284
|
+
// list that didn't scroll has to match on the index too, which is what
|
|
285
|
+
// tells apart two rows that read the same.
|
|
286
|
+
const wanted = modal.options[index];
|
|
287
|
+
const reached = landed.options[landed.highlighted];
|
|
288
|
+
const scrolled = landed.more.above !== modal.more.above || landed.more.below !== modal.more.below;
|
|
289
|
+
if (!reached || !sameRow(reached, wanted) || (!scrolled && landed.highlighted !== index)) {
|
|
290
|
+
return {
|
|
291
|
+
kind: 'failed',
|
|
292
|
+
why: `the cursor stopped on "${reached?.label ?? 'no row'}", not "${wanted.label}"`,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
pty.write('\r');
|
|
296
|
+
return { kind: 'answered', label, dialog: landed };
|
|
297
|
+
}
|
|
298
|
+
function sameRow(a, b) {
|
|
299
|
+
return a.number === b.number && a.text === b.text;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Scroll a long list on by a screenful less one row. The one row is what
|
|
303
|
+
* keeps the screens joined: a list that has scrolled draws a marker at the
|
|
304
|
+
* end it came from, in a row's place, and a whole screenful would carry the
|
|
305
|
+
* row beside the old screen past without ever drawing it.
|
|
306
|
+
*
|
|
307
|
+
* One arrow at a time, with the list read after each, until its `↓ 92 more`
|
|
308
|
+
* has come down by that much. Counting the presses ahead of time would mean
|
|
309
|
+
* knowing how the list scrolls, and lists don't agree: some move the cursor
|
|
310
|
+
* to their edge before the rows move at all, while `/mcp`'s tool list, once
|
|
311
|
+
* past its first screen, moves every row on every press. Nothing is committed
|
|
312
|
+
* either way, so nothing needs confirming. Wherever the list ends up is the
|
|
313
|
+
* next screen, and it is relayed like any other.
|
|
314
|
+
*/
|
|
315
|
+
async function scroll(pty, modal, direction, label) {
|
|
316
|
+
const hidden = (m) => (direction === 'down' ? m.more.below : m.more.above);
|
|
317
|
+
if (modal.highlighted < 0 || hidden(modal) === 0) {
|
|
318
|
+
return { kind: 'failed', why: 'the list had nothing further to scroll to' };
|
|
319
|
+
}
|
|
320
|
+
const arrow = direction === 'down' ? '\x1b[B' : '\x1b[A';
|
|
321
|
+
const goal = Math.max(0, hidden(modal) - Math.max(1, modal.options.length - 1));
|
|
322
|
+
// A walk to the edge and a page past it is the most this takes. A list
|
|
323
|
+
// still short of the goal after that isn't scrolling the way it says.
|
|
324
|
+
const most = 2 * modal.options.length + hidden(modal);
|
|
325
|
+
let now = modal;
|
|
326
|
+
for (let presses = 0; hidden(now) > goal && presses < most; presses++) {
|
|
327
|
+
pty.write(arrow);
|
|
328
|
+
const next = await redrawn(pty, now);
|
|
329
|
+
if (!next)
|
|
330
|
+
return { kind: 'failed', why: 'the list stopped moving while being scrolled' };
|
|
331
|
+
now = next;
|
|
332
|
+
}
|
|
333
|
+
return { kind: 'answered', label, dialog: modal };
|
|
334
|
+
}
|
|
335
|
+
/** The dialog once the screen has redrawn after a key, or null if it doesn't redraw in time. */
|
|
336
|
+
async function redrawn(pty, before) {
|
|
337
|
+
const deadline = Date.now() + REDRAW_MS;
|
|
338
|
+
while (Date.now() < deadline) {
|
|
339
|
+
await delay(ARROW_GAP_MS);
|
|
340
|
+
const screen = pty.screen();
|
|
341
|
+
const next = hasInputBox(screen) ? null : parseModal(screen);
|
|
342
|
+
if (next && !drawnAlike(next, before))
|
|
343
|
+
return next;
|
|
344
|
+
}
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* The dialog standing between claude and the end of a turn, or null.
|
|
349
|
+
*
|
|
350
|
+
* Both halves matter. Claude draws its input box for the whole of a working
|
|
351
|
+
* turn, so the box being *gone* is what separates a dialog from ordinary
|
|
352
|
+
* progress; and an ordinary footer (`⏵⏵ bypass permissions on · ← for agents`)
|
|
353
|
+
* parses as no dialog, so a half-drawn frame can't be mistaken for one.
|
|
354
|
+
*
|
|
355
|
+
* Split out from the caller because this is the judgement that decides whether
|
|
356
|
+
* a turn gets interrupted, and it is worth being able to point real screens at.
|
|
357
|
+
*/
|
|
358
|
+
export function blockingModal(screenText) {
|
|
359
|
+
if (hasInputBox(screenText))
|
|
360
|
+
return null;
|
|
361
|
+
return parseModal(screenText);
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Relay a dialog, and keep relaying it for as long as answering it leads on
|
|
365
|
+
* to more of it.
|
|
366
|
+
*
|
|
367
|
+
* Every press used to be expected to close the dialog. In `/mcp` that holds
|
|
368
|
+
* only for the last one: picking a server opens that server's screen, `View
|
|
369
|
+
* tools` opens its tool list, and Esc there steps back up a level rather than
|
|
370
|
+
* out. The first tap left the menu on whatever screen it opened and ended the
|
|
371
|
+
* turn calling the dialog still open — so a menu could be opened from the
|
|
372
|
+
* chat, and never used. What a press leads to is looked at instead: claude
|
|
373
|
+
* back at its input box ends the relay, a screen that differs from the one
|
|
374
|
+
* answered is relayed in its turn, and only a press that changes nothing
|
|
375
|
+
* leaves the dialog counted as still open.
|
|
376
|
+
*
|
|
377
|
+
* No count caps the screens. Each one waits on a tap, and each offers "Leave
|
|
378
|
+
* it alone", so the person answering decides when it ends.
|
|
379
|
+
*/
|
|
380
|
+
export async function relayDialog(pty, sessionKey, ctx, cwd, stage, timing) {
|
|
381
|
+
for (;;) {
|
|
382
|
+
const outcome = await relayModal(pty, sessionKey, ctx, cwd, stage);
|
|
383
|
+
if (outcome.kind !== 'answered')
|
|
384
|
+
return outcome;
|
|
385
|
+
const next = await afterPress(pty, outcome.dialog, timing);
|
|
386
|
+
if (next === 'closed')
|
|
387
|
+
return { kind: 'closed', label: outcome.label };
|
|
388
|
+
if (next === 'unchanged')
|
|
389
|
+
return { kind: 'still-open', label: outcome.label };
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Where a press left the dialog: closed, moved on to a screen of its own, or
|
|
394
|
+
* still showing exactly what was answered when the deadline came.
|
|
395
|
+
*
|
|
396
|
+
* Closed counts the moment the input box is back, as it always has. A new
|
|
397
|
+
* screen has to stand still first, as a screen must before the readiness wait
|
|
398
|
+
* will read it: a read taken while claude is still drawing can catch a frame
|
|
399
|
+
* halfway, and buttons built from it would be for a dialog that was never on
|
|
400
|
+
* screen. Only a press that changed nothing at all waits out the deadline.
|
|
401
|
+
*/
|
|
402
|
+
export async function afterPress(pty, pressed, timing) {
|
|
403
|
+
const deadline = Date.now() + timing.clearMs;
|
|
404
|
+
let seen = null;
|
|
405
|
+
let stillSince = Date.now();
|
|
406
|
+
while (Date.now() < deadline) {
|
|
407
|
+
const screen = pty.screen();
|
|
408
|
+
if (hasInputBox(screen))
|
|
409
|
+
return 'closed';
|
|
410
|
+
const signature = screenSignature(screen);
|
|
411
|
+
if (signature !== seen) {
|
|
412
|
+
seen = signature;
|
|
413
|
+
stillSince = Date.now();
|
|
414
|
+
}
|
|
415
|
+
else if (Date.now() - stillSince >= timing.settleMs) {
|
|
416
|
+
const modal = parseModal(screen);
|
|
417
|
+
if (modal && !drawnAlike(modal, pressed))
|
|
418
|
+
return 'moved';
|
|
419
|
+
}
|
|
420
|
+
await delay(PRESS_POLL_MS);
|
|
421
|
+
}
|
|
422
|
+
return 'unchanged';
|
|
423
|
+
}
|
|
424
|
+
function delay(ms) {
|
|
425
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Send the dialog, and say which entities it went out with — closing it later
|
|
429
|
+
* has to repeat them, or the edit strips the formatting.
|
|
430
|
+
*/
|
|
431
|
+
async function send(ctx, sessionKey, message, keyboard) {
|
|
432
|
+
const { chatId, threadId } = parseSessionKey(sessionKey);
|
|
433
|
+
const extra = {
|
|
434
|
+
...(keyboard ? { reply_markup: { inline_keyboard: keyboard } } : {}),
|
|
435
|
+
...(threadId !== undefined ? { message_thread_id: threadId } : {}),
|
|
436
|
+
};
|
|
437
|
+
try {
|
|
438
|
+
const sent = await ctx.api.sendMessage(chatId, message.text, { ...extra, entities: message.entities });
|
|
439
|
+
return { sent, entities: message.entities };
|
|
440
|
+
}
|
|
441
|
+
catch (err) {
|
|
442
|
+
// Nothing in the text is parsed, so if Telegram refused it, an entity is
|
|
443
|
+
// what it refused — and that shouldn't cost the dialog its buttons, the
|
|
444
|
+
// only way to answer it from the chat. Anything else, like a network
|
|
445
|
+
// failure, fails the plain send too and surfaces as it always did.
|
|
446
|
+
console.warn('[ModalRelay] formatted dialog refused, sending it plain:', sanitizeError(err));
|
|
447
|
+
return { sent: await ctx.api.sendMessage(chatId, message.text, extra) };
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
//# sourceMappingURL=modal-relay.js.map
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `--model` aliases Claude Code accepts, shared by every provider that
|
|
3
|
+
* ultimately spawns the CLI.
|
|
4
|
+
*
|
|
5
|
+
* Both PTY and SDK mode hand the alias to a `claude` binary (`--model <alias>`),
|
|
6
|
+
* so the list belongs to the CLI, not to us — mirror `claude --help` here when
|
|
7
|
+
* upstream adds one. Aliases outside the CLI's own set are treated as literal
|
|
8
|
+
* API model names, which means a wrong entry fails at request time rather than
|
|
9
|
+
* at spawn.
|
|
10
|
+
*/
|
|
11
|
+
import { getClaudeVersion, isAtLeast } from '../utils/claude-version.js';
|
|
12
|
+
export const CLAUDE_MODELS = [
|
|
13
|
+
{ id: 'fable', label: 'fable', description: 'Most capable — hardest, longest-running tasks' },
|
|
14
|
+
{ id: 'opus', label: 'opus', description: 'Most capable of the Opus tier (default)' },
|
|
15
|
+
{ id: 'sonnet', label: 'sonnet', description: 'Balanced' },
|
|
16
|
+
{ id: 'haiku', label: 'haiku', description: 'Fast & light' },
|
|
17
|
+
{ id: 'opusplan', label: 'opusplan', description: 'Opus while planning, Sonnet otherwise' },
|
|
18
|
+
{ id: 'best', label: 'best', description: 'Tracks whichever model is currently top-tier' },
|
|
19
|
+
{ id: 'fable[1m]', label: 'fable[1m]', description: 'fable with a 1M-token context window' },
|
|
20
|
+
{ id: 'opus[1m]', label: 'opus[1m]', description: 'opus with a 1M-token context window' },
|
|
21
|
+
{ id: 'sonnet[1m]', label: 'sonnet[1m]', description: 'sonnet with a 1M-token context window' },
|
|
22
|
+
];
|
|
23
|
+
/**
|
|
24
|
+
* Oldest release verified to expose the `fable` aliases. Deliberately
|
|
25
|
+
* conservative: 2.1.140 (shipped inside claude-agent-sdk 0.2.140) knows only
|
|
26
|
+
* sonnet/opus/haiku/best/opusplan and the two [1m] variants, 2.1.220 knows
|
|
27
|
+
* fable, and the exact release in between wasn't worth bisecting. Erring high
|
|
28
|
+
* hides a working alias; erring low breaks the user's next turn.
|
|
29
|
+
*/
|
|
30
|
+
export const FABLE_MIN_CLI_VERSION = [2, 1, 220];
|
|
31
|
+
const FABLE_ALIASES = new Set(['fable', 'fable[1m]']);
|
|
32
|
+
/**
|
|
33
|
+
* True for a model that burns through the plan's limits much faster than the
|
|
34
|
+
* default, so the bot keeps it in sight while it's on. Matches the aliases as
|
|
35
|
+
* well as full IDs (`claude-fable-5-1`, `anthropic/claude-fable-...`), since
|
|
36
|
+
* the status line gets the ID the API reports, not the alias that was picked.
|
|
37
|
+
*/
|
|
38
|
+
export function isPremiumModel(model) {
|
|
39
|
+
return !!model && /fable/i.test(model);
|
|
40
|
+
}
|
|
41
|
+
/** The badge a premium model gets wherever the bot names the model. */
|
|
42
|
+
export const PREMIUM_MODEL_ICON = '💎';
|
|
43
|
+
/**
|
|
44
|
+
* Shape check for a model string typed by hand rather than picked from the
|
|
45
|
+
* catalog — full IDs (`claude-opus-4-8`), dated Vertex snapshots
|
|
46
|
+
* (`claude-opus-4-5@20251101`), Bedrock ARNs and CCR's `vendor/model` and
|
|
47
|
+
* `vendor,model` forms all need to pass through untouched.
|
|
48
|
+
*
|
|
49
|
+
* This is a safety check, not a validity check: whether the ID exists is the
|
|
50
|
+
* CLI's and the API's business. What it does guarantee is that the value can't
|
|
51
|
+
* be mistaken for something other than a model name — leading `-` would make
|
|
52
|
+
* `--model <value>` swallow it as a flag, and whitespace would split the argv
|
|
53
|
+
* entry.
|
|
54
|
+
*/
|
|
55
|
+
const PASSTHROUGH_MODEL_RE = /^[A-Za-z0-9][A-Za-z0-9._/@,:[\]-]{0,99}$/;
|
|
56
|
+
export function isPassthroughModelId(value) {
|
|
57
|
+
return PASSTHROUGH_MODEL_RE.test(value);
|
|
58
|
+
}
|
|
59
|
+
/** Split out so it can be unit-tested without spawning a binary. */
|
|
60
|
+
export function filterModelsForVersion(models, supportsFable) {
|
|
61
|
+
if (supportsFable)
|
|
62
|
+
return models;
|
|
63
|
+
return models.filter((m) => !FABLE_ALIASES.has(m.id));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The aliases `bin` can actually serve. A failed probe is treated as "too old"
|
|
67
|
+
* so an unreadable binary costs the user a menu entry rather than a turn.
|
|
68
|
+
*/
|
|
69
|
+
export async function getModelsForBinary(bin) {
|
|
70
|
+
const version = await getClaudeVersion(bin);
|
|
71
|
+
return filterModelsForVersion(CLAUDE_MODELS, isAtLeast(version, FABLE_MIN_CLI_VERSION));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The modelUsage entry for `model`, which a reply names by its plain id while
|
|
75
|
+
* modelUsage may key it by an alias or with a suffix such as `[1m]`. Falls back
|
|
76
|
+
* on the first entry when none matches.
|
|
77
|
+
*/
|
|
78
|
+
export function findModelUsageKey(modelUsage, model) {
|
|
79
|
+
const keys = Object.keys(modelUsage);
|
|
80
|
+
return keys.find((k) => k === model)
|
|
81
|
+
?? keys.find((k) => modelUsage[k].canonicalModel === model)
|
|
82
|
+
?? keys.find((k) => k.startsWith(model) || model.startsWith(k.replace(/\[.*\]$/, '')))
|
|
83
|
+
?? keys[0];
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=model-catalog.js.map
|