@zhin.js/core 1.5.16 → 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
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { projectLocale, runRuntimeMiddleware } from './inbound-spine.js';
|
|
3
|
+
import { resolveLocale, } from '@zhin.js/plugin-runtime';
|
|
4
|
+
import { EndpointDeliveryError } from '@zhin.js/im-contract';
|
|
5
|
+
import { isCanonicalSegment } from '../../built/segment-contract/assert.js';
|
|
6
|
+
import { collectSegmentMedia } from '../../built/segment-contract/media.js';
|
|
7
|
+
import { isStrictCanonicalSegment } from '../../built/segment-contract/validate.js';
|
|
8
|
+
import { formatCompact, getLogger } from '@zhin.js/logger';
|
|
9
|
+
import { isJsxElement } from '@zhin.js/jsx';
|
|
10
|
+
import { createOutboundEnvelope, isComponentCall, isRawContent, isUntrustedContent, operationEndpoint } from './contracts.js';
|
|
11
|
+
import { OutboundRenderer, projectOutboundPayload, resolveOutboundPresentation } from './outbound-renderer.js';
|
|
12
|
+
import { flattenOutboundArray } from '../../built/outbound-content-arrays.js';
|
|
13
|
+
import { requireAdapters } from './endpoint-runtime.js';
|
|
14
|
+
import { formatConversationLog, previewMessageContent } from './message-events.js';
|
|
15
|
+
import { collectButtonPayloads } from './interactive-router.js';
|
|
16
|
+
const logger = getLogger('im.outbound');
|
|
17
|
+
/** A payload that cannot be rendered is not sent; say why, or the failure is invisible to the operator. */
|
|
18
|
+
function logRejectedPayload(conversation, stage, error) {
|
|
19
|
+
logger.warn(formatCompact({
|
|
20
|
+
op: 'outbound_payload_rejected',
|
|
21
|
+
stage,
|
|
22
|
+
conv: formatConversationLog(conversation),
|
|
23
|
+
error: error instanceof Error ? error.message : String(error),
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
/** Owns rendering, policy projection, middleware, Endpoint delivery, and delivery facts. */
|
|
27
|
+
export class OutboundDeliveryRuntime {
|
|
28
|
+
context;
|
|
29
|
+
#renderer;
|
|
30
|
+
constructor(context, renderer = new OutboundRenderer()) {
|
|
31
|
+
this.context = context;
|
|
32
|
+
this.#renderer = renderer;
|
|
33
|
+
}
|
|
34
|
+
async deliver(request, snapshot, execution) {
|
|
35
|
+
let rendering;
|
|
36
|
+
const receipt = await this.#deliver(request, snapshot, value => { rendering = value; }, execution);
|
|
37
|
+
this.context.diagnostics?.delivery(snapshot.generation, request.conversation, receipt, rendering);
|
|
38
|
+
return receipt;
|
|
39
|
+
}
|
|
40
|
+
async #deliver(request, snapshot, observeRendering, execution) {
|
|
41
|
+
const adapter = request.conversation.endpoint.id;
|
|
42
|
+
const boundEndpoint = request[operationEndpoint];
|
|
43
|
+
if (boundEndpoint && boundEndpoint.canonicalId !== adapter)
|
|
44
|
+
throw new Error('Outbound Endpoint binding does not match its conversation');
|
|
45
|
+
const presentation = resolveOutboundPresentation(snapshot, request.conversation, request.requester);
|
|
46
|
+
const jsxCache = new WeakMap();
|
|
47
|
+
const buttons = this.context.prepareButtons?.(request, snapshot);
|
|
48
|
+
const renderOptions = { jsxCache, bindButton: buttons?.bind, signal: boundEndpoint?.signal };
|
|
49
|
+
let initialPayload;
|
|
50
|
+
let initialProjection;
|
|
51
|
+
try {
|
|
52
|
+
const rendered = await this.#renderer.render(request.content, {
|
|
53
|
+
requester: request.requester,
|
|
54
|
+
snapshot: snapshot,
|
|
55
|
+
conversation: request.conversation,
|
|
56
|
+
incoming: request.incoming,
|
|
57
|
+
...renderOptions,
|
|
58
|
+
});
|
|
59
|
+
initialProjection = await projectOutboundPayload(rendered, request.conversation, snapshot, { deferInteractive: true, presentation, signal: renderOptions.signal });
|
|
60
|
+
initialPayload = initialProjection.payload;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
// Preparation precedes the transport boundary; cancellation here is confirmed not sent.
|
|
64
|
+
if (renderOptions.signal?.aborted)
|
|
65
|
+
return failedDeliveryReceipt('endpoint_inactive');
|
|
66
|
+
if (error instanceof EndpointDeliveryError && error.code === 'unsupported_operation' && error.disposition === 'not_sent')
|
|
67
|
+
return receiptFromEndpointError(error, false);
|
|
68
|
+
logRejectedPayload(request.conversation, 'render', error);
|
|
69
|
+
return rejectedDeliveryReceipt('outbound_payload_rejected');
|
|
70
|
+
}
|
|
71
|
+
let adapters;
|
|
72
|
+
try {
|
|
73
|
+
adapters = requireAdapters(snapshot);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return receiptFromEndpointError(error, false);
|
|
77
|
+
}
|
|
78
|
+
const envelope = createOutboundEnvelope({
|
|
79
|
+
conversation: request.conversation,
|
|
80
|
+
requester: request.requester,
|
|
81
|
+
generation: snapshot.generation,
|
|
82
|
+
clientAdapter: adapters.clientAdapter(adapter),
|
|
83
|
+
}, initialPayload, () => {
|
|
84
|
+
if (!boundEndpoint)
|
|
85
|
+
return adapters.clientById(adapter);
|
|
86
|
+
boundEndpoint.assertActive();
|
|
87
|
+
return boundEndpoint.endpoint;
|
|
88
|
+
});
|
|
89
|
+
// Record identities and fallback text before middleware can mutate the array
|
|
90
|
+
// or its segments. Do not copy/serialize media buffers or streams.
|
|
91
|
+
const initialSegmentSet = new Set(Array.isArray(initialPayload) ? initialPayload : []);
|
|
92
|
+
const initialItems = Array.isArray(initialPayload) ? initialPayload.map(item => ({
|
|
93
|
+
item, type: item?.type, text: item?.type === 'text' ? item.data?.text : undefined,
|
|
94
|
+
})) : undefined;
|
|
95
|
+
const retainsInitialProjection = (value) => (value === initialPayload && !initialItems)
|
|
96
|
+
|| (!!initialItems && initialItems.length > 0 && Array.isArray(value)
|
|
97
|
+
&& value.length === initialItems.length
|
|
98
|
+
&& initialItems.every(({ item, type, text }, index) => value[index] === item
|
|
99
|
+
&& item?.type === type && (type !== 'text' || item.data?.text === text)));
|
|
100
|
+
const retainsInitialSegments = (value) => retainsInitialProjection(value)
|
|
101
|
+
|| (!!initialItems && initialItems.length > 0 && Array.isArray(value)
|
|
102
|
+
&& initialItems.every(({ item, type, text }) => value.includes(item)
|
|
103
|
+
&& item?.type === type && (type !== 'text' || item.data?.text === text)));
|
|
104
|
+
const fallbackIncarnation = {
|
|
105
|
+
requester: request.requester,
|
|
106
|
+
snapshot,
|
|
107
|
+
node: snapshot.tree.get(request.requester),
|
|
108
|
+
...(boundEndpoint?.signal ? { signal: boundEndpoint.signal } : {}),
|
|
109
|
+
};
|
|
110
|
+
let terminalEntered = false;
|
|
111
|
+
let receipt;
|
|
112
|
+
try {
|
|
113
|
+
await runRuntimeMiddleware(snapshot, envelope, async () => {
|
|
114
|
+
terminalEntered = true;
|
|
115
|
+
let payload;
|
|
116
|
+
let projection;
|
|
117
|
+
const buttonPayloads = new Set();
|
|
118
|
+
try {
|
|
119
|
+
// Native payload replacements and in-place array mutations remain possible; only authoring values need evaluation.
|
|
120
|
+
const replacement = envelope.payload;
|
|
121
|
+
const renderItemOptions = {
|
|
122
|
+
requester: request.requester,
|
|
123
|
+
snapshot: snapshot,
|
|
124
|
+
conversation: request.conversation,
|
|
125
|
+
incoming: request.incoming,
|
|
126
|
+
...renderOptions,
|
|
127
|
+
};
|
|
128
|
+
let replaced = replacement;
|
|
129
|
+
if (needsAuthoringRender(replacement)) {
|
|
130
|
+
if (Array.isArray(replacement) && initialSegmentSet.size > 0 && replacement.some(item => initialSegmentSet.has(item))) {
|
|
131
|
+
const renderedItems = [];
|
|
132
|
+
for (const item of flattenOutboundArray(replacement)) {
|
|
133
|
+
if (initialSegmentSet.has(item) || !needsAuthoringRender(item)) {
|
|
134
|
+
renderedItems.push(item);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
renderedItems.push(await this.#renderer.render(item, renderItemOptions));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
replaced = flattenOutboundArray(renderedItems);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
replaced = await this.#renderer.render(replacement, renderItemOptions);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
projection = await projectOutboundPayload(replaced, request.conversation, snapshot, {
|
|
147
|
+
presentation, signal: renderOptions.signal,
|
|
148
|
+
inheritedFallbacks: retainsInitialSegments(replaced) ? initialProjection.rendering.fallbacks : [],
|
|
149
|
+
rememberFallback: (map) => {
|
|
150
|
+
for (const value of Object.values(map))
|
|
151
|
+
buttonPayloads.add(value);
|
|
152
|
+
return this.context.rememberFallback(request.conversation, snapshot.generation, map, {
|
|
153
|
+
incarnation: fallbackIncarnation,
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
payload = projection.payload;
|
|
158
|
+
observeRendering(projection.rendering);
|
|
159
|
+
for (const value of collectButtonPayloads(payload))
|
|
160
|
+
buttonPayloads.add(value);
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
if (renderOptions.signal?.aborted) {
|
|
164
|
+
receipt = failedDeliveryReceipt('endpoint_inactive');
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const unsupported = error instanceof EndpointDeliveryError && error.code === 'unsupported_operation' && error.disposition === 'not_sent';
|
|
168
|
+
if (!unsupported)
|
|
169
|
+
logRejectedPayload(request.conversation, 'middleware_render', error);
|
|
170
|
+
receipt = unsupported ? receiptFromEndpointError(error, false) : rejectedDeliveryReceipt('outbound_payload_rejected');
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (isEmptyOutboundPayload(payload)) {
|
|
174
|
+
receipt = suppressedDeliveryReceipt();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const revokeButtons = buttons?.activate(buttonPayloads);
|
|
178
|
+
const canonicalContent = typeof payload === 'string'
|
|
179
|
+
? [{ type: 'text', data: { text: payload } }]
|
|
180
|
+
: Array.isArray(payload) && payload.every(isCanonicalSegment) ? payload : undefined;
|
|
181
|
+
// Capture known canonical content before the endpoint can mutate it.
|
|
182
|
+
// Inline rendering bytes and opaque extensions never become new history.
|
|
183
|
+
let contentSources = canonicalContent?.map(snapshotHistorySource);
|
|
184
|
+
let contentUnavailable = false;
|
|
185
|
+
const payloadLength = Array.isArray(payload) ? payload.length : 1;
|
|
186
|
+
try {
|
|
187
|
+
const sendRequest = {
|
|
188
|
+
conversation: request.conversation,
|
|
189
|
+
locale: resolveLocale(request.locale ?? projectLocale(snapshot)),
|
|
190
|
+
payload,
|
|
191
|
+
};
|
|
192
|
+
if (execution) {
|
|
193
|
+
const transported = await execution.send(sendRequest);
|
|
194
|
+
if (transported.confirmedContent) {
|
|
195
|
+
contentSources = transported.confirmedContent.map(snapshotHistorySource);
|
|
196
|
+
// Explicit physical evidence must never fall back to the planned logical body.
|
|
197
|
+
contentUnavailable = !contentSources.length || contentSources.some(source => source === undefined);
|
|
198
|
+
}
|
|
199
|
+
if (transported.contentUnavailable) {
|
|
200
|
+
contentSources = undefined;
|
|
201
|
+
contentUnavailable = true;
|
|
202
|
+
}
|
|
203
|
+
receipt = transported.result === undefined ? suppressedDeliveryReceipt()
|
|
204
|
+
: receiptFromEndpointResult(transported.result, request.conversation, contentSources?.length ?? payloadLength);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const result = boundEndpoint ? await boundEndpoint.send(sendRequest)
|
|
208
|
+
: await requireAdapters(snapshot).send(adapter, sendRequest);
|
|
209
|
+
receipt = receiptFromEndpointResult(result, request.conversation, payloadLength);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
receipt = receiptFromEndpointError(error, true, request.conversation, payloadLength);
|
|
214
|
+
}
|
|
215
|
+
const evidence = projectPartEvidence(receipt, contentSources);
|
|
216
|
+
receipt = evidence.receipt;
|
|
217
|
+
if (receipt.status !== 'sent' && !receipt.failure?.deliveryUnknown && !receipt.parts?.length)
|
|
218
|
+
revokeButtons?.();
|
|
219
|
+
const recordedParts = [];
|
|
220
|
+
let firstSequence;
|
|
221
|
+
for (const part of evidence.parts) {
|
|
222
|
+
const partReceipt = { status: 'sent', ...(part.part.message ? { message: part.part.message } : {}),
|
|
223
|
+
...(part.part.outboundTextDigest ? { outboundTextDigest: part.part.outboundTextDigest } : {}) };
|
|
224
|
+
const history = execution?.history() ?? 'create';
|
|
225
|
+
let partSequence;
|
|
226
|
+
if (part.part.message && history !== 'skip') {
|
|
227
|
+
try {
|
|
228
|
+
const content = part.content ?? (contentUnavailable || receipt.parts ? 'unavailable' : undefined);
|
|
229
|
+
const seq = history === 'create'
|
|
230
|
+
? await this.context.record(request, partReceipt, content)
|
|
231
|
+
: await this.context.recordUpdate?.(request, partReceipt, history.revision, content);
|
|
232
|
+
if (typeof seq === 'number') {
|
|
233
|
+
partSequence = seq;
|
|
234
|
+
firstSequence ??= seq;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
logger.warn(formatCompact({ op: 'outbound_history_failed', error: error instanceof Error ? error.message : String(error) }));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const enrichedPart = partSequence !== undefined
|
|
242
|
+
? Object.freeze({ ...part.part, conversationSequence: partSequence })
|
|
243
|
+
: part.part;
|
|
244
|
+
recordedParts.push(enrichedPart);
|
|
245
|
+
execution?.confirmed(enrichedPart, part.content);
|
|
246
|
+
const enrichedPartReceipt = partSequence !== undefined
|
|
247
|
+
? { ...partReceipt, conversationSequence: partSequence }
|
|
248
|
+
: partReceipt;
|
|
249
|
+
try {
|
|
250
|
+
this.context.ledger?.(request.conversation, enrichedPartReceipt, part.content ?? (contentUnavailable || receipt.parts ? undefined : payload));
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
logger.warn(`outbound ledger write failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
254
|
+
}
|
|
255
|
+
if (part.part.message && history !== 'skip')
|
|
256
|
+
this.context.publish({
|
|
257
|
+
direction: 'outbound',
|
|
258
|
+
rendering: projection.rendering,
|
|
259
|
+
conversation: request.conversation,
|
|
260
|
+
requester: request.requester,
|
|
261
|
+
contentPreview: part.content ? previewMessageContent(part.content)
|
|
262
|
+
: contentUnavailable || receipt.parts ? '[Message content unavailable; inspect its reference.]' : previewMessageContent(payload),
|
|
263
|
+
messageId: part.part.message.id,
|
|
264
|
+
timestamp: Date.now(),
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
if (firstSequence !== undefined || (receipt.parts && recordedParts.length > 0)) {
|
|
268
|
+
receipt = Object.freeze({
|
|
269
|
+
...receipt,
|
|
270
|
+
...(receipt.parts ? { parts: Object.freeze(recordedParts) } : {}),
|
|
271
|
+
...(firstSequence !== undefined ? { conversationSequence: firstSequence } : {}),
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}, 'outbound');
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
return receipt ?? failedDeliveryReceipt('outbound_middleware_failed');
|
|
278
|
+
}
|
|
279
|
+
if (!terminalEntered) {
|
|
280
|
+
logger.debug(formatCompact({
|
|
281
|
+
op: 'replychain_runtime_send',
|
|
282
|
+
status: 'suppressed',
|
|
283
|
+
reason: 'middleware_stopped_before_terminal',
|
|
284
|
+
conv: formatConversationLog(request.conversation),
|
|
285
|
+
}));
|
|
286
|
+
return suppressedDeliveryReceipt();
|
|
287
|
+
}
|
|
288
|
+
logger.debug(formatCompact({
|
|
289
|
+
op: 'replychain_runtime_send',
|
|
290
|
+
status: receipt?.status ?? 'missing',
|
|
291
|
+
code: receipt?.failure?.code,
|
|
292
|
+
conv: formatConversationLog(request.conversation),
|
|
293
|
+
}));
|
|
294
|
+
return receipt ?? failedDeliveryReceipt('outbound_delivery_incomplete');
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function isEmptyOutboundPayload(payload) {
|
|
298
|
+
if (payload === '')
|
|
299
|
+
return true;
|
|
300
|
+
return Array.isArray(payload) && payload.every(item => item?.type === 'text' && item.data?.text === '');
|
|
301
|
+
}
|
|
302
|
+
function needsAuthoringRender(value) {
|
|
303
|
+
const isAuthoring = (item) => isJsxElement(item) || isComponentCall(item) || isRawContent(item) || isUntrustedContent(item);
|
|
304
|
+
return Array.isArray(value) ? flattenOutboundArray(value).some(isAuthoring) : isAuthoring(value);
|
|
305
|
+
}
|
|
306
|
+
export function failedDeliveryReceipt(code, retryable = false) {
|
|
307
|
+
return Object.freeze({
|
|
308
|
+
status: 'failed',
|
|
309
|
+
failure: Object.freeze({
|
|
310
|
+
code,
|
|
311
|
+
message: 'Outbound delivery failed.',
|
|
312
|
+
...(retryable ? { retryable: true } : {}),
|
|
313
|
+
}),
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
function receiptFromEndpointResult(result, conversation, payloadLength) {
|
|
317
|
+
if (typeof result !== 'string') {
|
|
318
|
+
const parts = normalizeEndpointParts(result?.parts, conversation, payloadLength);
|
|
319
|
+
if (!parts.length)
|
|
320
|
+
return unknownDeliveryReceipt('delivery_unconfirmed');
|
|
321
|
+
const message = parts.find(part => part.message)?.message;
|
|
322
|
+
if (!message)
|
|
323
|
+
return Object.freeze({ ...unknownDeliveryReceipt('delivery_unconfirmed'), parts });
|
|
324
|
+
return Object.freeze({ status: 'sent', ...(message ? { message } : {}), parts });
|
|
325
|
+
}
|
|
326
|
+
if (result.trim() === '') {
|
|
327
|
+
return unknownDeliveryReceipt('delivery_unconfirmed');
|
|
328
|
+
}
|
|
329
|
+
return Object.freeze({
|
|
330
|
+
status: 'sent',
|
|
331
|
+
message: Object.freeze({ conversation, id: result }),
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
export function receiptFromEndpointError(error, attempted = true, conversation, payloadLength) {
|
|
335
|
+
if (error instanceof EndpointDeliveryError) {
|
|
336
|
+
const parts = attempted && conversation ? normalizeEndpointParts(error.parts, conversation, payloadLength) : [];
|
|
337
|
+
// Disposition is transport evidence; an error code alone cannot override it.
|
|
338
|
+
let receipt;
|
|
339
|
+
if (error.disposition === 'unknown')
|
|
340
|
+
receipt = unknownDeliveryReceipt(error.code);
|
|
341
|
+
else if (error.disposition === 'rejected')
|
|
342
|
+
receipt = rejectedDeliveryReceipt(error.code, 'Endpoint rejected outbound delivery.');
|
|
343
|
+
else if (error.code === 'outbound_unsupported' || error.code === 'endpoint_not_found')
|
|
344
|
+
receipt = unsupportedDeliveryReceipt('outbound_unsupported');
|
|
345
|
+
else if (error.code === 'unsupported_operation')
|
|
346
|
+
receipt = unsupportedDeliveryReceipt(error.code);
|
|
347
|
+
else
|
|
348
|
+
receipt = failedDeliveryReceipt(error.code);
|
|
349
|
+
if (!parts.length)
|
|
350
|
+
return receipt;
|
|
351
|
+
const message = parts.find(part => part.message)?.message;
|
|
352
|
+
return Object.freeze({ ...receipt, ...(message ? { message } : {}), parts,
|
|
353
|
+
failure: Object.freeze({ ...receipt.failure, retryable: false }) });
|
|
354
|
+
}
|
|
355
|
+
// imhelper ProtocolError exposes stable protocol facts without a Core SDK dependency.
|
|
356
|
+
if (error && typeof error === 'object' && 'protocol' in error && 'kind' in error && 'response' in error
|
|
357
|
+
&& error.protocol === 'onebot-v11' && error.kind === 'protocol') {
|
|
358
|
+
const response = error.response;
|
|
359
|
+
if (response && typeof response === 'object' && 'status' in response && response.status === 'failed') {
|
|
360
|
+
return rejectedDeliveryReceipt('platform_rejected', 'Endpoint rejected outbound delivery.');
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// Before the endpoint send boundary no message can have entered a transport.
|
|
364
|
+
if (!attempted)
|
|
365
|
+
return failedDeliveryReceipt('endpoint_send_failed');
|
|
366
|
+
// Unknown SDK/network failures provide no evidence that replay is safe.
|
|
367
|
+
return unknownDeliveryReceipt('endpoint_send_failed');
|
|
368
|
+
}
|
|
369
|
+
/** Unknown identities stay as slots; a bad mapping cannot erase a real platform id. */
|
|
370
|
+
function normalizeEndpointParts(parts, conversation, payloadLength) {
|
|
371
|
+
if (!Array.isArray(parts))
|
|
372
|
+
return [];
|
|
373
|
+
return Object.freeze(Array.from(parts, part => {
|
|
374
|
+
const indices = Array.isArray(part?.sourceIndices) ? [...part.sourceIndices] : undefined;
|
|
375
|
+
return Object.freeze({
|
|
376
|
+
...(typeof part?.messageId === 'string' && part.messageId.trim() !== ''
|
|
377
|
+
? { message: Object.freeze({ conversation, id: part.messageId }) } : {}),
|
|
378
|
+
...(indices && payloadLength !== undefined
|
|
379
|
+
&& indices.every((index) => typeof index === 'number' && Number.isSafeInteger(index) && index >= 0 && index < payloadLength)
|
|
380
|
+
? { sourceIndices: Object.freeze(indices) } : {}),
|
|
381
|
+
});
|
|
382
|
+
}));
|
|
383
|
+
}
|
|
384
|
+
function snapshotHistorySource(segment) {
|
|
385
|
+
if (segment.type === 'markdown') {
|
|
386
|
+
const data = segment.data;
|
|
387
|
+
// The AI authoring contract has one closed Markdown shape. Core extensions remain opaque.
|
|
388
|
+
if (Object.keys(segment).length !== 2 || !Object.hasOwn(segment, 'type') || !Object.hasOwn(segment, 'data')
|
|
389
|
+
|| Object.keys(data).length !== 1 || !Object.hasOwn(data, 'content') || typeof data.content !== 'string')
|
|
390
|
+
return undefined;
|
|
391
|
+
return { type: 'markdown', data: { content: data.content } };
|
|
392
|
+
}
|
|
393
|
+
// Forward entries and opaque card/extensions have no bounded, simple content contract here.
|
|
394
|
+
if (!isStrictCanonicalSegment(segment) || segment.type === 'forward')
|
|
395
|
+
return undefined;
|
|
396
|
+
if (collectSegmentMedia([segment]).some(({ media }) => media.kind === 'base64' || media.value.startsWith('data:')))
|
|
397
|
+
return undefined;
|
|
398
|
+
const values = [...Object.values(segment.data), ...Object.values(segment.platform ?? {})];
|
|
399
|
+
if (values.some(value => ArrayBuffer.isView(value) || value instanceof ArrayBuffer))
|
|
400
|
+
return undefined;
|
|
401
|
+
try {
|
|
402
|
+
return structuredClone(segment);
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
return undefined;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
function projectPartEvidence(receipt, sources) {
|
|
409
|
+
const parts = receipt.parts ?? (receipt.status === 'sent' ? [{ message: receipt.message,
|
|
410
|
+
...(sources ? { sourceIndices: sources.map((_, index) => index) } : {}) }] : []);
|
|
411
|
+
const projected = parts.map(part => {
|
|
412
|
+
const selected = part.sourceIndices?.length && sources ? part.sourceIndices.map(index => sources[index]) : undefined;
|
|
413
|
+
const content = selected?.every((segment) => segment !== undefined) ? selected : undefined;
|
|
414
|
+
const outboundTextDigest = content?.every((segment) => segment.type === 'text')
|
|
415
|
+
? createHash('sha256').update(content.map(segment => segment.data.text).join('')).digest('hex') : undefined;
|
|
416
|
+
return { part: Object.freeze({ ...part, ...(outboundTextDigest ? { outboundTextDigest } : {}) }), ...(content ? { content } : {}) };
|
|
417
|
+
});
|
|
418
|
+
return {
|
|
419
|
+
receipt: { ...receipt, ...(receipt.parts ? { parts: Object.freeze(projected.map(part => part.part)) } : {}),
|
|
420
|
+
...(receipt.status === 'sent' && projected.length === 1 && projected[0]?.part.outboundTextDigest
|
|
421
|
+
? { outboundTextDigest: projected[0].part.outboundTextDigest } : {}) },
|
|
422
|
+
parts: projected,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
function unknownDeliveryReceipt(code) {
|
|
426
|
+
return Object.freeze({
|
|
427
|
+
status: 'failed',
|
|
428
|
+
failure: Object.freeze({
|
|
429
|
+
code,
|
|
430
|
+
message: 'Message delivery is unconfirmed; resending may duplicate delivery.',
|
|
431
|
+
retryable: false,
|
|
432
|
+
deliveryUnknown: true,
|
|
433
|
+
}),
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
function suppressedDeliveryReceipt() {
|
|
437
|
+
return Object.freeze({ status: 'suppressed' });
|
|
438
|
+
}
|
|
439
|
+
function unsupportedDeliveryReceipt(code) {
|
|
440
|
+
return Object.freeze({
|
|
441
|
+
status: 'unsupported',
|
|
442
|
+
failure: Object.freeze({ code, message: 'Outbound delivery is not supported.' }),
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
function rejectedDeliveryReceipt(code, message = 'Outbound payload was rejected.') {
|
|
446
|
+
return Object.freeze({
|
|
447
|
+
status: 'rejected',
|
|
448
|
+
failure: Object.freeze({ code, message }),
|
|
449
|
+
});
|
|
450
|
+
}
|
|
@@ -1,7 +1,56 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
1
|
+
import { type PluginId, type RuntimeSnapshot } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import { type ConversationRef } from '@zhin.js/im-contract';
|
|
3
|
+
import { type JSXElement } from '@zhin.js/jsx';
|
|
4
|
+
import { type MessageRenderOptions } from '../../jsx/message-renderer.js';
|
|
3
5
|
import { type IncomingContext, type SendContent } from './contracts.js';
|
|
6
|
+
import type { OutboundFallbackReason, RuntimeMessageRendering } from './message-events.js';
|
|
7
|
+
/** Keep the adapter's presentation or prefer its existing readable text fallback. */
|
|
8
|
+
export type OutboundPresentationMode = 'default' | 'text';
|
|
9
|
+
export interface OutboundPresentationPreferences {
|
|
10
|
+
readonly html?: OutboundPresentationMode;
|
|
11
|
+
readonly markdown?: OutboundPresentationMode;
|
|
12
|
+
readonly interactive?: OutboundPresentationMode;
|
|
13
|
+
}
|
|
14
|
+
export interface OutboundPresentationContext {
|
|
15
|
+
readonly conversation: ConversationRef;
|
|
16
|
+
readonly requester: PluginId;
|
|
17
|
+
readonly generation: number;
|
|
18
|
+
}
|
|
19
|
+
/** A synchronous, side-effect-free decision, shared by delivery and target previews. */
|
|
20
|
+
export type OutboundPresentation = (context: OutboundPresentationContext) => OutboundPresentationPreferences | undefined;
|
|
21
|
+
export declare const outboundPresentationToken: import("@zhin.js/plugin-runtime").Token<OutboundPresentation>;
|
|
22
|
+
/** Resolve once per operation; replacements reuse this detached, immutable decision. */
|
|
23
|
+
export declare function resolveOutboundPresentation(snapshot: RuntimeSnapshot, conversation: ConversationRef, requester: PluginId): OutboundPresentationPreferences | undefined;
|
|
24
|
+
export interface OutboundProjection {
|
|
25
|
+
readonly payload: unknown;
|
|
26
|
+
readonly rendering: RuntimeMessageRendering;
|
|
27
|
+
}
|
|
28
|
+
/** Shared by transport and target-aware previews. Does not send or register callbacks. */
|
|
29
|
+
export declare function projectOutboundPayload(rendered: unknown, conversation: ConversationRef, snapshot: RuntimeSnapshot, options?: {
|
|
30
|
+
readonly rememberFallback?: (map: Record<string, string>) => Record<string, string> | void;
|
|
31
|
+
readonly inheritedFallbacks?: readonly OutboundFallbackReason[];
|
|
32
|
+
/** Keep keyboard payloads until outbound middleware has had a chance to replace them. */
|
|
33
|
+
readonly deferInteractive?: boolean;
|
|
34
|
+
readonly signal?: AbortSignal;
|
|
35
|
+
readonly presentation?: OutboundPresentationPreferences;
|
|
36
|
+
}): Promise<OutboundProjection>;
|
|
37
|
+
/** One render operation's author, target, dependencies and shared JSX cache. */
|
|
38
|
+
export interface OutboundRenderContext {
|
|
39
|
+
readonly requester: PluginId;
|
|
40
|
+
readonly snapshot: RuntimeSnapshot;
|
|
41
|
+
readonly conversation?: ConversationRef;
|
|
42
|
+
readonly incoming?: IncomingContext;
|
|
43
|
+
readonly jsxCache?: WeakMap<JSXElement, Promise<unknown>>;
|
|
44
|
+
readonly bindButton?: MessageRenderOptions['bindButton'];
|
|
45
|
+
readonly signal?: AbortSignal;
|
|
46
|
+
}
|
|
47
|
+
/** Preview requires a concrete target but never sends or registers live buttons. */
|
|
48
|
+
export interface OutboundPreviewContext extends Pick<OutboundRenderContext, 'requester' | 'snapshot' | 'signal'> {
|
|
49
|
+
readonly conversation: ConversationRef;
|
|
50
|
+
}
|
|
4
51
|
export declare class OutboundRenderer {
|
|
5
52
|
#private;
|
|
6
|
-
|
|
53
|
+
/** Target projection only: no middleware, transport or live button registration. */
|
|
54
|
+
preview(content: SendContent, context: OutboundPreviewContext): Promise<OutboundProjection>;
|
|
55
|
+
render(content: SendContent, context: OutboundRenderContext): Promise<unknown>;
|
|
7
56
|
}
|