@zhin.js/core 1.1.35 → 1.1.38
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/README.md +79 -193
- package/lib/built/ai-outbound/index.d.ts +0 -1
- package/lib/built/ai-outbound/index.js +0 -1
- package/lib/built/ai-outbound/types.d.ts +0 -9
- package/lib/built/ai-trigger.d.ts +0 -5
- package/lib/built/ai-trigger.js +1 -27
- package/lib/built/common-adapter-tools.d.ts +0 -2
- package/lib/built/common-adapter-tools.js +0 -4
- package/lib/built/html-segment-fallback.d.ts +2 -5
- package/lib/built/html-segment-fallback.js +0 -15
- package/lib/built/interactive-segments/index.d.ts +0 -2
- package/lib/built/interactive-segments/index.js +0 -2
- package/lib/built/interactive-segments/keyboard-segment.d.ts +0 -1
- package/lib/built/interactive-segments/keyboard-segment.js +0 -1
- package/lib/built/interactive-segments/onebot-keyboard.d.ts +0 -1
- package/lib/built/interactive-segments/onebot-keyboard.js +0 -2
- package/lib/built/interactive-segments/resolve.d.ts +0 -1
- package/lib/built/interactive-segments/resolve.js +0 -1
- package/lib/built/interactive-segments/types.d.ts +0 -5
- package/lib/built/login-assist.d.ts +1 -8
- package/lib/built/message-enrich.d.ts +2 -8
- package/lib/built/message-enrich.js +3 -11
- package/lib/built/segment-contract/assert.js +2 -1
- package/lib/built/segment-contract/delivery.js +1 -0
- package/lib/built/segment-contract/index.d.ts +2 -2
- package/lib/built/segment-contract/index.js +1 -1
- package/lib/built/segment-contract/json-schema.d.ts +1 -1
- package/lib/built/segment-contract/json-schema.js +33 -2
- package/lib/built/segment-contract/preview.js +7 -0
- package/lib/built/segment-contract/types.d.ts +2 -2
- package/lib/built/segment-contract/validate.d.ts +43 -2
- package/lib/built/segment-contract/validate.js +37 -3
- package/lib/built/tool-access.d.ts +5 -0
- package/lib/built/tool-access.js +17 -0
- package/lib/built/user-interaction.js +0 -4
- package/lib/feature/handler.d.ts +1 -5
- package/lib/index.d.ts +4 -45
- package/lib/index.js +3 -39
- package/lib/message.d.ts +1 -2
- package/lib/models/system-log.d.ts +0 -2
- package/lib/models/system-log.js +1 -8
- package/lib/models/user.d.ts +0 -2
- package/lib/models/user.js +1 -7
- package/lib/notice.d.ts +1 -2
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +19 -0
- package/lib/plugin-runtime/im/conversation-runtime.js +312 -0
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +79 -0
- package/lib/plugin-runtime/im/endpoint-runtime.js +162 -0
- package/lib/plugin-runtime/im/im-runtime.d.ts +13 -106
- package/lib/plugin-runtime/im/im-runtime.js +58 -1061
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +31 -0
- package/lib/plugin-runtime/im/inbound-runtime.js +252 -0
- package/lib/plugin-runtime/im/index.d.ts +4 -2
- package/lib/plugin-runtime/im/index.js +2 -2
- package/lib/plugin-runtime/im/ingress-route.d.ts +10 -0
- package/lib/plugin-runtime/im/ingress-route.js +10 -0
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +23 -0
- package/lib/plugin-runtime/im/interaction-runtime.js +181 -0
- package/lib/plugin-runtime/im/interactive-router.d.ts +17 -0
- package/lib/plugin-runtime/im/interactive-router.js +98 -0
- package/lib/plugin-runtime/im/message-bus.d.ts +3 -3
- package/lib/plugin-runtime/im/message-bus.js +3 -3
- package/lib/plugin-runtime/im/message-events.d.ts +26 -0
- package/lib/plugin-runtime/im/message-events.js +61 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +19 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +165 -0
- package/lib/plugin-runtime/im/outbound-segments.d.ts +2 -3
- package/lib/plugin-runtime/im/outbound-segments.js +3 -4
- package/lib/plugin-runtime/im/runtime-middleware.d.ts +3 -0
- package/lib/plugin-runtime/im/runtime-middleware.js +13 -0
- package/lib/request.d.ts +1 -2
- package/lib/side-event/base.d.ts +1 -2
- package/lib/side-event/normalize.d.ts +1 -5
- package/lib/side-event/normalize.js +1 -11
- package/lib/system-event.d.ts +1 -2
- package/lib/types.d.ts +9 -116
- package/lib/utils.d.ts +51 -24
- package/lib/utils.js +11 -23
- package/package.json +18 -28
- package/lib/adapter.d.ts +0 -145
- package/lib/adapter.js +0 -377
- package/lib/built/adapter-process.d.ts +0 -58
- package/lib/built/adapter-process.js +0 -128
- package/lib/built/agent-preset.d.ts +0 -64
- package/lib/built/agent-preset.js +0 -79
- package/lib/built/ai-outbound/adapter-access.d.ts +0 -3
- package/lib/built/ai-outbound/adapter-access.js +0 -9
- package/lib/built/authorization.d.ts +0 -10
- package/lib/built/authorization.js +0 -76
- package/lib/built/command.d.ts +0 -72
- package/lib/built/command.js +0 -105
- package/lib/built/component.d.ts +0 -67
- package/lib/built/component.js +0 -97
- package/lib/built/config.d.ts +0 -98
- package/lib/built/config.js +0 -372
- package/lib/built/connect-endpoint-instance.d.ts +0 -17
- package/lib/built/connect-endpoint-instance.js +0 -42
- package/lib/built/database.d.ts +0 -68
- package/lib/built/database.js +0 -106
- package/lib/built/dispatcher.d.ts +0 -141
- package/lib/built/dispatcher.js +0 -383
- package/lib/built/endpoint-commands.d.ts +0 -3
- package/lib/built/endpoint-commands.js +0 -99
- package/lib/built/endpoint-lifecycle-service.d.ts +0 -28
- package/lib/built/endpoint-lifecycle-service.js +0 -242
- package/lib/built/endpoint-lifecycle.d.ts +0 -27
- package/lib/built/endpoint-lifecycle.js +0 -36
- package/lib/built/endpoint-manager.d.ts +0 -34
- package/lib/built/endpoint-manager.js +0 -1
- package/lib/built/html-renderer-loader.d.ts +0 -13
- package/lib/built/html-renderer-loader.js +0 -33
- package/lib/built/inbound-pipeline.d.ts +0 -37
- package/lib/built/inbound-pipeline.js +0 -63
- package/lib/built/inbound-runner.d.ts +0 -22
- package/lib/built/inbound-runner.js +0 -34
- package/lib/built/interactive-segment-contract.d.ts +0 -6
- package/lib/built/interactive-segment-contract.js +0 -9
- package/lib/built/interactive-segments/fallback-store.d.ts +0 -29
- package/lib/built/interactive-segments/fallback-store.js +0 -63
- package/lib/built/interactive-segments/handlers.d.ts +0 -16
- package/lib/built/interactive-segments/handlers.js +0 -74
- package/lib/built/introspection-format.d.ts +0 -10
- package/lib/built/introspection-format.js +0 -28
- package/lib/built/management-command-guard.d.ts +0 -15
- package/lib/built/management-command-guard.js +0 -25
- package/lib/built/message-filter.d.ts +0 -176
- package/lib/built/message-filter.js +0 -239
- package/lib/built/optional-peer-import.d.ts +0 -3
- package/lib/built/optional-peer-import.js +0 -33
- package/lib/built/outbound-media-contract.d.ts +0 -22
- package/lib/built/outbound-media-contract.js +0 -10
- package/lib/built/permission.d.ts +0 -51
- package/lib/built/permission.js +0 -99
- package/lib/built/permit-check.d.ts +0 -7
- package/lib/built/permit-check.js +0 -35
- package/lib/built/permit-parse.d.ts +0 -16
- package/lib/built/permit-parse.js +0 -32
- package/lib/built/platform-permit.d.ts +0 -20
- package/lib/built/platform-permit.js +0 -91
- package/lib/built/rich-segments/adapter-policies.d.ts +0 -9
- package/lib/built/rich-segments/adapter-policies.js +0 -27
- package/lib/built/rich-segments/base.d.ts +0 -11
- package/lib/built/rich-segments/base.js +0 -24
- package/lib/built/rich-segments/builtins.d.ts +0 -4
- package/lib/built/rich-segments/builtins.js +0 -40
- package/lib/built/rich-segments/capabilities.d.ts +0 -6
- package/lib/built/rich-segments/capabilities.js +0 -38
- package/lib/built/rich-segments/html-segment.d.ts +0 -15
- package/lib/built/rich-segments/html-segment.js +0 -62
- package/lib/built/rich-segments/index.d.ts +0 -13
- package/lib/built/rich-segments/index.js +0 -13
- package/lib/built/rich-segments/markdown-segment.d.ts +0 -14
- package/lib/built/rich-segments/markdown-segment.js +0 -58
- package/lib/built/rich-segments/qrcode-segment.d.ts +0 -18
- package/lib/built/rich-segments/qrcode-segment.js +0 -73
- package/lib/built/rich-segments/registry.d.ts +0 -16
- package/lib/built/rich-segments/registry.js +0 -52
- package/lib/built/rich-segments/resolve.d.ts +0 -5
- package/lib/built/rich-segments/resolve.js +0 -87
- package/lib/built/rich-segments/tts-segment.d.ts +0 -13
- package/lib/built/rich-segments/tts-segment.js +0 -51
- package/lib/built/rich-segments/types.d.ts +0 -79
- package/lib/built/rich-segments/types.js +0 -17
- package/lib/built/runtime-io.d.ts +0 -9
- package/lib/built/runtime-io.js +0 -67
- package/lib/built/schedule.d.ts +0 -54
- package/lib/built/schedule.js +0 -112
- package/lib/built/schema-endpoint-manager.d.ts +0 -17
- package/lib/built/schema-endpoint-manager.js +0 -73
- package/lib/built/schema-feature.d.ts +0 -77
- package/lib/built/schema-feature.js +0 -130
- package/lib/built/skill.d.ts +0 -120
- package/lib/built/skill.js +0 -151
- package/lib/built/speech-loader.d.ts +0 -14
- package/lib/built/speech-loader.js +0 -32
- package/lib/built/tool.d.ts +0 -151
- package/lib/built/tool.js +0 -388
- package/lib/command.d.ts +0 -50
- package/lib/command.js +0 -106
- package/lib/endpoint-capabilities.d.ts +0 -46
- package/lib/endpoint-capabilities.js +0 -55
- package/lib/endpoint.d.ts +0 -19
- package/lib/endpoint.js +0 -3
- package/lib/host-plugin-registry.d.ts +0 -8
- package/lib/host-plugin-registry.js +0 -12
- package/lib/plugin-context.d.ts +0 -27
- package/lib/plugin-context.js +0 -64
- package/lib/plugin-runtime/im/interactive.d.ts +0 -26
- package/lib/plugin-runtime/im/interactive.js +0 -46
- package/lib/plugin-runtime/im/service-tokens.d.ts +0 -18
- package/lib/plugin-runtime/im/service-tokens.js +0 -13
- package/lib/plugin.d.ts +0 -268
- package/lib/plugin.js +0 -583
- package/lib/schema-interaction.d.ts +0 -117
- package/lib/schema-interaction.js +0 -247
- package/lib/tool-zod.d.ts +0 -43
- package/lib/tool-zod.js +0 -184
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { GenerationAdmissionGate, RuntimeSnapshot, SnapshotLease } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import type { EndpointEvent } from '@zhin.js/adapter';
|
|
3
|
+
import type { DeliveryReceipt } from '@zhin.js/im-contract';
|
|
4
|
+
import type { UserInteraction } from '@zhin.js/interaction';
|
|
5
|
+
import type { Notice } from '../../notice.js';
|
|
6
|
+
import { Message, type IncomingMessage, type MessageSenderRef, type SendRequest } from './contracts.js';
|
|
7
|
+
import type { RuntimeInteractionCoordinator } from './interaction-runtime.js';
|
|
8
|
+
import { type RuntimeMessageEvent } from './message-events.js';
|
|
9
|
+
type InboundInteractionPort = Pick<RuntimeInteractionCoordinator<GenerationAdmissionGate>, 'create' | 'createForMessage' | 'createFromUnknown' | 'dispatch' | 'resolveClaim'>;
|
|
10
|
+
interface InboundRuntimeContext {
|
|
11
|
+
readonly interactions: InboundInteractionPort;
|
|
12
|
+
readonly inboundClaim?: (message: Message) => boolean | Promise<boolean>;
|
|
13
|
+
readonly enrichSender?: (sender: MessageSenderRef | undefined, conversation: IncomingMessage['conversation'], snapshot: RuntimeSnapshot) => MessageSenderRef | undefined;
|
|
14
|
+
acquire(): SnapshotLease;
|
|
15
|
+
release(lease: SnapshotLease): void;
|
|
16
|
+
deliver(request: SendRequest, snapshot: RuntimeSnapshot): Promise<DeliveryReceipt>;
|
|
17
|
+
recordIncoming(input: IncomingMessage, sender: MessageSenderRef | undefined): Promise<number | undefined>;
|
|
18
|
+
recordNotice(notice: Notice): Promise<void>;
|
|
19
|
+
publish(event: RuntimeMessageEvent): void;
|
|
20
|
+
}
|
|
21
|
+
/** Owns Endpoint ingress normalization, routing, dispatch, and side-event action scopes. */
|
|
22
|
+
export declare class InboundRuntime {
|
|
23
|
+
#private;
|
|
24
|
+
private readonly context;
|
|
25
|
+
constructor(context: InboundRuntimeContext, commandPrefix?: string);
|
|
26
|
+
createInteraction(message: Message, bind?: {
|
|
27
|
+
readonly subjectId: string;
|
|
28
|
+
}): UserInteraction | undefined;
|
|
29
|
+
receive(event: EndpointEvent, admission?: GenerationAdmissionGate): Promise<unknown>;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { isHandlerIndex, handlerFeatureId } from '../../feature/handler.js';
|
|
2
|
+
import { formatCompact, getLogger, truncatePreview } from '@zhin.js/logger';
|
|
3
|
+
import { sideEventSendChannel } from '../../side-event/base.js';
|
|
4
|
+
import { Message, } from './contracts.js';
|
|
5
|
+
import { defaultCommandPrefixResolver, MessageDispatcher } from './message-dispatcher.js';
|
|
6
|
+
import { requireAdapters } from './endpoint-runtime.js';
|
|
7
|
+
import { formatConversationLog, previewMessageContent, } from './message-events.js';
|
|
8
|
+
import { resolveIngressRoute } from './ingress-route.js';
|
|
9
|
+
import { runRuntimeMiddleware } from './runtime-middleware.js';
|
|
10
|
+
const logger = getLogger('im.inbound');
|
|
11
|
+
/** Owns Endpoint ingress normalization, routing, dispatch, and side-event action scopes. */
|
|
12
|
+
export class InboundRuntime {
|
|
13
|
+
context;
|
|
14
|
+
#dispatcher;
|
|
15
|
+
constructor(context, commandPrefix) {
|
|
16
|
+
this.context = context;
|
|
17
|
+
this.#dispatcher = new MessageDispatcher(commandPrefix === undefined
|
|
18
|
+
? defaultCommandPrefixResolver
|
|
19
|
+
: () => commandPrefix);
|
|
20
|
+
}
|
|
21
|
+
createInteraction(message, bind) {
|
|
22
|
+
return this.context.interactions.createForMessage(message, bind);
|
|
23
|
+
}
|
|
24
|
+
async receive(event, admission) {
|
|
25
|
+
switch (event.name) {
|
|
26
|
+
case 'message.receive':
|
|
27
|
+
return this.#receiveMessage(event, admission);
|
|
28
|
+
case 'notice.receive':
|
|
29
|
+
return this.#receiveNotice(event);
|
|
30
|
+
case 'request.receive':
|
|
31
|
+
return this.#receiveRequest(event);
|
|
32
|
+
case 'system.receive':
|
|
33
|
+
return this.#receiveSideEvent(event);
|
|
34
|
+
default:
|
|
35
|
+
return this.#receiveSideEvent(event);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async #receiveMessage(source, admission) {
|
|
39
|
+
const input = source.payload;
|
|
40
|
+
const lease = this.context.acquire();
|
|
41
|
+
let active = true;
|
|
42
|
+
try {
|
|
43
|
+
const conversation = input.conversation;
|
|
44
|
+
const adapter = conversation.endpoint.id;
|
|
45
|
+
const requester = requireAdapters(lease.value).owner(adapter);
|
|
46
|
+
logger.debug(formatCompact({
|
|
47
|
+
op: 'receive',
|
|
48
|
+
conv: formatConversationLog(conversation),
|
|
49
|
+
sender: `${input.sender?.name || 'undefined'}(${input.sender?.id || 'undefined'})`,
|
|
50
|
+
preview: truncatePreview(input.content),
|
|
51
|
+
}));
|
|
52
|
+
const enrichedSender = this.context.enrichSender
|
|
53
|
+
? this.context.enrichSender(input.sender, conversation, lease.value)
|
|
54
|
+
: input.sender;
|
|
55
|
+
const message = new Message(conversation, input.content, lease.value.generation, (content, replyRequester = requester, targetConversation) => {
|
|
56
|
+
if (!active)
|
|
57
|
+
throw new Error('Message reply scope has ended');
|
|
58
|
+
const effectiveConversation = targetConversation
|
|
59
|
+
? { endpoint: conversation.endpoint, ...targetConversation }
|
|
60
|
+
: conversation;
|
|
61
|
+
return this.context.deliver({
|
|
62
|
+
conversation: effectiveConversation,
|
|
63
|
+
requester: replyRequester,
|
|
64
|
+
content,
|
|
65
|
+
incoming: {
|
|
66
|
+
sender: enrichedSender,
|
|
67
|
+
content: input.content,
|
|
68
|
+
segments: input.segments,
|
|
69
|
+
messageId: input.message?.id,
|
|
70
|
+
timestamp: Date.now(),
|
|
71
|
+
endpointId: input.endpointId,
|
|
72
|
+
mentioned: input.mentioned,
|
|
73
|
+
},
|
|
74
|
+
}, lease.value);
|
|
75
|
+
}, enrichedSender, Object.freeze({ ...input.metadata }), input.segments ? Object.freeze([...input.segments]) : undefined, input.message, input.endpointId, input.mentioned, input.replyTo, () => {
|
|
76
|
+
if (!active)
|
|
77
|
+
throw new Error('Message Client scope has ended');
|
|
78
|
+
return source.client;
|
|
79
|
+
}, source.endpoint.adapter);
|
|
80
|
+
const conversationSequence = await this.context.recordIncoming(input, enrichedSender);
|
|
81
|
+
let result = Object.freeze({ matched: false });
|
|
82
|
+
const claimed = await this.context.inboundClaim?.(message) === true;
|
|
83
|
+
if (claimed) {
|
|
84
|
+
result = interactionResult(requester);
|
|
85
|
+
}
|
|
86
|
+
else if (this.context.interactions.resolveClaim(message)) {
|
|
87
|
+
result = interactionResult(requester);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const interactionFactory = (value) => this.context.interactions.createFromUnknown(value);
|
|
91
|
+
await runRuntimeMiddleware(lease.value, message, async () => {
|
|
92
|
+
const ingressRoute = resolveIngressRoute(lease.value);
|
|
93
|
+
const preRouted = await ingressRoute?.preRoute?.(message, lease, requester, conversationSequence) === true;
|
|
94
|
+
if (preRouted) {
|
|
95
|
+
result = Object.freeze({ matched: true, command: 'pre-route', owner: requester });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (ingressRoute?.shouldRouteBeforeDispatch?.(message) === true) {
|
|
99
|
+
const handled = await ingressRoute.route(message, lease, requester, conversationSequence);
|
|
100
|
+
if (handled) {
|
|
101
|
+
result = Object.freeze({ matched: true, command: 'ai', owner: requester });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
await this.#runHandlers(lease.value, 'message.receive', [
|
|
106
|
+
withEndpointEventPayload(source, message),
|
|
107
|
+
]);
|
|
108
|
+
result = await this.#dispatchInteractive(message, requester, admission)
|
|
109
|
+
?? await this.#dispatcher.dispatch(message, lease.value, interactionFactory);
|
|
110
|
+
if (!result.matched && ingressRoute) {
|
|
111
|
+
logger.debug(formatCompact({
|
|
112
|
+
op: 'unmatched',
|
|
113
|
+
conv: formatConversationLog(conversation),
|
|
114
|
+
}));
|
|
115
|
+
const handled = await ingressRoute.route(message, lease, requester, conversationSequence);
|
|
116
|
+
if (handled) {
|
|
117
|
+
result = Object.freeze({ matched: true, command: 'ai', owner: requester });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}, 'inbound');
|
|
121
|
+
}
|
|
122
|
+
if (result.matched) {
|
|
123
|
+
logger.debug(formatCompact({
|
|
124
|
+
op: 'dispatched',
|
|
125
|
+
conv: formatConversationLog(conversation),
|
|
126
|
+
command: result.command,
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
this.context.publish({
|
|
130
|
+
direction: 'inbound',
|
|
131
|
+
conversation,
|
|
132
|
+
...(input.sender !== undefined ? { sender: input.sender } : {}),
|
|
133
|
+
contentPreview: previewMessageContent(input.content),
|
|
134
|
+
...(input.message?.id ? { messageId: input.message.id } : {}),
|
|
135
|
+
timestamp: Date.now(),
|
|
136
|
+
});
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
active = false;
|
|
141
|
+
this.context.release(lease);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async #receiveNotice(source) {
|
|
145
|
+
await this.context.recordNotice(source.payload);
|
|
146
|
+
await this.#receiveSideEvent(withEndpointEventPayload(source, source.payload));
|
|
147
|
+
}
|
|
148
|
+
async #receiveRequest(source) {
|
|
149
|
+
await withRequestActionScope(source.payload, async (scoped) => {
|
|
150
|
+
await this.#receiveSideEvent(withEndpointEventPayload(source, scoped));
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
async #receiveSideEvent(event) {
|
|
154
|
+
const lease = this.context.acquire();
|
|
155
|
+
try {
|
|
156
|
+
await this.#runHandlers(lease.value, event.name, [event]);
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
this.context.release(lease);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async #runHandlers(snapshot, event, args) {
|
|
163
|
+
const index = handlers(snapshot);
|
|
164
|
+
if (!index)
|
|
165
|
+
return;
|
|
166
|
+
const options = {
|
|
167
|
+
resolveInteraction: (name, interactionArgs) => {
|
|
168
|
+
const eventContext = interactionArgs[0];
|
|
169
|
+
const payload = eventContext?.payload;
|
|
170
|
+
if (name === 'message.receive' && payload instanceof Message) {
|
|
171
|
+
return this.createInteraction(payload);
|
|
172
|
+
}
|
|
173
|
+
if (name === 'notice.receive'
|
|
174
|
+
|| name === 'request.receive'
|
|
175
|
+
|| name === 'system.receive') {
|
|
176
|
+
return this.#createInteractionForSideEvent(payload, snapshot);
|
|
177
|
+
}
|
|
178
|
+
return undefined;
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
await index.dispatch(event, args, options);
|
|
182
|
+
}
|
|
183
|
+
#createInteractionForSideEvent(payload, snapshot) {
|
|
184
|
+
const adapter = String(payload.$adapter);
|
|
185
|
+
const endpointKey = String(payload.$endpoint);
|
|
186
|
+
if (!adapter || !endpointKey)
|
|
187
|
+
return undefined;
|
|
188
|
+
const channel = sideEventSendChannel(payload);
|
|
189
|
+
const conversation = Object.freeze({
|
|
190
|
+
endpoint: Object.freeze({ adapter, id: endpointKey }),
|
|
191
|
+
kind: channel.type,
|
|
192
|
+
id: channel.id || endpointKey,
|
|
193
|
+
});
|
|
194
|
+
const requester = snapshot.root;
|
|
195
|
+
const source = Object.freeze({
|
|
196
|
+
conversation,
|
|
197
|
+
...(payload.$actor?.id
|
|
198
|
+
? { sender: Object.freeze({ id: payload.$actor.id }) }
|
|
199
|
+
: {}),
|
|
200
|
+
$reply: (content) => this.context.deliver({
|
|
201
|
+
conversation,
|
|
202
|
+
requester,
|
|
203
|
+
content,
|
|
204
|
+
}, snapshot),
|
|
205
|
+
});
|
|
206
|
+
return this.context.interactions.create(source);
|
|
207
|
+
}
|
|
208
|
+
async #dispatchInteractive(message, requester, admission) {
|
|
209
|
+
const handled = await this.context.interactions.dispatch(message, admission);
|
|
210
|
+
return handled
|
|
211
|
+
? Object.freeze({ matched: true, command: 'interactive', owner: requester })
|
|
212
|
+
: undefined;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function interactionResult(requester) {
|
|
216
|
+
return Object.freeze({ matched: true, command: 'interaction', owner: requester });
|
|
217
|
+
}
|
|
218
|
+
async function withRequestActionScope(request, dispatch) {
|
|
219
|
+
let active = true;
|
|
220
|
+
const actions = new Set();
|
|
221
|
+
const run = (action) => {
|
|
222
|
+
if (!active)
|
|
223
|
+
throw new Error('Request action port expired with its generation operation');
|
|
224
|
+
const operation = Promise.resolve().then(action);
|
|
225
|
+
actions.add(operation);
|
|
226
|
+
void operation.then(() => actions.delete(operation), () => actions.delete(operation));
|
|
227
|
+
return operation;
|
|
228
|
+
};
|
|
229
|
+
const scoped = Object.assign(Object.create(Object.getPrototypeOf(request)), request, {
|
|
230
|
+
$approve: async (remark) => run(() => request.$approve(remark)),
|
|
231
|
+
$reject: async (reason) => run(() => request.$reject(reason)),
|
|
232
|
+
});
|
|
233
|
+
try {
|
|
234
|
+
await dispatch(scoped);
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
active = false;
|
|
238
|
+
await Promise.allSettled([...actions]);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function handlers(snapshot) {
|
|
242
|
+
const projection = snapshot.projections.get(handlerFeatureId);
|
|
243
|
+
return isHandlerIndex(projection) ? projection : undefined;
|
|
244
|
+
}
|
|
245
|
+
function withEndpointEventPayload(source, payload) {
|
|
246
|
+
return Object.freeze({
|
|
247
|
+
name: source.name,
|
|
248
|
+
payload,
|
|
249
|
+
endpoint: source.endpoint,
|
|
250
|
+
client: source.client,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export * from './contracts.js';
|
|
2
|
+
export { EndpointRuntime } from './endpoint-runtime.js';
|
|
3
|
+
export type { EndpointRuntimeDetail, EndpointRuntimeSummary, RuntimeEndpointEditInput, RuntimeEndpointMessageInput, RuntimeEndpointReactionInput, RuntimeEndpointRemoveReactionInput, RuntimeEndpointSendInput, RuntimeEndpointTypingInput, } from './endpoint-runtime.js';
|
|
4
|
+
export * from './ingress-route.js';
|
|
2
5
|
export * from './im-runtime.js';
|
|
3
|
-
export * from './interactive.js';
|
|
4
6
|
export * from './login-assist-host.js';
|
|
5
7
|
export * from './message-bus.js';
|
|
6
8
|
export * from './message-dispatcher.js';
|
|
9
|
+
export type { RuntimeMessageEvent, RuntimeMessageEventSource, } from './message-events.js';
|
|
7
10
|
export * from './outbound-renderer.js';
|
|
8
11
|
export * from './outbound-segments.js';
|
|
9
|
-
export * from './service-tokens.js';
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export * from './contracts.js';
|
|
2
|
+
export { EndpointRuntime } from './endpoint-runtime.js';
|
|
3
|
+
export * from './ingress-route.js';
|
|
2
4
|
export * from './im-runtime.js';
|
|
3
|
-
export * from './interactive.js';
|
|
4
5
|
export * from './login-assist-host.js';
|
|
5
6
|
export * from './message-bus.js';
|
|
6
7
|
export * from './message-dispatcher.js';
|
|
7
8
|
export * from './outbound-renderer.js';
|
|
8
9
|
export * from './outbound-segments.js';
|
|
9
|
-
export * from './service-tokens.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type PluginId, type RuntimeSnapshot, type SnapshotLease } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import type { Message } from './contracts.js';
|
|
3
|
+
/** Generation-owned hooks before ordinary dispatch and after it misses. */
|
|
4
|
+
export interface IngressRoute {
|
|
5
|
+
preRoute?(message: Message, lease: SnapshotLease, requester: PluginId, conversationSequence: number | undefined): Promise<boolean>;
|
|
6
|
+
shouldRouteBeforeDispatch?(message: Message): boolean;
|
|
7
|
+
route(message: Message, lease: SnapshotLease, requester: PluginId, conversationSequence: number | undefined): Promise<boolean>;
|
|
8
|
+
}
|
|
9
|
+
export declare const ingressRouteToken: import("@zhin.js/plugin-runtime").Token<IngressRoute>;
|
|
10
|
+
export declare function resolveIngressRoute(snapshot: RuntimeSnapshot): IngressRoute | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createToken, } from '@zhin.js/plugin-runtime';
|
|
2
|
+
export const ingressRouteToken = createToken('zhin.im.ingress-route');
|
|
3
|
+
export function resolveIngressRoute(snapshot) {
|
|
4
|
+
const candidate = snapshot.resources.get(snapshot.root)?.get(ingressRouteToken.id);
|
|
5
|
+
return candidate
|
|
6
|
+
&& typeof candidate === 'object'
|
|
7
|
+
&& typeof candidate.route === 'function'
|
|
8
|
+
? candidate
|
|
9
|
+
: undefined;
|
|
10
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ConversationRef, type DeliveryReceipt } from '@zhin.js/im-contract';
|
|
2
|
+
import type { UserInteraction } from '@zhin.js/interaction';
|
|
3
|
+
import type { Message, MessageSenderRef, SendContent } from './contracts.js';
|
|
4
|
+
import { RuntimeInteractiveRouter } from './interactive-router.js';
|
|
5
|
+
export interface UserInteractionSource {
|
|
6
|
+
readonly conversation: ConversationRef;
|
|
7
|
+
readonly sender?: MessageSenderRef;
|
|
8
|
+
readonly $reply: (content: SendContent) => Promise<DeliveryReceipt>;
|
|
9
|
+
}
|
|
10
|
+
/** Owns one ImRuntime's conversational claims, actions, and keyboard fallback state. */
|
|
11
|
+
export declare class RuntimeInteractionCoordinator<Admission = never> {
|
|
12
|
+
#private;
|
|
13
|
+
constructor(router?: RuntimeInteractiveRouter<Admission>);
|
|
14
|
+
register(prefix: string, handler: (message: Message) => Promise<boolean> | boolean, admission?: Admission): () => void;
|
|
15
|
+
rememberFallback(conversation: ConversationRef, generation: number, map: Record<string, string>): void;
|
|
16
|
+
dispatch(message: Message, admission?: Admission): Promise<boolean>;
|
|
17
|
+
resolveClaim(message: Message): boolean;
|
|
18
|
+
createForMessage(message: Message, bind?: {
|
|
19
|
+
readonly subjectId: string;
|
|
20
|
+
}): UserInteraction | undefined;
|
|
21
|
+
createFromUnknown(source: unknown): UserInteraction | undefined;
|
|
22
|
+
create(source: UserInteractionSource, subjectId?: string): UserInteraction;
|
|
23
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { conversationRefKey } from '@zhin.js/im-contract';
|
|
2
|
+
import { assertUserInteractionRequest, parseUserInteractionAnswer, projectUserInteraction, renderUserInteraction, } from '../../built/user-interaction.js';
|
|
3
|
+
import { RuntimeInteractiveRouter } from './interactive-router.js';
|
|
4
|
+
class UserInteractionTimeoutError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'UserInteractionTimeoutError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
class UserInteractionSupersededError extends Error {
|
|
11
|
+
constructor() {
|
|
12
|
+
super('User interaction superseded');
|
|
13
|
+
this.name = 'UserInteractionSupersededError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class UserInteractionDeliveryError extends Error {
|
|
17
|
+
constructor(receipt) {
|
|
18
|
+
super(`User interaction delivery failed: ${receipt.failure?.code ?? receipt.status}`);
|
|
19
|
+
this.name = 'UserInteractionDeliveryError';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Owns one ImRuntime's conversational claims, actions, and keyboard fallback state. */
|
|
23
|
+
export class RuntimeInteractionCoordinator {
|
|
24
|
+
#router;
|
|
25
|
+
#claims = new Map();
|
|
26
|
+
constructor(router = new RuntimeInteractiveRouter()) {
|
|
27
|
+
this.#router = router;
|
|
28
|
+
}
|
|
29
|
+
register(prefix, handler, admission) {
|
|
30
|
+
return this.#router.register(prefix, handler, admission);
|
|
31
|
+
}
|
|
32
|
+
rememberFallback(conversation, generation, map) {
|
|
33
|
+
this.#router.rememberFallback(conversation, generation, map);
|
|
34
|
+
}
|
|
35
|
+
dispatch(message, admission) {
|
|
36
|
+
return this.#router.dispatch(message, admission);
|
|
37
|
+
}
|
|
38
|
+
resolveClaim(message) {
|
|
39
|
+
const claim = this.#claims.get(this.#conversationKey(message));
|
|
40
|
+
if (!claim)
|
|
41
|
+
return false;
|
|
42
|
+
claim.resolve(this.#router.resolvePayload(message) ?? message.content);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
createForMessage(message, bind) {
|
|
46
|
+
const subjectId = bind?.subjectId?.trim();
|
|
47
|
+
if (bind && !subjectId)
|
|
48
|
+
return undefined;
|
|
49
|
+
if (typeof message.$reply !== 'function' || !message.conversation)
|
|
50
|
+
return undefined;
|
|
51
|
+
return this.create(message, subjectId);
|
|
52
|
+
}
|
|
53
|
+
createFromUnknown(source) {
|
|
54
|
+
if (!source || typeof source !== 'object')
|
|
55
|
+
return undefined;
|
|
56
|
+
const message = source;
|
|
57
|
+
if (typeof message.$reply !== 'function' || !message.conversation)
|
|
58
|
+
return undefined;
|
|
59
|
+
return this.create(message);
|
|
60
|
+
}
|
|
61
|
+
create(source, subjectId) {
|
|
62
|
+
const defaultTimeout = 3 * 60 * 1000;
|
|
63
|
+
const defaultTimeoutText = '输入超时';
|
|
64
|
+
const claim = (timeout, timeoutText, signal) => this.#claimNextMessage(source, timeout, timeoutText, signal, subjectId);
|
|
65
|
+
const reply = async (view) => {
|
|
66
|
+
const receipt = await source.$reply(renderUserInteraction(view));
|
|
67
|
+
if (receipt.status !== 'sent')
|
|
68
|
+
throw new UserInteractionDeliveryError(receipt);
|
|
69
|
+
};
|
|
70
|
+
const ask = async (request, progress) => {
|
|
71
|
+
assertUserInteractionRequest(request);
|
|
72
|
+
const timeout = request.timeout ?? defaultTimeout;
|
|
73
|
+
const timeoutText = request.timeoutText ?? defaultTimeoutText;
|
|
74
|
+
const deadline = Date.now() + timeout;
|
|
75
|
+
const view = projectUserInteraction(request, progress);
|
|
76
|
+
await reply(view);
|
|
77
|
+
while (true) {
|
|
78
|
+
try {
|
|
79
|
+
const raw = await claim(Math.max(1, deadline - Date.now()), timeoutText, request.signal);
|
|
80
|
+
const result = parseUserInteractionAnswer(request, raw);
|
|
81
|
+
if (result.ok)
|
|
82
|
+
return result.value;
|
|
83
|
+
await reply({
|
|
84
|
+
...view,
|
|
85
|
+
tip: [request.invalidText ?? result.message, view.tip].filter(Boolean).join('\n'),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (error instanceof UserInteractionTimeoutError
|
|
90
|
+
&& 'default' in request
|
|
91
|
+
&& request.default !== undefined) {
|
|
92
|
+
return request.default;
|
|
93
|
+
}
|
|
94
|
+
if (error instanceof UserInteractionTimeoutError) {
|
|
95
|
+
await reply({ title: '交互已结束', description: error.message });
|
|
96
|
+
}
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const sequence = async (definition) => {
|
|
102
|
+
if (!definition.title.trim()) {
|
|
103
|
+
throw new TypeError('User interaction sequence title must not be empty');
|
|
104
|
+
}
|
|
105
|
+
const ids = new Set();
|
|
106
|
+
for (const step of definition.steps) {
|
|
107
|
+
if (!step.id.trim()) {
|
|
108
|
+
throw new TypeError('User interaction sequence step id must not be empty');
|
|
109
|
+
}
|
|
110
|
+
if (ids.has(step.id)) {
|
|
111
|
+
throw new TypeError(`Duplicate user interaction sequence step id: ${step.id}`);
|
|
112
|
+
}
|
|
113
|
+
ids.add(step.id);
|
|
114
|
+
}
|
|
115
|
+
const result = {};
|
|
116
|
+
for (let index = 0; index < definition.steps.length; index += 1) {
|
|
117
|
+
const step = definition.steps[index];
|
|
118
|
+
const request = {
|
|
119
|
+
...step,
|
|
120
|
+
timeout: step.timeout ?? definition.timeout,
|
|
121
|
+
timeoutText: step.timeoutText ?? definition.timeoutText,
|
|
122
|
+
invalidText: step.invalidText ?? definition.invalidText,
|
|
123
|
+
signal: step.signal ?? definition.signal,
|
|
124
|
+
};
|
|
125
|
+
result[step.id] = await ask(request, {
|
|
126
|
+
title: definition.title,
|
|
127
|
+
description: definition.description,
|
|
128
|
+
tip: definition.tip,
|
|
129
|
+
index: index + 1,
|
|
130
|
+
total: definition.steps.length,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return Object.freeze(result);
|
|
134
|
+
};
|
|
135
|
+
return Object.freeze({ ask, sequence });
|
|
136
|
+
}
|
|
137
|
+
#claimNextMessage(source, timeout, timeoutText, signal, subjectId) {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
if (signal?.aborted) {
|
|
140
|
+
reject(abortError(signal, timeoutText));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const key = this.#conversationKey(source, subjectId);
|
|
144
|
+
const existing = this.#claims.get(key);
|
|
145
|
+
if (existing) {
|
|
146
|
+
clearTimeout(existing.timer);
|
|
147
|
+
existing.reject(new UserInteractionSupersededError());
|
|
148
|
+
}
|
|
149
|
+
const timer = setTimeout(() => {
|
|
150
|
+
settle(undefined, new UserInteractionTimeoutError(timeoutText));
|
|
151
|
+
}, timeout);
|
|
152
|
+
const onAbort = () => settle(undefined, abortError(signal, timeoutText));
|
|
153
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
154
|
+
const settle = (value, error) => {
|
|
155
|
+
if (this.#claims.get(key) !== claim)
|
|
156
|
+
return;
|
|
157
|
+
this.#claims.delete(key);
|
|
158
|
+
clearTimeout(timer);
|
|
159
|
+
signal?.removeEventListener('abort', onAbort);
|
|
160
|
+
if (value !== undefined)
|
|
161
|
+
resolve(value);
|
|
162
|
+
else
|
|
163
|
+
reject(error ?? new UserInteractionTimeoutError(timeoutText));
|
|
164
|
+
};
|
|
165
|
+
const claim = {
|
|
166
|
+
resolve: (raw) => settle(raw),
|
|
167
|
+
reject: (error) => settle(undefined, error),
|
|
168
|
+
timer,
|
|
169
|
+
};
|
|
170
|
+
this.#claims.set(key, claim);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
#conversationKey(source, subjectId = source.sender?.id ?? '') {
|
|
174
|
+
return `${conversationRefKey(source.conversation)}:${subjectId}`;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function abortError(signal, fallback) {
|
|
178
|
+
if (signal?.reason instanceof Error)
|
|
179
|
+
return signal.reason;
|
|
180
|
+
return new Error(fallback);
|
|
181
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ConversationRef } from '@zhin.js/im-contract';
|
|
2
|
+
import type { Message } from './contracts.js';
|
|
3
|
+
type RuntimeInteractiveHandler = (message: Message) => Promise<boolean> | boolean;
|
|
4
|
+
/**
|
|
5
|
+
* One ImRuntime-owned authority for interactive routing and keyboard fallback state.
|
|
6
|
+
* No handler or conversation state escapes to another runtime instance.
|
|
7
|
+
*/
|
|
8
|
+
export declare class RuntimeInteractiveRouter<Admission = never> {
|
|
9
|
+
#private;
|
|
10
|
+
private readonly fallbackTtlMs;
|
|
11
|
+
constructor(fallbackTtlMs?: number);
|
|
12
|
+
register(prefix: string, handler: RuntimeInteractiveHandler, admission?: Admission): () => void;
|
|
13
|
+
rememberFallback(conversation: ConversationRef, generation: number, map: Record<string, string>, ttlMs?: number): void;
|
|
14
|
+
resolvePayload(message: Message): string | undefined;
|
|
15
|
+
dispatch(message: Message, admission?: Admission): Promise<boolean>;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { resolvePayloadFromText } from '../../built/interactive-segments/action.js';
|
|
2
|
+
const DEFAULT_FALLBACK_TTL_MS = 60 * 60 * 1000;
|
|
3
|
+
function interactiveConversationKey(conversation, generation) {
|
|
4
|
+
const base = `${String(conversation.endpoint.id)}~${conversation.kind}:${conversation.id}`;
|
|
5
|
+
const parent = conversation.parent
|
|
6
|
+
? `@${conversation.parent.kind}:${conversation.parent.id}`
|
|
7
|
+
: '';
|
|
8
|
+
const thread = conversation.threadId ? `#${conversation.threadId}` : '';
|
|
9
|
+
return `${generation}:${base}${parent}${thread}`;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* One ImRuntime-owned authority for interactive routing and keyboard fallback state.
|
|
13
|
+
* No handler or conversation state escapes to another runtime instance.
|
|
14
|
+
*/
|
|
15
|
+
export class RuntimeInteractiveRouter {
|
|
16
|
+
fallbackTtlMs;
|
|
17
|
+
#handlers = [];
|
|
18
|
+
#fallbacks = new Map();
|
|
19
|
+
constructor(fallbackTtlMs = DEFAULT_FALLBACK_TTL_MS) {
|
|
20
|
+
this.fallbackTtlMs = fallbackTtlMs;
|
|
21
|
+
}
|
|
22
|
+
register(prefix, handler, admission) {
|
|
23
|
+
if (!prefix)
|
|
24
|
+
throw new TypeError('Interactive handler prefix must not be empty');
|
|
25
|
+
const entry = Object.freeze({
|
|
26
|
+
prefix,
|
|
27
|
+
handler,
|
|
28
|
+
admission,
|
|
29
|
+
});
|
|
30
|
+
this.#handlers.push(entry);
|
|
31
|
+
return () => {
|
|
32
|
+
const index = this.#handlers.indexOf(entry);
|
|
33
|
+
if (index >= 0)
|
|
34
|
+
this.#handlers.splice(index, 1);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
rememberFallback(conversation, generation, map, ttlMs = this.fallbackTtlMs) {
|
|
38
|
+
if (Object.keys(map).length === 0)
|
|
39
|
+
return;
|
|
40
|
+
this.#pruneFallbacks();
|
|
41
|
+
this.#fallbacks.set(interactiveConversationKey(conversation, generation), Object.freeze({
|
|
42
|
+
map: Object.freeze({ ...map }),
|
|
43
|
+
expiresAt: Date.now() + ttlMs,
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
resolvePayload(message) {
|
|
47
|
+
const fromSegments = actionPayloadFromSegments(message.segments);
|
|
48
|
+
if (fromSegments)
|
|
49
|
+
return fromSegments;
|
|
50
|
+
const raw = message.content.trim();
|
|
51
|
+
if (!raw)
|
|
52
|
+
return undefined;
|
|
53
|
+
return resolvePayloadFromText(raw, this.#fallbackMap(message.conversation, message.generation));
|
|
54
|
+
}
|
|
55
|
+
async dispatch(message, admission) {
|
|
56
|
+
const payload = this.resolvePayload(message);
|
|
57
|
+
if (!payload)
|
|
58
|
+
return false;
|
|
59
|
+
let match;
|
|
60
|
+
for (const entry of this.#handlers) {
|
|
61
|
+
if (entry.admission && entry.admission !== admission)
|
|
62
|
+
continue;
|
|
63
|
+
if (payload.startsWith(entry.prefix)
|
|
64
|
+
&& (!match || entry.prefix.length > match.prefix.length)) {
|
|
65
|
+
match = entry;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return match ? Boolean(await match.handler(message)) : false;
|
|
69
|
+
}
|
|
70
|
+
#fallbackMap(conversation, generation) {
|
|
71
|
+
const key = interactiveConversationKey(conversation, generation);
|
|
72
|
+
const entry = this.#fallbacks.get(key);
|
|
73
|
+
if (!entry)
|
|
74
|
+
return undefined;
|
|
75
|
+
if (entry.expiresAt < Date.now()) {
|
|
76
|
+
this.#fallbacks.delete(key);
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
return entry.map;
|
|
80
|
+
}
|
|
81
|
+
#pruneFallbacks() {
|
|
82
|
+
const now = Date.now();
|
|
83
|
+
for (const [key, entry] of this.#fallbacks) {
|
|
84
|
+
if (entry.expiresAt < now)
|
|
85
|
+
this.#fallbacks.delete(key);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function actionPayloadFromSegments(segments) {
|
|
90
|
+
for (const segment of segments ?? []) {
|
|
91
|
+
if (segment.type !== 'action')
|
|
92
|
+
continue;
|
|
93
|
+
const payload = segment.data?.payload;
|
|
94
|
+
if (typeof payload === 'string' && payload)
|
|
95
|
+
return payload;
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Generation-owned IM lifecycle event bus.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Runtime services resolve this bus through Scope+Token and never depend on a
|
|
5
|
+
* process-global event owner.
|
|
6
6
|
*/
|
|
7
7
|
import type { DeliveryReceipt } from '@zhin.js/im-contract';
|
|
8
8
|
import type { Message, OutboundEnvelope, SendContent } from './contracts.js';
|