@xmanrui/dsh-im 5.5.0 → 5.6.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 (50) hide show
  1. package/README.en.md +2 -2
  2. package/README.md +2 -2
  3. package/lib/client.js +309 -243
  4. package/lib/index.js +307 -307
  5. package/package.json +4 -4
  6. package/plugin-src/client/channels/dingtalk/index.js +12 -9
  7. package/plugin-src/client/channels/feishu/group-settings.js +5 -5
  8. package/plugin-src/client/channels/feishu/index.js +33 -25
  9. package/plugin-src/client/channels/qq/index.js +5 -5
  10. package/plugin-src/client/channels/shared/token-api.js +12 -7
  11. package/plugin-src/client/channels/shared/token-channel.js +18 -11
  12. package/plugin-src/client/channels/wecom/index.js +5 -5
  13. package/plugin-src/client/channels/wecom-app/index.js +3 -3
  14. package/plugin-src/client/channels/weixin/index.js +2 -2
  15. package/plugin-src/client/channels/whatsapp/index.js +6 -6
  16. package/plugin-src/client/credential-binding.js +3 -2
  17. package/plugin-src/client/delivery-settings.js +3 -3
  18. package/plugin-src/client/ds-bot-settings.js +3 -3
  19. package/plugin-src/client/global-settings.js +5 -2
  20. package/plugin-src/client/i18n-ru.js +56 -34
  21. package/plugin-src/client/i18n.js +210 -124
  22. package/scripts/verify-package.mjs +1 -1
  23. package/src/channels/dingtalk/dingtalk-bridge.mjs +1 -0
  24. package/src/channels/feishu/bridge.mjs +25 -3
  25. package/src/channels/feishu/feishu-cards.mjs +1 -45
  26. package/src/channels/feishu/slash-command-panel.mjs +2 -5
  27. package/src/channels/qq/qq-bridge.mjs +1 -0
  28. package/src/channels/shared/bot-workspace-store.mjs +1 -1
  29. package/src/channels/shared/command-catalog.mjs +8 -5
  30. package/src/channels/shared/delivery-failure-log.mjs +111 -0
  31. package/src/channels/shared/diagnostic-details.mjs +1 -0
  32. package/src/channels/shared/harness-client.mjs +12 -6
  33. package/src/channels/shared/i18n-en/shared-b.mjs +5 -0
  34. package/src/channels/shared/i18n-en/shared-c.mjs +3 -0
  35. package/src/channels/shared/i18n-en/telegram.mjs +6 -0
  36. package/src/channels/shared/i18n-ru/shared-b.mjs +5 -0
  37. package/src/channels/shared/i18n-ru/shared-c.mjs +3 -0
  38. package/src/channels/shared/i18n-ru/telegram.mjs +5 -0
  39. package/src/channels/shared/message-failure.mjs +5 -2
  40. package/src/channels/shared/session-error.mjs +8 -0
  41. package/src/channels/shared/text-harness-bridge.mjs +201 -3
  42. package/src/channels/shared/workspace-command.mjs +41 -16
  43. package/src/channels/shared/workspace-labels.mjs +65 -0
  44. package/src/channels/shared/workspace-session.mjs +24 -4
  45. package/src/channels/telegram/telegram-api.mjs +31 -2
  46. package/src/channels/telegram/telegram-bridge.mjs +58 -0
  47. package/src/channels/telegram/telegram-runtime.mjs +85 -3
  48. package/src/channels/wecom/wecom-bridge.mjs +1 -0
  49. package/src/channels/wecom-app/wecom-app-bridge.mjs +1 -0
  50. package/src/channels/weixin/weixin-bridge.mjs +1 -0
@@ -1,5 +1,7 @@
1
1
  import { harnessQuestionText } from '../shared/harness-question.mjs';
2
+ import { t } from '../shared/i18n.mjs';
2
3
  import { TextHarnessBridge, createTextBridgeStatus } from '../shared/text-harness-bridge.mjs';
4
+ import { workspaceLabels } from '../shared/workspace-labels.mjs';
3
5
 
4
6
  export const TELEGRAM_DESCRIPTOR = Object.freeze({
5
7
  key: 'telegram',
@@ -71,11 +73,67 @@ export const TELEGRAM_INTERACTION_CARD = Object.freeze({
71
73
  parse: parseTelegramCardCallback,
72
74
  });
73
75
 
76
+ /**
77
+ * One keyboard row per workspace. Rows stack with no pagination: beyond this
78
+ * the phone view becomes a wall of buttons, so the bridge falls back to the
79
+ * text list that already answers `/ws <index>` (explicit degradation).
80
+ */
81
+ const MAX_WORKSPACE_BUTTONS = 20;
82
+
83
+ /**
84
+ * Encode one workspace button. The path cannot ride in `callback_data` (64-byte
85
+ * budget), so the press carries a presentation nonce plus the index into the
86
+ * snapshot the bridge cached when it sent the card. A stale card therefore
87
+ * cannot switch to a workspace that a fresher list no longer contains.
88
+ */
89
+ function workspaceCardCallbackData(nonce, index) {
90
+ // Versioned namespace (upstream convention, cf. openclaw tga1:/tgq1:): a future
91
+ // payload change can tell an old card apart instead of mis-parsing it.
92
+ return `w1|${nonce}|${index}`;
93
+ }
94
+
95
+ /** Parse a press produced by {@link workspaceCardCallbackData}; null for foreign payloads. */
96
+ export function parseTelegramWorkspaceCallback(data) {
97
+ const match = typeof data === 'string'
98
+ ? /^w1\|([A-Za-z0-9_-]{1,16})\|(\d{1,3})$/u.exec(data)
99
+ : null;
100
+ if (!match) return null;
101
+ return { nonce: match[1], index: Number(match[2]) };
102
+ }
103
+
104
+ /**
105
+ * Inline-keyboard presentation for the merged `/ws` listing.
106
+ *
107
+ * The card text is deliberately state-free: after a press only the keyboard is
108
+ * replaced (`editMessageReplyMarkup`), so a "current: …" sentence in the body
109
+ * would immediately lie about which workspace is current.
110
+ */
111
+ export const TELEGRAM_WORKSPACE_CARD = Object.freeze({
112
+ render(listing, { nonce } = {}) {
113
+ if (typeof nonce !== 'string' || !nonce) return null;
114
+ const paths = Array.isArray(listing?.paths) ? listing.paths : [];
115
+ if (paths.length === 0 || paths.length > MAX_WORKSPACE_BUTTONS) return null;
116
+ const current = typeof listing?.current === 'string' ? listing.current : null;
117
+ const labels = workspaceLabels(paths, listing?.titles ?? {});
118
+ return {
119
+ text: t('选择要切换的工作区(共 {count} 个):', { count: paths.length }),
120
+ markup: {
121
+ inline_keyboard: paths.map((path, index) => [{
122
+ text: `${path === current ? '✓ ' : ''}${index + 1}. ${labels[index].label}`,
123
+ callback_data: workspaceCardCallbackData(nonce, index),
124
+ }]),
125
+ },
126
+ };
127
+ },
128
+ parse: parseTelegramWorkspaceCallback,
129
+ });
130
+
74
131
  export class TelegramHarnessBridge extends TextHarnessBridge {
75
132
  constructor(options) {
76
133
  super({
77
134
  descriptor: TELEGRAM_DESCRIPTOR,
78
135
  interactionCard: TELEGRAM_INTERACTION_CARD,
136
+ workspaceCard: TELEGRAM_WORKSPACE_CARD,
79
137
  ...options,
80
138
  });
81
139
  }
@@ -454,12 +454,56 @@ function telegramFailure(error) {
454
454
  };
455
455
  }
456
456
 
457
- function deliveryResult(presentation, providerMessageIds, deliveryOutcome = 'sent', reason) {
457
+ function deliveryResult(presentation, providerMessageIds, deliveryOutcome = 'sent', reason, evidence = null) {
458
458
  return {
459
459
  presentation,
460
460
  providerMessageIds: [...new Set(providerMessageIds)],
461
461
  deliveryOutcome,
462
462
  ...(reason ? { reason } : {}),
463
+ ...(evidence ? { deliveryEvidence: { ...evidence, deliveryOutcome } } : {}),
464
+ };
465
+ }
466
+
467
+ /**
468
+ * What the send itself observed, kept next to the delivery result.
469
+ *
470
+ * The notice a user sees carries a reference id that used to be the only trace
471
+ * of an unconfirmed delivery. This record travels with the result so the same
472
+ * id, the attempt history and the text that never got a `message_id` can be
473
+ * written to the local log in one line.
474
+ */
475
+ function deliveryEvidence({ target, parseMode, parts, error, reason = null, failedPart = null }) {
476
+ const attempts = Array.isArray(error?.sendAttempts) ? error.sendAttempts : [];
477
+ return {
478
+ chat_id: target?.chatId ?? null,
479
+ message_thread_id: Number.isInteger(target?.messageThreadId) ? target.messageThreadId : null,
480
+ // The Bot API request carries no `parse_mode` field: a part is either Rich
481
+ // Markdown or bare text, and that is what decides which path failed.
482
+ parse_mode: parseMode,
483
+ provider_reason: (typeof error?.code === 'string' && error.code) || reason || null,
484
+ failed_part: Number.isInteger(failedPart) ? failedPart : null,
485
+ attempts,
486
+ parts,
487
+ };
488
+ }
489
+
490
+ /** One part of an answer, keeping its text only when Telegram never confirmed it. */
491
+ function deliveryPart(index, text, messageId) {
492
+ return {
493
+ index,
494
+ length: typeof text === 'string' ? text.length : 0,
495
+ message_id: messageId ?? null,
496
+ ...(messageId ? {} : { text: typeof text === 'string' ? text : '' }),
497
+ };
498
+ }
499
+
500
+ /** A plain fallback after a rejected Rich part still belongs to the same answer. */
501
+ function mergeEvidence(primary, fallback) {
502
+ if (!primary || !fallback) return fallback ?? primary ?? null;
503
+ return {
504
+ ...fallback,
505
+ provider_reason: fallback.provider_reason ?? primary.provider_reason,
506
+ attempts: [...primary.attempts, ...fallback.attempts],
463
507
  };
464
508
  }
465
509
 
@@ -800,6 +844,7 @@ export class TelegramBotClient {
800
844
  async #sendPlain(target, text) {
801
845
  const chunks = splitTelegramRegularText(text);
802
846
  const providerMessageIds = [];
847
+ const parts = [];
803
848
  for (const [index, chunk] of chunks.entries()) {
804
849
  try {
805
850
  const result = await this.#api.sendMessage({
@@ -811,13 +856,18 @@ export class TelegramBotClient {
811
856
  });
812
857
  const id = telegramMessageId(result);
813
858
  if (id) providerMessageIds.push(id);
859
+ parts.push(deliveryPart(index, chunk, id));
814
860
  } catch (error) {
861
+ parts.push(deliveryPart(index, chunk, null));
815
862
  const failure = telegramFailure(error);
816
863
  return deliveryResult(
817
864
  'text-fallback',
818
865
  providerMessageIds,
819
866
  failure.outcome,
820
867
  failure.reason,
868
+ deliveryEvidence({
869
+ target, parseMode: 'none', parts, error, failedPart: index,
870
+ }),
821
871
  );
822
872
  }
823
873
  }
@@ -825,11 +875,19 @@ export class TelegramBotClient {
825
875
  // reader may still be staring at nothing, so the final answer claims only
826
876
  // what it can prove and lets the caller report the rest.
827
877
  if (providerMessageIds.length !== chunks.length) {
878
+ const unconfirmed = parts.find((part) => !part.message_id) ?? null;
828
879
  return deliveryResult(
829
880
  'text-fallback',
830
881
  providerMessageIds,
831
882
  'unknown',
832
883
  'telegram-message-id-missing',
884
+ deliveryEvidence({
885
+ target,
886
+ parseMode: 'none',
887
+ parts,
888
+ reason: 'telegram-message-id-missing',
889
+ failedPart: unconfirmed?.index ?? null,
890
+ }),
833
891
  );
834
892
  }
835
893
  return deliveryResult('text-fallback', providerMessageIds);
@@ -865,6 +923,7 @@ export class TelegramBotClient {
865
923
  return this.#sendPlain(target, block.text);
866
924
  }
867
925
  const providerMessageIds = [];
926
+ const parts = [];
868
927
  for (const [index, chunk] of chunks.entries()) {
869
928
  try {
870
929
  const result = await this.#api.sendRichMessage({
@@ -876,14 +935,20 @@ export class TelegramBotClient {
876
935
  });
877
936
  const id = telegramMessageId(result);
878
937
  if (id) providerMessageIds.push(id);
938
+ parts.push(deliveryPart(index, chunk.source ?? chunk.markdown, id));
879
939
  } catch (error) {
940
+ parts.push(deliveryPart(index, chunk.source ?? chunk.markdown, null));
880
941
  const failure = telegramFailure(error);
942
+ const evidence = deliveryEvidence({
943
+ target, parseMode: 'rich_markdown', parts, error, failedPart: index,
944
+ });
881
945
  if (failure.outcome === 'unknown') {
882
946
  return deliveryResult(
883
947
  'telegram-rich-final',
884
948
  providerMessageIds,
885
949
  'unknown',
886
950
  failure.reason,
951
+ evidence,
887
952
  );
888
953
  }
889
954
  const remaining = chunks.slice(index).map((part) => part.source).join('');
@@ -891,22 +956,35 @@ export class TelegramBotClient {
891
956
  ...target,
892
957
  replyToMessageId: index === 0 ? target.replyToMessageId : undefined,
893
958
  }, remaining);
959
+ // A successful fallback is a delivered answer; only an unconfirmed one
960
+ // needs the Rich rejection in the log as well.
894
961
  return deliveryResult(
895
962
  fallback.presentation,
896
963
  [...providerMessageIds, ...fallback.providerMessageIds],
897
964
  fallback.deliveryOutcome,
898
965
  fallback.reason,
966
+ fallback.deliveryOutcome === 'sent'
967
+ ? null
968
+ : mergeEvidence(evidence, fallback.deliveryEvidence),
899
969
  );
900
970
  }
901
971
  }
902
972
  // Same proof as the plain path: a rich send that never returned an id
903
973
  // cannot be reported as delivered.
904
974
  if (providerMessageIds.length !== chunks.length) {
975
+ const unconfirmed = parts.find((part) => !part.message_id) ?? null;
905
976
  return deliveryResult(
906
977
  'telegram-rich-final',
907
978
  providerMessageIds,
908
979
  'unknown',
909
980
  'telegram-message-id-missing',
981
+ deliveryEvidence({
982
+ target,
983
+ parseMode: 'rich_markdown',
984
+ parts,
985
+ reason: 'telegram-message-id-missing',
986
+ failedPart: unconfirmed?.index ?? null,
987
+ }),
910
988
  );
911
989
  }
912
990
  return deliveryResult('telegram-rich-final', providerMessageIds);
@@ -1245,14 +1323,18 @@ export class TelegramBotClient {
1245
1323
  if (result?.deliveryOutcome === 'unknown') {
1246
1324
  // 结果未知(超时等):可能已送达,重发会造成重复,保留 unknown 状态。
1247
1325
  remember(result.providerMessageIds);
1248
- return deliveryResult('telegram-thinking', providerMessageIds, 'unknown', result.reason);
1326
+ return deliveryResult(
1327
+ 'telegram-thinking', providerMessageIds, 'unknown', result.reason, result.deliveryEvidence,
1328
+ );
1249
1329
  }
1250
1330
  // 明确失败:该分片确认未送达。纯文本分片再走一次 plain 重发
1251
1331
  // (markdown 分片的 plain 回退已在 #sendRich 内尝试过),只重发
1252
1332
  // 尚未发送的尾部内容,已成功的分片不重复。
1253
1333
  remember(result?.providerMessageIds);
1254
1334
  if (format === 'markdown') {
1255
- return deliveryResult('telegram-thinking', providerMessageIds, 'failed', result.reason);
1335
+ return deliveryResult(
1336
+ 'telegram-thinking', providerMessageIds, 'failed', result.reason, result.deliveryEvidence,
1337
+ );
1256
1338
  }
1257
1339
  warnFailure('final answer chunk', new Error('chunk definitively rejected'));
1258
1340
  try {
@@ -1463,6 +1463,7 @@ export class WecomHarnessBridge {
1463
1463
  await this.#state.markSeen(messageId);
1464
1464
  promptRecorded = true;
1465
1465
  const { answer, artifacts = [] } = await askInWorkspaceSession({
1466
+ onNotice: (notice) => this.#sendActive(chatId, notice),
1466
1467
  deferredDelivery: () => ({ coordinator: this.#deferred, target: { chatId } }),
1467
1468
  harness: this.#harness,
1468
1469
  state: this.#state,
@@ -633,6 +633,7 @@ export class WecomAppBridge {
633
633
  await this.#state.markSeen(messageId);
634
634
  promptRecorded = true;
635
635
  ({ answer, artifacts = [] } = await askInWorkspaceSession({
636
+ onNotice: (notice) => this.#send(sender, notice),
636
637
  deferredDelivery: () => ({ coordinator: this.#deferred, target: { toUserId: sender } }),
637
638
  harness: this.#harness,
638
639
  state: this.#state,
@@ -869,6 +869,7 @@ export class WeixinHarnessBridge {
869
869
  await this.#state.markSeen(messageId);
870
870
  promptRecorded = true;
871
871
  ({ answer, artifacts = [] } = await askInWorkspaceSession({
872
+ onNotice: (notice) => this.#send(sender, notice, contextToken, runId, { stopTyping: false }),
872
873
  deferredDelivery: () => ({ coordinator: this.#deferred, target: { toUserId: sender } }),
873
874
  harness: this.#harness,
874
875
  state: this.#state,