telecoder 1.2.0 → 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.
Files changed (197) hide show
  1. package/.env.example +142 -130
  2. package/CHANGELOG.md +683 -2
  3. package/README.md +36 -32
  4. package/dist/audio/transcribe.js +0 -9
  5. package/dist/bin/mcp-server.js +46 -61
  6. package/dist/bot/bot.js +162 -181
  7. package/dist/bot/command-catalog.js +263 -0
  8. package/dist/bot/deleted-threads.js +103 -0
  9. package/dist/bot/file-access.js +35 -26
  10. package/dist/bot/handlers/album.handler.js +128 -0
  11. package/dist/bot/handlers/{fork.handler.js → branch.handler.js} +267 -280
  12. package/dist/bot/handlers/command/access.js +1 -1
  13. package/dist/bot/handlers/command/admin.js +86 -110
  14. package/dist/bot/handlers/command/all-view.js +33 -0
  15. package/dist/bot/handlers/command/auto-approve.js +3 -3
  16. package/dist/bot/handlers/command/background.js +37 -0
  17. package/dist/bot/handlers/command/botname.js +76 -0
  18. package/dist/bot/handlers/command/btw.js +20 -69
  19. package/dist/bot/handlers/command/cleanup.js +329 -0
  20. package/dist/bot/handlers/command/model.js +40 -119
  21. package/dist/bot/handlers/command/prefs-scope.js +255 -90
  22. package/dist/bot/handlers/command/project.js +31 -109
  23. package/dist/bot/handlers/command/resume.js +8 -4
  24. package/dist/bot/handlers/command/session.js +74 -213
  25. package/dist/bot/handlers/command/settings.js +92 -447
  26. package/dist/bot/handlers/command/shared.js +32 -35
  27. package/dist/bot/handlers/command/tasks.js +263 -95
  28. package/dist/bot/handlers/command/thread.js +128 -0
  29. package/dist/bot/handlers/command/topic-store.js +14 -53
  30. package/dist/bot/handlers/command/topic.js +8 -61
  31. package/dist/bot/handlers/command/{cost.js → usage.js} +32 -59
  32. package/dist/bot/handlers/command-fallback.js +179 -0
  33. package/dist/bot/handlers/command.handler.js +96 -422
  34. package/dist/bot/handlers/{schedule.handler.js → loop.handler.js} +159 -73
  35. package/dist/bot/handlers/media-fallback.js +12 -7
  36. package/dist/bot/handlers/message/replies.js +10 -87
  37. package/dist/bot/handlers/message/shared.js +9 -31
  38. package/dist/bot/handlers/message/turn-notify.js +15 -49
  39. package/dist/bot/handlers/message/turn-runner.js +18 -103
  40. package/dist/bot/handlers/message.handler.js +59 -52
  41. package/dist/bot/handlers/newbot.handler.js +13 -4
  42. package/dist/bot/handlers/photo.handler.js +88 -129
  43. package/dist/bot/handlers/prompts.handler.js +11 -11
  44. package/dist/bot/handlers/running-note.js +2 -2
  45. package/dist/bot/handlers/session-guard.js +4 -4
  46. package/dist/bot/handlers/suggestion.handler.js +2 -2
  47. package/dist/bot/handlers/tree.handler.js +20 -20
  48. package/dist/bot/handlers/voice.handler.js +23 -85
  49. package/dist/bot/kept-threads.js +51 -0
  50. package/dist/bot/middleware/album-collector.js +69 -0
  51. package/dist/bot/middleware/auth.middleware.js +1 -1
  52. package/dist/bot/middleware/group-mention.middleware.js +11 -5
  53. package/dist/bot/middleware/queued-messages.js +143 -32
  54. package/dist/bot/middleware/stale-filter.js +0 -22
  55. package/dist/build-info.json +5 -5
  56. package/dist/claude/ask-user-form.js +517 -0
  57. package/dist/claude/ask-user.js +94 -26
  58. package/dist/claude/auto-topic-haiku.js +17 -0
  59. package/dist/claude/available-commands.js +22 -19
  60. package/dist/claude/background-hint.js +91 -0
  61. package/dist/claude/{fork-tree.js → branch-tree.js} +9 -9
  62. package/dist/claude/carry-over.js +0 -2
  63. package/dist/claude/ccr-shim.js +12 -10
  64. package/dist/claude/claude-auth.js +6 -10
  65. package/dist/claude/command-parser.js +49 -43
  66. package/dist/claude/cost-overlay-scraper.js +7 -2
  67. package/dist/claude/effort-level.js +5 -0
  68. package/dist/claude/enabled-plugins.js +12 -16
  69. package/dist/claude/in-flight-tracker.js +21 -0
  70. package/dist/claude/mcp-bridge.js +86 -58
  71. package/dist/claude/memory-guard.js +400 -0
  72. package/dist/claude/message-offsets.js +5 -5
  73. package/dist/claude/modal-relay.js +401 -34
  74. package/dist/claude/model-catalog.js +20 -3
  75. package/dist/claude/monitor-relay.js +130 -15
  76. package/dist/claude/native-commands.js +40 -0
  77. package/dist/claude/{pending-forks.js → pending-branches.js} +26 -26
  78. package/dist/claude/permission-gate.js +11 -10
  79. package/dist/claude/permission-mode.js +19 -45
  80. package/dist/claude/prewarm.js +70 -0
  81. package/dist/claude/pty-idle-reaper.js +96 -0
  82. package/dist/claude/pty-provider.js +473 -188
  83. package/dist/claude/pty-spawn-config.js +79 -32
  84. package/dist/claude/pty-watch.js +201 -0
  85. package/dist/claude/request-queue.js +7 -35
  86. package/dist/claude/rewind.js +111 -0
  87. package/dist/claude/scheduled-runner.js +1 -3
  88. package/dist/claude/scheduler.js +6 -2
  89. package/dist/claude/scope-guard.js +8 -3
  90. package/dist/claude/screen-marks.js +83 -0
  91. package/dist/claude/screen-render.js +96 -0
  92. package/dist/claude/session-history.js +4 -2
  93. package/dist/claude/session-jsonl.js +162 -15
  94. package/dist/claude/session-manager.js +29 -8
  95. package/dist/claude/session-move.js +70 -0
  96. package/dist/claude/shell-mode-gate.js +60 -0
  97. package/dist/claude/side-question.js +20 -17
  98. package/dist/claude/stage-images.js +5 -5
  99. package/dist/claude/tasks-overlay-scraper.js +143 -20
  100. package/dist/claude/thread-judge-haiku.js +88 -0
  101. package/dist/claude/tip-scraper.js +3 -4
  102. package/dist/claude/tui-modal.js +272 -27
  103. package/dist/claude/tui-state.js +74 -2
  104. package/dist/claude/turn-end.js +15 -1
  105. package/dist/claude/turn-prose.js +1 -1
  106. package/dist/claude/typed-answer.js +104 -0
  107. package/dist/claude/update-banner-relay.js +1 -1
  108. package/dist/cli/commands/doctor.js +48 -15
  109. package/dist/cli/commands/help.js +11 -2
  110. package/dist/cli/commands/init.js +16 -13
  111. package/dist/cli/commands/service.js +40 -8
  112. package/dist/cli/commands/start.js +10 -0
  113. package/dist/cli/commands/watch.js +322 -0
  114. package/dist/cli/dispatch.js +5 -0
  115. package/dist/cli/init/env-file.js +1 -1
  116. package/dist/cli/init/flow.js +114 -47
  117. package/dist/cli/init/prompter.js +8 -0
  118. package/dist/cli/init/telegram.js +35 -9
  119. package/dist/cli/service/render.js +3 -3
  120. package/dist/config.js +156 -131
  121. package/dist/index.js +107 -31
  122. package/dist/launcher.js +34 -7
  123. package/dist/providers/ccr-introspect.js +1 -8
  124. package/dist/providers/ccr-provider.js +19 -20
  125. package/dist/providers/claude-provider.js +4 -57
  126. package/dist/providers/prefs-sync.js +16 -59
  127. package/dist/providers/provider-router.js +24 -23
  128. package/dist/providers/spawn-settings.js +45 -0
  129. package/dist/providers/user-preferences.js +127 -19
  130. package/dist/restart-when-idle.js +27 -7
  131. package/dist/startup/auto-continue.js +2 -2
  132. package/dist/startup/auto-resume.js +3 -15
  133. package/dist/startup/notices.js +1 -1
  134. package/dist/startup/verify-token.js +4 -4
  135. package/dist/telegram/action-logger.js +3 -165
  136. package/dist/telegram/background-activity-card.js +1 -1
  137. package/dist/telegram/botname-settings.js +71 -125
  138. package/dist/telegram/client-options.js +6 -7
  139. package/dist/telegram/force-reply-tracker.js +55 -16
  140. package/dist/telegram/message-sender.js +92 -360
  141. package/dist/telegram/prompt-label.js +11 -5
  142. package/dist/telegram/streaming-turn.js +5 -7
  143. package/dist/telegram/table-lists.js +56 -0
  144. package/dist/telegram/task-list.js +4 -4
  145. package/dist/telegram/task-tracker.js +45 -126
  146. package/dist/telegram/terminal-renderer.js +1 -5
  147. package/dist/telegram/thread-titles.js +74 -2
  148. package/dist/telegram/threads-setup.js +1 -1
  149. package/dist/utils/admins.js +2 -2
  150. package/dist/utils/bot-icon.js +248 -0
  151. package/dist/utils/bot-token.js +8 -5
  152. package/dist/utils/build-info.js +3 -3
  153. package/dist/utils/caffeinate.js +6 -3
  154. package/dist/utils/claude-env.js +0 -1
  155. package/dist/utils/claude-version.js +2 -2
  156. package/dist/utils/config-home.js +3 -3
  157. package/dist/utils/download.js +41 -15
  158. package/dist/utils/exit-codes.js +11 -0
  159. package/dist/utils/format.js +3 -6
  160. package/dist/utils/home-project.js +1 -1
  161. package/dist/utils/idle-restart.js +1 -1
  162. package/dist/utils/install-mode.js +1 -1
  163. package/dist/utils/instance-config.js +1 -1
  164. package/dist/utils/instances.js +1 -1
  165. package/dist/utils/keyed-settings.js +2 -2
  166. package/dist/utils/memory-pressure.js +60 -0
  167. package/dist/utils/npm-version.js +2 -2
  168. package/dist/utils/resolve-claude-bin.js +13 -24
  169. package/dist/utils/respawn-backoff.js +9 -0
  170. package/dist/utils/stale-launcher.js +1 -1
  171. package/dist/utils/systemd.js +8 -8
  172. package/dist/utils/telegram-proxy.js +72 -0
  173. package/dist/utils/uploads-dir.js +27 -0
  174. package/dist/utils/verbosity.js +8 -8
  175. package/dist/utils/version-check.js +5 -5
  176. package/package.json +3 -4
  177. package/dist/bot/handlers/command/media.js +0 -197
  178. package/dist/bot/handlers/command/streaming-mode.js +0 -16
  179. package/dist/bot/handlers/message/throttle.js +0 -49
  180. package/dist/claude/agent-watchdog.js +0 -162
  181. package/dist/claude/agent.js +0 -1458
  182. package/dist/claude/held-query.js +0 -190
  183. package/dist/claude/mcp-tools.js +0 -354
  184. package/dist/claude/sdk-permission-gate.js +0 -77
  185. package/dist/claude/session-cost.js +0 -41
  186. package/dist/claude/system-prompt.js +0 -162
  187. package/dist/providers/sdk-provider.js +0 -37
  188. package/dist/telegram/telegraph-settings.js +0 -31
  189. package/dist/telegram/telegraph.js +0 -453
  190. package/dist/telegram/terminal-settings.js +0 -25
  191. package/dist/tts/tts-settings.js +0 -62
  192. package/dist/tts/tts.js +0 -245
  193. package/dist/tts/voice-reply.js +0 -78
  194. package/dist/utils/debug-agent.js +0 -117
  195. package/dist/utils/debug-watchdog.js +0 -121
  196. package/dist/utils/project-data-dir.js +0 -33
  197. package/dist/utils/reasoning-summary.js +0 -12
@@ -0,0 +1,179 @@
1
+ /**
2
+ * "Did you mean" for a mistyped bot command.
3
+ *
4
+ * A slash command the bot never registered falls through to the agent as a
5
+ * prompt, so `/task` or `/shel` ends up typed into Claude, which answers
6
+ * "unknown command" at best. Before that happens, a token within a typo of a
7
+ * bot command is checked against what Claude Code accepts here (its own
8
+ * commands, skills, plugins and `.claude/commands/`), and when Claude doesn't
9
+ * know it either the likely commands come back as buttons. A tap re-sends the
10
+ * message with the corrected command and the arguments as typed, back through
11
+ * the whole middleware chain as if the user had typed it.
12
+ *
13
+ * Anything Claude Code does know, or that we can't check, goes through as
14
+ * before: the fallback only ever answers for a name nobody accepts. What
15
+ * Claude Code knows is the headless probe's list plus CLAUDE_CODE_COMMAND_NAMES,
16
+ * the interactive-only commands and aliases the probe can't see. A name both
17
+ * miss still has a button that sends it to Claude Code as typed.
18
+ */
19
+ import { InlineKeyboard } from 'grammy';
20
+ import { sessionManager } from '../../claude/session-manager.js';
21
+ import { getBotCommandNames, getForwardedCommandNames } from '../../claude/command-parser.js';
22
+ import { getAvailableCommands, getCachedAvailableCommands } from '../../claude/available-commands.js';
23
+ import { getProjectCommands } from '../../claude/project-commands.js';
24
+ import { resolvePtyClaudeBin } from '../../utils/resolve-claude-bin.js';
25
+ import { CLAUDE_CODE_COMMAND_NAMES } from '../../claude/native-commands.js';
26
+ import { sanitizeError } from '../../utils/sanitize.js';
27
+ import { markAlreadyAddressed } from '../middleware/group-mention.middleware.js';
28
+ import { buildUnknownCommandMessage, findCommandFix } from '../command-catalog.js';
29
+ import { EntityText } from '../../telegram/entities.js';
30
+ import { editCallbackMessage, replyFormatted } from '../../telegram/send.js';
31
+ import { parseCallback } from './command/shared.js';
32
+ /** Keyed by the notice that carries the buttons. */
33
+ const pending = new Map();
34
+ const PENDING_TTL_MS = 15 * 60 * 1000;
35
+ const PENDING_MAX = 200;
36
+ /** How long to wait for a cold command probe before letting the message through. */
37
+ const PROBE_WAIT_MS = 6000;
38
+ const CALLBACK_PREFIX = 'cmdfix:';
39
+ /** The button that sends the message as it was typed, to Claude Code. */
40
+ const AS_TYPED = 'typed';
41
+ /** Messages the "as typed" button re-sent, which the fallback lets through. */
42
+ const letThrough = new Set();
43
+ const BUTTON_MAX = 48;
44
+ let botRef;
45
+ /** Called once, so a tap can send the corrected message back through the bot. */
46
+ export function initCommandFallback(bot) {
47
+ botRef = bot;
48
+ }
49
+ function key(chatId, messageId) {
50
+ return `${chatId}:${messageId}`;
51
+ }
52
+ function prune() {
53
+ const now = Date.now();
54
+ for (const [k, entry] of pending) {
55
+ if (entry.expiresAt < now)
56
+ pending.delete(k);
57
+ }
58
+ while (pending.size >= PENDING_MAX) {
59
+ const oldest = pending.keys().next().value;
60
+ if (oldest === undefined)
61
+ break;
62
+ pending.delete(oldest);
63
+ }
64
+ }
65
+ function commandLine(command, args) {
66
+ return args ? `/${command} ${args}` : `/${command}`;
67
+ }
68
+ /** The command line, cut to fit on a button. */
69
+ function shortLine(command, args) {
70
+ const line = commandLine(command, args);
71
+ return line.length > BUTTON_MAX ? line.slice(0, BUTTON_MAX - 1) + '…' : line;
72
+ }
73
+ function buttonLabel(command, args) {
74
+ return `▶️ ${shortLine(command, args)}`;
75
+ }
76
+ function delay(ms) {
77
+ return new Promise((resolve) => setTimeout(() => resolve(undefined), ms));
78
+ }
79
+ /**
80
+ * Answer a mistyped command with the likely ones. Returns true when it did,
81
+ * false when the message should go on to the agent as usual.
82
+ */
83
+ export async function offerCommandFix(ctx, sessionKey, text) {
84
+ const message = ctx.message;
85
+ if (message && letThrough.delete(key(message.chat.id, message.message_id)))
86
+ return false;
87
+ const fix = findCommandFix(text, {
88
+ botNames: getBotCommandNames(),
89
+ forwarded: getForwardedCommandNames(),
90
+ });
91
+ if (!fix)
92
+ return false;
93
+ if (CLAUDE_CODE_COMMAND_NAMES.has(fix.token))
94
+ return false;
95
+ const session = sessionManager.getSession(sessionKey);
96
+ if (!session)
97
+ return false;
98
+ const workingDirectory = session.workingDirectory;
99
+ // A project command added since the last probe isn't in the snapshot yet.
100
+ if (getProjectCommands(workingDirectory).some((c) => c.name === fix.token))
101
+ return false;
102
+ const executable = resolvePtyClaudeBin();
103
+ let snapshot = getCachedAvailableCommands(workingDirectory, executable);
104
+ if (!snapshot) {
105
+ // A cold cache means a probe — a fresh headless process — so show typing
106
+ // rather than leave the chat silent; the probe keeps running past the
107
+ // wait and fills the cache for next time.
108
+ const typing = ctx.replyWithChatAction?.('typing');
109
+ snapshot = await Promise.race([getAvailableCommands(workingDirectory, executable), delay(PROBE_WAIT_MS)]);
110
+ await typing?.catch(() => { });
111
+ }
112
+ // Without Claude's list there's no telling a typo from a command we don't
113
+ // know about, so let it through.
114
+ if (!snapshot)
115
+ return false;
116
+ if (snapshot.slashCommands.includes(fix.token))
117
+ return false;
118
+ const keyboard = new InlineKeyboard();
119
+ fix.suggestions.forEach((s, i) => {
120
+ keyboard.text(buttonLabel(s.command, fix.args), `${CALLBACK_PREFIX}${i}`).row();
121
+ });
122
+ keyboard.text(`⤵️ ${shortLine(fix.token, '')} to Claude Code, as typed`, `${CALLBACK_PREFIX}${AS_TYPED}`).row();
123
+ const sent = await replyFormatted(ctx, buildUnknownCommandMessage(fix.token, fix.suggestions), {
124
+ reply_markup: keyboard,
125
+ });
126
+ prune();
127
+ pending.set(key(sent.chat.id, sent.message_id), {
128
+ update: ctx.update,
129
+ fix,
130
+ expiresAt: Date.now() + PENDING_TTL_MS,
131
+ });
132
+ console.log(`[Commands] /${fix.token} isn't a command; suggested ${fix.suggestions.map((s) => '/' + s.command).join(', ')}`);
133
+ return true;
134
+ }
135
+ /** A tap on a suggestion: send the corrected message as if it had been typed. */
136
+ export async function handleCommandFixCallback(ctx) {
137
+ const parsed = parseCallback(ctx, CALLBACK_PREFIX);
138
+ const notice = ctx.callbackQuery?.message;
139
+ if (!parsed || !notice)
140
+ return;
141
+ const k = key(notice.chat.id, notice.message_id);
142
+ const entry = pending.get(k);
143
+ const picked = parsed.data.slice(CALLBACK_PREFIX.length);
144
+ const asTyped = picked === AS_TYPED;
145
+ const choice = asTyped ? undefined : entry?.fix.suggestions[Number(picked)];
146
+ if (!entry || (!choice && !asTyped) || entry.expiresAt < Date.now() || !botRef) {
147
+ await ctx.answerCallbackQuery({ text: 'That suggestion has expired — type the command again.' });
148
+ return;
149
+ }
150
+ pending.delete(k);
151
+ const update = structuredClone(entry.update);
152
+ const text = choice ? commandLine(choice.command, entry.fix.args) : update.message.text ?? '';
153
+ if (choice) {
154
+ update.message.text = text;
155
+ // grammY finds commands by their entity, so the new token needs one.
156
+ update.message.entities = [{ type: 'bot_command', offset: 0, length: choice.command.length + 1 }];
157
+ }
158
+ update.message.date = Math.floor(Date.now() / 1000);
159
+ // The typo's own id has been through the duplicate filter once already;
160
+ // the notice's never arrived as a message, so it is free.
161
+ update.message.message_id = notice.message_id;
162
+ // Whoever tapped sends it, under their own access.
163
+ if (ctx.from)
164
+ update.message.from = ctx.from;
165
+ // As typed, it goes past this fallback to Claude Code.
166
+ if (asTyped)
167
+ letThrough.add(key(update.message.chat.id, update.message.message_id));
168
+ await ctx.answerCallbackQuery();
169
+ await editCallbackMessage(ctx, new EntityText().add('▶️ ').code(text).build())
170
+ .catch((err) => console.warn('[Commands] Could not update the suggestion notice:', sanitizeError(err)));
171
+ markAlreadyAddressed(update);
172
+ console.log(asTyped
173
+ ? `[Commands] Sending /${entry.fix.token} to Claude Code as typed`
174
+ : `[Commands] Sending ${text.split(' ')[0]} for a mistyped /${entry.fix.token}`);
175
+ void botRef.handleUpdate(update).catch((err) => {
176
+ console.error('[Commands] Sending the corrected command failed:', sanitizeError(err));
177
+ });
178
+ }
179
+ //# sourceMappingURL=command-fallback.js.map