pi-roundtable 0.52.2 → 0.53.0
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 +18 -0
- package/docs/plugins.md +6 -2
- 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/contract/channels.ts +2 -1
- package/src/core/host/host-registration.ts +2 -0
- package/src/core/host/host.ts +27 -8
- 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 +75 -90
- package/src/core/routing/conversation-turns.ts +103 -93
- 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
|
@@ -15,12 +15,11 @@ import {
|
|
|
15
15
|
} from "../contract/channels.ts";
|
|
16
16
|
import type { SurfacePort } from "../contract/surface.ts";
|
|
17
17
|
import type { ChannelKey } from "../domain/sessions.ts";
|
|
18
|
-
import {
|
|
19
|
-
import type { Contact, ContactAssessor } from "../identity/contact.ts";
|
|
18
|
+
import type { ContactAssessor } from "../identity/contact.ts";
|
|
20
19
|
import type { IdentityService } from "../identity/identity-service.ts";
|
|
21
20
|
import { HostStoppingError, PluginError } from "../kernel/errors.ts";
|
|
22
21
|
import type { Logger } from "../kernel/log.ts";
|
|
23
|
-
import {
|
|
22
|
+
import { admitMessage } from "./admit-message.ts";
|
|
24
23
|
import { speakerOf } from "./background-speaker.ts";
|
|
25
24
|
import type { ChannelQueue } from "./channel-queue.ts";
|
|
26
25
|
import { tellDeleted, tellEdited } from "./claim-events.ts";
|
|
@@ -32,6 +31,8 @@ import {
|
|
|
32
31
|
QueuedMessages,
|
|
33
32
|
type RefuseTally,
|
|
34
33
|
} from "./queued-messages.ts";
|
|
34
|
+
import { tellStopping } from "./stopping-notice.ts";
|
|
35
|
+
import { RunningTurns, underRecord } from "./turn-records.ts";
|
|
35
36
|
|
|
36
37
|
export interface ChannelRouterOptions {
|
|
37
38
|
/** Claims in registration order; the router orders them by priority. */
|
|
@@ -55,6 +56,8 @@ export interface ChannelRouterOptions {
|
|
|
55
56
|
peerBots?: PeerBots;
|
|
56
57
|
/** The plugins' passive listeners; each message that reaches routing in an owned channel is handed to them. */
|
|
57
58
|
observers?: MessageObservers;
|
|
59
|
+
/** The runtime's stop, with which `abort` stops a turn in a channel whose claim has no `stop`. */
|
|
60
|
+
stopTurn?: (channel: ChannelKey) => boolean;
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
/** Claims by descending priority, keeping registration order among equals; refuses a repeated name. */
|
|
@@ -93,6 +96,8 @@ export class ChannelRouter implements ConversationPort {
|
|
|
93
96
|
readonly #warned = new Set<string>();
|
|
94
97
|
/** The channels already told that the host is restarting, so a burst of messages gets one notice. */
|
|
95
98
|
readonly #told = new Set<ChannelKey>();
|
|
99
|
+
/** The admitted turns running, so a shutdown that cuts one leaves its messages for the next process. */
|
|
100
|
+
readonly #turns = new RunningTurns();
|
|
96
101
|
|
|
97
102
|
constructor(options: ChannelRouterOptions) {
|
|
98
103
|
this.#options = options;
|
|
@@ -111,7 +116,10 @@ export class ChannelRouter implements ConversationPort {
|
|
|
111
116
|
* one turn.
|
|
112
117
|
*/
|
|
113
118
|
handle(message: InboundMessage): Promise<void> {
|
|
114
|
-
|
|
119
|
+
// Its own turn, never part of the one that delivered it, such as a plugin's turn passing it on.
|
|
120
|
+
return underRecord(undefined, () =>
|
|
121
|
+
this.#forwards.handle(message, (m) => this.#route(m)),
|
|
122
|
+
);
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
async #route(given: InboundMessage): Promise<void> {
|
|
@@ -151,7 +159,11 @@ export class ChannelRouter implements ConversationPort {
|
|
|
151
159
|
);
|
|
152
160
|
return undefined;
|
|
153
161
|
}
|
|
154
|
-
const admitted = await
|
|
162
|
+
const admitted = await admitMessage(claim, message, {
|
|
163
|
+
logger: this.#options.logger,
|
|
164
|
+
warned: this.#warned,
|
|
165
|
+
...(this.#options.contacts ? { contacts: this.#options.contacts } : {}),
|
|
166
|
+
});
|
|
155
167
|
if (!admitted) return undefined;
|
|
156
168
|
const { admission } = admitted;
|
|
157
169
|
if (listed) peers?.answered(message.channel);
|
|
@@ -178,70 +190,7 @@ export class ChannelRouter implements ConversationPort {
|
|
|
178
190
|
return run;
|
|
179
191
|
}
|
|
180
192
|
|
|
181
|
-
/**
|
|
182
|
-
* The claim's admission of the message, which carries the speaker its author resolves to; one
|
|
183
|
-
* who cannot be resolved, such as while the database is down, reaches the claim as no one. The
|
|
184
|
-
* author's contact is recorded, such as their first contact linked, only once the claim takes
|
|
185
|
-
* the message, so a message nobody serves links or makes no one. A linked author the rules
|
|
186
|
-
* refuse is still recorded as seen at no tier as the message is assessed, whether or not a
|
|
187
|
-
* claim then takes it, at most once in a while. When that record fails, or finds them someone
|
|
188
|
-
* else or no one now, such as linked elsewhere meanwhile, the message is dropped: it would run
|
|
189
|
-
* as a principal that may not exist, such as one only assessed at a first contact. The claim
|
|
190
|
-
* hears it through the admission's `dropped` or `unanswered`.
|
|
191
|
-
*/
|
|
192
|
-
async #admit(
|
|
193
|
-
claim: ChannelClaim,
|
|
194
|
-
message: InboundMessage,
|
|
195
|
-
): Promise<{ admission: Admission; message: InboundMessage } | undefined> {
|
|
196
|
-
const { logger, contacts } = this.#options;
|
|
197
|
-
const { speaker: _given, ...facts } = message;
|
|
198
|
-
const actor = actorOf(message, this.#warned, logger);
|
|
199
|
-
let contact: Contact | undefined;
|
|
200
|
-
if (actor && contacts)
|
|
201
|
-
try {
|
|
202
|
-
contact = await contacts.assess(actor, {
|
|
203
|
-
conversation: message.channel,
|
|
204
|
-
});
|
|
205
|
-
} catch (error) {
|
|
206
|
-
logger.error(
|
|
207
|
-
{ channel: message.channel, err: error },
|
|
208
|
-
"could not resolve who wrote a message; it reaches its claim as no one",
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
const admittedAs: InboundMessage = contact
|
|
212
|
-
? { ...facts, speaker: Object.freeze({ ...contact.speaker }) }
|
|
213
|
-
: facts;
|
|
214
|
-
const admission = claim.admit(admittedAs);
|
|
215
|
-
if (!admission) return undefined;
|
|
216
|
-
if (!contact) return { admission, message: admittedAs };
|
|
217
|
-
try {
|
|
218
|
-
const taken = await contact.take();
|
|
219
|
-
if (taken?.principalId === contact.speaker.principalId)
|
|
220
|
-
return { admission, message: admittedAs };
|
|
221
|
-
logger.warn(
|
|
222
|
-
{ channel: message.channel },
|
|
223
|
-
"the author of a message was linked elsewhere, or refused, while it was admitted; it does not run",
|
|
224
|
-
);
|
|
225
|
-
} catch (error) {
|
|
226
|
-
logger.error(
|
|
227
|
-
{ channel: message.channel, err: error },
|
|
228
|
-
"could not record who wrote a message; it does not run",
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
if (admission.kind === "turn") admission.dropped?.();
|
|
232
|
-
else
|
|
233
|
-
admission.unanswered({
|
|
234
|
-
status: "skipped",
|
|
235
|
-
reason: "its author was not who they were when it was admitted",
|
|
236
|
-
});
|
|
237
|
-
return undefined;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Drops a message the host no longer starts a turn for because it is shutting down, and tells
|
|
242
|
-
* its channel once that it restarts; no one is told for a bot's or an integration's message, such
|
|
243
|
-
* as a report, since a notice there would only start the next one.
|
|
244
|
-
*/
|
|
193
|
+
/** Drops a message the host no longer starts a turn for as it stops, and tells its channel (`tellStopping`). */
|
|
245
194
|
async #refuse(message: InboundMessage, admission: Admission): Promise<void> {
|
|
246
195
|
const { logger, surfaces } = this.#options;
|
|
247
196
|
const { channel } = message;
|
|
@@ -252,23 +201,7 @@ export class ChannelRouter implements ConversationPort {
|
|
|
252
201
|
reason: new HostStoppingError().message,
|
|
253
202
|
});
|
|
254
203
|
logger.info({ channel }, "message not run: the host is shutting down");
|
|
255
|
-
|
|
256
|
-
try {
|
|
257
|
-
// The surface may take it, and tell its channel itself.
|
|
258
|
-
if (await surfaces?.of(channel)?.refuseStopping?.(message)) return;
|
|
259
|
-
} catch (error) {
|
|
260
|
-
logger.warn(
|
|
261
|
-
{ channel, err: error },
|
|
262
|
-
"surface did not take a refused message",
|
|
263
|
-
);
|
|
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
|
-
);
|
|
204
|
+
await tellStopping(message, surfaces, this.#told, logger);
|
|
272
205
|
}
|
|
273
206
|
|
|
274
207
|
/**
|
|
@@ -291,7 +224,19 @@ export class ChannelRouter implements ConversationPort {
|
|
|
291
224
|
let waiting: QueuedEntry | undefined;
|
|
292
225
|
// During a turn a message joins it or is marked waiting; unmarkable, it is kept so a shutdown refuses it.
|
|
293
226
|
if (busy && queue.size(message.channel) > 0) {
|
|
294
|
-
|
|
227
|
+
// Joins the running turn's record first, so an answer that ends that turn as it takes the
|
|
228
|
+
// message counts for it; taken back when the turn does not take it.
|
|
229
|
+
const running = this.#turns.running(message.channel);
|
|
230
|
+
running?.add(message);
|
|
231
|
+
if (await busy.steer?.()) {
|
|
232
|
+
if (!running)
|
|
233
|
+
logger.warn(
|
|
234
|
+
{ channel: message.channel },
|
|
235
|
+
"message steered into a turn routing did not start; a shutdown that cuts that turn does not refuse it",
|
|
236
|
+
);
|
|
237
|
+
return { done: busy.react(STEERED_MARK) };
|
|
238
|
+
}
|
|
239
|
+
running?.remove(message);
|
|
295
240
|
// Listed before the mark is placed, so a delete or cancel during that wait reaches it.
|
|
296
241
|
waiting = this.#queued.add(claim, message, admission);
|
|
297
242
|
await busy.react(QUEUED_MARK);
|
|
@@ -300,11 +245,12 @@ export class ChannelRouter implements ConversationPort {
|
|
|
300
245
|
waiting = this.#queued.add(claim, message, admission, false);
|
|
301
246
|
const done = queue
|
|
302
247
|
.run(message.channel, () => {
|
|
303
|
-
if (!waiting) return admission.run();
|
|
248
|
+
if (!waiting) return this.#turns.run(message, () => admission.run());
|
|
304
249
|
// From here no cancel or edit reaches it: the turn has started or it was cancelled before.
|
|
305
250
|
if (!this.#queued.remove(waiting)) return Promise.resolve();
|
|
306
251
|
this.#queued.unmark(waiting);
|
|
307
|
-
|
|
252
|
+
const { message: started, admission: admitted } = waiting;
|
|
253
|
+
return this.#turns.run(started, () => admitted.run());
|
|
308
254
|
})
|
|
309
255
|
// pi-lens-ignore: no-unknown-parameters
|
|
310
256
|
.catch(async (error: unknown) => {
|
|
@@ -325,6 +271,28 @@ export class ChannelRouter implements ConversationPort {
|
|
|
325
271
|
return { done };
|
|
326
272
|
}
|
|
327
273
|
|
|
274
|
+
/** Marks every admitted turn still running as cut, as the drain limit passes. */
|
|
275
|
+
cutRunning(): void {
|
|
276
|
+
this.#turns.cut();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The channels where turns of admitted messages run outside their queue, which the drain waits for too. */
|
|
280
|
+
busy(): ChannelKey[] {
|
|
281
|
+
return this.#turns.busy();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Leaves each person's message of every turn cut at the drain limit, or refused as the host
|
|
286
|
+
* stopped, that did not answer, through its surface as a refused one is, counting in `tally`;
|
|
287
|
+
* call it once the abort grace is over. `dropping` ends once what the turns handed on is dropped.
|
|
288
|
+
*/
|
|
289
|
+
leaveCut(tally: RefuseTally): Promise<{ dropping: Promise<unknown> }> {
|
|
290
|
+
const { logger, surfaces } = this.#options;
|
|
291
|
+
return this.#turns.leaveCut(tally, logger, (message) =>
|
|
292
|
+
tellStopping(message, surfaces, this.#told, logger),
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
328
296
|
/** Refuses each message still waiting behind a turn after the drain, counting in `tally`. */
|
|
329
297
|
refuseWaiting(tally: RefuseTally): Promise<void> {
|
|
330
298
|
return this.#queued.refuseAll(
|
|
@@ -395,7 +363,12 @@ export class ChannelRouter implements ConversationPort {
|
|
|
395
363
|
* is checked to be; never rejects. A turn whose target no plugin contributes is skipped, never
|
|
396
364
|
* handed to another target's claim, and so is one whose author may not run it now.
|
|
397
365
|
*/
|
|
398
|
-
|
|
366
|
+
background(turn: BackgroundTurn): Promise<ScheduledOutcome> {
|
|
367
|
+
// Not part of a message's turn, even when work that turn started asks for it.
|
|
368
|
+
return underRecord(undefined, () => this.#background(turn));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async #background(turn: BackgroundTurn): Promise<ScheduledOutcome> {
|
|
399
372
|
if (this.#options.queue.closed) return stopping();
|
|
400
373
|
if (!this.#options.targets(turn.target))
|
|
401
374
|
return {
|
|
@@ -481,6 +454,18 @@ export class ChannelRouter implements ConversationPort {
|
|
|
481
454
|
return this.#owner(channel)?.stop?.(channel) ?? false;
|
|
482
455
|
}
|
|
483
456
|
|
|
457
|
+
/** Stops the channel's running turn as the host shuts down: its claim's `stop`, else the runtime's. */
|
|
458
|
+
abort(channel: ChannelKey): boolean {
|
|
459
|
+
const owner = this.#owner(channel);
|
|
460
|
+
if (owner?.stop) return owner.stop(channel);
|
|
461
|
+
const stopped = this.#options.stopTurn?.(channel) ?? false;
|
|
462
|
+
this.#options.logger.info(
|
|
463
|
+
{ channel, stopped },
|
|
464
|
+
"shutting down: asked the runtime to stop a turn whose claim has no stop",
|
|
465
|
+
);
|
|
466
|
+
return stopped;
|
|
467
|
+
}
|
|
468
|
+
|
|
484
469
|
postsInPlace(channel: ChannelKey): boolean {
|
|
485
470
|
return this.#owner(channel)?.postsInPlace === true;
|
|
486
471
|
}
|
|
@@ -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
|
}
|
|
@@ -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
|
+
}
|