pi-roundtable 0.51.2 → 0.52.1
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/CHANGELOG.md +22 -0
- package/docs/plugins.md +43 -0
- package/package.json +1 -1
- package/src/core/builtin/discord.ts +46 -1
- package/src/core/config/adapters.ts +19 -0
- package/src/core/contract/channels.ts +6 -0
- package/src/core/contract/surface.ts +7 -0
- package/src/core/discord/catch-up-gate.ts +207 -0
- package/src/core/discord/catch-up-journal.ts +134 -0
- package/src/core/discord/catch-up-reader.ts +121 -0
- package/src/core/discord/catch-up-service.ts +142 -0
- package/src/core/discord/catch-up.ts +385 -0
- package/src/core/discord/discord-surface.ts +18 -2
- package/src/core/discord/message-changes.ts +33 -4
- package/src/core/host/define-roundtable.ts +1 -0
- package/src/core/host/host.ts +31 -0
- package/src/core/i18n/conversation.ts +2 -0
- package/src/core/routing/channel-router.ts +27 -8
- package/src/core/routing/queued-messages.ts +33 -0
|
@@ -5,23 +5,52 @@ import { channelKey } from "../contract/surface.ts";
|
|
|
5
5
|
import type { Logger } from "../kernel/log.ts";
|
|
6
6
|
import { toInbound } from "./inbound-message.ts";
|
|
7
7
|
|
|
8
|
-
/**
|
|
8
|
+
/** Decides whether a message the gateway delivered is handed on: false keeps it from routing. */
|
|
9
|
+
export type InboundGate = (
|
|
10
|
+
message: InboundMessage,
|
|
11
|
+
botId: string,
|
|
12
|
+
) => Promise<boolean>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hands every message the client sees to `deliver` as the neutral message, in the order each
|
|
16
|
+
* channel's messages came, however long reading one takes; the bot's own are left out, and so is
|
|
17
|
+
* one the gate refuses.
|
|
18
|
+
*/
|
|
9
19
|
export function listenForMessages(
|
|
10
20
|
client: Client,
|
|
11
21
|
deliver: (message: InboundMessage) => void,
|
|
12
22
|
logger: Logger,
|
|
23
|
+
gate?: InboundGate,
|
|
13
24
|
): void {
|
|
25
|
+
const delivering = new Map<string, Promise<void>>();
|
|
14
26
|
client.on(Events.MessageCreate, (message) => {
|
|
15
|
-
|
|
16
|
-
|
|
27
|
+
const botId = client.user?.id;
|
|
28
|
+
const read = toInbound(message, {
|
|
29
|
+
botId,
|
|
17
30
|
applicationId: client.application?.id,
|
|
18
31
|
})
|
|
32
|
+
.then(async (inbound) => {
|
|
33
|
+
if (!inbound || !botId) return undefined;
|
|
34
|
+
return !gate || (await gate(inbound, botId)) ? inbound : undefined;
|
|
35
|
+
})
|
|
36
|
+
// Handled here, not when its turn comes: a read that fails while an earlier one waits would otherwise go unhandled.
|
|
37
|
+
.catch((error: unknown) => {
|
|
38
|
+
logger.error({ err: error }, "inbound message could not be read");
|
|
39
|
+
return undefined;
|
|
40
|
+
});
|
|
41
|
+
const { channelId } = message;
|
|
42
|
+
const after = (delivering.get(channelId) ?? Promise.resolve())
|
|
43
|
+
.then(() => read)
|
|
19
44
|
.then((inbound) => {
|
|
20
45
|
if (inbound) deliver(inbound);
|
|
21
46
|
})
|
|
22
47
|
.catch((error: unknown) =>
|
|
23
|
-
logger.error({ err: error }, "inbound message could not be
|
|
48
|
+
logger.error({ err: error }, "inbound message could not be delivered"),
|
|
24
49
|
);
|
|
50
|
+
delivering.set(channelId, after);
|
|
51
|
+
void after.then(() => {
|
|
52
|
+
if (delivering.get(channelId) === after) delivering.delete(channelId);
|
|
53
|
+
});
|
|
25
54
|
});
|
|
26
55
|
}
|
|
27
56
|
|
|
@@ -176,6 +176,7 @@ function discordAssembly(
|
|
|
176
176
|
...(discord.attachmentRead
|
|
177
177
|
? { attachmentRead: discord.attachmentRead }
|
|
178
178
|
: {}),
|
|
179
|
+
...(discord.catchUp ? { catchUp: discord.catchUp } : {}),
|
|
179
180
|
}),
|
|
180
181
|
admin: discord.admin ? [discordAdminPlugin({ owner })] : [],
|
|
181
182
|
};
|
package/src/core/host/host.ts
CHANGED
|
@@ -110,6 +110,23 @@ export interface RoundtableOptions {
|
|
|
110
110
|
/** The host running in this process; the text catalog, time zone and environment are process-wide. */
|
|
111
111
|
let running: Roundtable | undefined;
|
|
112
112
|
|
|
113
|
+
/** How long the messages still waiting after the drain are given to be refused, before the teardown. */
|
|
114
|
+
const REFUSE_WAITING_MS = 15_000;
|
|
115
|
+
|
|
116
|
+
/** `work`, or a rejection once `ms` pass; the timer never keeps the process alive. */
|
|
117
|
+
async function withinMs<T>(ms: number, work: Promise<T> | T): Promise<T> {
|
|
118
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
119
|
+
const late = new Promise<never>((_, reject) => {
|
|
120
|
+
timer = setTimeout(() => reject(new Error(`not done in ${ms} ms`)), ms);
|
|
121
|
+
timer.unref?.();
|
|
122
|
+
});
|
|
123
|
+
try {
|
|
124
|
+
return await Promise.race([work, late]);
|
|
125
|
+
} finally {
|
|
126
|
+
clearTimeout(timer);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
113
130
|
/**
|
|
114
131
|
* The process around the plugins: it sets them all up, links what they add (session parts,
|
|
115
132
|
* HTTP routes) and runs the preflight, then starts their services in order and the
|
|
@@ -305,6 +322,20 @@ export class Roundtable {
|
|
|
305
322
|
});
|
|
306
323
|
// Only what ran before the drain ended finishes; nothing delivered later starts.
|
|
307
324
|
this.#link.queue.seal();
|
|
325
|
+
// A message still waiting behind a turn never starts: its channel is told while the services run.
|
|
326
|
+
// Counted as it goes, and told to start no more once the limit passes.
|
|
327
|
+
const tally = { refused: 0, failed: 0, stopped: false };
|
|
328
|
+
const told = await attempt(() =>
|
|
329
|
+
withinMs(REFUSE_WAITING_MS, this.#link.router?.refuseWaiting(tally)),
|
|
330
|
+
);
|
|
331
|
+
tally.stopped = true;
|
|
332
|
+
if (!told.ok || tally.failed > 0)
|
|
333
|
+
logger.error(
|
|
334
|
+
{ err: told.ok ? undefined : told.error, ...tally },
|
|
335
|
+
"not every waiting message was refused; the rest are not answered",
|
|
336
|
+
);
|
|
337
|
+
else if (tally.refused > 0)
|
|
338
|
+
logger.info({ refused: tally.refused }, "waiting messages refused");
|
|
308
339
|
// A turn that started during the abort grace, a report's, is stopped too, so the teardown does not wait for it.
|
|
309
340
|
if (left.length > 0) stopQueuedTurns();
|
|
310
341
|
if (left.length > 0) {
|
|
@@ -14,6 +14,7 @@ export function conversationEn(ctx: CatalogContext) {
|
|
|
14
14
|
"Sorry, this reply did not go through. Please try again later.",
|
|
15
15
|
stoppedNotice: "-# Stopped.",
|
|
16
16
|
restartingNotice: `-# ${ctx.assistant} is restarting and takes no new messages right now; please send this again in a few minutes.`,
|
|
17
|
+
restartingAnswerLater: `-# ${ctx.assistant} is restarting; it will answer this once it is back, so there is no need to send it again.`,
|
|
17
18
|
roundLimitNotice:
|
|
18
19
|
"-# This round already has 8 replies, so it stops here; call us again if you need more.",
|
|
19
20
|
roundStoppedNotice:
|
|
@@ -55,6 +56,7 @@ export function conversationZhTW(
|
|
|
55
56
|
failureNotice: "抱歉,這次沒能完成回覆,請稍後再試一次。",
|
|
56
57
|
stoppedNotice: "-# 已停止。",
|
|
57
58
|
restartingNotice: `-# ${ctx.assistant} 正在重新啟動,暫時不接新訊息;請過幾分鐘再傳一次。`,
|
|
59
|
+
restartingAnswerLater: `-# ${ctx.assistant} 正在重新啟動,回來後會回覆這則訊息,不用再傳一次。`,
|
|
58
60
|
roundLimitNotice:
|
|
59
61
|
"-# 這一輪已經有 8 則回覆,先停在這裡;需要的話請再叫我們。",
|
|
60
62
|
roundStoppedNotice: "-# 本輪已停止,還沒輪到的成員不再發言。",
|
|
@@ -27,7 +27,11 @@ import { tellDeleted, tellEdited } from "./claim-events.ts";
|
|
|
27
27
|
import { ForwardJoin } from "./forward-join.ts";
|
|
28
28
|
import type { MessageObservers } from "./message-observers.ts";
|
|
29
29
|
import { type PeerBots, peerFacts } from "./peer-bots.ts";
|
|
30
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
type QueuedEntry,
|
|
32
|
+
QueuedMessages,
|
|
33
|
+
type RefuseTally,
|
|
34
|
+
} from "./queued-messages.ts";
|
|
31
35
|
|
|
32
36
|
export interface ChannelRouterOptions {
|
|
33
37
|
/** Claims in registration order; the router orders them by priority. */
|
|
@@ -248,16 +252,23 @@ export class ChannelRouter implements ConversationPort {
|
|
|
248
252
|
reason: new HostStoppingError().message,
|
|
249
253
|
});
|
|
250
254
|
logger.info({ channel }, "message not run: the host is shutting down");
|
|
251
|
-
if (message.authorIsBot || message.integration
|
|
252
|
-
return;
|
|
253
|
-
this.#told.add(channel);
|
|
255
|
+
if (message.authorIsBot || message.integration) return;
|
|
254
256
|
try {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
});
|
|
257
|
+
// The surface may take it, and tell its channel itself.
|
|
258
|
+
if (await surfaces?.of(channel)?.refuseStopping?.(message)) return;
|
|
258
259
|
} catch (error) {
|
|
259
|
-
logger.warn(
|
|
260
|
+
logger.warn(
|
|
261
|
+
{ channel, err: error },
|
|
262
|
+
"surface did not take a refused message",
|
|
263
|
+
);
|
|
260
264
|
}
|
|
265
|
+
if (this.#told.has(channel)) return;
|
|
266
|
+
this.#told.add(channel);
|
|
267
|
+
await surfaces
|
|
268
|
+
?.sendReply?.(channel, { chunks: [messages().restartingNotice] })
|
|
269
|
+
.catch((error: unknown) =>
|
|
270
|
+
logger.warn({ channel, err: error }, "restart notice not posted"),
|
|
271
|
+
);
|
|
261
272
|
}
|
|
262
273
|
|
|
263
274
|
/**
|
|
@@ -313,6 +324,14 @@ export class ChannelRouter implements ConversationPort {
|
|
|
313
324
|
return { done };
|
|
314
325
|
}
|
|
315
326
|
|
|
327
|
+
/** Refuses each message still waiting behind a turn after the drain, counting in `tally`. */
|
|
328
|
+
refuseWaiting(tally: RefuseTally): Promise<void> {
|
|
329
|
+
return this.#queued.refuseAll(
|
|
330
|
+
(e) => this.#refuse(e.message, e.admission),
|
|
331
|
+
tally,
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
316
335
|
queued(channel: ChannelKey): readonly QueuedMessage[] {
|
|
317
336
|
return this.#queued.list(channel);
|
|
318
337
|
}
|
|
@@ -29,6 +29,13 @@ export interface QueuedEntry {
|
|
|
29
29
|
* can only reach a message that has not started. Everything is synchronous, so no cancel or edit
|
|
30
30
|
* races the start of a turn: whichever runs first wins.
|
|
31
31
|
*/
|
|
32
|
+
/** How a refusal of every waiting message went, filled in as it goes; set `stopped` to start no more. */
|
|
33
|
+
export interface RefuseTally {
|
|
34
|
+
refused: number;
|
|
35
|
+
failed: number;
|
|
36
|
+
stopped: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
32
39
|
export class QueuedMessages {
|
|
33
40
|
readonly #entries = new Map<ChannelKey, Map<string, QueuedEntry>>();
|
|
34
41
|
readonly #logger: Logger;
|
|
@@ -131,6 +138,32 @@ export class QueuedMessages {
|
|
|
131
138
|
return "done";
|
|
132
139
|
}
|
|
133
140
|
|
|
141
|
+
/** Takes every waiting entry off the list and refuses it, the channels in parallel, each in order, until `tally.stopped`. */
|
|
142
|
+
async refuseAll(
|
|
143
|
+
refuse: (entry: QueuedEntry) => Promise<void>,
|
|
144
|
+
tally: RefuseTally,
|
|
145
|
+
): Promise<void> {
|
|
146
|
+
await Promise.all(
|
|
147
|
+
[...this.#entries.values()].map(async (entries) => {
|
|
148
|
+
for (const entry of [...entries.values()]) {
|
|
149
|
+
if (tally.stopped) return;
|
|
150
|
+
if (!this.remove(entry)) continue;
|
|
151
|
+
this.unmark(entry);
|
|
152
|
+
try {
|
|
153
|
+
await refuse(entry);
|
|
154
|
+
tally.refused += 1;
|
|
155
|
+
} catch (error) {
|
|
156
|
+
tally.failed += 1;
|
|
157
|
+
this.#logger.warn(
|
|
158
|
+
{ channel: entry.message.channel, err: error },
|
|
159
|
+
"a waiting message could not be refused",
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}),
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
134
167
|
/** Removes the waiting mark; a failure is logged, since the message may be gone. */
|
|
135
168
|
unmark(entry: QueuedEntry): void {
|
|
136
169
|
const { channel } = entry.message;
|