@xmanrui/dsh-im 5.7.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 (77) 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 +375 -160
  5. package/lib/index.js +330 -325
  6. package/package.json +3 -1
  7. package/plugin-src/client/channels/feishu/api.js +5 -0
  8. package/plugin-src/client/channels/feishu/conversation-directory-settings.js +73 -0
  9. package/plugin-src/client/channels/feishu/index.js +1 -0
  10. package/plugin-src/client/connection-error.js +1 -1
  11. package/plugin-src/client/delivery-settings.js +4 -0
  12. package/plugin-src/client/i18n-ru.js +17 -0
  13. package/plugin-src/client/i18n.js +17 -0
  14. package/plugin-src/client/styles.js +6 -0
  15. package/plugin-src/client/workspace-editor.js +6 -5
  16. package/plugin-src/host/channels/dingtalk/production.mjs +2 -1
  17. package/plugin-src/host/channels/feishu/production.mjs +2 -1
  18. package/plugin-src/host/channels/feishu/rpc.mjs +25 -0
  19. package/plugin-src/host/channels/matrix/production.mjs +2 -1
  20. package/plugin-src/host/channels/qq/production.mjs +2 -1
  21. package/plugin-src/host/channels/shared/binding-events.mjs +130 -0
  22. package/plugin-src/host/channels/shared/production.mjs +32 -3
  23. package/plugin-src/host/channels/shared/workspace-rpc.mjs +2 -0
  24. package/plugin-src/host/channels/slack/production.mjs +2 -1
  25. package/plugin-src/host/channels/wecom/production.mjs +2 -1
  26. package/plugin-src/host/channels/wecom-app/production.mjs +2 -1
  27. package/plugin-src/host/channels/weixin/production.mjs +2 -1
  28. package/plugin-src/host/channels/whatsapp/production.mjs +2 -1
  29. package/plugin-src/host/delivery-adapter.mjs +122 -1
  30. package/plugin-src/host/delivery-service.mjs +40 -0
  31. package/plugin-src/host/ds-bot-client.mjs +28 -2
  32. package/plugin-src/host/index.mjs +24 -1
  33. package/plugin-src/host/modern-harness-api.mjs +3 -0
  34. package/scripts/telegram-menu-scopes.mjs +57 -0
  35. package/scripts/verify-interface-language.mjs +60 -15
  36. package/scripts/verify-package.mjs +17 -6
  37. package/src/channels/dingtalk/dingtalk-bridge.mjs +2 -3
  38. package/src/channels/dingtalk/dingtalk-menu.mjs +4 -5
  39. package/src/channels/email/transports/imap-smtp.mjs +21 -11
  40. package/src/channels/feishu/bridge.mjs +75 -22
  41. package/src/channels/feishu/feishu-cards.mjs +96 -25
  42. package/src/channels/feishu/message-utils.mjs +1 -0
  43. package/src/channels/qq/qq-menu.mjs +3 -4
  44. package/src/channels/shared/action-card.mjs +331 -0
  45. package/src/channels/shared/bot-workspace-store.mjs +369 -6
  46. package/src/channels/shared/command-catalog.mjs +20 -5
  47. package/src/channels/shared/conversation/describe.mjs +116 -0
  48. package/src/channels/shared/conversation-directory-ensure.mjs +46 -0
  49. package/src/channels/shared/conversation-directory.mjs +29 -0
  50. package/src/channels/shared/conversation-state-store.mjs +62 -0
  51. package/src/channels/shared/diagnostic-details.mjs +38 -0
  52. package/src/channels/shared/harness-approval.mjs +58 -13
  53. package/src/channels/shared/harness-client.mjs +85 -2
  54. package/src/channels/shared/harness-session-binding.mjs +13 -1
  55. package/src/channels/shared/i18n-en/diagnostics.mjs +2 -0
  56. package/src/channels/shared/i18n-en/feishu.mjs +6 -0
  57. package/src/channels/shared/i18n-en/shared-a.mjs +14 -0
  58. package/src/channels/shared/i18n-en/telegram.mjs +14 -0
  59. package/src/channels/shared/i18n-ru/diagnostics.mjs +2 -0
  60. package/src/channels/shared/i18n-ru/feishu.mjs +6 -0
  61. package/src/channels/shared/i18n-ru/shared-a.mjs +14 -0
  62. package/src/channels/shared/i18n-ru/telegram.mjs +14 -0
  63. package/src/channels/shared/message-failure.mjs +8 -0
  64. package/src/channels/shared/model-command.mjs +5 -0
  65. package/src/channels/shared/new-command.mjs +7 -0
  66. package/src/channels/shared/preset-workspaces.mjs +48 -0
  67. package/src/channels/shared/qr-encoding-error.mjs +8 -2
  68. package/src/channels/shared/semantic/reply-reference.mjs +25 -5
  69. package/src/channels/shared/status-command.mjs +159 -0
  70. package/src/channels/shared/text-harness-bridge.mjs +89 -6
  71. package/src/channels/shared/workspace-command.mjs +36 -15
  72. package/src/channels/shared/workspace-session.mjs +30 -5
  73. package/src/channels/telegram/approval-card.mjs +379 -0
  74. package/src/channels/telegram/telegram-api.mjs +10 -1
  75. package/src/channels/telegram/telegram-bridge.mjs +7 -0
  76. package/src/channels/telegram/telegram-runtime.mjs +125 -13
  77. package/src/channels/wecom/wecom-bridge.mjs +5 -3
@@ -0,0 +1,331 @@
1
+ import { randomBytes } from 'node:crypto';
2
+
3
+ import { t } from './i18n.mjs';
4
+
5
+ /**
6
+ * Shared foundation for interactive cards that carry a one-shot button press.
7
+ *
8
+ * Every carded channel needs the same four things, and four independent
9
+ * implementations already existed (Feishu card keys, QQ opaque tokens, DingTalk
10
+ * revisions, Telegram question nonces). This module provides them once:
11
+ *
12
+ * - a versioned, bounded callback namespace (`<prefix>|<nonce>|<action>`) so a
13
+ * keyboard press is attributable to exactly one presentation;
14
+ * - a nonce registry with TTL and capacity limits, so a dead card cannot grow
15
+ * memory forever;
16
+ * - a synchronous first-press claim, so two taps (or two clients) execute once;
17
+ * - one set of Chinese notices for pressed/expired/superseded cards.
18
+ *
19
+ * Cards that also need to re-check live data (a list shrank, a session was
20
+ * deleted) should `claim()` first and then `settle(nonce, 'stale')` when the
21
+ * re-check fails.
22
+ */
23
+
24
+ /** Telegram rejects callback_data longer than 64 bytes. */
25
+ export const ACTION_CARD_CALLBACK_MAX_BYTES = 64;
26
+
27
+ const CALLBACK_PREFIX_PATTERN = /^[a-z][a-z0-9]{0,7}$/u;
28
+ const NONCE_PATTERN = /^[A-Za-z0-9_-]{1,32}$/u;
29
+ const ACTION_PATTERN = /^[A-Za-z0-9_-]{1,16}$/u;
30
+ const DEFAULT_NONCE_BYTES = 9;
31
+
32
+ export const ACTION_CARD_STATUS = Object.freeze({
33
+ /** Rendered, waiting for its first press. */
34
+ ACTIVE: 'active',
35
+ /** Pressed; the handler owns the outcome now. */
36
+ CLAIMED: 'claimed',
37
+ /** Handled successfully. */
38
+ RESOLVED: 'resolved',
39
+ /** TTL elapsed (approval timeout, dead keyboard). */
40
+ EXPIRED: 'expired',
41
+ /** A newer card replaced this one in the same conversation. */
42
+ SUPERSEDED: 'superseded',
43
+ /** The live re-check after a press found the underlying state changed. */
44
+ STALE: 'stale',
45
+ /** The owner revoked the card (route closed, approval withdrawn). */
46
+ REVOKED: 'revoked',
47
+ /** No such nonce (process restarted, foreign payload). */
48
+ UNKNOWN: 'unknown',
49
+ });
50
+
51
+ const LIVE_STATUSES = new Set([ACTION_CARD_STATUS.ACTIVE, ACTION_CARD_STATUS.CLAIMED]);
52
+ const TERMINAL_STATUSES = new Set([
53
+ ACTION_CARD_STATUS.RESOLVED,
54
+ ACTION_CARD_STATUS.EXPIRED,
55
+ ACTION_CARD_STATUS.SUPERSEDED,
56
+ ACTION_CARD_STATUS.STALE,
57
+ ACTION_CARD_STATUS.REVOKED,
58
+ ]);
59
+
60
+ /** One notice per card state; channels override the wording per surface. */
61
+ export const ACTION_CARD_STATE_TEXTS = Object.freeze({
62
+ [ACTION_CARD_STATUS.ACTIVE]: '正在处理,请稍候。',
63
+ [ACTION_CARD_STATUS.CLAIMED]: '正在提交,请稍候。',
64
+ [ACTION_CARD_STATUS.RESOLVED]: '该操作已处理,无需重复操作。',
65
+ [ACTION_CARD_STATUS.EXPIRED]: '该操作已过期,请重新打开。',
66
+ [ACTION_CARD_STATUS.SUPERSEDED]: '该操作已被新操作取代,请使用最新一条。',
67
+ [ACTION_CARD_STATUS.STALE]: '该内容已变化,请重新打开后再操作。',
68
+ [ACTION_CARD_STATUS.REVOKED]: '该操作已失效,请重新打开。',
69
+ [ACTION_CARD_STATUS.UNKNOWN]: '该操作已失效或不存在。',
70
+ });
71
+
72
+ /**
73
+ * A random, URL-safe presentation identity.
74
+ * 9 bytes → 12 base64url characters: collision-safe per conversation and small
75
+ * enough that the whole callback payload stays far under 64 bytes.
76
+ */
77
+ export function createActionNonce(byteLength = DEFAULT_NONCE_BYTES) {
78
+ if (!Number.isInteger(byteLength) || byteLength < 1 || byteLength > 24) {
79
+ throw new TypeError('An action card nonce needs 1..24 random bytes');
80
+ }
81
+ return randomBytes(byteLength).toString('base64url');
82
+ }
83
+
84
+ /**
85
+ * Build one versioned callback namespace, e.g. `ap1|<nonce>|<action>`.
86
+ * Encoding validates every token before emitting it, so a button can always be
87
+ * attributed and can never exceed Telegram's callback budget.
88
+ */
89
+ export function createCallbackNamespace(prefix) {
90
+ if (typeof prefix !== 'string' || !CALLBACK_PREFIX_PATTERN.test(prefix)) {
91
+ throw new TypeError('An action card callback prefix is invalid');
92
+ }
93
+ const maxBytes = ACTION_CARD_CALLBACK_MAX_BYTES;
94
+ return Object.freeze({
95
+ prefix,
96
+ maxBytes,
97
+ encode({ nonce, action } = {}) {
98
+ if (typeof nonce !== 'string' || !NONCE_PATTERN.test(nonce)) {
99
+ throw new TypeError('An action card nonce is invalid');
100
+ }
101
+ if (typeof action !== 'string' || !ACTION_PATTERN.test(action)) {
102
+ throw new TypeError('An action card action is invalid');
103
+ }
104
+ const data = `${prefix}|${nonce}|${action}`;
105
+ if (Buffer.byteLength(data, 'utf8') > maxBytes) {
106
+ throw new TypeError(`An action card callback must be at most ${maxBytes} bytes`);
107
+ }
108
+ return data;
109
+ },
110
+ parse(data) {
111
+ if (typeof data !== 'string') return null;
112
+ const parts = data.split('|');
113
+ if (parts.length !== 3 || parts[0] !== prefix) return null;
114
+ const [, nonce, action] = parts;
115
+ if (!NONCE_PATTERN.test(nonce) || !ACTION_PATTERN.test(action)) return null;
116
+ return { nonce, action };
117
+ },
118
+ });
119
+ }
120
+
121
+ /** The notice shown for a dead or already-handled card. */
122
+ export function actionCardStateText(status, texts = {}) {
123
+ const override = texts && typeof texts === 'object' ? texts[status] : undefined;
124
+ const fallback = ACTION_CARD_STATE_TEXTS[status]
125
+ ?? (texts && typeof texts === 'object' ? texts[ACTION_CARD_STATUS.UNKNOWN] : undefined)
126
+ ?? ACTION_CARD_STATE_TEXTS[ACTION_CARD_STATUS.UNKNOWN];
127
+ return t(typeof override === 'string' && override ? override : fallback);
128
+ }
129
+
130
+ /**
131
+ * In-memory presentation registry for one channel.
132
+ *
133
+ * Entries live for `ttlMs`; a terminal entry is kept until then so a late press
134
+ * still gets "已处理/已过期" instead of a generic "不存在". At most
135
+ * `maxEntries` cards are remembered (oldest is evicted first) and a new card in
136
+ * a conversation supersedes the previous live one.
137
+ */
138
+ export class ActionCardRegistry {
139
+ #ttlMs;
140
+ #maxEntries;
141
+ #now;
142
+ #createNonce;
143
+ #entries = new Map();
144
+
145
+ constructor({
146
+ ttlMs = 3_600_000,
147
+ maxEntries = 256,
148
+ now = Date.now,
149
+ createNonce = createActionNonce,
150
+ } = {}) {
151
+ if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
152
+ throw new TypeError('An action card TTL must be a positive number');
153
+ }
154
+ if (!Number.isInteger(maxEntries) || maxEntries < 1) {
155
+ throw new TypeError('An action card capacity must be a positive integer');
156
+ }
157
+ if (typeof now !== 'function') throw new TypeError('An action card clock must be a function');
158
+ if (typeof createNonce !== 'function') {
159
+ throw new TypeError('An action card nonce factory must be a function');
160
+ }
161
+ this.#ttlMs = ttlMs;
162
+ this.#maxEntries = maxEntries;
163
+ this.#now = now;
164
+ this.#createNonce = createNonce;
165
+ }
166
+
167
+ get size() {
168
+ return this.#entries.size;
169
+ }
170
+
171
+ /**
172
+ * Register one presentation. Issuing a new card in the same conversation
173
+ * retires the previous live one — one conversation shows one working card.
174
+ */
175
+ issue({ conversationKey, kind = '', payload = {}, ttlMs } = {}) {
176
+ const key = typeof conversationKey === 'string' ? conversationKey.trim() : '';
177
+ if (!key) throw new TypeError('An action card requires a conversation key');
178
+ const now = this.#now();
179
+ const lifetime = Number.isFinite(ttlMs) && ttlMs > 0 ? ttlMs : this.#ttlMs;
180
+ // Drop long-dead cards so TTL memory is reclaimed even when presses stop.
181
+ this.purge();
182
+ this.revokeConversation(key, { status: ACTION_CARD_STATUS.SUPERSEDED });
183
+
184
+ let nonce = null;
185
+ for (let attempt = 0; attempt < 8; attempt += 1) {
186
+ const candidate = this.#createNonce();
187
+ if (typeof candidate !== 'string' || !NONCE_PATTERN.test(candidate)) {
188
+ throw new TypeError('An action card nonce must be 1..32 URL-safe characters');
189
+ }
190
+ if (!this.#entries.has(candidate)) {
191
+ nonce = candidate;
192
+ break;
193
+ }
194
+ }
195
+ if (nonce === null) throw new Error('Could not allocate a unique action card nonce');
196
+
197
+ const entry = {
198
+ nonce,
199
+ conversationKey: key,
200
+ kind: typeof kind === 'string' ? kind : '',
201
+ payload: payload && typeof payload === 'object' ? payload : {},
202
+ status: ACTION_CARD_STATUS.ACTIVE,
203
+ createdAt: now,
204
+ expiresAt: now + lifetime,
205
+ claimedAt: null,
206
+ settledAt: null,
207
+ };
208
+ this.#entries.set(nonce, entry);
209
+ this.#enforceCapacity();
210
+ return entry;
211
+ }
212
+
213
+ /** Read an entry (and lazily mark it expired when its TTL elapsed). */
214
+ get(nonce) {
215
+ const entry = this.#lookup(nonce);
216
+ if (!entry) return null;
217
+ this.#expireIfNeeded(entry);
218
+ return entry;
219
+ }
220
+
221
+ /**
222
+ * Claim the first press synchronously. The caller must call this before its
223
+ * first `await`, so a second tap cannot also see the card as active.
224
+ */
225
+ claim(nonce) {
226
+ const entry = this.#lookup(nonce);
227
+ if (!entry) return { status: ACTION_CARD_STATUS.UNKNOWN, entry: null };
228
+ this.#expireIfNeeded(entry);
229
+ const liveStatus = entry.status;
230
+ if (liveStatus === ACTION_CARD_STATUS.ACTIVE) {
231
+ entry.status = ACTION_CARD_STATUS.CLAIMED;
232
+ entry.claimedAt = this.#now();
233
+ }
234
+ // `active` means this caller won the claim; any other value is a refusal.
235
+ return { status: liveStatus, entry };
236
+ }
237
+
238
+ /**
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.
243
+ *
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.
247
+ */
248
+ releaseClaim(nonce) {
249
+ const entry = this.#lookup(nonce);
250
+ if (!entry) return null;
251
+ this.#expireIfNeeded(entry);
252
+ if (entry.status !== ACTION_CARD_STATUS.CLAIMED) return null;
253
+ entry.status = ACTION_CARD_STATUS.ACTIVE;
254
+ entry.claimedAt = null;
255
+ return entry;
256
+ }
257
+
258
+ /**
259
+ * Move a live card to a terminal state. Returns false when there is nothing
260
+ * left to settle (unknown, expired, or already settled).
261
+ */
262
+ settle(nonce, status = ACTION_CARD_STATUS.RESOLVED) {
263
+ if (!TERMINAL_STATUSES.has(status)) {
264
+ throw new TypeError('An action card can only settle to a terminal status');
265
+ }
266
+ const entry = this.#lookup(nonce);
267
+ if (!entry) return false;
268
+ this.#expireIfNeeded(entry);
269
+ if (!LIVE_STATUSES.has(entry.status)) return false;
270
+ entry.status = status;
271
+ entry.settledAt = this.#now();
272
+ return true;
273
+ }
274
+
275
+ /** The one live card of a conversation, if any. */
276
+ activeFor(conversationKey) {
277
+ for (const entry of this.#entries.values()) {
278
+ if (entry.conversationKey !== conversationKey) continue;
279
+ this.#expireIfNeeded(entry);
280
+ if (LIVE_STATUSES.has(entry.status)) return entry;
281
+ }
282
+ return null;
283
+ }
284
+
285
+ /** Retire every live card of one conversation (route closed, list changed). */
286
+ revokeConversation(conversationKey, { status = ACTION_CARD_STATUS.SUPERSEDED } = {}) {
287
+ if (!TERMINAL_STATUSES.has(status)) {
288
+ throw new TypeError('An action card can only be revoked to a terminal status');
289
+ }
290
+ let revoked = 0;
291
+ for (const entry of this.#entries.values()) {
292
+ if (entry.conversationKey !== conversationKey) continue;
293
+ this.#expireIfNeeded(entry);
294
+ if (!LIVE_STATUSES.has(entry.status)) continue;
295
+ entry.status = status;
296
+ entry.settledAt = this.#now();
297
+ revoked += 1;
298
+ }
299
+ return revoked;
300
+ }
301
+
302
+ /** Drop every entry whose TTL elapsed; returns how many were removed. */
303
+ purge() {
304
+ const now = this.#now();
305
+ let removed = 0;
306
+ for (const [nonce, entry] of this.#entries) {
307
+ if (now >= entry.expiresAt) {
308
+ this.#entries.delete(nonce);
309
+ removed += 1;
310
+ }
311
+ }
312
+ return removed;
313
+ }
314
+
315
+ #lookup(nonce) {
316
+ return typeof nonce === 'string' ? this.#entries.get(nonce) ?? null : null;
317
+ }
318
+
319
+ #expireIfNeeded(entry) {
320
+ if (!LIVE_STATUSES.has(entry.status)) return;
321
+ if (this.#now() < entry.expiresAt) return;
322
+ entry.status = ACTION_CARD_STATUS.EXPIRED;
323
+ entry.settledAt = entry.expiresAt;
324
+ }
325
+
326
+ #enforceCapacity() {
327
+ while (this.#entries.size > this.#maxEntries) {
328
+ this.#entries.delete(this.#entries.keys().next().value);
329
+ }
330
+ }
331
+ }