loopengine 0.3.73 → 0.3.75
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-notify-triggers/lark.js +9 -3
- package/dist/core/http-notify-triggers/slack.js +6 -4
- package/dist/core/http-notify-triggers/telegram.js +4 -4
- package/dist/core/messaging/connectors/slack/format.d.ts +5 -0
- package/dist/core/messaging/connectors/slack/format.js +59 -10
- package/dist/core/messaging/connectors/slack/index.d.ts +1 -1
- package/dist/core/messaging/connectors/slack/index.js +1 -1
- package/dist/core/messaging/connectors/telegram/format.d.ts +6 -0
- package/dist/core/messaging/connectors/telegram/format.js +72 -16
- package/dist/core/messaging/connectors/telegram/index.d.ts +1 -1
- package/dist/core/messaging/connectors/telegram/index.js +2 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
|
@@ -60,6 +60,12 @@ async function getTenantAccessToken(appId, appSecret) {
|
|
|
60
60
|
cachedToken = { token: body.tenant_access_token, expiresAt: Date.now() + body.expire * 1000 };
|
|
61
61
|
return cachedToken.token;
|
|
62
62
|
}
|
|
63
|
+
/** The agent's own writing in a lark_md card: its Markdown shows as it
|
|
64
|
+
* is, but a link shows its address instead of hiding behind text, and it
|
|
65
|
+
* can't @mention everyone. */
|
|
66
|
+
function larkAgentText(text) {
|
|
67
|
+
return text.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)/g, '$1 ($2)').replace(/<at\b/gi, '‹at');
|
|
68
|
+
}
|
|
63
69
|
export class LarkNotifier {
|
|
64
70
|
appId;
|
|
65
71
|
appSecret;
|
|
@@ -93,7 +99,7 @@ export class LarkNotifier {
|
|
|
93
99
|
tag: 'div',
|
|
94
100
|
text: {
|
|
95
101
|
tag: 'lark_md',
|
|
96
|
-
content: `**tool:** ${tool}\n**args:** \`${JSON.stringify(args)}\`\n**scope:** ${scope.tenant}/${scope.environment}/${scope.agent}\n**reason:** ${reason}${this.decideLine(pendingId, 'approval')}`,
|
|
102
|
+
content: `**tool:** ${tool}\n**args:** \`${JSON.stringify(args)}\`\n**scope:** ${scope.tenant}/${scope.environment}/${scope.agent}\n**reason:** ${larkAgentText(reason)}${this.decideLine(pendingId, 'approval')}`,
|
|
97
103
|
},
|
|
98
104
|
},
|
|
99
105
|
...(this.buttons
|
|
@@ -128,7 +134,7 @@ export class LarkNotifier {
|
|
|
128
134
|
config: { wide_screen_mode: true },
|
|
129
135
|
header: { title: { tag: 'plain_text', content: `Question from ${agent}` } },
|
|
130
136
|
elements: [
|
|
131
|
-
{ tag: 'div', text: { tag: 'lark_md', content: question + (sessionId ? `\n_(session ${sessionId})_` : '') + this.decideLine(pendingId, 'question') } },
|
|
137
|
+
{ tag: 'div', text: { tag: 'lark_md', content: larkAgentText(question) + (sessionId ? `\n_(session ${sessionId})_` : '') + this.decideLine(pendingId, 'question') } },
|
|
132
138
|
...(this.buttons
|
|
133
139
|
? [
|
|
134
140
|
...(optionButtons.length ? [{ tag: 'action', actions: optionButtons }] : []),
|
|
@@ -164,7 +170,7 @@ export class LarkNotifier {
|
|
|
164
170
|
const token = await getTenantAccessToken(this.appId, this.appSecret);
|
|
165
171
|
const card = {
|
|
166
172
|
config: { wide_screen_mode: true },
|
|
167
|
-
elements: [{ tag: 'div', text: { tag: 'lark_md', content: text } }],
|
|
173
|
+
elements: [{ tag: 'div', text: { tag: 'lark_md', content: larkAgentText(text) } }],
|
|
168
174
|
};
|
|
169
175
|
await this.sendCard(token, card, 'lifecycle');
|
|
170
176
|
}
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
// call shape).
|
|
26
26
|
import { randomUUID } from 'node:crypto';
|
|
27
27
|
import { decidePath } from '../approval-links.js';
|
|
28
|
-
import {
|
|
28
|
+
import { slackAgentText, slackArgs, toSlackMrkdwn } from '../messaging/connectors/slack/format.js';
|
|
29
29
|
import { publicBaseUrl } from './webhook.js';
|
|
30
30
|
export class SlackNotifier {
|
|
31
31
|
botToken;
|
|
@@ -74,7 +74,7 @@ export class SlackNotifier {
|
|
|
74
74
|
type: 'mrkdwn',
|
|
75
75
|
// Arguments as labelled lines, long text quoted (not JSON);
|
|
76
76
|
// a Slack section holds 3,000 characters, so they're kept under.
|
|
77
|
-
text: fitSection(`*Approval requested:* \`${tool}\``, slackArgs(args), `*Why:* ${toSlackMrkdwn(
|
|
77
|
+
text: fitSection(`*Approval requested:* \`${tool}\``, slackArgs(args), `*Why:* ${toSlackMrkdwn(slackAgentText(reason))}${this.decideLine(pendingId, 'approval')}`),
|
|
78
78
|
},
|
|
79
79
|
},
|
|
80
80
|
{ type: 'context', elements: [{ type: 'mrkdwn', text: `${scope.agent} · ${scope.tenant}/${scope.environment}` }] },
|
|
@@ -101,7 +101,7 @@ export class SlackNotifier {
|
|
|
101
101
|
action_id: `answer_option_${i}`,
|
|
102
102
|
}));
|
|
103
103
|
this.postMessage(`Question from ${agent}: ${question}`, [
|
|
104
|
-
{ type: 'section', text: { type: 'mrkdwn', text: fitSection(`*Question from ${agent}${sessionId ? ` (session ${sessionId})` : ''}:*`, toSlackMrkdwn(
|
|
104
|
+
{ type: 'section', text: { type: 'mrkdwn', text: fitSection(`*Question from ${agent}${sessionId ? ` (session ${sessionId})` : ''}:*`, toSlackMrkdwn(slackAgentText(question)), this.decideLine(pendingId, 'question').trim()) } },
|
|
105
105
|
...(this.buttons
|
|
106
106
|
? [{ type: 'actions', elements: [...optionButtons, { type: 'button', text: { type: 'plain_text', text: 'Other…' }, action_id: 'open_answer_modal', value: pendingId }] }]
|
|
107
107
|
: []),
|
|
@@ -122,7 +122,9 @@ export class SlackNotifier {
|
|
|
122
122
|
fetch('https://slack.com/api/chat.postMessage', {
|
|
123
123
|
method: 'POST',
|
|
124
124
|
headers: { Authorization: `Bearer ${this.botToken}`, 'content-type': 'application/json; charset=utf-8' },
|
|
125
|
-
|
|
125
|
+
// A plain announcement (a run's result) is the agent's writing:
|
|
126
|
+
// formatted, escaped and with links showing (slackAgentText).
|
|
127
|
+
body: JSON.stringify({ channel: this.channelId, text: blocks ? text : toSlackMrkdwn(slackAgentText(text)), ...(blocks ? { blocks } : {}) }),
|
|
126
128
|
})
|
|
127
129
|
.then((res) => res.json())
|
|
128
130
|
.then((body) => {
|
|
@@ -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 { splitTelegramMarkdown, telegramEscape, toTelegramHtml } from '../messaging/connectors/telegram/format.js';
|
|
18
|
+
import { splitTelegramMarkdown, telegramAgentText, 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.
|
|
@@ -94,7 +94,7 @@ export class TelegramNotifier {
|
|
|
94
94
|
const lines = [
|
|
95
95
|
`**Approval requested:** \`${tool}\``,
|
|
96
96
|
formatArgs(args, telegramEscape),
|
|
97
|
-
`**Why:** ${
|
|
97
|
+
`**Why:** ${telegramAgentText(reason)}`,
|
|
98
98
|
this.decideLine(pendingId, 'approval'),
|
|
99
99
|
telegramEscape(`${scope.agent} · ${scope.tenant}/${scope.environment}`),
|
|
100
100
|
this.replyHint ? 'Reply to this message to ask about it.' : '',
|
|
@@ -111,7 +111,7 @@ export class TelegramNotifier {
|
|
|
111
111
|
notifyPendingQuestion(question, options, agent, sessionId, pendingId = randomUUID()) {
|
|
112
112
|
const lines = [
|
|
113
113
|
`**Question from ${telegramEscape(agent)}${sessionId ? ` (session ${telegramEscape(sessionId)})` : ''}:**`,
|
|
114
|
-
|
|
114
|
+
telegramAgentText(question),
|
|
115
115
|
this.decideLine(pendingId, 'question'),
|
|
116
116
|
this.replyHint ? (options?.length ? 'Or reply to this message with another answer.' : 'Reply to this message to answer.') : '',
|
|
117
117
|
];
|
|
@@ -123,6 +123,6 @@ export class TelegramNotifier {
|
|
|
123
123
|
/** A plain announcement (run started/finished, a scheduled task's
|
|
124
124
|
* result), with nothing to decide. */
|
|
125
125
|
postMessage(text) {
|
|
126
|
-
this.send(
|
|
126
|
+
this.send(telegramAgentText(text), 'an announcement');
|
|
127
127
|
}
|
|
128
128
|
}
|
|
@@ -3,5 +3,10 @@ export declare function toSlackMrkdwn(markdown: string): string;
|
|
|
3
3
|
* reason): with &, < and > escaped, a value can't become a mention, a
|
|
4
4
|
* channel-wide ping (<!channel>) or a disguised link. */
|
|
5
5
|
export declare function slackEscape(text: string): string;
|
|
6
|
+
/** Marks the agent's own writing (its question, its reason, its result):
|
|
7
|
+
* its Markdown is shown formatted, but escaped like slackEscape and with
|
|
8
|
+
* a link's address showing instead of hidden behind text, since what the
|
|
9
|
+
* agent writes can carry text from anyone. */
|
|
10
|
+
export declare function slackAgentText(text: string): string;
|
|
6
11
|
/** A call's arguments as Slack mrkdwn (core/messaging/format-args.ts). */
|
|
7
12
|
export declare function slackArgs(args: Record<string, unknown>, schema?: Record<string, unknown>): string;
|
|
@@ -8,17 +8,59 @@ import { formatArgs } from '../../format-args.js';
|
|
|
8
8
|
// below never turns it into formatting or a link; they're removed after.
|
|
9
9
|
const OPEN = '\u0001';
|
|
10
10
|
const CLOSE = '\u0002';
|
|
11
|
-
|
|
11
|
+
// The agent's own writing (slackAgentText): formatted, links shown.
|
|
12
|
+
const OPEN_AGENT = '\u0003';
|
|
13
|
+
const CLOSE_AGENT = '\u0004';
|
|
14
|
+
const MARKERS = /[\u0001-\u0006]/g;
|
|
15
|
+
const KEEP = /(```[\s\S]*?```|`[^`\n]*`|\u0001[\s\S]*?\u0002|\u0003[\s\S]*?\u0004)/;
|
|
16
|
+
// Where a kept piece sits while the text around it is converted.
|
|
17
|
+
const SLOT = '\u0005';
|
|
18
|
+
const SLOT_END = '\u0006';
|
|
19
|
+
const SLOTS = /\u0005(\d+)\u0006/g;
|
|
12
20
|
export function toSlackMrkdwn(markdown) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
// The kept pieces (code, someone else's text, the agent's writing) are
|
|
22
|
+
// set aside while the rest is converted as one, so formatting around
|
|
23
|
+
// them pairs up: "**Answered by <name>:**", "~~<old value>~~".
|
|
24
|
+
const kept = [];
|
|
25
|
+
const prose = markdown
|
|
16
26
|
.split(KEEP)
|
|
17
|
-
.map((part, i) =>
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
.map((part, i) => {
|
|
28
|
+
if (i % 2 === 0)
|
|
29
|
+
return part;
|
|
30
|
+
kept.push(part);
|
|
31
|
+
return `${SLOT}${kept.length - 1}${SLOT_END}`;
|
|
32
|
+
})
|
|
33
|
+
.join('');
|
|
34
|
+
return convertProse(prose)
|
|
35
|
+
.replace(SLOTS, (_m, n) => {
|
|
36
|
+
const part = kept[Number(n)];
|
|
37
|
+
return part.startsWith(OPEN_AGENT) ? agentMrkdwn(part.slice(1, -1)) : part;
|
|
38
|
+
})
|
|
39
|
+
.replace(MARKERS, '');
|
|
20
40
|
}
|
|
21
|
-
|
|
41
|
+
// A run of Markdown table lines ("| a | b |").
|
|
42
|
+
const TABLE = /((?:^[ \t]*\|.*\|[ \t]*(?:\n|$)){2,})/m;
|
|
43
|
+
/** The agent's Markdown as mrkdwn: &, < and > escaped first (so nothing
|
|
44
|
+
* becomes a mention, a channel-wide ping or a disguised link), code and
|
|
45
|
+
* tables as code blocks, the rest formatted with links' addresses showing. */
|
|
46
|
+
function agentMrkdwn(text) {
|
|
47
|
+
return escapeSlack(text)
|
|
48
|
+
.split(/(```[\s\S]*?```|`[^`\n]*`)/)
|
|
49
|
+
.map((part, i) => {
|
|
50
|
+
if (i % 2 === 1)
|
|
51
|
+
return part;
|
|
52
|
+
return part
|
|
53
|
+
.split(TABLE)
|
|
54
|
+
.map((piece, j) => (j % 2 === 1 ? '```\n' + piece.replace(/\n$/, '') + '\n```\n' : convertProse(piece, { links: false })))
|
|
55
|
+
.join('');
|
|
56
|
+
})
|
|
57
|
+
.join('');
|
|
58
|
+
}
|
|
59
|
+
function escapeSlack(text) {
|
|
60
|
+
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
61
|
+
}
|
|
62
|
+
function convertProse(text, options = {}) {
|
|
63
|
+
const links = options.links ?? true;
|
|
22
64
|
const bold = [];
|
|
23
65
|
const keepBold = (inner) => {
|
|
24
66
|
bold.push(inner);
|
|
@@ -37,7 +79,7 @@ function convertProse(text) {
|
|
|
37
79
|
// ~~strike~~ -> ~strike~
|
|
38
80
|
.replace(/~~(?=\S)([\s\S]*?\S)~~/g, '~$1~')
|
|
39
81
|
// [text](url) -> <url|text>
|
|
40
|
-
.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)/g, '<$2|$1>');
|
|
82
|
+
.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)/g, links ? '<$2|$1>' : '$1 ($2)');
|
|
41
83
|
out = out.replace(/\u0000(\d+)\u0000/g, (_m, n) => `*${bold[Number(n)]}*`);
|
|
42
84
|
return out;
|
|
43
85
|
}
|
|
@@ -45,7 +87,14 @@ function convertProse(text) {
|
|
|
45
87
|
* reason): with &, < and > escaped, a value can't become a mention, a
|
|
46
88
|
* channel-wide ping (<!channel>) or a disguised link. */
|
|
47
89
|
export function slackEscape(text) {
|
|
48
|
-
return OPEN + text.replace(
|
|
90
|
+
return OPEN + escapeSlack(text.replace(MARKERS, '')) + CLOSE;
|
|
91
|
+
}
|
|
92
|
+
/** Marks the agent's own writing (its question, its reason, its result):
|
|
93
|
+
* its Markdown is shown formatted, but escaped like slackEscape and with
|
|
94
|
+
* a link's address showing instead of hidden behind text, since what the
|
|
95
|
+
* agent writes can carry text from anyone. */
|
|
96
|
+
export function slackAgentText(text) {
|
|
97
|
+
return OPEN_AGENT + text.replace(MARKERS, '') + CLOSE_AGENT;
|
|
49
98
|
}
|
|
50
99
|
/** A call's arguments as Slack mrkdwn (core/messaging/format-args.ts). */
|
|
51
100
|
export function slackArgs(args, schema) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createSlackConnector, parseSlackEvent, type SlackEvent } from './events.js';
|
|
2
|
-
export { slackArgs, slackEscape, toSlackMrkdwn } from './format.js';
|
|
2
|
+
export { slackAgentText, slackArgs, slackEscape, toSlackMrkdwn } from './format.js';
|
|
3
3
|
export { SLACK_ANSWER_ACTION_ID, SLACK_ANSWER_BLOCK_ID, openSlackAnswerModal, parseSlackInteraction, replyInSlack, replyPrivatelyInSlack, slackOutcomeText, verifySlackRequest, type SlackInteraction, } from './interactions.js';
|
|
4
4
|
export { listSlackChannelMembers, listSlackChannels, slackAppManifest, slackBotIdentity, type SlackBotIdentity, type SlackChannel, type SlackMember } from './setup.js';
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
// Posting requests is still SlackNotifier's job
|
|
5
5
|
// (core/http-notify-triggers/slack.ts).
|
|
6
6
|
export { createSlackConnector, parseSlackEvent } from './events.js';
|
|
7
|
-
export { slackArgs, slackEscape, toSlackMrkdwn } from './format.js';
|
|
7
|
+
export { slackAgentText, slackArgs, slackEscape, toSlackMrkdwn } from './format.js';
|
|
8
8
|
export { SLACK_ANSWER_ACTION_ID, SLACK_ANSWER_BLOCK_ID, openSlackAnswerModal, parseSlackInteraction, replyInSlack, replyPrivatelyInSlack, slackOutcomeText, verifySlackRequest, } from './interactions.js';
|
|
9
9
|
export { listSlackChannelMembers, listSlackChannels, slackAppManifest, slackBotIdentity } from './setup.js';
|
|
@@ -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
|
+
/** Marks the agent's own writing (its question, its reason, its result):
|
|
5
|
+
* its Markdown is shown formatted, as the console shows it (bold, lists,
|
|
6
|
+
* headings, code, a table as fixed-width text), but a link shows its
|
|
7
|
+
* address instead of hiding it behind text, since what the agent writes
|
|
8
|
+
* can carry text from anyone. */
|
|
9
|
+
export declare function telegramAgentText(text: string): string;
|
|
4
10
|
/** Splits Markdown into parts that each fit one Telegram message, between
|
|
5
11
|
* paragraphs where it can. A part ending inside the agent's escaped text
|
|
6
12
|
* (telegramEscape) or a code block closes it, and the next part opens it
|
|
@@ -7,7 +7,15 @@
|
|
|
7
7
|
import { formatArgs } from '../../format-args.js';
|
|
8
8
|
const OPEN = '\u0001';
|
|
9
9
|
const CLOSE = '\u0002';
|
|
10
|
-
|
|
10
|
+
// The agent's own writing (telegramAgentText): formatted, links shown.
|
|
11
|
+
const OPEN_AGENT = '\u0003';
|
|
12
|
+
const CLOSE_AGENT = '\u0004';
|
|
13
|
+
const MARKERS = /[\u0001-\u0006]/g;
|
|
14
|
+
const KEEP = /(```[\s\S]*?```|`[^`\n]*`|\u0001[\s\S]*?\u0002|\u0003[\s\S]*?\u0004)/;
|
|
15
|
+
// Where a kept piece sits while the text around it is converted.
|
|
16
|
+
const SLOT = '\u0005';
|
|
17
|
+
const SLOT_END = '\u0006';
|
|
18
|
+
const SLOTS = /\u0005(\d+)\u0006/g;
|
|
11
19
|
// Telegram's limit is 4096 characters of text after the tags are read;
|
|
12
20
|
// cutting the Markdown well short of it keeps the tags whole.
|
|
13
21
|
const MAX_MARKDOWN = 3800;
|
|
@@ -17,7 +25,15 @@ function html(text) {
|
|
|
17
25
|
/** Marks text that isn't ours, so toTelegramHtml escapes it and leaves it
|
|
18
26
|
* as plain text. */
|
|
19
27
|
export function telegramEscape(text) {
|
|
20
|
-
return OPEN + text.replace(
|
|
28
|
+
return OPEN + text.replace(MARKERS, '') + CLOSE;
|
|
29
|
+
}
|
|
30
|
+
/** Marks the agent's own writing (its question, its reason, its result):
|
|
31
|
+
* its Markdown is shown formatted, as the console shows it (bold, lists,
|
|
32
|
+
* headings, code, a table as fixed-width text), but a link shows its
|
|
33
|
+
* address instead of hiding it behind text, since what the agent writes
|
|
34
|
+
* can carry text from anyone. */
|
|
35
|
+
export function telegramAgentText(text) {
|
|
36
|
+
return OPEN_AGENT + text.replace(MARKERS, '') + CLOSE_AGENT;
|
|
21
37
|
}
|
|
22
38
|
// A long message goes out as several, at most this many; past that the
|
|
23
39
|
// middle is left out (the last part, with its link and buttons, stays).
|
|
@@ -46,12 +62,17 @@ export function splitTelegramMarkdown(markdown, max = MAX_MARKDOWN - 100) {
|
|
|
46
62
|
let head = rest.slice(0, at).trimEnd();
|
|
47
63
|
rest = rest.slice(at).replace(/^[ \n]+/, '');
|
|
48
64
|
let escaped = false;
|
|
65
|
+
let agent = false;
|
|
49
66
|
let fenced = false;
|
|
50
67
|
for (let i = 0; i < head.length; i++) {
|
|
51
68
|
if (head[i] === OPEN)
|
|
52
69
|
escaped = true;
|
|
53
70
|
else if (head[i] === CLOSE)
|
|
54
71
|
escaped = false;
|
|
72
|
+
else if (head[i] === OPEN_AGENT)
|
|
73
|
+
agent = true;
|
|
74
|
+
else if (head[i] === CLOSE_AGENT)
|
|
75
|
+
agent = false;
|
|
55
76
|
else if (!escaped && head.startsWith('```', i)) {
|
|
56
77
|
fenced = !fenced;
|
|
57
78
|
i += 2;
|
|
@@ -61,10 +82,15 @@ export function splitTelegramMarkdown(markdown, max = MAX_MARKDOWN - 100) {
|
|
|
61
82
|
head += CLOSE;
|
|
62
83
|
rest = OPEN + rest;
|
|
63
84
|
}
|
|
64
|
-
|
|
85
|
+
// A code block open inside the agent's text closes before its marker.
|
|
86
|
+
if (fenced)
|
|
65
87
|
head += '\n```';
|
|
88
|
+
if (agent)
|
|
89
|
+
head += CLOSE_AGENT;
|
|
90
|
+
if (fenced)
|
|
66
91
|
rest = '```\n' + rest;
|
|
67
|
-
|
|
92
|
+
if (agent)
|
|
93
|
+
rest = OPEN_AGENT + rest;
|
|
68
94
|
parts.push(head);
|
|
69
95
|
}
|
|
70
96
|
parts.push(rest);
|
|
@@ -76,22 +102,52 @@ export function splitTelegramMarkdown(markdown, max = MAX_MARKDOWN - 100) {
|
|
|
76
102
|
}
|
|
77
103
|
export function toTelegramHtml(markdown) {
|
|
78
104
|
const cut = markdown.length > MAX_MARKDOWN ? markdown.slice(0, MAX_MARKDOWN).trimEnd() + '\n…' : markdown;
|
|
79
|
-
|
|
105
|
+
// The kept pieces (code, someone else's text, the agent's writing) are
|
|
106
|
+
// set aside while the rest is converted as one, so formatting around
|
|
107
|
+
// them pairs up: "**Question from <agent>:**" is bold.
|
|
108
|
+
const kept = [];
|
|
109
|
+
const prose = cut
|
|
80
110
|
.split(KEEP)
|
|
81
111
|
.map((part, i) => {
|
|
82
112
|
if (i % 2 === 0)
|
|
83
|
-
return
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
113
|
+
return part;
|
|
114
|
+
kept.push(part);
|
|
115
|
+
return `${SLOT}${kept.length - 1}${SLOT_END}`;
|
|
116
|
+
})
|
|
117
|
+
.join('');
|
|
118
|
+
return convertProse(prose)
|
|
119
|
+
.replace(SLOTS, (_m, n) => keptHtml(kept[Number(n)]))
|
|
120
|
+
.replace(MARKERS, '');
|
|
121
|
+
}
|
|
122
|
+
function keptHtml(part) {
|
|
123
|
+
if (part.startsWith(OPEN_AGENT))
|
|
124
|
+
return agentHtml(part.slice(1, -1));
|
|
125
|
+
const plain = part.replace(MARKERS, '');
|
|
126
|
+
if (part.startsWith(OPEN))
|
|
127
|
+
return html(plain);
|
|
128
|
+
if (part.startsWith('```'))
|
|
129
|
+
return `<pre>${html(plain.slice(3, -3).replace(/^[\w-]*\n/, ''))}</pre>`;
|
|
130
|
+
return `<code>${html(plain.slice(1, -1))}</code>`;
|
|
131
|
+
}
|
|
132
|
+
// A run of Markdown table lines ("| a | b |").
|
|
133
|
+
const TABLE = /((?:^[ \t]*\|.*\|[ \t]*(?:\n|$)){2,})/m;
|
|
134
|
+
/** The agent's Markdown: code and tables as fixed-width text, the rest
|
|
135
|
+
* formatted, links with their address showing. */
|
|
136
|
+
function agentHtml(text) {
|
|
137
|
+
return text
|
|
138
|
+
.split(/(```[\s\S]*?```|`[^`\n]*`)/)
|
|
139
|
+
.map((part, i) => {
|
|
140
|
+
if (i % 2 === 1)
|
|
141
|
+
return part.startsWith('```') ? `<pre>${html(part.slice(3, -3).replace(/^[\w-]*\n/, ''))}</pre>` : `<code>${html(part.slice(1, -1))}</code>`;
|
|
142
|
+
return part
|
|
143
|
+
.split(TABLE)
|
|
144
|
+
.map((piece, j) => (j % 2 === 1 ? `<pre>${html(piece.replace(/\n$/, ''))}</pre>\n` : convertProse(piece, { links: false })))
|
|
145
|
+
.join('');
|
|
90
146
|
})
|
|
91
|
-
.join('')
|
|
92
|
-
.replace(/[\u0001\u0002]/g, '');
|
|
147
|
+
.join('');
|
|
93
148
|
}
|
|
94
|
-
function convertProse(text) {
|
|
149
|
+
function convertProse(text, options = {}) {
|
|
150
|
+
const links = options.links ?? true;
|
|
95
151
|
return html(text)
|
|
96
152
|
// # Heading -> bold
|
|
97
153
|
.replace(/^#{1,6}[ \t]+(.+?)[ \t]*#*[ \t]*$/gm, '<b>$1</b>')
|
|
@@ -104,7 +160,7 @@ function convertProse(text) {
|
|
|
104
160
|
.replace(/~~(?=\S)([\s\S]*?\S)~~/g, '<s>$1</s>')
|
|
105
161
|
// [text](url): the URL was escaped with the rest, which is what an
|
|
106
162
|
// attribute needs, and can't hold a quote.
|
|
107
|
-
.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)"]+)\)/g, '<a href="$2">$1</a>');
|
|
163
|
+
.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)"]+)\)/g, links ? '<a href="$2">$1</a>' : '$1 ($2)');
|
|
108
164
|
}
|
|
109
165
|
/** A call's arguments as Telegram HTML (core/messaging/format-args.ts). */
|
|
110
166
|
export function telegramArgs(args, schema) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { MessagingConnector } from '../../connector.js';
|
|
2
|
-
export { splitTelegramMarkdown, telegramArgs, telegramEscape, toTelegramHtml } from './format.js';
|
|
2
|
+
export { splitTelegramMarkdown, telegramAgentText, 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
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 { splitTelegramMarkdown, telegramArgs, telegramEscape, toTelegramHtml } from './format.js';
|
|
4
|
+
export { splitTelegramMarkdown, telegramAgentText, 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. */
|
|
@@ -72,7 +72,7 @@ export function createTelegramConnector(botToken, agentDir) {
|
|
|
72
72
|
escape: telegramEscape,
|
|
73
73
|
async fetchThread(conversation, threadTs) {
|
|
74
74
|
// Without telegramEscape's markers, which are for formatting only.
|
|
75
|
-
return readTelegramThread(agentDir, conversation, threadTs).map((m) => ({ ...m, text: m.text.replace(/[\u0001
|
|
75
|
+
return readTelegramThread(agentDir, conversation, threadTs).map((m) => ({ ...m, text: m.text.replace(/[\u0001-\u0004]/g, '') }));
|
|
76
76
|
},
|
|
77
77
|
async postInThread(conversation, threadTs, text) {
|
|
78
78
|
await send(conversation, threadTs, text);
|
package/dist/index.d.ts
CHANGED
|
@@ -105,7 +105,8 @@ export { readActauthConfig, addActauthRule, updateActauthRule, removeActauthRule
|
|
|
105
105
|
export type { ActauthRuleInput, ActauthConfigView } from './web/actauth-admin.js';
|
|
106
106
|
export { installAbility, upgradeAbility, removeAbility, listInstalledAbilities, backfillDependencies, configureAgentMcpTools, AbilityManifestError, AbilityVersionError, AbilityCollisionError, AbilityAlreadyInstalledError, AbilityNotInstalledError, } from './bin/ability-manager.js';
|
|
107
107
|
export type { InstalledAbilityRecord, AbilityEnvDecl, UpgradeFileResult, McpToolChoice } from './bin/ability-manager.js';
|
|
108
|
-
export { APP_CATALOG, AppToolsError, appCatalog, appServer, appTools, catalogServer, connectedApps, isAppAbility, loadAppIcons, refreshAppTools, setAppDefault, type AgentAppTools, type AppCatalogEntry, type AppTool, type AppView, type ConnectedApp, } from './core/mcp-apps.js';
|
|
108
|
+
export { APP_CATALOG, AppToolsError, appCatalog, appServer, appToolInputs, appTools, catalogServer, connectedApps, isAppAbility, loadAppIcons, refreshAppTools, setAppDefault, type AgentAppTools, type AppCatalogEntry, type AppTool, type AppView, type ConnectedApp, } from './core/mcp-apps.js';
|
|
109
|
+
export { UNATTENDED_NOTE } from './core/unattended.js';
|
|
109
110
|
export { searchPublicAbilities, fetchLatestAbilityVersion } from './web/abilities-admin.js';
|
|
110
111
|
export type { PublicAbilitySearchResult } from './web/abilities-admin.js';
|
|
111
112
|
export { AbilitySourceError, abilityOrigin, readAbilitySources, saveAbilitySource, removeAbilitySource, listSourceAbilities, sourceTokenVar, type AbilitySource, type SourceAbility, type AbilityOrigin } from './core/ability-sources.js';
|
package/dist/index.js
CHANGED
|
@@ -94,7 +94,8 @@ export { readSkill, writeSkill, deleteSkill, SkillInvalidIdError, SkillNotFoundE
|
|
|
94
94
|
export { listDeclaredEnvVars, setEnvVar, unsetEnvVar, setAgentEnvVar, unsetAgentEnvVar, agentScopedEnvVarName, EnvVarNameError } from './web/env-admin.js';
|
|
95
95
|
export { readActauthConfig, addActauthRule, updateActauthRule, removeActauthRule, setDefaultDecision, ActauthRuleExistsError, ActauthRuleNotFoundError, } from './web/actauth-admin.js';
|
|
96
96
|
export { installAbility, upgradeAbility, removeAbility, listInstalledAbilities, backfillDependencies, configureAgentMcpTools, AbilityManifestError, AbilityVersionError, AbilityCollisionError, AbilityAlreadyInstalledError, AbilityNotInstalledError, } from './bin/ability-manager.js';
|
|
97
|
-
export { APP_CATALOG, AppToolsError, appCatalog, appServer, appTools, catalogServer, connectedApps, isAppAbility, loadAppIcons, refreshAppTools, setAppDefault, } from './core/mcp-apps.js';
|
|
97
|
+
export { APP_CATALOG, AppToolsError, appCatalog, appServer, appToolInputs, appTools, catalogServer, connectedApps, isAppAbility, loadAppIcons, refreshAppTools, setAppDefault, } from './core/mcp-apps.js';
|
|
98
|
+
export { UNATTENDED_NOTE } from './core/unattended.js';
|
|
98
99
|
export { searchPublicAbilities, fetchLatestAbilityVersion } from './web/abilities-admin.js';
|
|
99
100
|
// Private ability sources, the workspace's own abilities and each
|
|
100
101
|
// ability's details: the console's Abilities page and agent Abilities tab.
|