@qwen-code/channel-base 0.27.2-nightly.20261112.70a487be84 → 0.27.2

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.
@@ -13,7 +13,7 @@ import { matchMessageRoute } from './message-routes.js';
13
13
  import { NamedSessionManager, NamedSessionTaskError, } from './named-session-manager.js';
14
14
  import { getGlobalQwenDir } from './paths.js';
15
15
  import { sanitizeSenderName, sanitizeQuotedText, sanitizePromptText, sanitizePromptPath, sanitizeLogText, sanitizeDisplayText, truncateCodePoints, PROMPT_UNSAFE_INVISIBLES, } from './sanitize.js';
16
- import { BACKGROUND_WORKFLOW_APPROVAL_META_KEY, CHANNEL_WORKFLOW_ORIGIN_META_KEY, ChannelPromptCancelledError, parseChannelWorkflowOrigin, } from './ChannelAgentBridge.js';
16
+ import { ChannelPromptCancelledError } from './ChannelAgentBridge.js';
17
17
  import { ChannelLoopSkippedError } from './ChannelLoopScheduler.js';
18
18
  import { buildChannelWebhookPrompt, resolveChannelWebhookTarget, } from './ChannelWebhookTask.js';
19
19
  import { parseChannelMemoryIntent, } from './channel-memory-intent.js';
@@ -226,8 +226,6 @@ export class ChannelBase {
226
226
  };
227
227
  pendingPermissions = new Map();
228
228
  pendingPermissionsByChat = new Map();
229
- // A workflow may settle its approval while the named-task lookup awaits.
230
- preparingWorkflowApprovals = new Set();
231
229
  channelLoopToolHandler = {
232
230
  canHandle: (sessionId) => this.router.getTarget(sessionId)?.channelName === this.name,
233
231
  create: (sessionId, input) => this.createLoopFromTool(sessionId, input),
@@ -259,29 +257,17 @@ export class ChannelBase {
259
257
  }
260
258
  this.onToolCall(chatId, event);
261
259
  }
262
- async dispatchBackgroundResponse(sessionId, text, context) {
260
+ async dispatchBackgroundResponse(sessionId, text, _context) {
263
261
  if (text.trim().length === 0)
264
262
  return;
265
- if (context?.kind === 'workflow') {
266
- const origin = context.channelWorkflowOrigin;
267
- if (!origin)
268
- return;
269
- const delivery = await this.resolveBackgroundResponseDelivery(sessionId, origin);
270
- if (!delivery || !this.workflowTarget(sessionId, origin))
271
- return;
272
- await this.deliverBackgroundResponseToTarget(sessionId, text, delivery);
273
- return;
274
- }
275
263
  const delivery = await this.resolveBackgroundResponseDelivery(sessionId);
276
264
  if (!delivery || this.router.getTarget(sessionId) !== delivery.target) {
277
265
  return;
278
266
  }
279
267
  await this.deliverBackgroundResponseToTarget(sessionId, text, delivery);
280
268
  }
281
- getBackgroundResponseSourceLabel(sessionId, workflowOrigin) {
282
- const target = workflowOrigin
283
- ? this.workflowTarget(sessionId, workflowOrigin)
284
- : this.router.getTarget(sessionId);
269
+ getBackgroundResponseSourceLabel(sessionId) {
270
+ const target = this.router.getTarget(sessionId);
285
271
  const presentation = this.namedSessions?.presentation(sessionId);
286
272
  if (!target ||
287
273
  target.channelName !== this.name ||
@@ -293,17 +279,14 @@ export class ChannelBase {
293
279
  }
294
280
  return this.createSourceLabel(presentation, target);
295
281
  }
296
- async resolveBackgroundResponseDelivery(sessionId, workflowOrigin) {
297
- const getTarget = () => workflowOrigin
298
- ? this.workflowTarget(sessionId, workflowOrigin)
299
- : this.router.getTarget(sessionId);
300
- let target = getTarget();
282
+ async resolveBackgroundResponseDelivery(sessionId) {
283
+ let target = this.router.getTarget(sessionId);
301
284
  if (!target || target.channelName !== this.name)
302
285
  return undefined;
303
286
  let sourceLabel;
304
287
  if (this.namedSessions) {
305
288
  const presentation = await this.namedSessions.resolvePresentation(sessionId);
306
- const currentTarget = getTarget();
289
+ const currentTarget = this.router.getTarget(sessionId);
307
290
  const currentPresentation = this.namedSessions.presentation(sessionId);
308
291
  if (!presentation ||
309
292
  presentation.status !== 'open' ||
@@ -319,18 +302,10 @@ export class ChannelBase {
319
302
  target = currentTarget;
320
303
  sourceLabel = this.createSourceLabel(presentation, target);
321
304
  }
322
- return {
323
- target,
324
- sourceLabel,
325
- ...(workflowOrigin ? { workflowOrigin } : {}),
326
- };
305
+ return { target, sourceLabel };
327
306
  }
328
307
  async deliverBackgroundResponseToTarget(sessionId, text, delivery) {
329
- const { target, sourceLabel, workflowOrigin } = delivery;
330
- if (workflowOrigin && !workflowOrigin.userInitiated) {
331
- await this.pushProactive(target, text, sourceLabel);
332
- return;
333
- }
308
+ const { target, sourceLabel } = delivery;
334
309
  if (this.supportsProactiveSend() && this.supportsProactiveTarget(target)) {
335
310
  if (sourceLabel) {
336
311
  await this.pushProactive(target, text, sourceLabel);
@@ -340,10 +315,6 @@ export class ChannelBase {
340
315
  }
341
316
  return;
342
317
  }
343
- if (workflowOrigin) {
344
- await this.sendThreadMessage(target.chatId, target.threadId, text, sourceLabel);
345
- return;
346
- }
347
318
  if (sourceLabel) {
348
319
  await this.deliverBackgroundReply(target.chatId, text, sessionId, sourceLabel);
349
320
  }
@@ -485,18 +456,7 @@ export class ChannelBase {
485
456
  request.controller.abort();
486
457
  }
487
458
  async dispatchPermissionRequest(event) {
488
- const backgroundWorkflow = event.request.toolCall._meta?.[BACKGROUND_WORKFLOW_APPROVAL_META_KEY] ===
489
- true;
490
- const workflowOrigin = backgroundWorkflow
491
- ? parseChannelWorkflowOrigin(event.request.toolCall._meta?.[CHANNEL_WORKFLOW_ORIGIN_META_KEY])
492
- : undefined;
493
- if (backgroundWorkflow && workflowOrigin?.channelName !== this.name) {
494
- return;
495
- }
496
- const getTarget = () => workflowOrigin
497
- ? this.workflowTarget(event.sessionId, workflowOrigin)
498
- : this.permissionTargetForEvent(event.sessionId);
499
- const target = getTarget();
459
+ const target = this.permissionTargetForEvent(event);
500
460
  if (!target) {
501
461
  try {
502
462
  await this.bridge.respondToPermission?.(event.requestId, {
@@ -512,14 +472,10 @@ export class ChannelBase {
512
472
  let taskName;
513
473
  if (this.namedSessions) {
514
474
  let presentation;
515
- if (backgroundWorkflow) {
516
- this.preparingWorkflowApprovals.add(event.requestId);
517
- }
518
475
  try {
519
476
  presentation = await this.namedSessions.resolvePresentation(event.sessionId);
520
477
  }
521
478
  catch (err) {
522
- this.preparingWorkflowApprovals.delete(event.requestId);
523
479
  try {
524
480
  await this.bridge.respondToPermission?.(event.requestId, {
525
481
  outcome: { outcome: 'cancelled' },
@@ -530,11 +486,7 @@ export class ChannelBase {
530
486
  }
531
487
  throw err;
532
488
  }
533
- if (backgroundWorkflow &&
534
- !this.preparingWorkflowApprovals.delete(event.requestId)) {
535
- return;
536
- }
537
- const currentTarget = getTarget();
489
+ const currentTarget = this.permissionTargetForEvent(event);
538
490
  const currentPresentation = this.namedSessions.presentation(event.sessionId);
539
491
  if (!presentation ||
540
492
  presentation.status !== 'open' ||
@@ -555,9 +507,7 @@ export class ChannelBase {
555
507
  }
556
508
  return;
557
509
  }
558
- const active = backgroundWorkflow
559
- ? undefined
560
- : this.activePrompts.get(event.sessionId);
510
+ const active = this.activePrompts.get(event.sessionId);
561
511
  sourceLabel = active
562
512
  ? active.sourceLabel
563
513
  : this.createSourceLabel(presentation, target);
@@ -578,11 +528,8 @@ export class ChannelBase {
578
528
  const pending = {
579
529
  requestId: event.requestId,
580
530
  sessionId: event.sessionId,
581
- runId: backgroundWorkflow
582
- ? undefined
583
- : this.activePrompts.get(event.sessionId)?.runId,
531
+ runId: this.activePrompts.get(event.sessionId)?.runId,
584
532
  target,
585
- ...(workflowOrigin ? { workflowOrigin } : {}),
586
533
  request: event.request,
587
534
  ...(sourceLabel ? { sourceLabel } : {}),
588
535
  ...(taskName ? { taskName } : {}),
@@ -594,15 +541,13 @@ export class ChannelBase {
594
541
  requestIds.push(event.requestId);
595
542
  this.pendingPermissionsByChat.set(chatKey, requestIds);
596
543
  try {
597
- if (!backgroundWorkflow) {
598
- const presentation = this.tryPresentUserInput(pending);
599
- if (presentation && (await presentation)) {
600
- return;
601
- }
602
- const permissionPresentation = this.tryPresentPermission(pending);
603
- if (permissionPresentation && (await permissionPresentation)) {
604
- return;
605
- }
544
+ const presentation = this.tryPresentUserInput(pending);
545
+ if (presentation && (await presentation)) {
546
+ return;
547
+ }
548
+ const permissionPresentation = this.tryPresentPermission(pending);
549
+ if (permissionPresentation && (await permissionPresentation)) {
550
+ return;
606
551
  }
607
552
  const text = this.formatPermissionRequest(pending);
608
553
  if (target.threadId !== undefined &&
@@ -888,37 +833,12 @@ export class ChannelBase {
888
833
  });
889
834
  return pending.responsePromise;
890
835
  }
891
- workflowTarget(sessionId, origin) {
892
- const routed = this.router.getTarget(sessionId);
893
- if (origin.channelName !== this.name ||
894
- routed?.channelName !== this.name ||
895
- (origin.messageRoute !== undefined &&
896
- !this.messageRoutes.has(origin.messageRoute)) ||
897
- !this.router.isSessionLive(sessionId)) {
898
- return undefined;
899
- }
900
- if (this.namedSessions) {
901
- return this.sameTaskOwner(origin, routed) ? origin : undefined;
902
- }
903
- if (this.router.getSession(this.name, origin.senderId, origin.chatId, origin.threadId, origin.messageRoute) === sessionId) {
904
- return origin;
905
- }
906
- // Webhook sessions can have a synthetic routing key while retaining the
907
- // actual delivery target.
908
- if (!origin.userInitiated &&
909
- this.sameTaskOwner(origin, routed) &&
910
- origin.threadId === routed.threadId &&
911
- origin.messageRoute === routed.messageRoute) {
912
- return origin;
913
- }
914
- return undefined;
915
- }
916
- permissionTargetForEvent(sessionId) {
917
- const routeTarget = this.router.getTarget(sessionId);
836
+ permissionTargetForEvent(event) {
837
+ const routeTarget = this.router.getTarget(event.sessionId);
918
838
  if (!routeTarget || routeTarget.channelName !== this.name) {
919
839
  return undefined;
920
840
  }
921
- const active = this.activePrompts.get(sessionId);
841
+ const active = this.activePrompts.get(event.sessionId);
922
842
  if (!active) {
923
843
  return routeTarget;
924
844
  }
@@ -939,7 +859,6 @@ export class ChannelBase {
939
859
  return target;
940
860
  }
941
861
  dispatchPermissionResolved(event) {
942
- this.preparingWorkflowApprovals.delete(event.requestId);
943
862
  const pending = this.pendingPermissions.get(event.requestId);
944
863
  if (!pending) {
945
864
  return;
@@ -1252,9 +1171,6 @@ export class ChannelBase {
1252
1171
  supportsProactiveWebhookTarget(target) {
1253
1172
  return this.supportsProactiveTarget(target);
1254
1173
  }
1255
- canDeliverBackgroundWorkflow(_target) {
1256
- return true;
1257
- }
1258
1174
  async pushProactive(target, text, sourceLabel) {
1259
1175
  if (target.threadId) {
1260
1176
  throw new Error('Channel does not support proactive loop messages for threaded targets.');
@@ -1635,7 +1551,7 @@ export class ChannelBase {
1635
1551
  promptBridge.on('textChunk', onChunk);
1636
1552
  promptBridge.on('responseBoundary', onResponseBoundary);
1637
1553
  try {
1638
- const response = await this.runLoopBridgePrompt(promptBridge, sessionId, promptToSend, promptState, job.target, job.id, options.timeoutMs);
1554
+ const response = await this.runLoopBridgePrompt(promptBridge, sessionId, promptToSend, promptState, job.id, options.timeoutMs);
1639
1555
  await this.settleCancelRequested(promptState);
1640
1556
  if (promptState.cancelled) {
1641
1557
  throw new ChannelLoopSkippedError('loop cancelled before delivery', 'cancel_command');
@@ -1863,7 +1779,7 @@ export class ChannelBase {
1863
1779
  const promptBridge = this.bridge;
1864
1780
  promptBridge.on('textChunk', onChunk);
1865
1781
  try {
1866
- const response = await this.runLoopBridgePrompt(promptBridge, sessionId, promptToSend, promptState, target, taskId, options.timeoutMs);
1782
+ const response = await this.runLoopBridgePrompt(promptBridge, sessionId, promptToSend, promptState, taskId, options.timeoutMs);
1867
1783
  await this.settleCancelRequested(promptState);
1868
1784
  if (promptState.cancelled) {
1869
1785
  throw new ChannelLoopSkippedError('webhook task cancelled before delivery', 'cancel_command');
@@ -1937,10 +1853,9 @@ export class ChannelBase {
1937
1853
  webhookRoutingThreadId(task, target) {
1938
1854
  return `webhook:${task.source}:${target.threadId ?? target.chatId}`;
1939
1855
  }
1940
- async runLoopBridgePrompt(promptBridge, sessionId, promptText, promptState, workflowOrigin, jobId, timeoutMs) {
1856
+ async runLoopBridgePrompt(promptBridge, sessionId, promptText, promptState, jobId, timeoutMs) {
1941
1857
  const prompt = promptBridge.prompt(sessionId, promptText, {
1942
1858
  displayText: sanitizeDisplayText(promptText),
1943
- workflowOrigin,
1944
1859
  });
1945
1860
  prompt.catch(() => { });
1946
1861
  if (timeoutMs === undefined) {
@@ -2050,9 +1965,7 @@ export class ChannelBase {
2050
1965
  }
2051
1966
  active.cancelled = true;
2052
1967
  this.dropCollectBuffer(sessionId);
2053
- this.removePendingPermissionsForSession(sessionId, 'run_cancelled', {
2054
- preserveBackgroundWorkflows: reason !== 'clear',
2055
- });
1968
+ this.removePendingPermissionsForSession(sessionId, 'run_cancelled');
2056
1969
  this.emitTaskCancellation(active, sessionId, reason);
2057
1970
  return true;
2058
1971
  });
@@ -2314,18 +2227,15 @@ export class ChannelBase {
2314
2227
  this.pendingPermissionsByChat.set(chatKey, remaining);
2315
2228
  }
2316
2229
  }
2317
- removePendingPermissionsForSession(sessionId, reason = 'cancelled', options) {
2230
+ removePendingPermissionsForSession(sessionId, reason = 'cancelled') {
2318
2231
  const requestIds = Array.from(this.pendingPermissions)
2319
- .filter(([, pending]) => pending.sessionId === sessionId &&
2320
- (!options?.preserveBackgroundWorkflows ||
2321
- pending.request.toolCall._meta?.[BACKGROUND_WORKFLOW_APPROVAL_META_KEY] !== true))
2232
+ .filter(([, pending]) => pending.sessionId === sessionId)
2322
2233
  .map(([requestId]) => requestId);
2323
2234
  for (const requestId of requestIds) {
2324
2235
  this.removePendingPermission(requestId, reason);
2325
2236
  }
2326
2237
  }
2327
2238
  clearPendingPermissions() {
2328
- this.preparingWorkflowApprovals.clear();
2329
2239
  for (const requestId of Array.from(this.pendingPermissions.keys())) {
2330
2240
  this.removePendingPermission(requestId, 'cancelled');
2331
2241
  }
@@ -2392,15 +2302,7 @@ export class ChannelBase {
2392
2302
  return { kind: 'found', pending: matching[0] };
2393
2303
  }
2394
2304
  canEnvelopeAnswerPendingPermission(envelope, pending, requireOwner) {
2395
- const origin = pending.workflowOrigin;
2396
- if (origin) {
2397
- if (!this.workflowTarget(pending.sessionId, origin) ||
2398
- (requireOwner &&
2399
- (!origin.userInitiated || origin.senderId !== envelope.senderId))) {
2400
- return false;
2401
- }
2402
- }
2403
- else if (requireOwner) {
2305
+ if (requireOwner) {
2404
2306
  const active = this.activePrompts.get(pending.sessionId);
2405
2307
  if (!active ||
2406
2308
  pending.runId !== active.runId ||
@@ -5260,7 +5162,7 @@ export class ChannelBase {
5260
5162
  // turn at the channel level (cancelled is already set above), so
5261
5163
  // the event reflects that intent, not the bridge RPC outcome.
5262
5164
  this.emitTaskCancellation(active, sessionId, 'steer');
5263
- this.removePendingPermissionsForSession(sessionId, 'run_cancelled', { preserveBackgroundWorkflows: true });
5165
+ this.removePendingPermissionsForSession(sessionId, 'run_cancelled');
5264
5166
  }
5265
5167
  // Diagnostic watchdog: if the predecessor turn is STILL the active prompt
5266
5168
  // after the wind-down bound, this steered turn is wedged behind a hung
@@ -5487,22 +5389,7 @@ export class ChannelBase {
5487
5389
  if (!this.canStartInboundTurn()) {
5488
5390
  throw new ChannelPromptCancelledError();
5489
5391
  }
5490
- const workflowTarget = {
5491
- channelName: this.name,
5492
- chatId: envelope.chatId,
5493
- threadId: envelope.threadId,
5494
- senderId: envelope.senderId,
5495
- isGroup: envelope.isGroup,
5496
- messageRoute: envelope.messageRoute,
5497
- };
5498
5392
  let response = await promptBridge.prompt(sessionId, promptToSend, {
5499
- workflowOrigin: {
5500
- ...workflowTarget,
5501
- userInitiated: true,
5502
- ...(!this.canDeliverBackgroundWorkflow(workflowTarget)
5503
- ? { backgroundWorkflowBlocked: true }
5504
- : {}),
5505
- },
5506
5393
  ...(this.config.outputMode === 'per_task'
5507
5394
  ? {
5508
5395
  outputMode: 'per_task',