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.
- package/.env.example +142 -130
- package/CHANGELOG.md +683 -2
- package/README.md +36 -32
- package/dist/audio/transcribe.js +0 -9
- package/dist/bin/mcp-server.js +46 -61
- package/dist/bot/bot.js +162 -181
- package/dist/bot/command-catalog.js +263 -0
- package/dist/bot/deleted-threads.js +103 -0
- package/dist/bot/file-access.js +35 -26
- package/dist/bot/handlers/album.handler.js +128 -0
- package/dist/bot/handlers/{fork.handler.js → branch.handler.js} +267 -280
- package/dist/bot/handlers/command/access.js +1 -1
- package/dist/bot/handlers/command/admin.js +86 -110
- package/dist/bot/handlers/command/all-view.js +33 -0
- package/dist/bot/handlers/command/auto-approve.js +3 -3
- package/dist/bot/handlers/command/background.js +37 -0
- package/dist/bot/handlers/command/botname.js +76 -0
- package/dist/bot/handlers/command/btw.js +20 -69
- package/dist/bot/handlers/command/cleanup.js +329 -0
- package/dist/bot/handlers/command/model.js +40 -119
- package/dist/bot/handlers/command/prefs-scope.js +255 -90
- package/dist/bot/handlers/command/project.js +31 -109
- package/dist/bot/handlers/command/resume.js +8 -4
- package/dist/bot/handlers/command/session.js +74 -213
- package/dist/bot/handlers/command/settings.js +92 -447
- package/dist/bot/handlers/command/shared.js +32 -35
- package/dist/bot/handlers/command/tasks.js +263 -95
- package/dist/bot/handlers/command/thread.js +128 -0
- package/dist/bot/handlers/command/topic-store.js +14 -53
- package/dist/bot/handlers/command/topic.js +8 -61
- package/dist/bot/handlers/command/{cost.js → usage.js} +32 -59
- package/dist/bot/handlers/command-fallback.js +179 -0
- package/dist/bot/handlers/command.handler.js +96 -422
- package/dist/bot/handlers/{schedule.handler.js → loop.handler.js} +159 -73
- package/dist/bot/handlers/media-fallback.js +12 -7
- package/dist/bot/handlers/message/replies.js +10 -87
- package/dist/bot/handlers/message/shared.js +9 -31
- package/dist/bot/handlers/message/turn-notify.js +15 -49
- package/dist/bot/handlers/message/turn-runner.js +18 -103
- package/dist/bot/handlers/message.handler.js +59 -52
- package/dist/bot/handlers/newbot.handler.js +13 -4
- package/dist/bot/handlers/photo.handler.js +88 -129
- package/dist/bot/handlers/prompts.handler.js +11 -11
- package/dist/bot/handlers/running-note.js +2 -2
- package/dist/bot/handlers/session-guard.js +4 -4
- package/dist/bot/handlers/suggestion.handler.js +2 -2
- package/dist/bot/handlers/tree.handler.js +20 -20
- package/dist/bot/handlers/voice.handler.js +23 -85
- package/dist/bot/kept-threads.js +51 -0
- package/dist/bot/middleware/album-collector.js +69 -0
- package/dist/bot/middleware/auth.middleware.js +1 -1
- package/dist/bot/middleware/group-mention.middleware.js +11 -5
- package/dist/bot/middleware/queued-messages.js +143 -32
- package/dist/bot/middleware/stale-filter.js +0 -22
- package/dist/build-info.json +5 -5
- package/dist/claude/ask-user-form.js +517 -0
- package/dist/claude/ask-user.js +94 -26
- package/dist/claude/auto-topic-haiku.js +17 -0
- package/dist/claude/available-commands.js +22 -19
- package/dist/claude/background-hint.js +91 -0
- package/dist/claude/{fork-tree.js → branch-tree.js} +9 -9
- package/dist/claude/carry-over.js +0 -2
- package/dist/claude/ccr-shim.js +12 -10
- package/dist/claude/claude-auth.js +6 -10
- package/dist/claude/command-parser.js +49 -43
- package/dist/claude/cost-overlay-scraper.js +7 -2
- package/dist/claude/effort-level.js +5 -0
- package/dist/claude/enabled-plugins.js +12 -16
- package/dist/claude/in-flight-tracker.js +21 -0
- package/dist/claude/mcp-bridge.js +86 -58
- package/dist/claude/memory-guard.js +400 -0
- package/dist/claude/message-offsets.js +5 -5
- package/dist/claude/modal-relay.js +401 -34
- package/dist/claude/model-catalog.js +20 -3
- package/dist/claude/monitor-relay.js +130 -15
- package/dist/claude/native-commands.js +40 -0
- package/dist/claude/{pending-forks.js → pending-branches.js} +26 -26
- package/dist/claude/permission-gate.js +11 -10
- package/dist/claude/permission-mode.js +19 -45
- package/dist/claude/prewarm.js +70 -0
- package/dist/claude/pty-idle-reaper.js +96 -0
- package/dist/claude/pty-provider.js +473 -188
- package/dist/claude/pty-spawn-config.js +79 -32
- package/dist/claude/pty-watch.js +201 -0
- package/dist/claude/request-queue.js +7 -35
- package/dist/claude/rewind.js +111 -0
- package/dist/claude/scheduled-runner.js +1 -3
- package/dist/claude/scheduler.js +6 -2
- package/dist/claude/scope-guard.js +8 -3
- package/dist/claude/screen-marks.js +83 -0
- package/dist/claude/screen-render.js +96 -0
- package/dist/claude/session-history.js +4 -2
- package/dist/claude/session-jsonl.js +162 -15
- package/dist/claude/session-manager.js +29 -8
- package/dist/claude/session-move.js +70 -0
- package/dist/claude/shell-mode-gate.js +60 -0
- package/dist/claude/side-question.js +20 -17
- package/dist/claude/stage-images.js +5 -5
- package/dist/claude/tasks-overlay-scraper.js +143 -20
- package/dist/claude/thread-judge-haiku.js +88 -0
- package/dist/claude/tip-scraper.js +3 -4
- package/dist/claude/tui-modal.js +272 -27
- package/dist/claude/tui-state.js +74 -2
- package/dist/claude/turn-end.js +15 -1
- package/dist/claude/turn-prose.js +1 -1
- package/dist/claude/typed-answer.js +104 -0
- package/dist/claude/update-banner-relay.js +1 -1
- package/dist/cli/commands/doctor.js +48 -15
- package/dist/cli/commands/help.js +11 -2
- package/dist/cli/commands/init.js +16 -13
- package/dist/cli/commands/service.js +40 -8
- package/dist/cli/commands/start.js +10 -0
- package/dist/cli/commands/watch.js +322 -0
- package/dist/cli/dispatch.js +5 -0
- package/dist/cli/init/env-file.js +1 -1
- package/dist/cli/init/flow.js +114 -47
- package/dist/cli/init/prompter.js +8 -0
- package/dist/cli/init/telegram.js +35 -9
- package/dist/cli/service/render.js +3 -3
- package/dist/config.js +156 -131
- package/dist/index.js +107 -31
- package/dist/launcher.js +34 -7
- package/dist/providers/ccr-introspect.js +1 -8
- package/dist/providers/ccr-provider.js +19 -20
- package/dist/providers/claude-provider.js +4 -57
- package/dist/providers/prefs-sync.js +16 -59
- package/dist/providers/provider-router.js +24 -23
- package/dist/providers/spawn-settings.js +45 -0
- package/dist/providers/user-preferences.js +127 -19
- package/dist/restart-when-idle.js +27 -7
- package/dist/startup/auto-continue.js +2 -2
- package/dist/startup/auto-resume.js +3 -15
- package/dist/startup/notices.js +1 -1
- package/dist/startup/verify-token.js +4 -4
- package/dist/telegram/action-logger.js +3 -165
- package/dist/telegram/background-activity-card.js +1 -1
- package/dist/telegram/botname-settings.js +71 -125
- package/dist/telegram/client-options.js +6 -7
- package/dist/telegram/force-reply-tracker.js +55 -16
- package/dist/telegram/message-sender.js +92 -360
- package/dist/telegram/prompt-label.js +11 -5
- package/dist/telegram/streaming-turn.js +5 -7
- package/dist/telegram/table-lists.js +56 -0
- package/dist/telegram/task-list.js +4 -4
- package/dist/telegram/task-tracker.js +45 -126
- package/dist/telegram/terminal-renderer.js +1 -5
- package/dist/telegram/thread-titles.js +74 -2
- package/dist/telegram/threads-setup.js +1 -1
- package/dist/utils/admins.js +2 -2
- package/dist/utils/bot-icon.js +248 -0
- package/dist/utils/bot-token.js +8 -5
- package/dist/utils/build-info.js +3 -3
- package/dist/utils/caffeinate.js +6 -3
- package/dist/utils/claude-env.js +0 -1
- package/dist/utils/claude-version.js +2 -2
- package/dist/utils/config-home.js +3 -3
- package/dist/utils/download.js +41 -15
- package/dist/utils/exit-codes.js +11 -0
- package/dist/utils/format.js +3 -6
- package/dist/utils/home-project.js +1 -1
- package/dist/utils/idle-restart.js +1 -1
- package/dist/utils/install-mode.js +1 -1
- package/dist/utils/instance-config.js +1 -1
- package/dist/utils/instances.js +1 -1
- package/dist/utils/keyed-settings.js +2 -2
- package/dist/utils/memory-pressure.js +60 -0
- package/dist/utils/npm-version.js +2 -2
- package/dist/utils/resolve-claude-bin.js +13 -24
- package/dist/utils/respawn-backoff.js +9 -0
- package/dist/utils/stale-launcher.js +1 -1
- package/dist/utils/systemd.js +8 -8
- package/dist/utils/telegram-proxy.js +72 -0
- package/dist/utils/uploads-dir.js +27 -0
- package/dist/utils/verbosity.js +8 -8
- package/dist/utils/version-check.js +5 -5
- package/package.json +3 -4
- package/dist/bot/handlers/command/media.js +0 -197
- package/dist/bot/handlers/command/streaming-mode.js +0 -16
- package/dist/bot/handlers/message/throttle.js +0 -49
- package/dist/claude/agent-watchdog.js +0 -162
- package/dist/claude/agent.js +0 -1458
- package/dist/claude/held-query.js +0 -190
- package/dist/claude/mcp-tools.js +0 -354
- package/dist/claude/sdk-permission-gate.js +0 -77
- package/dist/claude/session-cost.js +0 -41
- package/dist/claude/system-prompt.js +0 -162
- package/dist/providers/sdk-provider.js +0 -37
- package/dist/telegram/telegraph-settings.js +0 -31
- package/dist/telegram/telegraph.js +0 -453
- package/dist/telegram/terminal-settings.js +0 -25
- package/dist/tts/tts-settings.js +0 -62
- package/dist/tts/tts.js +0 -245
- package/dist/tts/voice-reply.js +0 -78
- package/dist/utils/debug-agent.js +0 -117
- package/dist/utils/debug-watchdog.js +0 -121
- package/dist/utils/project-data-dir.js +0 -33
- package/dist/utils/reasoning-summary.js +0 -12
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Everything posted to the chat *after* a turn's main response: the catch-up
|
|
3
|
-
* relay that recovers prose the extractors dropped, the
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* of the status line, see `sendStatusLine`.
|
|
3
|
+
* relay that recovers prose the extractors dropped, the compaction notice,
|
|
4
|
+
* and the status line that comes last. The usage footer isn't a message of its
|
|
5
|
+
* own: it goes out at the bottom of the status line, see `sendStatusLine`.
|
|
7
6
|
*
|
|
8
|
-
*
|
|
9
|
-
* line the chat's /
|
|
7
|
+
* The notice consults the chat's verbosity settings itself, and the status
|
|
8
|
+
* line the chat's /footer ones, so callers fire both unconditionally via
|
|
10
9
|
* `closeTurn`.
|
|
11
10
|
*/
|
|
12
11
|
import { sessionManager } from '../../../claude/session-manager.js';
|
|
@@ -19,7 +18,6 @@ import { replyFormatted } from '../../../telegram/send.js';
|
|
|
19
18
|
import { bringCardDown } from '../../../telegram/background-activity-card.js';
|
|
20
19
|
import { resolveVerbosityFlags } from '../../../utils/verbosity.js';
|
|
21
20
|
import { fmtTokens } from '../../../utils/format.js';
|
|
22
|
-
import { stripReasoningSummary } from '../../../utils/reasoning-summary.js';
|
|
23
21
|
import { sendStatusLine } from '../command/settings.js';
|
|
24
22
|
/**
|
|
25
23
|
* After a turn's main response has been sent to Telegram, compare what we
|
|
@@ -31,7 +29,7 @@ import { sendStatusLine } from '../command/settings.js';
|
|
|
31
29
|
*
|
|
32
30
|
* Best-effort: any failure logs and swallows so a catch-up bug can never
|
|
33
31
|
* break the primary relay path. Skips silently when the session has no
|
|
34
|
-
* claudeSessionId
|
|
32
|
+
* claudeSessionId yet, since there is no log to compare against.
|
|
35
33
|
*/
|
|
36
34
|
export async function relayCatchUpIfMissed(ctx, sessionKey, relayedText) {
|
|
37
35
|
try {
|
|
@@ -40,10 +38,7 @@ export async function relayCatchUpIfMissed(ctx, sessionKey, relayedText) {
|
|
|
40
38
|
sessionManager.setLastRelayedAssistantText(sessionKey, relayedText);
|
|
41
39
|
return;
|
|
42
40
|
}
|
|
43
|
-
|
|
44
|
-
// has it. Compared as is, the summary alone counts as missing prose, and
|
|
45
|
-
// a turn with a tool call in it gets the whole reply posted again.
|
|
46
|
-
const jsonlText = stripReasoningSummary(readLastAssistantTurnText(session.workingDirectory, session.claudeSessionId) ?? '');
|
|
41
|
+
const jsonlText = readLastAssistantTurnText(session.workingDirectory, session.claudeSessionId) ?? '';
|
|
47
42
|
if (!jsonlText) {
|
|
48
43
|
sessionManager.setLastRelayedAssistantText(sessionKey, relayedText);
|
|
49
44
|
return;
|
|
@@ -90,58 +85,29 @@ async function sendCompactionNotification(ctx, compaction) {
|
|
|
90
85
|
await replyFormatted(ctx, new EntityText()
|
|
91
86
|
.add(`${emoji} `).bold('Context Compacted').newline(2)
|
|
92
87
|
.add(`${triggerLabel} — previous context was ${fmtTokens(c.preTokens)} tokens.\n`)
|
|
93
|
-
.add('The agent now has a summarized version of your conversation.')
|
|
94
|
-
.italic('Tip: Use /handoff before compaction to save a detailed context document.')
|
|
88
|
+
.add('The agent now has a summarized version of your conversation.')
|
|
95
89
|
.build());
|
|
96
90
|
}
|
|
97
91
|
catch (err) {
|
|
98
92
|
console.error('[Compaction] Failed to send notification:', err);
|
|
99
93
|
}
|
|
100
94
|
}
|
|
101
|
-
async function sendSessionInitNotification(ctx, sessionKey, sessionInit) {
|
|
102
|
-
if (!sessionInit)
|
|
103
|
-
return;
|
|
104
|
-
const chatId = ctx.chat?.id;
|
|
105
|
-
if (chatId === undefined)
|
|
106
|
-
return;
|
|
107
|
-
if (!resolveVerbosityFlags(chatId).notifyCompaction)
|
|
108
|
-
return;
|
|
109
|
-
const previousSessionId = sessionManager.getSession(sessionKey)?.claudeSessionId;
|
|
110
|
-
if (previousSessionId && sessionInit.sessionId !== previousSessionId) {
|
|
111
|
-
await replyFormatted(ctx, new EntityText()
|
|
112
|
-
.add('🔄 ').bold('New Agent Session').newline(2)
|
|
113
|
-
.add('A new agent session has started (previous context may be summarized).\n')
|
|
114
|
-
.add('Model: ').code(sessionInit.model).newline(2)
|
|
115
|
-
.italic('The agent may not remember earlier details. Consider sharing context.')
|
|
116
|
-
.build());
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
95
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
await sendCompactionNotification(ctx, response.compaction);
|
|
126
|
-
await sendSessionInitNotification(ctx, sessionKey, response.sessionInit);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* What closes a turn, however it started: the context notices, then the status
|
|
130
|
-
* line. A typed message, a voice note, an upload, /plan, /explore, /loop and a
|
|
131
|
-
* scheduled fire all end here. Each used to write its own ending, and only the
|
|
132
|
-
* typed message's had the status line, so a turn started any other way left
|
|
133
|
-
* the chat on the tail of its reply with nothing to say where things stood.
|
|
96
|
+
* What closes a turn, however it started: the compaction notice, then the
|
|
97
|
+
* status line. A typed message, a voice note, an upload and a scheduled fire
|
|
98
|
+
* all end here. Each used to write its own ending, and only the typed
|
|
99
|
+
* message's had the status line, so a turn started any other way left the
|
|
100
|
+
* chat on the tail of its reply with nothing to say where things stood.
|
|
134
101
|
*
|
|
135
102
|
* `prompt` is what the turn was sent, for a session whose log has no prompts
|
|
136
103
|
* for the status line to list.
|
|
137
104
|
*/
|
|
138
105
|
export async function closeTurn(ctx, sessionKey, response, prompt, options) {
|
|
139
|
-
await
|
|
106
|
+
await sendCompactionNotification(ctx, response.compaction);
|
|
140
107
|
const chatId = ctx.chat?.id;
|
|
141
108
|
if (chatId === undefined)
|
|
142
109
|
return;
|
|
143
|
-
// Not every response carries a usage: a
|
|
144
|
-
// iterations left one behind, and neither does a cancelled turn's. The line
|
|
110
|
+
// Not every response carries a usage: a cancelled turn's doesn't. The line
|
|
145
111
|
// then shows what the session last had, rather than no model and no context.
|
|
146
112
|
const usage = response.usage ?? getCachedUsage(sessionKey);
|
|
147
113
|
await sendStatusLine(ctx, chatId, sessionKey, usage, prompt, options);
|
|
@@ -2,77 +2,30 @@
|
|
|
2
2
|
* Turn execution: every path that actually runs a user prompt through a
|
|
3
3
|
* provider and renders the result into the chat.
|
|
4
4
|
*
|
|
5
|
-
* `runTurn` is the
|
|
6
|
-
*
|
|
7
|
-
* apart. `handleCcrThrottleCallback` lives here rather than in throttle.ts
|
|
8
|
-
* because it re-runs a prompt — keeping it here lets throttle.ts stay a leaf.
|
|
5
|
+
* `runTurn` is the one path a prompt takes, so the typed-message and
|
|
6
|
+
* suggestion-tap entry points cannot drift apart.
|
|
9
7
|
*/
|
|
10
|
-
import { sendToAgent
|
|
11
|
-
import { switchProvider } from '../../../providers/provider-switch.js';
|
|
12
|
-
import { config } from '../../../config.js';
|
|
13
|
-
import { setAbortController } from '../../../claude/request-queue.js';
|
|
8
|
+
import { sendToAgent } from '../../../providers/provider-router.js';
|
|
14
9
|
import { messageSender } from '../../../telegram/message-sender.js';
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import { getSessionKeyFromCtx } from '../../../utils/session-key.js';
|
|
18
|
-
import { getStreamingMode } from '../command.handler.js';
|
|
19
|
-
import { fireAutoTopic, runQueuedTurn } from './shared.js';
|
|
10
|
+
import { progressCallbacks, toolCallbacks, withStreamingTurn } from '../../../telegram/streaming-turn.js';
|
|
11
|
+
import { fireAutoTopic } from './shared.js';
|
|
20
12
|
import { closeTurn, relayCatchUpIfMissed } from './turn-notify.js';
|
|
21
|
-
import {
|
|
22
|
-
export async function handleCcrThrottleCallback(ctx) {
|
|
23
|
-
const data = ctx.callbackQuery?.data;
|
|
24
|
-
if (!data || !data.startsWith('ccr_throttle:'))
|
|
25
|
-
return;
|
|
26
|
-
const action = data.replace('ccr_throttle:', '');
|
|
27
|
-
const sessionKeyInfo = getSessionKeyFromCtx(ctx);
|
|
28
|
-
if (!sessionKeyInfo) {
|
|
29
|
-
await ctx.answerCallbackQuery({ text: 'Session not found' });
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const { sessionKey } = sessionKeyInfo;
|
|
33
|
-
const chatId = ctx.chat?.id;
|
|
34
|
-
if (action === 'cancel') {
|
|
35
|
-
lastThrottledPrompt.delete(sessionKey);
|
|
36
|
-
await ctx.answerCallbackQuery({ text: 'Cancelled' });
|
|
37
|
-
await ctx.editMessageReplyMarkup({ reply_markup: undefined });
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (action === 'switch') {
|
|
41
|
-
if (!config.CCR_ENABLED || !chatId) {
|
|
42
|
-
await ctx.answerCallbackQuery({ text: 'CCR not available' });
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const pending = lastThrottledPrompt.get(sessionKey);
|
|
46
|
-
if (getActiveProviderName(sessionKey) !== 'ccr') {
|
|
47
|
-
// Fork onto a fresh CCR session, carrying over a summary so the retry
|
|
48
|
-
// keeps context. A direct resume would replay the Anthropic session's
|
|
49
|
-
// thinking blocks; CCR tolerates that, but forking keeps the two
|
|
50
|
-
// backends' sessions cleanly separated for the eventual switch back.
|
|
51
|
-
await switchProvider(sessionKey, 'ccr');
|
|
52
|
-
}
|
|
53
|
-
lastThrottledPrompt.delete(sessionKey);
|
|
54
|
-
await ctx.answerCallbackQuery({ text: 'Switched to CCR' });
|
|
55
|
-
await ctx.editMessageReplyMarkup({ reply_markup: undefined });
|
|
56
|
-
if (!pending) {
|
|
57
|
-
await ctx.reply('🔌 Switched to CCR. Send your message again to retry.');
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
await ctx.reply('🔁 Retrying via CCR...');
|
|
61
|
-
await runQueuedTurn(ctx, sessionKey, pending, 'CcrRetry', () => runTurn(ctx, sessionKey, chatId, pending));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
13
|
+
import { guardSpawn } from '../../../claude/memory-guard.js';
|
|
64
14
|
/**
|
|
65
15
|
* Run one prompt in whichever response mode the chat is currently set to.
|
|
66
16
|
* Every entry point that executes a user prompt funnels through here, so the
|
|
67
17
|
* streaming-vs-wait decision lives in exactly one place.
|
|
68
18
|
*/
|
|
69
|
-
export async function runTurn(ctx, sessionKey,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
19
|
+
export async function runTurn(ctx, sessionKey, message) {
|
|
20
|
+
// A message that would start a new claude on a machine short of memory is
|
|
21
|
+
// held until the chat says so (see memory-guard.ts). One to a running
|
|
22
|
+
// claude passes straight through.
|
|
23
|
+
const guard = await guardSpawn(ctx, sessionKey);
|
|
24
|
+
if (!guard.proceed) {
|
|
25
|
+
await ctx.reply(guard.message);
|
|
26
|
+
return;
|
|
75
27
|
}
|
|
28
|
+
await streamTurn(ctx, sessionKey, message);
|
|
76
29
|
}
|
|
77
30
|
/**
|
|
78
31
|
* Entry point used by callback-triggered prompts (suggestion-tap, CCR
|
|
@@ -80,11 +33,11 @@ export async function runTurn(ctx, sessionKey, chatId, message) {
|
|
|
80
33
|
* would take, and fires the topic update so the bot name still tracks the
|
|
81
34
|
* topic the user implicitly chose by tapping.
|
|
82
35
|
*/
|
|
83
|
-
export async function dispatchPromptFromCallback(ctx, sessionKey,
|
|
36
|
+
export async function dispatchPromptFromCallback(ctx, sessionKey, message) {
|
|
84
37
|
fireAutoTopic(ctx, sessionKey, message);
|
|
85
|
-
await runTurn(ctx, sessionKey,
|
|
38
|
+
await runTurn(ctx, sessionKey, message);
|
|
86
39
|
}
|
|
87
|
-
async function
|
|
40
|
+
async function streamTurn(ctx, sessionKey, message) {
|
|
88
41
|
const startTime = Date.now();
|
|
89
42
|
await withStreamingTurn(ctx, sessionKey, message, async (abortController) => {
|
|
90
43
|
const response = await sendToAgent(sessionKey, message, {
|
|
@@ -95,47 +48,9 @@ async function handleStreamingResponse(ctx, sessionKey, message) {
|
|
|
95
48
|
});
|
|
96
49
|
await messageSender.finishStreaming(ctx, response.text, { nextPromptSuggestion: response.nextPromptSuggestion });
|
|
97
50
|
await relayCatchUpIfMissed(ctx, sessionKey, response.text || '');
|
|
98
|
-
await maybeSendVoiceReply(ctx, response.text);
|
|
99
51
|
// Completion notification for long tasks (streaming edits don't trigger push notifications)
|
|
100
52
|
await messageSender.sendCompletionNotification(ctx, Date.now() - startTime);
|
|
101
53
|
await closeTurn(ctx, sessionKey, response, message);
|
|
102
|
-
if (response.throttle) {
|
|
103
|
-
await postThrottlePrompt(ctx, sessionKey, message, response.throttle);
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
lastThrottledPrompt.delete(sessionKey);
|
|
107
|
-
}
|
|
108
54
|
});
|
|
109
55
|
}
|
|
110
|
-
async function handleWaitResponse(ctx, sessionKey, chatId, message) {
|
|
111
|
-
// Start continuous typing indicator (every 4s)
|
|
112
|
-
const keyInfo = getSessionKeyFromCtx(ctx);
|
|
113
|
-
const typingInterval = messageSender.startTypingIndicator(ctx.api, chatId, keyInfo?.threadId);
|
|
114
|
-
const abortController = new AbortController();
|
|
115
|
-
setAbortController(sessionKey, abortController);
|
|
116
|
-
try {
|
|
117
|
-
const response = await sendToAgent(sessionKey, message, {
|
|
118
|
-
abortController,
|
|
119
|
-
telegramCtx: ctx,
|
|
120
|
-
onToolResult: makeToolResultHandler(ctx),
|
|
121
|
-
onEditDiff: makeEditDiffHandler(ctx),
|
|
122
|
-
});
|
|
123
|
-
messageSender.stopTypingInterval(typingInterval);
|
|
124
|
-
const sentId = await messageSender.sendMessage(ctx, response.text);
|
|
125
|
-
await messageSender.attachForkButton(ctx, sessionKey, sentId);
|
|
126
|
-
await relayCatchUpIfMissed(ctx, sessionKey, response.text || '');
|
|
127
|
-
await maybeSendVoiceReply(ctx, response.text);
|
|
128
|
-
await closeTurn(ctx, sessionKey, response, message);
|
|
129
|
-
if (response.throttle) {
|
|
130
|
-
await postThrottlePrompt(ctx, sessionKey, message, response.throttle);
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
lastThrottledPrompt.delete(sessionKey);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
catch (error) {
|
|
137
|
-
messageSender.stopTypingInterval(typingInterval);
|
|
138
|
-
throw error;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
56
|
//# sourceMappingURL=turn-runner.js.map
|
|
@@ -3,43 +3,73 @@
|
|
|
3
3
|
* surface of the message-handling domain.
|
|
4
4
|
*
|
|
5
5
|
* `handleMessage` decides what a message *is* — a reply to a ForceReply
|
|
6
|
-
* prompt, a pending
|
|
6
|
+
* prompt, a pending branch to offer, or a prompt for the agent — and hands off to the module that owns that flow. The domains
|
|
7
7
|
* live in `./message/`:
|
|
8
8
|
*
|
|
9
9
|
* shared.ts helpers every entry point needs (session, topic, queueing)
|
|
10
10
|
* turn-runner.ts running a prompt through a provider; all turn entry points
|
|
11
11
|
* turn-notify.ts what gets posted after a turn: catch-up relay + notices
|
|
12
12
|
* replies.ts the ForceReply answer handlers
|
|
13
|
-
* throttle.ts CCR throttle state and the switch-and-retry prompt
|
|
14
13
|
*
|
|
15
14
|
* Re-exports below keep the import path stable for bot.ts and the other
|
|
16
15
|
* consumers that predate the split.
|
|
17
16
|
*/
|
|
18
17
|
import { isDuplicate, markProcessed } from '../../telegram/deduplication.js';
|
|
19
18
|
import { isStaleMessage } from '../middleware/stale-filter.js';
|
|
20
|
-
import { isClaudeCommand, stripCommandBotMention } from '../../claude/command-parser.js';
|
|
21
|
-
import {
|
|
19
|
+
import { isClaudeCommand, nativeCommandIn, stripCommandBotMention } from '../../claude/command-parser.js';
|
|
20
|
+
import { offerPendingBranchIfAny } from './branch.handler.js';
|
|
21
|
+
import { offerCommandFix } from './command-fallback.js';
|
|
22
22
|
import { getSessionKeyFromCtx } from '../../utils/session-key.js';
|
|
23
23
|
import { fireAutoTopic, requireSession, runQueuedTurn } from './message/shared.js';
|
|
24
24
|
import { runTurn } from './message/turn-runner.js';
|
|
25
|
-
import {
|
|
25
|
+
import { handleDirReply } from './message/replies.js';
|
|
26
|
+
import { repliedForceReplyFlow } from '../../telegram/force-reply-tracker.js';
|
|
26
27
|
export { closeTurn, relayCatchUpIfMissed } from './message/turn-notify.js';
|
|
27
28
|
export { runQueuedTurn } from './message/shared.js';
|
|
28
|
-
export {
|
|
29
|
-
export { handleCcrThrottleCallback, dispatchPromptFromCallback } from './message/turn-runner.js';
|
|
29
|
+
export { dispatchPromptFromCallback } from './message/turn-runner.js';
|
|
30
30
|
export async function handleMessage(ctx) {
|
|
31
|
-
const keyInfo = getSessionKeyFromCtx(ctx);
|
|
32
31
|
const rawText = ctx.message?.text;
|
|
33
|
-
|
|
34
|
-
const messageDate = ctx.message?.date;
|
|
35
|
-
if (!keyInfo || !rawText || !messageId || !messageDate)
|
|
32
|
+
if (!rawText)
|
|
36
33
|
return;
|
|
37
|
-
const { chatId, sessionKey } = keyInfo;
|
|
38
34
|
// In group chats Telegram appends `@BotName` to slash commands
|
|
39
35
|
// (`/compact@MyBot`). Strip it so command detection below — and native
|
|
40
36
|
// slash commands forwarded to the agent, like /compact — see a clean token
|
|
41
37
|
// rather than one the PTY TUI would type verbatim. No-op for ordinary text.
|
|
42
|
-
|
|
38
|
+
await deliver(ctx, stripCommandBotMention(rawText), false);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* `/cc <command>`: Claude Code's own command of that name, typed at it as if
|
|
42
|
+
* the bot had no command called the same. `/cc model` opens the picker the
|
|
43
|
+
* bot's `/model` stands in front of, and `/cc usage` the dialog behind the
|
|
44
|
+
* bot's `/usage`. From there it is a prompt like any
|
|
45
|
+
* other slash command the bot passes on: it waits its turn, and its dialog
|
|
46
|
+
* comes to the chat as buttons.
|
|
47
|
+
*/
|
|
48
|
+
export async function handleNativeCommand(ctx) {
|
|
49
|
+
const rawText = ctx.message?.text;
|
|
50
|
+
if (!rawText)
|
|
51
|
+
return;
|
|
52
|
+
const command = nativeCommandIn(rawText);
|
|
53
|
+
if (!command) {
|
|
54
|
+
await ctx.reply('Name the Claude Code command to run, as in /cc model or /cc usage.\n\n'
|
|
55
|
+
+ "It is for the commands a bot command of the same name hides. Claude Code's "
|
|
56
|
+
+ 'other commands work by typing them as they are.');
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
await deliver(ctx, command, true);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Take `text` through to the agent. `native` marks text that is a Claude Code
|
|
63
|
+
* command by the sender's own word (`/cc`), so it is neither checked for a
|
|
64
|
+
* typo of a bot command nor read as one.
|
|
65
|
+
*/
|
|
66
|
+
async function deliver(ctx, text, native) {
|
|
67
|
+
const keyInfo = getSessionKeyFromCtx(ctx);
|
|
68
|
+
const messageId = ctx.message?.message_id;
|
|
69
|
+
const messageDate = ctx.message?.date;
|
|
70
|
+
if (!keyInfo || !messageId || !messageDate)
|
|
71
|
+
return;
|
|
72
|
+
const { sessionKey } = keyInfo;
|
|
43
73
|
// Filter stale messages (sent before bot started)
|
|
44
74
|
if (isStaleMessage(messageDate)) {
|
|
45
75
|
console.log(`[Message] Ignoring stale message ${messageId} from before bot start`);
|
|
@@ -51,50 +81,27 @@ export async function handleMessage(ctx) {
|
|
|
51
81
|
return;
|
|
52
82
|
}
|
|
53
83
|
markProcessed(messageId);
|
|
54
|
-
//
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
await handleTelegraphReply(ctx, sessionKey, text);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
// Handle file download reply
|
|
69
|
-
if (replyText.includes('Download File')) {
|
|
70
|
-
await handleFileReply(ctx, sessionKey, text);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
// Handle plan mode reply
|
|
74
|
-
if (replyText.includes('Plan Mode') || replyText.includes('Describe your task')) {
|
|
75
|
-
await handleAgentReply(ctx, sessionKey, text, 'plan');
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
// Handle explore mode reply
|
|
79
|
-
if (replyText.includes('Explore Mode') || replyText.includes('What would you like to know')) {
|
|
80
|
-
await handleAgentReply(ctx, sessionKey, text, 'explore');
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
// Handle loop mode reply
|
|
84
|
-
if (replyText.includes('Loop Mode') || replyText.includes('work iteratively')) {
|
|
85
|
-
await handleAgentReply(ctx, sessionKey, text, 'loop');
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
84
|
+
// An answer to one of the bot's ForceReply prompts goes to the flow that asked.
|
|
85
|
+
const flow = repliedForceReplyFlow(ctx);
|
|
86
|
+
if (flow === 'dir') {
|
|
87
|
+
await handleDirReply(ctx, sessionKey, text);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
// A slash command the bot didn't register is on its way to Claude as a
|
|
91
|
+
// prompt. If it looks like a typo of a bot command, and Claude Code doesn't
|
|
92
|
+
// know it either, offer the likely commands instead.
|
|
93
|
+
if (!native && await offerCommandFix(ctx, sessionKey, text)) {
|
|
94
|
+
return;
|
|
88
95
|
}
|
|
89
96
|
// Skip if this is a Claude command (handled by command handler)
|
|
90
|
-
if (isClaudeCommand(text)) {
|
|
97
|
+
if (!native && isClaudeCommand(text)) {
|
|
91
98
|
return;
|
|
92
99
|
}
|
|
93
|
-
// If a /
|
|
100
|
+
// If a /branch from another bot is sitting in our pending-branches file, the
|
|
94
101
|
// user's first non-command message triggers the accept/decline prompt and
|
|
95
|
-
// gets held back. /
|
|
102
|
+
// gets held back. /branchaccept and /branchdecline (handled above as commands) bypass
|
|
96
103
|
// this — slash commands route to the command handler, not handleMessage.
|
|
97
|
-
if (await
|
|
104
|
+
if (await offerPendingBranchIfAny(ctx)) {
|
|
98
105
|
return;
|
|
99
106
|
}
|
|
100
107
|
// Check for active session — falls back to disk if the bot restarted recently.
|
|
@@ -102,6 +109,6 @@ export async function handleMessage(ctx) {
|
|
|
102
109
|
return;
|
|
103
110
|
fireAutoTopic(ctx, sessionKey, text);
|
|
104
111
|
// Queue the request - process one at a time per session
|
|
105
|
-
await runQueuedTurn(ctx, sessionKey, text, 'Message', () => runTurn(ctx, sessionKey,
|
|
112
|
+
await runQueuedTurn(ctx, sessionKey, text, 'Message', () => runTurn(ctx, sessionKey, text));
|
|
106
113
|
}
|
|
107
114
|
//# sourceMappingURL=message.handler.js.map
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* A bot with Bot Management Mode on (BotFather's Mini App) can offer a button
|
|
5
5
|
* that opens Telegram's own create-a-bot screen. Telegram reports the new bot
|
|
6
6
|
* in a `managed_bot_created` message, we fetch its token, lock it to its
|
|
7
|
-
* owner, and add it to instances.json with HOME_PROJECT set. The launcher
|
|
7
|
+
* owner, give it the TeleCoder icon, and add it to instances.json with HOME_PROJECT set. The launcher
|
|
8
8
|
* watches that file and starts it.
|
|
9
9
|
*
|
|
10
10
|
* Telegram doesn't say which button a new bot came from, so the project
|
|
@@ -14,9 +14,10 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import * as fs from 'fs';
|
|
16
16
|
import * as path from 'path';
|
|
17
|
-
import { Api } from 'grammy';
|
|
17
|
+
import { Api, InputFile } from 'grammy';
|
|
18
18
|
import { isAdmin } from '../../utils/admins.js';
|
|
19
19
|
import { sanitizeError } from '../../utils/sanitize.js';
|
|
20
|
+
import { BOT_ICON_FILENAME, botIconJpeg } from '../../utils/bot-icon.js';
|
|
20
21
|
import { addInstance } from '../../utils/instances.js';
|
|
21
22
|
import { InstancesConfigError } from '../../utils/instance-config.js';
|
|
22
23
|
import { getWorkspaceRoot } from '../../utils/workspace-guard.js';
|
|
@@ -64,7 +65,7 @@ export async function offerNewBot(api, userId, projectPath, threadId) {
|
|
|
64
65
|
};
|
|
65
66
|
}
|
|
66
67
|
if (!fs.existsSync(projectPath) || !fs.statSync(projectPath).isDirectory()) {
|
|
67
|
-
return { ok: false, message: `There's no project folder at ${projectPath}. Create it
|
|
68
|
+
return { ok: false, message: `There's no project folder at ${projectPath}. Create it first — ask Claude to, or run mkdir.` };
|
|
68
69
|
}
|
|
69
70
|
const project = path.basename(projectPath);
|
|
70
71
|
pending.set(userId, { projectPath, at: Date.now() });
|
|
@@ -89,7 +90,7 @@ export async function offerNewBot(api, userId, projectPath, threadId) {
|
|
|
89
90
|
});
|
|
90
91
|
return { ok: true, project };
|
|
91
92
|
}
|
|
92
|
-
/** The folder a project name or path means, the way /
|
|
93
|
+
/** The folder a project name or path means, the way /dir reads it. */
|
|
93
94
|
export function resolveProject(args) {
|
|
94
95
|
if (args.startsWith('~'))
|
|
95
96
|
return path.resolve(path.join(process.env.HOME || '', args.slice(1)));
|
|
@@ -182,6 +183,14 @@ export async function handleManagedBotCreated(ctx) {
|
|
|
182
183
|
catch (err) {
|
|
183
184
|
console.warn('[NewBot] Priming getUpdates failed:', sanitizeError(err));
|
|
184
185
|
}
|
|
186
|
+
// A plain letter on a coloured disc is hard to tell from the next bot's, so
|
|
187
|
+
// the new bot starts with the TeleCoder icon. Its owner can change it later.
|
|
188
|
+
try {
|
|
189
|
+
await new Api(token).setMyProfilePhoto({ type: 'static', photo: new InputFile(botIconJpeg(), BOT_ICON_FILENAME) });
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
console.warn('[NewBot] setMyProfilePhoto failed:', sanitizeError(err));
|
|
193
|
+
}
|
|
185
194
|
let name;
|
|
186
195
|
try {
|
|
187
196
|
name = addInstance({
|