telecoder 0.0.0-stage → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +375 -0
- package/CHANGELOG.md +1867 -0
- package/LICENSE +22 -0
- package/README.md +130 -2
- package/dist/audio/transcribe.js +41 -0
- package/dist/bin/mcp-server.js +363 -0
- package/dist/bin/tool-result.js +22 -0
- package/dist/bot/access/group-access.js +147 -0
- package/dist/bot/bot.js +573 -0
- package/dist/bot/command-catalog.js +263 -0
- package/dist/bot/deleted-threads.js +103 -0
- package/dist/bot/file-access.js +150 -0
- package/dist/bot/handlers/album.handler.js +128 -0
- package/dist/bot/handlers/branch.handler.js +1096 -0
- package/dist/bot/handlers/command/access.js +407 -0
- package/dist/bot/handlers/command/admin.js +803 -0
- package/dist/bot/handlers/command/all-view.js +33 -0
- package/dist/bot/handlers/command/auto-approve.js +138 -0
- package/dist/bot/handlers/command/background.js +37 -0
- package/dist/bot/handlers/command/botname.js +76 -0
- package/dist/bot/handlers/command/btw.js +175 -0
- package/dist/bot/handlers/command/cleanup.js +329 -0
- package/dist/bot/handlers/command/model.js +226 -0
- package/dist/bot/handlers/command/prefs-scope.js +362 -0
- package/dist/bot/handlers/command/project.js +458 -0
- package/dist/bot/handlers/command/resume.js +325 -0
- package/dist/bot/handlers/command/session.js +299 -0
- package/dist/bot/handlers/command/settings.js +657 -0
- package/dist/bot/handlers/command/shared.js +207 -0
- package/dist/bot/handlers/command/tasks.js +681 -0
- package/dist/bot/handlers/command/thread.js +128 -0
- package/dist/bot/handlers/command/topic-store.js +74 -0
- package/dist/bot/handlers/command/topic.js +27 -0
- package/dist/bot/handlers/command/usage.js +382 -0
- package/dist/bot/handlers/command-fallback.js +179 -0
- package/dist/bot/handlers/command.handler.js +446 -0
- package/dist/bot/handlers/loop.handler.js +389 -0
- package/dist/bot/handlers/media-fallback.js +126 -0
- package/dist/bot/handlers/message/replies.js +56 -0
- package/dist/bot/handlers/message/shared.js +95 -0
- package/dist/bot/handlers/message/turn-notify.js +135 -0
- package/dist/bot/handlers/message/turn-runner.js +56 -0
- package/dist/bot/handlers/message.handler.js +114 -0
- package/dist/bot/handlers/newbot.handler.js +230 -0
- package/dist/bot/handlers/photo.handler.js +286 -0
- package/dist/bot/handlers/prompts.handler.js +343 -0
- package/dist/bot/handlers/running-note.js +44 -0
- package/dist/bot/handlers/session-guard.js +78 -0
- package/dist/bot/handlers/suggestion.handler.js +84 -0
- package/dist/bot/handlers/tree.handler.js +480 -0
- package/dist/bot/handlers/view.js +41 -0
- package/dist/bot/handlers/voice.handler.js +147 -0
- package/dist/bot/kept-threads.js +51 -0
- package/dist/bot/middleware/admin-only.js +49 -0
- package/dist/bot/middleware/album-collector.js +69 -0
- package/dist/bot/middleware/auth.middleware.js +122 -0
- package/dist/bot/middleware/group-mention.middleware.js +145 -0
- package/dist/bot/middleware/group-role.middleware.js +75 -0
- package/dist/bot/middleware/message-batcher.js +87 -0
- package/dist/bot/middleware/queued-messages.js +1082 -0
- package/dist/bot/middleware/stale-filter.js +9 -0
- package/dist/build-info.json +8 -0
- package/dist/claude/ask-user-form.js +517 -0
- package/dist/claude/ask-user.js +363 -0
- package/dist/claude/auto-approve.js +225 -0
- package/dist/claude/auto-topic-haiku.js +143 -0
- package/dist/claude/available-commands.js +156 -0
- package/dist/claude/background-hint.js +91 -0
- package/dist/claude/branch-tree.js +393 -0
- package/dist/claude/carry-over.js +25 -0
- package/dist/claude/ccr-shim.js +48 -0
- package/dist/claude/charter-judge.js +214 -0
- package/dist/claude/claude-auth.js +117 -0
- package/dist/claude/command-parser.js +206 -0
- package/dist/claude/cost-overlay-scraper.js +134 -0
- package/dist/claude/effort-level.js +5 -0
- package/dist/claude/enabled-plugins.js +52 -0
- package/dist/claude/in-flight-tracker.js +82 -0
- package/dist/claude/ipc-server.js +192 -0
- package/dist/claude/mcp-bridge.js +481 -0
- package/dist/claude/memory-guard.js +400 -0
- package/dist/claude/message-offsets.js +88 -0
- package/dist/claude/modal-relay.js +817 -0
- package/dist/claude/model-catalog.js +102 -0
- package/dist/claude/monitor-relay.js +712 -0
- package/dist/claude/native-commands.js +40 -0
- package/dist/claude/pending-branches.js +93 -0
- package/dist/claude/pending-suggestions.js +50 -0
- package/dist/claude/permission-gate.js +338 -0
- package/dist/claude/permission-mode.js +152 -0
- package/dist/claude/poll-user.js +51 -0
- package/dist/claude/prewarm.js +70 -0
- package/dist/claude/project-commands.js +69 -0
- package/dist/claude/prompt-hold.js +124 -0
- package/dist/claude/prompt-suggestion-scraper.js +89 -0
- package/dist/claude/pty-idle-reaper.js +96 -0
- package/dist/claude/pty-provider.js +2499 -0
- package/dist/claude/pty-spawn-config.js +343 -0
- package/dist/claude/pty-stream.js +363 -0
- package/dist/claude/pty-watch.js +201 -0
- package/dist/claude/request-queue.js +107 -0
- package/dist/claude/rewind.js +111 -0
- package/dist/claude/scheduled-runner.js +89 -0
- package/dist/claude/scheduler.js +241 -0
- package/dist/claude/scope-guard.js +279 -0
- package/dist/claude/screen-marks.js +83 -0
- package/dist/claude/screen-render.js +96 -0
- package/dist/claude/secret-request.js +576 -0
- package/dist/claude/session-history.js +288 -0
- package/dist/claude/session-jsonl.js +1067 -0
- package/dist/claude/session-manager.js +216 -0
- package/dist/claude/session-move.js +70 -0
- package/dist/claude/session-scan.js +270 -0
- package/dist/claude/setup-token.js +285 -0
- package/dist/claude/shell-mode-gate.js +60 -0
- package/dist/claude/side-call-options.js +45 -0
- package/dist/claude/side-question.js +252 -0
- package/dist/claude/stage-images.js +62 -0
- package/dist/claude/subagent-tools.js +12 -0
- package/dist/claude/synthetic-ctx.js +51 -0
- package/dist/claude/tasks-overlay-scraper.js +217 -0
- package/dist/claude/thread-judge-haiku.js +88 -0
- package/dist/claude/tip-scraper.js +77 -0
- package/dist/claude/tui-modal.js +746 -0
- package/dist/claude/tui-state.js +456 -0
- package/dist/claude/turn-end.js +105 -0
- package/dist/claude/turn-prose.js +64 -0
- package/dist/claude/typed-answer.js +104 -0
- package/dist/claude/update-banner-relay.js +70 -0
- package/dist/cli/commands/doctor.js +338 -0
- package/dist/cli/commands/help.js +34 -0
- package/dist/cli/commands/init.js +126 -0
- package/dist/cli/commands/service.js +710 -0
- package/dist/cli/commands/start.js +63 -0
- package/dist/cli/commands/version.js +11 -0
- package/dist/cli/commands/watch.js +322 -0
- package/dist/cli/dispatch.js +84 -0
- package/dist/cli/init/env-file.js +99 -0
- package/dist/cli/init/flow.js +705 -0
- package/dist/cli/init/prompter.js +124 -0
- package/dist/cli/init/telegram.js +120 -0
- package/dist/cli/service/render.js +212 -0
- package/dist/cli.js +18 -0
- package/dist/config-report.js +107 -0
- package/dist/config.js +525 -0
- package/dist/index.js +443 -0
- package/dist/launcher.js +960 -0
- package/dist/node-version-check.js +27 -0
- package/dist/providers/ccr-health.js +119 -0
- package/dist/providers/ccr-introspect.js +142 -0
- package/dist/providers/ccr-provider.js +59 -0
- package/dist/providers/claude-provider.js +10 -0
- package/dist/providers/prefs-sync.js +121 -0
- package/dist/providers/project-favorites.js +74 -0
- package/dist/providers/provider-router.js +128 -0
- package/dist/providers/provider-switch.js +91 -0
- package/dist/providers/spawn-settings.js +45 -0
- package/dist/providers/types.js +8 -0
- package/dist/providers/user-preferences.js +322 -0
- package/dist/restart-when-idle.js +173 -0
- package/dist/startup/auto-continue.js +67 -0
- package/dist/startup/auto-resume.js +161 -0
- package/dist/startup/notices.js +120 -0
- package/dist/startup/shared.js +44 -0
- package/dist/startup/verify-token.js +31 -0
- package/dist/telegram/access-request.js +153 -0
- package/dist/telegram/action-logger.js +522 -0
- package/dist/telegram/admin-mention.js +107 -0
- package/dist/telegram/background-activity-card.js +502 -0
- package/dist/telegram/botname-settings.js +282 -0
- package/dist/telegram/button-text.js +26 -0
- package/dist/telegram/client-options.js +26 -0
- package/dist/telegram/deduplication.js +33 -0
- package/dist/telegram/entities.js +295 -0
- package/dist/telegram/force-reply-tracker.js +96 -0
- package/dist/telegram/lettered-options.js +87 -0
- package/dist/telegram/markdown.js +362 -0
- package/dist/telegram/message-sender.js +1019 -0
- package/dist/telegram/prompt-label.js +148 -0
- package/dist/telegram/prompt-suggestion.js +54 -0
- package/dist/telegram/send.js +123 -0
- package/dist/telegram/streaming-turn.js +103 -0
- package/dist/telegram/suggestions-settings.js +30 -0
- package/dist/telegram/table-lists.js +56 -0
- package/dist/telegram/task-list.js +367 -0
- package/dist/telegram/task-tracker.js +128 -0
- package/dist/telegram/terminal-renderer.js +667 -0
- package/dist/telegram/thread-titles.js +262 -0
- package/dist/telegram/threads-setup.js +117 -0
- package/dist/utils/admins.js +143 -0
- package/dist/utils/agent-timer.js +55 -0
- package/dist/utils/atomic-write.js +24 -0
- package/dist/utils/bot-icon.js +248 -0
- package/dist/utils/bot-token.js +77 -0
- package/dist/utils/bounded-map.js +55 -0
- package/dist/utils/build-info.js +57 -0
- package/dist/utils/caffeinate.js +53 -0
- package/dist/utils/claude-env.js +53 -0
- package/dist/utils/claude-version.js +48 -0
- package/dist/utils/config-home.js +62 -0
- package/dist/utils/download.js +137 -0
- package/dist/utils/exit-codes.js +59 -0
- package/dist/utils/file-type.js +149 -0
- package/dist/utils/format.js +78 -0
- package/dist/utils/home-project.js +30 -0
- package/dist/utils/host-stall.js +51 -0
- package/dist/utils/id-list.js +44 -0
- package/dist/utils/idle-restart.js +92 -0
- package/dist/utils/install-mode.js +63 -0
- package/dist/utils/instance-config.js +148 -0
- package/dist/utils/instances.js +95 -0
- package/dist/utils/json-store.js +140 -0
- package/dist/utils/keyed-settings.js +62 -0
- package/dist/utils/legacy-env.js +35 -0
- package/dist/utils/memory-pressure.js +60 -0
- package/dist/utils/npm-version.js +134 -0
- package/dist/utils/prefs-relay.js +107 -0
- package/dist/utils/proc-children.js +102 -0
- package/dist/utils/resolve-bin.js +86 -0
- package/dist/utils/resolve-claude-bin.js +52 -0
- package/dist/utils/respawn-backoff.js +28 -0
- package/dist/utils/sanitize.js +91 -0
- package/dist/utils/session-key.js +37 -0
- package/dist/utils/stale-launcher.js +109 -0
- package/dist/utils/state-dir-migration.js +92 -0
- package/dist/utils/systemd.js +126 -0
- package/dist/utils/telegram-proxy.js +72 -0
- package/dist/utils/uploads-dir.js +27 -0
- package/dist/utils/user-roster.js +275 -0
- package/dist/utils/verbosity.js +118 -0
- package/dist/utils/version-check.js +166 -0
- package/dist/utils/workspace-guard.js +44 -0
- package/dist/worker-restart.js +141 -0
- package/instances.json.example +41 -0
- package/package.json +75 -4
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A prompt as TeleCoder shows it back: in /prompts and /history, on
|
|
3
|
+
* the branches /tree draws, in the status line under an answer, and at the
|
|
4
|
+
* bottom of the live action log.
|
|
5
|
+
*
|
|
6
|
+
* What the session log records as a prompt is often not what was typed: an
|
|
7
|
+
* upload arrives wrapped in a note saying where the file landed, a paste in
|
|
8
|
+
* the tags Claude Code puts around it, a reply under the quote it answers.
|
|
9
|
+
* These undo that, so a prompt reads the way it was sent.
|
|
10
|
+
*
|
|
11
|
+
* Lives here rather than with the /prompts command it started in because the
|
|
12
|
+
* action log needs it too, and that command's module imports the message
|
|
13
|
+
* sender, which imports the action log.
|
|
14
|
+
*/
|
|
15
|
+
import { userTextKind } from '../claude/session-jsonl.js';
|
|
16
|
+
// An upload never reaches claude as the caption you typed. photo.handler wraps
|
|
17
|
+
// it in a note saying where the file landed and what to do with it, and that
|
|
18
|
+
// whole scaffold is what the transcript records — so replaying it verbatim
|
|
19
|
+
// buries the one line you actually wrote under six you didn't. Claude then
|
|
20
|
+
// makes it worse on the way in: it lifts every file path out of the note into
|
|
21
|
+
// an `[Image #N]` chip, leaving `Saved at:` and ` 1.` pointing at nothing.
|
|
22
|
+
//
|
|
23
|
+
// Collapse those notes back to what the upload was: kind, name, caption.
|
|
24
|
+
// Both wordings are matched: the one the handlers write, and the one they
|
|
25
|
+
// wrote while uploads went to the project, which old transcripts still carry.
|
|
26
|
+
const UPLOAD_CHIPS = /^(?:\[(?:Image|Pasted text) #\d+(?: \+\d+ lines)?\]\s*)+/;
|
|
27
|
+
const IMAGE_NOTE = /^User (?:sent an image\.|uploaded an image to the project\.)$/;
|
|
28
|
+
const FILE_NOTE = /^User (?:sent|uploaded) a file: (.+)$/;
|
|
29
|
+
const ALBUM_NOTE = /^User sent (\d+) files together\.$/;
|
|
30
|
+
// The line each handler puts directly after the caption — the caption itself
|
|
31
|
+
// is a user-supplied string that may run over several lines.
|
|
32
|
+
const AFTER_CAPTION = /^(?:The images? (?:is|are) attached as (?:a )?vision input|Use the Read tool to read (?:its|their) contents)/;
|
|
33
|
+
function uploadCaption(lines) {
|
|
34
|
+
const start = lines.findIndex((l) => l.startsWith('Caption: '));
|
|
35
|
+
if (start === -1)
|
|
36
|
+
return undefined;
|
|
37
|
+
let end = lines.length;
|
|
38
|
+
for (let i = start + 1; i < lines.length; i++) {
|
|
39
|
+
if (AFTER_CAPTION.test(lines[i])) {
|
|
40
|
+
end = i;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const raw = lines.slice(start, end).join('\n').slice('Caption: '.length).trim();
|
|
45
|
+
if (raw === '(none)')
|
|
46
|
+
return undefined;
|
|
47
|
+
return raw.replace(/^"/, '').replace(/"$/, '').trim() || undefined;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Render an upload note as the upload it stands for, or return `text` unchanged
|
|
51
|
+
* when it isn't one. Matching is anchored on the note's own first line, so a
|
|
52
|
+
* prompt that merely quotes one is left alone.
|
|
53
|
+
*/
|
|
54
|
+
export function summarizeUserPrompt(text) {
|
|
55
|
+
const lines = text.replace(UPLOAD_CHIPS, '').split('\n');
|
|
56
|
+
const first = lines[0]?.trim() ?? '';
|
|
57
|
+
let label;
|
|
58
|
+
if (IMAGE_NOTE.test(first)) {
|
|
59
|
+
label = '📷 Image';
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const named = FILE_NOTE.exec(first);
|
|
63
|
+
const album = ALBUM_NOTE.exec(first);
|
|
64
|
+
if (named)
|
|
65
|
+
label = `📎 ${named[1].trim()}`;
|
|
66
|
+
else if (album)
|
|
67
|
+
label = `📎 ${album[1]} files`;
|
|
68
|
+
}
|
|
69
|
+
if (!label)
|
|
70
|
+
return text;
|
|
71
|
+
const caption = uploadCaption(lines);
|
|
72
|
+
return caption ? `${label} — ${caption}` : label;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A prompt as a label: what the user asked, cleaned of what got wrapped
|
|
76
|
+
* around it — or undefined when it wasn't a turn of the conversation at all,
|
|
77
|
+
* but a command.
|
|
78
|
+
*
|
|
79
|
+
* /tree names a branch after the first prompt past a split, and the status
|
|
80
|
+
* line lists the last few; on the evidence of real transcripts either is often
|
|
81
|
+
* a paste, a reply quoted back, or a /compact. A paste keeps its text and
|
|
82
|
+
* loses its tags; a quote gives way to the words the user wrote under it,
|
|
83
|
+
* which say what the prompt asks rather than what it answers.
|
|
84
|
+
*/
|
|
85
|
+
export function promptLabel(raw) {
|
|
86
|
+
const text = raw.trim();
|
|
87
|
+
if (userTextKind(text) !== 'prompt')
|
|
88
|
+
return undefined;
|
|
89
|
+
const unwrapped = summarizeUserPrompt(unwrapPaste(text));
|
|
90
|
+
const lines = unwrapped.split('\n');
|
|
91
|
+
const own = lines.filter((line) => !/^\s*>/.test(line)).join(' ');
|
|
92
|
+
const quoted = lines.map((line) => line.replace(/^\s*>\s?/, '')).join(' ');
|
|
93
|
+
const label = (own.trim() ? own : quoted)
|
|
94
|
+
.replace(/\s+/g, ' ')
|
|
95
|
+
.trim()
|
|
96
|
+
// A prompt written as a list starts with its marker, which says nothing.
|
|
97
|
+
.replace(/^(?:[-*•]|\d+[.)])\s+/, '');
|
|
98
|
+
return label || undefined;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* `text` without the tags Claude Code wraps a long or multi-line paste in,
|
|
102
|
+
* along with the line break it puts inside each, so a paste in the middle of a
|
|
103
|
+
* turn keeps the blank line around it and gains no extra one.
|
|
104
|
+
*/
|
|
105
|
+
export function unwrapPaste(text) {
|
|
106
|
+
return text.replace(/<pasted_content[^>]*>\n?|\n?<\/pasted_content[^>]*>/g, '').trim();
|
|
107
|
+
}
|
|
108
|
+
// What stands in for the middle of a prompt too long to show whole.
|
|
109
|
+
const ELISION = ' … ';
|
|
110
|
+
// How far a cut moves to fall between words rather than inside one: about the
|
|
111
|
+
// longest word. A path or a URL runs longer, and is cut where the cut falls.
|
|
112
|
+
const WORD_SLACK = 15;
|
|
113
|
+
/**
|
|
114
|
+
* `label` in at most `max` characters: whole when it fits, otherwise its start
|
|
115
|
+
* and its end with an ellipsis where the middle was. A long prompt usually
|
|
116
|
+
* closes on what it asks — "what do you think?" — so a cut that kept only the
|
|
117
|
+
* start dropped the question. The start gets two thirds, being what says what
|
|
118
|
+
* the prompt is about.
|
|
119
|
+
*
|
|
120
|
+
* Cut by code point, so an emoji at a cut isn't split in half, and between
|
|
121
|
+
* words when there's a space near, so neither side ends or starts mid-word.
|
|
122
|
+
*/
|
|
123
|
+
export function cutLabel(label, max) {
|
|
124
|
+
const chars = Array.from(label);
|
|
125
|
+
if (chars.length <= max)
|
|
126
|
+
return label;
|
|
127
|
+
const room = max - ELISION.length;
|
|
128
|
+
const start = chars.slice(0, startEndsAt(chars, Math.ceil((room * 2) / 3))).join('');
|
|
129
|
+
const end = chars.slice(endStartsAt(chars, chars.length - Math.floor(room / 3))).join('');
|
|
130
|
+
return start.trimEnd() + ELISION + end.trimStart();
|
|
131
|
+
}
|
|
132
|
+
/** Where the start ends: at `cut`, or back at the space before the word `cut` falls in. */
|
|
133
|
+
function startEndsAt(chars, cut) {
|
|
134
|
+
for (let i = cut; i > 0 && cut - i <= WORD_SLACK; i--) {
|
|
135
|
+
if (/\s/.test(chars[i]))
|
|
136
|
+
return i;
|
|
137
|
+
}
|
|
138
|
+
return cut;
|
|
139
|
+
}
|
|
140
|
+
/** Where the end starts: at `cut`, or on past the word `cut` falls in. */
|
|
141
|
+
function endStartsAt(chars, cut) {
|
|
142
|
+
for (let i = cut - 1; i < chars.length && i - cut < WORD_SLACK; i++) {
|
|
143
|
+
if (/\s/.test(chars[i]))
|
|
144
|
+
return i + 1;
|
|
145
|
+
}
|
|
146
|
+
return cut;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=prompt-label.js.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How Claude Code's predicted next prompt is rendered in the chat.
|
|
3
|
+
*
|
|
4
|
+
* The suggestion is scraped from one row of a 120-column terminal, so it can
|
|
5
|
+
* arrive nearly 120 characters wide — several times what an inline button
|
|
6
|
+
* shows before Telegram ellipsises it, with no way for the reader to expand
|
|
7
|
+
* it. Offering it as a button alone therefore delivered a half-sentence and
|
|
8
|
+
* dropped the rest: the user could still tap it, but could not read what they
|
|
9
|
+
* were about to send.
|
|
10
|
+
*
|
|
11
|
+
* So the same rule `telecoder_ask_user` follows applies here. Text rides on
|
|
12
|
+
* its own button while it fits, which keeps the common short suggestion a
|
|
13
|
+
* single tap under the response and adds nothing to the chat. Anything wider
|
|
14
|
+
* goes into its own message — a card — where the full prompt is in the body
|
|
15
|
+
* and the button beneath it is a fixed short label that cannot be clipped.
|
|
16
|
+
*/
|
|
17
|
+
import { EntityText } from './entities.js';
|
|
18
|
+
import { fitsOnButton } from './button-text.js';
|
|
19
|
+
const BULB = '💡';
|
|
20
|
+
/** Fixed label for the card's button; the prompt itself is in the body above. */
|
|
21
|
+
export const SUGGESTION_CARD_BUTTON = `${BULB} Send this prompt`;
|
|
22
|
+
/**
|
|
23
|
+
* Collapse scraped ghost text to a single clean line. The flattened form is
|
|
24
|
+
* what gets stored, shown and ultimately sent, so what the user reads is
|
|
25
|
+
* exactly what Claude receives.
|
|
26
|
+
*/
|
|
27
|
+
export function flattenSuggestion(text) {
|
|
28
|
+
return text.replace(/\s+/gu, ' ').trim();
|
|
29
|
+
}
|
|
30
|
+
/** Button label for a suggestion short enough to carry its own text. */
|
|
31
|
+
export function formatSuggestionLabel(text) {
|
|
32
|
+
return `${BULB} ${flattenSuggestion(text)}`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* True when the whole suggestion — bulb and all — survives on a button. False
|
|
36
|
+
* means it needs a card; nothing is ever truncated on the way to the user.
|
|
37
|
+
*/
|
|
38
|
+
export function suggestionFitsOnButton(text) {
|
|
39
|
+
return fitsOnButton(formatSuggestionLabel(text));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The card body: a plain header over the prompt in italics.
|
|
43
|
+
*
|
|
44
|
+
* `sent: true` is the same message rewritten once the button has been tapped.
|
|
45
|
+
* The card is the only place the prompt is written out, so it has to keep
|
|
46
|
+
* showing it — but it must stop reading as an offer, the way an answered
|
|
47
|
+
* question grows a "✅ Answered" footer instead of silently losing its
|
|
48
|
+
* keyboard.
|
|
49
|
+
*/
|
|
50
|
+
export function buildSuggestionCardText(text, opts = {}) {
|
|
51
|
+
const header = opts.sent ? '✅ Sent:' : `${BULB} Suggested next prompt:`;
|
|
52
|
+
return new EntityText().add(header).newline().italic(flattenSuggestion(text)).build();
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=prompt-suggestion.js.map
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sending formatted text to Telegram: every message goes out as plain text
|
|
3
|
+
* plus entities (see ./entities.ts), never with a parse mode.
|
|
4
|
+
*
|
|
5
|
+
* These wrap the three things a handler does with text — reply, send to a
|
|
6
|
+
* chat, edit — so a call site hands over an {@link EntityMessage} (or
|
|
7
|
+
* markdown) and the splitting, trimming and the plain-text fallback happen in
|
|
8
|
+
* one place.
|
|
9
|
+
*/
|
|
10
|
+
import { GrammyError } from 'grammy';
|
|
11
|
+
import { trimMessage, plain } from './entities.js';
|
|
12
|
+
import { formatMarkdown, splitEntityMessage, MAX_ENTITIES, MAX_MESSAGE_LENGTH } from './markdown.js';
|
|
13
|
+
function asMessage(text) {
|
|
14
|
+
return trimMessage(typeof text === 'string' ? plain(text) : text);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Telegram turned the formatting down rather than the message. It only does
|
|
18
|
+
* that for an entity it won't accept — a link it can't open, an offset off
|
|
19
|
+
* the end — and the same text without them will go through.
|
|
20
|
+
*/
|
|
21
|
+
export function isEntityError(error) {
|
|
22
|
+
if (!(error instanceof GrammyError) || error.error_code !== 400)
|
|
23
|
+
return false;
|
|
24
|
+
return /entit|can't parse|wrong (?:http )?url|url host is empty|unsupported url protocol/i.test(error.description);
|
|
25
|
+
}
|
|
26
|
+
/** `other` with the message's entities on it, and no parse mode. */
|
|
27
|
+
function withEntities(other, entities) {
|
|
28
|
+
const { parse_mode: _ignored, ...rest } = (other ?? {});
|
|
29
|
+
return { ...rest, entities };
|
|
30
|
+
}
|
|
31
|
+
/** Post one message through `post`, unformatted again if Telegram refuses the formatting. */
|
|
32
|
+
async function postOne(post, text, other) {
|
|
33
|
+
const message = fitOne(asMessage(text));
|
|
34
|
+
try {
|
|
35
|
+
return await post(message.text || '…', withEntities(other, message.entities));
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (!isEntityError(error) || message.entities.length === 0)
|
|
39
|
+
throw error;
|
|
40
|
+
console.error('[Send] Telegram refused the formatting, sending plain text:', error.description);
|
|
41
|
+
return post(message.text, withEntities(other, []));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function apiPoster(api, chatId) {
|
|
45
|
+
return (text, other) => api.sendMessage(chatId, text, other);
|
|
46
|
+
}
|
|
47
|
+
/** `ctx.reply`, which keeps a reply in the forum topic it answers. */
|
|
48
|
+
function replyPoster(ctx) {
|
|
49
|
+
return (text, other) => ctx.reply(text, other);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Send one message — it must fit in one; see {@link sendParts} for anything
|
|
53
|
+
* longer. Retries once unformatted if Telegram refuses the formatting.
|
|
54
|
+
*/
|
|
55
|
+
export async function sendFormatted(api, chatId, text, other) {
|
|
56
|
+
return postOne(apiPoster(api, chatId), text, other);
|
|
57
|
+
}
|
|
58
|
+
/** {@link sendFormatted} as a reply in the chat (and topic) `ctx` came from. */
|
|
59
|
+
export async function replyFormatted(ctx, text, other) {
|
|
60
|
+
return postOne(replyPoster(ctx), text, other);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Edit a message's text. A message that has grown past Telegram's limit is
|
|
64
|
+
* clipped: an edit can't split, so whoever needs the rest must send it.
|
|
65
|
+
* Retries unformatted if Telegram refuses the formatting.
|
|
66
|
+
*/
|
|
67
|
+
export async function editFormatted(api, chatId, messageId, text, other) {
|
|
68
|
+
const message = fitOne(asMessage(text));
|
|
69
|
+
try {
|
|
70
|
+
await api.editMessageText(chatId, messageId, message.text || '…', withEntities(other, message.entities));
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (!isEntityError(error) || message.entities.length === 0)
|
|
74
|
+
throw error;
|
|
75
|
+
console.error('[Send] Telegram refused the formatting, editing to plain text:', error.description);
|
|
76
|
+
await api.editMessageText(chatId, messageId, message.text, withEntities(other, []));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** {@link editFormatted} on the message a callback query's button sits on. */
|
|
80
|
+
export async function editCallbackMessage(ctx, text, other) {
|
|
81
|
+
const message = ctx.callbackQuery?.message;
|
|
82
|
+
const chatId = message?.chat.id ?? ctx.chat?.id;
|
|
83
|
+
if (!message || chatId === undefined)
|
|
84
|
+
throw new Error('editCallbackMessage: no message on this update');
|
|
85
|
+
await editFormatted(ctx.api, chatId, message.message_id, text, other);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Send a message of any length as however many parts it needs. `other` goes on
|
|
89
|
+
* every part except `reply_markup`, which goes on the last, under the end of
|
|
90
|
+
* the text. Returns the parts as sent.
|
|
91
|
+
*/
|
|
92
|
+
export async function sendParts(api, chatId, text, other) {
|
|
93
|
+
return postSplit(apiPoster(api, chatId), splitEntityMessage(asMessage(text)), other);
|
|
94
|
+
}
|
|
95
|
+
/** {@link sendParts} from markdown: the way the model's own text is sent. */
|
|
96
|
+
export async function sendMarkdown(api, chatId, markdown, other) {
|
|
97
|
+
return postSplit(apiPoster(api, chatId), formatMarkdown(markdown), other);
|
|
98
|
+
}
|
|
99
|
+
/** {@link sendParts} as replies in the chat (and topic) `ctx` came from. */
|
|
100
|
+
export async function replyParts(ctx, text, other) {
|
|
101
|
+
return postSplit(replyPoster(ctx), splitEntityMessage(asMessage(text)), other);
|
|
102
|
+
}
|
|
103
|
+
/** {@link sendMarkdown} as replies in the chat (and topic) `ctx` came from. */
|
|
104
|
+
export async function replyMarkdown(ctx, markdown, other) {
|
|
105
|
+
return postSplit(replyPoster(ctx), formatMarkdown(markdown), other);
|
|
106
|
+
}
|
|
107
|
+
async function postSplit(post, parts, other) {
|
|
108
|
+
const { reply_markup, ...rest } = other ?? {};
|
|
109
|
+
const sent = [];
|
|
110
|
+
for (let i = 0; i < parts.length; i++) {
|
|
111
|
+
const last = i === parts.length - 1;
|
|
112
|
+
sent.push(await postOne(post, parts[i], last && reply_markup ? { ...rest, reply_markup } : rest));
|
|
113
|
+
}
|
|
114
|
+
return sent;
|
|
115
|
+
}
|
|
116
|
+
/** A message cut down to one Telegram message, ending in an ellipsis. */
|
|
117
|
+
function fitOne(message) {
|
|
118
|
+
if (message.text.length <= MAX_MESSAGE_LENGTH && message.entities.length <= MAX_ENTITIES)
|
|
119
|
+
return message;
|
|
120
|
+
const [first] = splitEntityMessage(message, MAX_MESSAGE_LENGTH - 1, { markers: false });
|
|
121
|
+
return { text: first.text + '…', entities: first.entities };
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=send.js.map
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of running a streamed agent turn that don't depend on *why* the
|
|
3
|
+
* turn is running.
|
|
4
|
+
*
|
|
5
|
+
* Lives under `telegram/` rather than with the message handlers because the
|
|
6
|
+
* scheduler drives turns too, and `claude/scheduled-runner.ts` cannot import
|
|
7
|
+
* the handler layer without closing a cycle. Everything here depends only on
|
|
8
|
+
* the message sender, the request queue and the verbosity settings — all of
|
|
9
|
+
* which sit below both callers.
|
|
10
|
+
*/
|
|
11
|
+
import { setAbortController } from '../claude/request-queue.js';
|
|
12
|
+
import { resolveVerbosityFlags } from '../utils/verbosity.js';
|
|
13
|
+
import { messageSender } from './message-sender.js';
|
|
14
|
+
/**
|
|
15
|
+
* Build the `onToolResult` callback passed to the provider runner. Resolves
|
|
16
|
+
* verbosity flags lazily on each event so a /verbosity change mid-turn takes
|
|
17
|
+
* effect immediately, and short-circuits when the chat opts out of tool
|
|
18
|
+
* previews entirely.
|
|
19
|
+
*/
|
|
20
|
+
export function makeToolResultHandler(ctx) {
|
|
21
|
+
return (event) => {
|
|
22
|
+
const cid = ctx.chat?.id;
|
|
23
|
+
if (cid === undefined)
|
|
24
|
+
return;
|
|
25
|
+
const flags = resolveVerbosityFlags(cid);
|
|
26
|
+
if (!flags.showToolResults)
|
|
27
|
+
return;
|
|
28
|
+
return messageSender.postToolResult(ctx, event, flags.toolResultMaxLines, flags.toolResultMaxChars);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Companion to `makeToolResultHandler` for Edit/Write diff previews. */
|
|
32
|
+
export function makeEditDiffHandler(ctx) {
|
|
33
|
+
return (event) => {
|
|
34
|
+
const cid = ctx.chat?.id;
|
|
35
|
+
if (cid === undefined)
|
|
36
|
+
return;
|
|
37
|
+
const flags = resolveVerbosityFlags(cid);
|
|
38
|
+
if (!flags.showDiffs)
|
|
39
|
+
return;
|
|
40
|
+
return messageSender.postEditDiff(ctx, event, flags.diffMaxLines);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Callbacks that render a turn's progress into the streaming message. Shared
|
|
45
|
+
* by every entry point that starts a turn.
|
|
46
|
+
*/
|
|
47
|
+
export function progressCallbacks(ctx) {
|
|
48
|
+
return {
|
|
49
|
+
onProgress: (progressText) => {
|
|
50
|
+
messageSender.updateStream(ctx, progressText);
|
|
51
|
+
},
|
|
52
|
+
onTip: (tip) => {
|
|
53
|
+
messageSender.updateTip(ctx, tip);
|
|
54
|
+
},
|
|
55
|
+
onBackgroundHint: (offered) => {
|
|
56
|
+
messageSender.updateBackgroundHint(ctx, offered);
|
|
57
|
+
},
|
|
58
|
+
onToolResult: makeToolResultHandler(ctx),
|
|
59
|
+
onEditDiff: makeEditDiffHandler(ctx),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Per-tool telemetry on top of `progressCallbacks`, for the `sendToAgent`
|
|
64
|
+
* paths that surface the running tool in the stream header.
|
|
65
|
+
*/
|
|
66
|
+
export function toolCallbacks(ctx, sessionKey) {
|
|
67
|
+
return {
|
|
68
|
+
onToolStart: (toolName, input) => {
|
|
69
|
+
messageSender.updateToolOperation(sessionKey, toolName, input, ctx);
|
|
70
|
+
},
|
|
71
|
+
onToolEnd: () => {
|
|
72
|
+
messageSender.clearToolOperation(sessionKey);
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Open a streaming message for a turn sent `prompt`, run `body` against a
|
|
78
|
+
* fresh abort controller, and guarantee the stream is torn down if anything
|
|
79
|
+
* throws. The prompt is shown again at the bottom of the action log, which
|
|
80
|
+
* pushes the message it came in off screen.
|
|
81
|
+
*
|
|
82
|
+
* Every caller previously repeated this preamble by hand. Two invariants are
|
|
83
|
+
* easy to lose that way and are now structural: the session's abort controller
|
|
84
|
+
* is always registered before the provider is called (so /cancel can reach the
|
|
85
|
+
* turn), and a failure always replaces the spinner with the error rather than
|
|
86
|
+
* leaving it animating forever.
|
|
87
|
+
*
|
|
88
|
+
* The error is re-thrown after teardown so the caller's own queue handling —
|
|
89
|
+
* "Queue cleared" suppression, error replies — still sees it.
|
|
90
|
+
*/
|
|
91
|
+
export async function withStreamingTurn(ctx, sessionKey, prompt, body) {
|
|
92
|
+
await messageSender.startStreaming(ctx, prompt);
|
|
93
|
+
const abortController = new AbortController();
|
|
94
|
+
setAbortController(sessionKey, abortController);
|
|
95
|
+
try {
|
|
96
|
+
await body(abortController);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
await messageSender.cancelStreaming(ctx, error);
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=streaming-turn.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-chat prompt-suggestion settings — controls whether TeleCoder enables
|
|
3
|
+
* Claude Code's `prompt_suggestion` feature for this session and scrapes the
|
|
4
|
+
* resulting ghost text. Persisted to ~/.telecoder/suggestions-settings.json.
|
|
5
|
+
*
|
|
6
|
+
* Takes effect on next PTY spawn (the env var is set at spawn time). A
|
|
7
|
+
* mid-session toggle is recorded but won't activate until the user starts a
|
|
8
|
+
* new session or the PTY is restarted for any other reason.
|
|
9
|
+
*/
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { config } from '../config.js';
|
|
12
|
+
import { createKeyedSettings } from '../utils/keyed-settings.js';
|
|
13
|
+
const store = createKeyedSettings({
|
|
14
|
+
file: 'suggestions-settings.json',
|
|
15
|
+
label: 'Suggestions',
|
|
16
|
+
entrySchema: z.object({ enabled: z.boolean().optional() }),
|
|
17
|
+
normalize: (stored) => ({
|
|
18
|
+
enabled: typeof stored?.enabled === 'boolean' ? stored.enabled : config.PROMPT_SUGGESTIONS_DEFAULT,
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
21
|
+
export function getSuggestionsSettings(sessionKey) {
|
|
22
|
+
return store.get(sessionKey);
|
|
23
|
+
}
|
|
24
|
+
export function setSuggestionsEnabled(sessionKey, enabled) {
|
|
25
|
+
store.update(sessionKey, { enabled });
|
|
26
|
+
}
|
|
27
|
+
export function isSuggestionsEnabled(sessionKey) {
|
|
28
|
+
return store.get(sessionKey).enabled;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=suggestions-settings.js.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown tables rewritten as bullet lists, for a report made of narrow
|
|
3
|
+
* label-and-number tables.
|
|
4
|
+
*
|
|
5
|
+
* Telegram has no tables, so markdown.ts draws each row as a block of
|
|
6
|
+
* `Header: cell` lines. That suits the prose tables Claude writes in a reply,
|
|
7
|
+
* but Claude Code's `/context` report is three tables of short rows, and drawn
|
|
8
|
+
* that way it runs past a hundred lines. Here each row is one line, its first
|
|
9
|
+
* cell in bold and the rest after it, and the other column headers go above
|
|
10
|
+
* the list in italics so the numbers keep their names:
|
|
11
|
+
*
|
|
12
|
+
* _Tokens · Percentage_
|
|
13
|
+
* - **System prompt:** 3.8k · 0.4%
|
|
14
|
+
*
|
|
15
|
+
* Everything that isn't a table passes through unchanged.
|
|
16
|
+
*/
|
|
17
|
+
export function tablesToLists(markdown) {
|
|
18
|
+
const lines = markdown.split('\n');
|
|
19
|
+
const out = [];
|
|
20
|
+
let i = 0;
|
|
21
|
+
while (i < lines.length) {
|
|
22
|
+
const header = cells(lines[i]);
|
|
23
|
+
if (!header || !SEPARATOR_RE.test(lines[i + 1] ?? '')) {
|
|
24
|
+
out.push(lines[i]);
|
|
25
|
+
i++;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
i += 2;
|
|
29
|
+
const names = header.slice(1).filter(Boolean);
|
|
30
|
+
if (names.length > 0)
|
|
31
|
+
out.push(`_${names.join(' · ')}_`);
|
|
32
|
+
for (let row = cells(lines[i] ?? ''); row; row = cells(lines[++i] ?? '')) {
|
|
33
|
+
const [first, ...rest] = row;
|
|
34
|
+
const values = rest.filter(Boolean).join(' · ');
|
|
35
|
+
if (first && values)
|
|
36
|
+
out.push(`- **${first}:** ${values}`);
|
|
37
|
+
else if (first || values)
|
|
38
|
+
out.push(`- ${first || values}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return out.join('\n');
|
|
42
|
+
}
|
|
43
|
+
/** A table's header rule: `|---|:--:|---|`, outer pipes optional. */
|
|
44
|
+
const SEPARATOR_RE = /^\s*\|?\s*:?-{3,}:?\s*(?:\|\s*:?-{3,}:?\s*)*\|?\s*$/;
|
|
45
|
+
/** A table row's cells, trimmed, or null when the line isn't a row. */
|
|
46
|
+
function cells(line) {
|
|
47
|
+
const t = line.trim();
|
|
48
|
+
if (!t.startsWith('|'))
|
|
49
|
+
return null;
|
|
50
|
+
return t
|
|
51
|
+
.replace(/^\|/, '')
|
|
52
|
+
.replace(/(?<!\\)\|$/, '')
|
|
53
|
+
.split(/(?<!\\)\|/)
|
|
54
|
+
.map((cell) => cell.trim());
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=table-lists.js.map
|