@zhin.js/core 1.1.56 → 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.
- package/lib/built/generic-segment-mapper.js +2 -0
- package/lib/built/html-to-text.js +2 -2
- package/lib/built/rich-segments/markdown-to-text.js +19 -0
- package/lib/built/segment-contract/validate.d.ts +1442 -300
- package/lib/built/user-interaction.d.ts +1 -1
- package/lib/built/user-interaction.js +3 -1
- package/lib/feature/command.d.ts +2 -4
- package/lib/feature/command.js +1 -2
- package/lib/feature/middleware.d.ts +2 -2
- package/lib/feature/middleware.js +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -0
- package/lib/plugin-runtime/im/command-feedback.d.ts +20 -1
- package/lib/plugin-runtime/im/command-feedback.js +63 -0
- package/lib/plugin-runtime/im/contracts.d.ts +60 -5
- package/lib/plugin-runtime/im/contracts.js +6 -2
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +2 -2
- package/lib/plugin-runtime/im/conversation-runtime.js +6 -4
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +2 -3
- package/lib/plugin-runtime/im/endpoint-runtime.js +2 -2
- package/lib/plugin-runtime/im/host/component-host-installer.d.ts +2 -0
- package/lib/plugin-runtime/im/host/component-host-installer.js +14 -0
- package/lib/plugin-runtime/im/host/conversation-migration.d.ts +5 -0
- package/lib/plugin-runtime/im/host/conversation-migration.js +75 -0
- package/lib/plugin-runtime/im/host/database-config.d.ts +10 -0
- package/lib/plugin-runtime/im/host/database-config.js +114 -0
- package/lib/plugin-runtime/im/host/database-host-installer.d.ts +13 -0
- package/lib/plugin-runtime/im/host/database-host-installer.js +389 -0
- package/lib/plugin-runtime/im/host/database-state.d.ts +9 -0
- package/lib/plugin-runtime/im/host/database-state.js +91 -0
- package/lib/plugin-runtime/im/host/index.d.ts +8 -0
- package/lib/plugin-runtime/im/host/index.js +8 -0
- package/lib/plugin-runtime/im/host/locale-host-installer.d.ts +3 -0
- package/lib/plugin-runtime/im/host/locale-host-installer.js +60 -0
- package/lib/plugin-runtime/im/host/outbound-host-installer.d.ts +8 -0
- package/lib/plugin-runtime/im/host/outbound-host-installer.js +153 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.d.ts +33 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.js +215 -0
- package/lib/plugin-runtime/im/im-runtime.d.ts +7 -3
- package/lib/plugin-runtime/im/im-runtime.js +9 -7
- package/lib/plugin-runtime/im/inbound-dedup.d.ts +33 -0
- package/lib/plugin-runtime/im/inbound-dedup.js +139 -1
- package/lib/plugin-runtime/im/inbound-origin.d.ts +51 -2
- package/lib/plugin-runtime/im/inbound-origin.js +108 -1
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +10 -13
- package/lib/plugin-runtime/im/inbound-runtime.js +88 -349
- package/lib/plugin-runtime/im/inbound-spine.d.ts +194 -0
- package/lib/plugin-runtime/im/inbound-spine.js +333 -0
- package/lib/plugin-runtime/im/index.d.ts +5 -7
- package/lib/plugin-runtime/im/index.js +5 -6
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +8 -10
- package/lib/plugin-runtime/im/interaction-runtime.js +86 -28
- package/lib/plugin-runtime/im/interactive-router.d.ts +55 -8
- package/lib/plugin-runtime/im/interactive-router.js +244 -35
- package/lib/plugin-runtime/im/message-dispatcher.d.ts +1 -1
- package/lib/plugin-runtime/im/message-dispatcher.js +58 -7
- package/lib/plugin-runtime/im/message-events.d.ts +33 -3
- package/lib/plugin-runtime/im/message-events.js +34 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +4 -4
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +75 -29
- package/lib/plugin-runtime/im/outbound-renderer.d.ts +32 -2
- package/lib/plugin-runtime/im/outbound-renderer.js +95 -4
- package/lib/runtime.d.ts +2 -0
- package/lib/runtime.js +2 -0
- package/lib/venue/contracts.d.ts +21 -0
- package/lib/venue/contracts.js +3 -0
- package/lib/venue/floor-controller.d.ts +11 -0
- package/lib/venue/floor-controller.js +63 -0
- package/lib/venue/index.d.ts +13 -0
- package/lib/venue/index.js +11 -0
- package/lib/venue/membership-port.d.ts +23 -0
- package/lib/venue/membership-port.js +141 -0
- package/lib/venue/projector.d.ts +38 -0
- package/lib/venue/projector.js +45 -0
- package/lib/venue/registry.d.ts +16 -0
- package/lib/venue/registry.js +63 -0
- package/lib/venue/router.d.ts +19 -0
- package/lib/venue/router.js +101 -0
- package/lib/venue/send-limiter.d.ts +17 -0
- package/lib/venue/send-limiter.js +73 -0
- package/lib/venue/validator.d.ts +6 -0
- package/lib/venue/validator.js +160 -0
- package/package.json +16 -17
- package/lib/plugin-runtime/im/captured-endpoint-operation.d.ts +0 -34
- package/lib/plugin-runtime/im/captured-endpoint-operation.js +0 -1
- package/lib/plugin-runtime/im/command-menu-navigation.d.ts +0 -13
- package/lib/plugin-runtime/im/command-menu-navigation.js +0 -43
- package/lib/plugin-runtime/im/command-menu-presentation.d.ts +0 -14
- package/lib/plugin-runtime/im/command-menu-presentation.js +0 -25
- package/lib/plugin-runtime/im/group-rule-violation.d.ts +0 -52
- package/lib/plugin-runtime/im/group-rule-violation.js +0 -10
- package/lib/plugin-runtime/im/ingress-route.d.ts +0 -10
- package/lib/plugin-runtime/im/ingress-route.js +0 -10
- package/lib/plugin-runtime/im/interaction-operation.d.ts +0 -12
- package/lib/plugin-runtime/im/interaction-operation.js +0 -2
- package/lib/plugin-runtime/im/jsx-button-registry.d.ts +0 -17
- package/lib/plugin-runtime/im/jsx-button-registry.js +0 -98
- package/lib/plugin-runtime/im/locale.d.ts +0 -5
- package/lib/plugin-runtime/im/locale.js +0 -12
- package/lib/plugin-runtime/im/login-assist-host.d.ts +0 -6
- package/lib/plugin-runtime/im/login-assist-host.js +0 -6
- package/lib/plugin-runtime/im/member-welcome.d.ts +0 -8
- package/lib/plugin-runtime/im/member-welcome.js +0 -11
- package/lib/plugin-runtime/im/message-bus.d.ts +0 -38
- package/lib/plugin-runtime/im/message-bus.js +0 -40
- package/lib/plugin-runtime/im/operation-endpoint.d.ts +0 -6
- package/lib/plugin-runtime/im/operation-endpoint.js +0 -2
- package/lib/plugin-runtime/im/operation-resources.d.ts +0 -15
- package/lib/plugin-runtime/im/operation-resources.js +0 -2
- package/lib/plugin-runtime/im/outbound-ledger.d.ts +0 -24
- package/lib/plugin-runtime/im/outbound-ledger.js +0 -114
- package/lib/plugin-runtime/im/outbound-presentation.d.ts +0 -19
- package/lib/plugin-runtime/im/outbound-presentation.js +0 -41
- package/lib/plugin-runtime/im/outbound-projection.d.ts +0 -17
- package/lib/plugin-runtime/im/outbound-projection.js +0 -55
- package/lib/plugin-runtime/im/runtime-middleware.d.ts +0 -5
- package/lib/plugin-runtime/im/runtime-middleware.js +0 -18
|
@@ -1,20 +1,9 @@
|
|
|
1
|
-
import { operationEndpoint } from './operation-endpoint.js';
|
|
2
|
-
import { isMiddlewareIndex, middlewareFeatureId } from '@zhin.js/middleware';
|
|
3
|
-
import { isHandlerIndex, handlerFeatureId } from '../../feature/handler.js';
|
|
4
|
-
import { bindOperationInput, operationInputBinding } from '@zhin.js/feature-kit';
|
|
5
1
|
import { formatCompact, getLogger, truncatePreview } from '@zhin.js/logger';
|
|
6
|
-
import {
|
|
7
|
-
import { RuntimeRequest } from '../../request.js';
|
|
8
|
-
import { RuntimeSystemEvent } from '../../system-event.js';
|
|
9
|
-
import { RuntimeMessage, } from './contracts.js';
|
|
10
|
-
import { defaultCommandPrefixResolver, MessageDispatcher } from './message-dispatcher.js';
|
|
11
|
-
import { messageLocale } from './locale.js';
|
|
2
|
+
import { RuntimeMessage, operationEndpoint, } from './contracts.js';
|
|
12
3
|
import { requireAdapters } from './endpoint-runtime.js';
|
|
4
|
+
import { arbitrateRoomInboundDeduplication, dispatchCanonicalEndpointEvent, dispatchInboundHandlers, dispatchInboundInteractive, handlers, interactionResult, messageLocale, resolveIngressRoute, resolveRoomMembership, runInboundMiddlewareAndSpine, withEndpointEventPayload, } from './inbound-spine.js';
|
|
5
|
+
import { defaultCommandPrefixResolver, MessageDispatcher } from './message-dispatcher.js';
|
|
13
6
|
import { formatConversationLog, previewMessageContent, } from './message-events.js';
|
|
14
|
-
import { resolveIngressRoute } from './ingress-route.js';
|
|
15
|
-
import { actionPayloadFromSegments } from './interactive-router.js';
|
|
16
|
-
import { readMentionSegmentTarget } from '../../built/segment-contract/mention.js';
|
|
17
|
-
import { roomMembershipToken, } from './inbound-origin.js';
|
|
18
7
|
const logger = getLogger('im.inbound');
|
|
19
8
|
/** Owns Endpoint ingress normalization, routing, dispatch, and side-event action scopes. */
|
|
20
9
|
export class InboundRuntime {
|
|
@@ -45,37 +34,6 @@ export class InboundRuntime {
|
|
|
45
34
|
return this.#receiveSideEvent(event);
|
|
46
35
|
}
|
|
47
36
|
}
|
|
48
|
-
#routeInput({ conversation, senderId, input, adapters, generation }) {
|
|
49
|
-
const mentionTargets = (input.segments ?? [])
|
|
50
|
-
.map((segment) => readMentionSegmentTarget(segment))
|
|
51
|
-
.filter((target) => target !== '' && target !== 'all');
|
|
52
|
-
const replyToId = input.replyTo?.id;
|
|
53
|
-
const replyToEndpointId = replyToId !== undefined && this.context.resolveReplyTarget
|
|
54
|
-
? this.context.resolveReplyTarget(conversation, replyToId)
|
|
55
|
-
: undefined;
|
|
56
|
-
const registry = this.context.registry;
|
|
57
|
-
return {
|
|
58
|
-
conversation,
|
|
59
|
-
senderId,
|
|
60
|
-
content: input.content,
|
|
61
|
-
mentionTargets,
|
|
62
|
-
...(replyToEndpointId !== undefined ? { replyToEndpointId } : {}),
|
|
63
|
-
pending: (endpointId) => {
|
|
64
|
-
// Registry/index skew or a throwing probe must never fail the whole message closed:
|
|
65
|
-
// an Endpoint we cannot probe simply has nothing pending.
|
|
66
|
-
try {
|
|
67
|
-
const adapter = adapters.clientAdapter(endpointId);
|
|
68
|
-
const probe = Object.freeze({ ...conversation, endpoint: Object.freeze({ id: endpointId, adapter }) });
|
|
69
|
-
return this.context.interactions.hasPendingAnswer?.(probe, senderId, generation, { content: input.content, segments: input.segments }) === true || this.context.inboundClaimPending?.(probe, senderId) === true;
|
|
70
|
-
}
|
|
71
|
-
catch {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
accountOf: (endpointId) => registry?.selfOf(endpointId),
|
|
76
|
-
isAvailable: (endpointId) => registry?.isReady(endpointId) === true,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
37
|
async #receiveMessage(source, admission) {
|
|
80
38
|
const receivedAt = Date.now();
|
|
81
39
|
const lease = this.context.acquire();
|
|
@@ -135,117 +93,50 @@ export class InboundRuntime {
|
|
|
135
93
|
}));
|
|
136
94
|
return Object.freeze({ matched: false });
|
|
137
95
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
// would not trigger.
|
|
147
|
-
let routedTarget = false;
|
|
148
|
-
let pendingSettle;
|
|
149
|
-
// Action segments are only ever produced by an adapter from a platform callback structure
|
|
150
|
-
// (e.g. Telegram callback_query); a plain text message cannot carry one. Such a callback is
|
|
151
|
-
// delivered only to the Endpoint that sent the keyboard, and only that Endpoint holds the
|
|
152
|
-
// keyboard's JSX registry / fallback. It is handled where it arrived: never routed elsewhere,
|
|
153
|
-
// never claimed, never turned into a duplicate, never forced to mentioned.
|
|
154
|
-
const platformCallback = actionPayloadFromSegments(input.segments) !== undefined;
|
|
155
|
-
if ((origin === 'human' || origin === undefined) && this.context.deduper && !platformCallback
|
|
156
|
-
&& (conversation.kind === 'group' || conversation.kind === 'channel')) {
|
|
157
|
-
try {
|
|
158
|
-
if (roomMembership?.hasRoom?.(conversation) === true) {
|
|
159
|
-
const senderId = String(enrichedSender?.id ?? '');
|
|
160
|
-
const deduper = this.context.deduper;
|
|
161
|
-
if (typeof roomMembership.route === 'function' && typeof deduper.arbitrate === 'function') {
|
|
162
|
-
const route = roomMembership.route.bind(roomMembership);
|
|
163
|
-
const routeInput = this.#routeInput({ conversation, senderId, input, adapters, generation: lease.value.generation });
|
|
164
|
-
const verdict = deduper.arbitrate(conversation, senderId, input.message?.id, () => {
|
|
165
|
-
try {
|
|
166
|
-
const decision = route(routeInput);
|
|
167
|
-
return decision
|
|
168
|
-
? { target: decision.endpointId, fallbacks: decision.fallbackEndpointIds ?? [] }
|
|
169
|
-
: undefined;
|
|
170
|
-
}
|
|
171
|
-
catch (error) {
|
|
172
|
-
// A broken router must not drop the message: degrade to first-come-first-served.
|
|
173
|
-
logger.warn(formatCompact({
|
|
174
|
-
op: 'room_route_error',
|
|
175
|
-
conv: formatConversationLog(conversation),
|
|
176
|
-
error: error instanceof Error ? error.message : String(error),
|
|
177
|
-
}));
|
|
178
|
-
return undefined;
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
switch (verdict.kind) {
|
|
182
|
-
case 'handle':
|
|
183
|
-
routedTarget = verdict.routed;
|
|
184
|
-
break;
|
|
185
|
-
case 'duplicate':
|
|
186
|
-
recordOnly = 'duplicate';
|
|
187
|
-
logger.debug(formatCompact({
|
|
188
|
-
op: 'room_non_target_copy',
|
|
189
|
-
conv: formatConversationLog(conversation),
|
|
190
|
-
}));
|
|
191
|
-
break;
|
|
192
|
-
case 'wait':
|
|
193
|
-
pendingSettle = verdict;
|
|
194
|
-
break;
|
|
195
|
-
case 'unkeyed':
|
|
196
|
-
// Known limitation: no message id / sender / private chat cannot be arbitrated,
|
|
197
|
-
// so each Endpoint keeps handling its own copy (may duplicate, never drops).
|
|
198
|
-
break;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
const claim = deduper.claim(conversation, senderId, input.message?.id);
|
|
203
|
-
if (claim === 'duplicate')
|
|
204
|
-
recordOnly = 'duplicate';
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
catch (error) {
|
|
96
|
+
if (typeof roomMembership?.isRoomReady === 'function') {
|
|
97
|
+
const readiness = roomMembership.isRoomReady({
|
|
98
|
+
platform: conversation.endpoint.adapter,
|
|
99
|
+
sceneType: conversation.kind,
|
|
100
|
+
sceneId: conversation.id,
|
|
101
|
+
...(conversation.threadId !== undefined ? { threadId: conversation.threadId } : {}),
|
|
102
|
+
});
|
|
103
|
+
if (!readiness.ready) {
|
|
209
104
|
logger.warn(formatCompact({
|
|
210
|
-
op: '
|
|
105
|
+
op: 'venue_unready',
|
|
211
106
|
conv: formatConversationLog(conversation),
|
|
212
|
-
|
|
107
|
+
unreadyEndpoints: readiness.unreadyEndpoints?.join(',') ?? '',
|
|
108
|
+
reason: 'Venue member endpoints have not reported selfUserId yet',
|
|
213
109
|
}));
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
if (pendingSettle) {
|
|
218
|
-
// The Router's target had not delivered its copy yet. Wait (grace period, a couple of
|
|
219
|
-
// seconds at most, lease held) for it; if it never shows up this copy takes over so the
|
|
220
|
-
// message is not lost. Nothing has been recorded yet, so a duplicate stays history-only.
|
|
221
|
-
const outcome = await pendingSettle.settle();
|
|
222
|
-
if (outcome === 'handle') {
|
|
223
|
-
routedTarget = true;
|
|
224
|
-
if (!pendingSettle.listed) {
|
|
225
|
-
logger.warn(formatCompact({
|
|
226
|
-
op: 'room_unlisted_member_takeover',
|
|
227
|
-
conv: formatConversationLog(conversation),
|
|
228
|
-
target: pendingSettle.target,
|
|
229
|
-
takeover: conversation.endpoint.id,
|
|
230
|
-
}));
|
|
231
|
-
}
|
|
232
|
-
logger.debug(formatCompact({
|
|
233
|
-
op: 'room_target_missing_takeover',
|
|
234
|
-
conv: formatConversationLog(conversation),
|
|
235
|
-
target: pendingSettle.target,
|
|
236
|
-
takeover: conversation.endpoint.id,
|
|
237
|
-
}));
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
recordOnly = 'duplicate';
|
|
110
|
+
return Object.freeze({ matched: false });
|
|
241
111
|
}
|
|
242
112
|
}
|
|
113
|
+
const { recordOnly, routedTarget } = await arbitrateRoomInboundDeduplication({
|
|
114
|
+
origin,
|
|
115
|
+
deduper: this.context.deduper,
|
|
116
|
+
roomMembership,
|
|
117
|
+
conversation,
|
|
118
|
+
enrichedSender,
|
|
119
|
+
incoming: input,
|
|
120
|
+
routeRequest: {
|
|
121
|
+
conversation,
|
|
122
|
+
senderId: String(enrichedSender?.id ?? ''),
|
|
123
|
+
input,
|
|
124
|
+
adapters,
|
|
125
|
+
generation: lease.value.generation,
|
|
126
|
+
interactions: this.context.interactions,
|
|
127
|
+
registry: this.context.registry,
|
|
128
|
+
inboundClaimPending: this.context.inboundClaimPending,
|
|
129
|
+
resolveReplyTarget: this.context.resolveReplyTarget,
|
|
130
|
+
},
|
|
131
|
+
});
|
|
243
132
|
const mentioned = routedTarget ? true : input.mentioned;
|
|
244
133
|
const locale = await messageLocale(lease.value, conversation, enrichedSender?.id);
|
|
134
|
+
let operationReplied = false;
|
|
245
135
|
const operationReply = (content, replyRequester = requester, targetConversation) => {
|
|
246
136
|
if (!active)
|
|
247
137
|
throw new Error('Message reply scope has ended');
|
|
248
138
|
boundEndpoint.assertActive();
|
|
139
|
+
operationReplied = true;
|
|
249
140
|
const effectiveConversation = targetConversation
|
|
250
141
|
? { ...targetConversation, endpoint: conversation.endpoint }
|
|
251
142
|
: conversation;
|
|
@@ -349,8 +240,6 @@ export class InboundRuntime {
|
|
|
349
240
|
let reply;
|
|
350
241
|
if (recordOnly === 'duplicate') {
|
|
351
242
|
this.context.diagnostics?.route('duplicate');
|
|
352
|
-
// Duplicate of a message already routed via another Endpoint: history only,
|
|
353
|
-
// no publish (avoids N copies in the console message feed).
|
|
354
243
|
logger.debug(formatCompact({
|
|
355
244
|
op: 'inbound_duplicate',
|
|
356
245
|
conv: formatConversationLog(conversation),
|
|
@@ -360,8 +249,6 @@ export class InboundRuntime {
|
|
|
360
249
|
}
|
|
361
250
|
if (recordOnly === 'dedup_error') {
|
|
362
251
|
this.context.diagnostics?.route('dedup_error');
|
|
363
|
-
// Undecidable: record only and never route (no duplicate routing), but publish so
|
|
364
|
-
// the console feed still shows the message and a persistent failure is visible.
|
|
365
252
|
this.context.publish({
|
|
366
253
|
direction: 'inbound',
|
|
367
254
|
conversation,
|
|
@@ -374,8 +261,6 @@ export class InboundRuntime {
|
|
|
374
261
|
}
|
|
375
262
|
if (origin === 'member' || origin === 'peer_bot') {
|
|
376
263
|
this.context.diagnostics?.route('bot_origin');
|
|
377
|
-
// Bot-origin messages are recorded as chat history only: no claim, handlers,
|
|
378
|
-
// commands, middleware or ingress route (prevents bot-to-bot loops).
|
|
379
264
|
logger.debug(formatCompact({
|
|
380
265
|
op: 'bot_origin_recorded',
|
|
381
266
|
conv: formatConversationLog(conversation),
|
|
@@ -405,76 +290,36 @@ export class InboundRuntime {
|
|
|
405
290
|
}
|
|
406
291
|
else {
|
|
407
292
|
const interactionFactory = (value) => this.context.interactions.createFromUnknown(value);
|
|
408
|
-
const
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
this.context.
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
conv: formatConversationLog(conversation),
|
|
438
|
-
}));
|
|
439
|
-
const handled = await ingressRoute.route(message, lease, requester, conversationSequence);
|
|
440
|
-
if (handled) {
|
|
441
|
-
this.context.diagnostics?.route('agent');
|
|
442
|
-
result = Object.freeze({ matched: true, command: 'ai', owner: requester });
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
const response = dispatched?.reply ?? (interactive?.value !== undefined && interactive.owner
|
|
446
|
-
? { owner: interactive.owner, content: interactive.value } : undefined);
|
|
447
|
-
return {
|
|
448
|
-
dispatch: result,
|
|
449
|
-
...(response ? { reply: response } : {}),
|
|
450
|
-
};
|
|
451
|
-
};
|
|
452
|
-
const middleware = lease.value.projections.get(middlewareFeatureId);
|
|
453
|
-
if (isMiddlewareIndex(middleware))
|
|
454
|
-
this.context.diagnostics?.step('middleware', 'entered');
|
|
455
|
-
const outcome = isMiddlewareIndex(middleware)
|
|
456
|
-
? await middleware.runInbound(message, terminal, {
|
|
457
|
-
stop: (owner) => {
|
|
458
|
-
this.context.diagnostics?.route('middleware');
|
|
459
|
-
this.context.diagnostics?.step('middleware', 'stopped');
|
|
460
|
-
return { dispatch: { matched: true, command: 'middleware', owner } };
|
|
461
|
-
},
|
|
462
|
-
replace: (owner, value, downstream) => {
|
|
463
|
-
this.context.diagnostics?.step('middleware', 'replaced');
|
|
464
|
-
if (!downstream?.dispatch.matched)
|
|
465
|
-
this.context.diagnostics?.route('middleware');
|
|
466
|
-
return {
|
|
467
|
-
dispatch: downstream?.dispatch.matched
|
|
468
|
-
? downstream.dispatch
|
|
469
|
-
: { matched: true, command: 'middleware', owner },
|
|
470
|
-
reply: { owner, content: value },
|
|
471
|
-
};
|
|
472
|
-
},
|
|
473
|
-
}, lease.value)
|
|
474
|
-
: await terminal();
|
|
293
|
+
const outcome = await runInboundMiddlewareAndSpine({
|
|
294
|
+
message,
|
|
295
|
+
lease,
|
|
296
|
+
requester,
|
|
297
|
+
conversationSequence,
|
|
298
|
+
admission,
|
|
299
|
+
signal: operationSignal,
|
|
300
|
+
interactionFactory,
|
|
301
|
+
ingressRoute: resolveIngressRoute(lease.value),
|
|
302
|
+
diagnostics: this.context.diagnostics,
|
|
303
|
+
runHandlers: () => dispatchInboundHandlers({
|
|
304
|
+
lease,
|
|
305
|
+
event: 'message.receive',
|
|
306
|
+
args: [withEndpointEventPayload(source, message)],
|
|
307
|
+
signal: operationSignal,
|
|
308
|
+
interactions: this.context.interactions,
|
|
309
|
+
deliver: (req, snap) => this.context.deliver(req, snap),
|
|
310
|
+
}),
|
|
311
|
+
dispatchInteractive: () => dispatchInboundInteractive({
|
|
312
|
+
message,
|
|
313
|
+
requester,
|
|
314
|
+
admission,
|
|
315
|
+
snapshot: lease.value,
|
|
316
|
+
interactions: this.context.interactions,
|
|
317
|
+
}),
|
|
318
|
+
dispatchCommand: (observe) => this.#dispatcher.dispatchWithReply(message, lease.value, interactionFactory, observe),
|
|
319
|
+
hasReplied: () => operationReplied,
|
|
320
|
+
findEventHandlerName: (event) => handlers(lease.value)?.list().find(descriptor => descriptor.event === event)?.name,
|
|
321
|
+
}, this.context.diagnostics);
|
|
475
322
|
result = outcome.dispatch;
|
|
476
|
-
if (isMiddlewareIndex(middleware))
|
|
477
|
-
this.context.diagnostics?.step('middleware', 'completed');
|
|
478
323
|
reply = outcome.reply;
|
|
479
324
|
}
|
|
480
325
|
this.context.diagnostics?.dispatch(result);
|
|
@@ -528,7 +373,6 @@ export class InboundRuntime {
|
|
|
528
373
|
const assertActive = () => {
|
|
529
374
|
if (!active)
|
|
530
375
|
throw new Error('Endpoint event context expired with its generation operation');
|
|
531
|
-
// A held Root callback may drain across Root retirement; a retired child may not.
|
|
532
376
|
captured.assertActive();
|
|
533
377
|
};
|
|
534
378
|
const context = {
|
|
@@ -543,40 +387,27 @@ export class InboundRuntime {
|
|
|
543
387
|
},
|
|
544
388
|
};
|
|
545
389
|
const dispatch = async (payload) => {
|
|
546
|
-
await
|
|
390
|
+
await dispatchInboundHandlers({
|
|
391
|
+
lease,
|
|
392
|
+
event: source.name,
|
|
393
|
+
args: [withEndpointEventPayload(source, payload)],
|
|
394
|
+
signal: operationSignal,
|
|
395
|
+
interactions: this.context.interactions,
|
|
396
|
+
deliver: (req, snap) => this.context.deliver(req, snap),
|
|
547
397
|
captureInteraction: (owner, conversation) => {
|
|
548
398
|
assertActive();
|
|
549
399
|
return this.context.captureOperation({ target: conversation, client: source.client, requester: owner, signal: captured.signal });
|
|
550
400
|
},
|
|
551
401
|
});
|
|
552
402
|
};
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
break;
|
|
562
|
-
}
|
|
563
|
-
case 'request':
|
|
564
|
-
await withRequestActionScope(source.payload, async (input) => {
|
|
565
|
-
const request = new RuntimeRequest(input, context);
|
|
566
|
-
// Recording is best effort and must not delay the request handlers.
|
|
567
|
-
const recording = Promise.resolve(this.context.recordRequest?.(request)).catch(() => undefined);
|
|
568
|
-
try {
|
|
569
|
-
await dispatch(request);
|
|
570
|
-
}
|
|
571
|
-
finally {
|
|
572
|
-
await recording;
|
|
573
|
-
}
|
|
574
|
-
}, assertActive);
|
|
575
|
-
break;
|
|
576
|
-
case 'system':
|
|
577
|
-
await dispatch(new RuntimeSystemEvent(source.payload, context));
|
|
578
|
-
break;
|
|
579
|
-
}
|
|
403
|
+
await dispatchCanonicalEndpointEvent({
|
|
404
|
+
source,
|
|
405
|
+
context,
|
|
406
|
+
assertActive,
|
|
407
|
+
recordNotice: (notice) => this.context.recordNotice(notice),
|
|
408
|
+
recordRequest: this.context.recordRequest ? (req) => this.context.recordRequest(req) : undefined,
|
|
409
|
+
dispatch,
|
|
410
|
+
});
|
|
580
411
|
}
|
|
581
412
|
finally {
|
|
582
413
|
active = false;
|
|
@@ -594,7 +425,14 @@ export class InboundRuntime {
|
|
|
594
425
|
try {
|
|
595
426
|
endpointLease = requireAdapters(lease.value).acquireInbound(event.endpoint.id, event.client);
|
|
596
427
|
const signal = AbortSignal.any([lease.stopSignal, endpointLease.signal]);
|
|
597
|
-
await
|
|
428
|
+
await dispatchInboundHandlers({
|
|
429
|
+
lease,
|
|
430
|
+
event: event.name,
|
|
431
|
+
args: [event],
|
|
432
|
+
signal,
|
|
433
|
+
interactions: this.context.interactions,
|
|
434
|
+
deliver: (req, snap) => this.context.deliver(req, snap),
|
|
435
|
+
});
|
|
598
436
|
}
|
|
599
437
|
finally {
|
|
600
438
|
try {
|
|
@@ -605,103 +443,4 @@ export class InboundRuntime {
|
|
|
605
443
|
}
|
|
606
444
|
}
|
|
607
445
|
}
|
|
608
|
-
async #runHandlers({ lease, event, args, signal = lease.stopSignal, captureInteraction }) {
|
|
609
|
-
const snapshot = lease.value;
|
|
610
|
-
const index = handlers(snapshot);
|
|
611
|
-
if (!index)
|
|
612
|
-
return;
|
|
613
|
-
const options = {
|
|
614
|
-
snapshot, signal,
|
|
615
|
-
resolveInteraction: (name, interactionArgs, owner) => {
|
|
616
|
-
const eventContext = interactionArgs[0];
|
|
617
|
-
const payload = eventContext?.payload;
|
|
618
|
-
if (name === 'message.receive' && payload instanceof RuntimeMessage) {
|
|
619
|
-
return this.context.interactions.createFromUnknown(bindOperationInput(payload, owner, snapshot));
|
|
620
|
-
}
|
|
621
|
-
if (name === 'notice.receive'
|
|
622
|
-
|| name === 'request.receive') {
|
|
623
|
-
return this.#createInteractionForSideEvent({ payload: payload, snapshot, requester: owner, signal,
|
|
624
|
-
...(captureInteraction ? { retain: () => captureInteraction(owner, payload.conversation) } : {}),
|
|
625
|
-
});
|
|
626
|
-
}
|
|
627
|
-
return undefined;
|
|
628
|
-
},
|
|
629
|
-
};
|
|
630
|
-
await index.dispatch(event, args, options);
|
|
631
|
-
}
|
|
632
|
-
#createInteractionForSideEvent({ payload, snapshot, requester, signal, retain }) {
|
|
633
|
-
const conversation = payload.conversation;
|
|
634
|
-
if (!conversation)
|
|
635
|
-
return undefined;
|
|
636
|
-
const source = Object.freeze({
|
|
637
|
-
conversation,
|
|
638
|
-
...(payload.actor?.id
|
|
639
|
-
? { sender: Object.freeze({ id: payload.actor.id }) }
|
|
640
|
-
: {}),
|
|
641
|
-
$reply: (content) => this.context.deliver({
|
|
642
|
-
conversation,
|
|
643
|
-
requester,
|
|
644
|
-
content,
|
|
645
|
-
}, snapshot),
|
|
646
|
-
});
|
|
647
|
-
return this.context.interactions.create(source, undefined, { snapshot, owner: requester, signal, retain });
|
|
648
|
-
}
|
|
649
|
-
async #dispatchInteractive({ message, requester, admission, snapshot }) {
|
|
650
|
-
const handled = await this.context.interactions.dispatch(message, admission, snapshot);
|
|
651
|
-
return handled
|
|
652
|
-
? Object.freeze({ matched: true, command: 'interactive', owner: requester })
|
|
653
|
-
: undefined;
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
function resolveRoomMembership(snapshot) {
|
|
657
|
-
try {
|
|
658
|
-
const candidate = snapshot.resources.get(snapshot.root)?.get(roomMembershipToken.id);
|
|
659
|
-
return candidate
|
|
660
|
-
&& typeof candidate === 'object'
|
|
661
|
-
&& typeof candidate.isMember === 'function'
|
|
662
|
-
? candidate
|
|
663
|
-
: undefined;
|
|
664
|
-
}
|
|
665
|
-
catch {
|
|
666
|
-
return undefined;
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
function interactionResult(requester) {
|
|
670
|
-
return Object.freeze({ matched: true, command: 'interaction', owner: requester });
|
|
671
|
-
}
|
|
672
|
-
async function withRequestActionScope(request, dispatch, assertOperation) {
|
|
673
|
-
let active = true;
|
|
674
|
-
const actions = new Set();
|
|
675
|
-
const run = (action) => {
|
|
676
|
-
if (!active)
|
|
677
|
-
throw new Error('Request action port expired with its generation operation');
|
|
678
|
-
const operation = Promise.resolve().then(() => { assertOperation(); return action(); });
|
|
679
|
-
actions.add(operation);
|
|
680
|
-
void operation.then(() => actions.delete(operation), () => actions.delete(operation));
|
|
681
|
-
return operation;
|
|
682
|
-
};
|
|
683
|
-
const scoped = Object.freeze({ ...request,
|
|
684
|
-
$approve: async (remark) => run(() => request.$approve(remark)),
|
|
685
|
-
$reject: async (reason) => run(() => request.$reject(reason)),
|
|
686
|
-
});
|
|
687
|
-
try {
|
|
688
|
-
await dispatch(scoped);
|
|
689
|
-
}
|
|
690
|
-
finally {
|
|
691
|
-
active = false;
|
|
692
|
-
await Promise.allSettled([...actions]);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
function handlers(snapshot) {
|
|
696
|
-
const projection = snapshot.projections.get(handlerFeatureId);
|
|
697
|
-
return isHandlerIndex(projection) ? projection : undefined;
|
|
698
|
-
}
|
|
699
|
-
function withEndpointEventPayload(source, payload) {
|
|
700
|
-
return Object.freeze({
|
|
701
|
-
name: source.name,
|
|
702
|
-
payload,
|
|
703
|
-
endpoint: source.endpoint,
|
|
704
|
-
client: source.client,
|
|
705
|
-
[operationInputBinding]: (owner, snapshot) => withEndpointEventPayload(source, bindOperationInput(payload, owner, snapshot)),
|
|
706
|
-
});
|
|
707
446
|
}
|