@xmoxmo/bncr 0.4.8 → 0.4.9
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/package.json +1 -1
- package/src/messaging/inbound/reply-dispatch.ts +132 -119
package/package.json
CHANGED
|
@@ -208,130 +208,143 @@ export async function runBncrInboundReplyDispatch(args: {
|
|
|
208
208
|
return;
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
accountId: resolution.accountId,
|
|
217
|
-
raw: parsed,
|
|
218
|
-
adapter: {
|
|
219
|
-
ingest: () => ({
|
|
220
|
-
id: msgId ?? `${resolution.canonicalTo}:${Date.now()}`,
|
|
221
|
-
timestamp: Date.now(),
|
|
222
|
-
rawText: rawBody,
|
|
223
|
-
textForAgent: ctxPayload.BodyForAgent,
|
|
224
|
-
textForCommands: ctxPayload.CommandBody,
|
|
225
|
-
raw: parsed,
|
|
226
|
-
}),
|
|
227
|
-
preflight: () =>
|
|
228
|
-
shouldDispatch
|
|
229
|
-
? undefined
|
|
230
|
-
: {
|
|
231
|
-
admission: {
|
|
232
|
-
kind: 'observeOnly' as const,
|
|
233
|
-
reason: 'bncr-group-mode-no-reply',
|
|
234
|
-
},
|
|
235
|
-
},
|
|
236
|
-
resolveTurn: () => {
|
|
237
|
-
const sessionMetaBarrier = createBncrSessionMetaTaskBarrier();
|
|
238
|
-
return {
|
|
239
|
-
channel: channelId,
|
|
240
|
-
accountId: resolution.accountId,
|
|
241
|
-
routeSessionKey: resolution.resolvedRoute.sessionKey,
|
|
242
|
-
storePath,
|
|
243
|
-
ctxPayload,
|
|
244
|
-
recordInboundSession: wrapBncrInboundRecordSessionLabelCorrection({
|
|
245
|
-
recordInboundSession: recordBncrInboundSession as (
|
|
246
|
-
...args: unknown[]
|
|
247
|
-
) => Promise<unknown> | unknown,
|
|
248
|
-
expectedPatch: sessionIdentityPatch,
|
|
249
|
-
}),
|
|
250
|
-
record: {
|
|
251
|
-
updateLastRoute,
|
|
252
|
-
onRecordError: (err: unknown) => {
|
|
253
|
-
emitBncrLogLine('warn', `[bncr] inbound record session failed: ${String(err)}`);
|
|
254
|
-
},
|
|
255
|
-
trackSessionMetaTask: (task: Promise<unknown>) => {
|
|
256
|
-
sessionMetaBarrier.track(task);
|
|
257
|
-
},
|
|
258
|
-
},
|
|
259
|
-
runDispatch: async () => {
|
|
260
|
-
await sessionMetaBarrier.wait();
|
|
261
|
-
await correctBncrInboundSessionLabel({
|
|
262
|
-
storePath,
|
|
263
|
-
sessionKey: resolution.dispatchSessionKey,
|
|
264
|
-
expectedPatch: sessionIdentityPatch,
|
|
265
|
-
});
|
|
266
|
-
return Promise.resolve(
|
|
267
|
-
dispatchOpenClawReplyWithBufferedBlockDispatcher(api, {
|
|
268
|
-
ctx: ctxPayload,
|
|
269
|
-
cfg: buildBncrReplyConfig(commandDispatchCfg).replyCfg,
|
|
270
|
-
dispatcherOptions: {
|
|
271
|
-
deliver: async (
|
|
272
|
-
payload: {
|
|
273
|
-
text?: string;
|
|
274
|
-
mediaUrl?: string;
|
|
275
|
-
mediaUrls?: string[];
|
|
276
|
-
audioAsVoice?: boolean;
|
|
277
|
-
},
|
|
278
|
-
info?: { kind?: 'tool' | 'block' | 'final' },
|
|
279
|
-
) => {
|
|
280
|
-
const kind = info?.kind;
|
|
281
|
-
const shouldForwardTool =
|
|
282
|
-
effectiveReply.blockStreaming && effectiveReply.allowTool;
|
|
283
|
-
|
|
284
|
-
if (kind === 'tool' && !shouldForwardTool) {
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
211
|
+
// Stop commands should bypass the serial chain so they can interrupt a running agent.
|
|
212
|
+
const rawTrimmed = (rawBody || '').trim();
|
|
213
|
+
const isStopCommand =
|
|
214
|
+
rawTrimmed === '/stop' || rawTrimmed.startsWith('/stop ') || rawTrimmed.startsWith('/stop@');
|
|
215
|
+
const dispatchSessionKey = resolution.dispatchSessionKey;
|
|
287
216
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
217
|
+
const runStopOrSerial = () => {
|
|
218
|
+
const task = () =>
|
|
219
|
+
Promise.resolve(
|
|
220
|
+
resolveBncrChannelInboundRuntime(api).run({
|
|
221
|
+
channel: channelId,
|
|
222
|
+
accountId: resolution.accountId,
|
|
223
|
+
raw: parsed,
|
|
224
|
+
adapter: {
|
|
225
|
+
ingest: () => ({
|
|
226
|
+
id: msgId ?? `${resolution.canonicalTo}:${Date.now()}`,
|
|
227
|
+
timestamp: Date.now(),
|
|
228
|
+
rawText: rawBody,
|
|
229
|
+
textForAgent: ctxPayload.BodyForAgent,
|
|
230
|
+
textForCommands: ctxPayload.CommandBody,
|
|
231
|
+
raw: parsed,
|
|
232
|
+
}),
|
|
233
|
+
preflight: () =>
|
|
234
|
+
shouldDispatch
|
|
235
|
+
? undefined
|
|
236
|
+
: {
|
|
237
|
+
admission: {
|
|
238
|
+
kind: 'observeOnly' as const,
|
|
239
|
+
reason: 'bncr-group-mode-no-reply',
|
|
308
240
|
},
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
241
|
+
},
|
|
242
|
+
resolveTurn: () => {
|
|
243
|
+
const sessionMetaBarrier = createBncrSessionMetaTaskBarrier();
|
|
244
|
+
return {
|
|
245
|
+
channel: channelId,
|
|
246
|
+
accountId: resolution.accountId,
|
|
247
|
+
routeSessionKey: resolution.resolvedRoute.sessionKey,
|
|
248
|
+
storePath,
|
|
249
|
+
ctxPayload,
|
|
250
|
+
recordInboundSession: wrapBncrInboundRecordSessionLabelCorrection({
|
|
251
|
+
recordInboundSession: recordBncrInboundSession as (
|
|
252
|
+
...args: unknown[]
|
|
253
|
+
) => Promise<unknown> | unknown,
|
|
254
|
+
expectedPatch: sessionIdentityPatch,
|
|
255
|
+
}),
|
|
256
|
+
record: {
|
|
257
|
+
updateLastRoute,
|
|
258
|
+
onRecordError: (err: unknown) => {
|
|
259
|
+
emitBncrLogLine('warn', `[bncr] inbound record session failed: ${String(err)}`);
|
|
260
|
+
},
|
|
261
|
+
trackSessionMetaTask: (task: Promise<unknown>) => {
|
|
262
|
+
sessionMetaBarrier.track(task);
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
runDispatch: async () => {
|
|
266
|
+
await sessionMetaBarrier.wait();
|
|
319
267
|
await correctBncrInboundSessionLabel({
|
|
320
268
|
storePath,
|
|
321
|
-
sessionKey:
|
|
269
|
+
sessionKey: dispatchSessionKey,
|
|
322
270
|
expectedPatch: sessionIdentityPatch,
|
|
323
271
|
});
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
272
|
+
return Promise.resolve(
|
|
273
|
+
dispatchOpenClawReplyWithBufferedBlockDispatcher(api, {
|
|
274
|
+
ctx: ctxPayload,
|
|
275
|
+
cfg: buildBncrReplyConfig(commandDispatchCfg).replyCfg,
|
|
276
|
+
dispatcherOptions: {
|
|
277
|
+
deliver: async (
|
|
278
|
+
payload: {
|
|
279
|
+
text?: string;
|
|
280
|
+
mediaUrl?: string;
|
|
281
|
+
mediaUrls?: string[];
|
|
282
|
+
audioAsVoice?: boolean;
|
|
283
|
+
},
|
|
284
|
+
info?: { kind?: 'tool' | 'block' | 'final' },
|
|
285
|
+
) => {
|
|
286
|
+
const kind = info?.kind;
|
|
287
|
+
const shouldForwardTool =
|
|
288
|
+
effectiveReply.blockStreaming && effectiveReply.allowTool;
|
|
289
|
+
|
|
290
|
+
if (kind === 'tool' && !shouldForwardTool) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
await enqueueFromReply({
|
|
295
|
+
accountId: replyRouteFact.accountId,
|
|
296
|
+
sessionKey: replyRouteFact.sessionKey,
|
|
297
|
+
route: replyRouteFact.route,
|
|
298
|
+
payload: {
|
|
299
|
+
text: payload.text,
|
|
300
|
+
mediaUrl: payload.mediaUrl,
|
|
301
|
+
mediaUrls: payload.mediaUrls,
|
|
302
|
+
kind: kind || 'final',
|
|
303
|
+
replyToId: msgId || undefined,
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
},
|
|
307
|
+
onError: (err: unknown) => {
|
|
308
|
+
emitBncrLogLine('error', `[bncr] outbound reply failed: ${String(err)}`);
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
replyOptions: {
|
|
312
|
+
disableBlockStreaming: !effectiveReply.blockStreaming,
|
|
313
|
+
shouldEmitToolResult: effectiveReply.allowTool ? () => true : undefined,
|
|
314
|
+
},
|
|
315
|
+
}),
|
|
316
|
+
);
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
},
|
|
320
|
+
onFinalize: () => {
|
|
321
|
+
const inboundAt = Date.now();
|
|
322
|
+
setInboundActivity(resolution.accountId, inboundAt);
|
|
323
|
+
scheduleSave();
|
|
324
|
+
},
|
|
332
325
|
},
|
|
333
|
-
},
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
326
|
+
}),
|
|
327
|
+
).finally(async () => {
|
|
328
|
+
await waitForBncrReplySessionQuiescence({
|
|
329
|
+
api,
|
|
330
|
+
storePath,
|
|
331
|
+
sessionKey: dispatchSessionKey,
|
|
332
|
+
msgId,
|
|
333
|
+
to: resolution.canonicalTo,
|
|
334
|
+
debugEnabled: cfg?.channels?.bncr?.debug?.verbose === true,
|
|
335
|
+
});
|
|
336
|
+
await correctBncrInboundSessionLabel({
|
|
337
|
+
storePath,
|
|
338
|
+
sessionKey: dispatchSessionKey,
|
|
339
|
+
expectedPatch: sessionIdentityPatch,
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
if (isStopCommand) {
|
|
344
|
+
return task();
|
|
345
|
+
}
|
|
346
|
+
return runBncrReplyDispatchSerial(dispatchSessionKey, task);
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
await runStopOrSerial();
|
|
337
350
|
}
|