pi-roundtable 0.52.1 → 0.52.3
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 +20 -1
- package/README.md +1 -0
- package/docs/plugins.md +9 -5
- package/package.json +1 -1
- package/src/core/agents/agent-messages.ts +79 -28
- package/src/core/agents/agent-outbox.ts +6 -3
- package/src/core/agents/team-editing.ts +8 -6
- package/src/core/agents/team-turns.ts +4 -2
- package/src/core/config/limits.ts +20 -1
- package/src/core/contract/channels.ts +2 -1
- package/src/core/discord/catch-up-gate.ts +24 -11
- package/src/core/discord/catch-up-journal.ts +4 -4
- package/src/core/host/define-roundtable.ts +5 -1
- package/src/core/host/drain.ts +1 -3
- package/src/core/host/host-registration.ts +2 -0
- package/src/core/host/host.ts +34 -13
- package/src/core/routing/admit-message.ts +75 -0
- package/src/core/routing/channel-queue.ts +11 -2
- package/src/core/routing/channel-router.ts +79 -93
- package/src/core/routing/conversation-turns.ts +103 -93
- package/src/core/routing/queued-messages.ts +27 -16
- package/src/core/routing/settle-turn.ts +38 -5
- package/src/core/routing/stopping-notice.ts +39 -0
- package/src/core/routing/turn-records.ts +341 -0
|
@@ -22,7 +22,8 @@ import type { Logger } from "../kernel/log.ts";
|
|
|
22
22
|
import type { EventSink } from "../plugin-api/plugin.ts";
|
|
23
23
|
import { isQuiet, replyBody } from "../presentation/reply-body.ts";
|
|
24
24
|
import { withReplyFiles } from "../shared/reply-files.ts";
|
|
25
|
-
import { endOf,
|
|
25
|
+
import { endOf, settleTurnIn } from "./settle-turn.ts";
|
|
26
|
+
import { currentRecord } from "./turn-records.ts";
|
|
26
27
|
|
|
27
28
|
/** One turn of a conversation, as a claim asks for it. */
|
|
28
29
|
export interface ConversationTurnInput {
|
|
@@ -169,99 +170,108 @@ export function conversationTurns(
|
|
|
169
170
|
options: ConversationTurnsOptions,
|
|
170
171
|
): ConversationTurns {
|
|
171
172
|
const { surfaces, events, logger } = options;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
...(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}),
|
|
231
|
-
|
|
232
|
-
|
|
173
|
+
const runOne = async (input: ConversationTurnInput): Promise<TurnResult> => {
|
|
174
|
+
const { channel, kind, speaker } = input;
|
|
175
|
+
options.linked();
|
|
176
|
+
// Refused before anything is shown or recorded, like a turn the host cannot run.
|
|
177
|
+
if (options.stopping?.()) {
|
|
178
|
+
// The message whose admission asked for it is left with the cut turns.
|
|
179
|
+
currentRecord()?.refusedByHost();
|
|
180
|
+
throw new HostStoppingError();
|
|
181
|
+
}
|
|
182
|
+
// A host without a runtime is a setup mistake, so it is refused before anything is shown.
|
|
183
|
+
const runtime = options.runtime();
|
|
184
|
+
// A conversation that cannot be recorded runs no turn: who it belongs to would be lost.
|
|
185
|
+
const conversation = await record(options.registry?.(), input);
|
|
186
|
+
const stopTyping = surfaces.startTyping(channel);
|
|
187
|
+
const hideStop = surfaces.showStop(channel);
|
|
188
|
+
const turn = { kind, channel, speaker };
|
|
189
|
+
events.turnStarted(turn);
|
|
190
|
+
// A claim that posts its own reply formats its own text, so only the default reply posts as it goes.
|
|
191
|
+
const interim = input.reply ? undefined : surfaces.interim(channel);
|
|
192
|
+
// What the runtime reports as it goes reaches the surface and the handlers until the turn ends.
|
|
193
|
+
let live = true;
|
|
194
|
+
const progress = (event: TurnProgress) => {
|
|
195
|
+
if (!live) return;
|
|
196
|
+
events.turnProgress?.({ ...turn, progress: event });
|
|
197
|
+
// pi-lens-ignore: no-unknown-parameters — a rejection reason is unknown; it only reaches the logger
|
|
198
|
+
surfaces
|
|
199
|
+
.progress?.(channel, event)
|
|
200
|
+
?.catch((error: unknown) =>
|
|
201
|
+
logger.warn({ channel, kind, err: error }, "progress not shown"),
|
|
202
|
+
);
|
|
203
|
+
};
|
|
204
|
+
let result: TurnResult;
|
|
205
|
+
try {
|
|
206
|
+
result = await settleTurnIn(
|
|
207
|
+
channel,
|
|
208
|
+
() =>
|
|
209
|
+
withReplyFiles(surfaces.of(channel)?.supportsFiles === true, () =>
|
|
210
|
+
runtime.runTurn({
|
|
211
|
+
channel,
|
|
212
|
+
kind,
|
|
213
|
+
selection: input.selection ?? {
|
|
214
|
+
id: DEFAULT_SELECTION,
|
|
215
|
+
...options.selection(),
|
|
216
|
+
},
|
|
217
|
+
text: input.text,
|
|
218
|
+
speaker,
|
|
219
|
+
...(input.origin === undefined ? {} : { origin: input.origin }),
|
|
220
|
+
...(input.client === undefined ? {} : { client: input.client }),
|
|
221
|
+
...(conversation ? { conversation } : {}),
|
|
222
|
+
...(input.attachments ? { attachments: input.attachments } : {}),
|
|
223
|
+
...(input.confirmed ? { confirmed: true } : {}),
|
|
224
|
+
...(input.steerable ? { steerable: true } : {}),
|
|
225
|
+
...(input.byMessage ? { byMessage: true } : {}),
|
|
226
|
+
...(input.interactive ? { interactive: true } : {}),
|
|
227
|
+
...(input.unattended ? { unattended: true } : {}),
|
|
228
|
+
...(input.timeLimitMs === undefined
|
|
229
|
+
? {}
|
|
230
|
+
: { timeLimitMs: input.timeLimitMs }),
|
|
231
|
+
...(interim ? { interim } : {}),
|
|
232
|
+
progress,
|
|
233
|
+
}),
|
|
234
|
+
),
|
|
235
|
+
"conversation turn",
|
|
236
|
+
);
|
|
237
|
+
} finally {
|
|
238
|
+
live = false;
|
|
239
|
+
hideStop();
|
|
240
|
+
}
|
|
241
|
+
events.turnEnded({
|
|
242
|
+
...turn,
|
|
243
|
+
result: endOf(result),
|
|
244
|
+
});
|
|
245
|
+
try {
|
|
246
|
+
if (!result.ok && !result.stopped)
|
|
247
|
+
logger.error({ channel, kind, err: result.error }, "turn failed");
|
|
248
|
+
if (isQuiet(result))
|
|
249
|
+
logger.info(
|
|
250
|
+
{ channel, kind },
|
|
251
|
+
"unattended turn had nothing to say; nothing posted",
|
|
252
|
+
);
|
|
253
|
+
else if (input.reply) await input.reply(result);
|
|
254
|
+
else
|
|
255
|
+
await surfaces.sendReply(
|
|
256
|
+
channel,
|
|
257
|
+
replyBody(
|
|
258
|
+
result,
|
|
259
|
+
supportOf(surfaces.of(channel), channel, surfaces.lang),
|
|
260
|
+
),
|
|
233
261
|
);
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
{ channel, kind },
|
|
248
|
-
"unattended turn had nothing to say; nothing posted",
|
|
249
|
-
);
|
|
250
|
-
else if (input.reply) await input.reply(result);
|
|
251
|
-
else
|
|
252
|
-
await surfaces.sendReply(
|
|
253
|
-
channel,
|
|
254
|
-
replyBody(
|
|
255
|
-
result,
|
|
256
|
-
supportOf(surfaces.of(channel), channel, surfaces.lang),
|
|
257
|
-
),
|
|
258
|
-
);
|
|
259
|
-
} catch (error) {
|
|
260
|
-
logger.error({ channel, kind, err: error }, "reply not posted");
|
|
261
|
-
} finally {
|
|
262
|
-
stopTyping();
|
|
263
|
-
}
|
|
264
|
-
return result;
|
|
262
|
+
} catch (error) {
|
|
263
|
+
logger.error({ channel, kind, err: error }, "reply not posted");
|
|
264
|
+
} finally {
|
|
265
|
+
stopTyping();
|
|
266
|
+
}
|
|
267
|
+
return result;
|
|
268
|
+
};
|
|
269
|
+
return {
|
|
270
|
+
run(input) {
|
|
271
|
+
// Asked for from a message's admission, awaited or not, it keeps that admission's work open.
|
|
272
|
+
const record = currentRecord();
|
|
273
|
+
record?.begin(input.channel);
|
|
274
|
+
return runOne(input).finally(() => record?.end(input.channel));
|
|
265
275
|
},
|
|
266
276
|
};
|
|
267
277
|
}
|
|
@@ -21,14 +21,10 @@ export interface QueuedEntry {
|
|
|
21
21
|
readonly queuedAt: number;
|
|
22
22
|
/** Cancelled while it waited: its turn does not run. */
|
|
23
23
|
cancelled: boolean;
|
|
24
|
+
/** Marked as waiting, so its author may see, cancel, or edit it; an unmarked one is only refused at shutdown. */
|
|
25
|
+
readonly listed: boolean;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
/**
|
|
27
|
-
* The messages waiting for their own turn, by channel and message id: the router adds one when
|
|
28
|
-
* the running turn did not take it and takes it out when its turn starts, so a cancel or an edit
|
|
29
|
-
* can only reach a message that has not started. Everything is synchronous, so no cancel or edit
|
|
30
|
-
* races the start of a turn: whichever runs first wins.
|
|
31
|
-
*/
|
|
32
28
|
/** How a refusal of every waiting message went, filled in as it goes; set `stopped` to start no more. */
|
|
33
29
|
export interface RefuseTally {
|
|
34
30
|
refused: number;
|
|
@@ -36,6 +32,12 @@ export interface RefuseTally {
|
|
|
36
32
|
stopped: boolean;
|
|
37
33
|
}
|
|
38
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The messages waiting for their own turn, by channel and message id: the router adds one when
|
|
37
|
+
* the running turn did not take it and takes it out when its turn starts, so a cancel or an edit
|
|
38
|
+
* can only reach a message that has not started. Everything is synchronous, so no cancel or edit
|
|
39
|
+
* races the start of a turn: whichever runs first wins.
|
|
40
|
+
*/
|
|
39
41
|
export class QueuedMessages {
|
|
40
42
|
readonly #entries = new Map<ChannelKey, Map<string, QueuedEntry>>();
|
|
41
43
|
readonly #logger: Logger;
|
|
@@ -50,6 +52,7 @@ export class QueuedMessages {
|
|
|
50
52
|
claim: ChannelClaim,
|
|
51
53
|
message: InboundMessage,
|
|
52
54
|
admission: TurnAdmission,
|
|
55
|
+
listed = true,
|
|
53
56
|
): QueuedEntry {
|
|
54
57
|
const entry: QueuedEntry = {
|
|
55
58
|
claim,
|
|
@@ -57,6 +60,7 @@ export class QueuedMessages {
|
|
|
57
60
|
admission,
|
|
58
61
|
queuedAt: this.#now(),
|
|
59
62
|
cancelled: false,
|
|
63
|
+
listed,
|
|
60
64
|
};
|
|
61
65
|
const channel = this.#entries.get(message.channel) ?? new Map();
|
|
62
66
|
channel.set(message.messageId, entry);
|
|
@@ -75,14 +79,16 @@ export class QueuedMessages {
|
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
list(channel: ChannelKey): readonly QueuedMessage[] {
|
|
78
|
-
return [...(this.#entries.get(channel)?.values() ?? [])]
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
return [...(this.#entries.get(channel)?.values() ?? [])]
|
|
83
|
+
.filter((entry) => entry.listed)
|
|
84
|
+
.map((entry) => ({
|
|
85
|
+
messageId: entry.message.messageId,
|
|
86
|
+
...(entry.message.speaker
|
|
87
|
+
? { principalId: entry.message.speaker.principalId }
|
|
88
|
+
: {}),
|
|
89
|
+
text: entry.message.text,
|
|
90
|
+
queuedAt: entry.queuedAt,
|
|
91
|
+
}));
|
|
86
92
|
}
|
|
87
93
|
|
|
88
94
|
/**
|
|
@@ -96,7 +102,7 @@ export class QueuedMessages {
|
|
|
96
102
|
by: QueuedBy | undefined,
|
|
97
103
|
unmark: boolean,
|
|
98
104
|
): QueuedChange {
|
|
99
|
-
const entry = this.#
|
|
105
|
+
const entry = this.#listed(channel, messageId);
|
|
100
106
|
if (!entry) return "unknown";
|
|
101
107
|
if (by && !this.#authored(entry, by)) return "forbidden";
|
|
102
108
|
this.remove(entry);
|
|
@@ -116,7 +122,7 @@ export class QueuedMessages {
|
|
|
116
122
|
by: QueuedBy | undefined,
|
|
117
123
|
text: string,
|
|
118
124
|
): QueuedChange {
|
|
119
|
-
const entry = this.#
|
|
125
|
+
const entry = this.#listed(channel, messageId);
|
|
120
126
|
if (!entry) return "unknown";
|
|
121
127
|
if (by && !this.#authored(entry, by)) return "forbidden";
|
|
122
128
|
if (entry.message.text === text) return "done";
|
|
@@ -177,6 +183,11 @@ export class QueuedMessages {
|
|
|
177
183
|
);
|
|
178
184
|
}
|
|
179
185
|
|
|
186
|
+
#listed(channel: ChannelKey, messageId: string): QueuedEntry | undefined {
|
|
187
|
+
const entry = this.#entries.get(channel)?.get(messageId);
|
|
188
|
+
return entry?.listed ? entry : undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
180
191
|
#authored(entry: QueuedEntry, by: QueuedBy): boolean {
|
|
181
192
|
const author = entry.message.speaker?.principalId;
|
|
182
193
|
return author !== undefined && author === by.principalId;
|
|
@@ -1,21 +1,54 @@
|
|
|
1
1
|
import type { ScheduledOutcome } from "../contract/channels.ts";
|
|
2
2
|
import type { TurnResult } from "../domain/conversation.ts";
|
|
3
3
|
import { AgentRunError } from "../domain/errors.ts";
|
|
4
|
+
import type { ChannelKey } from "../domain/sessions.ts";
|
|
5
|
+
import { HostStoppingError } from "../kernel/errors.ts";
|
|
4
6
|
import type { TurnEndEvent } from "../plugin-api/plugin.ts";
|
|
7
|
+
import { currentChannel, currentRecord } from "./turn-records.ts";
|
|
5
8
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Runs a turn; a thrown error becomes its failed result, labelled `<what> crashed`. Run in the
|
|
11
|
+
* work of a message's admission while that is open, its `run` or what it started, awaited or not,
|
|
12
|
+
* such as a queue task, an `ok` result counts as the answer to that message, and a turn that ends
|
|
13
|
+
* after a shutdown left the message for the next process gives a stopped result instead. Started
|
|
14
|
+
* without waiting, the drain waits for it in the channel of the queue task it runs in, or, outside
|
|
15
|
+
* any, in that of the admission's first message, and asks that channel's turn to stop at its limit.
|
|
16
|
+
*/
|
|
17
|
+
export function settleTurn<R>(
|
|
8
18
|
run: () => Promise<R>,
|
|
9
19
|
what: string,
|
|
10
20
|
): Promise<R | { ok: false; error: AgentRunError }> {
|
|
21
|
+
return settleTurnIn(currentChannel(), run, what);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* `settleTurn` for a turn that runs in `channel`, as `context.turns` and the agent team run theirs:
|
|
26
|
+
* the drain then waits for it in that channel rather than in that of the admission's first message.
|
|
27
|
+
*/
|
|
28
|
+
export async function settleTurnIn<R>(
|
|
29
|
+
channel: ChannelKey | undefined,
|
|
30
|
+
run: () => Promise<R>,
|
|
31
|
+
what: string,
|
|
32
|
+
): Promise<R | { ok: false; error: AgentRunError }> {
|
|
33
|
+
const record = currentRecord();
|
|
34
|
+
record?.begin(channel);
|
|
35
|
+
let result: R | { ok: false; error: AgentRunError };
|
|
11
36
|
try {
|
|
12
|
-
|
|
37
|
+
result = await run();
|
|
13
38
|
} catch (error) {
|
|
14
|
-
|
|
39
|
+
// A turn refused because the host stops leaves its message, as a cut one does, and is no crash.
|
|
40
|
+
const refused = record !== undefined && error instanceof HostStoppingError;
|
|
41
|
+
result = {
|
|
15
42
|
ok: false,
|
|
16
|
-
error: new AgentRunError(
|
|
43
|
+
error: new AgentRunError(
|
|
44
|
+
refused ? error.message : `${what} crashed: ${String(error)}`,
|
|
45
|
+
),
|
|
17
46
|
};
|
|
18
47
|
}
|
|
48
|
+
if (!record) return result;
|
|
49
|
+
const settled = record.settle(result);
|
|
50
|
+
record.end(channel);
|
|
51
|
+
return settled;
|
|
19
52
|
}
|
|
20
53
|
|
|
21
54
|
/** How a turn ended, as the `turnEnded` event reports it. */
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { InboundMessage } from "../contract/channels.ts";
|
|
2
|
+
import type { SurfacePort } from "../contract/surface.ts";
|
|
3
|
+
import type { ChannelKey } from "../domain/sessions.ts";
|
|
4
|
+
import { messages } from "../i18n/index.ts";
|
|
5
|
+
import type { Logger } from "../kernel/log.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Tells the channel of a person's message the host does not answer because it shuts down: its
|
|
9
|
+
* surface's `refuseStopping` may take it and tell the channel itself, such as leave it for the next
|
|
10
|
+
* process; otherwise the channel is told once (`told`) to send it again. No one is told for a bot's
|
|
11
|
+
* or an integration's message, such as a report, since a notice there would only start the next
|
|
12
|
+
* one. Never rejects.
|
|
13
|
+
*/
|
|
14
|
+
export async function tellStopping(
|
|
15
|
+
message: InboundMessage,
|
|
16
|
+
surfaces:
|
|
17
|
+
| (Pick<SurfacePort, "of"> & Partial<Pick<SurfacePort, "sendReply">>)
|
|
18
|
+
| undefined,
|
|
19
|
+
told: Set<ChannelKey>,
|
|
20
|
+
logger: Logger,
|
|
21
|
+
): Promise<void> {
|
|
22
|
+
const { channel } = message;
|
|
23
|
+
if (message.authorIsBot || message.integration) return;
|
|
24
|
+
try {
|
|
25
|
+
if (await surfaces?.of(channel)?.refuseStopping?.(message)) return;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
logger.warn(
|
|
28
|
+
{ channel, err: error },
|
|
29
|
+
"surface did not take a refused message",
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
if (told.has(channel)) return;
|
|
33
|
+
told.add(channel);
|
|
34
|
+
await surfaces
|
|
35
|
+
?.sendReply?.(channel, { chunks: [messages().restartingNotice] })
|
|
36
|
+
.catch((error: unknown) =>
|
|
37
|
+
logger.warn({ channel, err: error }, "restart notice not posted"),
|
|
38
|
+
);
|
|
39
|
+
}
|