@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,6 +5,7 @@ import { tmpdir } from 'node:os';
5
5
  import { basename, extname, isAbsolute, join, resolve } from 'node:path';
6
6
 
7
7
  export const OUTBOUND_ARTIFACT_TOOL = 'dsh_im_return_file';
8
+ const HOST_PRESENT_TOOL = 'present';
8
9
 
9
10
  const ARTIFACT_KIND = 'dsh-im-outbound-artifact';
10
11
  const ARTIFACT_READ_CHUNK_BYTES = 64 * 1024;
@@ -281,6 +282,7 @@ export class OutboundArtifactRegistry {
281
282
  #consumersByPrompt = new Map();
282
283
  #consumersByTurn = new Map();
283
284
  #openTurns = new Map();
285
+ #adoptions = new Map();
284
286
  #uuid;
285
287
 
286
288
  constructor({ uuid = randomUUID } = {}) {
@@ -375,6 +377,118 @@ export class OutboundArtifactRegistry {
375
377
  }
376
378
  }
377
379
 
380
+ /**
381
+ * Adopt the files the host `present` tool recorded for this Turn.
382
+ *
383
+ * `present` appends `deliverables/presented` to the durable Session and
384
+ * reports success, but the IM delivery path only claims artifacts registered
385
+ * through `dsh_im_return_file`. A file presented in an IM conversation was
386
+ * therefore dropped without an error: the answer stayed intact and the user
387
+ * received nothing. Snapshot the recorded paths into the same pipeline so the
388
+ * channel delivers them when the Turn ends.
389
+ */
390
+ adoptPresented(session, event, { logger } = {}) {
391
+ const sessionId = sessionIdOf(session);
392
+ const turn = event?.data?.turn;
393
+ const files = event?.data?.files;
394
+ if (!sessionId || !Number.isInteger(turn) || turn < 0) return null;
395
+ if (!Array.isArray(files) || files.length === 0) return null;
396
+ if (!this.#consumerForTurn(sessionId, turn)) {
397
+ logger?.warn?.('[dsh-im] files presented by the host tool were not delivered: no IM delivery consumer owns this turn', {
398
+ sessionId, turn, fileCount: files.length,
399
+ });
400
+ return null;
401
+ }
402
+ const workspace = session?.header?.cwd;
403
+ const keyForTurn = turnKey(sessionId, turn);
404
+ // Snapshot in parallel, commit in the order the answer presented them so
405
+ // the channel sends the files in a predictable sequence.
406
+ const adoption = Promise.all(files.map((file) => this.#snapshotPresentedFile({
407
+ sessionId, turn, workspace, file, logger,
408
+ }))).then((artifacts) => {
409
+ for (const artifact of artifacts) {
410
+ if (artifact) this.#commitArtifact(artifact);
411
+ }
412
+ });
413
+ const pending = this.#adoptions.get(keyForTurn) ?? [];
414
+ pending.push(adoption);
415
+ this.#adoptions.set(keyForTurn, pending);
416
+ return adoption;
417
+ }
418
+
419
+ /** Let a Turn end wait for in-flight present() adoption instead of outrunning it. */
420
+ async adoptionsSettled(sessionId, turn) {
421
+ if (typeof sessionId !== 'string' || !Number.isInteger(turn)) return;
422
+ const keyForTurn = turnKey(sessionId, turn);
423
+ const pending = this.#adoptions.get(keyForTurn);
424
+ if (!pending) return;
425
+ await Promise.allSettled(pending);
426
+ if (this.#adoptions.get(keyForTurn) === pending) this.#adoptions.delete(keyForTurn);
427
+ }
428
+
429
+ async #snapshotPresentedFile({ sessionId, turn, workspace, file, logger }) {
430
+ const requestedPath = typeof file?.path === 'string' ? file.path.trim() : '';
431
+ if (!requestedPath) return null;
432
+ try {
433
+ const snapshot = await snapshotFile(workspace, requestedPath);
434
+ return this.#buildArtifact(sessionId, turn, snapshot, {
435
+ callId: typeof file?.callId === 'string' ? file.callId : null,
436
+ kind: 'session-event',
437
+ toolName: HOST_PRESENT_TOOL,
438
+ });
439
+ } catch (error) {
440
+ logger?.warn?.('[dsh-im] could not adopt a file presented by the host tool:', {
441
+ sessionId, turn, path: requestedPath, reason: error?.code ?? error?.message,
442
+ });
443
+ return null;
444
+ }
445
+ }
446
+
447
+ #buildArtifact(sessionId, turn, snapshot, { callId, kind, toolName }) {
448
+ const artifact = Object.freeze({
449
+ kind: ARTIFACT_KIND,
450
+ schemaVersion: 1,
451
+ artifactId: this.#uuid(),
452
+ deliveryKey: this.#uuid(),
453
+ fileName: snapshot.fileName,
454
+ mediaType: snapshot.mediaType,
455
+ size: snapshot.size,
456
+ digest: snapshot.digest,
457
+ source: 'managed-temp',
458
+ registeredBy: Object.freeze({
459
+ kind,
460
+ eventId: typeof callId === 'string' ? callId : 'unknown',
461
+ toolName,
462
+ }),
463
+ origin: Object.freeze({
464
+ sessionId,
465
+ turn,
466
+ callId: typeof callId === 'string' ? callId : null,
467
+ }),
468
+ createdAt: Date.now(),
469
+ });
470
+ artifactStorage.set(artifact, {
471
+ path: snapshot.storagePath,
472
+ materializing: 0,
473
+ materialized: false,
474
+ releaseRequested: false,
475
+ cleanupRequested: false,
476
+ cleanupDeferred: false,
477
+ onCleanup: () => this.#forgetArtifact(artifact),
478
+ });
479
+ return artifact;
480
+ }
481
+
482
+ #commitArtifact(artifact) {
483
+ const keyForTurn = turnKey(artifact.origin.sessionId, artifact.origin.turn);
484
+ let committed = this.#turns.get(keyForTurn);
485
+ if (!committed) {
486
+ committed = new Map();
487
+ this.#turns.set(keyForTurn, committed);
488
+ }
489
+ committed.set(artifactKey(artifact), artifact);
490
+ }
491
+
378
492
  #currentTurn(session) {
379
493
  const sessionId = sessionIdOf(session);
380
494
  return this.#openTurns.has(sessionId)
@@ -418,6 +532,9 @@ export class OutboundArtifactRegistry {
418
532
  if (key.startsWith(prefix)) this.#consumersByTurn.delete(key);
419
533
  }
420
534
  this.#openTurns.delete(sessionId);
535
+ for (const key of [...this.#adoptions.keys()]) {
536
+ if (key.startsWith(prefix)) this.#adoptions.delete(key);
537
+ }
421
538
  for (const artifact of artifacts) cleanupArtifactStorage(artifact);
422
539
  }
423
540
 
@@ -446,7 +563,7 @@ export class OutboundArtifactRegistry {
446
563
  if (!consumer) throw unavailable();
447
564
 
448
565
  const snapshot = await snapshotFile(workspace, requestedPath, exec?.signal);
449
- const { storagePath, ...snapshotMetadata } = snapshot;
566
+ const { storagePath } = snapshot;
450
567
  // Copying and hashing yield to cancellation, disposal and Turn completion.
451
568
  // Never leave a late snapshot behind after its delivery owner has gone.
452
569
  try {
@@ -457,33 +574,10 @@ export class OutboundArtifactRegistry {
457
574
  await unlink(storagePath).catch(() => undefined);
458
575
  throw error;
459
576
  }
460
- const artifact = Object.freeze({
461
- kind: ARTIFACT_KIND,
462
- schemaVersion: 1,
463
- artifactId: this.#uuid(),
464
- deliveryKey: this.#uuid(),
465
- ...snapshotMetadata,
466
- source: 'managed-temp',
467
- registeredBy: Object.freeze({
468
- kind: 'tool-result',
469
- eventId: typeof exec?.callId === 'string' ? exec.callId : 'unknown',
470
- toolName: OUTBOUND_ARTIFACT_TOOL,
471
- }),
472
- origin: Object.freeze({
473
- sessionId,
474
- turn,
475
- callId: typeof exec?.callId === 'string' ? exec.callId : null,
476
- }),
477
- createdAt: Date.now(),
478
- });
479
- artifactStorage.set(artifact, {
480
- path: storagePath,
481
- materializing: 0,
482
- materialized: false,
483
- releaseRequested: false,
484
- cleanupRequested: false,
485
- cleanupDeferred: false,
486
- onCleanup: () => this.#forgetArtifact(artifact),
577
+ const artifact = this.#buildArtifact(sessionId, turn, snapshot, {
578
+ callId: exec?.callId,
579
+ kind: 'tool-result',
580
+ toolName: OUTBOUND_ARTIFACT_TOOL,
487
581
  });
488
582
  const keyForTurn = turnKey(sessionId, turn);
489
583
  let staged = this.#stagedTurns.get(keyForTurn);
@@ -526,6 +620,7 @@ export class OutboundArtifactRegistry {
526
620
  const keyForTurn = turnKey(sessionId, turn);
527
621
  const committed = this.#turns.get(keyForTurn);
528
622
  this.#turns.delete(keyForTurn);
623
+ this.#adoptions.delete(keyForTurn);
529
624
  if (!committed) return [];
530
625
  if (signal?.aborted) {
531
626
  for (const artifact of committed.values()) cleanupArtifactStorage(artifact);
@@ -553,6 +648,7 @@ export class OutboundArtifactRegistry {
553
648
  ]);
554
649
  this.#turns.delete(keyForTurn);
555
650
  this.#stagedTurns.delete(keyForTurn);
651
+ this.#adoptions.delete(keyForTurn);
556
652
  for (const artifact of artifacts) cleanupArtifactStorage(artifact);
557
653
  }
558
654
 
@@ -576,6 +672,7 @@ export class OutboundArtifactRegistry {
576
672
  this.#consumersByPrompt.clear();
577
673
  this.#consumersByTurn.clear();
578
674
  this.#openTurns.clear();
675
+ this.#adoptions.clear();
579
676
  }
580
677
 
581
678
  #bindClaimSignal(turnKey, signal) {
@@ -732,7 +829,14 @@ export function installOutboundArtifactTool(ctx, { registry = outboundArtifactRe
732
829
  ctx.on('agent/inbox/claimed', ({ agent, message, turn }) => {
733
830
  registry.observeClaimedMessage(agent?.session, message, turn);
734
831
  }, { global: true });
735
- ctx.on('session/event', (session, event) => registry.observeSessionEvent(session, event, { logger }));
832
+ ctx.on('session/event', (session, event) => {
833
+ registry.observeSessionEvent(session, event, { logger });
834
+ if (event?.type === 'deliverables/presented') {
835
+ registry.adoptPresented(session, event, { logger })?.catch?.((error) => {
836
+ logger?.warn?.('[dsh-im] present adoption failed:', error?.message);
837
+ });
838
+ }
839
+ });
736
840
  ctx.on('session/disposed', (session) => registry.disposeSession(session));
737
841
  ctx.systemPrompt.context({
738
842
  name: 'dsh-im:return-file',
@@ -79,7 +79,8 @@ function errorUnavailableReason(error) {
79
79
  function mergeDefined(base, loaded) {
80
80
  const merged = { ...base };
81
81
  for (const key of [
82
- 'messageId', 'authorId', 'authorName', 'content', 'attachments', 'files', 'unavailableReason',
82
+ 'messageId', 'authorId', 'authorName', 'content', 'attachments', 'files', 'images',
83
+ 'unavailableReason',
83
84
  ]) {
84
85
  if (loaded[key] !== undefined) merged[key] = loaded[key];
85
86
  }
@@ -108,6 +109,7 @@ function normalizeReference(reference) {
108
109
  const content = cleanString(reference.content, REPLY_CONTENT_MAX_CODE_POINTS, { multiline: true });
109
110
  const { attachments, truncated: attachmentsTruncated } = cleanAttachments(reference.attachments);
110
111
  const files = Array.isArray(reference.files) ? reference.files.filter((file) => file && typeof file.load === 'function') : [];
112
+ const images = Array.isArray(reference.images) ? reference.images.filter((image) => image && typeof image.load === 'function') : [];
111
113
  let unavailableReason = cleanUnavailableReason(reference.unavailableReason);
112
114
  if (!content.value && attachments.length === 0 && !unavailableReason) {
113
115
  unavailableReason = 'not-delivered';
@@ -121,13 +123,14 @@ function normalizeReference(reference) {
121
123
  ...(content.value ? { content: content.value } : {}),
122
124
  ...(attachments.length > 0 ? { attachments } : {}),
123
125
  ...(files.length > 0 ? { files } : {}),
126
+ ...(images.length > 0 ? { images } : {}),
124
127
  ...(unavailableReason ? { unavailableReason } : {}),
125
128
  ...(truncated ? { truncated: true } : {}),
126
129
  };
127
130
  }
128
131
 
129
132
  function replyBlock(reference) {
130
- const { files: _files, ...visibleReference } = reference;
133
+ const { files: _files, images: _images, ...visibleReference } = reference;
131
134
  const json = JSON.stringify(visibleReference).replace(/[<>&]/gu, (character) => ({
132
135
  '<': '\\u003c',
133
136
  '>': '\\u003e',
@@ -142,7 +145,7 @@ export function hasReplyReference(message) {
142
145
 
143
146
  export async function promptContentForInboundMessage(
144
147
  message,
145
- { signal, deferImages = false, originalFiles } = {},
148
+ { signal, deferImages = false, originalFiles, originalImages } = {},
146
149
  ) {
147
150
  if (!hasReplyReference(message)) {
148
151
  return promptContentForMessage(message, { signal, deferImages });
@@ -163,6 +166,18 @@ export async function promptContentForInboundMessage(
163
166
  if (!Array.isArray(message.files)) message.files = [];
164
167
  message.files.push(...reference.files);
165
168
  }
169
+ // Images follow the same contract: they join the current message's images so
170
+ // the Harness downloads them together with the message's own pictures, and
171
+ // the quote block itself never carries their bytes or download URLs.
172
+ if (Array.isArray(originalImages)) {
173
+ if (!Array.isArray(message.images)) message.images = [];
174
+ message.images.length = 0;
175
+ message.images.push(...originalImages);
176
+ }
177
+ if (Array.isArray(reference.images) && reference.images.length > 0) {
178
+ if (!Array.isArray(message.images)) message.images = [];
179
+ message.images.push(...reference.images);
180
+ }
166
181
  const currentContent = await promptContentForMessage(message, { signal, deferImages });
167
182
  return [{ type: 'text', text: replyBlock(reference) }, ...currentContent];
168
183
  }