omni-notify-mcp 1.3.466 → 1.3.467
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/package.json +1 -1
- package/dist/agentGateway.js +0 -606
- package/dist/chunk.js +0 -41
- package/dist/daemon.js +0 -131
- package/dist/desk.js +0 -268
- package/dist/gateway/chatPage.js +0 -949
- package/dist/gateway/conversationOwner.js +0 -141
- package/dist/gateway/extensionModule.js +0 -20
- package/dist/gateway/gatewayInstall.js +0 -83
- package/dist/gateway/iconData.js +0 -8
- package/dist/gateway/iconDoor.js +0 -23
- package/dist/gateway/lanProbe.js +0 -86
- package/dist/gateway/mcpDoor.js +0 -411
- package/dist/gateway/mcpEvents.js +0 -448
- package/dist/gateway/mcpHosts.js +0 -19
- package/dist/gateway/notifyDoor.js +0 -166
- package/dist/gateway/oauthDoor.js +0 -345
- package/dist/gateway/panel.js +0 -314
- package/dist/gateway/panelPage.js +0 -414
- package/dist/gateway/passkeyClient.js +0 -47
- package/dist/gateway/passkeyDoor.js +0 -974
- package/dist/gateway/publicAccess.js +0 -374
- package/dist/gateway/restDoor.js +0 -329
- package/dist/gateway/terminalDoor.js +0 -243
- package/dist/gateway/terminalPage.js +0 -613
- package/dist/gateway/tunnel.js +0 -166
- package/dist/messaging/chatInbound.js +0 -418
- package/dist/messaging/decisionDigit.js +0 -84
- package/dist/messaging/deliveryVerdict.js +0 -52
- package/dist/messaging/emailInbound.js +0 -306
- package/dist/messaging/inboundPayloads.js +0 -41
- package/dist/messaging/messenger.js +0 -525
- package/dist/messaging/notificationEngine.js +0 -202
- package/dist/messaging/setup.js +0 -55
- package/dist/messaging/slackSetup.js +0 -159
- package/dist/messaging/telegramError.js +0 -6
- package/dist/messaging/telegramFormat.js +0 -95
- package/dist/notify.js +0 -3275
- package/dist/shared/agentSession.js +0 -7322
- package/dist/shared/conversation/claudeProvider.js +0 -444
- package/dist/shared/conversation/cliSession.js +0 -462
- package/dist/shared/conversation/codexArchive.js +0 -67
- package/dist/shared/conversation/codexProvider.js +0 -240
- package/dist/shared/conversation/conversationOwner.js +0 -44
- package/dist/shared/conversation/conversations.js +0 -1009
- package/dist/shared/conversation/copilotProvider.js +0 -229
- package/dist/shared/conversation/nativeCliInvocation.js +0 -70
- package/dist/shared/conversation/nativeTerminal.js +0 -529
- package/dist/shared/conversation/nativeTerminalWorker.js +0 -158
- package/dist/shared/conversation/opencodeEvents/server.mjs +0 -31
- package/dist/shared/conversation/opencodeProvider.js +0 -338
- package/dist/shared/conversation/ownerContext.js +0 -1
- package/dist/shared/conversation/provider.js +0 -38
- package/dist/shared/conversation/providers.js +0 -30
- package/dist/shared/conversation/sessionDriver.js +0 -1067
- package/dist/shared/conversation/terminalTransport.js +0 -126
- package/dist/shared/providerOwnership.js +0 -294
- package/dist/shared/webhost/autoSwitch.cjs +0 -2504
- package/dist/shared/webhost/bullseyeWebHost.js +0 -1446
- package/dist/shared/webhost/homeRelative.js +0 -19
- package/dist/shared/webhost/originHandover.js +0 -653
- package/dist/shared/webhost/userSession.js +0 -178
- package/dist/shared/webhost/webviewShim.js +0 -1039
- package/dist/shared/webhost/workspaceList.js +0 -691
- package/dist/shared/workspaceList.js +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "omni-notify-mcp",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.467",
|
|
4
4
|
"homepage": "https://www.npmjs.com/package/omni-notify-mcp",
|
|
5
5
|
"description": "Reach your AI agents from anywhere — and let them reach you. One resident process on 127.0.0.1:7475 serves the whole tool surface to Claude Code, GitHub Copilot Chat and Codex: desktop, Telegram, Slack, ntfy, Discord, Teams and SMS are configured in one file, and agents notify you when long work finishes or a decision is needed, ask you a question and wait for your answer, and receive and reply to the messages you send them — with Do Not Disturb and idle detection deciding what actually gets through.",
|
|
6
6
|
"main": "dist/daemon.js",
|
package/dist/agentGateway.js
DELETED
|
@@ -1,606 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The agent gateway's chat side (#2052): a Telegram, Slack, Discord or Teams message that starts with `!` goes to
|
|
3
|
-
* the agent; everything else is left to the inbox exactly as before.
|
|
4
|
-
*
|
|
5
|
-
* One conversation per chat or Slack thread, kept in the conversations store (`shared/conversation/conversations.ts`,
|
|
6
|
-
* owned by `gateway/conversationOwner.ts`) beside
|
|
7
|
-
* the REST door's, and driven by the one session driver (`shared/conversation/sessionDriver.ts`, #2051): the start line, the
|
|
8
|
-
* status lines, a permission question and the answer come back to the chat, the answer split at 500 characters
|
|
9
|
-
* with (k/N) parts. `/stop` ends the chat's conversation and `/new` ends it so the next `!` opens a fresh one.
|
|
10
|
-
* An email thread (#2105) is one conversation for good, and gets only its answer, question or failure, each whole.
|
|
11
|
-
* Every conversation starts in Meni's workspace root, as the web page's and the MCP door's do.
|
|
12
|
-
*/
|
|
13
|
-
import { closeSync, existsSync, openSync, readdirSync, readSync, statSync } from "fs";
|
|
14
|
-
import { homedir } from "os";
|
|
15
|
-
import { join } from "path";
|
|
16
|
-
import { splitForNotify } from "./chunk.js";
|
|
17
|
-
import { choicesOf, StreamedReply } from "./messaging/messenger.js";
|
|
18
|
-
import { appendEvent, archiveConversation, conversationRefusal, openConversation, readConversation, createdIn, creationPending, creationsPending, editOwed, GatewayError, listConversations, monotonicMs, openConversationOf, openConversationsOf, queueNoticeOf, readEvents, resolveProject, rootFolder, routeAsk, stopTargetOf, sendToConversation, startConversation, turnInFlight, updateConversation, } from "./gateway/conversationOwner.js";
|
|
19
|
-
import { conversationOwner } from "./gateway/conversationOwner.js";
|
|
20
|
-
import { clearOpenCodeLogs } from "./gateway/conversationOwner.js";
|
|
21
|
-
import { resendUndelivered, turnEnded } from "./gateway/mcpEvents.js";
|
|
22
|
-
export const AGENT_PREFIX = "!";
|
|
23
|
-
/** The thread's newest conversation is ended; the next message opens a fresh one. */
|
|
24
|
-
async function endNewest(key, _platform, end) {
|
|
25
|
-
const open = this.conversationOf(key);
|
|
26
|
-
// NULL-OK: field open is OPTIONAL and therefore it is ok to check for NULL
|
|
27
|
-
if (open !== undefined)
|
|
28
|
-
end(open.id);
|
|
29
|
-
const stay = this.allOf(key).length;
|
|
30
|
-
return stay === 0 ? "🆕 New conversation. Your next ! message starts it." : `🆕 Newest conversation ended. ${stay} other conversation${stay === 1 ? " stays" : "s stay"} open.`;
|
|
31
|
-
}
|
|
32
|
-
const stoppedOfMany = ([stopped], others) => others === 0 ? "⏹ Conversation ended. Your next ! message opens a new one." : `⏹ Conversation ${stopped.id.slice(0, 8)} ended. ${others} other conversation${others === 1 ? " stays" : "s stay"} open.`;
|
|
33
|
-
/** Each carried event of a turn sent as its parts. */
|
|
34
|
-
function partsRelay(reply, carriage) {
|
|
35
|
-
return {
|
|
36
|
-
async event(event) {
|
|
37
|
-
if (carriage.carries(event.kind))
|
|
38
|
-
for (const part of carriage.parts(event))
|
|
39
|
-
await reply(part);
|
|
40
|
-
},
|
|
41
|
-
close: () => { },
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
/** A chat: the newest conversation not stopped (`/new` and `/stop` end it), and every line of the turn, the answer split at 500. */
|
|
45
|
-
const CHAT = {
|
|
46
|
-
conversationOf: openConversationOf,
|
|
47
|
-
routeOf: routeAsk,
|
|
48
|
-
stopOf: (key) => [stopTargetOf(key)].filter((c) => c !== undefined),
|
|
49
|
-
allOf: openConversationsOf,
|
|
50
|
-
carries: (kind) => kind !== "returned",
|
|
51
|
-
parts: (event) => (event.kind === "answer" ? splitForNotify(event.text) : [event.text]),
|
|
52
|
-
stopped: stoppedOfMany,
|
|
53
|
-
renew: endNewest,
|
|
54
|
-
folderOf: (text) => resolveProject(undefined, text),
|
|
55
|
-
forkOf: () => undefined,
|
|
56
|
-
};
|
|
57
|
-
/** Meni's one private chat with the bot (#4453): its main conversation, the newest not deleted and not forked beside another — an idle archive keeps it bound, only /new replaces it. */
|
|
58
|
-
const boundOf = (key) => listConversations().find((c) => c.chat === key && c.shelf !== "deleted" && c.forkOf === undefined);
|
|
59
|
-
/** The conversations forked beside the bound chat's main one while it was busy (#4584), newest first; a stopped or put-away one is done. */
|
|
60
|
-
const sidesOf = (key) => listConversations().filter((c) => c.chat === key && c.forkOf !== undefined && c.state !== "closed" && c.shelf === undefined);
|
|
61
|
-
/** A conversation in the middle of a turn, or holding a message stored for its next one. */
|
|
62
|
-
const busy = (c) => turnInFlight(readEvents(c.id), c) !== undefined;
|
|
63
|
-
/** The bound chat's main conversation while it is free (#4584); while it is busy undefined, so the message runs at once in a new conversation forked from it and never waits behind the reply under way. */
|
|
64
|
-
const freeBound = (key) => {
|
|
65
|
-
const main = boundOf(key);
|
|
66
|
-
// NULL-OK: field main is OPTIONAL and therefore it is ok to check for NULL
|
|
67
|
-
return main === undefined || !busy(main) ? main : undefined;
|
|
68
|
-
};
|
|
69
|
-
/** The bound chat: ONE main conversation at the workspace root, every message goes on with it, or beside it in a fork while it is busy (#4584); a stop pauses the one at work, /new opens the next one at once. */
|
|
70
|
-
const ONE = {
|
|
71
|
-
conversationOf: boundOf,
|
|
72
|
-
routeOf: freeBound,
|
|
73
|
-
stopOf: (key) => {
|
|
74
|
-
const working = ONE.allOf(key).filter(busy);
|
|
75
|
-
return working.length > 0 ? working : [boundOf(key)].filter((c) => c !== undefined);
|
|
76
|
-
},
|
|
77
|
-
allOf: (key) => [boundOf(key), ...sidesOf(key)].filter((c) => c !== undefined),
|
|
78
|
-
carries: CHAT.carries,
|
|
79
|
-
parts: CHAT.parts,
|
|
80
|
-
stopped: (stopped) => {
|
|
81
|
-
const forks = stopped.filter((c) => c.forkOf !== undefined).length;
|
|
82
|
-
const parallel = `${forks} parallel conversation${forks === 1 ? "" : "s"}`;
|
|
83
|
-
if (forks === stopped.length)
|
|
84
|
-
return `⏹ Stopped ${parallel}.`;
|
|
85
|
-
return `⏹ Stopped${forks === 0 ? "" : `, with ${parallel}`}. Your next message picks the conversation up where it stopped.`;
|
|
86
|
-
},
|
|
87
|
-
folderOf: () => rootFolder(),
|
|
88
|
-
forkOf: (key) => {
|
|
89
|
-
const main = boundOf(key);
|
|
90
|
-
// NULL-OK: field main is OPTIONAL and therefore it is ok to check for NULL
|
|
91
|
-
return main !== undefined && busy(main) ? main.sessionId : undefined;
|
|
92
|
-
},
|
|
93
|
-
async renew(key, platform, end) {
|
|
94
|
-
for (const c of ONE.allOf(key)) {
|
|
95
|
-
end(c.id);
|
|
96
|
-
archiveConversation(c.id);
|
|
97
|
-
}
|
|
98
|
-
const { notice } = await createdIn(rootFolder(), undefined, () => key, (on) => openConversation({ platform, chat: key, ...on }));
|
|
99
|
-
// NULL-OK: field notice is OPTIONAL and therefore it is ok to check for NULL
|
|
100
|
-
return `🆕 Fresh conversation — your next message starts it.${notice === undefined ? "" : `\n${notice.text}`}`;
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
const MAILED_KINDS = new Set(["answer", "failed", "stopped"]);
|
|
104
|
-
/** An email thread: its one conversation, a stopped one resumed by the next reply; only what needs Meni is mailed, each whole. */
|
|
105
|
-
const THREADS_ONE = (key) => listConversations().find((c) => c.chat === key);
|
|
106
|
-
const MAIL = {
|
|
107
|
-
conversationOf: THREADS_ONE,
|
|
108
|
-
routeOf: THREADS_ONE,
|
|
109
|
-
stopOf: (key) => [THREADS_ONE(key)].filter((c) => c !== undefined),
|
|
110
|
-
allOf: (key) => listConversations().filter((c) => c.chat === key),
|
|
111
|
-
carries: (kind) => MAILED_KINDS.has(kind),
|
|
112
|
-
parts: (event) => [event.text],
|
|
113
|
-
stopped: stoppedOfMany,
|
|
114
|
-
renew: endNewest,
|
|
115
|
-
folderOf: (text) => resolveProject(undefined, text),
|
|
116
|
-
forkOf: () => undefined,
|
|
117
|
-
};
|
|
118
|
-
const CARRIAGE = { telegram: ONE, slack: CHAT, discord: CHAT, teams: CHAT, ntfy: CHAT, sms: CHAT, email: MAIL };
|
|
119
|
-
/** The key of Meni's direct-message chat with the bot on Slack (#4453): one conversation, as his Telegram chat is; a Slack channel thread keeps its own. */
|
|
120
|
-
export const SLACK_DM = "slack:dm:";
|
|
121
|
-
/** How a chat key's conversations are carried. */
|
|
122
|
-
function carriageOf(platform, key) {
|
|
123
|
-
return key.startsWith(SLACK_DM) ? ONE : CARRIAGE[platform];
|
|
124
|
-
}
|
|
125
|
-
const COMMAND_RE = /^\/?(new|stop)(@\w+)?$/i;
|
|
126
|
-
const FINAL_KINDS = new Set(["answer", "failed", "stopped", "archived", "deleted"]);
|
|
127
|
-
export class AgentGateway {
|
|
128
|
-
driver;
|
|
129
|
-
static self;
|
|
130
|
-
static instance() {
|
|
131
|
-
return (AgentGateway.self ??= new AgentGateway(conversationOwner.driver));
|
|
132
|
-
}
|
|
133
|
-
/** The messages waiting on their answer, by conversation and user event. */
|
|
134
|
-
turns = new Map();
|
|
135
|
-
doors = new Map();
|
|
136
|
-
recovered = false;
|
|
137
|
-
constructor(driver) {
|
|
138
|
-
this.driver = driver;
|
|
139
|
-
driver.onEvent((event) => this.deliver(event));
|
|
140
|
-
driver.onEvent(turnEnded);
|
|
141
|
-
}
|
|
142
|
-
registerDoor(platform, door) {
|
|
143
|
-
this.doors.set(platform, door);
|
|
144
|
-
}
|
|
145
|
-
/** Starts the driver in the process that owns the pollers, then takes back every turn the last process owed its thread an answer for. */
|
|
146
|
-
boot() {
|
|
147
|
-
clearOpenCodeLogs();
|
|
148
|
-
this.driver.boot();
|
|
149
|
-
if (this.recovered)
|
|
150
|
-
return;
|
|
151
|
-
this.recovered = true;
|
|
152
|
-
resendUndelivered();
|
|
153
|
-
for (const c of listConversations())
|
|
154
|
-
this.recover(c);
|
|
155
|
-
}
|
|
156
|
-
/** Re-attaches each turn the record says is still owed: the events written since the last one sent go out now, and the answer still to come follows once. */
|
|
157
|
-
recover(c) {
|
|
158
|
-
const owed = Object.entries(c.owed ?? {});
|
|
159
|
-
if (owed.length === 0)
|
|
160
|
-
return;
|
|
161
|
-
if (c.state === "closed")
|
|
162
|
-
return editOwed(c.id, (o) => { for (const [turn] of owed)
|
|
163
|
-
delete o[turn]; });
|
|
164
|
-
const door = this.doors.get(c.platform);
|
|
165
|
-
if (door === undefined)
|
|
166
|
-
throw new Error(`conversation ${c.id} on ${c.platform} is owed an answer and no door of that platform is registered before boot`);
|
|
167
|
-
for (const [turn, sent] of owed) {
|
|
168
|
-
const pending = readEvents(c.id).filter((e) => e.turn === Number(turn) && e.n > sent && e.role !== "user").map((e) => ({ ...e, id: c.id }));
|
|
169
|
-
// OWNER: ITER/LOOP — a thread that cannot be reached is shown on its conversation and the next owed turn is still carried on.
|
|
170
|
-
void this.awaitTurn(c.id, Number(turn), partsRelay((part) => door(c, part), carriageOf(c.platform, c.chat ?? "")), pending).catch((err) => appendEvent(c.id, { role: "system", kind: "status", text: `❌ The answer could not be sent to this ${c.platform} thread after the BN restart: ${err instanceof Error ? err.message : String(err)}` }));
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
/** No turn running or queued, no chat message waiting on its conversation's creation, and no chat waiting on an answer: the gateway can pass to another process without losing one. */
|
|
174
|
-
quiet() {
|
|
175
|
-
return this.driver.quiet() && !creationsPending() && [...this.turns.values()].every((turns) => turns.size === 0);
|
|
176
|
-
}
|
|
177
|
-
/** Gives the store back when this process hands the gateway over. */
|
|
178
|
-
async shutdown() {
|
|
179
|
-
await this.driver.shutdown();
|
|
180
|
-
this.recovered = false;
|
|
181
|
-
}
|
|
182
|
-
/** What this message asks of the agent, or undefined when it is not addressed to it and belongs to the inbox. */
|
|
183
|
-
classify(key, raw) {
|
|
184
|
-
const text = raw.trim();
|
|
185
|
-
if (text.startsWith(AGENT_PREFIX)) {
|
|
186
|
-
const body = text.slice(AGENT_PREFIX.length).trim();
|
|
187
|
-
const cmd = body.match(COMMAND_RE);
|
|
188
|
-
if (cmd)
|
|
189
|
-
return { kind: cmd[1].toLowerCase() === "new" ? "new" : "stop" };
|
|
190
|
-
return { kind: "say", text: body };
|
|
191
|
-
}
|
|
192
|
-
const cmd = text.match(COMMAND_RE);
|
|
193
|
-
if (cmd && openConversationOf(key) !== undefined)
|
|
194
|
-
return { kind: cmd[1].toLowerCase() === "new" ? "new" : "stop" };
|
|
195
|
-
return undefined;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* A message of the bound chat is all for its one conversation (#4453): `/new` and `/stop` act, anything else is said as it is — a leading `!`
|
|
199
|
-
* of the old door is taken off. A bare "new" or "stop" is a command only where `bare` says the chat cannot type a slash (Slack's own client
|
|
200
|
-
* takes "/new" as its slash command); in Telegram it is his answer — "new" to "a new or the existing branch?" — never the end of the conversation.
|
|
201
|
-
*/
|
|
202
|
-
classifyChat(raw, bare) {
|
|
203
|
-
const said = raw.trim();
|
|
204
|
-
const text = said.replace(/^!\s*/, "");
|
|
205
|
-
const cmd = text.match(COMMAND_RE);
|
|
206
|
-
const commanded = cmd !== null && (bare || text.startsWith("/") || said.startsWith(AGENT_PREFIX));
|
|
207
|
-
return commanded ? { kind: cmd[1].toLowerCase() === "new" ? "new" : "stop" } : { kind: "say", text };
|
|
208
|
-
}
|
|
209
|
-
/** A message inside a thread the agent holds is all for the agent: `/new` and `/stop` act, anything else is said, with or without `!`. */
|
|
210
|
-
classifyInThread(key, raw) {
|
|
211
|
-
return this.classify(key, raw) ?? { kind: "say", text: raw.trim() };
|
|
212
|
-
}
|
|
213
|
-
/** Whether this email message was already taken as a turn of some conversation, so a BN restart never answers it twice. */
|
|
214
|
-
emailTaken(messageId) {
|
|
215
|
-
// NULL-OK: field email is OPTIONAL and therefore it is ok to check for NULL
|
|
216
|
-
return listConversations().some((c) => c.email !== undefined && c.email.messageIds.includes(messageId));
|
|
217
|
-
}
|
|
218
|
-
/** Shows on this chat key's conversation that its answer never reached the chat, so the web page tells what the chat could not. */
|
|
219
|
-
noteUndelivered(key, platform, text) {
|
|
220
|
-
const c = carriageOf(platform, key).conversationOf(key);
|
|
221
|
-
// NULL-OK: field c is OPTIONAL and therefore it is ok to check for NULL
|
|
222
|
-
if (c === undefined)
|
|
223
|
-
return;
|
|
224
|
-
appendEvent(c.id, { role: "system", kind: "status", text });
|
|
225
|
-
}
|
|
226
|
-
openSlackThreads() {
|
|
227
|
-
const newestOfThread = new Set();
|
|
228
|
-
return listConversations().filter((c) => {
|
|
229
|
-
if (c.platform !== "slack" || c.state === "closed" || c.slack === undefined || newestOfThread.has(c.chat ?? c.id))
|
|
230
|
-
return false;
|
|
231
|
-
newestOfThread.add(c.chat ?? c.id);
|
|
232
|
-
return true;
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
/** Records how far a Slack thread has been read. */
|
|
236
|
-
markSlackRead(key, readTs) {
|
|
237
|
-
for (const c of openConversationsOf(key))
|
|
238
|
-
updateConversation(c.id, { slack: { ...c.slack, readTs } });
|
|
239
|
-
}
|
|
240
|
-
/** Acts on one addressed message. Resolves when its answer (or its failure) has been sent back. */
|
|
241
|
-
async handle(key, platform, act, out, origin = {}) {
|
|
242
|
-
const arrived = monotonicMs();
|
|
243
|
-
const carriage = carriageOf(platform, key);
|
|
244
|
-
const reply = typeof out === "function" ? out : out.say;
|
|
245
|
-
const relayOf = (id, n, queued) => (typeof out === "function" ? partsRelay(out, carriage) : out.turn(id, n, queued));
|
|
246
|
-
const refusal = conversationRefusal();
|
|
247
|
-
if (refusal !== undefined)
|
|
248
|
-
return reply(refusal);
|
|
249
|
-
// NULL-OK: field pending is OPTIONAL and therefore it is ok to check for NULL
|
|
250
|
-
for (let pending = creationPending(key); pending !== undefined; pending = creationPending(key))
|
|
251
|
-
await pending;
|
|
252
|
-
const open = carriage.conversationOf(key);
|
|
253
|
-
if (act.kind === "stop") {
|
|
254
|
-
const pressed = carriage.allOf(key).filter((c) => c.id === act.conversation);
|
|
255
|
-
const stopped = pressed.length > 0 ? pressed : carriage.stopOf(key);
|
|
256
|
-
if (stopped.length === 0)
|
|
257
|
-
return reply("Nothing to stop here — no conversation is open.");
|
|
258
|
-
for (const c of stopped)
|
|
259
|
-
this.end(c.id);
|
|
260
|
-
return reply(carriage.stopped(stopped, carriage.allOf(key).filter((c) => !stopped.includes(c)).length));
|
|
261
|
-
}
|
|
262
|
-
if (act.kind === "new")
|
|
263
|
-
return reply(await carriage.renew(key, platform, (id) => this.end(id)));
|
|
264
|
-
if (act.text === "")
|
|
265
|
-
return reply("Send `! <your message>` to talk to the agent.");
|
|
266
|
-
const target = carriage.routeOf(key, act.text);
|
|
267
|
-
const forkOf = carriage.forkOf(key);
|
|
268
|
-
// NULL-OK: field target is OPTIONAL and therefore it is ok to check for NULL
|
|
269
|
-
if (target === undefined) {
|
|
270
|
-
let project;
|
|
271
|
-
try {
|
|
272
|
-
project = carriage.folderOf(act.text);
|
|
273
|
-
}
|
|
274
|
-
catch (err) {
|
|
275
|
-
// OWNER: the message is the trigger root — a refused project (no home board) is answered to the chat as its reason, never thrown past the poll loop
|
|
276
|
-
if (err instanceof GatewayError)
|
|
277
|
-
return reply(err.message);
|
|
278
|
-
throw err;
|
|
279
|
-
}
|
|
280
|
-
// A conversation opened in a thread the chat already reads starts reading after what was read, never from the thread's root again.
|
|
281
|
-
// NULL-OK: field open is OPTIONAL and therefore it is ok to check for NULL
|
|
282
|
-
const slack = origin.slack !== undefined && open?.slack !== undefined ? { ...origin.slack, readTs: open.slack.readTs } : origin.slack;
|
|
283
|
-
const turn = {};
|
|
284
|
-
const [creation] = await Promise.allSettled([createdIn(project, undefined, () => key, (on) => {
|
|
285
|
-
const created = startConversation({ platform, chat: key, slack, email: origin.email, ...on, forkOf, text: act.text, receivedMs: monotonicMs() - arrived });
|
|
286
|
-
// NULL-OK: field open is OPTIONAL and therefore it is ok to check for NULL
|
|
287
|
-
const told = open === undefined ? [] : [this.told(created.id, 1, `🆕 An independent ask: it runs in its own conversation ${created.id.slice(0, 8)} while ${open.id.slice(0, 8)} keeps working.`)];
|
|
288
|
-
turn.answered = this.awaitTurn(created.id, 1, relayOf(created.id, 1, false), told);
|
|
289
|
-
turn.id = created.id;
|
|
290
|
-
return created;
|
|
291
|
-
})]);
|
|
292
|
-
if (creation.status === "rejected") {
|
|
293
|
-
// NULL-OK: field id is OPTIONAL and therefore it is ok to check for NULL
|
|
294
|
-
if (turn.id === undefined)
|
|
295
|
-
throw creation.reason;
|
|
296
|
-
const reason = creation.reason instanceof Error ? creation.reason.message : String(creation.reason);
|
|
297
|
-
process.stderr.write(`[agent-gateway] conversation ${turn.id.slice(0, 8)}: the unread-room notice could not be written (${reason}); the turn runs on and its answer is delivered\n`);
|
|
298
|
-
this.deliver(this.told(turn.id, 1, `⚠️ No CLI was named and DeepAgent's subscription room could not be read; that notice could not be written to this conversation: ${reason}`));
|
|
299
|
-
return turn.answered;
|
|
300
|
-
}
|
|
301
|
-
// NULL-OK: field notice is OPTIONAL and therefore it is ok to check for NULL
|
|
302
|
-
if (creation.value.notice !== undefined)
|
|
303
|
-
this.deliver({ ...creation.value.notice, id: creation.value.made.id });
|
|
304
|
-
return turn.answered;
|
|
305
|
-
}
|
|
306
|
-
// NULL-OK: field email is OPTIONAL and therefore it is ok to check for NULL
|
|
307
|
-
if (origin.email !== undefined)
|
|
308
|
-
updateConversation(target.id, { email: { ...target.email, messageIds: [...target.email.messageIds, ...origin.email.messageIds] } });
|
|
309
|
-
const sent = sendToConversation(target.id, act.text, monotonicMs() - arrived);
|
|
310
|
-
// NULL-OK: field notice is OPTIONAL and therefore it is ok to check for NULL
|
|
311
|
-
const notice = queueNoticeOf(target.id, sent.n);
|
|
312
|
-
// NULL-OK: field open is OPTIONAL and therefore it is ok to check for NULL
|
|
313
|
-
const told = open !== undefined && target.id !== open.id ? [this.told(target.id, sent.n, `↪ Conversation ${target.id.slice(0, 8)} is doing that work; your message goes to it.`)] : [];
|
|
314
|
-
return this.awaitTurn(target.id, sent.n, relayOf(target.id, sent.n, notice !== undefined), [...told, ...(notice === undefined ? [] : [{ ...notice, id: target.id }])]);
|
|
315
|
-
}
|
|
316
|
-
/** A line to the chat that says where its message went; it rides in the turn's own delivery, so it goes out after the message is stored. */
|
|
317
|
-
told(id, n, text) {
|
|
318
|
-
return { id, n, turn: n, role: "system", kind: "status", text, ts: new Date().toISOString() };
|
|
319
|
-
}
|
|
320
|
-
end(id) {
|
|
321
|
-
for (const turn of this.turns.get(id)?.values() ?? [])
|
|
322
|
-
turn.close();
|
|
323
|
-
this.turns.delete(id);
|
|
324
|
-
this.driver.stop(id);
|
|
325
|
-
editOwed(id, (o) => { for (const turn of Object.keys(o))
|
|
326
|
-
delete o[turn]; });
|
|
327
|
-
}
|
|
328
|
-
/** Sends each event of the turn back in order and resolves on its last; a part the chat refuses rejects it, and its caller logs it NOT DELIVERED. */
|
|
329
|
-
awaitTurn(id, n, relay, pending = []) {
|
|
330
|
-
editOwed(id, (o) => { o[n] ??= n; });
|
|
331
|
-
let wake = () => { };
|
|
332
|
-
let closed = false;
|
|
333
|
-
const turns = this.turns.get(id) ?? new Map();
|
|
334
|
-
turns.set(n, { push: (event) => { pending.push(event); wake(); }, close: () => { closed = true; wake(); } });
|
|
335
|
-
this.turns.set(id, turns);
|
|
336
|
-
return (async () => {
|
|
337
|
-
for (;;) {
|
|
338
|
-
if (pending.length === 0) {
|
|
339
|
-
if (closed)
|
|
340
|
-
return relay.close();
|
|
341
|
-
await new Promise((resolve) => { wake = resolve; });
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
const event = pending.shift();
|
|
345
|
-
await relay.event(event).catch((err) => {
|
|
346
|
-
relay.close();
|
|
347
|
-
throw err;
|
|
348
|
-
});
|
|
349
|
-
const final = FINAL_KINDS.has(event.kind ?? "");
|
|
350
|
-
if (!closed)
|
|
351
|
-
editOwed(id, (o) => { if (final)
|
|
352
|
-
delete o[n];
|
|
353
|
-
else
|
|
354
|
-
o[n] = event.n; });
|
|
355
|
-
if (final) {
|
|
356
|
-
// NULL-OK: field turns is OPTIONAL and therefore it is ok to check for NULL
|
|
357
|
-
this.turns.get(id)?.delete(n);
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
})();
|
|
362
|
-
}
|
|
363
|
-
deliver(event) {
|
|
364
|
-
const turns = this.turns.get(event.id);
|
|
365
|
-
// NULL-OK: field turn is OPTIONAL and therefore it is ok to check for NULL
|
|
366
|
-
if (event.turn === undefined && turns !== undefined) {
|
|
367
|
-
for (const waiting of turns.values())
|
|
368
|
-
waiting.push(event);
|
|
369
|
-
this.turns.delete(event.id);
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
// NULL-OK: field turn is OPTIONAL and therefore it is ok to check for NULL
|
|
373
|
-
const turn = event.turn === undefined ? undefined : turns?.get(event.turn);
|
|
374
|
-
// NULL-OK: field turn is OPTIONAL and therefore it is ok to check for NULL
|
|
375
|
-
if (turn === undefined)
|
|
376
|
-
return;
|
|
377
|
-
turn.push(event);
|
|
378
|
-
if (FINAL_KINDS.has(event.kind ?? ""))
|
|
379
|
-
turns.delete(event.turn);
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
/**
|
|
383
|
-
* What the bound chat's conversation is told before its first message (#4453), about 300 tokens: that it is BN's agent
|
|
384
|
-
* in Meni's one chat, how the work it hands off reports back (his words in notify's answers, #4123), how it labels a reply,
|
|
385
|
-
* what the chat can show, and how it asks him something there.
|
|
386
|
-
*/
|
|
387
|
-
const chatBrief = (messenger) => `You are BN's agent in Meni's one ${messenger} chat with the BN bot: every message he sends the bot reaches you, and your reply streams back into that chat. Work exactly as the BN agent CLI does — answer a question directly; route a request to the project it belongs to, file it as a story on that project's board and let FleetView's queue run it, so this chat stays free for his next message; a story or lane you start reports back into this same chat — brief it with his own words, verbatim, to pass in the notify tool's answers when it tells him what he asked for, because notify refuses every other message.
|
|
388
|
-
Start every reply with a short bracketed label for the work it is about — the story id ([#4453]), the project ([AlphaWave]) or a few words — so he never has to pick a thread: you decide which work each message is about.
|
|
389
|
-
The chat shows bold, italic, code, links, bullets and emojis, never tables; keep replies short. Your reply already reaches the chat — never also send it with the notify tool.
|
|
390
|
-
Never use the AskUserQuestion tool here: its question stays on a screen he cannot see. To ask him, end the reply with the question and one line per choice starting with 🔘 — each becomes a choice he taps, and he may answer in his own words instead.`;
|
|
391
|
-
/** What a message forked beside the busy main conversation is told (#4584), about 70 tokens. */
|
|
392
|
-
const BESIDE = "(BN: the main conversation of this chat is still busy on an earlier message of his, so this one runs at once beside it, in a parallel conversation. Answer this message only, under its own label, and leave the work under way to the main conversation.)";
|
|
393
|
-
/** The answers his chat got from the forked conversations since the main one last heard from him (#4584): the main one, and each new fork of it, reads them as part of the chat. */
|
|
394
|
-
function answeredBeside(key, main) {
|
|
395
|
-
const heard = readEvents(main.id).filter((e) => e.role === "user").at(-1)?.ts ?? main.createdAt;
|
|
396
|
-
const answers = listConversations()
|
|
397
|
-
.filter((c) => c.chat === key && c.forkOf !== undefined && c.shelf !== "deleted")
|
|
398
|
-
.flatMap((c) => readEvents(c.id).filter((e) => e.kind === "answer" && e.ts > heard))
|
|
399
|
-
.sort((x, y) => x.ts.localeCompare(y.ts));
|
|
400
|
-
return answers.length === 0 ? [] : [`(BN: while the main conversation worked, these answers reached his chat from conversations running beside it — read them as part of this chat:\n${answers.map((e) => `• «${e.text}»`).join("\n")})`];
|
|
401
|
-
}
|
|
402
|
-
/** The text one message of the bound chat is sent as: the brief on the conversation's first turn, what the chat was answered beside it and whether this one runs beside it, then what the chat knows around it, then his own words, last. */
|
|
403
|
-
export function chatTurnText(key, text, context) {
|
|
404
|
-
const brief = chatBrief(key.startsWith(SLACK_DM) ? "Slack" : "Telegram");
|
|
405
|
-
const bound = boundOf(key);
|
|
406
|
-
// NULL-OK: field bound is OPTIONAL and therefore it is ok to check for NULL
|
|
407
|
-
if (bound === undefined)
|
|
408
|
-
return [brief, ...context, text].join("\n\n");
|
|
409
|
-
// A message beside a main conversation whose session is not written yet runs in a session of its own, which has not had the brief.
|
|
410
|
-
const first = !readEvents(bound.id).some((e) => e.role === "user") || (busy(bound) && !bound.started);
|
|
411
|
-
return [...(first ? [brief] : []), ...answeredBeside(key, bound), ...(busy(bound) ? [BESIDE] : []), ...context, text].join("\n\n");
|
|
412
|
-
}
|
|
413
|
-
/** The Claude CLI's own folder of session transcripts. */
|
|
414
|
-
function claudeProjects() {
|
|
415
|
-
return join(process.env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"), "projects");
|
|
416
|
-
}
|
|
417
|
-
/** Where the CLI keeps this session's transcript: found by its id across every folder, as the CLI itself finds it. */
|
|
418
|
-
function transcriptOf(sessionId) {
|
|
419
|
-
const projects = claudeProjects();
|
|
420
|
-
if (!existsSync(projects))
|
|
421
|
-
return undefined;
|
|
422
|
-
for (const dir of readdirSync(projects)) {
|
|
423
|
-
const file = join(projects, dir, `${sessionId}.jsonl`);
|
|
424
|
-
if (existsSync(file))
|
|
425
|
-
return file;
|
|
426
|
-
}
|
|
427
|
-
return undefined;
|
|
428
|
-
}
|
|
429
|
-
/** A file-system error Node names by its code (ENOENT, EBUSY, EPERM, …): the transcript moved, is locked or is gone. */
|
|
430
|
-
function fsFailure(err) {
|
|
431
|
-
return err instanceof Error && typeof err.code === "string";
|
|
432
|
-
}
|
|
433
|
-
/** A tool step as he reads it: the tool and what it is doing. */
|
|
434
|
-
function stepOf(block) {
|
|
435
|
-
const input = block.input ?? {};
|
|
436
|
-
const what = [input.description, input.command, input.file_path, input.pattern, input.url, input.prompt].find((v) => typeof v === "string" && v.trim() !== "");
|
|
437
|
-
// NULL-OK: field what is OPTIONAL and therefore it is ok to check for NULL
|
|
438
|
-
const shown = what === undefined ? "" : ` — ${what.replace(/\s+/g, " ").slice(0, 90)}`;
|
|
439
|
-
return `🔧 ${String(block.name ?? "tool")}${shown}`;
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* A conversation's Claude transcript read as it grows while a turn runs: each text block the agent writes and each tool
|
|
443
|
-
* it starts reach the chat's preview. The CLI writes one line per block, so the preview moves block by block.
|
|
444
|
-
*/
|
|
445
|
-
class TranscriptTail {
|
|
446
|
-
sessionId;
|
|
447
|
-
reply;
|
|
448
|
-
log;
|
|
449
|
-
file;
|
|
450
|
-
offset = 0;
|
|
451
|
-
rest = "";
|
|
452
|
-
timer;
|
|
453
|
-
/** Why the transcript could not be read on the last tick, named once until a read succeeds again. */
|
|
454
|
-
unread;
|
|
455
|
-
static of(id, reply, log) {
|
|
456
|
-
const c = readConversation(id);
|
|
457
|
-
if (c.provider !== "claude")
|
|
458
|
-
return undefined;
|
|
459
|
-
try {
|
|
460
|
-
return new TranscriptTail(c.sessionId, reply, log);
|
|
461
|
-
}
|
|
462
|
-
catch (err) {
|
|
463
|
-
if (!fsFailure(err))
|
|
464
|
-
throw err;
|
|
465
|
-
// OWNER: the turn's answer comes from the CLI's own end-of-turn report, never from the tail — a transcript that cannot be read now leaves this turn without a text preview, named, and its answer still reaches the chat.
|
|
466
|
-
log(`transcript ${c.sessionId} could not be read (${err.message}) — this turn's preview shows its working state only`);
|
|
467
|
-
return undefined;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
constructor(sessionId, reply, log) {
|
|
471
|
-
this.sessionId = sessionId;
|
|
472
|
-
this.reply = reply;
|
|
473
|
-
this.log = log;
|
|
474
|
-
this.file = transcriptOf(sessionId);
|
|
475
|
-
// NULL-OK: field file is OPTIONAL and therefore it is ok to check for NULL
|
|
476
|
-
this.offset = this.file === undefined ? 0 : statSync(this.file).size;
|
|
477
|
-
// GATE: how often a running turn's transcript is read for the chat's preview; the preview itself is paced by the messenger.
|
|
478
|
-
this.timer = setInterval(() => this.tick(), 400);
|
|
479
|
-
this.timer.unref();
|
|
480
|
-
}
|
|
481
|
-
stop() {
|
|
482
|
-
clearInterval(this.timer);
|
|
483
|
-
}
|
|
484
|
-
/** One read on the timer: a transcript moved, locked or gone is named once and read again on the next tick, never a crash of the daemon. */
|
|
485
|
-
tick() {
|
|
486
|
-
try {
|
|
487
|
-
this.read();
|
|
488
|
-
this.unread = undefined;
|
|
489
|
-
}
|
|
490
|
-
catch (err) {
|
|
491
|
-
if (!fsFailure(err))
|
|
492
|
-
throw err;
|
|
493
|
-
// OWNER: the timer is this read's own trigger root — the file system refused this read (ENOENT, EBUSY, EPERM, …), so it is named once, a file that is gone is looked for again, and the next tick reads again.
|
|
494
|
-
if (err.code === "ENOENT")
|
|
495
|
-
this.file = undefined;
|
|
496
|
-
if (this.unread !== err.message)
|
|
497
|
-
this.log(`transcript ${this.sessionId} could not be read (${err.message}) — the chat's preview waits for it; the answer still comes`);
|
|
498
|
-
this.unread = err.message;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
read() {
|
|
502
|
-
this.file ??= transcriptOf(this.sessionId);
|
|
503
|
-
// NULL-OK: field file is OPTIONAL and therefore it is ok to check for NULL
|
|
504
|
-
if (this.file === undefined)
|
|
505
|
-
return;
|
|
506
|
-
const size = statSync(this.file).size;
|
|
507
|
-
if (size <= this.offset)
|
|
508
|
-
return;
|
|
509
|
-
const fd = openSync(this.file, "r");
|
|
510
|
-
const buf = Buffer.alloc(size - this.offset);
|
|
511
|
-
try {
|
|
512
|
-
readSync(fd, buf, 0, buf.length, this.offset);
|
|
513
|
-
}
|
|
514
|
-
finally {
|
|
515
|
-
closeSync(fd);
|
|
516
|
-
}
|
|
517
|
-
this.offset = size;
|
|
518
|
-
const lines = (this.rest + buf.toString("utf8")).split("\n");
|
|
519
|
-
this.rest = lines.pop();
|
|
520
|
-
for (const line of lines)
|
|
521
|
-
this.take(line);
|
|
522
|
-
}
|
|
523
|
-
take(line) {
|
|
524
|
-
if (line.trim() === "")
|
|
525
|
-
return;
|
|
526
|
-
let entry;
|
|
527
|
-
try {
|
|
528
|
-
entry = JSON.parse(line);
|
|
529
|
-
}
|
|
530
|
-
catch (err) {
|
|
531
|
-
if (!(err instanceof SyntaxError))
|
|
532
|
-
throw err;
|
|
533
|
-
// OWNER: ITER/LOOP — a transcript line that is not JSON is left out of the preview and named; the answer itself comes from the CLI's Stop, not from here.
|
|
534
|
-
this.log(`transcript ${this.sessionId}: a line is not JSON (${err.message}) — it is left out of the chat's preview`);
|
|
535
|
-
return;
|
|
536
|
-
}
|
|
537
|
-
if (entry.type !== "assistant" || !Array.isArray(entry.message?.content))
|
|
538
|
-
return;
|
|
539
|
-
for (const block of entry.message.content) {
|
|
540
|
-
if (block.type === "text" && typeof block.text === "string")
|
|
541
|
-
this.reply.wrote(block.text);
|
|
542
|
-
if (block.type === "tool_use")
|
|
543
|
-
this.reply.step(stepOf(block));
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
/** The draft of each streamed turn this process shows, one id per turn so no turn's preview lands on another's. */
|
|
548
|
-
let draftIds = 0;
|
|
549
|
-
/** The conversation each draft still streaming belongs to, so his stop on one of the parallel replies stops that reply's conversation (#4584). */
|
|
550
|
-
const drafts = new Map();
|
|
551
|
-
/** The conversation whose reply streams in this draft, or undefined once its turn ended or when an earlier BN process drew it. */
|
|
552
|
-
export function conversationOfDraft(draft) {
|
|
553
|
-
return drafts.get(draft);
|
|
554
|
-
}
|
|
555
|
-
/** The bound chat's turns as they happen (#4453): the working state at once, the agent's text and steps as a paced preview, the answer with its choices as buttons. */
|
|
556
|
-
export function streamedChat(messenger, log) {
|
|
557
|
-
return {
|
|
558
|
-
say: (text) => messenger.send(text),
|
|
559
|
-
turn(id, _n, queued) {
|
|
560
|
-
const draft = ++draftIds;
|
|
561
|
-
drafts.set(draft, id);
|
|
562
|
-
const reply = new StreamedReply(messenger, draft, log);
|
|
563
|
-
let tail;
|
|
564
|
-
if (!queued)
|
|
565
|
-
reply.begin();
|
|
566
|
-
const ended = () => {
|
|
567
|
-
drafts.delete(draft);
|
|
568
|
-
// NULL-OK: field tail is OPTIONAL and therefore it is ok to check for NULL
|
|
569
|
-
tail?.stop();
|
|
570
|
-
};
|
|
571
|
-
const close = () => {
|
|
572
|
-
ended();
|
|
573
|
-
reply.close();
|
|
574
|
-
};
|
|
575
|
-
return {
|
|
576
|
-
close,
|
|
577
|
-
async event(event) {
|
|
578
|
-
switch (event.kind) {
|
|
579
|
-
case "start":
|
|
580
|
-
reply.begin(event.text);
|
|
581
|
-
// NULL-OK: field tail is OPTIONAL and therefore it is ok to check for NULL
|
|
582
|
-
tail ??= TranscriptTail.of(id, reply, log);
|
|
583
|
-
return;
|
|
584
|
-
case "answer": {
|
|
585
|
-
ended();
|
|
586
|
-
const { text, choices } = choicesOf(event.text);
|
|
587
|
-
return reply.finish(text, choices.map((label, i) => ({ label, data: `b|${id.slice(0, 8)}|${i}` })));
|
|
588
|
-
}
|
|
589
|
-
case "failed":
|
|
590
|
-
case "stopped":
|
|
591
|
-
case "archived":
|
|
592
|
-
case "deleted":
|
|
593
|
-
ended();
|
|
594
|
-
return reply.finish(event.text);
|
|
595
|
-
case "returned":
|
|
596
|
-
case "resumed":
|
|
597
|
-
case "restored":
|
|
598
|
-
return;
|
|
599
|
-
default:
|
|
600
|
-
return messenger.send(event.text);
|
|
601
|
-
}
|
|
602
|
-
},
|
|
603
|
-
};
|
|
604
|
-
},
|
|
605
|
-
};
|
|
606
|
-
}
|
package/dist/chunk.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
export const NOTIFY_CHUNK_LIMIT = 500;
|
|
2
|
-
function packChunks(message, body, wordAware) {
|
|
3
|
-
const chunks = [];
|
|
4
|
-
let rest = message;
|
|
5
|
-
while (rest.length > body) {
|
|
6
|
-
let cut = body;
|
|
7
|
-
if (wordAware) {
|
|
8
|
-
const window = rest.slice(0, body + 1);
|
|
9
|
-
const brk = Math.max(window.lastIndexOf(" "), window.lastIndexOf("\n"));
|
|
10
|
-
if (brk >= Math.floor(body * 0.6))
|
|
11
|
-
cut = brk;
|
|
12
|
-
}
|
|
13
|
-
chunks.push(rest.slice(0, cut).replace(/\s+$/, ""));
|
|
14
|
-
rest = rest.slice(cut).replace(/^\s+/, "");
|
|
15
|
-
}
|
|
16
|
-
if (rest.length)
|
|
17
|
-
chunks.push(rest);
|
|
18
|
-
return chunks;
|
|
19
|
-
}
|
|
20
|
-
const BLOCK_START = /^\s*(#{1,6}\s|>|[-*+]\s|\||```|\*\*\S)/;
|
|
21
|
-
export function withLead(lead, text) {
|
|
22
|
-
return `${lead}${BLOCK_START.test(text) ? "\n" : " "}${text}`;
|
|
23
|
-
}
|
|
24
|
-
export function splitForNotify(message, limit = NOTIFY_CHUNK_LIMIT) {
|
|
25
|
-
if (message.length <= limit)
|
|
26
|
-
return [message];
|
|
27
|
-
let parts = Math.ceil(message.length / limit);
|
|
28
|
-
for (;;) {
|
|
29
|
-
const reserve = `(${parts}/${parts}) `.length;
|
|
30
|
-
const needed = Math.ceil(message.length / Math.max(1, limit - reserve));
|
|
31
|
-
if (needed <= parts)
|
|
32
|
-
break;
|
|
33
|
-
parts = needed;
|
|
34
|
-
}
|
|
35
|
-
const body = Math.max(1, limit - `(${parts}/${parts}) `.length);
|
|
36
|
-
let chunks = packChunks(message, body, true);
|
|
37
|
-
if (chunks.length > parts)
|
|
38
|
-
chunks = packChunks(message, body, false);
|
|
39
|
-
const total = chunks.length;
|
|
40
|
-
return chunks.map((chunk, i) => withLead(`(${i + 1}/${total})`, chunk));
|
|
41
|
-
}
|