telecoder 0.0.0-stage → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +363 -0
- package/CHANGELOG.md +1186 -0
- package/LICENSE +22 -0
- package/README.md +126 -2
- package/dist/audio/transcribe.js +50 -0
- package/dist/bin/mcp-server.js +378 -0
- package/dist/bin/tool-result.js +22 -0
- package/dist/bot/access/group-access.js +147 -0
- package/dist/bot/bot.js +592 -0
- package/dist/bot/file-access.js +141 -0
- package/dist/bot/handlers/command/access.js +407 -0
- package/dist/bot/handlers/command/admin.js +827 -0
- package/dist/bot/handlers/command/auto-approve.js +138 -0
- package/dist/bot/handlers/command/btw.js +224 -0
- package/dist/bot/handlers/command/cost.js +409 -0
- package/dist/bot/handlers/command/media.js +197 -0
- package/dist/bot/handlers/command/model.js +305 -0
- package/dist/bot/handlers/command/prefs-scope.js +197 -0
- package/dist/bot/handlers/command/project.js +536 -0
- package/dist/bot/handlers/command/resume.js +321 -0
- package/dist/bot/handlers/command/session.js +438 -0
- package/dist/bot/handlers/command/settings.js +1012 -0
- package/dist/bot/handlers/command/shared.js +210 -0
- package/dist/bot/handlers/command/streaming-mode.js +16 -0
- package/dist/bot/handlers/command/tasks.js +513 -0
- package/dist/bot/handlers/command/topic-store.js +113 -0
- package/dist/bot/handlers/command/topic.js +80 -0
- package/dist/bot/handlers/command.handler.js +772 -0
- package/dist/bot/handlers/fork.handler.js +1109 -0
- package/dist/bot/handlers/media-fallback.js +121 -0
- package/dist/bot/handlers/message/replies.js +133 -0
- package/dist/bot/handlers/message/shared.js +117 -0
- package/dist/bot/handlers/message/throttle.js +49 -0
- package/dist/bot/handlers/message/turn-notify.js +169 -0
- package/dist/bot/handlers/message/turn-runner.js +141 -0
- package/dist/bot/handlers/message.handler.js +107 -0
- package/dist/bot/handlers/newbot.handler.js +221 -0
- package/dist/bot/handlers/photo.handler.js +327 -0
- package/dist/bot/handlers/prompts.handler.js +343 -0
- package/dist/bot/handlers/running-note.js +44 -0
- package/dist/bot/handlers/schedule.handler.js +303 -0
- package/dist/bot/handlers/session-guard.js +78 -0
- package/dist/bot/handlers/suggestion.handler.js +84 -0
- package/dist/bot/handlers/tree.handler.js +480 -0
- package/dist/bot/handlers/view.js +41 -0
- package/dist/bot/handlers/voice.handler.js +209 -0
- package/dist/bot/middleware/admin-only.js +49 -0
- package/dist/bot/middleware/auth.middleware.js +122 -0
- package/dist/bot/middleware/group-mention.middleware.js +139 -0
- package/dist/bot/middleware/group-role.middleware.js +75 -0
- package/dist/bot/middleware/message-batcher.js +87 -0
- package/dist/bot/middleware/queued-messages.js +971 -0
- package/dist/bot/middleware/stale-filter.js +31 -0
- package/dist/build-info.json +8 -0
- package/dist/claude/agent-watchdog.js +162 -0
- package/dist/claude/agent.js +1458 -0
- package/dist/claude/ask-user.js +295 -0
- package/dist/claude/auto-approve.js +225 -0
- package/dist/claude/auto-topic-haiku.js +126 -0
- package/dist/claude/available-commands.js +153 -0
- package/dist/claude/carry-over.js +27 -0
- package/dist/claude/ccr-shim.js +46 -0
- package/dist/claude/charter-judge.js +214 -0
- package/dist/claude/claude-auth.js +121 -0
- package/dist/claude/command-parser.js +200 -0
- package/dist/claude/cost-overlay-scraper.js +129 -0
- package/dist/claude/enabled-plugins.js +56 -0
- package/dist/claude/fork-tree.js +393 -0
- package/dist/claude/held-query.js +190 -0
- package/dist/claude/in-flight-tracker.js +61 -0
- package/dist/claude/ipc-server.js +192 -0
- package/dist/claude/mcp-bridge.js +453 -0
- package/dist/claude/mcp-tools.js +354 -0
- package/dist/claude/message-offsets.js +88 -0
- package/dist/claude/modal-relay.js +450 -0
- package/dist/claude/model-catalog.js +85 -0
- package/dist/claude/monitor-relay.js +597 -0
- package/dist/claude/pending-forks.js +93 -0
- package/dist/claude/pending-suggestions.js +50 -0
- package/dist/claude/permission-gate.js +337 -0
- package/dist/claude/permission-mode.js +178 -0
- package/dist/claude/poll-user.js +51 -0
- package/dist/claude/project-commands.js +69 -0
- package/dist/claude/prompt-hold.js +124 -0
- package/dist/claude/prompt-suggestion-scraper.js +89 -0
- package/dist/claude/pty-provider.js +2214 -0
- package/dist/claude/pty-spawn-config.js +296 -0
- package/dist/claude/pty-stream.js +363 -0
- package/dist/claude/request-queue.js +135 -0
- package/dist/claude/scheduled-runner.js +91 -0
- package/dist/claude/scheduler.js +237 -0
- package/dist/claude/scope-guard.js +274 -0
- package/dist/claude/sdk-permission-gate.js +77 -0
- package/dist/claude/secret-request.js +576 -0
- package/dist/claude/session-cost.js +41 -0
- package/dist/claude/session-history.js +286 -0
- package/dist/claude/session-jsonl.js +920 -0
- package/dist/claude/session-manager.js +195 -0
- package/dist/claude/session-scan.js +270 -0
- package/dist/claude/setup-token.js +285 -0
- package/dist/claude/side-call-options.js +45 -0
- package/dist/claude/side-question.js +249 -0
- package/dist/claude/stage-images.js +62 -0
- package/dist/claude/subagent-tools.js +12 -0
- package/dist/claude/synthetic-ctx.js +51 -0
- package/dist/claude/system-prompt.js +162 -0
- package/dist/claude/tasks-overlay-scraper.js +94 -0
- package/dist/claude/tip-scraper.js +78 -0
- package/dist/claude/tui-modal.js +501 -0
- package/dist/claude/tui-state.js +384 -0
- package/dist/claude/turn-end.js +91 -0
- package/dist/claude/turn-prose.js +64 -0
- package/dist/claude/update-banner-relay.js +70 -0
- package/dist/cli/commands/doctor.js +305 -0
- package/dist/cli/commands/help.js +25 -0
- package/dist/cli/commands/init.js +123 -0
- package/dist/cli/commands/service.js +678 -0
- package/dist/cli/commands/start.js +53 -0
- package/dist/cli/commands/version.js +11 -0
- package/dist/cli/dispatch.js +79 -0
- package/dist/cli/init/env-file.js +99 -0
- package/dist/cli/init/flow.js +638 -0
- package/dist/cli/init/prompter.js +116 -0
- package/dist/cli/init/telegram.js +94 -0
- package/dist/cli/service/render.js +212 -0
- package/dist/cli.js +18 -0
- package/dist/config-report.js +107 -0
- package/dist/config.js +500 -0
- package/dist/index.js +367 -0
- package/dist/launcher.js +933 -0
- package/dist/node-version-check.js +27 -0
- package/dist/providers/ccr-health.js +119 -0
- package/dist/providers/ccr-introspect.js +149 -0
- package/dist/providers/ccr-provider.js +60 -0
- package/dist/providers/claude-provider.js +63 -0
- package/dist/providers/prefs-sync.js +164 -0
- package/dist/providers/project-favorites.js +74 -0
- package/dist/providers/provider-router.js +127 -0
- package/dist/providers/provider-switch.js +91 -0
- package/dist/providers/sdk-provider.js +37 -0
- package/dist/providers/types.js +8 -0
- package/dist/providers/user-preferences.js +214 -0
- package/dist/restart-when-idle.js +153 -0
- package/dist/startup/auto-continue.js +67 -0
- package/dist/startup/auto-resume.js +173 -0
- package/dist/startup/notices.js +120 -0
- package/dist/startup/shared.js +44 -0
- package/dist/startup/verify-token.js +31 -0
- package/dist/telegram/access-request.js +153 -0
- package/dist/telegram/action-logger.js +684 -0
- package/dist/telegram/admin-mention.js +107 -0
- package/dist/telegram/background-activity-card.js +502 -0
- package/dist/telegram/botname-settings.js +336 -0
- package/dist/telegram/button-text.js +26 -0
- package/dist/telegram/client-options.js +27 -0
- package/dist/telegram/deduplication.js +33 -0
- package/dist/telegram/entities.js +295 -0
- package/dist/telegram/force-reply-tracker.js +57 -0
- package/dist/telegram/lettered-options.js +87 -0
- package/dist/telegram/markdown.js +362 -0
- package/dist/telegram/message-sender.js +1287 -0
- package/dist/telegram/prompt-label.js +142 -0
- package/dist/telegram/prompt-suggestion.js +54 -0
- package/dist/telegram/send.js +123 -0
- package/dist/telegram/streaming-turn.js +105 -0
- package/dist/telegram/suggestions-settings.js +30 -0
- package/dist/telegram/task-list.js +367 -0
- package/dist/telegram/task-tracker.js +209 -0
- package/dist/telegram/telegraph-settings.js +31 -0
- package/dist/telegram/telegraph.js +453 -0
- package/dist/telegram/terminal-renderer.js +671 -0
- package/dist/telegram/terminal-settings.js +25 -0
- package/dist/telegram/thread-titles.js +190 -0
- package/dist/telegram/threads-setup.js +117 -0
- package/dist/tts/tts-settings.js +62 -0
- package/dist/tts/tts.js +245 -0
- package/dist/tts/voice-reply.js +78 -0
- package/dist/utils/admins.js +143 -0
- package/dist/utils/agent-timer.js +55 -0
- package/dist/utils/atomic-write.js +24 -0
- package/dist/utils/bot-token.js +74 -0
- package/dist/utils/bounded-map.js +55 -0
- package/dist/utils/build-info.js +57 -0
- package/dist/utils/caffeinate.js +50 -0
- package/dist/utils/claude-env.js +54 -0
- package/dist/utils/claude-version.js +48 -0
- package/dist/utils/config-home.js +62 -0
- package/dist/utils/debug-agent.js +117 -0
- package/dist/utils/debug-watchdog.js +121 -0
- package/dist/utils/download.js +111 -0
- package/dist/utils/exit-codes.js +48 -0
- package/dist/utils/file-type.js +149 -0
- package/dist/utils/format.js +81 -0
- package/dist/utils/home-project.js +30 -0
- package/dist/utils/host-stall.js +51 -0
- package/dist/utils/id-list.js +44 -0
- package/dist/utils/idle-restart.js +92 -0
- package/dist/utils/install-mode.js +63 -0
- package/dist/utils/instance-config.js +148 -0
- package/dist/utils/instances.js +95 -0
- package/dist/utils/json-store.js +140 -0
- package/dist/utils/keyed-settings.js +62 -0
- package/dist/utils/legacy-env.js +35 -0
- package/dist/utils/npm-version.js +134 -0
- package/dist/utils/prefs-relay.js +107 -0
- package/dist/utils/proc-children.js +102 -0
- package/dist/utils/project-data-dir.js +33 -0
- package/dist/utils/reasoning-summary.js +12 -0
- package/dist/utils/resolve-bin.js +86 -0
- package/dist/utils/resolve-claude-bin.js +63 -0
- package/dist/utils/respawn-backoff.js +19 -0
- package/dist/utils/sanitize.js +91 -0
- package/dist/utils/session-key.js +37 -0
- package/dist/utils/stale-launcher.js +109 -0
- package/dist/utils/state-dir-migration.js +92 -0
- package/dist/utils/systemd.js +126 -0
- package/dist/utils/user-roster.js +275 -0
- package/dist/utils/verbosity.js +118 -0
- package/dist/utils/version-check.js +166 -0
- package/dist/utils/workspace-guard.js +44 -0
- package/dist/worker-restart.js +141 -0
- package/instances.json.example +41 -0
- package/package.json +76 -4
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown, as Claude writes it, turned into a Telegram message: plain text
|
|
3
|
+
* with the formatting carried as entities. See ./entities.ts for why nothing
|
|
4
|
+
* goes out with a parse mode.
|
|
5
|
+
*
|
|
6
|
+
* The markdown is parsed into a syntax tree (CommonMark plus GitHub's tables,
|
|
7
|
+
* strikethrough and bare-URL links) and the tree is walked into an
|
|
8
|
+
* {@link EntityText}. Markdown that doesn't parse as formatting — an unclosed
|
|
9
|
+
* `**`, a half-written link while a reply is still streaming — comes out as the
|
|
10
|
+
* literal characters, never as an error.
|
|
11
|
+
*
|
|
12
|
+
* What it renders, kept as the earlier MarkdownV2 conversion showed it:
|
|
13
|
+
* headings in bold, `•` and `1.` list markers, `———` for a thematic break, and
|
|
14
|
+
* a table as one block of `**Header**: cell` lines per row, since Telegram has
|
|
15
|
+
* no tables. `<expandable>…</expandable>` becomes a quote collapsed to its
|
|
16
|
+
* first lines.
|
|
17
|
+
*/
|
|
18
|
+
import { fromMarkdown } from 'mdast-util-from-markdown';
|
|
19
|
+
import { gfmFromMarkdown } from 'mdast-util-gfm';
|
|
20
|
+
import { gfm } from 'micromark-extension-gfm';
|
|
21
|
+
import { EntityText, indentMessage, isHighSurrogate, isLinkableUrl, sliceMessage, trimMessage, } from './entities.js';
|
|
22
|
+
/** Telegram's cap on a message's text, in UTF-16 code units. */
|
|
23
|
+
export const MAX_MESSAGE_LENGTH = 4096;
|
|
24
|
+
/** Telegram applies at most this many entities to a message. */
|
|
25
|
+
export const MAX_ENTITIES = 100;
|
|
26
|
+
/** Room kept free in every part of a split message for its `[2/3]` marker. */
|
|
27
|
+
const PART_MARKER_RESERVE = 16;
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// Markdown → entities
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
const EXPANDABLE_TAG_RE = /<expandable>([\s\S]*?)<\/expandable>/g;
|
|
32
|
+
const expandablePlaceholder = (i) => `XCGEXPANDABLEBLOCK${i}ENDX`;
|
|
33
|
+
const EXPANDABLE_PLACEHOLDER_RE = /^XCGEXPANDABLEBLOCK(\d+)ENDX$/;
|
|
34
|
+
/**
|
|
35
|
+
* Pull `<expandable>` regions out before parsing, outside fenced code, so a
|
|
36
|
+
* markdown parser that reads them as raw HTML can't split them across nodes.
|
|
37
|
+
* Each leaves a placeholder paragraph of its own.
|
|
38
|
+
*/
|
|
39
|
+
function extractExpandableBlocks(text) {
|
|
40
|
+
const blocks = [];
|
|
41
|
+
const parts = text.split(/(```[\s\S]*?```)/g);
|
|
42
|
+
const processed = parts.map((segment, i) => {
|
|
43
|
+
if (i % 2 === 1)
|
|
44
|
+
return segment;
|
|
45
|
+
return segment.replace(EXPANDABLE_TAG_RE, (_, body) => {
|
|
46
|
+
blocks.push(body.replace(/^\n+|\n+$/g, ''));
|
|
47
|
+
return `\n\n${expandablePlaceholder(blocks.length - 1)}\n\n`;
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
return { text: processed.join(''), blocks };
|
|
51
|
+
}
|
|
52
|
+
/** Convert markdown to a Telegram message: plain text plus entities. */
|
|
53
|
+
export function markdownToEntities(markdown) {
|
|
54
|
+
const { text, blocks } = extractExpandableBlocks(markdown);
|
|
55
|
+
return trimMessage(renderMarkdown(text, { inQuote: false, expandables: blocks }));
|
|
56
|
+
}
|
|
57
|
+
function renderMarkdown(markdown, ctx) {
|
|
58
|
+
let tree;
|
|
59
|
+
try {
|
|
60
|
+
tree = fromMarkdown(markdown, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] });
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
// The parser accepts any string; this is belt and braces. Plain text is
|
|
64
|
+
// the one rendering that can't be refused.
|
|
65
|
+
console.error('[Markdown] Parse failed, sending as plain text:', error);
|
|
66
|
+
return { text: markdown, entities: [] };
|
|
67
|
+
}
|
|
68
|
+
return renderBlocks(tree.children, ctx, '\n\n');
|
|
69
|
+
}
|
|
70
|
+
function renderBlocks(nodes, ctx, separator) {
|
|
71
|
+
const b = new EntityText();
|
|
72
|
+
let first = true;
|
|
73
|
+
for (const node of nodes) {
|
|
74
|
+
const rendered = renderBlock(node, ctx);
|
|
75
|
+
if (rendered === null)
|
|
76
|
+
continue;
|
|
77
|
+
if (!first)
|
|
78
|
+
b.add(separator);
|
|
79
|
+
b.append(rendered);
|
|
80
|
+
first = false;
|
|
81
|
+
}
|
|
82
|
+
return b.build();
|
|
83
|
+
}
|
|
84
|
+
/** One block-level node, or null for one that renders nothing (a link definition). */
|
|
85
|
+
function renderBlock(node, ctx) {
|
|
86
|
+
const b = new EntityText();
|
|
87
|
+
switch (node.type) {
|
|
88
|
+
case 'paragraph': {
|
|
89
|
+
const placeholder = paragraphPlaceholder(node.children);
|
|
90
|
+
if (placeholder !== null)
|
|
91
|
+
return renderExpandable(ctx.expandables[placeholder] ?? '', ctx);
|
|
92
|
+
renderInline(b, node.children);
|
|
93
|
+
return b.build();
|
|
94
|
+
}
|
|
95
|
+
case 'heading':
|
|
96
|
+
b.wrap('bold', (inner) => renderInline(inner, node.children));
|
|
97
|
+
return b.build();
|
|
98
|
+
case 'thematicBreak':
|
|
99
|
+
return b.add('———').build();
|
|
100
|
+
case 'code':
|
|
101
|
+
// An empty block has nothing to show, and a `pre` over nothing is refused.
|
|
102
|
+
if (!node.value)
|
|
103
|
+
return null;
|
|
104
|
+
return b.pre(node.value, node.lang ?? undefined).build();
|
|
105
|
+
case 'blockquote': {
|
|
106
|
+
const inner = renderBlocks(node.children, { ...ctx, inQuote: true }, '\n');
|
|
107
|
+
if (ctx.inQuote)
|
|
108
|
+
return inner;
|
|
109
|
+
return b.quote(inner).build();
|
|
110
|
+
}
|
|
111
|
+
case 'list':
|
|
112
|
+
return renderList(node, ctx);
|
|
113
|
+
case 'table':
|
|
114
|
+
return renderTable(node);
|
|
115
|
+
case 'html':
|
|
116
|
+
return b.add(node.value).build();
|
|
117
|
+
case 'definition':
|
|
118
|
+
return null;
|
|
119
|
+
case 'footnoteDefinition': {
|
|
120
|
+
b.add(`[^${node.identifier}]: `);
|
|
121
|
+
b.append(indentMessage(renderBlocks(node.children, ctx, '\n'), ' ', true));
|
|
122
|
+
return b.build();
|
|
123
|
+
}
|
|
124
|
+
default:
|
|
125
|
+
// Anything phrasing-level that turns up at block level.
|
|
126
|
+
renderInline(b, [node]);
|
|
127
|
+
return b.build();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function paragraphPlaceholder(children) {
|
|
131
|
+
if (children.length !== 1 || children[0].type !== 'text')
|
|
132
|
+
return null;
|
|
133
|
+
const match = EXPANDABLE_PLACEHOLDER_RE.exec(children[0].value.trim());
|
|
134
|
+
return match ? Number(match[1]) : null;
|
|
135
|
+
}
|
|
136
|
+
function renderExpandable(body, ctx) {
|
|
137
|
+
const inner = trimMessage(renderMarkdown(body, { ...ctx, inQuote: true }));
|
|
138
|
+
if (!inner.text)
|
|
139
|
+
return null;
|
|
140
|
+
if (ctx.inQuote)
|
|
141
|
+
return inner;
|
|
142
|
+
return new EntityText().quote(inner, true).build();
|
|
143
|
+
}
|
|
144
|
+
function renderList(list, ctx) {
|
|
145
|
+
const b = new EntityText();
|
|
146
|
+
const start = list.start ?? 1;
|
|
147
|
+
list.children.forEach((item, i) => {
|
|
148
|
+
if (i > 0)
|
|
149
|
+
b.newline();
|
|
150
|
+
b.append(renderListItem(item, list.ordered ? `${start + i}. ` : '• ', ctx));
|
|
151
|
+
});
|
|
152
|
+
return b.build();
|
|
153
|
+
}
|
|
154
|
+
function renderListItem(item, marker, ctx) {
|
|
155
|
+
const b = new EntityText().add(marker);
|
|
156
|
+
if (item.checked === true)
|
|
157
|
+
b.add('☑ ');
|
|
158
|
+
else if (item.checked === false)
|
|
159
|
+
b.add('☐ ');
|
|
160
|
+
const body = renderBlocks(item.children, ctx, '\n');
|
|
161
|
+
// Continuation lines — a second paragraph, a nested list — sit indented
|
|
162
|
+
// under the item rather than flush with the margin.
|
|
163
|
+
return b.append(indentMessage(body, ' ', true)).build();
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* A table as one block per data row, each cell on a line of its own under its
|
|
167
|
+
* column's header in bold. A table with a header row only lists the headers.
|
|
168
|
+
*/
|
|
169
|
+
function renderTable(table) {
|
|
170
|
+
const [headerRow, ...rows] = table.children;
|
|
171
|
+
const headers = headerRow?.children ?? [];
|
|
172
|
+
const b = new EntityText();
|
|
173
|
+
if (rows.length === 0) {
|
|
174
|
+
headers.forEach((cell, i) => {
|
|
175
|
+
if (i > 0)
|
|
176
|
+
b.newline();
|
|
177
|
+
b.add('- ');
|
|
178
|
+
renderInline(b, cell.children);
|
|
179
|
+
});
|
|
180
|
+
return b.build();
|
|
181
|
+
}
|
|
182
|
+
rows.forEach((row, r) => {
|
|
183
|
+
if (r > 0)
|
|
184
|
+
b.newline(2);
|
|
185
|
+
let firstLine = true;
|
|
186
|
+
row.children.forEach((cell, c) => {
|
|
187
|
+
if (cell.children.length === 0)
|
|
188
|
+
return;
|
|
189
|
+
if (!firstLine)
|
|
190
|
+
b.newline();
|
|
191
|
+
firstLine = false;
|
|
192
|
+
const header = headers[c];
|
|
193
|
+
if (header && header.children.length > 0) {
|
|
194
|
+
// The label is bold already, so a header that is bold in the markdown
|
|
195
|
+
// too doesn't need a second bold inside the first.
|
|
196
|
+
b.wrap('bold', (inner) => renderInline(inner, withoutStrong(header.children)));
|
|
197
|
+
b.add(': ');
|
|
198
|
+
}
|
|
199
|
+
renderInline(b, cell.children);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
return b.build();
|
|
203
|
+
}
|
|
204
|
+
function withoutStrong(nodes) {
|
|
205
|
+
return nodes.flatMap((n) => (n.type === 'strong' ? withoutStrong(n.children) : [n]));
|
|
206
|
+
}
|
|
207
|
+
function renderInline(b, nodes) {
|
|
208
|
+
for (const node of nodes) {
|
|
209
|
+
switch (node.type) {
|
|
210
|
+
case 'text':
|
|
211
|
+
b.add(node.value);
|
|
212
|
+
break;
|
|
213
|
+
case 'strong':
|
|
214
|
+
b.wrap('bold', (inner) => renderInline(inner, node.children));
|
|
215
|
+
break;
|
|
216
|
+
case 'emphasis':
|
|
217
|
+
b.wrap('italic', (inner) => renderInline(inner, node.children));
|
|
218
|
+
break;
|
|
219
|
+
case 'delete':
|
|
220
|
+
b.wrap('strikethrough', (inner) => renderInline(inner, node.children));
|
|
221
|
+
break;
|
|
222
|
+
case 'inlineCode':
|
|
223
|
+
b.code(node.value);
|
|
224
|
+
break;
|
|
225
|
+
case 'break':
|
|
226
|
+
b.newline();
|
|
227
|
+
break;
|
|
228
|
+
case 'link':
|
|
229
|
+
renderLink(b, node.url, node.children);
|
|
230
|
+
break;
|
|
231
|
+
case 'image':
|
|
232
|
+
b.link(node.alt || node.url, node.url);
|
|
233
|
+
break;
|
|
234
|
+
case 'linkReference':
|
|
235
|
+
case 'imageReference':
|
|
236
|
+
// An unresolved reference: what the model typed, minus the brackets.
|
|
237
|
+
if ('children' in node)
|
|
238
|
+
renderInline(b, node.children);
|
|
239
|
+
else
|
|
240
|
+
b.add(node.alt ?? '');
|
|
241
|
+
break;
|
|
242
|
+
case 'footnoteReference':
|
|
243
|
+
b.add(`[^${node.identifier}]`);
|
|
244
|
+
break;
|
|
245
|
+
case 'html':
|
|
246
|
+
b.add(node.value);
|
|
247
|
+
break;
|
|
248
|
+
default:
|
|
249
|
+
b.add(textOf(node));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* A link keeps the formatting inside its label. One Telegram won't take (see
|
|
255
|
+
* {@link EntityText.link}) is written out as its label and address instead.
|
|
256
|
+
*/
|
|
257
|
+
function renderLink(b, url, children) {
|
|
258
|
+
const label = children.map(textOf).join('');
|
|
259
|
+
if (!isLinkableUrl(url)) {
|
|
260
|
+
b.link(label, url);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (!label) {
|
|
264
|
+
b.link(url, url);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
b.wrap('text_link', (inner) => renderInline(inner, children), { url });
|
|
268
|
+
}
|
|
269
|
+
function textOf(node) {
|
|
270
|
+
if ('value' in node && typeof node.value === 'string')
|
|
271
|
+
return node.value;
|
|
272
|
+
if ('children' in node)
|
|
273
|
+
return node.children.map((c) => textOf(c)).join('');
|
|
274
|
+
if (node.type === 'image')
|
|
275
|
+
return node.alt ?? '';
|
|
276
|
+
return '';
|
|
277
|
+
}
|
|
278
|
+
// ---------------------------------------------------------------------------
|
|
279
|
+
// Splitting
|
|
280
|
+
// ---------------------------------------------------------------------------
|
|
281
|
+
/**
|
|
282
|
+
* Split a message into parts Telegram will take: at most `maxLength` code
|
|
283
|
+
* units and {@link MAX_ENTITIES} entities each. Parts break at a paragraph, a
|
|
284
|
+
* line or a word where one is near enough to the limit, and anywhere at all
|
|
285
|
+
* otherwise — formatting that runs across a break continues on the other side,
|
|
286
|
+
* a code block included. With more than one part, each ends in `[n/total]`.
|
|
287
|
+
*/
|
|
288
|
+
export function splitEntityMessage(message, maxLength = MAX_MESSAGE_LENGTH, { markers = true } = {}) {
|
|
289
|
+
let rest = trimMessage(message);
|
|
290
|
+
if (fits(rest, maxLength))
|
|
291
|
+
return [rest];
|
|
292
|
+
const budget = Math.max(1, maxLength - (markers ? PART_MARKER_RESERVE : 0));
|
|
293
|
+
// The marker is an entity too.
|
|
294
|
+
const entityBudget = markers ? MAX_ENTITIES - 1 : MAX_ENTITIES;
|
|
295
|
+
const parts = [];
|
|
296
|
+
while (!fits(rest, budget, entityBudget)) {
|
|
297
|
+
const cut = findCut(rest, budget, entityBudget);
|
|
298
|
+
const part = trimMessage(sliceMessage(rest, 0, cut));
|
|
299
|
+
if (part.text)
|
|
300
|
+
parts.push(part);
|
|
301
|
+
rest = trimLeading(sliceMessage(rest, cut));
|
|
302
|
+
}
|
|
303
|
+
rest = trimMessage(rest);
|
|
304
|
+
if (rest.text)
|
|
305
|
+
parts.push(rest);
|
|
306
|
+
if (parts.length <= 1 || !markers)
|
|
307
|
+
return parts;
|
|
308
|
+
return parts.map((part, i) => {
|
|
309
|
+
const b = new EntityText().append(part).newline(2).italic(`[${i + 1}/${parts.length}]`);
|
|
310
|
+
return b.length <= maxLength ? b.build() : part;
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
function fits(message, maxLength, maxEntities = MAX_ENTITIES) {
|
|
314
|
+
return message.text.length <= maxLength && message.entities.length <= maxEntities;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Where to end the next part. Prefers the last paragraph break, then line
|
|
318
|
+
* break, then space, in the back half of the window; inside a code block only
|
|
319
|
+
* a line break, so no line of code is cut in two unless it has to be.
|
|
320
|
+
*/
|
|
321
|
+
function findCut(message, budget, entityBudget) {
|
|
322
|
+
const { text, entities } = message;
|
|
323
|
+
let limit = Math.min(budget, text.length);
|
|
324
|
+
if (entities.length > entityBudget) {
|
|
325
|
+
// End the part before the first entity it has no room for.
|
|
326
|
+
const starts = entities.map((e) => e.offset).sort((a, b) => a - b);
|
|
327
|
+
const overflowAt = starts[entityBudget];
|
|
328
|
+
if (overflowAt > 0)
|
|
329
|
+
limit = Math.min(limit, overflowAt);
|
|
330
|
+
}
|
|
331
|
+
if (limit >= text.length)
|
|
332
|
+
return text.length;
|
|
333
|
+
const window = text.slice(0, limit);
|
|
334
|
+
const half = limit / 2;
|
|
335
|
+
const inPre = entities.some((e) => e.type === 'pre' && e.offset < limit && e.offset + e.length > limit);
|
|
336
|
+
let cut = limit;
|
|
337
|
+
const breaks = inPre ? ['\n'] : ['\n\n', '\n', ' '];
|
|
338
|
+
for (const sep of breaks) {
|
|
339
|
+
const at = window.lastIndexOf(sep);
|
|
340
|
+
if (at > half) {
|
|
341
|
+
cut = at + sep.length;
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (cut > 1 && cut < text.length && isHighSurrogate(text.charCodeAt(cut - 1)))
|
|
346
|
+
cut--;
|
|
347
|
+
return Math.max(1, cut);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Drop whitespace from the front of the next part — but inside a code block
|
|
351
|
+
* only newlines, since the spaces there are the code's indentation.
|
|
352
|
+
*/
|
|
353
|
+
function trimLeading(message) {
|
|
354
|
+
const startsInPre = message.entities.some((e) => e.type === 'pre' && e.offset === 0);
|
|
355
|
+
const match = (startsInPre ? /^\n+/ : /^\s+/).exec(message.text);
|
|
356
|
+
return match ? sliceMessage(message, match[0].length) : message;
|
|
357
|
+
}
|
|
358
|
+
/** Markdown to the message parts it is sent as. */
|
|
359
|
+
export function formatMarkdown(markdown, maxLength = MAX_MESSAGE_LENGTH) {
|
|
360
|
+
return splitEntityMessage(markdownToEntities(markdown), maxLength);
|
|
361
|
+
}
|
|
362
|
+
//# sourceMappingURL=markdown.js.map
|