@zhin.js/core 1.1.44 → 1.1.46

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 (34) hide show
  1. package/README.md +6 -0
  2. package/lib/built/html-to-text.d.ts +0 -3
  3. package/lib/built/html-to-text.js +130 -39
  4. package/lib/built/interactive-segments/resolve.d.ts +2 -2
  5. package/lib/built/interactive-segments/resolve.js +20 -12
  6. package/lib/built/readable-link.d.ts +4 -0
  7. package/lib/built/readable-link.js +32 -0
  8. package/lib/built/rich-segments/markdown-to-text.d.ts +1 -1
  9. package/lib/built/rich-segments/markdown-to-text.js +123 -11
  10. package/lib/built/segment-contract/json-schema.d.ts +1 -5
  11. package/lib/built/segment-contract/json-schema.js +35 -14
  12. package/lib/built/user-interaction.d.ts +1 -1
  13. package/lib/built/user-interaction.js +4 -5
  14. package/lib/plugin-runtime/im/command-menu-navigation.d.ts +6 -0
  15. package/lib/plugin-runtime/im/command-menu-navigation.js +33 -0
  16. package/lib/plugin-runtime/im/contracts.d.ts +12 -2
  17. package/lib/plugin-runtime/im/contracts.js +24 -2
  18. package/lib/plugin-runtime/im/im-runtime.d.ts +2 -0
  19. package/lib/plugin-runtime/im/im-runtime.js +24 -4
  20. package/lib/plugin-runtime/im/inbound-runtime.d.ts +5 -0
  21. package/lib/plugin-runtime/im/inbound-runtime.js +193 -135
  22. package/lib/plugin-runtime/im/index.d.ts +1 -0
  23. package/lib/plugin-runtime/im/interaction-operation.d.ts +8 -0
  24. package/lib/plugin-runtime/im/interaction-operation.js +2 -0
  25. package/lib/plugin-runtime/im/interaction-runtime.d.ts +5 -3
  26. package/lib/plugin-runtime/im/interaction-runtime.js +150 -51
  27. package/lib/plugin-runtime/im/message-diagnostics.d.ts +70 -0
  28. package/lib/plugin-runtime/im/message-diagnostics.js +182 -0
  29. package/lib/plugin-runtime/im/message-dispatcher.d.ts +1 -1
  30. package/lib/plugin-runtime/im/message-dispatcher.js +31 -4
  31. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +2 -0
  32. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +7 -0
  33. package/lib/plugin-runtime/im/outbound-segments.js +4 -3
  34. package/package.json +12 -12
@@ -1,9 +1,11 @@
1
1
  import type { JSXElement } from '@zhin.js/jsx';
2
+ import type { CommandMenuNavigation } from '@zhin.js/command';
2
3
  import type { PluginId, RuntimeSnapshot } from '@zhin.js/plugin-runtime';
3
- import { operationInputBinding } from '@zhin.js/feature-kit';
4
+ import { operationInputBinding, operationInputSignal } from '@zhin.js/feature-kit';
4
5
  import type { ConversationRef, DeliveryReceipt, MessageRef } from '@zhin.js/im-contract';
5
6
  import type { MessageOrigin } from './inbound-origin.js';
6
7
  import type { MediaRef, Segment } from '../../built/segment-contract/types.js';
8
+ import { interactionOperationScope, type InteractionOperationScope } from './interaction-operation.js';
7
9
  export type { MediaRef, Segment };
8
10
  declare const componentCallBrand: "zhin.component-call/1";
9
11
  declare const rawContentBrand: "zhin.raw-content/1";
@@ -102,7 +104,8 @@ export interface OutboundMessageService {
102
104
  }
103
105
  export interface MessageDispatchResult {
104
106
  readonly matched: boolean;
105
- readonly status?: 'unmatched' | 'invalid' | 'denied' | 'executed';
107
+ readonly status?: 'unmatched' | 'invalid' | 'denied' | 'limited' | 'executed';
108
+ readonly limit?: import('@zhin.js/command').CommandExecutionLimit;
106
109
  readonly issues?: readonly Readonly<{
107
110
  field: string;
108
111
  message: string;
@@ -111,6 +114,7 @@ export interface MessageDispatchResult {
111
114
  readonly command?: string;
112
115
  readonly owner?: PluginId;
113
116
  readonly value?: unknown;
117
+ readonly menu?: CommandMenuNavigation;
114
118
  }
115
119
  /** Canonical message value delivered to inbound middleware and handlers. */
116
120
  export interface MessageBase extends IncomingMessage {
@@ -173,9 +177,15 @@ export declare class RuntimeMessage implements MessageBase {
173
177
  snapshot?: RuntimeSnapshot;
174
178
  assertActive?: () => void;
175
179
  origin?: MessageOrigin;
180
+ retirementSignal?: AbortSignal;
181
+ registerOperationView?: (view: object, owner: PluginId) => void;
176
182
  }>);
177
183
  /** @internal A frozen view whose sends retain the current author's exact operation. */
178
184
  [operationInputBinding](owner: PluginId, snapshot: RuntimeSnapshot): RuntimeMessage;
185
+ /** @internal Retirement notification; ordinary leased sends may still drain. */
186
+ [operationInputSignal](): AbortSignal | undefined;
187
+ /** @internal The command/handler author's actual leased operation. */
188
+ [interactionOperationScope](): InteractionOperationScope | undefined;
179
189
  /** 平台原生消息 id(`message` 未提供时为 undefined)。 */
180
190
  get id(): string | undefined;
181
191
  readonly $reply: (content: SendContent) => Promise<DeliveryReceipt>;
@@ -1,4 +1,5 @@
1
- import { operationInputBinding } from '@zhin.js/feature-kit';
1
+ import { operationInputBinding, operationInputSignal } from '@zhin.js/feature-kit';
2
+ import { interactionOperationScope } from './interaction-operation.js';
2
3
  const componentCallBrand = 'zhin.component-call/1';
3
4
  const rawContentBrand = 'zhin.raw-content/1';
4
5
  export function component(name, props) {
@@ -51,6 +52,8 @@ export class RuntimeMessage {
51
52
  #author;
52
53
  #snapshot;
53
54
  #assertActive;
55
+ #retirementSignal;
56
+ #registerOperationView;
54
57
  origin;
55
58
  /** @internal Constructed only by the generation-owned IM Runtime. */
56
59
  constructor(conversation, content, generation, reply, sender, metadata = Object.freeze({}),
@@ -80,6 +83,10 @@ export class RuntimeMessage {
80
83
  this.#author = binding.requester;
81
84
  this.#snapshot = binding.snapshot;
82
85
  this.#assertActive = binding.assertActive;
86
+ this.#retirementSignal = binding.retirementSignal;
87
+ this.#registerOperationView = binding.registerOperationView;
88
+ if (binding.snapshot)
89
+ binding.registerOperationView?.(this, binding.requester ?? binding.snapshot.root);
83
90
  const requester = binding.requester;
84
91
  this.$reply = (content) => requester === undefined ? reply(content) : reply(content, requester);
85
92
  this.$replyFrom = (requester, content) => reply(content, requester);
@@ -126,7 +133,22 @@ export class RuntimeMessage {
126
133
  throw new Error(`Unknown Message author: ${owner}`);
127
134
  if (this.#author === owner && this.#snapshot === snapshot)
128
135
  return this;
129
- return new RuntimeMessage(this.conversation, this.content, this.generation, this.#reply, this.sender, this.metadata, this.segments, this.message, this.endpointId, this.mentioned, this.replyTo, this.#resolveClient, this.clientAdapter, { requester: owner, snapshot, assertActive: this.#assertActive, origin: this.origin });
136
+ return new RuntimeMessage(this.conversation, this.content, this.generation, this.#reply, this.sender, this.metadata, this.segments, this.message, this.endpointId, this.mentioned, this.replyTo, this.#resolveClient, this.clientAdapter, { requester: owner, snapshot, assertActive: this.#assertActive, origin: this.origin,
137
+ retirementSignal: this.#retirementSignal, registerOperationView: this.#registerOperationView });
138
+ }
139
+ /** @internal Retirement notification; ordinary leased sends may still drain. */
140
+ [operationInputSignal]() {
141
+ this.#assertActive?.();
142
+ return this.#retirementSignal;
143
+ }
144
+ /** @internal The command/handler author's actual leased operation. */
145
+ [interactionOperationScope]() {
146
+ this.#assertActive?.();
147
+ return this.#snapshot ? {
148
+ snapshot: this.#snapshot,
149
+ owner: this.#author ?? this.#snapshot.root,
150
+ retirementSignal: this.#retirementSignal,
151
+ } : undefined;
130
152
  }
131
153
  /** 平台原生消息 id(`message` 未提供时为 undefined)。 */
132
154
  get id() {
@@ -10,6 +10,7 @@ import type { OutboundRenderer } from './outbound-renderer.js';
10
10
  import { EndpointRuntime } from './endpoint-runtime.js';
11
11
  import { type RuntimeMessageEventSource } from './message-events.js';
12
12
  import { InboundDeduper } from './inbound-dedup.js';
13
+ import { type RuntimeMessageDiagnosticsReader } from './message-diagnostics.js';
13
14
  /** @public Stable inbound and outbound IM gateway token for Adapter integrations. */
14
15
  export declare const outboundMessageToken: import("@zhin.js/plugin-runtime").Token<OutboundMessageService>;
15
16
  export interface ImRuntimeOptions {
@@ -42,6 +43,7 @@ export interface ImRuntimeOptions {
42
43
  }
43
44
  export declare class ImRuntime implements OutboundMessageService {
44
45
  #private;
46
+ readonly messageDiagnostics: RuntimeMessageDiagnosticsReader;
45
47
  readonly messageEvents: RuntimeMessageEventSource;
46
48
  readonly endpoints: EndpointRuntime;
47
49
  constructor(options?: ImRuntimeOptions);
@@ -1,6 +1,7 @@
1
1
  import { createToken, generationAdmissionBinder, } from '@zhin.js/plugin-runtime';
2
2
  import { AsyncLocalStorage } from 'node:async_hooks';
3
3
  import { PermissionHost, permissionHostToken } from '@zhin.js/permission';
4
+ import { CommandExecutionBudget, commandExecutionAdmissionToken } from '@zhin.js/command/internal/execution';
4
5
  import { MessageBus, messageBusToken } from './message-bus.js';
5
6
  import { endpointEventGatewayToken, getProcessPeerRegistry, } from '@zhin.js/adapter';
6
7
  import { loginAssistToken } from './login-assist-host.js';
@@ -14,9 +15,17 @@ import { InboundRuntime } from './inbound-runtime.js';
14
15
  import { InboundDeduper } from './inbound-dedup.js';
15
16
  import { OutboundLedger, ledgerScopeOf } from './outbound-ledger.js';
16
17
  import { createInboundOriginClassifier } from './inbound-origin.js';
18
+ import { RuntimeMessageDiagnostics } from './message-diagnostics.js';
17
19
  /** @public Stable inbound and outbound IM gateway token for Adapter integrations. */
18
20
  export const outboundMessageToken = createToken('zhin.im.outbound-message');
19
21
  export class ImRuntime {
22
+ #commandExecution = new CommandExecutionBudget();
23
+ #commandOperation = new AsyncLocalStorage();
24
+ #messageDiagnostics = new RuntimeMessageDiagnostics();
25
+ messageDiagnostics = Object.freeze({
26
+ list: (options) => this.#messageDiagnostics.list(options),
27
+ get: (id) => this.#messageDiagnostics.get(id),
28
+ });
20
29
  #messageEventStream = new RuntimeMessageEventStream();
21
30
  messageEvents = Object.freeze({
22
31
  subscribe: (listener) => this.#messageEventStream.subscribe(listener),
@@ -38,6 +47,7 @@ export class ImRuntime {
38
47
  ledger,
39
48
  });
40
49
  this.#outbound = new OutboundDeliveryRuntime({
50
+ diagnostics: this.#messageDiagnostics,
41
51
  ledger: (conversation, receipt, payload) => ledger.record(conversation, receipt, payload),
42
52
  record: (request, receipt) => this.#conversations.recordOutbound(request, receipt),
43
53
  rememberFallback: (conversation, generation, map) => this.#interactions.rememberFallback(conversation, generation, map),
@@ -45,6 +55,9 @@ export class ImRuntime {
45
55
  prepareButtons: (request, snapshot) => this.#interactions.prepareButtons(request, snapshot),
46
56
  }, options.renderer);
47
57
  this.#inbound = new InboundRuntime({
58
+ commandExecution: this.#commandExecution,
59
+ runCommandOperation: (controller, operation) => this.#commandOperation.run(controller, operation),
60
+ diagnostics: this.#messageDiagnostics,
48
61
  interactions: this.#interactions,
49
62
  inboundClaim: options.inboundClaim,
50
63
  originClassifier,
@@ -55,7 +68,7 @@ export class ImRuntime {
55
68
  enrichSender: options.enrichSender,
56
69
  acquire: () => this.#acquire(),
57
70
  release: (lease) => this.#release(lease),
58
- deliver: (request, snapshot) => this.#outbound.deliver(request, snapshot),
71
+ deliver: (request, snapshot) => this.#deliver(request, snapshot),
59
72
  recordIncoming: (input, sender) => this.#conversations.recordIncoming(input, sender),
60
73
  recordNotice: (notice) => this.#conversations.recordNotice(notice),
61
74
  markInteractionAnswer: (message) => this.#conversations.markInteractionAnswer(message),
@@ -64,7 +77,7 @@ export class ImRuntime {
64
77
  this.endpoints = new EndpointRuntime({
65
78
  acquire: () => this.#acquire(),
66
79
  release: (lease) => this.#release(lease),
67
- send: (request, snapshot) => this.#outbound.deliver(request, snapshot),
80
+ send: (request, snapshot) => this.#deliver(request, snapshot),
68
81
  });
69
82
  }
70
83
  /** Process composition replaces the bootstrap memory store after required DB activation. */
@@ -120,6 +133,7 @@ export class ImRuntime {
120
133
  resources.provide(endpointEventGatewayToken, this.endpointEvents);
121
134
  resources.provide(loginAssistToken, this.loginAssist);
122
135
  resources.provide(permissionHostToken, this.permissionHost);
136
+ resources.provide(commandExecutionAdmissionToken, this.#commandExecution.admission);
123
137
  resources.provide(messageBusToken, this.messageBus);
124
138
  }
125
139
  /** The sole Adapter ingress, also installed as endpointEventGatewayToken. */
@@ -160,7 +174,7 @@ export class ImRuntime {
160
174
  async send(request) {
161
175
  const lease = this.#acquire();
162
176
  try {
163
- return await this.#outbound.deliver(request, lease.value);
177
+ return await this.#deliver(request, lease.value);
164
178
  }
165
179
  finally {
166
180
  this.#release(lease);
@@ -171,7 +185,7 @@ export class ImRuntime {
171
185
  if (!this.#snapshots?.owns(lease) || !lease.active) {
172
186
  throw new Error('Outbound message generation lease expired');
173
187
  }
174
- return this.#outbound.deliver(request, lease.value);
188
+ return this.#deliver(request, lease.value);
175
189
  }
176
190
  /**
177
191
  * Console `GET /api/stats` 同源计数:非 root 插件节点 + AdapterIndex endpoints。
@@ -203,6 +217,12 @@ export class ImRuntime {
203
217
  });
204
218
  }
205
219
  }
220
+ /** All unified outbound entry points observe the actual delivery, never their own caller/drain promise. */
221
+ #deliver(request, snapshot) {
222
+ const delivery = this.#outbound.deliver(request, snapshot);
223
+ this.#commandOperation.getStore()?.trackSend(delivery);
224
+ return delivery;
225
+ }
206
226
  #acquire() {
207
227
  const inherited = this.#operationSnapshot.getStore();
208
228
  if (inherited?.active)
@@ -2,14 +2,19 @@ import type { GenerationAdmissionGate, RuntimeSnapshot, SnapshotLease } from '@z
2
2
  import type { EndpointEvent, PeerRegistry } from '@zhin.js/adapter';
3
3
  import type { DeliveryReceipt } from '@zhin.js/im-contract';
4
4
  import type { UserInteraction } from '@zhin.js/interaction';
5
+ import type { CommandExecutionBudget, CommandOperationController } from '@zhin.js/command/internal/execution';
5
6
  import { type Notice } from '../../notice.js';
6
7
  import { type Message, type IncomingMessage, type MessageSenderRef, type SendRequest } from './contracts.js';
8
+ import type { RuntimeMessageDiagnostics } from './message-diagnostics.js';
7
9
  import type { RuntimeInteractionCoordinator } from './interaction-runtime.js';
8
10
  import { type RuntimeMessageEvent } from './message-events.js';
9
11
  import type { InboundDeduper } from './inbound-dedup.js';
10
12
  import { type InboundOriginClassifier } from './inbound-origin.js';
11
13
  type InboundInteractionPort = Pick<RuntimeInteractionCoordinator<GenerationAdmissionGate>, 'create' | 'createForMessage' | 'createFromUnknown' | 'dispatch' | 'resolveClaim'> & Partial<Pick<RuntimeInteractionCoordinator<GenerationAdmissionGate>, 'hasPendingAnswer'>>;
12
14
  interface InboundRuntimeContext {
15
+ readonly commandExecution?: CommandExecutionBudget;
16
+ runCommandOperation?<T>(controller: CommandOperationController, operation: () => T): T;
17
+ readonly diagnostics?: RuntimeMessageDiagnostics;
13
18
  readonly interactions: InboundInteractionPort;
14
19
  readonly inboundClaim?: (message: Message) => boolean | Promise<boolean>;
15
20
  /** Trusted origin classifier. Absent: messages carry no origin (legacy behavior). */