@zhin.js/core 1.1.34 → 1.1.37
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 +84 -142
- package/lib/built/ai-access.d.ts +36 -0
- package/lib/built/ai-access.js +46 -0
- package/lib/built/ai-outbound/extensions/onebot-keyboard.d.ts +2 -0
- package/lib/built/ai-outbound/extensions/onebot-keyboard.js +67 -0
- package/lib/built/ai-outbound/extensions/qq-message.d.ts +3 -0
- package/lib/built/ai-outbound/extensions/qq-message.js +23 -0
- package/lib/built/ai-outbound/index.d.ts +8 -0
- package/lib/built/ai-outbound/index.js +8 -0
- package/lib/built/ai-outbound/parse.d.ts +13 -0
- package/lib/built/ai-outbound/parse.js +104 -0
- package/lib/built/ai-outbound/plain-mention-rewrite.d.ts +6 -0
- package/lib/built/ai-outbound/plain-mention-rewrite.js +37 -0
- package/lib/built/ai-outbound/prompt.d.ts +11 -0
- package/lib/built/ai-outbound/prompt.js +44 -0
- package/lib/built/ai-outbound/resolve.d.ts +4 -0
- package/lib/built/ai-outbound/resolve.js +64 -0
- package/lib/built/ai-outbound/structured-detect.d.ts +3 -0
- package/lib/built/ai-outbound/structured-detect.js +6 -0
- package/lib/built/ai-outbound/types.d.ts +49 -0
- package/lib/built/ai-outbound/types.js +5 -0
- package/lib/built/ai-trigger.d.ts +12 -17
- package/lib/built/ai-trigger.js +39 -70
- package/lib/built/common-adapter-tools.d.ts +33 -29
- package/lib/built/common-adapter-tools.js +79 -67
- package/lib/built/generated-qrcode.d.ts +24 -0
- package/lib/built/generated-qrcode.js +45 -0
- package/lib/built/generic-segment-mapper.d.ts +4 -0
- package/lib/built/generic-segment-mapper.js +190 -0
- package/lib/built/html-segment-fallback.d.ts +2 -5
- package/lib/built/html-segment-fallback.js +0 -16
- package/lib/built/html-to-text.d.ts +0 -1
- package/lib/built/html-to-text.js +15 -15
- package/lib/built/interactive-segments/action.d.ts +17 -0
- package/lib/built/interactive-segments/action.js +69 -0
- package/lib/built/interactive-segments/button-spec.d.ts +10 -0
- package/lib/built/interactive-segments/button-spec.js +21 -0
- package/lib/built/interactive-segments/index.d.ts +6 -0
- package/lib/built/interactive-segments/index.js +6 -0
- package/lib/built/interactive-segments/keyboard-segment.d.ts +10 -0
- package/lib/built/interactive-segments/keyboard-segment.js +11 -0
- package/lib/built/interactive-segments/onebot-keyboard.d.ts +6 -0
- package/lib/built/interactive-segments/onebot-keyboard.js +49 -0
- package/lib/built/interactive-segments/resolve.d.ts +14 -0
- package/lib/built/interactive-segments/resolve.js +109 -0
- package/lib/built/interactive-segments/types.d.ts +45 -0
- package/lib/built/interactive-segments/types.js +9 -0
- package/lib/built/login-assist.d.ts +43 -16
- package/lib/built/login-assist.js +113 -11
- package/lib/built/message-enrich.d.ts +32 -0
- package/lib/built/message-enrich.js +67 -0
- package/lib/built/outbound-media-utils.d.ts +9 -0
- package/lib/built/outbound-media-utils.js +62 -0
- package/lib/built/queue-im-field-contract.d.ts +7 -2
- package/lib/built/queue-im-field-contract.js +13 -4
- package/lib/built/rich-segments/markdown-to-text.d.ts +3 -0
- package/lib/built/rich-segments/markdown-to-text.js +27 -0
- package/lib/built/roles.d.ts +4 -5
- package/lib/built/roles.js +4 -5
- package/lib/built/segment-contract/assert.d.ts +7 -0
- package/lib/built/segment-contract/assert.js +39 -0
- package/lib/built/segment-contract/delivery.d.ts +5 -0
- package/lib/built/segment-contract/delivery.js +40 -0
- package/lib/built/segment-contract/image.d.ts +5 -0
- package/lib/built/segment-contract/image.js +10 -0
- package/lib/built/segment-contract/index.d.ts +10 -0
- package/lib/built/segment-contract/index.js +9 -0
- package/lib/built/segment-contract/json-schema.d.ts +28 -0
- package/lib/built/segment-contract/json-schema.js +128 -0
- package/lib/built/segment-contract/media.d.ts +12 -0
- package/lib/built/segment-contract/media.js +22 -0
- package/lib/built/segment-contract/mention.d.ts +10 -0
- package/lib/built/segment-contract/mention.js +26 -0
- package/lib/built/segment-contract/preview.d.ts +3 -0
- package/lib/built/segment-contract/preview.js +159 -0
- package/lib/built/segment-contract/text.d.ts +7 -0
- package/lib/built/segment-contract/text.js +34 -0
- package/lib/built/segment-contract/types.d.ts +58 -0
- package/lib/built/segment-contract/types.js +1 -0
- package/lib/built/segment-contract/validate.d.ts +229 -0
- package/lib/built/segment-contract/validate.js +172 -0
- package/lib/built/tool-access.d.ts +5 -0
- package/lib/built/tool-access.js +17 -0
- package/lib/built/user-interaction.d.ts +30 -0
- package/lib/built/user-interaction.js +244 -0
- package/lib/component.d.ts +0 -1
- package/lib/component.js +30 -14
- package/lib/errors.d.ts +2 -3
- package/lib/errors.js +4 -5
- package/lib/feature/adapter.d.ts +2 -0
- package/lib/feature/adapter.js +2 -0
- package/lib/feature/command.d.ts +2 -0
- package/lib/feature/command.js +2 -0
- package/lib/feature/component.d.ts +2 -0
- package/lib/feature/component.js +2 -0
- package/lib/feature/handler.d.ts +48 -0
- package/lib/feature/handler.js +26 -0
- package/lib/feature/middleware.d.ts +2 -0
- package/lib/feature/middleware.js +2 -0
- package/lib/im-scene.d.ts +29 -0
- package/lib/im-scene.js +75 -0
- package/lib/im-session-id.d.ts +39 -0
- package/lib/im-session-id.js +47 -0
- package/lib/index.d.ts +29 -36
- package/lib/index.js +27 -41
- package/lib/jsx-dev-runtime.d.ts +0 -1
- package/lib/jsx-dev-runtime.js +0 -1
- package/lib/jsx-runtime.d.ts +1 -1
- package/lib/jsx-runtime.js +0 -1
- package/lib/jsx.d.ts +19 -22
- package/lib/jsx.js +0 -1
- package/lib/message.d.ts +17 -12
- package/lib/message.js +16 -13
- package/lib/models/system-log.d.ts +0 -3
- package/lib/models/system-log.js +1 -9
- package/lib/models/user.d.ts +0 -3
- package/lib/models/user.js +1 -8
- package/lib/notice.d.ts +16 -60
- package/lib/notice.js +1 -4
- package/lib/plugin-runtime/im/contracts.d.ts +191 -0
- package/lib/plugin-runtime/im/contracts.js +169 -0
- 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 +84 -0
- package/lib/plugin-runtime/im/im-runtime.js +201 -0
- 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 +11 -0
- package/lib/plugin-runtime/im/index.js +9 -0
- 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/login-assist-host.d.ts +6 -0
- package/lib/plugin-runtime/im/login-assist-host.js +6 -0
- package/lib/plugin-runtime/im/message-bus.d.ts +38 -0
- package/lib/plugin-runtime/im/message-bus.js +40 -0
- package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -0
- package/lib/plugin-runtime/im/message-dispatcher.js +107 -0
- 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-renderer.d.ts +7 -0
- package/lib/plugin-runtime/im/outbound-renderer.js +70 -0
- package/lib/plugin-runtime/im/outbound-segments.d.ts +71 -0
- package/lib/plugin-runtime/im/outbound-segments.js +219 -0
- package/lib/plugin-runtime/im/public-api.d.ts +6 -0
- package/lib/plugin-runtime/im/public-api.js +6 -0
- 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 +17 -62
- package/lib/request.js +0 -4
- package/lib/side-event/base.d.ts +51 -0
- package/lib/side-event/base.js +30 -0
- package/lib/side-event/dispatch.d.ts +15 -0
- package/lib/side-event/dispatch.js +140 -0
- package/lib/side-event/index.d.ts +4 -0
- package/lib/side-event/index.js +4 -0
- package/lib/side-event/normalize.d.ts +38 -0
- package/lib/side-event/normalize.js +111 -0
- package/lib/side-event/types.d.ts +23 -0
- package/lib/side-event/types.js +55 -0
- package/lib/system-event.d.ts +14 -0
- package/lib/system-event.js +7 -0
- package/lib/types.d.ts +35 -193
- package/lib/types.js +0 -1
- package/lib/utils.d.ts +81 -6
- package/lib/utils.js +54 -25
- package/package.json +94 -16
- package/lib/adapter.d.ts +0 -96
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -217
- package/lib/adapter.js.map +0 -1
- package/lib/agent-prompt.d.ts +0 -37
- package/lib/agent-prompt.d.ts.map +0 -1
- package/lib/agent-prompt.js +0 -2
- package/lib/agent-prompt.js.map +0 -1
- package/lib/bot.d.ts +0 -38
- package/lib/bot.d.ts.map +0 -1
- package/lib/bot.js +0 -2
- package/lib/bot.js.map +0 -1
- package/lib/built/adapter-process.d.ts +0 -43
- package/lib/built/adapter-process.d.ts.map +0 -1
- package/lib/built/adapter-process.js +0 -81
- package/lib/built/adapter-process.js.map +0 -1
- package/lib/built/agent-preset.d.ts +0 -65
- package/lib/built/agent-preset.d.ts.map +0 -1
- package/lib/built/agent-preset.js +0 -80
- package/lib/built/agent-preset.js.map +0 -1
- package/lib/built/ai-trigger.d.ts.map +0 -1
- package/lib/built/ai-trigger.js.map +0 -1
- package/lib/built/command.d.ts +0 -68
- package/lib/built/command.d.ts.map +0 -1
- package/lib/built/command.js +0 -92
- package/lib/built/command.js.map +0 -1
- package/lib/built/common-adapter-tools.d.ts.map +0 -1
- package/lib/built/common-adapter-tools.js.map +0 -1
- package/lib/built/component.d.ts +0 -69
- package/lib/built/component.d.ts.map +0 -1
- package/lib/built/component.js +0 -99
- package/lib/built/component.js.map +0 -1
- package/lib/built/config.d.ts +0 -99
- package/lib/built/config.d.ts.map +0 -1
- package/lib/built/config.js +0 -355
- package/lib/built/config.js.map +0 -1
- package/lib/built/cron.d.ts +0 -75
- package/lib/built/cron.d.ts.map +0 -1
- package/lib/built/cron.js +0 -122
- package/lib/built/cron.js.map +0 -1
- package/lib/built/database.d.ts +0 -66
- package/lib/built/database.d.ts.map +0 -1
- package/lib/built/database.js +0 -100
- package/lib/built/database.js.map +0 -1
- package/lib/built/dispatcher.d.ts +0 -139
- package/lib/built/dispatcher.d.ts.map +0 -1
- package/lib/built/dispatcher.js +0 -361
- package/lib/built/dispatcher.js.map +0 -1
- package/lib/built/html-segment-fallback.d.ts.map +0 -1
- package/lib/built/html-segment-fallback.js.map +0 -1
- package/lib/built/html-to-text.d.ts.map +0 -1
- package/lib/built/html-to-text.js.map +0 -1
- package/lib/built/inbound-runner.d.ts +0 -23
- package/lib/built/inbound-runner.d.ts.map +0 -1
- package/lib/built/inbound-runner.js +0 -37
- package/lib/built/inbound-runner.js.map +0 -1
- package/lib/built/login-assist.d.ts.map +0 -1
- package/lib/built/login-assist.js.map +0 -1
- package/lib/built/mcp-mesh-registrar.d.ts +0 -9
- package/lib/built/mcp-mesh-registrar.d.ts.map +0 -1
- package/lib/built/mcp-mesh-registrar.js +0 -17
- package/lib/built/mcp-mesh-registrar.js.map +0 -1
- package/lib/built/message-filter.d.ts +0 -177
- package/lib/built/message-filter.d.ts.map +0 -1
- package/lib/built/message-filter.js +0 -241
- package/lib/built/message-filter.js.map +0 -1
- package/lib/built/permission.d.ts +0 -55
- package/lib/built/permission.d.ts.map +0 -1
- package/lib/built/permission.js +0 -112
- package/lib/built/permission.js.map +0 -1
- package/lib/built/prepend-quote-context.d.ts +0 -35
- package/lib/built/prepend-quote-context.d.ts.map +0 -1
- package/lib/built/prepend-quote-context.js +0 -131
- package/lib/built/prepend-quote-context.js.map +0 -1
- package/lib/built/queue-im-field-contract.d.ts.map +0 -1
- package/lib/built/queue-im-field-contract.js.map +0 -1
- package/lib/built/roles.d.ts.map +0 -1
- package/lib/built/roles.js.map +0 -1
- package/lib/built/runtime-io.d.ts +0 -10
- package/lib/built/runtime-io.d.ts.map +0 -1
- package/lib/built/runtime-io.js +0 -68
- package/lib/built/runtime-io.js.map +0 -1
- package/lib/built/schema-feature.d.ts +0 -78
- package/lib/built/schema-feature.d.ts.map +0 -1
- package/lib/built/schema-feature.js +0 -132
- package/lib/built/schema-feature.js.map +0 -1
- package/lib/built/skill.d.ts +0 -121
- package/lib/built/skill.d.ts.map +0 -1
- package/lib/built/skill.js +0 -152
- package/lib/built/skill.js.map +0 -1
- package/lib/built/tool.d.ts +0 -150
- package/lib/built/tool.d.ts.map +0 -1
- package/lib/built/tool.js +0 -383
- package/lib/built/tool.js.map +0 -1
- package/lib/command.d.ts +0 -42
- package/lib/command.d.ts.map +0 -1
- package/lib/command.js +0 -75
- package/lib/command.js.map +0 -1
- package/lib/component.d.ts.map +0 -1
- package/lib/component.js.map +0 -1
- package/lib/errors.d.ts.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/jsx-dev-runtime.d.ts.map +0 -1
- package/lib/jsx-dev-runtime.js.map +0 -1
- package/lib/jsx-runtime.d.ts.map +0 -1
- package/lib/jsx-runtime.js.map +0 -1
- package/lib/jsx.d.ts.map +0 -1
- package/lib/jsx.js.map +0 -1
- package/lib/message-quote.d.ts +0 -11
- package/lib/message-quote.d.ts.map +0 -1
- package/lib/message-quote.js +0 -91
- package/lib/message-quote.js.map +0 -1
- package/lib/message.d.ts.map +0 -1
- package/lib/message.js.map +0 -1
- package/lib/models/system-log.d.ts.map +0 -1
- package/lib/models/system-log.js.map +0 -1
- package/lib/models/user.d.ts.map +0 -1
- package/lib/models/user.js.map +0 -1
- package/lib/notice.d.ts.map +0 -1
- package/lib/notice.js.map +0 -1
- package/lib/plugin.d.ts +0 -258
- package/lib/plugin.d.ts.map +0 -1
- package/lib/plugin.js +0 -688
- package/lib/plugin.js.map +0 -1
- package/lib/prompt.d.ts +0 -118
- package/lib/prompt.d.ts.map +0 -1
- package/lib/prompt.js +0 -244
- package/lib/prompt.js.map +0 -1
- package/lib/request.d.ts.map +0 -1
- package/lib/request.js.map +0 -1
- package/lib/scheduler/scheduler.d.ts +0 -49
- package/lib/scheduler/scheduler.d.ts.map +0 -1
- package/lib/scheduler/scheduler.js +0 -352
- package/lib/scheduler/scheduler.js.map +0 -1
- package/lib/scheduler/types.d.ts +0 -71
- package/lib/scheduler/types.d.ts.map +0 -1
- package/lib/scheduler/types.js +0 -8
- package/lib/scheduler/types.js.map +0 -1
- package/lib/tool-zod.d.ts +0 -28
- package/lib/tool-zod.d.ts.map +0 -1
- package/lib/tool-zod.js +0 -98
- package/lib/tool-zod.js.map +0 -1
- package/lib/types-generator.d.ts +0 -6
- package/lib/types-generator.d.ts.map +0 -1
- package/lib/types-generator.js +0 -72
- package/lib/types-generator.js.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js.map +0 -1
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { createToken, generationAdmissionBinder, } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
import { PermissionHost, permissionHostToken } from '@zhin.js/permission';
|
|
4
|
+
import { MessageBus, messageBusToken } from './message-bus.js';
|
|
5
|
+
import { endpointEventGatewayToken, } from '@zhin.js/adapter';
|
|
6
|
+
import { loginAssistToken } from './login-assist-host.js';
|
|
7
|
+
import { LoginAssist } from '../../built/login-assist.js';
|
|
8
|
+
import { RuntimeInteractionCoordinator } from './interaction-runtime.js';
|
|
9
|
+
import { ConversationRuntime } from './conversation-runtime.js';
|
|
10
|
+
import { EndpointRuntime, requireAdapters, } from './endpoint-runtime.js';
|
|
11
|
+
import { RuntimeMessageEventStream, } from './message-events.js';
|
|
12
|
+
import { OutboundDeliveryRuntime, failedDeliveryReceipt, } from './outbound-delivery-runtime.js';
|
|
13
|
+
import { InboundRuntime } from './inbound-runtime.js';
|
|
14
|
+
/** @public Stable inbound and outbound IM gateway token for Adapter integrations. */
|
|
15
|
+
export const outboundMessageToken = createToken('zhin.im.outbound-message');
|
|
16
|
+
export class ImRuntime {
|
|
17
|
+
#messageEventStream = new RuntimeMessageEventStream();
|
|
18
|
+
messageEvents = Object.freeze({
|
|
19
|
+
subscribe: (listener) => this.#messageEventStream.subscribe(listener),
|
|
20
|
+
});
|
|
21
|
+
#interactions = new RuntimeInteractionCoordinator();
|
|
22
|
+
#conversations;
|
|
23
|
+
#outbound;
|
|
24
|
+
#inbound;
|
|
25
|
+
endpoints;
|
|
26
|
+
#operationSnapshot = new AsyncLocalStorage();
|
|
27
|
+
#snapshots;
|
|
28
|
+
constructor(options = {}) {
|
|
29
|
+
this.#conversations = new ConversationRuntime(options.conversationEvents);
|
|
30
|
+
this.#outbound = new OutboundDeliveryRuntime({
|
|
31
|
+
record: (request, receipt) => this.#conversations.recordOutbound(request, receipt),
|
|
32
|
+
rememberFallback: (conversation, generation, map) => this.#interactions.rememberFallback(conversation, generation, map),
|
|
33
|
+
publish: (event) => this.#messageEventStream.publish(event),
|
|
34
|
+
}, options.renderer);
|
|
35
|
+
this.#inbound = new InboundRuntime({
|
|
36
|
+
interactions: this.#interactions,
|
|
37
|
+
inboundClaim: options.inboundClaim,
|
|
38
|
+
enrichSender: options.enrichSender,
|
|
39
|
+
acquire: () => this.#acquire(),
|
|
40
|
+
release: (lease) => this.#release(lease),
|
|
41
|
+
deliver: (request, snapshot) => this.#outbound.deliver(request, snapshot),
|
|
42
|
+
recordIncoming: (input, sender) => this.#conversations.recordIncoming(input, sender),
|
|
43
|
+
recordNotice: (notice) => this.#conversations.recordNotice(notice),
|
|
44
|
+
publish: (event) => this.#messageEventStream.publish(event),
|
|
45
|
+
}, options.commandPrefix);
|
|
46
|
+
this.endpoints = new EndpointRuntime({
|
|
47
|
+
acquire: () => this.#acquire(),
|
|
48
|
+
release: (lease) => this.#release(lease),
|
|
49
|
+
send: (request, snapshot) => this.#outbound.deliver(request, snapshot),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** Process composition replaces the bootstrap memory store after required DB activation. */
|
|
53
|
+
replaceConversationEventStore(store) {
|
|
54
|
+
this.#conversations.replaceStore(store);
|
|
55
|
+
}
|
|
56
|
+
get conversationEventReader() {
|
|
57
|
+
return this.#conversations.reader;
|
|
58
|
+
}
|
|
59
|
+
/** Pins all nested IM operations to the snapshot current at operation ingress. */
|
|
60
|
+
async runWithSnapshotView(operation) {
|
|
61
|
+
const inherited = this.#operationSnapshot.getStore();
|
|
62
|
+
if (inherited?.active)
|
|
63
|
+
return operation();
|
|
64
|
+
if (!this.#snapshots)
|
|
65
|
+
throw new Error('ImRuntime is not attached to a Root');
|
|
66
|
+
const lease = this.#snapshots.acquire();
|
|
67
|
+
try {
|
|
68
|
+
return await this.#operationSnapshot.run(lease, operation);
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
lease.release();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async resolveConversationReference(lease, reference, context) {
|
|
75
|
+
if (!this.#snapshots?.owns(lease) || !lease.active) {
|
|
76
|
+
return Object.freeze({ status: 'expired', code: 'generation_lease_expired' });
|
|
77
|
+
}
|
|
78
|
+
const local = await this.#conversations.resolveLocal(reference);
|
|
79
|
+
if (local)
|
|
80
|
+
return local;
|
|
81
|
+
context.signal.throwIfAborted();
|
|
82
|
+
const conversation = reference.kind === 'message' ? reference.message.conversation : reference.conversation;
|
|
83
|
+
return requireAdapters(lease.value).resolveContent(conversation.endpoint.id, reference, context);
|
|
84
|
+
}
|
|
85
|
+
async readConversationContext(conversation, consumer, throughSequence, limit = 50, excludeMessageId) {
|
|
86
|
+
return this.#conversations.readContext(conversation, consumer, throughSequence, limit, excludeMessageId);
|
|
87
|
+
}
|
|
88
|
+
async commitConversationContext(conversation, consumer, cursor) {
|
|
89
|
+
await this.#conversations.commitContext(conversation, consumer, cursor);
|
|
90
|
+
}
|
|
91
|
+
attach(snapshots) {
|
|
92
|
+
if (this.#snapshots && this.#snapshots !== snapshots) {
|
|
93
|
+
throw new Error('ImRuntime is already attached to another Root');
|
|
94
|
+
}
|
|
95
|
+
this.#snapshots = snapshots;
|
|
96
|
+
}
|
|
97
|
+
permissionHost = new PermissionHost();
|
|
98
|
+
messageBus = new MessageBus();
|
|
99
|
+
loginAssist = new LoginAssist();
|
|
100
|
+
install(resources) {
|
|
101
|
+
resources.provide(outboundMessageToken, this);
|
|
102
|
+
resources.provide(endpointEventGatewayToken, this.endpointEvents);
|
|
103
|
+
resources.provide(loginAssistToken, this.loginAssist);
|
|
104
|
+
resources.provide(permissionHostToken, this.permissionHost);
|
|
105
|
+
resources.provide(messageBusToken, this.messageBus);
|
|
106
|
+
}
|
|
107
|
+
/** The sole Adapter ingress, also installed as endpointEventGatewayToken. */
|
|
108
|
+
endpointEvents = (() => {
|
|
109
|
+
const self = this;
|
|
110
|
+
const gateway = {
|
|
111
|
+
receive: (event) => self.#inbound.receive(event),
|
|
112
|
+
[generationAdmissionBinder](gate) {
|
|
113
|
+
return Object.freeze({
|
|
114
|
+
receive: async (event) => gate.enter(() => self.#inbound.receive(event, gate)),
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
return gateway;
|
|
119
|
+
})();
|
|
120
|
+
[generationAdmissionBinder](gate) {
|
|
121
|
+
const gateway = {
|
|
122
|
+
send: async (request) => gate.enter(() => this.send(request))
|
|
123
|
+
?? failedDeliveryReceipt('generation_not_admitted'),
|
|
124
|
+
registerInteractiveHandler: (prefix, handler) => this.#interactions.register(prefix, handler, gate),
|
|
125
|
+
};
|
|
126
|
+
return Object.freeze(gateway);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 注册 interactive action 回跳 handler(prefix 最长匹配;返回注销函数)。
|
|
130
|
+
* 在 Command dispatch 之前路由:action 段 / 数字回跳 / 指令预填 payload。
|
|
131
|
+
*/
|
|
132
|
+
registerInteractiveHandler(prefix, handler) {
|
|
133
|
+
return this.#interactions.register(prefix, handler);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* User interaction bound to this conversation.
|
|
137
|
+
* `bind.subjectId` waits for that user (e.g. master) instead of the message sender.
|
|
138
|
+
*/
|
|
139
|
+
createInteraction(message, bind) {
|
|
140
|
+
return this.#inbound.createInteraction(message, bind);
|
|
141
|
+
}
|
|
142
|
+
async send(request) {
|
|
143
|
+
const lease = this.#acquire();
|
|
144
|
+
try {
|
|
145
|
+
return await this.#outbound.deliver(request, lease.value);
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
this.#release(lease);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Sends through the exact generation lease that admitted the current operation. */
|
|
152
|
+
async sendWithSnapshotLease(lease, request) {
|
|
153
|
+
if (!this.#snapshots?.owns(lease) || !lease.active) {
|
|
154
|
+
throw new Error('Outbound message generation lease expired');
|
|
155
|
+
}
|
|
156
|
+
return this.#outbound.deliver(request, lease.value);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Console `GET /api/stats` 同源计数:非 root 插件节点 + AdapterIndex endpoints。
|
|
160
|
+
* 供命令 / 状态卡等在 Plugin Runtime 下读取(legacy `root.adapters` / `root.children` 已不存在)。
|
|
161
|
+
*/
|
|
162
|
+
inventory() {
|
|
163
|
+
try {
|
|
164
|
+
const lease = this.#acquire();
|
|
165
|
+
try {
|
|
166
|
+
const plugins = [...lease.value.tree.values()]
|
|
167
|
+
.filter((node) => node.parent !== undefined).length;
|
|
168
|
+
const endpoints = requireAdapters(lease.value).describe();
|
|
169
|
+
return Object.freeze({
|
|
170
|
+
plugins,
|
|
171
|
+
endpoints: Object.freeze({
|
|
172
|
+
total: endpoints.length,
|
|
173
|
+
online: endpoints.filter((endpoint) => endpoint.status === 'online').length,
|
|
174
|
+
}),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
this.#release(lease);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return Object.freeze({
|
|
183
|
+
plugins: 0,
|
|
184
|
+
endpoints: Object.freeze({ total: 0, online: 0 }),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
#acquire() {
|
|
189
|
+
const inherited = this.#operationSnapshot.getStore();
|
|
190
|
+
if (inherited?.active)
|
|
191
|
+
return inherited;
|
|
192
|
+
if (!this.#snapshots)
|
|
193
|
+
throw new Error('ImRuntime is not attached to a Root');
|
|
194
|
+
return this.#snapshots.acquire();
|
|
195
|
+
}
|
|
196
|
+
#release(lease) {
|
|
197
|
+
if (this.#operationSnapshot.getStore() === lease)
|
|
198
|
+
return;
|
|
199
|
+
lease.release();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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';
|
|
5
|
+
export * from './im-runtime.js';
|
|
6
|
+
export * from './login-assist-host.js';
|
|
7
|
+
export * from './message-bus.js';
|
|
8
|
+
export * from './message-dispatcher.js';
|
|
9
|
+
export type { RuntimeMessageEvent, RuntimeMessageEventSource, } from './message-events.js';
|
|
10
|
+
export * from './outbound-renderer.js';
|
|
11
|
+
export * from './outbound-segments.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './contracts.js';
|
|
2
|
+
export { EndpointRuntime } from './endpoint-runtime.js';
|
|
3
|
+
export * from './ingress-route.js';
|
|
4
|
+
export * from './im-runtime.js';
|
|
5
|
+
export * from './login-assist-host.js';
|
|
6
|
+
export * from './message-bus.js';
|
|
7
|
+
export * from './message-dispatcher.js';
|
|
8
|
+
export * from './outbound-renderer.js';
|
|
9
|
+
export * from './outbound-segments.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
|
+
}
|