@zhin.js/core 1.1.41 → 1.1.43
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/lib/built/interactive-segments/action.js +1 -1
- package/lib/built/roles.d.ts +6 -1
- package/lib/built/roles.js +11 -20
- package/lib/built/segment-contract/assert.js +1 -1
- package/lib/built/segment-contract/json-schema.js +1 -1
- package/lib/built/segment-contract/validate.d.ts +301 -158
- package/lib/built/user-interaction.js +9 -3
- package/lib/feature/middleware-public-api.d.ts +5 -0
- package/lib/feature/middleware-public-api.js +5 -0
- package/lib/feature/middleware.d.ts +20 -2
- package/lib/feature/middleware.js +6 -2
- package/lib/jsx/intrinsic-elements.d.ts +83 -0
- package/lib/jsx/intrinsic-elements.js +1 -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 +126 -0
- package/lib/jsx/message-tags.js +47 -0
- package/lib/jsx-dev-runtime.d.ts +2 -1
- package/lib/jsx-dev-runtime.js +1 -1
- package/lib/jsx-runtime.d.ts +2 -1
- package/lib/jsx-runtime.js +1 -1
- package/lib/jsx.d.ts +5 -0
- package/lib/jsx.js +2 -0
- package/lib/plugin-runtime/im/contracts.d.ts +14 -0
- package/lib/plugin-runtime/im/contracts.js +3 -1
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +13 -1
- package/lib/plugin-runtime/im/conversation-runtime.js +47 -1
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +8 -3
- package/lib/plugin-runtime/im/endpoint-runtime.js +46 -3
- package/lib/plugin-runtime/im/im-runtime.d.ts +12 -1
- package/lib/plugin-runtime/im/im-runtime.js +19 -1
- package/lib/plugin-runtime/im/inbound-dedup.d.ts +51 -0
- package/lib/plugin-runtime/im/inbound-dedup.js +184 -0
- package/lib/plugin-runtime/im/inbound-origin.d.ts +101 -0
- package/lib/plugin-runtime/im/inbound-origin.js +76 -0
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +16 -2
- package/lib/plugin-runtime/im/inbound-runtime.js +238 -14
- package/lib/plugin-runtime/im/index.d.ts +3 -1
- package/lib/plugin-runtime/im/index.js +1 -0
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +17 -3
- package/lib/plugin-runtime/im/interaction-runtime.js +31 -3
- package/lib/plugin-runtime/im/interactive-router.d.ts +19 -2
- package/lib/plugin-runtime/im/interactive-router.js +80 -23
- package/lib/plugin-runtime/im/jsx-button-registry.d.ts +17 -0
- package/lib/plugin-runtime/im/jsx-button-registry.js +97 -0
- package/lib/plugin-runtime/im/message-dispatcher.js +10 -0
- package/lib/plugin-runtime/im/message-events.d.ts +7 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +5 -1
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +49 -44
- package/lib/plugin-runtime/im/outbound-ledger.d.ts +24 -0
- package/lib/plugin-runtime/im/outbound-ledger.js +114 -0
- package/lib/plugin-runtime/im/outbound-projection.d.ts +15 -0
- package/lib/plugin-runtime/im/outbound-projection.js +55 -0
- package/lib/plugin-runtime/im/outbound-renderer.d.ts +6 -1
- package/lib/plugin-runtime/im/outbound-renderer.js +51 -12
- package/lib/plugin-runtime/im/outbound-segments.d.ts +4 -1
- package/lib/plugin-runtime/im/outbound-segments.js +37 -7
- package/package.json +16 -16
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { htmlRendererToken } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import { EndpointDeliveryError } from '@zhin.js/im-contract';
|
|
3
|
+
import { flattenOutboundArray } from '../../built/outbound-content-arrays.js';
|
|
4
|
+
import { assertCanonicalSegments } from '../../built/segment-contract/assert.js';
|
|
5
|
+
import { applyOutboundInteractivePolicy, applyOutboundMarkdownPolicy, normalizeOutboundPayload, resolveOutboundHtmlPolicy, resolveOutboundInteractivePolicy, resolveOutboundMarkdownPolicy, resolveOutboundMediaPolicy, resolveOutboundSupportedSegments, } from './outbound-segments.js';
|
|
6
|
+
/** Shared by transport and target-aware previews. Does not send or register callbacks. */
|
|
7
|
+
export async function projectOutboundPayload(rendered, conversation, snapshot, options = {}) {
|
|
8
|
+
options.signal?.throwIfAborted();
|
|
9
|
+
const adapter = conversation.endpoint.id;
|
|
10
|
+
const reasons = new Set(options.inheritedFallbacks);
|
|
11
|
+
const values = Array.isArray(rendered) ? flattenOutboundArray(rendered) : rendered;
|
|
12
|
+
const contains = (type) => (Array.isArray(values) ? values : [values])
|
|
13
|
+
.some(value => value && typeof value === 'object' && value.type === type);
|
|
14
|
+
const markdown = resolveOutboundMarkdownPolicy(adapter, snapshot);
|
|
15
|
+
if (markdown === 'text' && contains('markdown'))
|
|
16
|
+
reasons.add('markdown_text_policy');
|
|
17
|
+
const host = snapshot.resources.get(snapshot.root)?.get(htmlRendererToken.id);
|
|
18
|
+
let payload = await normalizeOutboundPayload(applyOutboundMarkdownPolicy(values, markdown), host, {
|
|
19
|
+
mediaPolicy: resolveOutboundMediaPolicy(adapter, snapshot),
|
|
20
|
+
htmlPolicy: resolveOutboundHtmlPolicy(adapter, snapshot),
|
|
21
|
+
onFallback: reason => reasons.add(reason),
|
|
22
|
+
signal: options.signal,
|
|
23
|
+
});
|
|
24
|
+
options.signal?.throwIfAborted();
|
|
25
|
+
const interactive = resolveOutboundInteractivePolicy(adapter, snapshot);
|
|
26
|
+
if (interactive === 'text' && Array.isArray(payload) && payload.some(item => item?.type === 'keyboard')) {
|
|
27
|
+
reasons.add('interactive_text_policy');
|
|
28
|
+
}
|
|
29
|
+
if (!options.deferInteractive)
|
|
30
|
+
payload = applyOutboundInteractivePolicy(payload, interactive, options.rememberFallback);
|
|
31
|
+
if (Array.isArray(payload)) {
|
|
32
|
+
payload = payload.filter(item => !(item?.type === 'text' && item.data?.text === ''));
|
|
33
|
+
assertCanonicalSegments(payload);
|
|
34
|
+
}
|
|
35
|
+
const supported = resolveOutboundSupportedSegments(adapter, snapshot);
|
|
36
|
+
const supportedPayload = options.deferInteractive ? applyOutboundInteractivePolicy(payload, interactive) : payload;
|
|
37
|
+
if (supported && Array.isArray(supportedPayload)) {
|
|
38
|
+
const unsupported = supportedPayload.find(segment => segment && typeof segment === 'object'
|
|
39
|
+
&& typeof segment.type === 'string' && !supported.includes(segment.type));
|
|
40
|
+
if (unsupported)
|
|
41
|
+
throw new EndpointDeliveryError('unsupported_operation', `Adapter does not support canonical segment ${unsupported.type}`, 'not_sent');
|
|
42
|
+
}
|
|
43
|
+
const forms = new Set();
|
|
44
|
+
for (const item of Array.isArray(payload) ? payload : [payload]) {
|
|
45
|
+
if (typeof item === 'string' || item?.type === 'text')
|
|
46
|
+
forms.add('text');
|
|
47
|
+
else if (item?.type === 'image')
|
|
48
|
+
forms.add('image');
|
|
49
|
+
else if (item != null)
|
|
50
|
+
forms.add('native');
|
|
51
|
+
}
|
|
52
|
+
return Object.freeze({ payload, rendering: Object.freeze({
|
|
53
|
+
forms: Object.freeze([...forms]), fallbacks: Object.freeze([...reasons]),
|
|
54
|
+
}) });
|
|
55
|
+
}
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { type PluginId, type RuntimeSnapshot } from '@zhin.js/plugin-runtime';
|
|
2
2
|
import type { ConversationRef } from '@zhin.js/im-contract';
|
|
3
3
|
import { type JSXElement } from '@zhin.js/jsx';
|
|
4
|
+
import { type MessageRenderOptions } from '../../jsx/message-renderer.js';
|
|
4
5
|
import { type IncomingContext, type SendContent } from './contracts.js';
|
|
6
|
+
import { type OutboundProjection } from './outbound-projection.js';
|
|
5
7
|
export interface OutboundRenderOptions {
|
|
6
|
-
readonly jsxCache?: WeakMap<JSXElement, Promise<
|
|
8
|
+
readonly jsxCache?: WeakMap<JSXElement, Promise<unknown>>;
|
|
9
|
+
readonly bindButton?: MessageRenderOptions['bindButton'];
|
|
7
10
|
readonly signal?: AbortSignal;
|
|
8
11
|
}
|
|
9
12
|
export declare class OutboundRenderer {
|
|
10
13
|
#private;
|
|
14
|
+
/** Target projection only: no middleware, transport or live button registration. */
|
|
15
|
+
preview(content: SendContent, requester: PluginId, snapshot: RuntimeSnapshot, conversation: ConversationRef, options?: Pick<OutboundRenderOptions, 'signal'>): Promise<OutboundProjection>;
|
|
11
16
|
render(content: SendContent, requester: PluginId, snapshot: RuntimeSnapshot, conversation?: ConversationRef, incoming?: IncomingContext, options?: OutboundRenderOptions): Promise<unknown>;
|
|
12
17
|
}
|
|
@@ -1,21 +1,39 @@
|
|
|
1
1
|
import { componentHostToken } from '@zhin.js/plugin-runtime';
|
|
2
|
-
import { isJsxElement
|
|
3
|
-
import {
|
|
2
|
+
import { isJsxElement } from '@zhin.js/jsx';
|
|
3
|
+
import { isNativeMessageTag, renderToSegments } from '../../jsx/message-renderer.js';
|
|
4
4
|
import { flattenOutboundArray, MAX_OUTBOUND_ARRAY_DEPTH } from '../../built/outbound-content-arrays.js';
|
|
5
5
|
import { componentFeatureId, isComponentIndex, } from '@zhin.js/component';
|
|
6
6
|
import { isComponentCall, isRawContent, isSegmentContent, } from './contracts.js';
|
|
7
|
+
import { JsxButtonRegistry } from './jsx-button-registry.js';
|
|
8
|
+
import { requireAdapters } from './endpoint-runtime.js';
|
|
9
|
+
import { projectOutboundPayload } from './outbound-projection.js';
|
|
7
10
|
const maxComponentDepth = 32;
|
|
8
11
|
const TEMPLATE_MARKER = '${';
|
|
9
12
|
export class OutboundRenderer {
|
|
13
|
+
/** Target projection only: no middleware, transport or live button registration. */
|
|
14
|
+
async preview(content, requester, snapshot, conversation, options = {}) {
|
|
15
|
+
const adapters = requireAdapters(snapshot);
|
|
16
|
+
const endpointId = conversation.endpoint.id;
|
|
17
|
+
if (adapters.clientAdapter(endpointId) !== conversation.endpoint.adapter)
|
|
18
|
+
throw new Error('Preview endpoint identity mismatch');
|
|
19
|
+
if (!adapters.list().find(item => item.id === endpointId)?.capabilities.includes('outbound')) {
|
|
20
|
+
throw new Error('Preview endpoint does not support outbound messages');
|
|
21
|
+
}
|
|
22
|
+
const buttons = new JsxButtonRegistry().prepare({ content, requester, conversation }, snapshot);
|
|
23
|
+
const rendered = await this.render(content, requester, snapshot, conversation, undefined, { ...options, bindButton: buttons.bind });
|
|
24
|
+
const result = await projectOutboundPayload(rendered, conversation, snapshot, options);
|
|
25
|
+
options.signal?.throwIfAborted();
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
10
28
|
async render(content, requester, snapshot, conversation, incoming, options = {}) {
|
|
11
29
|
options.signal?.throwIfAborted();
|
|
12
30
|
const host = resolveComponentHost(snapshot);
|
|
13
31
|
const ctx = conversation ? buildTemplateContext(conversation, incoming) : undefined;
|
|
14
|
-
const rendered = await this.#render(content, requester, snapshot, host, ctx, 0, options.jsxCache ?? new WeakMap(), options.signal, new Set());
|
|
32
|
+
const rendered = await this.#render(content, requester, snapshot, host, ctx, 0, options.jsxCache ?? new WeakMap(), options.signal, new Set(), options.bindButton);
|
|
15
33
|
options.signal?.throwIfAborted();
|
|
16
34
|
return rendered;
|
|
17
35
|
}
|
|
18
|
-
async #render(content, requester, snapshot, host, ctx, depth, jsxCache, signal, arrayAncestors) {
|
|
36
|
+
async #render(content, requester, snapshot, host, ctx, depth, jsxCache, signal, arrayAncestors, bindButton, jsxAncestors = new Set()) {
|
|
19
37
|
signal?.throwIfAborted();
|
|
20
38
|
if (depth > maxComponentDepth)
|
|
21
39
|
throw new Error('Component render depth exceeded 32');
|
|
@@ -28,19 +46,38 @@ export class OutboundRenderer {
|
|
|
28
46
|
throw new RangeError('SendContent array depth exceeded 512');
|
|
29
47
|
// Each parallel branch gets its own ancestry; repeated sibling arrays are valid.
|
|
30
48
|
const ancestors = new Set(arrayAncestors).add(content);
|
|
31
|
-
const items = await Promise.all(content.map((item) => this.#render(item, requester, snapshot, host, ctx, depth, jsxCache, signal, ancestors)));
|
|
49
|
+
const items = await Promise.all(content.map((item) => this.#render(item, requester, snapshot, host, ctx, depth, jsxCache, signal, ancestors, bindButton, jsxAncestors)));
|
|
32
50
|
signal?.throwIfAborted();
|
|
33
51
|
return flattenOutboundArray(items);
|
|
34
52
|
}
|
|
35
53
|
if (isJsxElement(content)) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
54
|
+
if (jsxAncestors.has(content))
|
|
55
|
+
throw new TypeError('Circular JSX component tree');
|
|
56
|
+
let segments = jsxCache.get(content);
|
|
57
|
+
if (!segments) {
|
|
58
|
+
const ancestors = new Set(jsxAncestors).add(content);
|
|
59
|
+
const renderNamedComponent = async (name, props) => {
|
|
60
|
+
const components = snapshot.projections.get(componentFeatureId);
|
|
61
|
+
if (!isComponentIndex(components)) {
|
|
62
|
+
throw new TypeError(`Unknown message tag ${name}; register a component or wrap HTML in <html>`);
|
|
63
|
+
}
|
|
64
|
+
const rendered = await components.render(requester, name, props, { snapshot, ...(signal ? { signal } : {}) });
|
|
65
|
+
if (rendered == null)
|
|
66
|
+
return [];
|
|
67
|
+
// Resolve every SendContent shape with the same depth and ancestry
|
|
68
|
+
// guards as ComponentCall. Root raw payloads remain opaque.
|
|
69
|
+
return this.#render(rendered, requester, snapshot, host, ctx, depth + 1, jsxCache, signal, arrayAncestors, bindButton, ancestors);
|
|
70
|
+
};
|
|
71
|
+
segments = typeof content.type === 'string' && !isNativeMessageTag(content.type)
|
|
72
|
+
? renderNamedComponent(content.type, content.props)
|
|
73
|
+
: renderToSegments(content, { signal, bindButton,
|
|
74
|
+
renderComponent: async (name, props) => await renderNamedComponent(name, props),
|
|
75
|
+
});
|
|
76
|
+
jsxCache.set(content, segments);
|
|
40
77
|
}
|
|
41
|
-
const rendered = await
|
|
78
|
+
const rendered = await segments;
|
|
42
79
|
signal?.throwIfAborted();
|
|
43
|
-
return rendered
|
|
80
|
+
return rendered;
|
|
44
81
|
}
|
|
45
82
|
if (isRawContent(content))
|
|
46
83
|
return content.payload;
|
|
@@ -55,7 +92,9 @@ export class OutboundRenderer {
|
|
|
55
92
|
}
|
|
56
93
|
if (isComponentCall(content)) {
|
|
57
94
|
const rendered = await requireComponents(snapshot).render(requester, content.name, content.props, { snapshot, ...(signal ? { signal } : {}) });
|
|
58
|
-
|
|
95
|
+
if (rendered == null)
|
|
96
|
+
return [];
|
|
97
|
+
return this.#render(rendered, requester, snapshot, host, ctx, depth + 1, jsxCache, signal, arrayAncestors, bindButton, jsxAncestors);
|
|
59
98
|
}
|
|
60
99
|
throw new TypeError('Unsupported SendContent');
|
|
61
100
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CapabilityId, HtmlRendererHost, RuntimeSnapshot } from '@zhin.js/plugin-runtime';
|
|
2
2
|
import { type InteractivePolicy } from '../../built/interactive-segments/types.js';
|
|
3
|
+
import type { OutboundFallbackReason } from './message-events.js';
|
|
3
4
|
/**
|
|
4
5
|
* Outbound payload normalization for the Plugin Runtime IM pipeline.
|
|
5
6
|
*
|
|
@@ -39,10 +40,12 @@ export interface OutboundSegmentsPolicy {
|
|
|
39
40
|
readonly html?: 'direct' | 'image' | 'text';
|
|
40
41
|
}
|
|
41
42
|
export interface NormalizeOutboundOptions {
|
|
43
|
+
readonly signal?: AbortSignal;
|
|
42
44
|
/** 缺省 `url-or-text`(未声明 adapter 的保守兜底:仅 URL 媒体可达)。 */
|
|
43
45
|
readonly mediaPolicy?: OutboundMediaPolicy;
|
|
44
46
|
readonly htmlPolicy?: 'direct' | 'image' | 'text';
|
|
45
47
|
readonly onHtmlFallback?: (reason: 'renderer_unavailable' | 'renderer_failed' | 'media_unsupported') => void;
|
|
48
|
+
readonly onFallback?: (reason: OutboundFallbackReason) => void;
|
|
46
49
|
}
|
|
47
50
|
/** HTML transport is an adapter capability, independent of its package name. */
|
|
48
51
|
export declare function resolveOutboundHtmlPolicy(adapter: CapabilityId, snapshot: RuntimeSnapshot): 'direct' | 'image' | 'text';
|
|
@@ -67,7 +70,7 @@ export declare function applyOutboundMarkdownPolicy(payload: unknown, policy: 'n
|
|
|
67
70
|
* keyboard 段降级:'text' 端点把 keyboard 渲染为编号文本,并把有效
|
|
68
71
|
* fallback 映射交给调用方所属的 runtime;'native' 端点透传 keyboard。
|
|
69
72
|
*/
|
|
70
|
-
export declare function applyOutboundInteractivePolicy(payload: unknown, policy: InteractivePolicy, remember?: (map: Record<string, string>) => void): unknown;
|
|
73
|
+
export declare function applyOutboundInteractivePolicy(payload: unknown, policy: InteractivePolicy, remember?: (map: Record<string, string>) => Record<string, string> | void): unknown;
|
|
71
74
|
/**
|
|
72
75
|
* Normalize a rendered outbound payload toward canonical wire segments:
|
|
73
76
|
* - segment arrays stay arrays (html segments converted per element,
|
|
@@ -124,8 +124,9 @@ export function applyOutboundInteractivePolicy(payload, policy, remember) {
|
|
|
124
124
|
if (!isKeyboardSegment(item))
|
|
125
125
|
return item;
|
|
126
126
|
const data = item.data;
|
|
127
|
-
remember?.(effectiveKeyboardFallbackMap(data));
|
|
128
|
-
|
|
127
|
+
const map = remember?.(effectiveKeyboardFallbackMap(data));
|
|
128
|
+
const projected = map ? { ...data, fallback: { hint: data.fallback?.hint ?? '请选择(回复数字):', map } } : data;
|
|
129
|
+
return { type: 'text', data: { text: renderKeyboardAsText(projected) } };
|
|
129
130
|
});
|
|
130
131
|
}
|
|
131
132
|
/** 多 endpoint 展开的 record id(`slot.id~entry`)→ slot id(entry 名禁含 `~`,见 adapter-index)。 */
|
|
@@ -147,13 +148,14 @@ function adapterTypeName(packageName) {
|
|
|
147
148
|
* - anything else (plain strings, legacy `{ text }` shorthands) passes through.
|
|
148
149
|
*/
|
|
149
150
|
export async function normalizeOutboundPayload(payload, renderer, options) {
|
|
151
|
+
options?.signal?.throwIfAborted();
|
|
150
152
|
const mediaPolicy = options?.mediaPolicy ?? DEFAULT_MEDIA_POLICY;
|
|
151
153
|
if (Array.isArray(payload)) {
|
|
152
154
|
const resolved = await Promise.all(flattenOutboundArray(payload).map((item) => normalizeOneSegment(item, renderer, mediaPolicy, options)));
|
|
153
|
-
return applyOutboundMediaPolicy(resolved, mediaPolicy);
|
|
155
|
+
return applyOutboundMediaPolicy(resolved, mediaPolicy, options?.onFallback);
|
|
154
156
|
}
|
|
155
157
|
if (isOutboundSegment(payload)) {
|
|
156
|
-
return applyOutboundMediaPolicy([await normalizeOneSegment(payload, renderer, mediaPolicy, options)], mediaPolicy);
|
|
158
|
+
return applyOutboundMediaPolicy([await normalizeOneSegment(payload, renderer, mediaPolicy, options)], mediaPolicy, options?.onFallback);
|
|
157
159
|
}
|
|
158
160
|
return payload;
|
|
159
161
|
}
|
|
@@ -180,17 +182,20 @@ async function renderHtmlSegment(segment, renderer, mediaPolicy, options) {
|
|
|
180
182
|
const html = typeof data.html === 'string' ? data.html : '';
|
|
181
183
|
// url-or-text 端点无法投递 base64 图片(本层无上传通道),直接文本降级,跳过渲染。
|
|
182
184
|
if (options?.htmlPolicy === 'text') {
|
|
185
|
+
if (html)
|
|
186
|
+
options.onFallback?.('html_text_policy');
|
|
183
187
|
return { type: 'text', data: { text: htmlSegmentFallbackText(data, html) } };
|
|
184
188
|
}
|
|
185
189
|
if (html && renderer && mediaPolicy !== 'url-or-text') {
|
|
186
190
|
try {
|
|
187
|
-
const result = await renderer.render(html, {
|
|
191
|
+
const result = await awaitHtmlRender(renderer.render(html, {
|
|
192
|
+
...(options?.signal ? { signal: options.signal } : {}),
|
|
188
193
|
width: typeof data.width === 'number' ? data.width : DEFAULT_CARD_WIDTH,
|
|
189
194
|
format: 'png',
|
|
190
195
|
...(typeof data.backgroundColor === 'string'
|
|
191
196
|
? { backgroundColor: data.backgroundColor }
|
|
192
197
|
: {}),
|
|
193
|
-
});
|
|
198
|
+
}), options?.signal);
|
|
194
199
|
if (result.format === 'png' && result.data && typeof result.data === 'object') {
|
|
195
200
|
const base64 = Buffer.from(result.data).toString('base64');
|
|
196
201
|
return {
|
|
@@ -206,22 +211,46 @@ async function renderHtmlSegment(segment, renderer, mediaPolicy, options) {
|
|
|
206
211
|
};
|
|
207
212
|
}
|
|
208
213
|
options?.onHtmlFallback?.('renderer_failed');
|
|
214
|
+
options?.onFallback?.('renderer_failed');
|
|
209
215
|
}
|
|
210
216
|
catch {
|
|
217
|
+
options?.signal?.throwIfAborted();
|
|
211
218
|
options?.onHtmlFallback?.('renderer_failed');
|
|
219
|
+
options?.onFallback?.('renderer_failed');
|
|
212
220
|
}
|
|
213
221
|
}
|
|
214
222
|
else if (html) {
|
|
215
223
|
options?.onHtmlFallback?.(mediaPolicy === 'url-or-text' ? 'media_unsupported' : 'renderer_unavailable');
|
|
224
|
+
options?.onFallback?.(mediaPolicy === 'url-or-text' ? 'media_unsupported' : 'renderer_unavailable');
|
|
216
225
|
}
|
|
217
226
|
return { type: 'text', data: { text: htmlSegmentFallbackText(data, html) } };
|
|
218
227
|
}
|
|
228
|
+
/** Abort the caller promptly even when the optional host cannot cancel its work. */
|
|
229
|
+
async function awaitHtmlRender(work, signal) {
|
|
230
|
+
if (!signal)
|
|
231
|
+
return work;
|
|
232
|
+
let onAbort;
|
|
233
|
+
try {
|
|
234
|
+
return await new Promise((resolve, reject) => {
|
|
235
|
+
onAbort = () => reject(signal.reason);
|
|
236
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
237
|
+
// Attach both handlers even when already aborted: late host rejection is
|
|
238
|
+
// observed and cannot become an unhandled rejection after cancellation.
|
|
239
|
+
work.then(resolve, reject);
|
|
240
|
+
if (signal.aborted)
|
|
241
|
+
onAbort();
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
finally {
|
|
245
|
+
signal.removeEventListener('abort', onAbort);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
219
248
|
/**
|
|
220
249
|
* 媒体协商:仅 `url-or-text` 需要在本层动作——媒体段的非 URL MediaRef
|
|
221
250
|
* (base64 / 本地路径)无法投递,降级为文本(alt / file_name 优先)。
|
|
222
251
|
*/
|
|
223
252
|
const MEDIA_SEGMENT_TYPES = new Set(['image', 'audio', 'video', 'file']);
|
|
224
|
-
function applyOutboundMediaPolicy(segments, mediaPolicy) {
|
|
253
|
+
function applyOutboundMediaPolicy(segments, mediaPolicy, onFallback) {
|
|
225
254
|
if (mediaPolicy !== 'url-or-text')
|
|
226
255
|
return segments;
|
|
227
256
|
return segments.map((segment) => {
|
|
@@ -231,6 +260,7 @@ function applyOutboundMediaPolicy(segments, mediaPolicy) {
|
|
|
231
260
|
const media = data.media;
|
|
232
261
|
if (!isMediaRef(media) || media.kind === 'url')
|
|
233
262
|
return segment;
|
|
263
|
+
onFallback?.('media_unsupported');
|
|
234
264
|
const label = typeof data.alt === 'string' && data.alt
|
|
235
265
|
? data.alt
|
|
236
266
|
: typeof data.name === 'string' && data.name
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.43",
|
|
4
4
|
"description": "Zhin机器人核心框架",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -68,29 +68,29 @@
|
|
|
68
68
|
"dependencies": {
|
|
69
69
|
"qrcode": "^1.5.4",
|
|
70
70
|
"segment-matcher": "^1.0.5",
|
|
71
|
-
"@zhin.js/adapter": "1.1.
|
|
72
|
-
"@zhin.js/command": "1.1.
|
|
73
|
-
"@zhin.js/
|
|
74
|
-
"@zhin.js/
|
|
75
|
-
"@zhin.js/handler": "1.1.
|
|
71
|
+
"@zhin.js/adapter": "1.1.18",
|
|
72
|
+
"@zhin.js/command": "1.1.6",
|
|
73
|
+
"@zhin.js/database": "1.1.3",
|
|
74
|
+
"@zhin.js/component": "1.1.5",
|
|
75
|
+
"@zhin.js/handler": "1.1.4",
|
|
76
76
|
"@zhin.js/im-contract": "1.1.3",
|
|
77
|
-
"@zhin.js/interaction": "1.1.
|
|
78
|
-
"@zhin.js/kernel": "1.1.2",
|
|
77
|
+
"@zhin.js/interaction": "1.1.1",
|
|
79
78
|
"@zhin.js/logger": "1.1.1",
|
|
80
|
-
"@zhin.js/middleware": "1.1.
|
|
81
|
-
"@zhin.js/permission": "1.1.
|
|
82
|
-
"@zhin.js/
|
|
83
|
-
"@zhin.js/
|
|
84
|
-
"@zhin.js/
|
|
85
|
-
"@zhin.js/
|
|
86
|
-
"@zhin.js/
|
|
79
|
+
"@zhin.js/middleware": "1.1.5",
|
|
80
|
+
"@zhin.js/permission": "1.1.5",
|
|
81
|
+
"@zhin.js/schedule-feature": "1.1.4",
|
|
82
|
+
"@zhin.js/schema": "1.1.2",
|
|
83
|
+
"@zhin.js/plugin-runtime": "1.1.13",
|
|
84
|
+
"@zhin.js/feature-kit": "1.1.4",
|
|
85
|
+
"@zhin.js/kernel": "1.1.3",
|
|
86
|
+
"@zhin.js/jsx": "1.1.2"
|
|
87
87
|
},
|
|
88
88
|
"devDependencies": {
|
|
89
89
|
"@types/node": "^26.1.2",
|
|
90
90
|
"@types/qrcode": "^1.5.6",
|
|
91
91
|
"ajv": "8.18.0",
|
|
92
92
|
"typescript": "^6.0.3",
|
|
93
|
-
"@zhin.js/ai": "1.1.
|
|
93
|
+
"@zhin.js/ai": "1.1.38"
|
|
94
94
|
},
|
|
95
95
|
"repository": {
|
|
96
96
|
"type": "git",
|