@zhin.js/core 1.1.57 → 1.1.58

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 (89) hide show
  1. package/lib/built/user-interaction.d.ts +1 -1
  2. package/lib/feature/command.d.ts +2 -4
  3. package/lib/feature/command.js +1 -2
  4. package/lib/feature/middleware.d.ts +2 -2
  5. package/lib/feature/middleware.js +1 -1
  6. package/lib/index.d.ts +1 -1
  7. package/lib/plugin-runtime/im/command-feedback.d.ts +20 -1
  8. package/lib/plugin-runtime/im/command-feedback.js +63 -0
  9. package/lib/plugin-runtime/im/contracts.d.ts +60 -5
  10. package/lib/plugin-runtime/im/contracts.js +6 -2
  11. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +2 -3
  12. package/lib/plugin-runtime/im/endpoint-runtime.js +2 -2
  13. package/lib/plugin-runtime/im/host/component-host-installer.d.ts +2 -0
  14. package/lib/plugin-runtime/im/host/component-host-installer.js +14 -0
  15. package/lib/plugin-runtime/im/host/conversation-migration.d.ts +5 -0
  16. package/lib/plugin-runtime/im/host/conversation-migration.js +75 -0
  17. package/lib/plugin-runtime/im/host/database-config.d.ts +10 -0
  18. package/lib/plugin-runtime/im/host/database-config.js +114 -0
  19. package/lib/plugin-runtime/im/host/database-host-installer.d.ts +13 -0
  20. package/lib/plugin-runtime/im/host/database-host-installer.js +389 -0
  21. package/lib/plugin-runtime/im/host/database-state.d.ts +9 -0
  22. package/lib/plugin-runtime/im/host/database-state.js +91 -0
  23. package/lib/plugin-runtime/im/host/index.d.ts +8 -0
  24. package/lib/plugin-runtime/im/host/index.js +8 -0
  25. package/lib/plugin-runtime/im/host/locale-host-installer.d.ts +3 -0
  26. package/lib/plugin-runtime/im/host/locale-host-installer.js +60 -0
  27. package/lib/plugin-runtime/im/host/outbound-host-installer.d.ts +8 -0
  28. package/lib/plugin-runtime/im/host/outbound-host-installer.js +153 -0
  29. package/lib/plugin-runtime/im/host/sender-enrichment.d.ts +33 -0
  30. package/lib/plugin-runtime/im/host/sender-enrichment.js +215 -0
  31. package/lib/plugin-runtime/im/im-runtime.d.ts +7 -3
  32. package/lib/plugin-runtime/im/im-runtime.js +9 -7
  33. package/lib/plugin-runtime/im/inbound-dedup.d.ts +32 -0
  34. package/lib/plugin-runtime/im/inbound-dedup.js +138 -1
  35. package/lib/plugin-runtime/im/inbound-origin.d.ts +24 -2
  36. package/lib/plugin-runtime/im/inbound-origin.js +108 -1
  37. package/lib/plugin-runtime/im/inbound-runtime.d.ts +10 -13
  38. package/lib/plugin-runtime/im/inbound-runtime.js +74 -411
  39. package/lib/plugin-runtime/im/inbound-spine.d.ts +194 -0
  40. package/lib/plugin-runtime/im/inbound-spine.js +333 -0
  41. package/lib/plugin-runtime/im/index.d.ts +4 -7
  42. package/lib/plugin-runtime/im/index.js +4 -6
  43. package/lib/plugin-runtime/im/interaction-runtime.d.ts +4 -7
  44. package/lib/plugin-runtime/im/interaction-runtime.js +5 -8
  45. package/lib/plugin-runtime/im/interactive-router.d.ts +31 -4
  46. package/lib/plugin-runtime/im/interactive-router.js +134 -2
  47. package/lib/plugin-runtime/im/message-dispatcher.d.ts +1 -1
  48. package/lib/plugin-runtime/im/message-dispatcher.js +1 -2
  49. package/lib/plugin-runtime/im/message-events.d.ts +33 -3
  50. package/lib/plugin-runtime/im/message-events.js +34 -0
  51. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +1 -2
  52. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +4 -8
  53. package/lib/plugin-runtime/im/outbound-renderer.d.ts +32 -2
  54. package/lib/plugin-runtime/im/outbound-renderer.js +95 -4
  55. package/package.json +13 -14
  56. package/lib/plugin-runtime/im/captured-endpoint-operation.d.ts +0 -34
  57. package/lib/plugin-runtime/im/captured-endpoint-operation.js +0 -1
  58. package/lib/plugin-runtime/im/command-menu-navigation.d.ts +0 -13
  59. package/lib/plugin-runtime/im/command-menu-navigation.js +0 -43
  60. package/lib/plugin-runtime/im/command-menu-presentation.d.ts +0 -14
  61. package/lib/plugin-runtime/im/command-menu-presentation.js +0 -25
  62. package/lib/plugin-runtime/im/group-rule-violation.d.ts +0 -52
  63. package/lib/plugin-runtime/im/group-rule-violation.js +0 -10
  64. package/lib/plugin-runtime/im/ingress-route.d.ts +0 -10
  65. package/lib/plugin-runtime/im/ingress-route.js +0 -10
  66. package/lib/plugin-runtime/im/interaction-operation.d.ts +0 -12
  67. package/lib/plugin-runtime/im/interaction-operation.js +0 -2
  68. package/lib/plugin-runtime/im/jsx-button-registry.d.ts +0 -31
  69. package/lib/plugin-runtime/im/jsx-button-registry.js +0 -135
  70. package/lib/plugin-runtime/im/locale.d.ts +0 -5
  71. package/lib/plugin-runtime/im/locale.js +0 -12
  72. package/lib/plugin-runtime/im/login-assist-host.d.ts +0 -6
  73. package/lib/plugin-runtime/im/login-assist-host.js +0 -6
  74. package/lib/plugin-runtime/im/member-welcome.d.ts +0 -8
  75. package/lib/plugin-runtime/im/member-welcome.js +0 -11
  76. package/lib/plugin-runtime/im/message-bus.d.ts +0 -38
  77. package/lib/plugin-runtime/im/message-bus.js +0 -40
  78. package/lib/plugin-runtime/im/operation-endpoint.d.ts +0 -6
  79. package/lib/plugin-runtime/im/operation-endpoint.js +0 -2
  80. package/lib/plugin-runtime/im/operation-resources.d.ts +0 -15
  81. package/lib/plugin-runtime/im/operation-resources.js +0 -2
  82. package/lib/plugin-runtime/im/outbound-ledger.d.ts +0 -24
  83. package/lib/plugin-runtime/im/outbound-ledger.js +0 -114
  84. package/lib/plugin-runtime/im/outbound-presentation.d.ts +0 -19
  85. package/lib/plugin-runtime/im/outbound-presentation.js +0 -41
  86. package/lib/plugin-runtime/im/outbound-projection.d.ts +0 -17
  87. package/lib/plugin-runtime/im/outbound-projection.js +0 -55
  88. package/lib/plugin-runtime/im/runtime-middleware.d.ts +0 -5
  89. package/lib/plugin-runtime/im/runtime-middleware.js +0 -18
@@ -1,7 +1,29 @@
1
1
  import { type CapabilityId } from '@zhin.js/plugin-runtime';
2
- import type { ConversationRef } from '@zhin.js/im-contract';
2
+ import type { ConversationRef, DeliveryReceipt } from '@zhin.js/im-contract';
3
3
  import type { PeerRegistry } from '@zhin.js/adapter';
4
- import { type OutboundLedger } from './outbound-ledger.js';
4
+ /**
5
+ * scope 键不含 endpoint id:同一个群里不同 Bot 的 conversationRefKey 不同,
6
+ * 但它们互相的回显仍属同一会话。
7
+ */
8
+ export declare function ledgerScopeOf(conversation: ConversationRef): string;
9
+ export declare function digestText(text: string): string | undefined;
10
+ export interface LedgerMatch {
11
+ readonly endpointId: string;
12
+ }
13
+ export declare class OutboundLedger {
14
+ #private;
15
+ private readonly now;
16
+ constructor(now?: () => number);
17
+ get size(): number;
18
+ /** 仅在 `receipt.status === 'sent'` 时记录;`payload` 应为最终投影后的 payload。 */
19
+ record(conversation: ConversationRef, receipt: DeliveryReceipt, payload: unknown): void;
20
+ matchById(scope: string, messageId: unknown): LedgerMatch | undefined;
21
+ /**
22
+ * 摘要兜底(“账号 id 对不上”时):30 秒窗口、一次性。
23
+ * 权衡:人类在窗口内原样复读 Bot 刚发的文本,会被当作回显丢弃一次。
24
+ */
25
+ matchByDigest(scope: string, text: string, now?: number): LedgerMatch | undefined;
26
+ }
5
27
  /** 入站消息来源。仅 `human` 能触发 agent turn;其余只记录。 */
6
28
  export type MessageOrigin = 'self' | 'member' | 'peer_bot' | 'human';
7
29
  /** @internal */
@@ -1,6 +1,113 @@
1
+ import { createHash } from 'node:crypto';
1
2
  import { createToken } from '@zhin.js/plugin-runtime';
2
3
  import { getLogger } from '@zhin.js/logger';
3
- import { ledgerScopeOf } from './outbound-ledger.js';
4
+ import { segmentsToPlainText } from '../../built/segment-contract/text.js';
5
+ const MAX_ENTRIES = 2000;
6
+ const TTL_MS = 10 * 60_000;
7
+ const DIGEST_WINDOW_MS = 30_000;
8
+ const MIN_DIGEST_TEXT = 4;
9
+ /**
10
+ * scope 键不含 endpoint id:同一个群里不同 Bot 的 conversationRefKey 不同,
11
+ * 但它们互相的回显仍属同一会话。
12
+ */
13
+ export function ledgerScopeOf(conversation) {
14
+ return [
15
+ conversation.endpoint.adapter,
16
+ conversation.kind,
17
+ conversation.id,
18
+ conversation.parent?.kind ?? '',
19
+ conversation.parent?.id ?? '',
20
+ conversation.threadId ?? '',
21
+ ].join('\0');
22
+ }
23
+ export function digestText(text) {
24
+ const normalized = text.replace(/\s+/g, ' ').trim();
25
+ if (normalized.length < MIN_DIGEST_TEXT)
26
+ return undefined;
27
+ return createHash('sha256').update(normalized).digest('hex').slice(0, 16);
28
+ }
29
+ function payloadText(payload) {
30
+ if (typeof payload === 'string')
31
+ return payload;
32
+ if (Array.isArray(payload)) {
33
+ return segmentsToPlainText(payload.filter((item) => !!item && typeof item === 'object' && typeof item.type === 'string'));
34
+ }
35
+ return '';
36
+ }
37
+ export class OutboundLedger {
38
+ now;
39
+ #entries = [];
40
+ constructor(now = Date.now) {
41
+ this.now = now;
42
+ }
43
+ get size() {
44
+ return this.#entries.length;
45
+ }
46
+ /** 仅在 `receipt.status === 'sent'` 时记录;`payload` 应为最终投影后的 payload。 */
47
+ record(conversation, receipt, payload) {
48
+ if (receipt.status !== 'sent')
49
+ return;
50
+ const at = this.now();
51
+ this.#prune(at);
52
+ const rawId = receipt.message?.id;
53
+ const messageId = rawId === undefined || rawId === null || String(rawId) === '' ? undefined : String(rawId);
54
+ const digest = digestText(payloadText(payload));
55
+ if (messageId === undefined && digest === undefined)
56
+ return;
57
+ this.#entries.push({
58
+ scope: ledgerScopeOf(conversation),
59
+ endpointId: conversation.endpoint.id,
60
+ ...(messageId !== undefined ? { messageId } : {}),
61
+ ...(digest !== undefined ? { digest } : {}),
62
+ at,
63
+ digestConsumed: false,
64
+ });
65
+ while (this.#entries.length > MAX_ENTRIES)
66
+ this.#entries.shift();
67
+ }
68
+ matchById(scope, messageId) {
69
+ if (typeof messageId !== 'string' && typeof messageId !== 'number')
70
+ return undefined;
71
+ const id = String(messageId);
72
+ if (id === '')
73
+ return undefined;
74
+ const at = this.now();
75
+ for (let i = this.#entries.length - 1; i >= 0; i--) {
76
+ const entry = this.#entries[i];
77
+ if (at - entry.at > TTL_MS)
78
+ continue;
79
+ if (entry.scope === scope && entry.messageId === id)
80
+ return { endpointId: entry.endpointId };
81
+ }
82
+ return undefined;
83
+ }
84
+ /**
85
+ * 摘要兜底(“账号 id 对不上”时):30 秒窗口、一次性。
86
+ * 权衡:人类在窗口内原样复读 Bot 刚发的文本,会被当作回显丢弃一次。
87
+ */
88
+ matchByDigest(scope, text, now = this.now()) {
89
+ const digest = digestText(typeof text === 'string' ? text : '');
90
+ if (digest === undefined)
91
+ return undefined;
92
+ for (let i = this.#entries.length - 1; i >= 0; i--) {
93
+ const entry = this.#entries[i];
94
+ if (now - entry.at > DIGEST_WINDOW_MS)
95
+ continue;
96
+ if (entry.digestConsumed || entry.scope !== scope || entry.digest !== digest)
97
+ continue;
98
+ entry.digestConsumed = true;
99
+ return { endpointId: entry.endpointId };
100
+ }
101
+ return undefined;
102
+ }
103
+ #prune(at) {
104
+ let drop = 0;
105
+ while (drop < this.#entries.length && at - this.#entries[drop].at > TTL_MS)
106
+ drop++;
107
+ if (drop > 0)
108
+ this.#entries.splice(0, drop);
109
+ }
110
+ }
4
111
  const logger = getLogger('im.inbound.origin');
5
112
  /** @internal */
6
113
  export const MESSAGE_ORIGINS = Object.freeze(['self', 'member', 'peer_bot', 'human']);
@@ -1,19 +1,16 @@
1
- import type { CaptureEndpointOperationInput, CapturedEndpointOperation } from './captured-endpoint-operation.js';
2
- import type { GenerationAdmissionGate, PluginId, RuntimeSnapshot, SnapshotLease } from '@zhin.js/plugin-runtime';
3
1
  import type { EndpointEvent, PeerRegistry } from '@zhin.js/adapter';
4
- import { type OperationResourceView } from '@zhin.js/feature-kit';
5
- import type { DeliveryReceipt } from '@zhin.js/im-contract';
6
- import type { UserInteraction } from '@zhin.js/interaction';
7
2
  import type { CommandExecutionBudget, CommandOperationController } from '@zhin.js/command/internal/execution';
8
- import { type Notice } from '../../notice.js';
9
- import { type Request } from '../../request.js';
10
- import { type Message, type IncomingMessage, type MessageSenderRef, type SendRequest } from './contracts.js';
3
+ import type { OperationResourceView } from '@zhin.js/feature-kit';
4
+ import type { DeliveryReceipt, UserInteraction } from '@zhin.js/im-contract';
5
+ import type { GenerationAdmissionGate, PluginId, RuntimeSnapshot, SnapshotLease } from '@zhin.js/plugin-runtime';
6
+ import type { Notice } from '../../notice.js';
7
+ import type { Request } from '../../request.js';
8
+ import { RuntimeMessage, type CaptureEndpointOperationInput, type CapturedEndpointOperation, type IncomingMessage, type Message, type MessageSenderRef, type SendRequest } from './contracts.js';
9
+ import type { InboundDeduper } from './inbound-dedup.js';
10
+ import type { InboundOriginClassifier } from './inbound-origin.js';
11
+ import { type InboundInteractionPort } from './inbound-spine.js';
11
12
  import type { RuntimeMessageDiagnostics } from './message-diagnostics.js';
12
- import type { RuntimeInteractionCoordinator } from './interaction-runtime.js';
13
13
  import { type RuntimeMessageEvent } from './message-events.js';
14
- import type { InboundDeduper } from './inbound-dedup.js';
15
- import { type InboundOriginClassifier } from './inbound-origin.js';
16
- type InboundInteractionPort = Pick<RuntimeInteractionCoordinator<GenerationAdmissionGate>, 'create' | 'createForMessage' | 'createFromUnknown' | 'dispatch' | 'resolveClaim'> & Partial<Pick<RuntimeInteractionCoordinator<GenerationAdmissionGate>, 'hasPendingAnswer'>>;
17
14
  interface InboundRuntimeContext {
18
15
  readonly commandExecution?: CommandExecutionBudget;
19
16
  runCommandOperation?<T>(controller: CommandOperationController, operation: () => T): T;
@@ -22,7 +19,7 @@ interface InboundRuntimeContext {
22
19
  captureOperation(input: CaptureEndpointOperationInput): CapturedEndpointOperation;
23
20
  createOperationResources?(input: Readonly<{
24
21
  lease: SnapshotLease;
25
- message: import('./contracts.js').RuntimeMessage;
22
+ message: RuntimeMessage;
26
23
  owner: PluginId;
27
24
  signal: AbortSignal;
28
25
  }>): OperationResourceView | undefined;