omni-notify-mcp 1.3.443 → 1.3.445
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/agentGateway.js +293 -23
- package/dist/gateway/panel.js +7 -3
- package/dist/messaging/decisionDigit.js +1 -1
- package/dist/messaging/messenger.js +482 -0
- package/dist/notify.js +353 -116
- package/dist/shared/agentSession.js +74 -27
- package/dist/shared/webhost/autoSwitch.cjs +173 -78
- package/package.json +1 -1
package/dist/agentGateway.js
CHANGED
|
@@ -10,12 +10,37 @@
|
|
|
10
10
|
* An email thread (#2105) is one conversation for good, and gets only its answer, question or failure, each whole.
|
|
11
11
|
* Every conversation starts in Meni's workspace root, as the web page's and the MCP door's do.
|
|
12
12
|
*/
|
|
13
|
+
import { closeSync, existsSync, openSync, readdirSync, readSync, statSync } from "fs";
|
|
14
|
+
import { homedir } from "os";
|
|
15
|
+
import { join } from "path";
|
|
13
16
|
import { splitForNotify } from "./chunk.js";
|
|
14
|
-
import {
|
|
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, updateConversation, } from "./gateway/conversationOwner.js";
|
|
15
19
|
import { conversationOwner } from "./gateway/conversationOwner.js";
|
|
16
20
|
import { clearOpenCodeLogs } from "./gateway/conversationOwner.js";
|
|
17
21
|
import { resendUndelivered, turnEnded } from "./gateway/mcpEvents.js";
|
|
18
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
|
+
}
|
|
19
44
|
/** A chat: the newest conversation not stopped (`/new` and `/stop` end it), and every line of the turn, the answer split at 500. */
|
|
20
45
|
const CHAT = {
|
|
21
46
|
conversationOf: openConversationOf,
|
|
@@ -24,6 +49,33 @@ const CHAT = {
|
|
|
24
49
|
allOf: openConversationsOf,
|
|
25
50
|
carries: (kind) => kind !== "returned",
|
|
26
51
|
parts: (event) => (event.kind === "answer" ? splitForNotify(event.text) : [event.text]),
|
|
52
|
+
stopped: stoppedOfMany,
|
|
53
|
+
renew: endNewest,
|
|
54
|
+
folderOf: (text) => resolveProject(undefined, text),
|
|
55
|
+
};
|
|
56
|
+
/** Meni's one private chat with the bot (#4453): its newest conversation not deleted — an idle archive keeps it bound, only /new replaces it. */
|
|
57
|
+
const boundOf = (key) => listConversations().find((c) => c.chat === key && c.shelf !== "deleted");
|
|
58
|
+
/** The bound chat: ONE conversation at the workspace root, every message goes on with it, a stop pauses it, /new opens the next one at once. */
|
|
59
|
+
const ONE = {
|
|
60
|
+
conversationOf: boundOf,
|
|
61
|
+
routeOf: boundOf,
|
|
62
|
+
stopOf: boundOf,
|
|
63
|
+
allOf: (key) => [boundOf(key)].filter((c) => c !== undefined),
|
|
64
|
+
carries: CHAT.carries,
|
|
65
|
+
parts: CHAT.parts,
|
|
66
|
+
stopped: () => "⏹ Stopped. Your next message picks the conversation up where it stopped.",
|
|
67
|
+
folderOf: () => rootFolder(),
|
|
68
|
+
async renew(key, platform, end) {
|
|
69
|
+
const bound = boundOf(key);
|
|
70
|
+
// NULL-OK: field bound is OPTIONAL and therefore it is ok to check for NULL
|
|
71
|
+
if (bound !== undefined) {
|
|
72
|
+
end(bound.id);
|
|
73
|
+
archiveConversation(bound.id);
|
|
74
|
+
}
|
|
75
|
+
const { notice } = await createdIn(rootFolder(), undefined, () => key, (on) => openConversation({ platform, chat: key, ...on }));
|
|
76
|
+
// NULL-OK: field notice is OPTIONAL and therefore it is ok to check for NULL
|
|
77
|
+
return `🆕 Fresh conversation — your next message starts it.${notice === undefined ? "" : `\n${notice.text}`}`;
|
|
78
|
+
},
|
|
27
79
|
};
|
|
28
80
|
const MAILED_KINDS = new Set(["answer", "failed", "stopped"]);
|
|
29
81
|
/** An email thread: its one conversation, a stopped one resumed by the next reply; only what needs Meni is mailed, each whole. */
|
|
@@ -35,8 +87,17 @@ const MAIL = {
|
|
|
35
87
|
allOf: (key) => listConversations().filter((c) => c.chat === key),
|
|
36
88
|
carries: (kind) => MAILED_KINDS.has(kind),
|
|
37
89
|
parts: (event) => [event.text],
|
|
90
|
+
stopped: stoppedOfMany,
|
|
91
|
+
renew: endNewest,
|
|
92
|
+
folderOf: (text) => resolveProject(undefined, text),
|
|
38
93
|
};
|
|
39
|
-
const CARRIAGE = { telegram:
|
|
94
|
+
const CARRIAGE = { telegram: ONE, slack: CHAT, discord: CHAT, teams: CHAT, ntfy: CHAT, sms: CHAT, email: MAIL };
|
|
95
|
+
/** 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. */
|
|
96
|
+
export const SLACK_DM = "slack:dm:";
|
|
97
|
+
/** How a chat key's conversations are carried. */
|
|
98
|
+
function carriageOf(platform, key) {
|
|
99
|
+
return key.startsWith(SLACK_DM) ? ONE : CARRIAGE[platform];
|
|
100
|
+
}
|
|
40
101
|
const COMMAND_RE = /^\/?(new|stop)(@\w+)?$/i;
|
|
41
102
|
const FINAL_KINDS = new Set(["answer", "failed", "stopped", "archived", "deleted"]);
|
|
42
103
|
export class AgentGateway {
|
|
@@ -82,7 +143,7 @@ export class AgentGateway {
|
|
|
82
143
|
for (const [turn, sent] of owed) {
|
|
83
144
|
const pending = readEvents(c.id).filter((e) => e.turn === Number(turn) && e.n > sent && e.role !== "user").map((e) => ({ ...e, id: c.id }));
|
|
84
145
|
// OWNER: ITER/LOOP — a thread that cannot be reached is shown on its conversation and the next owed turn is still carried on.
|
|
85
|
-
void this.awaitTurn(c.id, Number(turn), (part) => door(c, part),
|
|
146
|
+
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)}` }));
|
|
86
147
|
}
|
|
87
148
|
}
|
|
88
149
|
/** 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. */
|
|
@@ -109,6 +170,18 @@ export class AgentGateway {
|
|
|
109
170
|
return { kind: cmd[1].toLowerCase() === "new" ? "new" : "stop" };
|
|
110
171
|
return undefined;
|
|
111
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* 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 `!`
|
|
175
|
+
* 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
|
|
176
|
+
* 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.
|
|
177
|
+
*/
|
|
178
|
+
classifyChat(raw, bare) {
|
|
179
|
+
const said = raw.trim();
|
|
180
|
+
const text = said.replace(/^!\s*/, "");
|
|
181
|
+
const cmd = text.match(COMMAND_RE);
|
|
182
|
+
const commanded = cmd !== null && (bare || text.startsWith("/") || said.startsWith(AGENT_PREFIX));
|
|
183
|
+
return commanded ? { kind: cmd[1].toLowerCase() === "new" ? "new" : "stop" } : { kind: "say", text };
|
|
184
|
+
}
|
|
112
185
|
/** A message inside a thread the agent holds is all for the agent: `/new` and `/stop` act, anything else is said, with or without `!`. */
|
|
113
186
|
classifyInThread(key, raw) {
|
|
114
187
|
return this.classify(key, raw) ?? { kind: "say", text: raw.trim() };
|
|
@@ -120,7 +193,7 @@ export class AgentGateway {
|
|
|
120
193
|
}
|
|
121
194
|
/** Shows on this chat key's conversation that its answer never reached the chat, so the web page tells what the chat could not. */
|
|
122
195
|
noteUndelivered(key, platform, text) {
|
|
123
|
-
const c =
|
|
196
|
+
const c = carriageOf(platform, key).conversationOf(key);
|
|
124
197
|
// NULL-OK: field c is OPTIONAL and therefore it is ok to check for NULL
|
|
125
198
|
if (c === undefined)
|
|
126
199
|
return;
|
|
@@ -141,9 +214,11 @@ export class AgentGateway {
|
|
|
141
214
|
updateConversation(c.id, { slack: { ...c.slack, readTs } });
|
|
142
215
|
}
|
|
143
216
|
/** Acts on one addressed message. Resolves when its answer (or its failure) has been sent back. */
|
|
144
|
-
async handle(key, platform, act,
|
|
217
|
+
async handle(key, platform, act, out, origin = {}) {
|
|
145
218
|
const arrived = monotonicMs();
|
|
146
|
-
const carriage =
|
|
219
|
+
const carriage = carriageOf(platform, key);
|
|
220
|
+
const reply = typeof out === "function" ? out : out.say;
|
|
221
|
+
const relayOf = (id, n, queued) => (typeof out === "function" ? partsRelay(out, carriage) : out.turn(id, n, queued));
|
|
147
222
|
const refusal = conversationRefusal();
|
|
148
223
|
if (refusal !== undefined)
|
|
149
224
|
return reply(refusal);
|
|
@@ -157,16 +232,10 @@ export class AgentGateway {
|
|
|
157
232
|
if (stopped === undefined)
|
|
158
233
|
return reply("Nothing to stop here — no conversation is open.");
|
|
159
234
|
this.end(stopped.id);
|
|
160
|
-
|
|
161
|
-
return reply(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.`);
|
|
162
|
-
}
|
|
163
|
-
if (act.kind === "new") {
|
|
164
|
-
// NULL-OK: field open is OPTIONAL and therefore it is ok to check for NULL
|
|
165
|
-
if (open !== undefined)
|
|
166
|
-
this.end(open.id);
|
|
167
|
-
const stay = carriage.allOf(key).length;
|
|
168
|
-
return reply(stay === 0 ? "🆕 New conversation. Your next ! message starts it." : `🆕 Newest conversation ended. ${stay} other conversation${stay === 1 ? " stays" : "s stay"} open.`);
|
|
235
|
+
return reply(carriage.stopped(stopped, carriage.allOf(key).filter((c) => c.id !== stopped.id).length));
|
|
169
236
|
}
|
|
237
|
+
if (act.kind === "new")
|
|
238
|
+
return reply(await carriage.renew(key, platform, (id) => this.end(id)));
|
|
170
239
|
if (act.text === "")
|
|
171
240
|
return reply("Send `! <your message>` to talk to the agent.");
|
|
172
241
|
const target = carriage.routeOf(key, act.text);
|
|
@@ -174,7 +243,7 @@ export class AgentGateway {
|
|
|
174
243
|
if (target === undefined) {
|
|
175
244
|
let project;
|
|
176
245
|
try {
|
|
177
|
-
project =
|
|
246
|
+
project = carriage.folderOf(act.text);
|
|
178
247
|
}
|
|
179
248
|
catch (err) {
|
|
180
249
|
// 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
|
|
@@ -190,7 +259,7 @@ export class AgentGateway {
|
|
|
190
259
|
const created = startConversation({ platform, chat: key, slack, email: origin.email, ...on, text: act.text, receivedMs: monotonicMs() - arrived });
|
|
191
260
|
// NULL-OK: field open is OPTIONAL and therefore it is ok to check for NULL
|
|
192
261
|
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.`)];
|
|
193
|
-
turn.answered = this.awaitTurn(created.id, 1,
|
|
262
|
+
turn.answered = this.awaitTurn(created.id, 1, relayOf(created.id, 1, false), told);
|
|
194
263
|
turn.id = created.id;
|
|
195
264
|
return created;
|
|
196
265
|
})]);
|
|
@@ -216,7 +285,7 @@ export class AgentGateway {
|
|
|
216
285
|
const notice = queueNoticeOf(target.id, sent.n);
|
|
217
286
|
// NULL-OK: field open is OPTIONAL and therefore it is ok to check for NULL
|
|
218
287
|
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.`)] : [];
|
|
219
|
-
return this.awaitTurn(target.id, sent.n,
|
|
288
|
+
return this.awaitTurn(target.id, sent.n, relayOf(target.id, sent.n, notice !== undefined), [...told, ...(notice === undefined ? [] : [{ ...notice, id: target.id }])]);
|
|
220
289
|
}
|
|
221
290
|
/** 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. */
|
|
222
291
|
told(id, n, text) {
|
|
@@ -231,7 +300,7 @@ export class AgentGateway {
|
|
|
231
300
|
delete o[turn]; });
|
|
232
301
|
}
|
|
233
302
|
/** 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. */
|
|
234
|
-
awaitTurn(id, n,
|
|
303
|
+
awaitTurn(id, n, relay, pending = []) {
|
|
235
304
|
editOwed(id, (o) => { o[n] ??= n; });
|
|
236
305
|
let wake = () => { };
|
|
237
306
|
let closed = false;
|
|
@@ -242,14 +311,15 @@ export class AgentGateway {
|
|
|
242
311
|
for (;;) {
|
|
243
312
|
if (pending.length === 0) {
|
|
244
313
|
if (closed)
|
|
245
|
-
return;
|
|
314
|
+
return relay.close();
|
|
246
315
|
await new Promise((resolve) => { wake = resolve; });
|
|
247
316
|
continue;
|
|
248
317
|
}
|
|
249
318
|
const event = pending.shift();
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
319
|
+
await relay.event(event).catch((err) => {
|
|
320
|
+
relay.close();
|
|
321
|
+
throw err;
|
|
322
|
+
});
|
|
253
323
|
const final = FINAL_KINDS.has(event.kind ?? "");
|
|
254
324
|
if (!closed)
|
|
255
325
|
editOwed(id, (o) => { if (final)
|
|
@@ -283,3 +353,203 @@ export class AgentGateway {
|
|
|
283
353
|
turns.delete(event.turn);
|
|
284
354
|
}
|
|
285
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* What the bound chat's conversation is told before its first message (#4453), about 300 tokens: that it is BN's agent
|
|
358
|
+
* 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,
|
|
359
|
+
* what the chat can show, and how it asks him something there.
|
|
360
|
+
*/
|
|
361
|
+
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.
|
|
362
|
+
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.
|
|
363
|
+
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.
|
|
364
|
+
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.`;
|
|
365
|
+
/** The text one message of the bound chat is sent as: the brief on the conversation's first turn, then what the chat knows around it, then his own words, last. */
|
|
366
|
+
export function chatTurnText(key, text, context) {
|
|
367
|
+
const bound = boundOf(key);
|
|
368
|
+
// NULL-OK: field bound is OPTIONAL and therefore it is ok to check for NULL
|
|
369
|
+
const first = bound === undefined || !readEvents(bound.id).some((e) => e.role === "user");
|
|
370
|
+
return [...(first ? [chatBrief(key.startsWith(SLACK_DM) ? "Slack" : "Telegram")] : []), ...context, text].join("\n\n");
|
|
371
|
+
}
|
|
372
|
+
/** The Claude CLI's own folder of session transcripts. */
|
|
373
|
+
function claudeProjects() {
|
|
374
|
+
return join(process.env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"), "projects");
|
|
375
|
+
}
|
|
376
|
+
/** Where the CLI keeps this session's transcript: found by its id across every folder, as the CLI itself finds it. */
|
|
377
|
+
function transcriptOf(sessionId) {
|
|
378
|
+
const projects = claudeProjects();
|
|
379
|
+
if (!existsSync(projects))
|
|
380
|
+
return undefined;
|
|
381
|
+
for (const dir of readdirSync(projects)) {
|
|
382
|
+
const file = join(projects, dir, `${sessionId}.jsonl`);
|
|
383
|
+
if (existsSync(file))
|
|
384
|
+
return file;
|
|
385
|
+
}
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
/** A file-system error Node names by its code (ENOENT, EBUSY, EPERM, …): the transcript moved, is locked or is gone. */
|
|
389
|
+
function fsFailure(err) {
|
|
390
|
+
return err instanceof Error && typeof err.code === "string";
|
|
391
|
+
}
|
|
392
|
+
/** A tool step as he reads it: the tool and what it is doing. */
|
|
393
|
+
function stepOf(block) {
|
|
394
|
+
const input = block.input ?? {};
|
|
395
|
+
const what = [input.description, input.command, input.file_path, input.pattern, input.url, input.prompt].find((v) => typeof v === "string" && v.trim() !== "");
|
|
396
|
+
// NULL-OK: field what is OPTIONAL and therefore it is ok to check for NULL
|
|
397
|
+
const shown = what === undefined ? "" : ` — ${what.replace(/\s+/g, " ").slice(0, 90)}`;
|
|
398
|
+
return `🔧 ${String(block.name ?? "tool")}${shown}`;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* A conversation's Claude transcript read as it grows while a turn runs: each text block the agent writes and each tool
|
|
402
|
+
* it starts reach the chat's preview. The CLI writes one line per block, so the preview moves block by block.
|
|
403
|
+
*/
|
|
404
|
+
class TranscriptTail {
|
|
405
|
+
sessionId;
|
|
406
|
+
reply;
|
|
407
|
+
log;
|
|
408
|
+
file;
|
|
409
|
+
offset = 0;
|
|
410
|
+
rest = "";
|
|
411
|
+
timer;
|
|
412
|
+
/** Why the transcript could not be read on the last tick, named once until a read succeeds again. */
|
|
413
|
+
unread;
|
|
414
|
+
static of(id, reply, log) {
|
|
415
|
+
const c = readConversation(id);
|
|
416
|
+
if (c.provider !== "claude")
|
|
417
|
+
return undefined;
|
|
418
|
+
try {
|
|
419
|
+
return new TranscriptTail(c.sessionId, reply, log);
|
|
420
|
+
}
|
|
421
|
+
catch (err) {
|
|
422
|
+
if (!fsFailure(err))
|
|
423
|
+
throw err;
|
|
424
|
+
// 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.
|
|
425
|
+
log(`transcript ${c.sessionId} could not be read (${err.message}) — this turn's preview shows its working state only`);
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
constructor(sessionId, reply, log) {
|
|
430
|
+
this.sessionId = sessionId;
|
|
431
|
+
this.reply = reply;
|
|
432
|
+
this.log = log;
|
|
433
|
+
this.file = transcriptOf(sessionId);
|
|
434
|
+
// NULL-OK: field file is OPTIONAL and therefore it is ok to check for NULL
|
|
435
|
+
this.offset = this.file === undefined ? 0 : statSync(this.file).size;
|
|
436
|
+
// GATE: how often a running turn's transcript is read for the chat's preview; the preview itself is paced by the messenger.
|
|
437
|
+
this.timer = setInterval(() => this.tick(), 400);
|
|
438
|
+
this.timer.unref();
|
|
439
|
+
}
|
|
440
|
+
stop() {
|
|
441
|
+
clearInterval(this.timer);
|
|
442
|
+
}
|
|
443
|
+
/** 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. */
|
|
444
|
+
tick() {
|
|
445
|
+
try {
|
|
446
|
+
this.read();
|
|
447
|
+
this.unread = undefined;
|
|
448
|
+
}
|
|
449
|
+
catch (err) {
|
|
450
|
+
if (!fsFailure(err))
|
|
451
|
+
throw err;
|
|
452
|
+
// 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.
|
|
453
|
+
if (err.code === "ENOENT")
|
|
454
|
+
this.file = undefined;
|
|
455
|
+
if (this.unread !== err.message)
|
|
456
|
+
this.log(`transcript ${this.sessionId} could not be read (${err.message}) — the chat's preview waits for it; the answer still comes`);
|
|
457
|
+
this.unread = err.message;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
read() {
|
|
461
|
+
this.file ??= transcriptOf(this.sessionId);
|
|
462
|
+
// NULL-OK: field file is OPTIONAL and therefore it is ok to check for NULL
|
|
463
|
+
if (this.file === undefined)
|
|
464
|
+
return;
|
|
465
|
+
const size = statSync(this.file).size;
|
|
466
|
+
if (size <= this.offset)
|
|
467
|
+
return;
|
|
468
|
+
const fd = openSync(this.file, "r");
|
|
469
|
+
const buf = Buffer.alloc(size - this.offset);
|
|
470
|
+
try {
|
|
471
|
+
readSync(fd, buf, 0, buf.length, this.offset);
|
|
472
|
+
}
|
|
473
|
+
finally {
|
|
474
|
+
closeSync(fd);
|
|
475
|
+
}
|
|
476
|
+
this.offset = size;
|
|
477
|
+
const lines = (this.rest + buf.toString("utf8")).split("\n");
|
|
478
|
+
this.rest = lines.pop();
|
|
479
|
+
for (const line of lines)
|
|
480
|
+
this.take(line);
|
|
481
|
+
}
|
|
482
|
+
take(line) {
|
|
483
|
+
if (line.trim() === "")
|
|
484
|
+
return;
|
|
485
|
+
let entry;
|
|
486
|
+
try {
|
|
487
|
+
entry = JSON.parse(line);
|
|
488
|
+
}
|
|
489
|
+
catch (err) {
|
|
490
|
+
if (!(err instanceof SyntaxError))
|
|
491
|
+
throw err;
|
|
492
|
+
// 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.
|
|
493
|
+
this.log(`transcript ${this.sessionId}: a line is not JSON (${err.message}) — it is left out of the chat's preview`);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (entry.type !== "assistant" || !Array.isArray(entry.message?.content))
|
|
497
|
+
return;
|
|
498
|
+
for (const block of entry.message.content) {
|
|
499
|
+
if (block.type === "text" && typeof block.text === "string")
|
|
500
|
+
this.reply.wrote(block.text);
|
|
501
|
+
if (block.type === "tool_use")
|
|
502
|
+
this.reply.step(stepOf(block));
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
/** The draft of each streamed turn this process shows, one id per turn so no turn's preview lands on another's. */
|
|
507
|
+
let draftIds = 0;
|
|
508
|
+
/** 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. */
|
|
509
|
+
export function streamedChat(messenger, log) {
|
|
510
|
+
return {
|
|
511
|
+
say: (text) => messenger.send(text),
|
|
512
|
+
turn(id, _n, queued) {
|
|
513
|
+
const reply = new StreamedReply(messenger, ++draftIds, log);
|
|
514
|
+
let tail;
|
|
515
|
+
if (!queued)
|
|
516
|
+
reply.begin();
|
|
517
|
+
const close = () => {
|
|
518
|
+
// NULL-OK: field tail is OPTIONAL and therefore it is ok to check for NULL
|
|
519
|
+
tail?.stop();
|
|
520
|
+
reply.close();
|
|
521
|
+
};
|
|
522
|
+
return {
|
|
523
|
+
close,
|
|
524
|
+
async event(event) {
|
|
525
|
+
switch (event.kind) {
|
|
526
|
+
case "start":
|
|
527
|
+
reply.begin(event.text);
|
|
528
|
+
// NULL-OK: field tail is OPTIONAL and therefore it is ok to check for NULL
|
|
529
|
+
tail ??= TranscriptTail.of(id, reply, log);
|
|
530
|
+
return;
|
|
531
|
+
case "answer": {
|
|
532
|
+
// NULL-OK: field tail is OPTIONAL and therefore it is ok to check for NULL
|
|
533
|
+
tail?.stop();
|
|
534
|
+
const { text, choices } = choicesOf(event.text);
|
|
535
|
+
return reply.finish(text, choices.map((label, i) => ({ label, data: `b|${id.slice(0, 8)}|${i}` })));
|
|
536
|
+
}
|
|
537
|
+
case "failed":
|
|
538
|
+
case "stopped":
|
|
539
|
+
case "archived":
|
|
540
|
+
case "deleted":
|
|
541
|
+
// NULL-OK: field tail is OPTIONAL and therefore it is ok to check for NULL
|
|
542
|
+
tail?.stop();
|
|
543
|
+
return reply.finish(event.text);
|
|
544
|
+
case "returned":
|
|
545
|
+
case "resumed":
|
|
546
|
+
case "restored":
|
|
547
|
+
return;
|
|
548
|
+
default:
|
|
549
|
+
return messenger.send(event.text);
|
|
550
|
+
}
|
|
551
|
+
},
|
|
552
|
+
};
|
|
553
|
+
},
|
|
554
|
+
};
|
|
555
|
+
}
|
package/dist/gateway/panel.js
CHANGED
|
@@ -24,11 +24,10 @@ export const SETTINGS = [
|
|
|
24
24
|
{ path: ["desktop", "tts"], label: "Read aloud", kind: "toggle", hint: "On speaks each desktop notification out loud." },
|
|
25
25
|
{ path: ["desktop", "ttsVoice"], label: "Voice", kind: "text", hint: "Edge voice name, e.g. en-US-GuyNeural. Empty uses the default voice." },
|
|
26
26
|
] },
|
|
27
|
-
{ guide: { summary: "Create a bot (2 min)", steps: ["Open Telegram, search `@BotFather`.", "Send `/newbot` and follow the prompts; copy the token it gives you.", "
|
|
27
|
+
{ guide: { summary: "Create a bot (2 min)", steps: ["Open Telegram, search `@BotFather`.", "Send `/newbot` and follow the prompts; copy the token it gives you.", "Paste the token below — that switches Telegram on.", "Send your bot any message: BN binds that chat to you, and the chat is your BN agent from then on."], need: [["telegram.token"], ["telegram.chatIds"]] }, title: "✈️ Telegram", rows: [
|
|
28
28
|
{ path: ["telegram", "enabled"], label: "Enabled", kind: "toggle" },
|
|
29
|
-
{ path: ["telegram", "token"], label: "Bot token", kind: "secret", hint: "From @BotFather after /newbot, shaped like 123456:ABC-DEF..." },
|
|
29
|
+
{ path: ["telegram", "token"], label: "Bot token", kind: "secret", hint: "From @BotFather after /newbot, shaped like 123456:ABC-DEF... Pasting it switches Telegram on; the first message you send the bot binds your chat." },
|
|
30
30
|
{ path: ["telegram", "parseMode"], label: "Formatting", kind: "choice", choices: TELEGRAM_PARSE_MODES, fallback: "HTML", hint: "HTML (default) turns an agent's markdown bold, italics, code, links and bullets into Telegram formatting. MarkdownV2 sends the text as written for Telegram's own markdown. none sends plain text." },
|
|
31
|
-
{ path: ["telegram", "chatIds"], label: "Chat ids (comma-separated)", kind: "list", hint: "Numeric ids of the chats or groups to notify (group ids start with -). Send your bot a message, then open https://api.telegram.org/bot<TOKEN>/getUpdates and copy each \"chat\":{\"id\":...}." },
|
|
32
31
|
] },
|
|
33
32
|
{ guide: { summary: "Connect Slack", steps: ["Go to api.slack.com/apps, open your app (or Create New App).", "Easiest: Incoming Webhooks, Add New Webhook to Workspace, pick the channel, copy the URL into Webhook URL.", "Or: OAuth & Permissions, add the chat:write scope, install the app, copy the Bot User OAuth Token (xoxb-), invite the bot to the channel, and list the channel id.", "Switch Enabled to On."], need: [["slack.botToken", "slack.webhookUrl"]] }, title: "💬 Slack", rows: [
|
|
34
33
|
{ path: ["slack", "enabled"], label: "Enabled", kind: "toggle" },
|
|
@@ -92,6 +91,8 @@ function nextStep(group, cfg) {
|
|
|
92
91
|
};
|
|
93
92
|
const label = (dotted) => group.rows.find((r) => r.path.join(".") === dotted).label.replace(/ \(.*\)$/, "");
|
|
94
93
|
const missing = guide.need.find((alts) => !alts.some(filled));
|
|
94
|
+
if (missing?.[0] === "telegram.chatIds")
|
|
95
|
+
return "Next: send your bot any message in Telegram — BN binds that chat to you.";
|
|
95
96
|
if (missing)
|
|
96
97
|
return `Next: fill in ${missing.map(label).join(" or ")}.`;
|
|
97
98
|
return readAt(cfg, [group.rows[0].path[0], "enabled"]) === true ? "Ready. Ask an agent to send a test notify to confirm it arrives." : "Next: switch Enabled to On.";
|
|
@@ -139,6 +140,9 @@ export function saveSetting(pathKeys, value) {
|
|
|
139
140
|
throw new GatewayError(400, `${pathKeys.join(".")} is not a notify setting`);
|
|
140
141
|
const next = settingValue(row, value);
|
|
141
142
|
writeConfigValue(row.path, next);
|
|
143
|
+
// Setup is one act (#4453): a pasted bot token switches Telegram on; the bot binds his chat from his first message.
|
|
144
|
+
if (row.path.join(".") === "telegram.token" && next !== "")
|
|
145
|
+
writeConfigValue(["telegram", "enabled"], true);
|
|
142
146
|
return { path: row.path.join("."), kind: row.kind, value: next };
|
|
143
147
|
}
|
|
144
148
|
function rawConfig() {
|
|
@@ -25,7 +25,7 @@ export function decisionReply(text, repliedTo) {
|
|
|
25
25
|
}
|
|
26
26
|
const KEYCAPS = ["1️⃣", "2️⃣", "3️⃣", "4️⃣"];
|
|
27
27
|
/** The `callback_data` a decision button carries: the digit it picks, the decision itself named by the message the button sits on. */
|
|
28
|
-
const PICK_DATA = "pick:";
|
|
28
|
+
export const PICK_DATA = "pick:";
|
|
29
29
|
/** Telegram's inline keyboard for a single-pick board decision message: one button per numbered option line, in order; undefined for any other message, a pick-several decision included. */
|
|
30
30
|
export function decisionButtons(text) {
|
|
31
31
|
if (decisionLink(text) === undefined)
|