@zhin.js/core 1.5.15 → 1.6.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 +13 -11
- package/lib/built/login-assist.js +32 -26
- 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 +19 -48
- package/lib/index.js +14 -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 +89 -0
- package/lib/plugin-runtime/im/endpoint-runtime.js +381 -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 +114 -0
- package/lib/plugin-runtime/im/host/database-host-installer.d.ts +29 -0
- package/lib/plugin-runtime/im/host/database-host-installer.js +442 -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 +234 -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
|
@@ -1,21 +1,54 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readOperationInputResources } from '@zhin.js/feature-kit';
|
|
2
|
+
import { adapterFeatureId, isAdapterIndex } from '@zhin.js/adapter';
|
|
2
3
|
import { formatCompact, getLogger, truncatePreview } from '@zhin.js/logger';
|
|
4
|
+
import { commandMenuReply, renderCommandFeedback } from './command-feedback.js';
|
|
5
|
+
import { gatedCommandIndex } from './feature-gate.js';
|
|
3
6
|
const logger = getLogger('command');
|
|
4
|
-
function
|
|
5
|
-
return
|
|
7
|
+
function dispatchOutcome(result, replyOwner = result.owner) {
|
|
8
|
+
return { dispatch: result, ...(result.value !== undefined && replyOwner
|
|
9
|
+
? { reply: { owner: replyOwner, content: result.value } } : {}) };
|
|
10
|
+
}
|
|
11
|
+
function ownerOfMessage(message, snapshot) {
|
|
12
|
+
// adapter is a platform literal (e.g. terminal), not the Plugin owner.
|
|
13
|
+
const slot = snapshot.capabilities.get(message.conversation.endpoint.id);
|
|
14
|
+
if (slot)
|
|
15
|
+
return slot.owner;
|
|
16
|
+
// Expanded endpoint records have identities distinct from their source slot.
|
|
17
|
+
// Resolve their ownership through the generation's canonical AdapterIndex.
|
|
18
|
+
const adapters = snapshot.projections.get(adapterFeatureId);
|
|
19
|
+
if (!isAdapterIndex(adapters))
|
|
20
|
+
return undefined;
|
|
21
|
+
try {
|
|
22
|
+
return adapters.owner(message.conversation.endpoint.id);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
6
27
|
}
|
|
7
28
|
/**
|
|
8
29
|
* 默认解析:读消息所属适配器实例 config 的 `commandPrefix`(默认 `''`);
|
|
9
30
|
* 实例声明 `endpoints` 数组时,按消息 endpoint 名找 entry,`entry.commandPrefix` 覆盖顶层。
|
|
10
31
|
*/
|
|
11
32
|
export const defaultCommandPrefixResolver = (message, snapshot) => {
|
|
12
|
-
const
|
|
33
|
+
const owner = ownerOfMessage(message, snapshot);
|
|
34
|
+
const config = (owner ? snapshot.config.get(owner) : undefined);
|
|
13
35
|
if (!config)
|
|
14
36
|
return '';
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
37
|
+
const adapters = snapshot.projections.get(adapterFeatureId);
|
|
38
|
+
const endpointCapabilityId = message.conversation.endpoint.id;
|
|
39
|
+
const desiredEndpointId = isAdapterIndex(adapters)
|
|
40
|
+
? adapters.endpointEntry(endpointCapabilityId)?.desired.endpointId
|
|
41
|
+
: undefined;
|
|
42
|
+
const candidateIds = new Set([message.endpointId, desiredEndpointId].filter((v) => typeof v === 'string' && v.length > 0));
|
|
43
|
+
if (candidateIds.size > 0 && Array.isArray(config.endpoints)) {
|
|
44
|
+
const entry = config.endpoints.find((item) => {
|
|
45
|
+
if (!item || typeof item !== 'object')
|
|
46
|
+
return false;
|
|
47
|
+
const id = item.id;
|
|
48
|
+
const name = item.name;
|
|
49
|
+
return (typeof id === 'string' && candidateIds.has(id))
|
|
50
|
+
|| (typeof name === 'string' && candidateIds.has(name));
|
|
51
|
+
});
|
|
19
52
|
if (typeof entry?.commandPrefix === 'string')
|
|
20
53
|
return entry.commandPrefix;
|
|
21
54
|
}
|
|
@@ -26,24 +59,50 @@ export class MessageDispatcher {
|
|
|
26
59
|
constructor(resolvePrefix = defaultCommandPrefixResolver) {
|
|
27
60
|
this.resolvePrefix = resolvePrefix;
|
|
28
61
|
}
|
|
29
|
-
|
|
62
|
+
commandEnvironment(message, snapshot) {
|
|
30
63
|
const prefix = this.resolvePrefix(message, snapshot);
|
|
31
|
-
|
|
64
|
+
const commands = gatedCommandIndex(message, snapshot);
|
|
65
|
+
return Object.freeze({ prefix, menu: commands ? commands.formatMenu(prefix, message.locale) : undefined });
|
|
66
|
+
}
|
|
67
|
+
async dispatch(message, snapshot, interactionFactory, observe) {
|
|
68
|
+
return (await this.dispatchWithReply(message, snapshot, interactionFactory, observe)).dispatch;
|
|
69
|
+
}
|
|
70
|
+
/** @internal The inbound runtime retains reply authorship without changing command facts. */
|
|
71
|
+
async dispatchWithReply(message, snapshot, interactionFactory, observe) {
|
|
72
|
+
const note = (stage, status) => {
|
|
73
|
+
try {
|
|
74
|
+
observe?.(stage, status);
|
|
75
|
+
}
|
|
76
|
+
catch { /* Observation never controls dispatch. */ }
|
|
77
|
+
};
|
|
78
|
+
const prefix = message.commandPrefix ?? this.resolvePrefix(message, snapshot);
|
|
79
|
+
let input = normalizeCommandInputText(message);
|
|
32
80
|
if (prefix && !input.startsWith(prefix)) {
|
|
33
|
-
|
|
81
|
+
note('prefix', 'mismatch');
|
|
82
|
+
return dispatchOutcome(Object.freeze({ matched: false }));
|
|
34
83
|
}
|
|
84
|
+
note('prefix', 'matched');
|
|
35
85
|
if (prefix)
|
|
36
86
|
input = input.slice(prefix.length).trim();
|
|
37
|
-
if (!input)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
87
|
+
if (!input) {
|
|
88
|
+
note('dispatch', 'empty_input');
|
|
89
|
+
return dispatchOutcome(Object.freeze({ matched: false }));
|
|
90
|
+
}
|
|
91
|
+
// ADR 0050: commands of a Plugin disabled for this endpoint do not exist here (no "disabled" reply).
|
|
92
|
+
const commands = gatedCommandIndex(message, snapshot);
|
|
93
|
+
if (!commands) {
|
|
94
|
+
note('dispatch', 'commands_unavailable');
|
|
95
|
+
return dispatchOutcome(Object.freeze({ matched: false }));
|
|
96
|
+
}
|
|
42
97
|
const structuredInput = message.segments
|
|
43
98
|
? stripCommandPrefix(message.segments, prefix)
|
|
44
99
|
: undefined;
|
|
45
100
|
const matchInput = structuredInput ?? input;
|
|
46
|
-
|
|
101
|
+
note('dispatch', 'started');
|
|
102
|
+
const result = await commands.dispatch({ input: matchInput, source: message, interactionFactory: interactionFactory, commandPrefix: prefix, snapshot: snapshot, operation: readOperationInputResources(message, snapshot) });
|
|
103
|
+
const feedback = await renderCommandFeedback(result, message, snapshot);
|
|
104
|
+
if (feedback)
|
|
105
|
+
return dispatchOutcome(Object.freeze({ ...result, value: feedback.content }), feedback.owner);
|
|
47
106
|
if (result.matched && result.value !== undefined) {
|
|
48
107
|
if (!result.owner)
|
|
49
108
|
throw new Error('Matched Command is missing its owner');
|
|
@@ -52,7 +111,6 @@ export class MessageDispatcher {
|
|
|
52
111
|
command: result.command,
|
|
53
112
|
owner: result.owner,
|
|
54
113
|
}));
|
|
55
|
-
await message.$replyFrom(result.owner, result.value);
|
|
56
114
|
}
|
|
57
115
|
else if (!result.matched) {
|
|
58
116
|
logger.debug(formatCompact({
|
|
@@ -60,22 +118,75 @@ export class MessageDispatcher {
|
|
|
60
118
|
preview: truncatePreview(input),
|
|
61
119
|
}));
|
|
62
120
|
}
|
|
63
|
-
return result
|
|
121
|
+
return dispatchOutcome(result.menu ? Object.freeze({ ...result,
|
|
122
|
+
value: await commandMenuReply(result, { commands: commands, snapshot: snapshot, conversation: message.conversation, prefix: prefix }),
|
|
123
|
+
}) : result);
|
|
64
124
|
}
|
|
65
125
|
}
|
|
66
126
|
/**
|
|
67
127
|
* Keep the text and structured views aligned. Falling back to `content` is
|
|
68
128
|
* intentional when an adapter supplies inconsistent segment data.
|
|
129
|
+
*
|
|
130
|
+
* Platforms put a quote (`reply`) segment before the text ("quote a message, then type the
|
|
131
|
+
* command"). Leading quotes are moved after the command text so the command name still
|
|
132
|
+
* matches and a `reply` parameter can still read the quote; `message.replyTo` is untouched.
|
|
69
133
|
*/
|
|
134
|
+
function hasTextAfterLeadingMentions(segments) {
|
|
135
|
+
let seenMention = false;
|
|
136
|
+
for (const segment of segments) {
|
|
137
|
+
if (segment.type === 'reply')
|
|
138
|
+
continue;
|
|
139
|
+
if (segment.type === 'mention' || segment.type === 'at') {
|
|
140
|
+
seenMention = true;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
return seenMention && segment.type === 'text' && typeof segment.data.text === 'string' && segment.data.text.trim().length > 0;
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
function normalizeCommandInputText(message) {
|
|
148
|
+
const raw = message.content.trim();
|
|
149
|
+
const segments = message.segments;
|
|
150
|
+
if (segments && hasTextAfterLeadingMentions(segments)) {
|
|
151
|
+
let atStart = true;
|
|
152
|
+
const textParts = [];
|
|
153
|
+
for (const segment of segments) {
|
|
154
|
+
if (atStart && (segment.type === 'reply' || segment.type === 'mention' || segment.type === 'at')) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
atStart = false;
|
|
158
|
+
if (segment.type === 'text' && typeof segment.data?.text === 'string') {
|
|
159
|
+
textParts.push(segment.data.text);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const candidate = textParts.join('').trim();
|
|
163
|
+
if (candidate)
|
|
164
|
+
return candidate;
|
|
165
|
+
}
|
|
166
|
+
if (message.conversation?.kind !== 'private' && /^(?:@\S+\s+)+/u.test(raw)) {
|
|
167
|
+
return raw.replace(/^(?:@\S+\s+)+/u, '').trim();
|
|
168
|
+
}
|
|
169
|
+
return raw;
|
|
170
|
+
}
|
|
70
171
|
function stripCommandPrefix(segments, prefix) {
|
|
71
172
|
let pendingPrefix = prefix;
|
|
72
173
|
let atStart = true;
|
|
174
|
+
let trimLeading = true;
|
|
175
|
+
const skipLeadingMentions = hasTextAfterLeadingMentions(segments);
|
|
73
176
|
const result = [];
|
|
177
|
+
const leadingQuotes = [];
|
|
74
178
|
for (const segment of segments) {
|
|
75
179
|
if (!atStart) {
|
|
76
180
|
result.push(segment);
|
|
77
181
|
continue;
|
|
78
182
|
}
|
|
183
|
+
if (segment.type === 'reply') {
|
|
184
|
+
leadingQuotes.push(segment);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (skipLeadingMentions && (segment.type === 'mention' || segment.type === 'at')) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
79
190
|
if (segment.type !== 'text' || typeof segment.data.text !== 'string') {
|
|
80
191
|
if (pendingPrefix)
|
|
81
192
|
return undefined;
|
|
@@ -84,6 +195,13 @@ function stripCommandPrefix(segments, prefix) {
|
|
|
84
195
|
continue;
|
|
85
196
|
}
|
|
86
197
|
let text = segment.data.text;
|
|
198
|
+
// Match the leading whitespace normalization of message.content without
|
|
199
|
+
// discarding structured arguments when the prefix follows whitespace.
|
|
200
|
+
if (trimLeading) {
|
|
201
|
+
text = text.trimStart();
|
|
202
|
+
if (text)
|
|
203
|
+
trimLeading = false;
|
|
204
|
+
}
|
|
87
205
|
if (pendingPrefix) {
|
|
88
206
|
if (text.startsWith(pendingPrefix)) {
|
|
89
207
|
text = text.slice(pendingPrefix.length);
|
|
@@ -103,5 +221,5 @@ function stripCommandPrefix(segments, prefix) {
|
|
|
103
221
|
atStart = false;
|
|
104
222
|
result.push({ ...segment, data: { ...segment.data, text } });
|
|
105
223
|
}
|
|
106
|
-
return pendingPrefix ? undefined : result;
|
|
224
|
+
return pendingPrefix ? undefined : [...result, ...leadingQuotes];
|
|
107
225
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type PluginId } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import type { ConversationRef, DeliveryReceipt } from '@zhin.js/im-contract';
|
|
3
|
+
import type { Message, MessageSenderRef, OutboundEnvelope, SendContent } from './contracts.js';
|
|
4
|
+
export interface EndpointEvent {
|
|
5
|
+
readonly adapter: string;
|
|
6
|
+
readonly endpointKey: string;
|
|
7
|
+
readonly error?: Error;
|
|
8
|
+
}
|
|
9
|
+
export interface MessageBusEventMap {
|
|
10
|
+
'message.receive': [message: Message];
|
|
11
|
+
'before.sendMessage': [envelope: OutboundEnvelope];
|
|
12
|
+
'message.send': [payload: {
|
|
13
|
+
adapter: string;
|
|
14
|
+
endpointKey: string;
|
|
15
|
+
content: SendContent;
|
|
16
|
+
receipt: DeliveryReceipt;
|
|
17
|
+
}];
|
|
18
|
+
'endpoint.connect': [event: EndpointEvent];
|
|
19
|
+
'endpoint.disconnect': [event: EndpointEvent];
|
|
20
|
+
'endpoint.error': [event: EndpointEvent];
|
|
21
|
+
}
|
|
22
|
+
export type MessageBusEventName = keyof MessageBusEventMap;
|
|
23
|
+
export declare class MessageBus {
|
|
24
|
+
private readonly emitter;
|
|
25
|
+
constructor();
|
|
26
|
+
on<E extends MessageBusEventName>(event: E, listener: (...args: MessageBusEventMap[E]) => void): () => void;
|
|
27
|
+
once<E extends MessageBusEventName>(event: E, listener: (...args: MessageBusEventMap[E]) => void): () => void;
|
|
28
|
+
emit<E extends MessageBusEventName>(event: E, ...args: MessageBusEventMap[E]): void;
|
|
29
|
+
off<E extends MessageBusEventName>(event: E, listener: (...args: MessageBusEventMap[E]) => void): void;
|
|
30
|
+
listenerCount(event: MessageBusEventName): number;
|
|
31
|
+
removeAllListeners(event?: MessageBusEventName): void;
|
|
32
|
+
}
|
|
33
|
+
export declare const messageBusToken: import("@zhin.js/plugin-runtime").Token<MessageBus>;
|
|
34
|
+
export type OutboundFallbackReason = 'renderer_unavailable' | 'renderer_failed' | 'media_unsupported' | 'html_text_policy' | 'markdown_text_policy' | 'interactive_text_policy';
|
|
35
|
+
/** Final payload handed to the Endpoint, not a claim about the remote client's display. */
|
|
36
|
+
export interface RuntimeMessageRendering {
|
|
37
|
+
readonly forms: readonly ('text' | 'image' | 'native')[];
|
|
38
|
+
readonly fallbacks: readonly OutboundFallbackReason[];
|
|
39
|
+
}
|
|
40
|
+
/** Bounded message projection for runtime observers such as Console and Inbox. */
|
|
41
|
+
export interface RuntimeMessageEvent {
|
|
42
|
+
readonly direction: 'inbound' | 'outbound';
|
|
43
|
+
readonly conversation: ConversationRef;
|
|
44
|
+
readonly sender?: MessageSenderRef;
|
|
45
|
+
readonly requester?: PluginId;
|
|
46
|
+
readonly contentPreview: string;
|
|
47
|
+
readonly messageId?: string;
|
|
48
|
+
readonly timestamp: number;
|
|
49
|
+
readonly rendering?: RuntimeMessageRendering;
|
|
50
|
+
}
|
|
51
|
+
export interface RuntimeMessageEventSource {
|
|
52
|
+
subscribe(listener: (event: RuntimeMessageEvent) => void): () => void;
|
|
53
|
+
}
|
|
54
|
+
/** Owns one ImRuntime's observer subscriptions without exposing publication authority. */
|
|
55
|
+
export declare class RuntimeMessageEventStream implements RuntimeMessageEventSource {
|
|
56
|
+
#private;
|
|
57
|
+
subscribe(listener: (event: RuntimeMessageEvent) => void): () => void;
|
|
58
|
+
publish(event: RuntimeMessageEvent): void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* #996: a social/scene fact just landed in `conversation_events` (notice or join request).
|
|
62
|
+
* Observers (the Agent's scene-event wake) decide priority themselves; no raw platform payload.
|
|
63
|
+
*/
|
|
64
|
+
export interface RuntimeSceneEventSignal {
|
|
65
|
+
readonly sequence: number;
|
|
66
|
+
readonly event: import('@zhin.js/im-contract').ConversationEvent;
|
|
67
|
+
/** Live endpoint key of the receiving bot account. */
|
|
68
|
+
readonly endpointId?: string;
|
|
69
|
+
/** Bot account candidates (endpoint key, platform self id) for "is the target the bot". */
|
|
70
|
+
readonly selfIds: readonly string[];
|
|
71
|
+
}
|
|
72
|
+
export interface RuntimeSceneEventSource {
|
|
73
|
+
subscribe(listener: (signal: RuntimeSceneEventSignal) => void): () => void;
|
|
74
|
+
}
|
|
75
|
+
export declare class RuntimeSceneEventStream implements RuntimeSceneEventSource {
|
|
76
|
+
#private;
|
|
77
|
+
subscribe(listener: (signal: RuntimeSceneEventSignal) => void): () => void;
|
|
78
|
+
publish(signal: RuntimeSceneEventSignal): void;
|
|
79
|
+
}
|
|
80
|
+
/** Log-safe structured conversation summary. */
|
|
81
|
+
export declare function formatConversationLog(conversation: ConversationRef): string;
|
|
82
|
+
/** Content projection bounded for observer and log surfaces. */
|
|
83
|
+
export declare function previewMessageContent(content: unknown): string;
|
|
84
|
+
/** Flattens any SendContent-like value to display text; non-text segments become `[type]`. */
|
|
85
|
+
export declare function flattenContent(content: unknown): string;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { createToken } from '@zhin.js/plugin-runtime';
|
|
3
|
+
export class MessageBus {
|
|
4
|
+
emitter = new EventEmitter();
|
|
5
|
+
constructor() {
|
|
6
|
+
this.emitter.setMaxListeners(50);
|
|
7
|
+
}
|
|
8
|
+
on(event, listener) {
|
|
9
|
+
this.emitter.on(event, listener);
|
|
10
|
+
return () => this.emitter.off(event, listener);
|
|
11
|
+
}
|
|
12
|
+
once(event, listener) {
|
|
13
|
+
this.emitter.once(event, listener);
|
|
14
|
+
return () => this.emitter.off(event, listener);
|
|
15
|
+
}
|
|
16
|
+
emit(event, ...args) {
|
|
17
|
+
this.emitter.emit(event, ...args);
|
|
18
|
+
}
|
|
19
|
+
off(event, listener) {
|
|
20
|
+
this.emitter.off(event, listener);
|
|
21
|
+
}
|
|
22
|
+
listenerCount(event) {
|
|
23
|
+
return this.emitter.listenerCount(event);
|
|
24
|
+
}
|
|
25
|
+
removeAllListeners(event) {
|
|
26
|
+
if (event) {
|
|
27
|
+
this.emitter.removeAllListeners(event);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.emitter.removeAllListeners();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export const messageBusToken = createToken('zhin.im.message-bus');
|
|
35
|
+
const messagePreviewLimit = 200;
|
|
36
|
+
/** Owns one ImRuntime's observer subscriptions without exposing publication authority. */
|
|
37
|
+
export class RuntimeMessageEventStream {
|
|
38
|
+
#listeners = new Set();
|
|
39
|
+
subscribe(listener) {
|
|
40
|
+
this.#listeners.add(listener);
|
|
41
|
+
return () => { this.#listeners.delete(listener); };
|
|
42
|
+
}
|
|
43
|
+
publish(event) {
|
|
44
|
+
for (const listener of this.#listeners) {
|
|
45
|
+
try {
|
|
46
|
+
listener(event);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Observers never participate in message delivery success or failure.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export class RuntimeSceneEventStream {
|
|
55
|
+
#listeners = new Set();
|
|
56
|
+
subscribe(listener) {
|
|
57
|
+
this.#listeners.add(listener);
|
|
58
|
+
return () => { this.#listeners.delete(listener); };
|
|
59
|
+
}
|
|
60
|
+
publish(signal) {
|
|
61
|
+
for (const listener of this.#listeners) {
|
|
62
|
+
try {
|
|
63
|
+
listener(signal);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// Observers never participate in ingress success or failure.
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Log-safe structured conversation summary. */
|
|
72
|
+
export function formatConversationLog(conversation) {
|
|
73
|
+
const base = `${conversation.kind}:${conversation.id}`;
|
|
74
|
+
const parent = conversation.parent
|
|
75
|
+
? `@${conversation.parent.kind}:${conversation.parent.id}`
|
|
76
|
+
: '';
|
|
77
|
+
const thread = conversation.threadId ? `#${conversation.threadId}` : '';
|
|
78
|
+
return `${base}${parent}${thread}`;
|
|
79
|
+
}
|
|
80
|
+
/** Content projection bounded for observer and log surfaces. */
|
|
81
|
+
export function previewMessageContent(content) {
|
|
82
|
+
const text = flattenContent(content);
|
|
83
|
+
return text.length > messagePreviewLimit
|
|
84
|
+
? `${text.slice(0, messagePreviewLimit)}…`
|
|
85
|
+
: text;
|
|
86
|
+
}
|
|
87
|
+
/** Flattens any SendContent-like value to display text; non-text segments become `[type]`. */
|
|
88
|
+
export function flattenContent(content) {
|
|
89
|
+
if (typeof content === 'string')
|
|
90
|
+
return content;
|
|
91
|
+
if (content == null)
|
|
92
|
+
return '';
|
|
93
|
+
if (Array.isArray(content))
|
|
94
|
+
return content.map((item) => flattenContent(item)).join('');
|
|
95
|
+
if (typeof content === 'object') {
|
|
96
|
+
const record = content;
|
|
97
|
+
const data = record.data;
|
|
98
|
+
if (typeof record.type === 'string') {
|
|
99
|
+
if (data && typeof data.text === 'string')
|
|
100
|
+
return data.text;
|
|
101
|
+
return `[${record.type}]`;
|
|
102
|
+
}
|
|
103
|
+
if (typeof record.text === 'string')
|
|
104
|
+
return record.text;
|
|
105
|
+
try {
|
|
106
|
+
return JSON.stringify(content) ?? '';
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return String(content);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return String(content);
|
|
113
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type RuntimeSnapshot } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import { type ConversationRef, type DeliveryReceipt, type DeliveryPart, type EndpointSendResult, type Segment } from '@zhin.js/im-contract';
|
|
3
|
+
import { type SendRequest } from './contracts.js';
|
|
4
|
+
import { OutboundRenderer } from './outbound-renderer.js';
|
|
5
|
+
import { type RuntimeMessageEvent } from './message-events.js';
|
|
6
|
+
import { type JsxButtonBatch, type PluginIncarnationRef, type RememberFallbackContext } from './interactive-router.js';
|
|
7
|
+
import type { RuntimeMessageDiagnostics } from './message-diagnostics.js';
|
|
8
|
+
interface OutboundDeliveryContext {
|
|
9
|
+
readonly diagnostics?: RuntimeMessageDiagnostics;
|
|
10
|
+
prepareButtons?(request: SendRequest, snapshot: RuntimeSnapshot): JsxButtonBatch;
|
|
11
|
+
record(request: SendRequest, receipt: DeliveryReceipt, canonicalContent?: readonly Segment[] | 'unavailable'): Promise<number | undefined | void>;
|
|
12
|
+
recordUpdate?(request: SendRequest, receipt: DeliveryReceipt, revision: number, canonicalContent?: readonly Segment[] | 'unavailable'): Promise<number | undefined | void>;
|
|
13
|
+
/** Side-channel correlation ledger for inbound origin; failures never affect the receipt. */
|
|
14
|
+
ledger?(conversation: ConversationRef, receipt: DeliveryReceipt, payload: unknown): void;
|
|
15
|
+
rememberFallback(conversation: ConversationRef, generation: number, map: Record<string, string>, context?: number | RememberFallbackContext | PluginIncarnationRef): Record<string, string> | void;
|
|
16
|
+
publish(event: RuntimeMessageEvent): void;
|
|
17
|
+
}
|
|
18
|
+
/** Internal transport continuation. Rendering, policy and middleware still own every submitted revision. */
|
|
19
|
+
export interface OutboundDeliveryExecution {
|
|
20
|
+
send(request: import('@zhin.js/adapter').EndpointSendRequest): Promise<{
|
|
21
|
+
readonly result?: EndpointSendResult;
|
|
22
|
+
readonly confirmedContent?: readonly Segment[];
|
|
23
|
+
readonly contentUnavailable?: true;
|
|
24
|
+
}>;
|
|
25
|
+
history(): 'create' | 'skip' | Readonly<{
|
|
26
|
+
revision: number;
|
|
27
|
+
}>;
|
|
28
|
+
confirmed(part: DeliveryPart, content: readonly Segment[] | undefined): void;
|
|
29
|
+
}
|
|
30
|
+
/** Owns rendering, policy projection, middleware, Endpoint delivery, and delivery facts. */
|
|
31
|
+
export declare class OutboundDeliveryRuntime {
|
|
32
|
+
#private;
|
|
33
|
+
private readonly context;
|
|
34
|
+
constructor(context: OutboundDeliveryContext, renderer?: OutboundRenderer);
|
|
35
|
+
deliver(request: SendRequest, snapshot: RuntimeSnapshot, execution?: OutboundDeliveryExecution): Promise<DeliveryReceipt>;
|
|
36
|
+
}
|
|
37
|
+
export declare function failedDeliveryReceipt(code: string, retryable?: boolean): DeliveryReceipt;
|
|
38
|
+
export declare function receiptFromEndpointError(error: unknown, attempted?: boolean, conversation?: ConversationRef, payloadLength?: number): DeliveryReceipt;
|
|
39
|
+
export {};
|