@xmanrui/dsh-im 5.8.0 β†’ 5.9.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 (58) hide show
  1. package/README.en.md +42 -7
  2. package/README.md +53 -7
  3. package/docs/preset-workspaces.md +37 -0
  4. package/lib/client.js +34 -2
  5. package/lib/index.js +329 -326
  6. package/package.json +3 -1
  7. package/plugin-src/client/connection-error.js +1 -1
  8. package/plugin-src/host/channels/dingtalk/production.mjs +2 -1
  9. package/plugin-src/host/channels/feishu/production.mjs +2 -1
  10. package/plugin-src/host/channels/matrix/production.mjs +2 -1
  11. package/plugin-src/host/channels/qq/production.mjs +2 -1
  12. package/plugin-src/host/channels/shared/binding-events.mjs +130 -0
  13. package/plugin-src/host/channels/shared/production.mjs +32 -3
  14. package/plugin-src/host/channels/slack/production.mjs +2 -1
  15. package/plugin-src/host/channels/wecom/production.mjs +2 -1
  16. package/plugin-src/host/channels/wecom-app/production.mjs +2 -1
  17. package/plugin-src/host/channels/weixin/production.mjs +2 -1
  18. package/plugin-src/host/channels/whatsapp/production.mjs +2 -1
  19. package/plugin-src/host/delivery-adapter.mjs +122 -1
  20. package/plugin-src/host/delivery-service.mjs +40 -0
  21. package/plugin-src/host/ds-bot-client.mjs +28 -2
  22. package/plugin-src/host/index.mjs +24 -1
  23. package/plugin-src/host/modern-harness-api.mjs +3 -0
  24. package/scripts/telegram-menu-scopes.mjs +57 -0
  25. package/scripts/verify-interface-language.mjs +60 -15
  26. package/src/channels/dingtalk/dingtalk-bridge.mjs +2 -3
  27. package/src/channels/dingtalk/dingtalk-menu.mjs +4 -5
  28. package/src/channels/email/transports/imap-smtp.mjs +21 -11
  29. package/src/channels/feishu/bridge.mjs +46 -10
  30. package/src/channels/feishu/feishu-cards.mjs +96 -25
  31. package/src/channels/qq/qq-menu.mjs +3 -4
  32. package/src/channels/shared/action-card.mjs +331 -0
  33. package/src/channels/shared/bot-workspace-store.mjs +123 -8
  34. package/src/channels/shared/command-catalog.mjs +20 -5
  35. package/src/channels/shared/conversation/describe.mjs +116 -0
  36. package/src/channels/shared/conversation-state-store.mjs +62 -0
  37. package/src/channels/shared/diagnostic-details.mjs +38 -0
  38. package/src/channels/shared/harness-approval.mjs +58 -13
  39. package/src/channels/shared/harness-client.mjs +13 -0
  40. package/src/channels/shared/harness-session-binding.mjs +13 -1
  41. package/src/channels/shared/i18n-en/feishu.mjs +6 -0
  42. package/src/channels/shared/i18n-en/shared-a.mjs +9 -0
  43. package/src/channels/shared/i18n-en/telegram.mjs +14 -0
  44. package/src/channels/shared/i18n-ru/feishu.mjs +6 -0
  45. package/src/channels/shared/i18n-ru/shared-a.mjs +9 -0
  46. package/src/channels/shared/i18n-ru/telegram.mjs +14 -0
  47. package/src/channels/shared/model-command.mjs +5 -0
  48. package/src/channels/shared/preset-workspaces.mjs +48 -0
  49. package/src/channels/shared/qr-encoding-error.mjs +8 -2
  50. package/src/channels/shared/status-command.mjs +159 -0
  51. package/src/channels/shared/text-harness-bridge.mjs +86 -4
  52. package/src/channels/shared/workspace-command.mjs +20 -12
  53. package/src/channels/shared/workspace-session.mjs +30 -5
  54. package/src/channels/telegram/approval-card.mjs +379 -0
  55. package/src/channels/telegram/telegram-api.mjs +10 -1
  56. package/src/channels/telegram/telegram-bridge.mjs +7 -0
  57. package/src/channels/telegram/telegram-runtime.mjs +125 -13
  58. package/src/channels/wecom/wecom-bridge.mjs +5 -3
@@ -2,6 +2,7 @@ import { harnessQuestionText } from '../shared/harness-question.mjs';
2
2
  import { t } from '../shared/i18n.mjs';
3
3
  import { TextHarnessBridge, createTextBridgeStatus } from '../shared/text-harness-bridge.mjs';
4
4
  import { workspaceLabels } from '../shared/workspace-labels.mjs';
5
+ import { TelegramApprovalCard } from './approval-card.mjs';
5
6
 
6
7
  export const TELEGRAM_DESCRIPTOR = Object.freeze({
7
8
  key: 'telegram',
@@ -130,11 +131,17 @@ export const TELEGRAM_WORKSPACE_CARD = Object.freeze({
130
131
 
131
132
  export class TelegramHarnessBridge extends TextHarnessBridge {
132
133
  constructor(options) {
134
+ // Approval cards are on by default; `approvalCard: null` keeps the
135
+ // plain-text approval flow for a caller that wants it (tests/tools).
136
+ // The controller owns the per-conversation nonce registry.
133
137
  super({
134
138
  descriptor: TELEGRAM_DESCRIPTOR,
135
139
  interactionCard: TELEGRAM_INTERACTION_CARD,
136
140
  workspaceCard: TELEGRAM_WORKSPACE_CARD,
137
141
  ...options,
142
+ approvalCard: options && Object.hasOwn(options, 'approvalCard')
143
+ ? options.approvalCard
144
+ : new TelegramApprovalCard({ logger: options?.logger }),
138
145
  });
139
146
  }
140
147
  }
@@ -35,11 +35,22 @@ function commandMenuEntries(catalog, translate) {
35
35
  .map((entry) => ({
36
36
  command: entry.name,
37
37
  description: translate(entry.description ?? item.description),
38
+ // A private-only command's aliases are private-only too, so the group
39
+ // scope never advertises a command it cannot run there.
40
+ privateOnly: (entry.privateOnly ?? item.privateOnly) === true,
38
41
  })));
39
42
  }
40
43
 
44
+ function publicMenuCommands(entries) {
45
+ return entries.map(({ command, description }) => ({ command, description }));
46
+ }
47
+
48
+ /**
49
+ * Flat, scope-agnostic menu (direct-chat superset) used by help/tests and kept
50
+ * as a stable public shape. Runtime registration uses the scoped lists below.
51
+ */
41
52
  export function telegramCommandMenu(catalog = SHARED_COMMAND_CATALOG) {
42
- const commands = commandMenuEntries(catalog, t);
53
+ const commands = publicMenuCommands(commandMenuEntries(catalog, t));
43
54
  validateTelegramCommands(commands, { allowEmpty: true });
44
55
  return commands;
45
56
  }
@@ -47,9 +58,45 @@ export function telegramCommandMenu(catalog = SHARED_COMMAND_CATALOG) {
47
58
  // Retain the source-language export for existing consumers, derived from the
48
59
  // catalog. Runtime synchronization always generates a fresh, localized list.
49
60
  export const TELEGRAM_COMMAND_MENU = Object.freeze(
50
- commandMenuEntries(SHARED_COMMAND_CATALOG, (text) => text).map((item) => Object.freeze(item)),
61
+ publicMenuCommands(commandMenuEntries(SHARED_COMMAND_CATALOG, (text) => text))
62
+ .map((item) => Object.freeze(item)),
51
63
  );
52
64
 
65
+ /**
66
+ * Telegram resolves the commands shown in a chat from the most specific scope
67
+ * that applies: a chat scope, then AllPrivateChats / AllGroupChats, then the
68
+ * default scope. Both lists are registered without a language code, so one
69
+ * menu covers every client language (the labels follow the host language).
70
+ */
71
+ export const TELEGRAM_COMMAND_MENU_SCOPES = Object.freeze({
72
+ private: Object.freeze({ type: 'all_private_chats' }),
73
+ group: Object.freeze({ type: 'all_group_chats' }),
74
+ });
75
+
76
+ /**
77
+ * The two menus the runtime publishes. Private chats receive every command;
78
+ * groups drop commands whose handlers only accept direct chats (history,
79
+ * batch, send, cancel). Hiding a command from a menu never changes the
80
+ * handler's own permission checks.
81
+ */
82
+ export function telegramCommandMenuScopes(catalog = SHARED_COMMAND_CATALOG) {
83
+ const entries = commandMenuEntries(catalog, t);
84
+ const privateCommands = publicMenuCommands(entries);
85
+ const groupCommands = publicMenuCommands(entries.filter((entry) => !entry.privateOnly));
86
+ validateTelegramCommands(privateCommands, { allowEmpty: true });
87
+ validateTelegramCommands(groupCommands, { allowEmpty: true });
88
+ return Object.freeze([
89
+ Object.freeze({
90
+ scope: TELEGRAM_COMMAND_MENU_SCOPES.private,
91
+ commands: Object.freeze(privateCommands),
92
+ }),
93
+ Object.freeze({
94
+ scope: TELEGRAM_COMMAND_MENU_SCOPES.group,
95
+ commands: Object.freeze(groupCommands),
96
+ }),
97
+ ]);
98
+ }
99
+
53
100
  function mentionedUsername(message, username) {
54
101
  if (!username) return false;
55
102
  return [
@@ -274,6 +321,7 @@ export function normalizeTelegramCallback(update, { botId } = {}) {
274
321
  senderName: [callback.from?.first_name, callback.from?.last_name]
275
322
  .filter((value) => typeof value === 'string' && value.trim())
276
323
  .map((value) => value.trim()).join(' ') || callback.from?.username,
324
+ senderUsername: callback.from?.username,
277
325
  conversationTitle: direct ? undefined : message.chat?.title,
278
326
  chatId: String(chatId),
279
327
  threadId: messageThreadId === undefined ? undefined : String(messageThreadId),
@@ -399,6 +447,7 @@ export function normalizeTelegramUpdate(update, {
399
447
  senderName: [message.from?.first_name, message.from?.last_name]
400
448
  .filter((value) => typeof value === 'string' && value.trim())
401
449
  .map((value) => value.trim()).join(' ') || message.from?.username,
450
+ senderUsername: message.from?.username,
402
451
  conversationTitle: direct ? undefined : message.chat?.title,
403
452
  chatId: String(chatId),
404
453
  threadId: messageThreadId === undefined ? undefined : String(messageThreadId),
@@ -714,6 +763,11 @@ export function formatThinkingLine(text) {
714
763
  return `πŸ’­ ${truncateCapped(firstParagraph, THINKING_LINE_LIMIT)}`;
715
764
  }
716
765
 
766
+ /** Telegram reports an idempotent edit with HTTP 400; treat it as success. */
767
+ function isMessageNotModified(error) {
768
+ return /message is not modified/iu.test(error?.message ?? '');
769
+ }
770
+
717
771
  export class TelegramBotClient {
718
772
  #api;
719
773
  #signal;
@@ -777,12 +831,62 @@ export class TelegramBotClient {
777
831
 
778
832
  /** Replace a delivered card's keyboard in place; an empty keyboard removes it. */
779
833
  async updateInteractionCard(target, providerMessageId, { markup } = {}) {
780
- await this.#api.editMessageReplyMarkup({
781
- chatId: target.chatId,
782
- messageId: Number(providerMessageId),
783
- replyMarkup: markup,
784
- signal: this.#signal,
785
- });
834
+ try {
835
+ await this.#api.editMessageReplyMarkup({
836
+ chatId: target.chatId,
837
+ messageId: Number(providerMessageId),
838
+ replyMarkup: markup,
839
+ signal: this.#signal,
840
+ });
841
+ } catch (error) {
842
+ // Clearing a keyboard that is already gone is the desired end state.
843
+ if (!isMessageNotModified(error)) throw error;
844
+ }
845
+ }
846
+
847
+ /**
848
+ * Rewrite a delivered card's text and keyboard in one `editMessageText` call.
849
+ *
850
+ * Telegram answers "message is not modified" when the client already holds the
851
+ * exact payload (a duplicate resolution); that is success, not failure. If a
852
+ * Bot API deployment refuses the combined text+keyboard edit, degrade to the
853
+ * two calls this client already supports β€” the interface stays the same.
854
+ */
855
+ async editInteractionCard(target, providerMessageId, { text, markup } = {}) {
856
+ const messageId = Number(providerMessageId);
857
+ if (!Number.isSafeInteger(messageId)) {
858
+ throw new TypeError('Telegram card message id must be a safe integer');
859
+ }
860
+ try {
861
+ return await this.#api.editMessageText({
862
+ chatId: target.chatId,
863
+ messageId,
864
+ text,
865
+ replyMarkup: markup,
866
+ signal: this.#signal,
867
+ });
868
+ } catch (error) {
869
+ if (isMessageNotModified(error)) return { unchanged: true };
870
+ if (error?.code !== 'telegram-400' || markup === undefined) throw error;
871
+ // Clear the dead keyboard first so a stale card cannot keep submitting.
872
+ try {
873
+ await this.#api.editMessageReplyMarkup({
874
+ chatId: target.chatId,
875
+ messageId,
876
+ replyMarkup: markup,
877
+ signal: this.#signal,
878
+ });
879
+ } catch (markupError) {
880
+ if (!isMessageNotModified(markupError)) throw markupError;
881
+ }
882
+ await this.#api.editMessageText({
883
+ chatId: target.chatId,
884
+ messageId,
885
+ text,
886
+ signal: this.#signal,
887
+ });
888
+ return { fallback: true };
889
+ }
786
890
  }
787
891
 
788
892
  /** Stop the client-side spinner on a pressed button. */
@@ -1630,12 +1734,20 @@ export class TelegramRuntime {
1630
1734
  }
1631
1735
  }
1632
1736
 
1633
- // Both operations use the existing default scope and language. Sending the
1634
- // full list replaces old entries; an empty catalog clears that list.
1737
+ // Commands are registered per BotCommandScope, and the scopes overlap: a
1738
+ // default-scope list from an earlier version would keep applying wherever a
1739
+ // scoped list is missing and could advertise private-only commands in groups.
1740
+ // The scoped lists are written first and only then is the legacy default
1741
+ // scope cleared, so a failed scoped write leaves the previous (default-scope)
1742
+ // menu usable until the next reconnect instead of blanking the menu. No
1743
+ // language code is sent, so one list covers all client languages.
1635
1744
  async #sendCommandMenu(api, signal) {
1636
- const commands = telegramCommandMenu(this.#commandCatalog);
1637
- if (commands.length > 0) await api.setMyCommands({ commands, signal });
1638
- else await api.deleteMyCommands({ signal });
1745
+ const menus = telegramCommandMenuScopes(this.#commandCatalog);
1746
+ for (const { scope, commands } of menus) {
1747
+ if (commands.length > 0) await api.setMyCommands({ commands, scope, signal });
1748
+ else await api.deleteMyCommands({ scope, signal });
1749
+ }
1750
+ await api.deleteMyCommands({ signal });
1639
1751
  }
1640
1752
 
1641
1753
  /**
@@ -37,7 +37,7 @@ import {
37
37
  isPresetCommand,
38
38
  runPresetCommand,
39
39
  } from '../shared/preset-command.mjs';
40
- import { runWorkspaceCommand, workspacePathSnapshot } from '../shared/workspace-command.mjs';
40
+ import { conversationSessionWorkspace, runWorkspaceCommand, workspacePathSnapshot } from '../shared/workspace-command.mjs';
41
41
  import { askInWorkspaceSession } from '../shared/workspace-session.mjs';
42
42
  import {
43
43
  captureContextEnhancement,
@@ -660,7 +660,8 @@ export class WecomHarnessBridge {
660
660
  const options = { signal: this.#signal };
661
661
  const settled = await Promise.allSettled([
662
662
  workspacePathSnapshot(this.#harness, options),
663
- this.#harness.listWorkspaceSessions?.(workspace, options),
663
+ conversationSessionWorkspace(this.#harness, key, options)
664
+ .then((path) => this.#harness.listWorkspaceSessions?.(path, options)),
664
665
  (async () => {
665
666
  const session = sessionId ? this.#harness.workspaceSession?.(sessionId, key) : null;
666
667
  return typeof session?.models === 'function'
@@ -852,7 +853,8 @@ export class WecomHarnessBridge {
852
853
  let description = '';
853
854
  if (menu.section === 'sessions') {
854
855
  title = t('πŸ“‹ δΌšθ―εˆ—θ‘¨');
855
- const listed = await this.#harness.listWorkspaceSessions(this.#harness.currentWorkspace(), options);
856
+ const workspace = await conversationSessionWorkspace(this.#harness, key, options);
857
+ const listed = await this.#harness.listWorkspaceSessions(workspace, options);
856
858
  entries = listed.sessions.map((session) => [
857
859
  `${session.sessionId === this.#state.sessionFor(key) ? 'βœ“ ' : ''}${session.title || t('ζš‚ζ— ζ ‡ι’˜')}`,
858
860
  `/session ${session.sessionId}`,