@xmanrui/dsh-im 5.3.0 → 5.4.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.
Files changed (49) hide show
  1. package/THIRD_PARTY_NOTICES.md +209 -0
  2. package/docs/client-integration.md +2 -2
  3. package/lib/client.js +1688 -233
  4. package/lib/index.js +322 -318
  5. package/package.json +1 -1
  6. package/plugin-src/client/channels/telegram/group-triggers-settings.js +393 -0
  7. package/plugin-src/client/delivery-settings.js +21 -1
  8. package/plugin-src/client/ds-bot-avatar-data.js +537 -0
  9. package/plugin-src/client/ds-bot-avatar.js +55 -0
  10. package/plugin-src/client/ds-bot-session-avatars.js +77 -0
  11. package/plugin-src/client/ds-bot-settings.js +194 -0
  12. package/plugin-src/client/i18n.js +59 -1
  13. package/plugin-src/client/index.js +12 -1
  14. package/plugin-src/client/plugin-page.js +3 -8
  15. package/plugin-src/client/styles.js +25 -5
  16. package/plugin-src/host/channels/dingtalk/production.mjs +2 -1
  17. package/plugin-src/host/channels/feishu/production.mjs +13 -5
  18. package/plugin-src/host/channels/qq/production.mjs +2 -1
  19. package/plugin-src/host/channels/shared/group-triggers-rpc.mjs +110 -0
  20. package/plugin-src/host/channels/shared/production.mjs +7 -1
  21. package/plugin-src/host/channels/slack/production.mjs +2 -1
  22. package/plugin-src/host/channels/telegram/index.mjs +1 -0
  23. package/plugin-src/host/channels/telegram/production.mjs +2 -0
  24. package/plugin-src/host/channels/telegram/rpc.mjs +17 -2
  25. package/plugin-src/host/channels/wecom/production.mjs +4 -1
  26. package/plugin-src/host/channels/wecom-app/production.mjs +2 -1
  27. package/plugin-src/host/channels/weixin/production.mjs +2 -1
  28. package/plugin-src/host/channels/whatsapp/production.mjs +2 -1
  29. package/plugin-src/host/delivery-adapter.mjs +89 -9
  30. package/plugin-src/host/delivery-rpc.mjs +33 -1
  31. package/plugin-src/host/delivery-service.mjs +70 -1
  32. package/plugin-src/host/delivery-suggestions.mjs +11 -0
  33. package/plugin-src/host/ds-bot-client.mjs +74 -0
  34. package/plugin-src/host/index.mjs +5 -1
  35. package/src/channels/feishu/feishu-cards.mjs +11 -7
  36. package/src/channels/shared/bot-workspace-store.mjs +136 -7
  37. package/src/channels/shared/group-history-store.mjs +26 -0
  38. package/src/channels/shared/i18n-en/feishu.mjs +1 -0
  39. package/src/channels/shared/i18n-en/wecom.mjs +4 -0
  40. package/src/channels/shared/session-binding-lock.mjs +2 -0
  41. package/src/channels/shared/workspace-session.mjs +5 -1
  42. package/src/channels/telegram/config-store.mjs +5 -1
  43. package/src/channels/telegram/group-triggers.mjs +229 -0
  44. package/src/channels/telegram/telegram-api.mjs +12 -0
  45. package/src/channels/telegram/telegram-controller.mjs +110 -1
  46. package/src/channels/telegram/telegram-runtime.mjs +15 -2
  47. package/src/channels/wecom/stream-supervisor.mjs +184 -0
  48. package/src/channels/wecom/wecom-bridge.mjs +25 -11
  49. package/src/channels/wecom/wecom-runtime.mjs +12 -1
@@ -74,6 +74,7 @@ import {
74
74
  evaluateInboundAccess,
75
75
  } from '../shared/inbound-access.mjs';
76
76
  import { t } from '../shared/i18n.mjs';
77
+ import { createWecomStreamSupervisor } from './stream-supervisor.mjs';
77
78
 
78
79
  const DEFAULT_FILE_UPLOAD_TIMEOUT_MS = 120_000;
79
80
 
@@ -586,6 +587,8 @@ export class WecomHarnessBridge {
586
587
  #status;
587
588
  #logger;
588
589
  #replyTimeoutMs;
590
+ #streamRotateAfterMs;
591
+ #streamSilentProbeMs;
589
592
  #generateReqId;
590
593
  #signal;
591
594
  #fileUploadTimeoutMs;
@@ -613,6 +616,8 @@ export class WecomHarnessBridge {
613
616
  status = createWecomBridgeStatus(),
614
617
  logger = console,
615
618
  replyTimeoutMs = 600_000,
619
+ streamRotateAfterMs = 510_000,
620
+ streamSilentProbeMs = 120_000,
616
621
  generateStreamId = generateReqId,
617
622
  fileUploadTimeoutMs = DEFAULT_FILE_UPLOAD_TIMEOUT_MS,
618
623
  signal,
@@ -632,6 +637,8 @@ export class WecomHarnessBridge {
632
637
  this.#status = status;
633
638
  this.#logger = logger;
634
639
  this.#replyTimeoutMs = replyTimeoutMs;
640
+ this.#streamRotateAfterMs = streamRotateAfterMs;
641
+ this.#streamSilentProbeMs = streamSilentProbeMs;
635
642
  this.#generateReqId = generateStreamId;
636
643
  this.#fileUploadTimeoutMs = Math.min(fileUploadTimeoutMs, DEFAULT_FILE_UPLOAD_TIMEOUT_MS);
637
644
  this.#signal = signal;
@@ -1359,6 +1366,7 @@ export class WecomHarnessBridge {
1359
1366
  const key = conversationKey(frame);
1360
1367
  let streamId = null;
1361
1368
  let streamStarted = false;
1369
+ let streamCtl = null;
1362
1370
  let streamThinkingText = t('正在思考中…');
1363
1371
  let streamAnswerText = '';
1364
1372
  let batchSettled = batchSubmission === null;
@@ -1424,6 +1432,17 @@ export class WecomHarnessBridge {
1424
1432
  } catch (error) {
1425
1433
  this.#logger.warn?.('[dsh-im:wecom] unable to start a stream; using an active reply:', error);
1426
1434
  }
1435
+ streamCtl = createWecomStreamSupervisor({
1436
+ client: this.#client,
1437
+ frame,
1438
+ chatId,
1439
+ generateStreamId: () => this.#generateReqId('stream'),
1440
+ previewText: () => splitUtf8(streamContent(streamThinkingText, streamAnswerText))[0],
1441
+ logger: this.#logger,
1442
+ rotateAfterMs: this.#streamRotateAfterMs,
1443
+ silentProbeMs: this.#streamSilentProbeMs,
1444
+ });
1445
+ if (streamStarted) streamCtl.start(streamId);
1427
1446
 
1428
1447
  let content = hasImages || hasReply
1429
1448
  ? await promptContentForInboundMessage(message, { signal: this.#signal, deferImages: true })
@@ -1469,7 +1488,7 @@ export class WecomHarnessBridge {
1469
1488
  const preview = splitUtf8(
1470
1489
  streamContent(streamThinkingText, streamAnswerText),
1471
1490
  )[0];
1472
- if (preview) await this.#client.replyStreamNonBlocking(frame, streamId, preview, false);
1491
+ if (preview) await streamCtl.progress(preview);
1473
1492
  }
1474
1493
  : undefined,
1475
1494
  onInteraction: (interaction) => this.#handleInteraction(interaction, {
@@ -1500,7 +1519,7 @@ export class WecomHarnessBridge {
1500
1519
  if (streamStarted && streamChunks.length > 0) {
1501
1520
  try {
1502
1521
  const providerMessageIds = [];
1503
- const streamed = await this.#client.replyStream(frame, streamId, streamChunks[0], true);
1522
+ const streamed = await streamCtl.finishStream(streamChunks[0]);
1504
1523
  const streamedMessageId = providerMessageId(streamed);
1505
1524
  if (streamedMessageId) providerMessageIds.push(streamedMessageId);
1506
1525
  for (const chunk of streamChunks.slice(1)) {
@@ -1566,11 +1585,8 @@ export class WecomHarnessBridge {
1566
1585
  }
1567
1586
  if (error?.code === 'turn-stopped') {
1568
1587
  if (streamStarted && streamId) {
1569
- await this.#client.replyStream(
1570
- frame,
1571
- streamId,
1588
+ await streamCtl.finishStream(
1572
1589
  streamContent(streamThinkingText, t('已停止。'), { finish: true }),
1573
- true,
1574
1590
  )
1575
1591
  .catch(() => undefined);
1576
1592
  }
@@ -1594,12 +1610,9 @@ export class WecomHarnessBridge {
1594
1610
  ? `${errorText}\n\n${batchFailureMessage}`
1595
1611
  : errorText;
1596
1612
  try {
1597
- if (streamStarted && streamId) {
1598
- await this.#client.replyStream(
1599
- frame,
1600
- streamId,
1613
+ if (streamStarted && streamId && streamCtl.isAlive()) {
1614
+ await streamCtl.finishStream(
1601
1615
  streamContent(streamThinkingText, visibleError, { finish: true }),
1602
- true,
1603
1616
  );
1604
1617
  } else {
1605
1618
  await this.#sendImmediate(frame, chatId, visibleError);
@@ -1609,6 +1622,7 @@ export class WecomHarnessBridge {
1609
1622
  this.#logger.error?.('[dsh-im:wecom] failed to send the safe error reply');
1610
1623
  }
1611
1624
  } finally {
1625
+ streamCtl?.stop();
1612
1626
  await Promise.allSettled([
1613
1627
  this.#cancelPendingInteraction(key),
1614
1628
  this.#approvals.closeRoute(key),
@@ -35,9 +35,12 @@ export class WecomRuntime {
35
35
  #logger;
36
36
  #diagnostics;
37
37
  #replyTimeoutMs;
38
+ #streamRotateAfterMs;
39
+ #streamSilentProbeMs;
38
40
  #connectTimeoutMs;
39
41
  #maxReconnectAttempts;
40
42
  #createClient;
43
+ #createBridge;
41
44
  #status = createWecomRuntimeStatus();
42
45
  #client = null;
43
46
  #bridge = null;
@@ -54,9 +57,12 @@ export class WecomRuntime {
54
57
  accessPolicy,
55
58
  logger = console,
56
59
  replyTimeoutMs = 600_000,
60
+ streamRotateAfterMs = 510_000,
61
+ streamSilentProbeMs = 120_000,
57
62
  connectTimeoutMs = 20_000,
58
63
  maxReconnectAttempts = 10,
59
64
  createClient = (options) => new WSClient(options),
65
+ createBridge = (options) => new WecomHarnessBridge(options),
60
66
  }) {
61
67
  if (!config || !secret || !harness || !state) {
62
68
  throw new TypeError('WecomRuntime requires config, secret, Harness, and state');
@@ -68,7 +74,10 @@ export class WecomRuntime {
68
74
  this.#contextEnhancement = contextEnhancement;
69
75
  this.#accessPolicy = accessPolicy;
70
76
  this.#logger = logger; this.#diagnostics = createConnectionDiagnostics({ channel: 'wecom', logger });
77
+ this.#createBridge = createBridge;
71
78
  this.#replyTimeoutMs = replyTimeoutMs;
79
+ this.#streamRotateAfterMs = streamRotateAfterMs;
80
+ this.#streamSilentProbeMs = streamSilentProbeMs;
72
81
  this.#connectTimeoutMs = connectTimeoutMs;
73
82
  this.#maxReconnectAttempts = maxReconnectAttempts;
74
83
  this.#createClient = createClient;
@@ -119,7 +128,7 @@ export class WecomRuntime {
119
128
  throw new TypeError('Enterprise WeChat client factory returned an invalid client');
120
129
  }
121
130
  this.#client = client;
122
- this.#bridge = new WecomHarnessBridge({
131
+ this.#bridge = this.#createBridge({
123
132
  client,
124
133
  harness: this.#harness,
125
134
  state: this.#state,
@@ -128,6 +137,8 @@ export class WecomRuntime {
128
137
  status: this.#status,
129
138
  logger: this.#logger,
130
139
  replyTimeoutMs: this.#replyTimeoutMs,
140
+ streamRotateAfterMs: this.#streamRotateAfterMs,
141
+ streamSilentProbeMs: this.#streamSilentProbeMs,
131
142
  signal,
132
143
  });
133
144