@zhin.js/core 1.5.16 → 1.7.0
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 +78 -195
- package/lib/ai-completion-contract.d.ts +135 -0
- package/lib/ai-completion-contract.js +69 -0
- package/lib/built/ai-access.d.ts +2 -21
- package/lib/built/ai-access.js +1 -46
- package/lib/built/ai-outbound/index.d.ts +0 -2
- package/lib/built/ai-outbound/index.js +0 -2
- package/lib/built/ai-outbound/prompt.d.ts +2 -0
- package/lib/built/ai-outbound/prompt.js +2 -0
- package/lib/built/ai-outbound/types.d.ts +1 -10
- package/lib/built/ai-trigger.d.ts +39 -78
- package/lib/built/ai-trigger.js +3 -254
- package/lib/built/common-adapter-tools.d.ts +21 -6
- package/lib/built/common-adapter-tools.js +28 -17
- package/lib/built/generic-segment-mapper.js +4 -1
- package/lib/built/html-segment-fallback.d.ts +6 -7
- package/lib/built/html-segment-fallback.js +1 -16
- package/lib/built/html-to-text.d.ts +0 -3
- package/lib/built/html-to-text.js +131 -40
- package/lib/built/interaction-messages.d.ts +1 -0
- package/lib/built/interaction-messages.js +30 -0
- package/lib/built/interactive-segments/action.d.ts +3 -4
- package/lib/built/interactive-segments/action.js +2 -5
- 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 +2 -3
- package/lib/built/interactive-segments/resolve.js +21 -14
- package/lib/built/interactive-segments/types.d.ts +2 -7
- package/lib/built/interactive-segments/types.js +22 -2
- package/lib/built/login-assist.d.ts +330 -25
- package/lib/built/login-assist.js +1156 -75
- package/lib/built/login-captcha.d.ts +62 -0
- package/lib/built/login-captcha.js +107 -0
- package/lib/built/message-enrich.d.ts +24 -27
- package/lib/built/message-enrich.js +53 -49
- package/lib/built/outbound-content-arrays.d.ts +4 -0
- package/lib/built/outbound-content-arrays.js +32 -0
- package/lib/built/outbound-media-trust.d.ts +46 -0
- package/lib/built/outbound-media-trust.js +245 -0
- package/lib/built/outbound-media-utils.d.ts +5 -3
- package/lib/built/outbound-media-utils.js +5 -3
- package/lib/built/readable-link.d.ts +4 -0
- package/lib/built/readable-link.js +32 -0
- package/lib/built/rich-segments/markdown-to-text.d.ts +1 -2
- package/lib/built/rich-segments/markdown-to-text.js +141 -17
- package/lib/built/roles.d.ts +141 -12
- package/lib/built/roles.js +253 -52
- package/lib/built/segment-contract/assert.js +8 -2
- package/lib/built/segment-contract/delivery.js +1 -0
- package/lib/built/segment-contract/index.d.ts +3 -3
- package/lib/built/segment-contract/index.js +2 -2
- package/lib/built/segment-contract/json-schema.d.ts +39 -5
- package/lib/built/segment-contract/json-schema.js +207 -24
- package/lib/built/segment-contract/preview.js +8 -1
- package/lib/built/segment-contract/text.js +1 -1
- package/lib/built/segment-contract/types.d.ts +2 -2
- package/lib/built/segment-contract/validate.d.ts +1553 -224
- package/lib/built/segment-contract/validate.js +39 -3
- package/lib/built/tool-access.d.ts +5 -0
- package/lib/built/tool-access.js +17 -0
- package/lib/built/user-interaction.d.ts +5 -4
- package/lib/built/user-interaction.js +64 -52
- package/lib/errors.d.ts +1 -18
- package/lib/errors.js +1 -26
- package/lib/feature/command-public-api.d.ts +5 -0
- package/lib/feature/command-public-api.js +5 -0
- package/lib/feature/command.d.ts +2 -0
- package/lib/feature/command.js +1 -0
- package/lib/feature/component.d.ts +1 -0
- package/lib/feature/component.js +1 -0
- package/lib/feature/handler.d.ts +13 -7
- package/lib/feature/handler.js +2 -1
- package/lib/feature/middleware-public-api.d.ts +6 -0
- package/lib/feature/middleware-public-api.js +6 -0
- package/lib/feature/middleware.d.ts +22 -2
- package/lib/feature/middleware.js +7 -2
- package/lib/im-scene.d.ts +4 -3
- package/lib/im-scene.js +9 -9
- package/lib/im-session-id.d.ts +20 -12
- package/lib/im-session-id.js +8 -8
- package/lib/index.d.ts +20 -48
- package/lib/index.js +15 -41
- package/lib/jsx/intrinsic-elements.d.ts +83 -0
- package/lib/jsx/message-renderer.d.ts +13 -0
- package/lib/jsx/message-renderer.js +128 -0
- package/lib/jsx/message-tags.d.ts +133 -0
- package/lib/jsx/message-tags.js +50 -0
- package/lib/jsx-dev-runtime.d.ts +2 -2
- package/lib/jsx-dev-runtime.js +1 -2
- package/lib/jsx-runtime.d.ts +2 -12
- package/lib/jsx-runtime.js +1 -10
- package/lib/jsx.d.ts +6 -30
- package/lib/jsx.js +3 -93
- package/lib/message.d.ts +0 -40
- package/lib/message.js +1 -27
- 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 +38 -33
- package/lib/notice.js +23 -7
- package/lib/plugin-runtime/im/command-feedback.d.ts +59 -0
- package/lib/plugin-runtime/im/command-feedback.js +148 -0
- package/lib/plugin-runtime/im/contracts.d.ts +217 -32
- package/lib/plugin-runtime/im/contracts.js +141 -23
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +42 -0
- package/lib/plugin-runtime/im/conversation-runtime.js +451 -0
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +112 -0
- package/lib/plugin-runtime/im/endpoint-runtime.js +434 -0
- package/lib/plugin-runtime/im/feature-gate.d.ts +21 -0
- package/lib/plugin-runtime/im/feature-gate.js +88 -0
- 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 +152 -0
- package/lib/plugin-runtime/im/host/database-console-schema.d.ts +79 -0
- package/lib/plugin-runtime/im/host/database-console-schema.js +477 -0
- package/lib/plugin-runtime/im/host/database-console-sql.d.ts +66 -0
- package/lib/plugin-runtime/im/host/database-console-sql.js +459 -0
- package/lib/plugin-runtime/im/host/database-host-installer.d.ts +60 -0
- package/lib/plugin-runtime/im/host/database-host-installer.js +1166 -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 +34 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.js +82 -0
- package/lib/plugin-runtime/im/im-runtime.d.ts +40 -111
- package/lib/plugin-runtime/im/im-runtime.js +235 -1059
- package/lib/plugin-runtime/im/inbound-dedup.d.ts +84 -0
- package/lib/plugin-runtime/im/inbound-dedup.js +322 -0
- package/lib/plugin-runtime/im/inbound-origin.d.ts +150 -0
- package/lib/plugin-runtime/im/inbound-origin.js +183 -0
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +59 -0
- package/lib/plugin-runtime/im/inbound-runtime.js +478 -0
- package/lib/plugin-runtime/im/inbound-spine.d.ts +196 -0
- package/lib/plugin-runtime/im/inbound-spine.js +335 -0
- package/lib/plugin-runtime/im/index.d.ts +15 -4
- package/lib/plugin-runtime/im/index.js +11 -4
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +50 -0
- package/lib/plugin-runtime/im/interaction-runtime.js +390 -0
- package/lib/plugin-runtime/im/interactive-router.d.ts +81 -0
- package/lib/plugin-runtime/im/interactive-router.js +382 -0
- package/lib/plugin-runtime/im/message-diagnostics.d.ts +70 -0
- package/lib/plugin-runtime/im/message-diagnostics.js +182 -0
- package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -4
- package/lib/plugin-runtime/im/message-dispatcher.js +138 -20
- package/lib/plugin-runtime/im/message-events.d.ts +85 -0
- package/lib/plugin-runtime/im/message-events.js +113 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +39 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +450 -0
- package/lib/plugin-runtime/im/outbound-renderer.d.ts +52 -3
- package/lib/plugin-runtime/im/outbound-renderer.js +188 -8
- package/lib/plugin-runtime/im/outbound-segments.d.ts +29 -5
- package/lib/plugin-runtime/im/outbound-segments.js +130 -36
- package/lib/plugin-runtime/im/progressive-reply.d.ts +58 -0
- package/lib/plugin-runtime/im/progressive-reply.js +501 -0
- package/lib/plugin-runtime/im/public-api.d.ts +1 -1
- package/lib/request.d.ts +27 -36
- package/lib/request.js +14 -6
- package/lib/side-event/base.d.ts +64 -44
- package/lib/side-event/base.js +72 -19
- package/lib/side-event/index.d.ts +1 -1
- package/lib/side-event/index.js +1 -1
- package/lib/side-event/normalize.d.ts +7 -38
- package/lib/side-event/normalize.js +4 -109
- package/lib/side-event/scene-events.d.ts +40 -0
- package/lib/side-event/scene-events.js +147 -0
- package/lib/side-event/types.d.ts +4 -13
- package/lib/side-event/types.js +1 -55
- package/lib/system-event.d.ts +17 -12
- package/lib/system-event.js +8 -6
- package/lib/types.d.ts +23 -167
- package/lib/utils.d.ts +50 -33
- package/lib/utils.js +12 -185
- 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 +24 -50
- 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/ai-outbound/extensions/qq-message.d.ts +0 -3
- package/lib/built/ai-outbound/extensions/qq-message.js +0 -23
- 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/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/queue-im-field-contract.d.ts +0 -32
- package/lib/built/queue-im-field-contract.js +0 -67
- 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/component.d.ts +0 -26
- package/lib/component.js +0 -583
- 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/login-assist-host.d.ts +0 -6
- package/lib/plugin-runtime/im/login-assist-host.js +0 -6
- 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/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/side-event/dispatch.d.ts +0 -15
- package/lib/side-event/dispatch.js +0 -140
- package/lib/tool-zod.d.ts +0 -43
- package/lib/tool-zod.js +0 -184
- /package/lib/{built/endpoint-manager.js → jsx/intrinsic-elements.js} +0 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import { createCoreReplyStream } from './progressive-reply.js';
|
|
2
|
+
import { supportsIncrementalOutbound } from './inbound-spine.js';
|
|
3
|
+
import { operationEndpoint, } from './contracts.js';
|
|
4
|
+
import { adapterFeatureId, isAdapterIndex, } from '@zhin.js/adapter';
|
|
5
|
+
import { normalizeDeliveryTarget } from '@zhin.js/plugin-runtime';
|
|
6
|
+
import { EndpointDeliveryError, } from '@zhin.js/im-contract';
|
|
7
|
+
/** Owns generation-leased Endpoint discovery, control, management, and Console delivery. */
|
|
8
|
+
export class EndpointRuntime {
|
|
9
|
+
context;
|
|
10
|
+
constructor(context) {
|
|
11
|
+
this.context = context;
|
|
12
|
+
}
|
|
13
|
+
list() {
|
|
14
|
+
const flows = this.#loginFlows();
|
|
15
|
+
return this.#withLease((snapshot) => requireAdapters(snapshot).describe().map((row) => Object.freeze({
|
|
16
|
+
...loginFacts(flows, row, adapterTypeName(snapshot.tree.get(row.owner)?.packageName) ?? row.name, requireAdapters(snapshot).loginDeclaration(row.id)),
|
|
17
|
+
id: String(row.id),
|
|
18
|
+
endpoint: Object.freeze({ id: String(row.id), adapter: requireAdapters(snapshot).clientAdapter(row.id) }),
|
|
19
|
+
name: row.name,
|
|
20
|
+
adapter: adapterTypeName(snapshot.tree.get(row.owner)?.packageName) ?? row.name,
|
|
21
|
+
owner: row.owner,
|
|
22
|
+
connected: row.connected,
|
|
23
|
+
admitted: row.admitted,
|
|
24
|
+
transportState: row.transportState,
|
|
25
|
+
eventDiagnostics: row.eventDiagnostics,
|
|
26
|
+
status: row.status,
|
|
27
|
+
phase: row.phase,
|
|
28
|
+
operations: row.operations,
|
|
29
|
+
managementCapabilities: row.managementCapabilities,
|
|
30
|
+
})), Object.freeze([]));
|
|
31
|
+
}
|
|
32
|
+
capabilities(input) {
|
|
33
|
+
return this.#withLease((snapshot) => {
|
|
34
|
+
const index = requireAdapters(snapshot);
|
|
35
|
+
const id = index.resolve(input.adapter, input.endpointKey);
|
|
36
|
+
return id ? index.capabilities(id) : undefined;
|
|
37
|
+
}, undefined);
|
|
38
|
+
}
|
|
39
|
+
get(adapter, endpointKey) {
|
|
40
|
+
const flows = this.#loginFlows();
|
|
41
|
+
return this.#withLease((snapshot) => {
|
|
42
|
+
const index = requireAdapters(snapshot);
|
|
43
|
+
const id = index.resolve(adapter, endpointKey);
|
|
44
|
+
if (!id)
|
|
45
|
+
return null;
|
|
46
|
+
const row = index.describe().find((item) => item.id === id);
|
|
47
|
+
if (!row)
|
|
48
|
+
return null;
|
|
49
|
+
return Object.freeze({
|
|
50
|
+
...loginFacts(flows, row, adapterTypeName(snapshot.tree.get(row.owner)?.packageName) ?? row.name, index.loginDeclaration(row.id)),
|
|
51
|
+
name: row.name,
|
|
52
|
+
endpoint: Object.freeze({ id: String(row.id), adapter: index.clientAdapter(row.id) }),
|
|
53
|
+
adapter: adapterTypeName(snapshot.tree.get(row.owner)?.packageName) ?? row.name,
|
|
54
|
+
connected: row.connected,
|
|
55
|
+
admitted: row.admitted,
|
|
56
|
+
transportState: row.transportState,
|
|
57
|
+
eventDiagnostics: row.eventDiagnostics,
|
|
58
|
+
status: row.status,
|
|
59
|
+
phase: row.phase,
|
|
60
|
+
operations: row.operations,
|
|
61
|
+
managementCapabilities: row.managementCapabilities,
|
|
62
|
+
});
|
|
63
|
+
}, null);
|
|
64
|
+
}
|
|
65
|
+
/** Retain the actual ingress Client instead of resolving the latest account when work settles. */
|
|
66
|
+
captureOperation(input) {
|
|
67
|
+
const target = normalizeDeliveryTarget(input.target);
|
|
68
|
+
const snapshot = this.context.retain();
|
|
69
|
+
let child;
|
|
70
|
+
try {
|
|
71
|
+
const index = requireAdapters(snapshot.value);
|
|
72
|
+
const id = target.endpoint.id;
|
|
73
|
+
if (index.clientAdapter(id) !== target.endpoint.adapter) {
|
|
74
|
+
throw new EndpointDeliveryError('endpoint_identity_mismatch', 'Source belongs to another adapter.', 'not_sent');
|
|
75
|
+
}
|
|
76
|
+
const callerSignal = input.signal
|
|
77
|
+
? AbortSignal.any([snapshot.stopSignal, input.signal]) : snapshot.stopSignal;
|
|
78
|
+
child = index.acquireInbound(id, input.client, callerSignal);
|
|
79
|
+
const captured = child;
|
|
80
|
+
let released = false;
|
|
81
|
+
let calls = 0;
|
|
82
|
+
const releaseResources = () => {
|
|
83
|
+
try {
|
|
84
|
+
captured.release();
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
if (snapshot.active)
|
|
88
|
+
this.context.release(snapshot);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const assertActive = () => {
|
|
92
|
+
if (released || !snapshot.active)
|
|
93
|
+
throw new EndpointDeliveryError('operation_scope_ended', 'Retained operation has ended.', 'not_sent');
|
|
94
|
+
captured.assertActive();
|
|
95
|
+
};
|
|
96
|
+
// The capture stops admitting on release; accepted calls retain its source and Root to drain.
|
|
97
|
+
const retainCall = () => {
|
|
98
|
+
calls += 1;
|
|
99
|
+
return () => { if (--calls === 0 && released)
|
|
100
|
+
releaseResources(); };
|
|
101
|
+
};
|
|
102
|
+
const scope = { assertActive, signal: captured.signal, retain: retainCall };
|
|
103
|
+
const requester = input.requester ?? index.owner(id);
|
|
104
|
+
const capabilities = index.capabilities(id);
|
|
105
|
+
const declared = ['recall', 'edit', 'reaction', 'typing'].find(operation => capabilities.operations?.[operation]);
|
|
106
|
+
const port = declared ? index.control(id, declared, scope) : undefined;
|
|
107
|
+
const controls = port && Object.freeze({
|
|
108
|
+
...(port.recall ? { recall: async (request) => {
|
|
109
|
+
assertSameEndpoint(target, request.message.conversation);
|
|
110
|
+
return port.recall(request);
|
|
111
|
+
} } : {}),
|
|
112
|
+
...(port.edit ? { edit: async (request) => {
|
|
113
|
+
assertSameEndpoint(target, request.message.conversation);
|
|
114
|
+
return port.edit(request);
|
|
115
|
+
} } : {}),
|
|
116
|
+
...(port.addReaction ? { addReaction: async (request) => {
|
|
117
|
+
assertSameEndpoint(target, request.message.conversation);
|
|
118
|
+
return port.addReaction(request);
|
|
119
|
+
} } : {}),
|
|
120
|
+
...(port.removeReaction ? { removeReaction: async (request) => {
|
|
121
|
+
assertSameEndpoint(target, request.message.conversation);
|
|
122
|
+
return port.removeReaction(request);
|
|
123
|
+
} } : {}),
|
|
124
|
+
...(port.typing ? { typing: async (request) => {
|
|
125
|
+
assertSameEndpoint(target, request.conversation);
|
|
126
|
+
return port.typing(request);
|
|
127
|
+
} } : {}),
|
|
128
|
+
});
|
|
129
|
+
return Object.freeze({
|
|
130
|
+
target, signal: captured.signal, control: controls,
|
|
131
|
+
management: index.management(id, scope),
|
|
132
|
+
profile: index.profile(id, scope),
|
|
133
|
+
send: async (content, options) => {
|
|
134
|
+
assertActive();
|
|
135
|
+
// Retain the source authority, while each send owns its caller cancellation.
|
|
136
|
+
const releaseCall = retainCall();
|
|
137
|
+
try {
|
|
138
|
+
const sendLease = index.acquire(id, options?.signal
|
|
139
|
+
? AbortSignal.any([captured.signal, options.signal]) : captured.signal, captured.endpoint);
|
|
140
|
+
try {
|
|
141
|
+
return await this.context.send({ conversation: target, requester, content,
|
|
142
|
+
...(options?.locale ? { locale: options.locale } : {}), [operationEndpoint]: sendLease }, snapshot.value);
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
sendLease.release();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
releaseCall();
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
openReplyStream: (options = {}) => {
|
|
153
|
+
assertActive();
|
|
154
|
+
if (!captured.streaming || !supportsIncrementalOutbound(snapshot.value, target.endpoint.adapter))
|
|
155
|
+
return undefined;
|
|
156
|
+
if (captured.streaming.supports?.({ conversation: target, source: options.source }) === false)
|
|
157
|
+
return undefined;
|
|
158
|
+
const releaseCall = retainCall();
|
|
159
|
+
let streamLease;
|
|
160
|
+
try {
|
|
161
|
+
streamLease = index.acquire(id, captured.signal, captured.endpoint);
|
|
162
|
+
const retained = streamLease;
|
|
163
|
+
return createCoreReplyStream({ endpoint: retained, streaming: retained.streaming, options,
|
|
164
|
+
deliver: (content, execution) => {
|
|
165
|
+
// Opening admitted this whole stream; capture.release only closes new admission.
|
|
166
|
+
retained.assertActive();
|
|
167
|
+
return this.context.send({ conversation: target, requester, content,
|
|
168
|
+
...(options.locale ? { locale: options.locale } : {}), [operationEndpoint]: retained }, snapshot.value, execution);
|
|
169
|
+
},
|
|
170
|
+
release: () => { try {
|
|
171
|
+
retained.release();
|
|
172
|
+
}
|
|
173
|
+
finally {
|
|
174
|
+
releaseCall();
|
|
175
|
+
} }, });
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
streamLease?.release();
|
|
179
|
+
releaseCall();
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
release: () => {
|
|
184
|
+
if (released)
|
|
185
|
+
return;
|
|
186
|
+
released = true;
|
|
187
|
+
if (!calls)
|
|
188
|
+
releaseResources();
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
try {
|
|
194
|
+
child?.release();
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
this.context.release(snapshot);
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async send(input) {
|
|
203
|
+
const lease = this.context.acquire();
|
|
204
|
+
try {
|
|
205
|
+
const index = requireAdapters(lease.value);
|
|
206
|
+
const resolved = index.resolve(input.adapter, input.endpointKey);
|
|
207
|
+
if (!resolved)
|
|
208
|
+
throw new Error('endpoint not found');
|
|
209
|
+
const requester = index.owner(resolved);
|
|
210
|
+
const conversation = {
|
|
211
|
+
...input.conversation,
|
|
212
|
+
endpoint: { id: String(resolved), adapter: index.clientAdapter(resolved) },
|
|
213
|
+
};
|
|
214
|
+
const result = await this.context.send({
|
|
215
|
+
conversation,
|
|
216
|
+
requester,
|
|
217
|
+
locale: input.locale,
|
|
218
|
+
content: normalizeConsoleContent(input.content),
|
|
219
|
+
}, lease.value);
|
|
220
|
+
return requireSentReceipt(result);
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
this.context.release(lease);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/** Resolves a persisted identity exactly in the current operation generation. */
|
|
227
|
+
async sendTarget(target, content, requester, options) {
|
|
228
|
+
const conversation = normalizeDeliveryTarget(target);
|
|
229
|
+
const lease = this.context.acquire();
|
|
230
|
+
try {
|
|
231
|
+
const index = requireAdapters(lease.value);
|
|
232
|
+
const id = conversation.endpoint.id;
|
|
233
|
+
let owner;
|
|
234
|
+
let adapter;
|
|
235
|
+
try {
|
|
236
|
+
owner = index.owner(id);
|
|
237
|
+
adapter = index.clientAdapter(id);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
throw new EndpointDeliveryError('endpoint_not_found', 'The saved delivery endpoint no longer exists.', 'not_sent');
|
|
241
|
+
}
|
|
242
|
+
if (adapter !== conversation.endpoint.adapter) {
|
|
243
|
+
throw new EndpointDeliveryError('endpoint_identity_mismatch', 'The saved delivery endpoint belongs to a different adapter.', 'not_sent');
|
|
244
|
+
}
|
|
245
|
+
const result = await this.context.send({
|
|
246
|
+
conversation,
|
|
247
|
+
requester: requester ?? owner,
|
|
248
|
+
locale: options?.locale,
|
|
249
|
+
// The unified renderer validates JSX, components and native Segments.
|
|
250
|
+
content: content,
|
|
251
|
+
}, lease.value);
|
|
252
|
+
return requireSentReceipt(result);
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
this.context.release(lease);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async addReaction(input) {
|
|
259
|
+
return this.#withControl({ target: input.message.conversation, method: 'addReaction',
|
|
260
|
+
run: control => control.addReaction({ message: input.message, emoji: input.emoji, hint: {
|
|
261
|
+
sceneType: input.sceneType, channelId: input.channelId,
|
|
262
|
+
} }) });
|
|
263
|
+
}
|
|
264
|
+
async removeReaction(input) {
|
|
265
|
+
await this.#withControl({ target: input.message.conversation, method: 'removeReaction',
|
|
266
|
+
run: control => control.removeReaction({ message: input.message, reactionId: input.reactionId }) });
|
|
267
|
+
}
|
|
268
|
+
async recall(input) {
|
|
269
|
+
await this.#withControl({ target: input.message.conversation, method: 'recall',
|
|
270
|
+
run: control => control.recall({ message: input.message }) });
|
|
271
|
+
}
|
|
272
|
+
async edit(input) {
|
|
273
|
+
return this.#withControl({ target: input.message.conversation, method: 'edit',
|
|
274
|
+
run: control => control.edit({ message: input.message, content: input.content }) });
|
|
275
|
+
}
|
|
276
|
+
async typing(input) {
|
|
277
|
+
await this.#withControl({ target: input.conversation, method: 'typing',
|
|
278
|
+
run: control => control.typing({ conversation: input.conversation, active: input.active }) });
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* ADR 0058: run with the Endpoint's login declaration and lease-scoped login control port.
|
|
282
|
+
* `null` when the Endpoint is unknown or the snapshot is gone.
|
|
283
|
+
*/
|
|
284
|
+
async withLogin(adapter, endpointKey, run) {
|
|
285
|
+
let lease;
|
|
286
|
+
try {
|
|
287
|
+
lease = this.context.acquire();
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
const index = requireAdapters(lease.value);
|
|
294
|
+
const id = index.resolve(adapter, endpointKey);
|
|
295
|
+
if (!id)
|
|
296
|
+
return null;
|
|
297
|
+
const declaration = index.loginDeclaration(id);
|
|
298
|
+
const control = index.loginControl(id, { assertActive: () => {
|
|
299
|
+
if (!lease.active)
|
|
300
|
+
throw new EndpointDeliveryError('operation_scope_ended', 'Login operation scope has ended', 'not_sent');
|
|
301
|
+
} });
|
|
302
|
+
return await run(Object.freeze({ ...(declaration ? { declaration } : {}), ...(control ? { control } : {}) }));
|
|
303
|
+
}
|
|
304
|
+
finally {
|
|
305
|
+
this.context.release(lease);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
#loginFlows() {
|
|
309
|
+
try {
|
|
310
|
+
return this.context.loginFlows?.() ?? [];
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
return [];
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
async withManagement(adapter, endpointKey, run) {
|
|
317
|
+
let lease;
|
|
318
|
+
try {
|
|
319
|
+
lease = this.context.acquire();
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
const index = requireAdapters(lease.value);
|
|
326
|
+
const id = index.resolve(adapter, endpointKey);
|
|
327
|
+
const endpoint = id ? index.connection(String(id), String(id)) : undefined;
|
|
328
|
+
if (!endpoint)
|
|
329
|
+
return null;
|
|
330
|
+
return await run(index.management(id, { assertActive: () => {
|
|
331
|
+
if (!lease.active)
|
|
332
|
+
throw new EndpointDeliveryError('operation_scope_ended', 'Management operation scope has ended', 'not_sent');
|
|
333
|
+
} }) ?? Object.freeze({}));
|
|
334
|
+
}
|
|
335
|
+
finally {
|
|
336
|
+
this.context.release(lease);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async #withControl({ target: source, method, run }) {
|
|
340
|
+
const target = normalizeDeliveryTarget(source);
|
|
341
|
+
const lease = this.context.acquire();
|
|
342
|
+
try {
|
|
343
|
+
const index = requireAdapters(lease.value);
|
|
344
|
+
const id = target.endpoint.id;
|
|
345
|
+
let actualAdapter;
|
|
346
|
+
try {
|
|
347
|
+
actualAdapter = index.clientAdapter(id);
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
throw new EndpointDeliveryError('endpoint_not_found', 'The referenced account no longer exists.', 'not_sent');
|
|
351
|
+
}
|
|
352
|
+
if (actualAdapter !== target.endpoint.adapter) {
|
|
353
|
+
throw new EndpointDeliveryError('endpoint_identity_mismatch', 'Message reference belongs to another adapter.', 'not_sent');
|
|
354
|
+
}
|
|
355
|
+
const operation = method === 'addReaction' || method === 'removeReaction' ? 'reaction' : method;
|
|
356
|
+
const control = index.control(id, operation, { assertActive: () => {
|
|
357
|
+
if (!lease.active)
|
|
358
|
+
throw new EndpointDeliveryError('operation_scope_ended', 'Control operation has ended.', 'not_sent');
|
|
359
|
+
} });
|
|
360
|
+
if (!control || typeof control[method] !== 'function') {
|
|
361
|
+
throw new EndpointDeliveryError('unsupported_operation', `Endpoint does not support ${method}.`, 'not_sent');
|
|
362
|
+
}
|
|
363
|
+
return await run(control);
|
|
364
|
+
}
|
|
365
|
+
finally {
|
|
366
|
+
this.context.release(lease);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
#withLease(operation, fallback) {
|
|
370
|
+
let lease;
|
|
371
|
+
try {
|
|
372
|
+
lease = this.context.acquire();
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
return fallback;
|
|
376
|
+
}
|
|
377
|
+
try {
|
|
378
|
+
return operation(lease.value);
|
|
379
|
+
}
|
|
380
|
+
catch {
|
|
381
|
+
return fallback;
|
|
382
|
+
}
|
|
383
|
+
finally {
|
|
384
|
+
this.context.release(lease);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
export function requireAdapters(snapshot) {
|
|
389
|
+
const projection = snapshot.projections.get(adapterFeatureId);
|
|
390
|
+
if (!isAdapterIndex(projection)) {
|
|
391
|
+
throw new Error('Adapter Feature projection is not installed');
|
|
392
|
+
}
|
|
393
|
+
return projection;
|
|
394
|
+
}
|
|
395
|
+
export function adapterTypeName(packageName) {
|
|
396
|
+
if (!packageName)
|
|
397
|
+
return undefined;
|
|
398
|
+
return packageName.replace(/^@[^/]+\/adapter-/, '');
|
|
399
|
+
}
|
|
400
|
+
function normalizeConsoleContent(content) {
|
|
401
|
+
if (typeof content === 'string')
|
|
402
|
+
return content;
|
|
403
|
+
if (Array.isArray(content))
|
|
404
|
+
return content;
|
|
405
|
+
return String(content);
|
|
406
|
+
}
|
|
407
|
+
function requireSentReceipt(result) {
|
|
408
|
+
if (result.status !== 'sent') {
|
|
409
|
+
throw new EndpointDeliveryError(result.failure?.code ?? `outbound_${result.status}`, result.failure?.message ?? 'Outbound delivery was suppressed.', result.failure?.deliveryUnknown ? 'unknown'
|
|
410
|
+
: result.status === 'rejected' ? 'rejected' : 'not_sent', { receipt: result, ...(result.parts ? { parts: result.parts.map(part => ({
|
|
411
|
+
...(part.message ? { messageId: part.message.id } : {}), ...(part.sourceIndices ? { sourceIndices: part.sourceIndices } : {}),
|
|
412
|
+
})) } : {}) });
|
|
413
|
+
}
|
|
414
|
+
return { messageId: result.message?.id ?? '', receipt: result };
|
|
415
|
+
}
|
|
416
|
+
/** A canonical message/address cannot be redirected by a second endpoint selector. */
|
|
417
|
+
function assertSameEndpoint(expected, actual) {
|
|
418
|
+
if (expected.endpoint.id !== actual.endpoint.id || expected.endpoint.adapter !== actual.endpoint.adapter) {
|
|
419
|
+
throw new EndpointDeliveryError('endpoint_identity_mismatch', 'Message reference belongs to another Endpoint.', 'not_sent');
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
/** ADR 0058: pending-login facts of one Endpoint row from the latest login flows. */
|
|
423
|
+
function loginFacts(flows, row, adapter, declaration) {
|
|
424
|
+
const flow = flows.find((item) => item.endpointIdentity?.canonicalId === String(row.id))
|
|
425
|
+
?? flows.find((item) => !item.endpointIdentity && item.adapter === adapter
|
|
426
|
+
&& (item.endpointKey === row.name || item.endpointKey === String(row.id)));
|
|
427
|
+
const unfinished = !!flow && (flow.state === 'active' || flow.state === 'expired');
|
|
428
|
+
const index = unfinished && flow.currentStep ? flow.steps.findIndex((step) => step.id === flow.currentStep) : -1;
|
|
429
|
+
return {
|
|
430
|
+
pendingLogin: unfinished && !!flow.activeTaskId,
|
|
431
|
+
...(index >= 0 ? { loginStep: Object.freeze({ index: index + 1, total: flow.steps.length, title: flow.steps[index].title }) } : {}),
|
|
432
|
+
...(declaration ? { login: declaration } : {}),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ADR 0050 §6 / 0055 §5 dispatch gate: a Plugin switched off for one adapter / endpoint /
|
|
3
|
+
* conversation does not exist there. Indexes are projected once per generation; when an endpoint disables some
|
|
4
|
+
* owners, an equivalent index over the remaining slots is derived and cached per
|
|
5
|
+
* (projection, disabled owner set), so the hot path without overrides stays unchanged.
|
|
6
|
+
*/
|
|
7
|
+
import { CommandIndex } from '@zhin.js/command';
|
|
8
|
+
import { HandlerIndex } from '@zhin.js/handler';
|
|
9
|
+
import { MiddlewareIndex } from '@zhin.js/middleware';
|
|
10
|
+
import { type PluginId, type RuntimeSnapshot } from '@zhin.js/plugin-runtime';
|
|
11
|
+
/**
|
|
12
|
+
* Resolution key of a message, side event or endpoint event: `<alias>/<endpointId>/<kind>:<id>`
|
|
13
|
+
* with a conversation, `<alias>/<endpointId>` without one; undefined when unknown.
|
|
14
|
+
*/
|
|
15
|
+
export declare function featureKeyOf(value: unknown, snapshot: RuntimeSnapshot): string | undefined;
|
|
16
|
+
/** Plugin owners switched off for this input's endpoint in `snapshot`. */
|
|
17
|
+
export declare function disabledOwnersOf(value: unknown, snapshot: RuntimeSnapshot): ReadonlySet<PluginId>;
|
|
18
|
+
/** Command index without the commands of owners disabled for `input`'s endpoint (also hides them from menus). */
|
|
19
|
+
export declare function gatedCommandIndex(input: unknown, snapshot: RuntimeSnapshot): CommandIndex | undefined;
|
|
20
|
+
export declare function gatedMiddlewareIndex(input: unknown, snapshot: RuntimeSnapshot): MiddlewareIndex | undefined;
|
|
21
|
+
export declare function gatedHandlerIndex(input: unknown, snapshot: RuntimeSnapshot): HandlerIndex | undefined;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ADR 0050 §6 / 0055 §5 dispatch gate: a Plugin switched off for one adapter / endpoint /
|
|
3
|
+
* conversation does not exist there. Indexes are projected once per generation; when an endpoint disables some
|
|
4
|
+
* owners, an equivalent index over the remaining slots is derived and cached per
|
|
5
|
+
* (projection, disabled owner set), so the hot path without overrides stays unchanged.
|
|
6
|
+
*/
|
|
7
|
+
import { CommandIndex, commandFeatureId, isCommandIndex } from '@zhin.js/command';
|
|
8
|
+
import { HandlerIndex, handlerFeatureId, isHandlerIndex } from '@zhin.js/handler';
|
|
9
|
+
import { MiddlewareIndex, middlewareFeatureId, isMiddlewareIndex } from '@zhin.js/middleware';
|
|
10
|
+
import { disabledFeatureOwners, resolveEndpointFeatureKey, } from '@zhin.js/plugin-runtime';
|
|
11
|
+
function record(value) {
|
|
12
|
+
return !!value && typeof value === 'object';
|
|
13
|
+
}
|
|
14
|
+
function endpointOf(value) {
|
|
15
|
+
if (!record(value))
|
|
16
|
+
return undefined;
|
|
17
|
+
const conversation = record(value.conversation) ? value.conversation : undefined;
|
|
18
|
+
const fromConversation = conversation && record(conversation.endpoint) ? conversation.endpoint.id : undefined;
|
|
19
|
+
const fromEvent = record(value.endpoint) ? value.endpoint.id : undefined;
|
|
20
|
+
const id = typeof fromConversation === 'string' ? fromConversation : typeof fromEvent === 'string' ? fromEvent : undefined;
|
|
21
|
+
if (id !== undefined) {
|
|
22
|
+
return { id, ...(typeof value.endpointId === 'string' ? { endpointId: value.endpointId } : {}),
|
|
23
|
+
// ADR 0055: inputs with a conversation resolve down to the conversation level.
|
|
24
|
+
...(typeof fromConversation === 'string' && conversation ? { conversation: { kind: conversation.kind, id: conversation.id } } : {}) };
|
|
25
|
+
}
|
|
26
|
+
return 'payload' in value ? endpointOf(value.payload) : undefined;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolution key of a message, side event or endpoint event: `<alias>/<endpointId>/<kind>:<id>`
|
|
30
|
+
* with a conversation, `<alias>/<endpointId>` without one; undefined when unknown.
|
|
31
|
+
*/
|
|
32
|
+
export function featureKeyOf(value, snapshot) {
|
|
33
|
+
const endpoint = endpointOf(value);
|
|
34
|
+
return endpoint ? resolveEndpointFeatureKey(snapshot, endpoint.id, endpoint.endpointId, endpoint.conversation) : undefined;
|
|
35
|
+
}
|
|
36
|
+
/** Plugin owners switched off for this input's endpoint in `snapshot`. */
|
|
37
|
+
export function disabledOwnersOf(value, snapshot) {
|
|
38
|
+
const key = featureKeyOf(value, snapshot);
|
|
39
|
+
return key ? disabledFeatureOwners(snapshot, snapshot.tree.keys(), key) : new Set();
|
|
40
|
+
}
|
|
41
|
+
const derived = new WeakMap();
|
|
42
|
+
function gated(index, snapshot, feature, disabled,
|
|
43
|
+
// Slot definitions were validated by the provider; the derived index reuses them as-is.
|
|
44
|
+
build) {
|
|
45
|
+
if (disabled.size === 0)
|
|
46
|
+
return index;
|
|
47
|
+
const slots = [...snapshot.capabilities.values()].filter(slot => slot.feature === feature);
|
|
48
|
+
if (!slots.some(slot => disabled.has(slot.owner)))
|
|
49
|
+
return index;
|
|
50
|
+
const key = [...disabled].sort().join('\0');
|
|
51
|
+
let byKey = derived.get(index);
|
|
52
|
+
if (!byKey) {
|
|
53
|
+
byKey = new Map();
|
|
54
|
+
derived.set(index, byKey);
|
|
55
|
+
}
|
|
56
|
+
const cached = byKey.get(key);
|
|
57
|
+
if (cached)
|
|
58
|
+
return cached;
|
|
59
|
+
const value = build(slots.filter(slot => !disabled.has(slot.owner)));
|
|
60
|
+
byKey.set(key, value);
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
/** Command index without the commands of owners disabled for `input`'s endpoint (also hides them from menus). */
|
|
64
|
+
export function gatedCommandIndex(input, snapshot) {
|
|
65
|
+
const index = snapshot.projections.get(commandFeatureId);
|
|
66
|
+
if (!isCommandIndex(index))
|
|
67
|
+
return undefined;
|
|
68
|
+
return gated(index, snapshot, commandFeatureId, disabledOwnersOf(input, snapshot), slots => {
|
|
69
|
+
// Same menu contract as the Command provider's projection.
|
|
70
|
+
const rootConfig = snapshot.config.get(snapshot.root);
|
|
71
|
+
const rawKeyword = rootConfig?.menuKeyword;
|
|
72
|
+
const menu = rawKeyword === false || rawKeyword === '' ? undefined
|
|
73
|
+
: typeof rawKeyword === 'string' ? { keyword: rawKeyword } : {};
|
|
74
|
+
return new CommandIndex(slots, snapshot, menu);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
export function gatedMiddlewareIndex(input, snapshot) {
|
|
78
|
+
const index = snapshot.projections.get(middlewareFeatureId);
|
|
79
|
+
if (!isMiddlewareIndex(index))
|
|
80
|
+
return undefined;
|
|
81
|
+
return gated(index, snapshot, middlewareFeatureId, disabledOwnersOf(input, snapshot), slots => new MiddlewareIndex(slots, snapshot));
|
|
82
|
+
}
|
|
83
|
+
export function gatedHandlerIndex(input, snapshot) {
|
|
84
|
+
const index = snapshot.projections.get(handlerFeatureId);
|
|
85
|
+
if (!isHandlerIndex(index))
|
|
86
|
+
return undefined;
|
|
87
|
+
return gated(index, snapshot, handlerFeatureId, disabledOwnersOf(input, snapshot), slots => new HandlerIndex(slots, snapshot));
|
|
88
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { renderTemplatePaths } from '@zhin.js/kernel';
|
|
2
|
+
import { componentHostToken, } from '@zhin.js/plugin-runtime';
|
|
3
|
+
function createComponentHost() {
|
|
4
|
+
return {
|
|
5
|
+
compileTemplate(text, context) {
|
|
6
|
+
return renderTemplatePaths(text, { ...context });
|
|
7
|
+
},
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export function installComponentHost() {
|
|
11
|
+
return ({ resources }) => {
|
|
12
|
+
resources.provide(componentHostToken, createComponentHost());
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type StructuredDatabase } from '@zhin.js/database';
|
|
2
|
+
/** Run before installing the durable event reader into a candidate generation. */
|
|
3
|
+
export declare function migrateConversationColumns(database: StructuredDatabase): Promise<void>;
|
|
4
|
+
/** Older observations without canonical addresses remain explicitly unknown. */
|
|
5
|
+
export declare function migrateInboxColumns(database: StructuredDatabase): Promise<void>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { INBOX_MESSAGE_QUERY_COLUMNS, INBOX_TABLE_MESSAGE, encodeStorageIdentity } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import { ensureDatabaseColumns, ensureDatabaseIndex } from '@zhin.js/database';
|
|
3
|
+
import { CONVERSATION_EVENT_QUERY_MODEL, conversationEventQueryFields } from '@zhin.js/im-contract';
|
|
4
|
+
/** Run before installing the durable event reader into a candidate generation. */
|
|
5
|
+
export async function migrateConversationColumns(database) {
|
|
6
|
+
if (!['sqlite', 'mysql', 'pg'].includes(database.dialectName))
|
|
7
|
+
return;
|
|
8
|
+
await ensureDatabaseColumns(database, 'conversation_events', CONVERSATION_EVENT_QUERY_MODEL);
|
|
9
|
+
const model = database.models.get('conversation_events');
|
|
10
|
+
if (!model)
|
|
11
|
+
throw new Error('Missing conversation_events model');
|
|
12
|
+
let afterId;
|
|
13
|
+
while (true) {
|
|
14
|
+
const rows = await model.select('id', 'event_json').where({ $and: [
|
|
15
|
+
...(afterId === undefined ? [] : [{ id: { $gt: afterId } }]),
|
|
16
|
+
{ $or: [{ query_version: null }, { message_revision: null }] },
|
|
17
|
+
] }).orderBy('id').limit(200);
|
|
18
|
+
if (!rows.length)
|
|
19
|
+
break;
|
|
20
|
+
for (const row of rows) {
|
|
21
|
+
const event = JSON.parse(String(row.event_json));
|
|
22
|
+
const fields = conversationEventQueryFields(event);
|
|
23
|
+
await model.update(fields).where({ id: row.id });
|
|
24
|
+
}
|
|
25
|
+
// Seek past the completed batch instead of re-scanning already backfilled rows.
|
|
26
|
+
afterId = rows[rows.length - 1].id;
|
|
27
|
+
}
|
|
28
|
+
// Derived indexes can be rebuilt from event_json; rollback ignores the additive columns/indexes.
|
|
29
|
+
await ensureDatabaseIndex(database, 'conversation_events', 'conversation_events_message_sequence', [
|
|
30
|
+
{ name: 'message_key', text: true }, { name: 'event_type', text: true }, { name: 'id' },
|
|
31
|
+
]);
|
|
32
|
+
await ensureDatabaseIndex(database, 'conversation_events', 'conversation_events_message_revision', [
|
|
33
|
+
{ name: 'message_key', text: true }, { name: 'event_type', text: true }, { name: 'message_revision' }, { name: 'id' },
|
|
34
|
+
]);
|
|
35
|
+
await ensureDatabaseIndex(database, 'conversation_events', 'conversation_events_conversation_time', [{ name: 'conversation_key', text: true }, { name: 'time' }]);
|
|
36
|
+
await ensureDatabaseIndex(database, 'conversation_events', 'conversation_events_type_time', [{ name: 'event_type', text: true }, { name: 'time' }]);
|
|
37
|
+
await ensureDatabaseIndex(database, 'conversation_events', 'conversation_events_scene', [
|
|
38
|
+
{ name: 'endpoint_adapter', text: true }, { name: 'endpoint_id', text: true },
|
|
39
|
+
{ name: 'conversation_kind', text: true }, { name: 'conversation_id', text: true },
|
|
40
|
+
{ name: 'id' },
|
|
41
|
+
]);
|
|
42
|
+
// Operator scope directory selects only canonical address columns. SQLite/PostgreSQL
|
|
43
|
+
// can cover the full tuple; MySQL TEXT prefixes cannot cover full identities and
|
|
44
|
+
// seven 128-character utf8mb4 prefixes exceed its key budget, so keep an endpoint seek.
|
|
45
|
+
await ensureDatabaseIndex(database, 'conversation_events', 'conversation_events_scope_directory', (database.dialectName === 'mysql' ? ['endpoint_id'] : [
|
|
46
|
+
'endpoint_id', 'endpoint_adapter', 'conversation_kind', 'conversation_id', 'parent_kind', 'parent_id', 'thread_id',
|
|
47
|
+
]).map(name => ({ name, text: true })));
|
|
48
|
+
}
|
|
49
|
+
/** Older observations without canonical addresses remain explicitly unknown. */
|
|
50
|
+
export async function migrateInboxColumns(database) {
|
|
51
|
+
if (!['sqlite', 'mysql', 'pg'].includes(database.dialectName))
|
|
52
|
+
return;
|
|
53
|
+
await ensureDatabaseColumns(database, INBOX_TABLE_MESSAGE, INBOX_MESSAGE_QUERY_COLUMNS);
|
|
54
|
+
const model = database.models.get(INBOX_TABLE_MESSAGE);
|
|
55
|
+
while (true) {
|
|
56
|
+
const rows = await model.select('id', 'raw').where({ query_version: null }).orderBy('id').limit(200);
|
|
57
|
+
if (!rows.length)
|
|
58
|
+
break;
|
|
59
|
+
for (const row of rows) {
|
|
60
|
+
let observation;
|
|
61
|
+
try {
|
|
62
|
+
observation = JSON.parse(String(row.raw));
|
|
63
|
+
}
|
|
64
|
+
catch { /* Legacy raw is optional, not a canonical event. */ }
|
|
65
|
+
const current = observation?.$console === 'zhin.message-observation/1' ? observation : undefined;
|
|
66
|
+
const encoded = (value) => typeof value === 'string' ? encodeStorageIdentity(value) : null;
|
|
67
|
+
await model.update({ query_version: 1,
|
|
68
|
+
direction: ['inbound', 'outbound'].includes(current?.direction) ? current.direction : null,
|
|
69
|
+
endpoint_capability_id: encoded(current?.conversation?.endpoint?.id),
|
|
70
|
+
endpoint_adapter_id: encoded(current?.conversation?.endpoint?.adapter),
|
|
71
|
+
thread_id: encoded(current?.conversation?.threadId), }).where({ id: row.id, query_version: null });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
await ensureDatabaseIndex(database, INBOX_TABLE_MESSAGE, 'inbox_message_direction_time', [{ name: 'direction', text: true }, { name: 'created_at' }]);
|
|
75
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type DatabaseHostConfig = {
|
|
2
|
+
readonly dialect: string;
|
|
3
|
+
} & Record<string, unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* Validate and normalize the expanded Root database configuration before a
|
|
6
|
+
* third-party driver sees it. Dotenv expansion produces strings, while the
|
|
7
|
+
* network clients require numeric ports and otherwise surface opaque NaN
|
|
8
|
+
* socket errors.
|
|
9
|
+
*/
|
|
10
|
+
export declare function normalizeDatabaseHostConfig(input: DatabaseHostConfig): DatabaseHostConfig;
|