loopengine 0.3.71 → 0.3.72
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/dist/core/http-notifier.js +4 -3
- package/dist/core/http-notify-triggers/telegram.d.ts +3 -0
- package/dist/core/http-notify-triggers/telegram.js +25 -14
- package/dist/core/messaging/connectors/telegram/format.d.ts +6 -0
- package/dist/core/messaging/connectors/telegram/format.js +55 -0
- package/dist/core/messaging/connectors/telegram/index.d.ts +1 -1
- package/dist/core/messaging/connectors/telegram/index.js +22 -15
- package/package.json +1 -1
|
@@ -185,9 +185,10 @@ function buildResolved(httpNotifier, agentDir) {
|
|
|
185
185
|
// linked to it, so replies to it reach the agent.
|
|
186
186
|
const telegram = new TelegramNotifier({
|
|
187
187
|
botToken, chatId, publicUrl, buttons, replyHint,
|
|
188
|
-
onPosted: ({ chat, messageId, text, pendingId, kind }) => {
|
|
189
|
-
|
|
190
|
-
|
|
188
|
+
onPosted: ({ chat, messageId, text, pendingId, kind, root }) => {
|
|
189
|
+
// A long request's later messages belong to its first.
|
|
190
|
+
recordTelegramMessage(agentDir, { chat, id: messageId, root: root ?? messageId, fromBot: true, text });
|
|
191
|
+
if (pendingId && kind && !root)
|
|
191
192
|
recordThread(agentDir, { app: 'telegram', conversation: chat, ts: messageId, pendingId, kind });
|
|
192
193
|
},
|
|
193
194
|
});
|
|
@@ -11,6 +11,9 @@ export interface TelegramPosted {
|
|
|
11
11
|
text: string;
|
|
12
12
|
pendingId?: string;
|
|
13
13
|
kind?: 'approval' | 'question';
|
|
14
|
+
/** For a request too long for one message: the first message, which
|
|
15
|
+
* the later ones belong to (a reply to any of them reaches it). */
|
|
16
|
+
root?: string;
|
|
14
17
|
}
|
|
15
18
|
export declare class TelegramNotifier implements DurableApprover, DurableQuestionHandler {
|
|
16
19
|
private readonly botToken;
|
|
@@ -15,7 +15,7 @@ import { randomUUID } from 'node:crypto';
|
|
|
15
15
|
import { decidePath } from '../approval-links.js';
|
|
16
16
|
import { formatArgs } from '../messaging/format-args.js';
|
|
17
17
|
import { callTelegram } from '../messaging/connectors/telegram/index.js';
|
|
18
|
-
import { telegramEscape, toTelegramHtml } from '../messaging/connectors/telegram/format.js';
|
|
18
|
+
import { splitTelegramMarkdown, telegramEscape, toTelegramHtml } from '../messaging/connectors/telegram/format.js';
|
|
19
19
|
import { telegramCallbackData } from '../messaging/connectors/telegram/updates.js';
|
|
20
20
|
import { publicBaseUrl } from './webhook.js';
|
|
21
21
|
// Telegram's limit for one message's text.
|
|
@@ -64,20 +64,31 @@ export class TelegramNotifier {
|
|
|
64
64
|
return base ? `[${kind === 'approval' ? 'Open to approve or deny' : 'Open to answer'}](${base}${decidePath(pendingId, kind)})` : '';
|
|
65
65
|
}
|
|
66
66
|
send(markdown, label, keyboard, request) {
|
|
67
|
-
// Not awaited, by DurableApprover's contract; a failure is logged.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
67
|
+
// Not awaited, by DurableApprover's contract; a failure is logged. Too
|
|
68
|
+
// long for one message, it goes out as several, in order: the later
|
|
69
|
+
// ones reply to the first, and the last carries the buttons.
|
|
70
|
+
const parts = splitTelegramMarkdown(markdown);
|
|
71
|
+
void (async () => {
|
|
72
|
+
let first;
|
|
73
|
+
for (const [i, part] of parts.entries()) {
|
|
74
|
+
const last = i === parts.length - 1;
|
|
75
|
+
const sent = await callTelegram(this.botToken, 'sendMessage', {
|
|
76
|
+
chat_id: this.chatId,
|
|
77
|
+
text: toTelegramHtml(part),
|
|
78
|
+
parse_mode: 'HTML',
|
|
79
|
+
link_preview_options: { is_disabled: true },
|
|
80
|
+
...(first ? { reply_parameters: { message_id: Number(first), allow_sending_without_reply: true } } : {}),
|
|
81
|
+
...(last && keyboard?.length ? { reply_markup: { inline_keyboard: keyboard } } : {}),
|
|
82
|
+
});
|
|
83
|
+
if (!sent.ok || !sent.result) {
|
|
84
|
+
console.error(`[loopengine] TelegramNotifier: send message failed for ${label}:`, sent.description);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const messageId = String(sent.result.message_id);
|
|
88
|
+
this.onPosted?.({ chat: String(sent.result.chat?.id ?? this.chatId), messageId, text: part, ...(request ?? {}), ...(first ? { root: first } : {}) });
|
|
89
|
+
first ??= messageId;
|
|
78
90
|
}
|
|
79
|
-
|
|
80
|
-
});
|
|
91
|
+
})();
|
|
81
92
|
}
|
|
82
93
|
requestDurableApproval(tool, args, scope, reason, pendingId = randomUUID()) {
|
|
83
94
|
const lines = [
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/** Marks text that isn't ours, so toTelegramHtml escapes it and leaves it
|
|
2
2
|
* as plain text. */
|
|
3
3
|
export declare function telegramEscape(text: string): string;
|
|
4
|
+
/** Splits Markdown into parts that each fit one Telegram message, between
|
|
5
|
+
* paragraphs where it can. A part ending inside the agent's escaped text
|
|
6
|
+
* (telegramEscape) or a code block closes it, and the next part opens it
|
|
7
|
+
* again, so every part formats on its own. Each is a little under the
|
|
8
|
+
* limit, leaving room for those markers and the cut-short note. */
|
|
9
|
+
export declare function splitTelegramMarkdown(markdown: string, max?: number): string[];
|
|
4
10
|
export declare function toTelegramHtml(markdown: string): string;
|
|
5
11
|
/** A call's arguments as Telegram HTML (core/messaging/format-args.ts). */
|
|
6
12
|
export declare function telegramArgs(args: Record<string, unknown>, schema?: Record<string, unknown>): string;
|
|
@@ -19,6 +19,61 @@ function html(text) {
|
|
|
19
19
|
export function telegramEscape(text) {
|
|
20
20
|
return OPEN + text.replace(/[\u0001\u0002]/g, '') + CLOSE;
|
|
21
21
|
}
|
|
22
|
+
// A long message goes out as several, at most this many; past that the
|
|
23
|
+
// middle is left out (the last part, with its link and buttons, stays).
|
|
24
|
+
const MAX_PARTS = 10;
|
|
25
|
+
/** Where a part may end: a paragraph break, else a line break, else a
|
|
26
|
+
* space, as late as fits; mid-word only when there's none in the later
|
|
27
|
+
* half. */
|
|
28
|
+
function breakAt(text, max) {
|
|
29
|
+
for (const sep of ['\n\n', '\n', ' ']) {
|
|
30
|
+
const at = text.lastIndexOf(sep, max);
|
|
31
|
+
if (at > max / 2)
|
|
32
|
+
return at;
|
|
33
|
+
}
|
|
34
|
+
return max;
|
|
35
|
+
}
|
|
36
|
+
/** Splits Markdown into parts that each fit one Telegram message, between
|
|
37
|
+
* paragraphs where it can. A part ending inside the agent's escaped text
|
|
38
|
+
* (telegramEscape) or a code block closes it, and the next part opens it
|
|
39
|
+
* again, so every part formats on its own. Each is a little under the
|
|
40
|
+
* limit, leaving room for those markers and the cut-short note. */
|
|
41
|
+
export function splitTelegramMarkdown(markdown, max = MAX_MARKDOWN - 100) {
|
|
42
|
+
const parts = [];
|
|
43
|
+
let rest = markdown;
|
|
44
|
+
while (rest.length > max) {
|
|
45
|
+
const at = breakAt(rest, max);
|
|
46
|
+
let head = rest.slice(0, at).trimEnd();
|
|
47
|
+
rest = rest.slice(at).replace(/^[ \n]+/, '');
|
|
48
|
+
let escaped = false;
|
|
49
|
+
let fenced = false;
|
|
50
|
+
for (let i = 0; i < head.length; i++) {
|
|
51
|
+
if (head[i] === OPEN)
|
|
52
|
+
escaped = true;
|
|
53
|
+
else if (head[i] === CLOSE)
|
|
54
|
+
escaped = false;
|
|
55
|
+
else if (!escaped && head.startsWith('```', i)) {
|
|
56
|
+
fenced = !fenced;
|
|
57
|
+
i += 2;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (escaped) {
|
|
61
|
+
head += CLOSE;
|
|
62
|
+
rest = OPEN + rest;
|
|
63
|
+
}
|
|
64
|
+
if (fenced) {
|
|
65
|
+
head += '\n```';
|
|
66
|
+
rest = '```\n' + rest;
|
|
67
|
+
}
|
|
68
|
+
parts.push(head);
|
|
69
|
+
}
|
|
70
|
+
parts.push(rest);
|
|
71
|
+
if (parts.length <= MAX_PARTS)
|
|
72
|
+
return parts;
|
|
73
|
+
const kept = parts.slice(0, MAX_PARTS - 1);
|
|
74
|
+
kept[kept.length - 1] += '\n\n… (the rest is cut short here; the decision link has it all)';
|
|
75
|
+
return [...kept, parts[parts.length - 1]];
|
|
76
|
+
}
|
|
22
77
|
export function toTelegramHtml(markdown) {
|
|
23
78
|
const cut = markdown.length > MAX_MARKDOWN ? markdown.slice(0, MAX_MARKDOWN).trimEnd() + '\n…' : markdown;
|
|
24
79
|
return cut
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { MessagingConnector } from '../../connector.js';
|
|
2
|
-
export { telegramArgs, telegramEscape, toTelegramHtml } from './format.js';
|
|
2
|
+
export { splitTelegramMarkdown, telegramArgs, telegramEscape, toTelegramHtml } from './format.js';
|
|
3
3
|
export { readTelegramThread, recordTelegramMessage, telegramPeople, telegramRootOf, type TelegramPerson } from './store.js';
|
|
4
4
|
export { parseTelegramUpdate, telegramCallbackData, verifyTelegramRequest, type TelegramButton, type TelegramUpdate } from './updates.js';
|
|
5
5
|
export interface TelegramResult<T = unknown> {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { telegramEscape, toTelegramHtml } from './format.js';
|
|
1
|
+
import { splitTelegramMarkdown, telegramEscape, toTelegramHtml } from './format.js';
|
|
2
2
|
import { readTelegramThread, recordTelegramMessage, telegramPeople } from './store.js';
|
|
3
3
|
import { telegramCallbackData } from './updates.js';
|
|
4
|
-
export { telegramArgs, telegramEscape, toTelegramHtml } from './format.js';
|
|
4
|
+
export { splitTelegramMarkdown, telegramArgs, telegramEscape, toTelegramHtml } from './format.js';
|
|
5
5
|
export { readTelegramThread, recordTelegramMessage, telegramPeople, telegramRootOf } from './store.js';
|
|
6
6
|
export { parseTelegramUpdate, telegramCallbackData, verifyTelegramRequest } from './updates.js';
|
|
7
7
|
/** One Bot API call. Never throws: a network failure reads as not ok. */
|
|
@@ -41,21 +41,28 @@ function personLabel(agentDir, user) {
|
|
|
41
41
|
return (person?.name ?? `user ${user}`).replace(/[*_~`[\]<>]/g, '');
|
|
42
42
|
}
|
|
43
43
|
export function createTelegramConnector(botToken, agentDir) {
|
|
44
|
+
// A reply too long for one message goes out as several, the last with
|
|
45
|
+
// the buttons; its id is the one returned (the buttons' message).
|
|
44
46
|
const send = async (conversation, threadTs, text, keyboard) => {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
47
|
+
const parts = splitTelegramMarkdown(text);
|
|
48
|
+
let id;
|
|
49
|
+
for (const [i, part] of parts.entries()) {
|
|
50
|
+
const last = i === parts.length - 1;
|
|
51
|
+
const sent = await callTelegram(botToken, 'sendMessage', {
|
|
52
|
+
chat_id: conversation,
|
|
53
|
+
text: toTelegramHtml(part),
|
|
54
|
+
parse_mode: 'HTML',
|
|
55
|
+
link_preview_options: { is_disabled: true },
|
|
56
|
+
...replyTo(threadTs),
|
|
57
|
+
...(last && keyboard ? { reply_markup: { inline_keyboard: keyboard } } : {}),
|
|
58
|
+
});
|
|
59
|
+
if (!sent.ok || !sent.result) {
|
|
60
|
+
console.error('[loopengine] Telegram sendMessage (thread reply) failed:', sent.description);
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
id = String(sent.result.message_id);
|
|
64
|
+
recordTelegramMessage(agentDir, { chat: conversation, id, root: threadTs, fromBot: true, text: part });
|
|
56
65
|
}
|
|
57
|
-
const id = String(sent.result.message_id);
|
|
58
|
-
recordTelegramMessage(agentDir, { chat: conversation, id, root: threadTs, fromBot: true, text });
|
|
59
66
|
return id;
|
|
60
67
|
};
|
|
61
68
|
return {
|