@zhin.js/core 1.1.53 → 1.1.55

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 (41) hide show
  1. package/README.md +13 -26
  2. package/lib/built/interaction-messages.d.ts +1 -1
  3. package/lib/built/login-assist.d.ts +12 -3
  4. package/lib/built/login-assist.js +32 -26
  5. package/lib/feature/handler.d.ts +12 -2
  6. package/lib/feature/handler.js +2 -1
  7. package/lib/index.d.ts +3 -1
  8. package/lib/index.js +2 -0
  9. package/lib/jsx/message-tags.d.ts +3 -3
  10. package/lib/jsx/message-tags.js +3 -0
  11. package/lib/plugin-runtime/im/captured-endpoint-operation.d.ts +34 -0
  12. package/lib/plugin-runtime/im/captured-endpoint-operation.js +1 -0
  13. package/lib/plugin-runtime/im/command-menu-navigation.d.ts +8 -1
  14. package/lib/plugin-runtime/im/command-menu-navigation.js +3 -3
  15. package/lib/plugin-runtime/im/contracts.d.ts +69 -48
  16. package/lib/plugin-runtime/im/contracts.js +65 -58
  17. package/lib/plugin-runtime/im/conversation-runtime.d.ts +2 -0
  18. package/lib/plugin-runtime/im/conversation-runtime.js +41 -4
  19. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +13 -15
  20. package/lib/plugin-runtime/im/endpoint-runtime.js +194 -42
  21. package/lib/plugin-runtime/im/group-rule-violation.d.ts +5 -0
  22. package/lib/plugin-runtime/im/im-runtime.d.ts +3 -0
  23. package/lib/plugin-runtime/im/im-runtime.js +33 -7
  24. package/lib/plugin-runtime/im/inbound-runtime.d.ts +2 -0
  25. package/lib/plugin-runtime/im/inbound-runtime.js +138 -33
  26. package/lib/plugin-runtime/im/index.d.ts +2 -1
  27. package/lib/plugin-runtime/im/interaction-operation.d.ts +5 -1
  28. package/lib/plugin-runtime/im/interaction-runtime.d.ts +12 -2
  29. package/lib/plugin-runtime/im/interaction-runtime.js +50 -35
  30. package/lib/plugin-runtime/im/message-dispatcher.js +2 -2
  31. package/lib/plugin-runtime/im/operation-endpoint.d.ts +6 -0
  32. package/lib/plugin-runtime/im/operation-endpoint.js +2 -0
  33. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +16 -2
  34. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +86 -20
  35. package/lib/plugin-runtime/im/outbound-renderer.d.ts +12 -3
  36. package/lib/plugin-runtime/im/outbound-renderer.js +20 -13
  37. package/lib/plugin-runtime/im/progressive-reply.d.ts +52 -0
  38. package/lib/plugin-runtime/im/progressive-reply.js +495 -0
  39. package/lib/plugin-runtime/im/runtime-middleware.d.ts +2 -0
  40. package/lib/plugin-runtime/im/runtime-middleware.js +5 -0
  41. package/package.json +15 -15
@@ -1,4 +1,7 @@
1
- import { adapterFeatureId, isAdapterIndex, resolveEndpointManagement, } from '@zhin.js/adapter';
1
+ import { operationEndpoint } from './operation-endpoint.js';
2
+ import { createCoreReplyStream } from './progressive-reply.js';
3
+ import { supportsIncrementalOutbound } from './runtime-middleware.js';
4
+ import { adapterFeatureId, isAdapterIndex, } from '@zhin.js/adapter';
2
5
  import { normalizeDeliveryTarget } from '@zhin.js/plugin-runtime';
3
6
  import { EndpointDeliveryError, } from '@zhin.js/im-contract';
4
7
  /** Owns generation-leased Endpoint discovery, control, management, and Console delivery. */
@@ -27,14 +30,14 @@ export class EndpointRuntime {
27
30
  capabilities(input) {
28
31
  return this.#withLease((snapshot) => {
29
32
  const index = requireAdapters(snapshot);
30
- const id = resolveEndpointIdentity(snapshot, index, input.adapter, input.endpointKey);
33
+ const id = index.resolve(input.adapter, input.endpointKey);
31
34
  return id ? index.capabilities(id) : undefined;
32
35
  }, undefined);
33
36
  }
34
37
  get(adapter, endpointKey) {
35
38
  return this.#withLease((snapshot) => {
36
39
  const index = requireAdapters(snapshot);
37
- const id = resolveEndpointIdentity(snapshot, index, adapter, endpointKey);
40
+ const id = index.resolve(adapter, endpointKey);
38
41
  if (!id)
39
42
  return null;
40
43
  const row = index.describe().find((item) => item.id === id);
@@ -55,11 +58,148 @@ export class EndpointRuntime {
55
58
  });
56
59
  }, null);
57
60
  }
61
+ /** Retain the actual ingress Client instead of resolving the latest account when work settles. */
62
+ captureOperation(input) {
63
+ const target = normalizeDeliveryTarget(input.target);
64
+ const snapshot = this.context.retain();
65
+ let child;
66
+ try {
67
+ const index = requireAdapters(snapshot.value);
68
+ const id = target.endpoint.id;
69
+ if (index.clientAdapter(id) !== target.endpoint.adapter) {
70
+ throw new EndpointDeliveryError('endpoint_identity_mismatch', 'Source belongs to another adapter.', 'not_sent');
71
+ }
72
+ const callerSignal = input.signal
73
+ ? AbortSignal.any([snapshot.stopSignal, input.signal]) : snapshot.stopSignal;
74
+ child = index.acquireInbound(id, input.client, callerSignal);
75
+ const captured = child;
76
+ let released = false;
77
+ let calls = 0;
78
+ const releaseResources = () => {
79
+ try {
80
+ captured.release();
81
+ }
82
+ finally {
83
+ if (snapshot.active)
84
+ this.context.release(snapshot);
85
+ }
86
+ };
87
+ const assertActive = () => {
88
+ if (released || !snapshot.active)
89
+ throw new EndpointDeliveryError('operation_scope_ended', 'Retained operation has ended.', 'not_sent');
90
+ captured.assertActive();
91
+ };
92
+ // The capture stops admitting on release; accepted calls retain its source and Root to drain.
93
+ const retainCall = () => {
94
+ calls += 1;
95
+ return () => { if (--calls === 0 && released)
96
+ releaseResources(); };
97
+ };
98
+ const scope = { assertActive, signal: captured.signal, retain: retainCall };
99
+ const requester = input.requester ?? index.owner(id);
100
+ const capabilities = index.capabilities(id);
101
+ const declared = ['recall', 'edit', 'reaction', 'typing'].find(operation => capabilities.operations?.[operation]);
102
+ const port = declared ? index.control(id, declared, scope) : undefined;
103
+ const controls = port && Object.freeze({
104
+ ...(port.recall ? { recall: async (request) => {
105
+ assertSameEndpoint(target, request.message.conversation);
106
+ return port.recall(request);
107
+ } } : {}),
108
+ ...(port.edit ? { edit: async (request) => {
109
+ assertSameEndpoint(target, request.message.conversation);
110
+ return port.edit(request);
111
+ } } : {}),
112
+ ...(port.addReaction ? { addReaction: async (request) => {
113
+ assertSameEndpoint(target, request.message.conversation);
114
+ return port.addReaction(request);
115
+ } } : {}),
116
+ ...(port.removeReaction ? { removeReaction: async (request) => {
117
+ assertSameEndpoint(target, request.message.conversation);
118
+ return port.removeReaction(request);
119
+ } } : {}),
120
+ ...(port.typing ? { typing: async (request) => {
121
+ assertSameEndpoint(target, request.conversation);
122
+ return port.typing(request);
123
+ } } : {}),
124
+ });
125
+ return Object.freeze({
126
+ target, signal: captured.signal, control: controls,
127
+ management: index.management(id, scope),
128
+ profile: index.profile(id, scope),
129
+ send: async (content, options) => {
130
+ assertActive();
131
+ // Retain the source authority, while each send owns its caller cancellation.
132
+ const releaseCall = retainCall();
133
+ try {
134
+ const sendLease = index.acquire(id, options?.signal
135
+ ? AbortSignal.any([captured.signal, options.signal]) : captured.signal, captured.endpoint);
136
+ try {
137
+ return await this.context.send({ conversation: target, requester, content,
138
+ ...(options?.locale ? { locale: options.locale } : {}), [operationEndpoint]: sendLease }, snapshot.value);
139
+ }
140
+ finally {
141
+ sendLease.release();
142
+ }
143
+ }
144
+ finally {
145
+ releaseCall();
146
+ }
147
+ },
148
+ openReplyStream: (options = {}) => {
149
+ assertActive();
150
+ if (!captured.streaming || !supportsIncrementalOutbound(snapshot.value, target.endpoint.adapter))
151
+ return undefined;
152
+ if (captured.streaming.supports?.({ conversation: target, source: options.source }) === false)
153
+ return undefined;
154
+ const releaseCall = retainCall();
155
+ let streamLease;
156
+ try {
157
+ streamLease = index.acquire(id, captured.signal, captured.endpoint);
158
+ const retained = streamLease;
159
+ return createCoreReplyStream({ endpoint: retained, streaming: retained.streaming, options,
160
+ deliver: (content, execution) => {
161
+ // Opening admitted this whole stream; capture.release only closes new admission.
162
+ retained.assertActive();
163
+ return this.context.send({ conversation: target, requester, content,
164
+ ...(options.locale ? { locale: options.locale } : {}), [operationEndpoint]: retained }, snapshot.value, execution);
165
+ },
166
+ release: () => { try {
167
+ retained.release();
168
+ }
169
+ finally {
170
+ releaseCall();
171
+ } }, });
172
+ }
173
+ catch (error) {
174
+ streamLease?.release();
175
+ releaseCall();
176
+ throw error;
177
+ }
178
+ },
179
+ release: () => {
180
+ if (released)
181
+ return;
182
+ released = true;
183
+ if (!calls)
184
+ releaseResources();
185
+ },
186
+ });
187
+ }
188
+ catch (error) {
189
+ try {
190
+ child?.release();
191
+ }
192
+ finally {
193
+ this.context.release(snapshot);
194
+ }
195
+ throw error;
196
+ }
197
+ }
58
198
  async send(input) {
59
199
  const lease = this.context.acquire();
60
200
  try {
61
201
  const index = requireAdapters(lease.value);
62
- const resolved = resolveEndpointIdentity(lease.value, index, input.adapter, input.endpointKey);
202
+ const resolved = index.resolve(input.adapter, input.endpointKey);
63
203
  if (!resolved)
64
204
  throw new Error('endpoint not found');
65
205
  const requester = index.owner(resolved);
@@ -112,22 +252,26 @@ export class EndpointRuntime {
112
252
  }
113
253
  }
114
254
  async addReaction(input) {
115
- return this.#withControl(input.adapter, input.endpointKey, 'reaction', (control) => control.addReaction?.(input.message, input.emoji, {
116
- sceneType: input.sceneType,
117
- channelId: input.channelId,
118
- }) ?? null, null);
255
+ return this.#withControl({ target: input.message.conversation, method: 'addReaction',
256
+ run: control => control.addReaction({ message: input.message, emoji: input.emoji, hint: {
257
+ sceneType: input.sceneType, channelId: input.channelId,
258
+ } }) });
119
259
  }
120
260
  async removeReaction(input) {
121
- await this.#withControl(input.adapter, input.endpointKey, 'reaction', (control) => control.removeReaction?.(input.message, input.reactionId), undefined);
261
+ await this.#withControl({ target: input.message.conversation, method: 'removeReaction',
262
+ run: control => control.removeReaction({ message: input.message, reactionId: input.reactionId }) });
122
263
  }
123
264
  async recall(input) {
124
- await this.#withControl(input.adapter, input.endpointKey, 'recall', (control) => control.recall?.(input.message), undefined);
265
+ await this.#withControl({ target: input.message.conversation, method: 'recall',
266
+ run: control => control.recall({ message: input.message }) });
125
267
  }
126
268
  async edit(input) {
127
- return this.#withControl(input.adapter, input.endpointKey, 'edit', (control) => control.edit?.(input.message, input.content) ?? null, null);
269
+ return this.#withControl({ target: input.message.conversation, method: 'edit',
270
+ run: control => control.edit({ message: input.message, content: input.content }) });
128
271
  }
129
272
  async typing(input) {
130
- await this.#withControl(input.adapter, input.endpointKey, 'typing', (control) => control.typing?.(input.conversation, input.active), undefined);
273
+ await this.#withControl({ target: input.conversation, method: 'typing',
274
+ run: control => control.typing({ conversation: input.conversation, active: input.active }) });
131
275
  }
132
276
  async withManagement(adapter, endpointKey, run) {
133
277
  let lease;
@@ -139,29 +283,44 @@ export class EndpointRuntime {
139
283
  }
140
284
  try {
141
285
  const index = requireAdapters(lease.value);
142
- const id = resolveEndpointIdentity(lease.value, index, adapter, endpointKey);
286
+ const id = index.resolve(adapter, endpointKey);
143
287
  const endpoint = id ? index.connection(String(id), String(id)) : undefined;
144
288
  if (!endpoint)
145
289
  return null;
146
- return await run(resolveEndpointManagement(endpoint) ?? Object.freeze({}));
290
+ return await run(index.management(id, { assertActive: () => {
291
+ if (!lease.active)
292
+ throw new EndpointDeliveryError('operation_scope_ended', 'Management operation scope has ended', 'not_sent');
293
+ } }) ?? Object.freeze({}));
147
294
  }
148
295
  finally {
149
296
  this.context.release(lease);
150
297
  }
151
298
  }
152
- async #withControl(adapter, endpointKey, operation, run, fallback) {
153
- let lease;
154
- try {
155
- lease = this.context.acquire();
156
- }
157
- catch {
158
- return fallback;
159
- }
299
+ async #withControl({ target: source, method, run }) {
300
+ const target = normalizeDeliveryTarget(source);
301
+ const lease = this.context.acquire();
160
302
  try {
161
303
  const index = requireAdapters(lease.value);
162
- const id = resolveEndpointIdentity(lease.value, index, adapter, endpointKey);
163
- const control = id ? index.control(id, operation) : undefined;
164
- return control ? await run(control) : fallback;
304
+ const id = target.endpoint.id;
305
+ let actualAdapter;
306
+ try {
307
+ actualAdapter = index.clientAdapter(id);
308
+ }
309
+ catch {
310
+ throw new EndpointDeliveryError('endpoint_not_found', 'The referenced account no longer exists.', 'not_sent');
311
+ }
312
+ if (actualAdapter !== target.endpoint.adapter) {
313
+ throw new EndpointDeliveryError('endpoint_identity_mismatch', 'Message reference belongs to another adapter.', 'not_sent');
314
+ }
315
+ const operation = method === 'addReaction' || method === 'removeReaction' ? 'reaction' : method;
316
+ const control = index.control(id, operation, { assertActive: () => {
317
+ if (!lease.active)
318
+ throw new EndpointDeliveryError('operation_scope_ended', 'Control operation has ended.', 'not_sent');
319
+ } });
320
+ if (!control || typeof control[method] !== 'function') {
321
+ throw new EndpointDeliveryError('unsupported_operation', `Endpoint does not support ${method}.`, 'not_sent');
322
+ }
323
+ return await run(control);
165
324
  }
166
325
  finally {
167
326
  this.context.release(lease);
@@ -193,21 +352,6 @@ export function requireAdapters(snapshot) {
193
352
  }
194
353
  return projection;
195
354
  }
196
- /** Resolves public list identities against the exact operation snapshot; ambiguous aliases never pick an account. */
197
- export function resolveEndpointIdentity(snapshot, index, adapter, endpointKey) {
198
- const live = new Map(index.describe().map(row => [row.id, row.name]));
199
- const matches = index.list().filter(record => {
200
- const localName = record.id.split('\0').pop() ?? record.id;
201
- const alias = adapterTypeName(snapshot.tree.get(record.owner)?.packageName);
202
- const adapterMatches = index.clientAdapter(record.id) === adapter
203
- || alias === adapter || record.name === adapter || String(record.id) === adapter
204
- || localName === adapter || String(record.owner) === adapter || record.owner.endsWith(`/${adapter}`);
205
- const endpointMatches = record.name === endpointKey || String(record.id) === endpointKey
206
- || record.id.endsWith(`/${endpointKey}`) || live.get(record.id) === endpointKey;
207
- return adapterMatches && endpointMatches;
208
- });
209
- return matches.length === 1 ? matches[0].id : undefined;
210
- }
211
355
  export function adapterTypeName(packageName) {
212
356
  if (!packageName)
213
357
  return undefined;
@@ -223,7 +367,15 @@ function normalizeConsoleContent(content) {
223
367
  function requireSentReceipt(result) {
224
368
  if (result.status !== 'sent') {
225
369
  throw new EndpointDeliveryError(result.failure?.code ?? `outbound_${result.status}`, result.failure?.message ?? 'Outbound delivery was suppressed.', result.failure?.deliveryUnknown ? 'unknown'
226
- : result.status === 'rejected' ? 'rejected' : 'not_sent');
370
+ : result.status === 'rejected' ? 'rejected' : 'not_sent', { receipt: result, ...(result.parts ? { parts: result.parts.map(part => ({
371
+ ...(part.message ? { messageId: part.message.id } : {}), ...(part.sourceIndices ? { sourceIndices: part.sourceIndices } : {}),
372
+ })) } : {}) });
373
+ }
374
+ return { messageId: result.message?.id ?? '', receipt: result };
375
+ }
376
+ /** A canonical message/address cannot be redirected by a second endpoint selector. */
377
+ function assertSameEndpoint(expected, actual) {
378
+ if (expected.endpoint.id !== actual.endpoint.id || expected.endpoint.adapter !== actual.endpoint.adapter) {
379
+ throw new EndpointDeliveryError('endpoint_identity_mismatch', 'Message reference belongs to another Endpoint.', 'not_sent');
227
380
  }
228
- return { messageId: result.message?.id ?? '' };
229
381
  }
@@ -3,6 +3,11 @@ import type { ConversationRef } from '@zhin.js/im-contract';
3
3
  export type GroupRuleViolationTrigger = 'advertising' | 'abuse';
4
4
  /** @public */
5
5
  export interface GroupRuleViolationReport {
6
+ /** Runtime-only authority captured from the actual incoming Client; never persist this object. */
7
+ readonly source: Readonly<{
8
+ client: unknown;
9
+ signal: AbortSignal;
10
+ }>;
6
11
  /** 违规消息所在的群会话(`kind` 必须是 `group`)。 */
7
12
  readonly conversation: ConversationRef;
8
13
  readonly memberId: string;
@@ -7,6 +7,7 @@ import { Message, type IncomingMessage, type OutboundMessageService, type Messag
7
7
  import { LoginAssist } from '../../built/login-assist.js';
8
8
  import type { UserInteraction } from '@zhin.js/interaction';
9
9
  import type { OutboundRenderer } from './outbound-renderer.js';
10
+ import { type PendingUserInteraction } from './interaction-runtime.js';
10
11
  import { EndpointRuntime } from './endpoint-runtime.js';
11
12
  import { type RuntimeMessageEventSource, type RuntimeSceneEventSource } from './message-events.js';
12
13
  import { InboundDeduper } from './inbound-dedup.js';
@@ -82,6 +83,8 @@ export declare class ImRuntime implements OutboundMessageService {
82
83
  createInteraction(message: Message, bind?: {
83
84
  readonly subjectId: string;
84
85
  }): UserInteraction | undefined;
86
+ /** Reads a prompt within the caller's current generation lease; answering uses the original IM ingress. */
87
+ readPendingInteraction(conversation: ConversationRef, subjectId: string, generation: number): PendingUserInteraction | undefined;
85
88
  send(request: SendRequest): Promise<DeliveryReceipt>;
86
89
  /** Sends through the exact generation lease that admitted the current operation. */
87
90
  sendWithSnapshotLease(lease: SnapshotLease, request: SendRequest): Promise<DeliveryReceipt>;
@@ -1,4 +1,4 @@
1
- import { createToken, generationAdmissionBinder, } from '@zhin.js/plugin-runtime';
1
+ import { OwnerOperation, 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
4
  import { CommandExecutionBudget, commandExecutionAdmissionToken } from '@zhin.js/command/internal/execution';
@@ -56,6 +56,7 @@ export class ImRuntime {
56
56
  diagnostics: this.#messageDiagnostics,
57
57
  ledger: (conversation, receipt, payload) => ledger.record(conversation, receipt, payload),
58
58
  record: (request, receipt, canonicalContent) => this.#conversations.recordOutbound(request, receipt, canonicalContent),
59
+ recordUpdate: (request, receipt, revision, canonicalContent) => this.#conversations.recordOutboundUpdate(request, receipt, revision, canonicalContent),
59
60
  rememberFallback: (conversation, generation, map) => this.#interactions.rememberFallback(conversation, generation, map),
60
61
  publish: (event) => this.#messageEventStream.publish(event),
61
62
  prepareButtons: (request, snapshot) => this.#interactions.prepareButtons(request, snapshot),
@@ -65,6 +66,7 @@ export class ImRuntime {
65
66
  runCommandOperation: (controller, operation) => this.#commandOperation.run(controller, operation),
66
67
  diagnostics: this.#messageDiagnostics,
67
68
  interactions: this.#interactions,
69
+ captureOperation: input => this.endpoints.captureOperation(input),
68
70
  inboundClaim: options.inboundClaim,
69
71
  originClassifier,
70
72
  deduper,
@@ -83,8 +85,9 @@ export class ImRuntime {
83
85
  }, options.commandPrefix);
84
86
  this.endpoints = new EndpointRuntime({
85
87
  acquire: () => this.#acquire(),
88
+ retain: () => this.#retainSnapshot(),
86
89
  release: (lease) => this.#release(lease),
87
- send: (request, snapshot) => this.#deliver(request, snapshot),
90
+ send: (request, snapshot, execution) => this.#deliver(request, snapshot, execution),
88
91
  });
89
92
  }
90
93
  /** Process composition replaces the bootstrap memory store after required DB activation. */
@@ -160,13 +163,26 @@ export class ImRuntime {
160
163
  self.#release(lease);
161
164
  }
162
165
  };
166
+ const runWithReceipt = (receive, gate) => self.runWithSnapshotView(async () => {
167
+ const lease = self.#acquire();
168
+ return receive(Object.freeze({ receive: event => {
169
+ if (!lease.active)
170
+ throw new Error('Endpoint receipt scope has ended');
171
+ lease.stopSignal.throwIfAborted();
172
+ return self.#operationSnapshot.run(lease, () => self.#inbound.receive(event, gate));
173
+ } }));
174
+ });
163
175
  const gateway = {
164
- receive: (event) => self.#inbound.receive(event),
176
+ run: runWithReceipt,
177
+ receive: event => self.runWithSnapshotView(() => self.#inbound.receive(event)),
165
178
  resolveLocale,
166
179
  [generationAdmissionBinder](gate) {
180
+ const owner = new OwnerOperation(gate, 'Endpoint ingress');
181
+ const run = (receive) => owner.run(() => runWithReceipt(receive, gate));
167
182
  return Object.freeze({
168
- receive: async (event) => gate.enter(() => self.#inbound.receive(event, gate)),
169
- resolveLocale: async (endpoint, senderId) => gate.enter(() => resolveLocale(endpoint, senderId)),
183
+ run,
184
+ receive: event => run(() => self.#inbound.receive(event, gate)),
185
+ resolveLocale: (endpoint, senderId) => run(() => resolveLocale(endpoint, senderId)),
170
186
  });
171
187
  },
172
188
  };
@@ -194,6 +210,10 @@ export class ImRuntime {
194
210
  createInteraction(message, bind) {
195
211
  return this.#inbound.createInteraction(message, bind);
196
212
  }
213
+ /** Reads a prompt within the caller's current generation lease; answering uses the original IM ingress. */
214
+ readPendingInteraction(conversation, subjectId, generation) {
215
+ return this.#interactions.readPendingInteraction(conversation, subjectId, generation);
216
+ }
197
217
  async send(request) {
198
218
  const lease = this.#acquire();
199
219
  try {
@@ -241,8 +261,8 @@ export class ImRuntime {
241
261
  }
242
262
  }
243
263
  /** All unified outbound entry points observe the actual delivery, never their own caller/drain promise. */
244
- #deliver(request, snapshot) {
245
- const delivery = this.#outbound.deliver(request, snapshot);
264
+ #deliver(request, snapshot, execution) {
265
+ const delivery = this.#outbound.deliver(request, snapshot, execution);
246
266
  this.#commandOperation.getStore()?.trackSend(delivery);
247
267
  return delivery;
248
268
  }
@@ -260,6 +280,12 @@ export class ImRuntime {
260
280
  selfIds: Object.freeze([...new Set(selfIds)]),
261
281
  }));
262
282
  }
283
+ #retainSnapshot() {
284
+ if (!this.#snapshots)
285
+ throw new Error('ImRuntime is not attached to a Root');
286
+ const inherited = this.#operationSnapshot.getStore();
287
+ return inherited?.active ? this.#snapshots.retain(inherited) : this.#snapshots.acquire();
288
+ }
263
289
  #acquire() {
264
290
  const inherited = this.#operationSnapshot.getStore();
265
291
  if (inherited?.active)
@@ -1,3 +1,4 @@
1
+ import type { CaptureEndpointOperationInput, CapturedEndpointOperation } from './captured-endpoint-operation.js';
1
2
  import type { GenerationAdmissionGate, RuntimeSnapshot, SnapshotLease } from '@zhin.js/plugin-runtime';
2
3
  import type { EndpointEvent, PeerRegistry } from '@zhin.js/adapter';
3
4
  import type { DeliveryReceipt } from '@zhin.js/im-contract';
@@ -17,6 +18,7 @@ interface InboundRuntimeContext {
17
18
  runCommandOperation?<T>(controller: CommandOperationController, operation: () => T): T;
18
19
  readonly diagnostics?: RuntimeMessageDiagnostics;
19
20
  readonly interactions: InboundInteractionPort;
21
+ captureOperation(input: CaptureEndpointOperationInput): CapturedEndpointOperation;
20
22
  readonly inboundClaim?: (message: Message) => boolean | Promise<boolean>;
21
23
  /** Trusted origin classifier. Absent: messages carry no origin (legacy behavior). */
22
24
  readonly originClassifier?: InboundOriginClassifier;