@xmanrui/dsh-im 5.0.2 → 5.1.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 (42) hide show
  1. package/README.en.md +11 -1
  2. package/README.md +11 -1
  3. package/lib/client.js +654 -495
  4. package/lib/index.js +318 -294
  5. package/package.json +1 -1
  6. package/plugin-src/client/channels/feishu/group-settings.js +10 -9
  7. package/plugin-src/client/channels/feishu/index.js +3 -3
  8. package/plugin-src/client/global-settings.js +29 -24
  9. package/plugin-src/client/group-history-settings.js +72 -0
  10. package/plugin-src/client/i18n.js +19 -3
  11. package/plugin-src/client/styles.js +12 -4
  12. package/plugin-src/host/channels/feishu/production.mjs +2 -0
  13. package/plugin-src/host/channels/feishu/rpc.mjs +5 -4
  14. package/plugin-src/host/channels/qq/production.mjs +2 -0
  15. package/plugin-src/host/channels/shared/production.mjs +2 -0
  16. package/plugin-src/host/channels/slack/production.mjs +2 -0
  17. package/plugin-src/host/channels/whatsapp/production.mjs +2 -0
  18. package/plugin-src/host/group-history-rpc.mjs +22 -0
  19. package/plugin-src/host/group-history-runtime.mjs +21 -0
  20. package/plugin-src/host/inbound-ttl-rpc.mjs +6 -2
  21. package/plugin-src/host/index.mjs +4 -1
  22. package/src/channels/discord/discord-runtime.mjs +8 -0
  23. package/src/channels/feishu/bridge.mjs +23 -1
  24. package/src/channels/feishu/feishu-runtime.mjs +5 -0
  25. package/src/channels/feishu/multi-bot-controller.mjs +17 -3
  26. package/src/channels/feishu/plugin-controller.mjs +2 -0
  27. package/src/channels/feishu/repair-manager.mjs +2 -1
  28. package/src/channels/qq/qq-bridge.mjs +19 -0
  29. package/src/channels/qq/qq-runtime.mjs +4 -0
  30. package/src/channels/shared/conversation-state-store.mjs +1 -0
  31. package/src/channels/shared/group-history-access.mjs +133 -0
  32. package/src/channels/shared/group-history-store.mjs +383 -0
  33. package/src/channels/shared/group-history.mjs +56 -0
  34. package/src/channels/shared/harness-client.mjs +3 -0
  35. package/src/channels/shared/i18n-en/feishu.mjs +2 -2
  36. package/src/channels/shared/semantic/artifact.mjs +19 -6
  37. package/src/channels/shared/text-harness-bridge.mjs +1 -0
  38. package/src/channels/slack/manifest.mjs +2 -0
  39. package/src/channels/slack/slack-runtime.mjs +10 -2
  40. package/src/channels/telegram/telegram-runtime.mjs +7 -0
  41. package/src/channels/whatsapp/state-store.mjs +34 -1
  42. package/src/channels/whatsapp/whatsapp-runtime.mjs +42 -3
@@ -1,3 +1,36 @@
1
1
  import { ConversationStateStore } from '../shared/conversation-state-store.mjs';
2
2
 
3
- export class WhatsappStateStore extends ConversationStateStore {}
3
+ const BOT_MESSAGE_LIMIT = 2_000;
4
+
5
+ export class WhatsappStateStore extends ConversationStateStore {
6
+ #botMessageIds = new Set();
7
+
8
+ async load() {
9
+ await super.load();
10
+ const stored = this.extensionState().whatsappBotMessageIds;
11
+ this.#botMessageIds = new Set(Array.isArray(stored)
12
+ ? stored.filter((id) => typeof id === 'string' && id).slice(-BOT_MESSAGE_LIMIT)
13
+ : []);
14
+ return this;
15
+ }
16
+
17
+ isBotMessage(jid, id) {
18
+ return typeof id === 'string' && Boolean(id) && this.#botMessageIds.has(`${jid}:${id}`);
19
+ }
20
+
21
+ async rememberBotMessage(jid, id) {
22
+ if (typeof jid !== 'string' || !jid.endsWith('@g.us')
23
+ || typeof id !== 'string' || !id || this.isBotMessage(jid, id)) return;
24
+ this.#botMessageIds.add(`${jid}:${id}`);
25
+ while (this.#botMessageIds.size > BOT_MESSAGE_LIMIT) {
26
+ this.#botMessageIds.delete(this.#botMessageIds.values().next().value);
27
+ }
28
+ this.extensionState().whatsappBotMessageIds = [...this.#botMessageIds];
29
+ await this.persist();
30
+ }
31
+
32
+ async remove() {
33
+ await super.remove();
34
+ this.#botMessageIds.clear();
35
+ }
36
+ }
@@ -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
 
@@ -311,6 +312,7 @@ function whatsappAccountMatcher(accountJid, aliases) {
311
312
  export function normalizeWhatsappMessage(message, accountJid, {
312
313
  download = downloadMediaMessage,
313
314
  accountAliases = [],
315
+ isBotMessage = () => false,
314
316
  } = {}) {
315
317
  const remoteJid = typeof message?.key?.remoteJid === 'string' ? message.key.remoteJid : '';
316
318
  const alternateRemoteJid = typeof message?.key?.remoteJidAlt === 'string'
@@ -332,8 +334,11 @@ export function normalizeWhatsappMessage(message, accountJid, {
332
334
  const context = messageContext(content);
333
335
  const mentioned = Array.isArray(context?.mentionedJid)
334
336
  && context.mentionedJid.some(matchesAccount);
335
- const replyToSelf = typeof context?.participant === 'string'
336
- && matchesAccount(context.participant);
337
+ // The linked account also sends human messages. Only a known bot message
338
+ // in this chat counts as a reply trigger, not every reply to the account.
339
+ const replyToBot = typeof context?.participant === 'string'
340
+ && matchesAccount(context.participant)
341
+ && isBotMessage(remoteJid, context.stanzaId) === true;
337
342
  const image = whatsappImageSource(message, content, download, { viewOnce });
338
343
  const file = whatsappFileSource(message, content, download);
339
344
  const replyTo = whatsappReplyReference(context);
@@ -352,7 +357,7 @@ export function normalizeWhatsappMessage(message, accountJid, {
352
357
  images: image ? [image] : [],
353
358
  files: file ? [file] : [],
354
359
  ...(replyTo ? { replyTo } : {}),
355
- addressed: !group || fromMe || mentioned || replyToSelf,
360
+ addressed: !group || mentioned || replyToBot,
356
361
  selfChat,
357
362
  replyTarget: { jid: remoteJid, quoted: message, selfChat },
358
363
  reactionTarget: { jid: remoteJid, key: message.key },
@@ -462,6 +467,7 @@ function whatsappArtifactError(error) {
462
467
  export class WhatsappBotClient {
463
468
  #socket;
464
469
  #outboundIds;
470
+ #rememberBotMessage;
465
471
  #signal;
466
472
  #abortController = new AbortController();
467
473
  #mediaUploadTimeoutMs;
@@ -473,9 +479,11 @@ export class WhatsappBotClient {
473
479
  signal,
474
480
  mediaUploadTimeoutMs = WHATSAPP_MEDIA_UPLOAD_TIMEOUT_MS,
475
481
  logger = console,
482
+ rememberBotMessage = async () => {},
476
483
  } = {}) {
477
484
  this.#socket = socket;
478
485
  this.#outboundIds = outboundIds;
486
+ this.#rememberBotMessage = rememberBotMessage;
479
487
  this.#signal = signal
480
488
  ? AbortSignal.any([signal, this.#abortController.signal])
481
489
  : this.#abortController.signal;
@@ -530,6 +538,7 @@ export class WhatsappBotClient {
530
538
  );
531
539
  const result = await waitWithSignal(pending, this.#signal);
532
540
  this.#outboundIds.remember(result?.key?.id);
541
+ await this.#rememberBotMessage(target.jid, edit?.id ?? result?.key?.id ?? messageId);
533
542
  return {
534
543
  ...result,
535
544
  key: { remoteJid: target.jid, fromMe: true, id: messageId, ...result?.key },
@@ -663,6 +672,7 @@ export class WhatsappBotClient {
663
672
  }
664
673
  this.#signal?.throwIfAborted();
665
674
  this.#outboundIds.remember(result?.key?.id);
675
+ await this.#rememberBotMessage(target.jid, result?.key?.id ?? messageId);
666
676
  return result;
667
677
  }
668
678
 
@@ -715,6 +725,7 @@ export class WhatsappRuntime {
715
725
  #harness;
716
726
  #state;
717
727
  #contextEnhancement;
728
+ #groupHistory;
718
729
  #accessPolicy;
719
730
  #logger;
720
731
  #diagnostics;
@@ -735,6 +746,7 @@ export class WhatsappRuntime {
735
746
  harness,
736
747
  state,
737
748
  contextEnhancement,
749
+ groupHistory,
738
750
  accessPolicy,
739
751
  logger = console,
740
752
  replyTimeoutMs = 600_000,
@@ -750,6 +762,7 @@ export class WhatsappRuntime {
750
762
  this.#harness = harness;
751
763
  this.#state = state;
752
764
  this.#contextEnhancement = contextEnhancement;
765
+ this.#groupHistory = groupHistory;
753
766
  this.#accessPolicy = accessPolicy;
754
767
  this.#logger = logger; this.#diagnostics = createConnectionDiagnostics({ channel: 'whatsapp', logger });
755
768
  this.#replyTimeoutMs = replyTimeoutMs;
@@ -800,6 +813,7 @@ export class WhatsappRuntime {
800
813
  onMessage: async (raw, context) => {
801
814
  const linkedAccount = context?.socket?.user;
802
815
  const message = normalizeWhatsappMessage(raw, this.#config.accountJid, {
816
+ isBotMessage: (jid, id) => this.#state.isBotMessage?.(jid, id) === true,
803
817
  accountAliases: [
804
818
  linkedAccount?.id,
805
819
  linkedAccount?.lid,
@@ -811,6 +825,22 @@ export class WhatsappRuntime {
811
825
  }),
812
826
  });
813
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
+ }
814
844
  this.#status.lastCheckedAt = Date.now();
815
845
  await this.#bridge.accept(message);
816
846
  },
@@ -838,6 +868,15 @@ export class WhatsappRuntime {
838
868
  throw new Error('WhatsApp linked account does not match the saved bot');
839
869
  }
840
870
  const client = new WhatsappBotClient(session.socket, outboundIds, {
871
+ rememberBotMessage: async (jid, id) => {
872
+ try {
873
+ await this.#state.rememberBotMessage?.(jid, id);
874
+ } catch (error) {
875
+ // Delivery already succeeded; a bookkeeping failure must not turn
876
+ // it into a failed send that can be retried and duplicated.
877
+ this.#logger.warn?.('[dsh-im:whatsapp] failed to persist bot message identity:', error);
878
+ }
879
+ },
841
880
  signal: controller.signal,
842
881
  mediaUploadTimeoutMs: this.#mediaUploadTimeoutMs,
843
882
  logger: this.#logger,