omni-notify-mcp 1.3.487 → 1.3.488
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 +292 -429
- package/dist/daemon.js +6 -6
- package/dist/gateway/chatPage.js +0 -536
- package/dist/gateway/gatewayInstall.js +1 -2
- package/dist/gateway/oauthDoor.js +1 -2
- package/dist/gateway/panel.js +14 -17
- package/dist/gateway/passkeyDoor.js +1 -2
- package/dist/gateway/publicAccess.js +1 -2
- package/dist/gateway/restDoor.js +322 -161
- package/dist/messaging/chatInbound.js +3 -1
- package/dist/messaging/emailInbound.js +3 -7
- package/dist/notify.js +169 -65
- package/dist/shared/webhost/originHandover.js +4 -3
- package/package.json +2 -9
- package/dist/gateway/conversationOwner.js +0 -139
- package/dist/gateway/mcpDoor.js +0 -411
- package/dist/gateway/mcpEvents.js +0 -448
- package/dist/gateway/terminalDoor.js +0 -243
- package/dist/gateway/terminalPage.js +0 -613
- package/dist/shared/agentSession.js +0 -8375
- package/dist/shared/conversation/agentReaper.js +0 -218
- package/dist/shared/conversation/claudeProvider.js +0 -585
- package/dist/shared/conversation/cliSession.js +0 -481
- package/dist/shared/conversation/codexArchive.js +0 -67
- package/dist/shared/conversation/codexProvider.js +0 -240
- package/dist/shared/conversation/conversationOwner.js +0 -43
- package/dist/shared/conversation/conversations.js +0 -1037
- 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 -36
- package/dist/shared/conversation/opencodeProvider.js +0 -375
- package/dist/shared/conversation/ownerContext.js +0 -1
- package/dist/shared/conversation/provider.js +0 -44
- package/dist/shared/conversation/providerProof.js +0 -75
- package/dist/shared/conversation/providers.js +0 -30
- package/dist/shared/conversation/sessionDriver.js +0 -1182
- package/dist/shared/conversation/terminalTransport.js +0 -126
- package/dist/shared/providerOwnership.js +0 -294
- package/dist/shared/webhost/autoSwitch.cjs +0 -2558
- package/dist/shared/workspaceList.js +0 -1
- package/scripts/shared/ownerGuard.cjs +0 -135
- package/scripts/shared/ownerGuard.sh +0 -64
- /package/dist/shared/{conversation/toolLine.js → toolLine.js} +0 -0
package/dist/agentGateway.js
CHANGED
|
@@ -1,467 +1,326 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The agent gateway's chat side (#2052): a Telegram, Slack, Discord
|
|
3
|
-
* the agent
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
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.
|
|
2
|
+
* The agent gateway's chat side (#2052, #4231): a Telegram, Slack, Discord, Teams, ntfy, SMS or email message addressed to
|
|
3
|
+
* the agent goes to AgenticBullseye (AB), the ONE owner of every conversation, through its chat carriage
|
|
4
|
+
* (`POST /v1/chat/<op>`); everything else is left to the inbox exactly as before. BN keeps only the transports: it holds
|
|
5
|
+
* each message's own way back to its thread until AB releases it, and AB sends every part of the turn back through BN's
|
|
6
|
+
* reply door (`POST /v1/chat/reply`), which carries it on that way back — or, with none held, through the platform's
|
|
7
|
+
* registered door, rebuilt from the conversation record alone. Meni's one chat (#4453) is streamed: AB relays each event
|
|
8
|
+
* of its turn whole to BN's relay door (`POST /v1/chat/relay`), and the chat shows it as it happens — the working state,
|
|
9
|
+
* the agent's text and steps read from its own transcript, the answer with its choices as buttons.
|
|
12
10
|
*/
|
|
11
|
+
import { randomUUID } from "crypto";
|
|
13
12
|
import { closeSync, existsSync, openSync, readdirSync, readSync, statSync } from "fs";
|
|
14
13
|
import { homedir } from "os";
|
|
15
14
|
import { join } from "path";
|
|
16
|
-
import { splitForNotify } from "./chunk.js";
|
|
17
15
|
import { choicesOf, StreamedReply } from "./messaging/messenger.js";
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import { clearOpenCodeLogs } from "./gateway/conversationOwner.js";
|
|
21
|
-
import { toolLine } from "./shared/conversation/provider.js";
|
|
22
|
-
import { resendUndelivered, turnEnded } from "./gateway/mcpEvents.js";
|
|
16
|
+
import { GatewayError, readGatewaySettings } from "./notify.js";
|
|
17
|
+
import { toolLine } from "./shared/toolLine.js";
|
|
23
18
|
export const AGENT_PREFIX = "!";
|
|
24
|
-
/** The
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
19
|
+
/** 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. */
|
|
20
|
+
export const SLACK_DM = "slack:dm:";
|
|
21
|
+
const COMMAND_RE = /^\/?(new|stop)(@\w+)?$/i;
|
|
22
|
+
/** The kinds that end a turn: a relay that carried one is done. */
|
|
23
|
+
const FINAL_KINDS = new Set(["answer", "failed", "stopped", "archived", "deleted"]);
|
|
24
|
+
// GATE: the port AgenticBullseye serves on (#4196 Section 9); NOTIFY_AGENT_CLUSTER_URL names another ONLY under the test gate, so a suite drives the real carriage on a cluster it owns, and without the gate no environment can redirect it.
|
|
25
|
+
const CLUSTER_URL = "http://127.0.0.1:7481";
|
|
26
|
+
// GATE: a refused connection to AB is tried five times, waiting 0.5, 1, 2 and 4 s — the bound AB's own call into BN takes (AgenticBullseye wire.ts BN_CALL_TRIES) — then AB is unreachable.
|
|
27
|
+
const CLUSTER_CALL_TRIES = 5;
|
|
28
|
+
const CLUSTER_FIRST_WAIT_MS = 500;
|
|
29
|
+
/** AgenticBullseye's base URL: the one this machine runs, or the stand-in a suite names under the test gate. */
|
|
30
|
+
export function clusterBase() {
|
|
31
|
+
return process.env.NOTIFY_MCP_TEST_ENDPOINTS === "1" && process.env.NOTIFY_AGENT_CLUSTER_URL ? process.env.NOTIFY_AGENT_CLUSTER_URL.replace(/\/+$/, "") : CLUSTER_URL;
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
};
|
|
33
|
+
/** The gateway bearer every call to AB carries; with no `agentGateway.token` in config.json BN has no way into AB and says so. */
|
|
34
|
+
export function clusterToken() {
|
|
35
|
+
const settings = readGatewaySettings();
|
|
36
|
+
// NULL-OK: field settings is OPTIONAL and therefore it is ok to check for NULL
|
|
37
|
+
if (settings === undefined)
|
|
38
|
+
throw new GatewayError(503, "no agentGateway.token is set in config.json, so BN cannot reach AgenticBullseye, which runs every agent conversation — create the gateway token on BN's Public access page");
|
|
39
|
+
return settings.token;
|
|
40
|
+
}
|
|
41
|
+
/** The refusal BN answers when AB cannot be reached at all: a 503 naming the cluster and the cause, never a local stand-in. */
|
|
42
|
+
export function clusterUnreachable(err) {
|
|
43
|
+
// NULL-OK: field cause is OPTIONAL and therefore it is ok to check for NULL
|
|
44
|
+
const cause = err.cause?.code ?? err.code ?? err.message;
|
|
45
|
+
return new GatewayError(503, `AgenticBullseye at ${clusterBase()} could not be reached (${cause}); the agent conversations run there`);
|
|
46
46
|
}
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
parts: (event) => (event.kind === "answer" ? splitForNotify(event.text) : [event.text]),
|
|
55
|
-
stopped: stoppedOfMany,
|
|
56
|
-
renew: endNewest,
|
|
57
|
-
folderOf: (text) => resolveProject(undefined, text),
|
|
58
|
-
forkOf: () => undefined,
|
|
59
|
-
};
|
|
60
|
-
/** 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. */
|
|
61
|
-
const boundOf = (key) => listConversations().find((c) => c.chat === key && c.shelf !== "deleted" && c.forkOf === undefined);
|
|
62
|
-
/** 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. */
|
|
63
|
-
const sidesOf = (key) => listConversations().filter((c) => c.chat === key && c.forkOf !== undefined && c.state !== "closed" && c.shelf === undefined);
|
|
64
|
-
/** A conversation in the middle of a turn, or holding a message stored for its next one. */
|
|
65
|
-
const busy = (c) => turnInFlight(readEvents(c.id), c) !== undefined;
|
|
66
|
-
/** The busy main conversation a message forks beside (#4584): an OpenCode one never forks, since each fork is a new OpenCode process left behind it, so its message queues on it instead. */
|
|
67
|
-
const forkedFrom = (key) => {
|
|
68
|
-
const main = boundOf(key);
|
|
69
|
-
// NULL-OK: field main is OPTIONAL and therefore it is ok to check for NULL
|
|
70
|
-
return main !== undefined && main.provider !== "opencode" && busy(main) ? main : undefined;
|
|
71
|
-
};
|
|
72
|
-
/** The bound chat's main conversation, unless it is busy and forks (#4584): then undefined, so the message runs at once in a new conversation forked from it and never waits behind the reply under way. */
|
|
73
|
-
const freeBound = (key) => (forkedFrom(key) === undefined ? boundOf(key) : undefined);
|
|
74
|
-
/** 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. */
|
|
75
|
-
const ONE = {
|
|
76
|
-
conversationOf: boundOf,
|
|
77
|
-
routeOf: freeBound,
|
|
78
|
-
stopOf: (key) => {
|
|
79
|
-
const working = ONE.allOf(key).filter(busy);
|
|
80
|
-
return working.length > 0 ? working : [boundOf(key)].filter((c) => c !== undefined);
|
|
81
|
-
},
|
|
82
|
-
allOf: (key) => [boundOf(key), ...sidesOf(key)].filter((c) => c !== undefined),
|
|
83
|
-
carries: CHAT.carries,
|
|
84
|
-
parts: CHAT.parts,
|
|
85
|
-
stopped: (stopped) => {
|
|
86
|
-
const forks = stopped.filter((c) => c.forkOf !== undefined).length;
|
|
87
|
-
const parallel = `${forks} parallel conversation${forks === 1 ? "" : "s"}`;
|
|
88
|
-
if (forks === stopped.length)
|
|
89
|
-
return `⏹ Stopped ${parallel}.`;
|
|
90
|
-
return `⏹ Stopped${forks === 0 ? "" : `, with ${parallel}`}. Your next message picks the conversation up where it stopped.`;
|
|
91
|
-
},
|
|
92
|
-
folderOf: () => rootFolder(),
|
|
93
|
-
forkOf: (key) => forkedFrom(key)?.sessionId,
|
|
94
|
-
async renew(key, platform, end, provider) {
|
|
95
|
-
for (const c of ONE.allOf(key)) {
|
|
96
|
-
end(c.id);
|
|
97
|
-
archiveConversation(c.id);
|
|
47
|
+
/** One call to AB under the gateway bearer: AB's JSON answer, or AB's own refusal as a GatewayError carrying its status and words. */
|
|
48
|
+
export async function clusterCall(method, path, body) {
|
|
49
|
+
let answer;
|
|
50
|
+
for (let attempt = 1;; attempt++) {
|
|
51
|
+
try {
|
|
52
|
+
answer = await fetch(`${clusterBase()}${path}`, { method, headers: { authorization: `Bearer ${clusterToken()}`, ...(body === undefined ? {} : { "content-type": "application/json" }) }, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
53
|
+
break;
|
|
98
54
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
55
|
+
catch (err) {
|
|
56
|
+
if (!(err instanceof TypeError))
|
|
57
|
+
throw err;
|
|
58
|
+
// NULL-OK: field cause is OPTIONAL and therefore it is ok to check for NULL
|
|
59
|
+
const refused = err.cause?.code === "ECONNREFUSED";
|
|
60
|
+
// OWNER: ITER/LOOP — a refused connection never reached AB (it is restarting), so it is tried again after a pause; any other failure, or the last try, is AB unreachable, thrown as the 503 naming it.
|
|
61
|
+
if (!refused || attempt === CLUSTER_CALL_TRIES)
|
|
62
|
+
throw clusterUnreachable(err);
|
|
63
|
+
await new Promise((resume) => setTimeout(resume, CLUSTER_FIRST_WAIT_MS << (attempt - 1)));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const text = await answer.text();
|
|
67
|
+
let parsed;
|
|
68
|
+
try {
|
|
69
|
+
parsed = JSON.parse(text);
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
if (!(err instanceof SyntaxError))
|
|
73
|
+
throw err;
|
|
74
|
+
throw new GatewayError(502, `AgenticBullseye answered ${method} ${path} with HTTP ${answer.status} and a body that is not JSON: ${text.slice(0, 200)}`);
|
|
75
|
+
}
|
|
76
|
+
if (!answer.ok) {
|
|
77
|
+
// NULL-OK: field error is OPTIONAL and therefore it is ok to check for NULL
|
|
78
|
+
const said = parsed?.error;
|
|
79
|
+
throw new GatewayError(answer.status, typeof said === "string" ? said : `AgenticBullseye answered ${method} ${path} with HTTP ${answer.status}: ${text.slice(0, 200)}`);
|
|
80
|
+
}
|
|
81
|
+
return parsed;
|
|
125
82
|
}
|
|
126
|
-
/**
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
function carriedOver(from, to) {
|
|
130
|
-
const said = readEvents(from.id).filter((e) => e.role === "user" || e.kind === "answer").slice(-CARRIED_TURNS);
|
|
131
|
-
if (said.length === 0)
|
|
132
|
-
return undefined;
|
|
133
|
-
// A stored message of his ends with his own words (the brief and context ride before them), so the end of each turn is what is kept.
|
|
134
|
-
const lines = said.map((e) => `${e.role === "user" ? "He said" : `${from.provider} answered`}: ${e.text.length > CARRIED_CHARS ? `(cut)… ${e.text.slice(-CARRIED_CHARS)}` : e.text}`);
|
|
135
|
-
return `(BN: he switched this chat from ${from.provider} to ${to}. This conversation goes on from the one ${from.provider} held — its latest turns, read them as this chat's history:\n${lines.join("\n\n")})`;
|
|
83
|
+
/** One chat carriage operation, `POST <AB>/v1/chat/<op>`. */
|
|
84
|
+
function carriage(op, body) {
|
|
85
|
+
return clusterCall("POST", `/v1/chat/${op}`, body);
|
|
136
86
|
}
|
|
137
|
-
const COMMAND_RE = /^\/?(new|stop)(@\w+)?$/i;
|
|
138
|
-
const FINAL_KINDS = new Set(["answer", "failed", "stopped", "archived", "deleted"]);
|
|
139
87
|
export class AgentGateway {
|
|
140
|
-
driver;
|
|
141
88
|
static self;
|
|
142
89
|
static instance() {
|
|
143
|
-
return (AgentGateway.self ??= new AgentGateway(
|
|
90
|
+
return (AgentGateway.self ??= new AgentGateway());
|
|
144
91
|
}
|
|
145
|
-
/** The messages
|
|
146
|
-
|
|
92
|
+
/** The messages whose turn is still sending, by the reply id handed to AB with them. */
|
|
93
|
+
held = new Map();
|
|
147
94
|
doors = new Map();
|
|
148
|
-
recovered = false;
|
|
149
|
-
constructor(driver) {
|
|
150
|
-
this.driver = driver;
|
|
151
|
-
driver.onEvent((event) => this.deliver(event));
|
|
152
|
-
driver.onEvent(turnEnded);
|
|
153
|
-
}
|
|
154
95
|
registerDoor(platform, door) {
|
|
155
96
|
this.doors.set(platform, door);
|
|
156
97
|
}
|
|
157
|
-
/**
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
98
|
+
/**
|
|
99
|
+
* No message's turn is still sending through this process, so the gateway can pass to another one without losing a reply's
|
|
100
|
+
* own thread. A hold whose message AB took while AB answers that no turn of its own is in flight waits on nothing — AB
|
|
101
|
+
* restarted mid-turn, or its release never reached BN — and is let go here; AB unreachable is thrown, as nothing proves then
|
|
102
|
+
* whether its turns still run.
|
|
103
|
+
*/
|
|
104
|
+
async quiesce() {
|
|
105
|
+
if (this.held.size === 0)
|
|
106
|
+
return true;
|
|
107
|
+
if (![...this.held.values()].some((held) => held.taken))
|
|
108
|
+
return false;
|
|
109
|
+
if (!(await carriage("quiet", {})).quiet)
|
|
110
|
+
return false;
|
|
111
|
+
for (const [replyId, held] of [...this.held])
|
|
112
|
+
if (held.taken)
|
|
113
|
+
this.release(replyId);
|
|
114
|
+
return this.held.size === 0;
|
|
167
115
|
}
|
|
168
|
-
/**
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
for (const [turn, sent] of owed) {
|
|
180
|
-
const pending = readEvents(c.id).filter((e) => e.turn === Number(turn) && e.n > sent && e.role !== "user").map((e) => ({ ...e, id: c.id }));
|
|
181
|
-
// OWNER: ITER/LOOP — a thread that cannot be reached is shown on its conversation and the next owed turn is still carried on.
|
|
182
|
-
const answered = this.awaitTurn(c.id, Number(turn), partsRelay((part) => door(c, part), carriageOf(c.platform, c.chat ?? "")), pending);
|
|
183
|
-
// NULL-OK: field forkOf is OPTIONAL and therefore it is ok to check for NULL
|
|
184
|
-
void (c.forkOf === undefined ? answered : answered.finally(() => this.end(c.id))).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)}` }));
|
|
185
|
-
}
|
|
116
|
+
/**
|
|
117
|
+
* What this message asks of the agent, or undefined when it is not addressed to it and belongs to the inbox. Only a
|
|
118
|
+
* message that can be addressed to the agent at all — a `!` message, or a bare `/new` or `/stop` — is asked of AB, so
|
|
119
|
+
* the inbox never waits on the cluster.
|
|
120
|
+
*/
|
|
121
|
+
async classify(key, raw) {
|
|
122
|
+
const text = raw.trim();
|
|
123
|
+
if (!text.startsWith(AGENT_PREFIX) && !COMMAND_RE.test(text))
|
|
124
|
+
return undefined;
|
|
125
|
+
// NULL-OK: field act is OPTIONAL and therefore it is ok to check for NULL
|
|
126
|
+
return (await carriage("classify", { key, raw })).act ?? undefined;
|
|
186
127
|
}
|
|
187
|
-
/**
|
|
188
|
-
|
|
189
|
-
return
|
|
128
|
+
/** A message inside a thread the agent holds is all for the agent: `/new` and `/stop` act, anything else is said, with or without `!`. */
|
|
129
|
+
async classifyInThread(key, raw) {
|
|
130
|
+
return (await carriage("classifyInThread", { key, raw })).act;
|
|
190
131
|
}
|
|
191
|
-
/**
|
|
192
|
-
async
|
|
193
|
-
await
|
|
194
|
-
this.recovered = false;
|
|
132
|
+
/** A message of Meni's one chat is all for its one conversation (#4453); `bare` says the chat cannot type a slash, so a bare "new" or "stop" is a command there. */
|
|
133
|
+
async classifyChat(raw, bare) {
|
|
134
|
+
return (await carriage("classifyChat", { raw, bare })).act;
|
|
195
135
|
}
|
|
196
|
-
/**
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
if (text.startsWith(AGENT_PREFIX)) {
|
|
200
|
-
const body = text.slice(AGENT_PREFIX.length).trim();
|
|
201
|
-
const cmd = body.match(COMMAND_RE);
|
|
202
|
-
if (cmd)
|
|
203
|
-
return { kind: cmd[1].toLowerCase() === "new" ? "new" : "stop" };
|
|
204
|
-
return { kind: "say", text: body };
|
|
205
|
-
}
|
|
206
|
-
const cmd = text.match(COMMAND_RE);
|
|
207
|
-
if (cmd && openConversationOf(key) !== undefined)
|
|
208
|
-
return { kind: cmd[1].toLowerCase() === "new" ? "new" : "stop" };
|
|
209
|
-
return undefined;
|
|
136
|
+
/** The text one message of Meni's one chat is sent as: the bound conversation's brief on its first turn, then what the chat knows around it, then his own words. */
|
|
137
|
+
async turnText(key, text, context) {
|
|
138
|
+
return (await carriage("turnText", { key, text, context })).text;
|
|
210
139
|
}
|
|
211
140
|
/**
|
|
212
|
-
* A message
|
|
213
|
-
*
|
|
214
|
-
* 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.
|
|
141
|
+
* A message's act as AB classifies it; one AB could not classify (unreachable, or refused with its reason) has that
|
|
142
|
+
* reason answered to its chat and reads as "answered", so the poll reads on and the message is never taken for the inbox.
|
|
215
143
|
*/
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
144
|
+
async classified(classify, reply) {
|
|
145
|
+
try {
|
|
146
|
+
return await classify();
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
if (!(err instanceof GatewayError))
|
|
150
|
+
throw err;
|
|
151
|
+
// OWNER: the message is the trigger root — a message AgenticBullseye could not classify is answered to the chat with the reason, never thrown past the poll loop and never taken for the inbox
|
|
152
|
+
await reply(`❌ ${err.message}`);
|
|
153
|
+
return "answered";
|
|
154
|
+
}
|
|
226
155
|
}
|
|
227
156
|
/** Whether this email message was already taken as a turn of some conversation, so a BN restart never answers it twice. */
|
|
228
|
-
emailTaken(messageId) {
|
|
229
|
-
|
|
230
|
-
return listConversations().some((c) => c.email !== undefined && c.email.messageIds.includes(messageId));
|
|
157
|
+
async emailTaken(messageId) {
|
|
158
|
+
return (await carriage("emailTaken", { messageId })).taken;
|
|
231
159
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const c = carriageOf(platform, key).conversationOf(key);
|
|
235
|
-
// NULL-OK: field c is OPTIONAL and therefore it is ok to check for NULL
|
|
236
|
-
if (c === undefined)
|
|
237
|
-
return;
|
|
238
|
-
appendEvent(c.id, { role: "system", kind: "status", text });
|
|
160
|
+
async openSlackThreads() {
|
|
161
|
+
return (await carriage("openSlackThreads", {})).conversations;
|
|
239
162
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return
|
|
243
|
-
if (c.platform !== "slack" || c.state === "closed" || c.slack === undefined || newestOfThread.has(c.chat ?? c.id))
|
|
244
|
-
return false;
|
|
245
|
-
newestOfThread.add(c.chat ?? c.id);
|
|
246
|
-
return true;
|
|
247
|
-
});
|
|
163
|
+
/** Every conversation AB holds, each marked `working` while a turn of it is under way: the Slack Home tab and the Slack thread reader list them. */
|
|
164
|
+
async conversations() {
|
|
165
|
+
return (await carriage("conversations", {})).conversations;
|
|
248
166
|
}
|
|
249
167
|
/** Records how far a Slack thread has been read. */
|
|
250
|
-
markSlackRead(key, readTs) {
|
|
251
|
-
|
|
252
|
-
updateConversation(c.id, { slack: { ...c.slack, readTs } });
|
|
168
|
+
async markSlackRead(key, readTs) {
|
|
169
|
+
await carriage("markSlackRead", { key, readTs });
|
|
253
170
|
}
|
|
254
|
-
/**
|
|
171
|
+
/**
|
|
172
|
+
* Hands one addressed message to AB with its own way back held under a fresh reply id. A plain reply resolves once the
|
|
173
|
+
* turn's final part went out or AB released it; a streamed chat (#4453) is told `streamed`, so AB relays each event of
|
|
174
|
+
* its turns whole, and resolves on AB's release alone. It rejects when a part the thread refused is the reason — AB has
|
|
175
|
+
* already shown that on the conversation. An act AB did not take is answered to the chat with AB's reason.
|
|
176
|
+
*/
|
|
255
177
|
async handle(key, platform, act, out, origin = {}) {
|
|
256
|
-
const
|
|
257
|
-
const
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
178
|
+
const replyId = randomUUID();
|
|
179
|
+
const say = typeof out === "function" ? out : out.say;
|
|
180
|
+
const answered = new Promise((resolve, reject) => {
|
|
181
|
+
const held = {
|
|
182
|
+
out,
|
|
183
|
+
relays: new Map(),
|
|
184
|
+
taken: false,
|
|
185
|
+
settle: () => {
|
|
186
|
+
this.held.delete(replyId);
|
|
187
|
+
for (const relay of held.relays.values())
|
|
188
|
+
relay.close();
|
|
189
|
+
held.relays.clear();
|
|
190
|
+
// NULL-OK: field failed is OPTIONAL and therefore it is ok to check for NULL
|
|
191
|
+
if (held.failed === undefined)
|
|
192
|
+
resolve();
|
|
193
|
+
else
|
|
194
|
+
reject(held.failed);
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
this.held.set(replyId, held);
|
|
198
|
+
});
|
|
199
|
+
try {
|
|
200
|
+
await carriage("handle", { key, platform, act, origin, replyId, streamed: typeof out !== "function" });
|
|
201
|
+
const taken = this.held.get(replyId);
|
|
202
|
+
// NULL-OK: field taken is OPTIONAL and therefore it is ok to check for NULL
|
|
203
|
+
if (taken !== undefined)
|
|
204
|
+
taken.taken = true;
|
|
205
|
+
}
|
|
206
|
+
catch (err) {
|
|
207
|
+
this.release(replyId);
|
|
208
|
+
if (!(err instanceof GatewayError))
|
|
209
|
+
throw err;
|
|
210
|
+
// OWNER: the message is the trigger root — an act AgenticBullseye did not take (unreachable, or refused with its reason) is answered to the chat as that reason, never thrown past the poll loop
|
|
211
|
+
return say(`❌ ${err.message}`);
|
|
212
|
+
}
|
|
213
|
+
return answered;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* AB's reply door: one part of a turn, sent through the reply its message is held with, or — none held (BN restarted, or
|
|
217
|
+
* AB recovered an owed turn with no reply id) — through the platform's registered door, from the conversation's record.
|
|
218
|
+
* A part with no conversation (a line that belongs to none, such as a switch refusal) has only its held reply. A part the
|
|
219
|
+
* thread refuses is thrown as a 502 naming why, which AB records on the conversation. A streamed chat's part is one of
|
|
220
|
+
* its plain lines, and its hold is let go only on AB's release.
|
|
221
|
+
*/
|
|
222
|
+
async carry(replyId, platform, conversation, part, final) {
|
|
223
|
+
// NULL-OK: field replyId is OPTIONAL and therefore it is ok to check for NULL
|
|
224
|
+
const held = replyId === null ? undefined : this.held.get(replyId);
|
|
225
|
+
// NULL-OK: field held is OPTIONAL and therefore it is ok to check for NULL
|
|
226
|
+
if (held === undefined) {
|
|
227
|
+
// NULL-OK: field conversation is OPTIONAL and therefore it is ok to check for NULL
|
|
228
|
+
if (conversation === null)
|
|
229
|
+
throw new GatewayError(404, `no ${platform} reply is held under ${replyId} and the part names no conversation, so it has no way to a thread`);
|
|
230
|
+
const door = this.doors.get(platform);
|
|
231
|
+
// NULL-OK: field door is OPTIONAL and therefore it is ok to check for NULL
|
|
232
|
+
if (door === undefined)
|
|
233
|
+
throw new GatewayError(404, `no ${platform} door is registered in this BN process, so the part has no way to its thread`);
|
|
234
|
+
return sent(platform, () => door(conversation, part));
|
|
274
235
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
catch (err) {
|
|
280
|
-
if (!(err instanceof GatewayError))
|
|
281
|
-
throw err;
|
|
282
|
-
// OWNER: the message is the trigger root — a fresh conversation the gateway refused (its CLI does not answer, no subscription has room) is answered to the chat with the reason, never silence.
|
|
283
|
-
return reply(`❌ NOT STARTED — ${err.message}`);
|
|
284
|
-
}
|
|
236
|
+
const out = held.out;
|
|
237
|
+
const streamed = typeof out !== "function";
|
|
238
|
+
try {
|
|
239
|
+
await sent(platform, () => (streamed ? out.say(part) : out(part)));
|
|
285
240
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
const replaced = open !== undefined && act.provider !== undefined && open.provider !== act.provider;
|
|
289
|
-
// #4671 — a provider switch keeps the thread: the conversation it replaces hands its latest turns to the new one, so nothing said is lost.
|
|
290
|
-
const carried = replaced ? carriedOver(open, act.provider) : undefined;
|
|
291
|
-
// The conversations it replaces end only once the new one is made: a provider that refuses leaves the chat on the one it had, every turn kept.
|
|
292
|
-
const retire = () => {
|
|
293
|
-
if (!replaced)
|
|
294
|
-
return;
|
|
295
|
-
for (const c of carriage.allOf(key)) {
|
|
296
|
-
this.end(c.id);
|
|
297
|
-
archiveConversation(c.id);
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
// The one chat's brief rides the first turn of a conversation; the replacing one has had none, so it is given it with the turns carried over.
|
|
301
|
-
// NULL-OK: field carried is OPTIONAL and therefore it is ok to check for NULL
|
|
302
|
-
const said = carried === undefined ? act.text : [...(carriage === ONE ? [chatBrief(key.startsWith(SLACK_DM) ? "Slack" : "Telegram")] : []), carried, act.text].join("\n\n");
|
|
303
|
-
const switched = (id) => (replaced ? [this.told(id, 1, `🔀 Switched from ${open.provider} to ${act.provider} — the conversation goes on there, its last turns carried over.`)] : []);
|
|
304
|
-
const target = replaced ? undefined : carriage.routeOf(key, act.text);
|
|
305
|
-
const forkOf = replaced ? undefined : carriage.forkOf(key);
|
|
306
|
-
// NULL-OK: field target is OPTIONAL and therefore it is ok to check for NULL
|
|
307
|
-
if (target === undefined) {
|
|
308
|
-
let project;
|
|
309
|
-
try {
|
|
310
|
-
project = carriage.folderOf(act.text);
|
|
311
|
-
}
|
|
312
|
-
catch (err) {
|
|
313
|
-
// 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
|
|
314
|
-
if (err instanceof GatewayError)
|
|
315
|
-
return reply(err.message);
|
|
241
|
+
catch (err) {
|
|
242
|
+
if (!(err instanceof GatewayError))
|
|
316
243
|
throw err;
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
244
|
+
held.failed = err;
|
|
245
|
+
throw err;
|
|
246
|
+
}
|
|
247
|
+
finally {
|
|
248
|
+
if (final && !streamed)
|
|
249
|
+
held.settle();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* AB's relay door (#4453): one event of a streamed turn shown on the chat its message is held with, through that turn's
|
|
254
|
+
* relay (made on its first event), or `close` — the turn ended without a last event of its own. An event the chat
|
|
255
|
+
* refuses is thrown as a 502 naming why; a relay that carried the turn's last event is done.
|
|
256
|
+
*/
|
|
257
|
+
async relay(replyId, turn, event, close) {
|
|
258
|
+
const held = this.held.get(replyId);
|
|
259
|
+
// NULL-OK: field held is OPTIONAL and therefore it is ok to check for NULL
|
|
260
|
+
if (held === undefined)
|
|
261
|
+
throw new GatewayError(404, `no streamed chat is held under ${replyId} (its turn was released, or BN restarted since), so turn ${turn.id} #${turn.n} has no chat to show it`);
|
|
262
|
+
const out = held.out;
|
|
263
|
+
if (typeof out === "function")
|
|
264
|
+
throw new GatewayError(409, `the reply held under ${replyId} is not a streamed chat; its parts go to /v1/chat/reply`);
|
|
265
|
+
const name = `${turn.id}:${turn.n}`;
|
|
266
|
+
if (close) {
|
|
267
|
+
// NULL-OK: field relay is OPTIONAL and therefore it is ok to check for NULL
|
|
268
|
+
held.relays.get(name)?.close();
|
|
269
|
+
held.relays.delete(name);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
// NULL-OK: field event is OPTIONAL and therefore it is ok to check for NULL
|
|
273
|
+
if (event === undefined)
|
|
274
|
+
throw new GatewayError(400, "a relay carries an event of the turn, or close: true");
|
|
275
|
+
const relay = held.relays.get(name) ?? out.turn(turn);
|
|
276
|
+
held.relays.set(name, relay);
|
|
277
|
+
try {
|
|
278
|
+
await relay.event(event, turn);
|
|
279
|
+
}
|
|
280
|
+
catch (err) {
|
|
281
|
+
if (!deliveryFailure(err))
|
|
282
|
+
throw err;
|
|
283
|
+
held.failed = new GatewayError(502, `the chat did not take the ${event.kind ?? "status"} line of turn ${turn.id} #${turn.n}: ${err.message}`);
|
|
284
|
+
throw held.failed;
|
|
285
|
+
}
|
|
286
|
+
finally {
|
|
287
|
+
if (FINAL_KINDS.has(event.kind))
|
|
288
|
+
held.relays.delete(name);
|
|
350
289
|
}
|
|
351
|
-
// NULL-OK: field email is OPTIONAL and therefore it is ok to check for NULL
|
|
352
|
-
if (origin.email !== undefined)
|
|
353
|
-
updateConversation(target.id, { email: { ...target.email, messageIds: [...target.email.messageIds, ...origin.email.messageIds] } });
|
|
354
|
-
const sent = sendToConversation(target.id, act.text, monotonicMs() - arrived);
|
|
355
|
-
// NULL-OK: field notice is OPTIONAL and therefore it is ok to check for NULL
|
|
356
|
-
const notice = queueNoticeOf(target.id, sent.n);
|
|
357
|
-
// NULL-OK: field open is OPTIONAL and therefore it is ok to check for NULL
|
|
358
|
-
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.`)] : [];
|
|
359
|
-
return this.awaitTurn(target.id, sent.n, relayOf(target.id, sent.n, notice !== undefined), [...told, ...(notice === undefined ? [] : [{ ...notice, id: target.id }])]);
|
|
360
290
|
}
|
|
361
|
-
/**
|
|
362
|
-
|
|
363
|
-
|
|
291
|
+
/** AB's release once a handled message's turn has settled: its held reply is let go. One no longer held (let go on its final part, or BN restarted since) has nothing left to let go. */
|
|
292
|
+
release(replyId) {
|
|
293
|
+
// NULL-OK: field held is OPTIONAL and therefore it is ok to check for NULL
|
|
294
|
+
this.held.get(replyId)?.settle();
|
|
364
295
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
this.turns.delete(id);
|
|
369
|
-
this.driver.stop(id);
|
|
370
|
-
editOwed(id, (o) => { for (const turn of Object.keys(o))
|
|
371
|
-
delete o[turn]; });
|
|
296
|
+
/** Asks AB to re-drive every turn it still owes a thread, once this process holds the doors those turns go out through. */
|
|
297
|
+
async recover() {
|
|
298
|
+
await carriage("recover", {});
|
|
372
299
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
const turns = this.turns.get(id) ?? new Map();
|
|
379
|
-
turns.set(n, { push: (event) => { pending.push(event); wake(); }, close: () => { closed = true; wake(); } });
|
|
380
|
-
this.turns.set(id, turns);
|
|
381
|
-
return (async () => {
|
|
382
|
-
for (;;) {
|
|
383
|
-
if (pending.length === 0) {
|
|
384
|
-
if (closed)
|
|
385
|
-
return relay.close();
|
|
386
|
-
await new Promise((resolve) => { wake = resolve; });
|
|
387
|
-
continue;
|
|
388
|
-
}
|
|
389
|
-
const event = pending.shift();
|
|
390
|
-
await relay.event(event).catch((err) => {
|
|
391
|
-
relay.close();
|
|
392
|
-
throw err;
|
|
393
|
-
});
|
|
394
|
-
if (LIVE_KINDS.has(event.kind))
|
|
395
|
-
continue;
|
|
396
|
-
const final = FINAL_KINDS.has(event.kind ?? "");
|
|
397
|
-
if (!closed)
|
|
398
|
-
editOwed(id, (o) => { if (final)
|
|
399
|
-
delete o[n];
|
|
400
|
-
else
|
|
401
|
-
o[n] = event.n; });
|
|
402
|
-
if (final) {
|
|
403
|
-
// NULL-OK: field turns is OPTIONAL and therefore it is ok to check for NULL
|
|
404
|
-
this.turns.get(id)?.delete(n);
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
})();
|
|
300
|
+
}
|
|
301
|
+
/** One part handed to its thread; a refusal of the thread's own platform is thrown as the 502 the reply door answers. */
|
|
302
|
+
async function sent(platform, send) {
|
|
303
|
+
try {
|
|
304
|
+
await send();
|
|
409
305
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
for (const waiting of turns.values())
|
|
415
|
-
waiting.push(event);
|
|
416
|
-
this.turns.delete(event.id);
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
// NULL-OK: field turn is OPTIONAL and therefore it is ok to check for NULL
|
|
420
|
-
const turn = event.turn === undefined ? undefined : turns?.get(event.turn);
|
|
421
|
-
// NULL-OK: field turn is OPTIONAL and therefore it is ok to check for NULL
|
|
422
|
-
if (turn === undefined)
|
|
423
|
-
return;
|
|
424
|
-
turn.push(event);
|
|
425
|
-
if (FINAL_KINDS.has(event.kind ?? ""))
|
|
426
|
-
turns.delete(event.turn);
|
|
306
|
+
catch (err) {
|
|
307
|
+
if (!deliveryFailure(err))
|
|
308
|
+
throw err;
|
|
309
|
+
throw new GatewayError(502, `the ${platform} thread did not take the part: ${err.message}`);
|
|
427
310
|
}
|
|
428
311
|
}
|
|
429
|
-
/** What each chat can show (#4585): Telegram's phone chat takes inline marks only; Slack renders the reply as Block Kit, and each of its threads is one piece of work. */
|
|
430
|
-
const SHOWS = {
|
|
431
|
-
Telegram: "The chat shows bold, italic, code, links, bullets and emojis, never tables; keep replies short.",
|
|
432
|
-
Slack: "The chat shows # headings as headers, --- as dividers, code blocks, > quotes, bold, italic, links, bullets and emojis, and a markdown table as a card per row, its columns in two-column fields; each Slack thread is one piece of work, so answer the work of the thread you are in. Keep replies short.",
|
|
433
|
-
};
|
|
434
312
|
/**
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
*
|
|
313
|
+
* Whether a send failed in the world — the platform refused it, could not be reached, timed out, or its channel is off —
|
|
314
|
+
* rather than in our own code: a reference, range or syntax fault, or a TypeError the runtime raised with no cause (a call
|
|
315
|
+
* on something that is not a function), is a bug and is thrown as itself, never recorded on the conversation as a refusal.
|
|
316
|
+
* A network TypeError from fetch always carries its cause.
|
|
438
317
|
*/
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
/** 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. */
|
|
446
|
-
function answeredBeside(key, main) {
|
|
447
|
-
const heard = readEvents(main.id).filter((e) => e.role === "user").at(-1)?.ts ?? main.createdAt;
|
|
448
|
-
const answers = listConversations()
|
|
449
|
-
.filter((c) => c.chat === key && c.forkOf !== undefined && c.shelf !== "deleted")
|
|
450
|
-
.flatMap((c) => readEvents(c.id).filter((e) => e.kind === "answer" && e.ts > heard))
|
|
451
|
-
.sort((x, y) => x.ts.localeCompare(y.ts));
|
|
452
|
-
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")})`];
|
|
453
|
-
}
|
|
454
|
-
/** 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. */
|
|
455
|
-
export function chatTurnText(key, text, context) {
|
|
456
|
-
const brief = chatBrief(key.startsWith(SLACK_DM) ? "Slack" : "Telegram");
|
|
457
|
-
const bound = boundOf(key);
|
|
458
|
-
// NULL-OK: field bound is OPTIONAL and therefore it is ok to check for NULL
|
|
459
|
-
if (bound === undefined)
|
|
460
|
-
return [brief, ...context, text].join("\n\n");
|
|
461
|
-
// 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.
|
|
462
|
-
const beside = forkedFrom(key) !== undefined;
|
|
463
|
-
const first = !readEvents(bound.id).some((e) => e.role === "user") || (beside && !bound.started);
|
|
464
|
-
return [...(first ? [brief] : []), ...answeredBeside(key, bound), ...(beside ? [BESIDE] : []), ...context, text].join("\n\n");
|
|
318
|
+
function deliveryFailure(err) {
|
|
319
|
+
if (!(err instanceof Error))
|
|
320
|
+
return false;
|
|
321
|
+
if (err instanceof ReferenceError || err instanceof RangeError || err instanceof SyntaxError)
|
|
322
|
+
return false;
|
|
323
|
+
return !(err instanceof TypeError) || err.cause !== undefined;
|
|
465
324
|
}
|
|
466
325
|
/** The Claude CLI's own folder of session transcripts. */
|
|
467
326
|
function claudeProjects() {
|
|
@@ -497,18 +356,18 @@ class TranscriptTail {
|
|
|
497
356
|
timer;
|
|
498
357
|
/** Why the transcript could not be read on the last tick, named once until a read succeeds again. */
|
|
499
358
|
unread;
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (
|
|
359
|
+
/** The tail of the relayed turn's own CLI session: a Claude turn's transcript, read from where it stands now; any other CLI writes none here. */
|
|
360
|
+
static of(turn, reply, log) {
|
|
361
|
+
if (turn.provider !== "claude")
|
|
503
362
|
return undefined;
|
|
504
363
|
try {
|
|
505
|
-
return new TranscriptTail(
|
|
364
|
+
return new TranscriptTail(turn.sessionId, reply, log);
|
|
506
365
|
}
|
|
507
366
|
catch (err) {
|
|
508
367
|
if (!fsFailure(err))
|
|
509
368
|
throw err;
|
|
510
369
|
// 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.
|
|
511
|
-
log(`transcript ${
|
|
370
|
+
log(`transcript ${turn.sessionId} could not be read (${err.message}) — this turn's preview shows its working state only`);
|
|
512
371
|
return undefined;
|
|
513
372
|
}
|
|
514
373
|
}
|
|
@@ -598,14 +457,16 @@ function tookLine(event) {
|
|
|
598
457
|
if (ms === undefined)
|
|
599
458
|
return "";
|
|
600
459
|
const seconds = Math.max(1, Math.round(ms / 1000));
|
|
601
|
-
return
|
|
460
|
+
return `
|
|
461
|
+
|
|
462
|
+
⏱ ${seconds < 60 ? `${seconds} s` : `${Math.floor(seconds / 60)} min ${seconds % 60} s`}`;
|
|
602
463
|
}
|
|
603
464
|
/** The bound chat's turns as they happen (#4453): the start line at once, each text the agent finishes as a message of its own, the answer with its choices as buttons. */
|
|
604
465
|
export function streamedChat(messenger, log) {
|
|
605
466
|
return {
|
|
606
467
|
say: async (text) => { await messenger.send(text); },
|
|
607
|
-
turn(
|
|
608
|
-
const chat = parallel ? messenger.alongside() : messenger;
|
|
468
|
+
turn(turn) {
|
|
469
|
+
const chat = turn.parallel === true ? messenger.alongside() : messenger;
|
|
609
470
|
const reply = new StreamedReply(chat, log);
|
|
610
471
|
let tail;
|
|
611
472
|
const ended = () => {
|
|
@@ -618,12 +479,14 @@ export function streamedChat(messenger, log) {
|
|
|
618
479
|
};
|
|
619
480
|
return {
|
|
620
481
|
close,
|
|
621
|
-
async event(event) {
|
|
482
|
+
async event(event, now) {
|
|
622
483
|
switch (event.kind) {
|
|
623
484
|
case "start":
|
|
624
485
|
// The start line stays in the store for the web page; the chat gets no message before the first real output, so nothing sits between his message and his next one.
|
|
625
486
|
// NULL-OK: field tail is OPTIONAL and therefore it is ok to check for NULL
|
|
626
|
-
|
|
487
|
+
// The session the turn runs on is read off the event that starts it: a warm spare or a rotated session gives the record its id only then.
|
|
488
|
+
// NULL-OK: field now is OPTIONAL and therefore it is ok to check for NULL
|
|
489
|
+
tail ??= TranscriptTail.of(now ?? turn, reply, log);
|
|
627
490
|
return;
|
|
628
491
|
case "said":
|
|
629
492
|
reply.said(event.text);
|
|
@@ -639,13 +502,13 @@ export function streamedChat(messenger, log) {
|
|
|
639
502
|
return;
|
|
640
503
|
case "asking": {
|
|
641
504
|
const { text, choices } = choicesOf(event.text);
|
|
642
|
-
reply.ask(text, choices.map((label, i) => ({ label, data: `b|${id.slice(0, 8)}|${i}` })));
|
|
505
|
+
reply.ask(text, choices.map((label, i) => ({ label, data: `b|${turn.id.slice(0, 8)}|${i}` })));
|
|
643
506
|
return;
|
|
644
507
|
}
|
|
645
508
|
case "answer": {
|
|
646
509
|
ended();
|
|
647
510
|
const { text, choices } = choicesOf(event.text);
|
|
648
|
-
return reply.finish(`${text}${tookLine(event)}`, choices.map((label, i) => ({ label, data: `b|${id.slice(0, 8)}|${i}` })));
|
|
511
|
+
return reply.finish(`${text}${tookLine(event)}`, choices.map((label, i) => ({ label, data: `b|${turn.id.slice(0, 8)}|${i}` })));
|
|
649
512
|
}
|
|
650
513
|
case "failed":
|
|
651
514
|
ended();
|