@xmanrui/dsh-im 5.0.3 → 5.2.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 (44) hide show
  1. package/README.en.md +18 -8
  2. package/README.md +18 -8
  3. package/docs/client-integration.md +10 -2
  4. package/lib/client.js +687 -496
  5. package/lib/index.js +318 -294
  6. package/package.json +1 -1
  7. package/plugin-src/client/channels/feishu/group-settings.js +10 -9
  8. package/plugin-src/client/channels/feishu/index.js +3 -3
  9. package/plugin-src/client/global-settings.js +29 -24
  10. package/plugin-src/client/group-history-settings.js +72 -0
  11. package/plugin-src/client/i18n.js +19 -3
  12. package/plugin-src/client/index.js +4 -1
  13. package/plugin-src/client/plugin-page.js +18 -0
  14. package/plugin-src/client/styles.js +13 -4
  15. package/plugin-src/host/channels/feishu/production.mjs +2 -0
  16. package/plugin-src/host/channels/feishu/rpc.mjs +5 -4
  17. package/plugin-src/host/channels/qq/production.mjs +2 -0
  18. package/plugin-src/host/channels/shared/production.mjs +2 -0
  19. package/plugin-src/host/channels/slack/production.mjs +2 -0
  20. package/plugin-src/host/channels/whatsapp/production.mjs +2 -0
  21. package/plugin-src/host/group-history-rpc.mjs +22 -0
  22. package/plugin-src/host/group-history-runtime.mjs +21 -0
  23. package/plugin-src/host/inbound-ttl-rpc.mjs +6 -2
  24. package/plugin-src/host/index.mjs +4 -1
  25. package/src/channels/discord/discord-runtime.mjs +8 -0
  26. package/src/channels/feishu/bridge.mjs +23 -1
  27. package/src/channels/feishu/feishu-runtime.mjs +5 -0
  28. package/src/channels/feishu/multi-bot-controller.mjs +17 -3
  29. package/src/channels/feishu/plugin-controller.mjs +2 -0
  30. package/src/channels/feishu/repair-manager.mjs +2 -1
  31. package/src/channels/qq/qq-bridge.mjs +19 -0
  32. package/src/channels/qq/qq-runtime.mjs +4 -0
  33. package/src/channels/shared/group-history-access.mjs +133 -0
  34. package/src/channels/shared/group-history-store.mjs +383 -0
  35. package/src/channels/shared/group-history.mjs +56 -0
  36. package/src/channels/shared/harness-client.mjs +9 -1
  37. package/src/channels/shared/i18n-en/feishu.mjs +2 -2
  38. package/src/channels/shared/text-harness-bridge.mjs +1 -0
  39. package/src/channels/slack/manifest.mjs +2 -0
  40. package/src/channels/slack/slack-runtime.mjs +10 -2
  41. package/src/channels/telegram/telegram-runtime.mjs +7 -0
  42. package/src/channels/wecom/wecom-runtime.mjs +8 -1
  43. package/src/channels/weixin/weixin-api.mjs +21 -2
  44. package/src/channels/whatsapp/whatsapp-runtime.mjs +20 -0
@@ -21,6 +21,7 @@ const ILINK_CLIENT_VERSION = (2 << 16) | (4 << 8) | 6;
21
21
  const DEFAULT_TIMEOUT_MS = 15_000;
22
22
  const DEFAULT_LONG_POLL_TIMEOUT_MS = 35_000;
23
23
  const WEIXIN_CDN_UPLOAD_RETRIES = 3;
24
+ const WEIXIN_TEXT_SEND_RETRY_DELAYS_MS = [400, 1_200];
24
25
  const WEIXIN_CDN_UPLOAD_IDLE_TIMEOUT_MS = 60_000;
25
26
  const WEIXIN_CDN_UPLOAD_CHUNK_BYTES = 64 * 1024;
26
27
  const WEIXIN_MESSAGE_ID_TIMESTAMP_SHIFT = 22n;
@@ -497,6 +498,24 @@ async function requestJson(fetchImpl, {
497
498
  }
498
499
  }
499
500
 
501
+ // A text reply is the only thing the user gets, so a single connection-level failure must not
502
+ // discard it. Only `network-error` is repeated: it means no HTTP response arrived at all, while
503
+ // `timeout`, `http-error` and `send-rejected` may already have been delivered. iLink does not
504
+ // deduplicate by `client_id`, so the caller keeps one id for every attempt of the same chunk and
505
+ // accepts that a request which reached the service just before the socket died can still arrive twice.
506
+ async function requestTextSend(fetchImpl, createRequest, { signal }) {
507
+ for (let attempt = 0; ; attempt += 1) {
508
+ try {
509
+ return await requestJson(fetchImpl, createRequest());
510
+ } catch (error) {
511
+ const retryDelayMs = WEIXIN_TEXT_SEND_RETRY_DELAYS_MS[attempt];
512
+ if (retryDelayMs === undefined || error?.code !== 'network-error' || signal?.aborted) throw error;
513
+ await new Promise((resolve) => { setTimeout(resolve, retryDelayMs); });
514
+ signal?.throwIfAborted?.();
515
+ }
516
+ }
517
+ }
518
+
500
519
  function validateLoginResponse(value) {
501
520
  if (!value || typeof value !== 'object' || !LOGIN_STATUSES.has(value.status)) {
502
521
  throw new WeixinApiError('invalid-login-status', '微信服务返回了无法识别的扫码状态。');
@@ -741,7 +760,7 @@ export function createWeixinApi({ fetchImpl = fetch, uploadFetchImpl = fetchImpl
741
760
  const content = nonEmptyString(text);
742
761
  if (!recipient || !content) throw new TypeError('toUserId and text are required');
743
762
  const clientId = `dsh-weixin-${randomUUID()}`;
744
- const response = await requestJson(fetchImpl, {
763
+ const response = await requestTextSend(fetchImpl, () => ({
745
764
  method: 'POST',
746
765
  baseUrl,
747
766
  endpoint: 'ilink/bot/sendmessage',
@@ -760,7 +779,7 @@ export function createWeixinApi({ fetchImpl = fetch, uploadFetchImpl = fetchImpl
760
779
  },
761
780
  base_info: baseInfo(),
762
781
  },
763
- });
782
+ }), { signal });
764
783
  const sendRejection = rejectedProviderResponse(response);
765
784
  if (sendRejection) {
766
785
  throw new WeixinApiError(
@@ -1,3 +1,4 @@
1
+ import { captureGroupHistory, groupHistoryEntry } from '../shared/group-history.mjs';
1
2
  import { createConnectionDiagnostics, atConnectionStage } from '../shared/connection-error.mjs';
2
3
  import { createHash, randomBytes } from 'node:crypto';
3
4
 
@@ -724,6 +725,7 @@ export class WhatsappRuntime {
724
725
  #harness;
725
726
  #state;
726
727
  #contextEnhancement;
728
+ #groupHistory;
727
729
  #accessPolicy;
728
730
  #logger;
729
731
  #diagnostics;
@@ -744,6 +746,7 @@ export class WhatsappRuntime {
744
746
  harness,
745
747
  state,
746
748
  contextEnhancement,
749
+ groupHistory,
747
750
  accessPolicy,
748
751
  logger = console,
749
752
  replyTimeoutMs = 600_000,
@@ -759,6 +762,7 @@ export class WhatsappRuntime {
759
762
  this.#harness = harness;
760
763
  this.#state = state;
761
764
  this.#contextEnhancement = contextEnhancement;
765
+ this.#groupHistory = groupHistory;
762
766
  this.#accessPolicy = accessPolicy;
763
767
  this.#logger = logger; this.#diagnostics = createConnectionDiagnostics({ channel: 'whatsapp', logger });
764
768
  this.#replyTimeoutMs = replyTimeoutMs;
@@ -821,6 +825,22 @@ export class WhatsappRuntime {
821
825
  }),
822
826
  });
823
827
  if (!message || outboundIds.has(message.providerMessageId) || !this.#bridge) return;
828
+ let groupName;
829
+ if (this.#groupHistory && message.kind === 'group' && message.addressed) {
830
+ let timeout;
831
+ try {
832
+ groupName = (await Promise.race([
833
+ context?.socket?.groupMetadata?.(message.conversationId),
834
+ new Promise((resolve) => { timeout = setTimeout(resolve, 2500); timeout.unref?.(); }),
835
+ ]))?.subject;
836
+ } catch { /* optional metadata must not block normal replies */ }
837
+ finally { clearTimeout(timeout); }
838
+ }
839
+ if (!hasViewOnceWrapper(raw.message)) {
840
+ await captureGroupHistory(this.#groupHistory, message, groupHistoryEntry('whatsapp', raw, message, { groupName }), {
841
+ logger: this.#logger,
842
+ });
843
+ }
824
844
  this.#status.lastCheckedAt = Date.now();
825
845
  await this.#bridge.accept(message);
826
846
  },