@xmanrui/dsh-im 5.9.1 → 5.11.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 (135) hide show
  1. package/PROACTIVE_DELIVERY.en.md +33 -9
  2. package/PROACTIVE_DELIVERY.md +33 -9
  3. package/README.en.md +42 -7
  4. package/README.md +46 -8
  5. package/bin/dsh-im.mjs +65 -0
  6. package/docs/client-integration.md +4 -0
  7. package/lib/client.js +1735 -203
  8. package/lib/index.js +367 -341
  9. package/package.json +7 -6
  10. package/plugin-src/client/access-policy-settings.js +210 -12
  11. package/plugin-src/client/bot-general-settings.js +44 -3
  12. package/plugin-src/client/delivery-settings.js +2 -0
  13. package/plugin-src/client/delivery-sync-settings.js +269 -0
  14. package/plugin-src/client/global-settings.js +45 -7
  15. package/plugin-src/client/i18n-ru.js +112 -0
  16. package/plugin-src/client/i18n.js +110 -0
  17. package/plugin-src/client/im-channels.js +419 -0
  18. package/plugin-src/client/index.js +50 -2
  19. package/plugin-src/client/model-setting.js +6 -3
  20. package/plugin-src/client/styles.js +23 -7
  21. package/plugin-src/client/update-panel.js +2 -1
  22. package/plugin-src/host/channel-list-rpc.mjs +280 -0
  23. package/plugin-src/host/channels/dingtalk/production.mjs +28 -10
  24. package/plugin-src/host/channels/dingtalk/rpc.mjs +10 -1
  25. package/plugin-src/host/channels/feishu/production.mjs +50 -37
  26. package/plugin-src/host/channels/feishu/rpc.mjs +10 -1
  27. package/plugin-src/host/channels/matrix/production.mjs +28 -10
  28. package/plugin-src/host/channels/matrix/rpc.mjs +10 -0
  29. package/plugin-src/host/channels/qq/production.mjs +30 -11
  30. package/plugin-src/host/channels/qq/rpc.mjs +10 -1
  31. package/plugin-src/host/channels/shared/access-policy-production.mjs +9 -0
  32. package/plugin-src/host/channels/shared/binding-events.mjs +109 -0
  33. package/plugin-src/host/channels/shared/production.mjs +14 -1
  34. package/plugin-src/host/channels/shared/reply-content-rpc.mjs +17 -0
  35. package/plugin-src/host/channels/shared/rpc.mjs +9 -0
  36. package/plugin-src/host/channels/shared/workspace-rpc.mjs +1 -0
  37. package/plugin-src/host/channels/slack/production.mjs +28 -10
  38. package/plugin-src/host/channels/slack/rpc.mjs +10 -0
  39. package/plugin-src/host/channels/telegram/production.mjs +5 -0
  40. package/plugin-src/host/channels/wecom/production.mjs +28 -10
  41. package/plugin-src/host/channels/wecom/rpc.mjs +10 -1
  42. package/plugin-src/host/channels/wecom-app/production.mjs +28 -10
  43. package/plugin-src/host/channels/wecom-app/rpc.mjs +9 -0
  44. package/plugin-src/host/channels/weixin/production.mjs +28 -10
  45. package/plugin-src/host/channels/weixin/rpc.mjs +10 -1
  46. package/plugin-src/host/channels/whatsapp/production.mjs +28 -10
  47. package/plugin-src/host/channels/whatsapp/rpc.mjs +10 -1
  48. package/plugin-src/host/delivery-adapter.mjs +35 -20
  49. package/plugin-src/host/delivery-service.mjs +99 -1
  50. package/plugin-src/host/delivery-suggestions.mjs +31 -1
  51. package/plugin-src/host/delivery-sync-rpc.mjs +100 -0
  52. package/plugin-src/host/harness-connection.mjs +2 -1
  53. package/plugin-src/host/harness-peer.mjs +3 -1
  54. package/plugin-src/host/host-language-rpc.mjs +16 -2
  55. package/plugin-src/host/host-language.mjs +29 -0
  56. package/plugin-src/host/index.mjs +30 -1
  57. package/plugin-src/host/modern-harness-api.mjs +5 -1
  58. package/plugin-src/host/root-scope.mjs +32 -0
  59. package/plugin-src/host/session-sync-coordinator.mjs +290 -122
  60. package/plugin-src/host/session-sync-targets.mjs +71 -0
  61. package/plugin-src/management-rpc.mjs +18 -2
  62. package/scripts/verify-package.mjs +48 -10
  63. package/src/channels/dingtalk/dingtalk-bridge.mjs +50 -6
  64. package/src/channels/dingtalk/state-store.mjs +56 -0
  65. package/src/channels/discord/discord-api.mjs +2 -1
  66. package/src/channels/feishu/bridge.mjs +154 -58
  67. package/src/channels/feishu/feishu-cards.mjs +10 -4
  68. package/src/channels/feishu/feishu-runtime.mjs +11 -3
  69. package/src/channels/feishu/message-utils.mjs +106 -30
  70. package/src/channels/feishu/slash-command-panel.mjs +26 -0
  71. package/src/channels/feishu/state-store.mjs +14 -0
  72. package/src/channels/matrix/matrix-api.mjs +2 -1
  73. package/src/channels/qq/qq-bridge.mjs +114 -14
  74. package/src/channels/qq/state-store.mjs +55 -1
  75. package/src/channels/shared/access-policy.mjs +60 -7
  76. package/src/channels/shared/action-card.mjs +83 -7
  77. package/src/channels/shared/bot-workspace-store.mjs +255 -11
  78. package/src/channels/shared/busy-message-dispatcher.mjs +2 -2
  79. package/src/channels/shared/channel-capabilities.mjs +44 -0
  80. package/src/channels/shared/command-catalog.mjs +68 -3
  81. package/src/channels/shared/command-permission.mjs +6 -2
  82. package/src/channels/shared/conversation-state-store.mjs +16 -0
  83. package/src/channels/shared/deferred-delivery-coordinator.mjs +6 -1
  84. package/src/channels/shared/deferred-delivery.mjs +4 -0
  85. package/src/channels/shared/delivery-sync-settings-store.mjs +86 -0
  86. package/src/channels/shared/delivery-sync-settings.mjs +122 -0
  87. package/src/channels/shared/diagnostic-details.mjs +6 -1
  88. package/src/channels/shared/failure-log-file.mjs +79 -0
  89. package/src/channels/shared/harness-client.mjs +259 -96
  90. package/src/channels/shared/i18n-en/diagnostics.mjs +1 -0
  91. package/src/channels/shared/i18n-en/feishu.mjs +2 -2
  92. package/src/channels/shared/i18n-en/shared-a.mjs +13 -2
  93. package/src/channels/shared/i18n-en/shared-b.mjs +73 -4
  94. package/src/channels/shared/i18n-en/shared-d.mjs +144 -0
  95. package/src/channels/shared/i18n-en/weixin.mjs +4 -0
  96. package/src/channels/shared/i18n-en.mjs +2 -0
  97. package/src/channels/shared/i18n-ru/diagnostics.mjs +1 -0
  98. package/src/channels/shared/i18n-ru/feishu.mjs +2 -2
  99. package/src/channels/shared/i18n-ru/shared-a.mjs +13 -2
  100. package/src/channels/shared/i18n-ru/shared-b.mjs +72 -4
  101. package/src/channels/shared/i18n-ru/shared-d.mjs +144 -0
  102. package/src/channels/shared/i18n-ru/weixin.mjs +4 -0
  103. package/src/channels/shared/i18n-ru.mjs +2 -0
  104. package/src/channels/shared/inbound-access.mjs +10 -0
  105. package/src/channels/shared/interface-language-store.mjs +49 -10
  106. package/src/channels/shared/interface-language.mjs +19 -1
  107. package/src/channels/shared/markdown-chunks.mjs +5 -0
  108. package/src/channels/shared/message-failure.mjs +22 -1
  109. package/src/channels/shared/model-setting.mjs +6 -3
  110. package/src/channels/shared/new-command.mjs +158 -4
  111. package/src/channels/shared/plan-goal-command.mjs +132 -0
  112. package/src/channels/shared/qrcode.mjs +5 -0
  113. package/src/channels/shared/reply-content.mjs +102 -0
  114. package/src/channels/shared/reset-command.mjs +46 -0
  115. package/src/channels/shared/semantic/artifact.mjs +133 -29
  116. package/src/channels/shared/semantic/reply-reference.mjs +18 -3
  117. package/src/channels/shared/session-nav-command.mjs +999 -0
  118. package/src/channels/shared/session-ring-support.mjs +184 -0
  119. package/src/channels/shared/session-ring.mjs +202 -0
  120. package/src/channels/shared/session-title.mjs +11 -0
  121. package/src/channels/shared/status-command.mjs +2 -1
  122. package/src/channels/shared/text-harness-bridge.mjs +166 -7
  123. package/src/channels/shared/workspace-command.mjs +567 -31
  124. package/src/channels/shared/workspace-session.mjs +52 -4
  125. package/src/channels/slack/slack-api.mjs +2 -1
  126. package/src/channels/telegram/config-store.mjs +10 -2
  127. package/src/channels/telegram/telegram-api.mjs +19 -1
  128. package/src/channels/telegram/telegram-rich-message.mjs +17 -5
  129. package/src/channels/telegram/telegram-runtime.mjs +14 -1
  130. package/src/channels/wecom/state-store.mjs +13 -1
  131. package/src/channels/wecom/wecom-bridge.mjs +183 -26
  132. package/src/channels/wecom-app/state-store.mjs +13 -1
  133. package/src/channels/wecom-app/wecom-app-bridge.mjs +44 -4
  134. package/src/channels/weixin/state-store.mjs +13 -1
  135. package/src/channels/weixin/weixin-bridge.mjs +71 -4
@@ -5,9 +5,12 @@ export const ACCESS_POLICY_USER_ID_MAX_LENGTH = 256;
5
5
 
6
6
  const POLICY_KEYS = ['direct', 'group'];
7
7
  const SCOPE_KEYS = ['mode', 'open', 'allowlist'];
8
- const OPEN_KEYS = ['defaultCanExecuteCommands', 'commandPermissionOverrides'];
8
+ const OPEN_KEYS = ['defaultCanExecuteCommands', 'commandPermissionOverrides', 'commandAllowlist'];
9
+ const OPEN_REQUIRED_KEYS = ['defaultCanExecuteCommands', 'commandPermissionOverrides'];
9
10
  const ALLOWLIST_KEYS = ['users'];
10
- const USER_KEYS = ['id', 'canExecuteCommands'];
11
+ const USER_KEYS = ['id', 'canExecuteCommands', 'commandAllowlist'];
12
+ const USER_REQUIRED_KEYS = ['id', 'canExecuteCommands'];
13
+ const COMMAND_ALLOWLIST_LIMIT = 100;
11
14
  const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/;
12
15
 
13
16
  function invalidAccessPolicy(message) {
@@ -23,6 +26,41 @@ function hasExactKeys(input, keys) {
23
26
  && keys.every((key) => Object.hasOwn(input, key));
24
27
  }
25
28
 
29
+ /** Accept required keys plus a bounded set of optional keys. */
30
+ function hasAllowedKeys(input, required, optional = []) {
31
+ if (!input || typeof input !== 'object' || Array.isArray(input)
32
+ || ![Object.prototype, null].includes(Object.getPrototypeOf(input))) return false;
33
+ const keys = Reflect.ownKeys(input);
34
+ return required.every((key) => Object.hasOwn(input, key))
35
+ && keys.every((key) => required.includes(key) || optional.includes(key));
36
+ }
37
+
38
+ /**
39
+ * Canonical command name: first token, optional leading slash, lower case.
40
+ * `/sessionlist --limit 5` and `/SessionList` both become `sessionlist`.
41
+ */
42
+ export function normalizeCommandName(value) {
43
+ if (typeof value !== 'string') return null;
44
+ const first = value.trim().split(/\s+/u)[0];
45
+ if (!first) return null;
46
+ const name = (first.startsWith('/') ? first.slice(1) : first).toLowerCase();
47
+ return /^[a-z0-9][a-z0-9:_-]{0,63}$/u.test(name) ? name : null;
48
+ }
49
+
50
+ function validateCommandAllowlist(input, label) {
51
+ if (input === undefined) return null;
52
+ if (!Array.isArray(input)) throw invalidAccessPolicy(`${label}必须是命令名数组。`);
53
+ if (input.length > COMMAND_ALLOWLIST_LIMIT) {
54
+ throw invalidAccessPolicy(`${label}最多包含 ${COMMAND_ALLOWLIST_LIMIT} 个命令。`);
55
+ }
56
+ const names = input.map((value) => {
57
+ const name = normalizeCommandName(value);
58
+ if (!name) throw invalidAccessPolicy(`${label}包含无效命令名。`);
59
+ return name;
60
+ });
61
+ return Object.freeze([...new Set(names)]);
62
+ }
63
+
26
64
  /** Normalize one opaque channel identity without interpreting its contents. */
27
65
  export function normalizeAccessPolicyUserId(value) {
28
66
  if (typeof value === 'number') {
@@ -40,15 +78,17 @@ export function normalizeAccessPolicyUserId(value) {
40
78
  }
41
79
 
42
80
  function validateAccessPolicyUser(input) {
43
- if (!hasExactKeys(input, USER_KEYS)) {
44
- throw invalidAccessPolicy('用户条目必须包含用户标识和命令权限。');
81
+ if (!hasAllowedKeys(input, USER_REQUIRED_KEYS, USER_KEYS)) {
82
+ throw invalidAccessPolicy('用户条目必须包含用户标识、命令权限和可选的命令白名单。');
45
83
  }
46
84
  if (typeof input.canExecuteCommands !== 'boolean') {
47
85
  throw invalidAccessPolicy('命令权限必须是布尔值。');
48
86
  }
87
+ const commandAllowlist = validateCommandAllowlist(input.commandAllowlist, '用户命令白名单');
49
88
  return Object.freeze({
50
89
  id: normalizeAccessPolicyUserId(input.id),
51
90
  canExecuteCommands: input.canExecuteCommands,
91
+ ...(commandAllowlist === null ? {} : { commandAllowlist }),
52
92
  });
53
93
  }
54
94
 
@@ -62,8 +102,8 @@ function validateUsers(input, { listMessage, duplicateMessage }) {
62
102
  }
63
103
 
64
104
  function validateOpenSettings(input) {
65
- if (!hasExactKeys(input, OPEN_KEYS)) {
66
- throw invalidAccessPolicy('开放模式设置必须完整。');
105
+ if (!hasAllowedKeys(input, OPEN_REQUIRED_KEYS, OPEN_KEYS)) {
106
+ throw invalidAccessPolicy('开放模式设置必须完整(可选的 commandAllowlist 除外)。');
67
107
  }
68
108
  if (typeof input.defaultCanExecuteCommands !== 'boolean') {
69
109
  throw invalidAccessPolicy('开放模式默认命令权限必须是布尔值。');
@@ -74,6 +114,9 @@ function validateOpenSettings(input) {
74
114
  listMessage: '开放模式命令权限覆盖用户必须是数组。',
75
115
  duplicateMessage: '开放模式命令权限覆盖用户不能包含重复的用户标识。',
76
116
  }),
117
+ ...(input.commandAllowlist === undefined
118
+ ? {}
119
+ : { commandAllowlist: validateCommandAllowlist(input.commandAllowlist, '开放模式命令白名单') }),
77
120
  });
78
121
  }
79
122
 
@@ -167,6 +210,7 @@ export function evaluateAccessPolicy(policy, {
167
210
  conversationType,
168
211
  senderIds,
169
212
  isCommand = false,
213
+ command = null,
170
214
  equals = (left, right) => left === right,
171
215
  } = {}) {
172
216
  const normalizedPolicy = normalizeAccessPolicy(policy);
@@ -175,6 +219,7 @@ export function evaluateAccessPolicy(policy, {
175
219
  || typeof isCommand !== 'boolean' || typeof equals !== 'function') {
176
220
  return INVALID_CONTEXT;
177
221
  }
222
+ const normalizedCommand = normalizeCommandName(command);
178
223
  const candidates = (Array.isArray(senderIds) ? senderIds : [senderIds])
179
224
  .flatMap((candidate) => {
180
225
  try {
@@ -204,7 +249,15 @@ export function evaluateAccessPolicy(policy, {
204
249
  ? matchedUsers.every((user) => user.canExecuteCommands)
205
250
  : scope.open.defaultCanExecuteCommands)
206
251
  : matchedUsers.every((user) => user.canExecuteCommands);
207
- if (!canExecuteCommands) return COMMAND_NOT_ALLOWED;
252
+ if (!canExecuteCommands) {
253
+ const scopeAllowsCommand = scope.mode === 'open'
254
+ && normalizedCommand !== null
255
+ && (scope.open.commandAllowlist ?? []).includes(normalizedCommand);
256
+ const usersAllowCommand = matchedUsers.length > 0
257
+ && normalizedCommand !== null
258
+ && matchedUsers.every((user) => user.commandAllowlist?.includes(normalizedCommand) === true);
259
+ if (!scopeAllowsCommand && !usersAllowCommand) return COMMAND_NOT_ALLOWED;
260
+ }
208
261
  }
209
262
  return ALLOWED;
210
263
  }
@@ -135,6 +135,83 @@ export function actionCardStateText(status, texts = {}) {
135
135
  * `maxEntries` cards are remembered (oldest is evicted first) and a new card in
136
136
  * a conversation supersedes the previous live one.
137
137
  */
138
+ /** Telegram truncates button labels itself; we never want to rely on that. */
139
+ export const ACTION_CARD_BUTTON_TEXT_MAX = 60;
140
+
141
+ const UNSAFE_BUTTON_TEXT = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]+/gu;
142
+
143
+ /**
144
+ * Make arbitrary text safe to put on a button.
145
+ *
146
+ * Workspace paths, session titles and preset labels all reach buttons, and any
147
+ * of them can carry control characters or newlines that would break the layout
148
+ * (or, for a bidirectional override, reorder what the user reads). Every card did
149
+ * this slightly differently — one of them skipped the control-character pass —
150
+ * so it lives here now and all of them call it.
151
+ */
152
+ export function safeButtonText(value) {
153
+ if (typeof value !== 'string') return '';
154
+ return value.replace(UNSAFE_BUTTON_TEXT, ' ').replace(/\s+/gu, ' ').trim();
155
+ }
156
+
157
+ /** `safeButtonText` plus the ellipsis every button label needs. */
158
+ export function truncateButtonText(value) {
159
+ const text = safeButtonText(value) || '•';
160
+ return text.length > ACTION_CARD_BUTTON_TEXT_MAX
161
+ ? `${text.slice(0, ACTION_CARD_BUTTON_TEXT_MAX - 1)}…`
162
+ : text;
163
+ }
164
+
165
+ /**
166
+ * Longest we will wait for a rate-limited card operation.
167
+ *
168
+ * Telegram answers a throttled call with `retry_after` and expects the bot to
169
+ * come back then. A card edit is cheap and user-initiated (they just pressed a
170
+ * button), so waiting a few seconds and trying once more is much better than
171
+ * telling them "could not update, send the command again". Anything longer than
172
+ * this is not worth holding the turn open: the caller falls back.
173
+ */
174
+ export const ACTION_CARD_RETRY_MAX_WAIT_MS = 5_000;
175
+
176
+ /** `retry_after` in milliseconds, read from either spelling Telegram uses. */
177
+ function rateLimitDelayMs(error) {
178
+ const value = error?.retry_after ?? error?.retryAfter;
179
+ const seconds = Number(value);
180
+ if (!Number.isFinite(seconds) || seconds < 0) return null;
181
+ return Math.ceil(seconds * 1_000);
182
+ }
183
+
184
+ function isRateLimited(error) {
185
+ const code = Number(error?.providerCode ?? error?.status);
186
+ return code === 429 || error?.code === 'telegram-429';
187
+ }
188
+
189
+ /**
190
+ * Run one Telegram card operation, honouring a 429 exactly once.
191
+ *
192
+ * Every card paid for this separately before: a throttled edit surfaced as
193
+ * "无法更新卡片,请重新发送命令" even though Telegram had just said when to retry.
194
+ * This helper makes that recovery the default for all cards.
195
+ *
196
+ * Returns the operation's result, or rethrows the last error.
197
+ */
198
+ export async function withActionCardRetry(operation, {
199
+ maxWaitMs = ACTION_CARD_RETRY_MAX_WAIT_MS,
200
+ } = {}) {
201
+ try {
202
+ return await operation();
203
+ } catch (error) {
204
+ const delay = isRateLimited(error) ? rateLimitDelayMs(error) : null;
205
+ // Only a short, explicit, known wait is worth taking; anything else is the
206
+ // caller's failure to report.
207
+ if (delay === null || delay > maxWaitMs) throw error;
208
+ // Deliberately NOT unref'd: a throttled retry is the only thing this call is
209
+ // waiting for, so the timer must keep the loop alive until it fires.
210
+ await new Promise((resolve) => { setTimeout(resolve, delay); });
211
+ return operation();
212
+ }
213
+ }
214
+
138
215
  export class ActionCardRegistry {
139
216
  #ttlMs;
140
217
  #maxEntries;
@@ -236,14 +313,13 @@ export class ActionCardRegistry {
236
313
  }
237
314
 
238
315
  /**
239
- * Give a claimed card its keyboard back after a submission that failed but is
240
- * still retryable (a transient RPC error, a dropped response). Without this a
241
- * card stays `claimed` forever and every later press is answered with "still
242
- * submitting", so the only recovery is typing the decision as text.
316
+ * Give a claimed card its keyboard back after an operation that failed but is
317
+ * still retryable (a transient RPC error, a refused write).
243
318
  *
244
- * Only a live claim can be released: a card already resolved, expired or
245
- * superseded must stay dead, otherwise a stale tap could decide a newer
246
- * approval. Returns the released entry, or null when nothing was released.
319
+ * `settle()` only accepts terminal statuses, so a caller that wants the press
320
+ * to be available again has no other way back. Only a live claim can be
321
+ * released: a card already resolved, expired or superseded must stay dead, or
322
+ * a stale press could act on newer state. Returns the released entry, or null.
247
323
  */
248
324
  releaseClaim(nonce) {
249
325
  const entry = this.#lookup(nonce);