@zhin.js/core 1.1.40 → 1.1.42
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/ai-trigger.js +2 -2
- package/lib/built/html-segment-fallback.d.ts +4 -2
- package/lib/built/html-segment-fallback.js +1 -1
- package/lib/built/interactive-segments/action.js +1 -1
- package/lib/built/interactive-segments/resolve.js +1 -1
- package/lib/built/interactive-segments/types.d.ts +2 -2
- package/lib/built/interactive-segments/types.js +22 -2
- package/lib/built/outbound-content-arrays.d.ts +4 -0
- package/lib/built/outbound-content-arrays.js +32 -0
- package/lib/built/outbound-media-utils.d.ts +5 -3
- package/lib/built/outbound-media-utils.js +5 -3
- package/lib/built/segment-contract/assert.js +6 -1
- package/lib/built/segment-contract/json-schema.d.ts +1 -1
- package/lib/built/segment-contract/json-schema.js +30 -2
- package/lib/feature/component.d.ts +1 -0
- package/lib/feature/component.js +1 -0
- package/lib/index.d.ts +3 -2
- package/lib/index.js +2 -1
- 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 -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 -11
- package/lib/plugin-runtime/im/contracts.d.ts +14 -6
- package/lib/plugin-runtime/im/contracts.js +28 -6
- package/lib/plugin-runtime/im/im-runtime.js +1 -0
- package/lib/plugin-runtime/im/inbound-runtime.js +33 -9
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +6 -3
- package/lib/plugin-runtime/im/interaction-runtime.js +14 -3
- package/lib/plugin-runtime/im/interactive-router.d.ts +1 -1
- package/lib/plugin-runtime/im/interactive-router.js +19 -16
- package/lib/plugin-runtime/im/jsx-button-registry.d.ts +17 -0
- package/lib/plugin-runtime/im/jsx-button-registry.js +93 -0
- package/lib/plugin-runtime/im/message-dispatcher.js +0 -1
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +3 -1
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +50 -18
- package/lib/plugin-runtime/im/outbound-renderer.d.ts +9 -2
- package/lib/plugin-runtime/im/outbound-renderer.js +52 -6
- package/lib/plugin-runtime/im/outbound-segments.d.ts +6 -1
- package/lib/plugin-runtime/im/outbound-segments.js +28 -8
- package/lib/types.d.ts +5 -7
- package/lib/utils.d.ts +4 -4
- package/lib/utils.js +9 -17
- package/package.json +15 -13
- package/lib/component.d.ts +0 -26
- package/lib/component.js +0 -583
package/lib/built/ai-trigger.js
CHANGED
|
@@ -86,7 +86,7 @@ export function isAtEndpoint(message, endpointAtIds) {
|
|
|
86
86
|
return true;
|
|
87
87
|
}
|
|
88
88
|
for (const seg of messageElements(message)) {
|
|
89
|
-
if (seg.type === 'text' && seg.data?.text && textMentionsEndpoint(seg.data.text, endpointKeys)) {
|
|
89
|
+
if (seg.type === 'text' && typeof seg.data?.text === 'string' && textMentionsEndpoint(seg.data.text, endpointKeys)) {
|
|
90
90
|
return true;
|
|
91
91
|
}
|
|
92
92
|
}
|
|
@@ -126,7 +126,7 @@ export function removeAtEndpoint(message, endpointAtIds) {
|
|
|
126
126
|
return messageElements(message)
|
|
127
127
|
.filter((seg) => !isAtSegmentForEndpoint(seg, endpointKeys))
|
|
128
128
|
.map((seg) => {
|
|
129
|
-
if (seg.type !== 'text' ||
|
|
129
|
+
if (seg.type !== 'text' || typeof seg.data?.text !== 'string')
|
|
130
130
|
return seg;
|
|
131
131
|
const stripped = stripTextAtEndpoint(seg.data.text, endpointKeys).trim();
|
|
132
132
|
if (!stripped)
|
|
@@ -2,5 +2,7 @@
|
|
|
2
2
|
* Convert unresolved `html` segments to portable text content.
|
|
3
3
|
* Canonical outbound rendering owns when this fallback is applied.
|
|
4
4
|
*/
|
|
5
|
-
import type {
|
|
6
|
-
|
|
5
|
+
import type { MessageElement } from '../types.js';
|
|
6
|
+
type SegmentContent = string | MessageElement | readonly SegmentContent[];
|
|
7
|
+
export declare function coerceHtmlSegmentsToText(content: SegmentContent): SegmentContent;
|
|
8
|
+
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { segment } from '../utils.js';
|
|
2
2
|
import { htmlToFallbackText } from './html-to-text.js';
|
|
3
3
|
function asArray(content) {
|
|
4
|
-
return Array.isArray(content) ? content : [content];
|
|
4
|
+
return Array.isArray(content) ? content.flatMap(item => asArray(item)) : [content];
|
|
5
5
|
}
|
|
6
6
|
function resolveHtmlSegmentText(data) {
|
|
7
7
|
if (typeof data.text === 'string' && data.text.length > 0)
|
|
@@ -44,7 +44,7 @@ export function resolveTextFallbackPayload(raw, map) {
|
|
|
44
44
|
const trimmed = raw.trim();
|
|
45
45
|
if (map[trimmed])
|
|
46
46
|
return map[trimmed];
|
|
47
|
-
const match = trimmed.match(/^(?:ttt\s+)?(\d)$/i);
|
|
47
|
+
const match = trimmed.match(/^(?:ttt\s+)?(\d+)$/i);
|
|
48
48
|
if (match && map[match[1]])
|
|
49
49
|
return map[match[1]];
|
|
50
50
|
return undefined;
|
|
@@ -12,7 +12,7 @@ function toKeyboardElement(data) {
|
|
|
12
12
|
return { type: KEYBOARD_SEGMENT_TYPE, data };
|
|
13
13
|
}
|
|
14
14
|
function asArray(content) {
|
|
15
|
-
return Array.isArray(content) ? content : [content];
|
|
15
|
+
return Array.isArray(content) ? content.flatMap(item => asArray(item)) : [content];
|
|
16
16
|
}
|
|
17
17
|
function packSegments(out) {
|
|
18
18
|
if (out.length === 0)
|
|
@@ -35,11 +35,11 @@ export type ActionSegmentData = {
|
|
|
35
35
|
};
|
|
36
36
|
export type InteractivePolicy = 'native' | 'text';
|
|
37
37
|
export declare const DEFAULT_INTERACTIVE_POLICY: InteractivePolicy;
|
|
38
|
-
export declare function isKeyboardSegment(item:
|
|
38
|
+
export declare function isKeyboardSegment(item: unknown): item is MessageElement & {
|
|
39
39
|
type: typeof KEYBOARD_SEGMENT_TYPE;
|
|
40
40
|
data: KeyboardSegmentData;
|
|
41
41
|
};
|
|
42
|
-
export declare function isActionSegment(item:
|
|
42
|
+
export declare function isActionSegment(item: unknown): item is MessageElement & {
|
|
43
43
|
type: typeof ACTION_SEGMENT_TYPE;
|
|
44
44
|
data: ActionSegmentData;
|
|
45
45
|
};
|
|
@@ -2,8 +2,28 @@ export const KEYBOARD_SEGMENT_TYPE = 'keyboard';
|
|
|
2
2
|
export const ACTION_SEGMENT_TYPE = 'action';
|
|
3
3
|
export const DEFAULT_INTERACTIVE_POLICY = 'text';
|
|
4
4
|
export function isKeyboardSegment(item) {
|
|
5
|
-
|
|
5
|
+
if (!isRecord(item) || item.type !== KEYBOARD_SEGMENT_TYPE || !isRecord(item.data))
|
|
6
|
+
return false;
|
|
7
|
+
const { rows, fallback } = item.data;
|
|
8
|
+
return Array.isArray(rows) && rows.every(row => Array.isArray(row) && row.every(isButtonData))
|
|
9
|
+
&& (fallback === undefined || (isRecord(fallback) && typeof fallback.hint === 'string'
|
|
10
|
+
&& isRecord(fallback.map) && Object.values(fallback.map).every(value => typeof value === 'string')));
|
|
6
11
|
}
|
|
7
12
|
export function isActionSegment(item) {
|
|
8
|
-
return item
|
|
13
|
+
return isRecord(item) && item.type === ACTION_SEGMENT_TYPE && isRecord(item.data)
|
|
14
|
+
&& typeof item.data.id === 'string' && typeof item.data.payload === 'string'
|
|
15
|
+
&& (item.data.sourceMessageId === undefined || typeof item.data.sourceMessageId === 'string');
|
|
16
|
+
}
|
|
17
|
+
function isRecord(value) {
|
|
18
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
function isButtonData(value) {
|
|
21
|
+
return isRecord(value) && typeof value.id === 'string' && typeof value.label === 'string'
|
|
22
|
+
&& typeof value.payload === 'string'
|
|
23
|
+
&& (value.disabled === undefined || typeof value.disabled === 'boolean')
|
|
24
|
+
&& (value.style === undefined || value.style === 'primary' || value.style === 'danger' || value.style === 'secondary')
|
|
25
|
+
&& (value.mode === undefined || value.mode === 'callback' || value.mode === 'command')
|
|
26
|
+
&& (value.command === undefined || (isRecord(value.command)
|
|
27
|
+
&& (value.command.enter === undefined || typeof value.command.enter === 'boolean')
|
|
28
|
+
&& (value.command.reply === undefined || typeof value.command.reply === 'boolean')));
|
|
9
29
|
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Independent structural budget; array nesting does not spend component recursion depth. */
|
|
2
|
+
export declare const MAX_OUTBOUND_ARRAY_DEPTH = 512;
|
|
3
|
+
/** Flatten untrusted authoring/native arrays without recursion or shared-reference false positives. */
|
|
4
|
+
export declare function flattenOutboundArray(values: readonly unknown[]): unknown[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Independent structural budget; array nesting does not spend component recursion depth. */
|
|
2
|
+
export const MAX_OUTBOUND_ARRAY_DEPTH = 512;
|
|
3
|
+
/** Flatten untrusted authoring/native arrays without recursion or shared-reference false positives. */
|
|
4
|
+
export function flattenOutboundArray(values) {
|
|
5
|
+
const output = [];
|
|
6
|
+
const active = new Set([values]);
|
|
7
|
+
const frames = [{ values, index: 0 }];
|
|
8
|
+
while (frames.length) {
|
|
9
|
+
const frame = frames[frames.length - 1];
|
|
10
|
+
if (frame.index >= frame.values.length) {
|
|
11
|
+
active.delete(frame.values);
|
|
12
|
+
frames.pop();
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const index = frame.index++;
|
|
16
|
+
// Array.flat skips holes, but explicit undefined must remain for validation.
|
|
17
|
+
if (!(index in frame.values))
|
|
18
|
+
continue;
|
|
19
|
+
const value = frame.values[index];
|
|
20
|
+
if (!Array.isArray(value)) {
|
|
21
|
+
output.push(value);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (active.has(value))
|
|
25
|
+
throw new TypeError('Cyclic SendContent array');
|
|
26
|
+
if (frames.length >= MAX_OUTBOUND_ARRAY_DEPTH)
|
|
27
|
+
throw new RangeError('SendContent array depth exceeded 512');
|
|
28
|
+
active.add(value);
|
|
29
|
+
frames.push({ values: value, index: 0 });
|
|
30
|
+
}
|
|
31
|
+
return output;
|
|
32
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type { MessageElement
|
|
1
|
+
import type { MessageElement } from '../types.js';
|
|
2
2
|
export declare function isRemoteUrl(value: string): boolean;
|
|
3
3
|
/** 从 message 段 data 提取 base64 Buffer(data: URL / base64:// / raw base64) */
|
|
4
4
|
export declare function extractBase64Buffer(data: Record<string, unknown>): Buffer | undefined;
|
|
5
5
|
/** 解析本地文件路径(非 http(s) / base64) */
|
|
6
6
|
export declare function resolveLocalMediaPath(data: Record<string, unknown>): string | undefined;
|
|
7
|
-
|
|
7
|
+
/** Already rendered content; JSX, component calls and raw wrappers need OutboundRenderer first. */
|
|
8
|
+
export type RenderedMessageContent = string | MessageElement | readonly RenderedMessageContent[];
|
|
9
|
+
export declare function asMessageElements(content: RenderedMessageContent): MessageElement[];
|
|
8
10
|
export declare function isMediaSegmentType(type: string): boolean;
|
|
9
|
-
export declare function collectOutboundMediaKinds(content:
|
|
11
|
+
export declare function collectOutboundMediaKinds(content: RenderedMessageContent | undefined): string[];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isSegmentContent } from '../plugin-runtime/im/contracts.js';
|
|
1
2
|
const MEDIA_SEGMENT_TYPES = new Set(['image', 'audio', 'video', 'file']);
|
|
2
3
|
export function isRemoteUrl(value) {
|
|
3
4
|
return /^https?:\/\//i.test(value);
|
|
@@ -40,10 +41,11 @@ export function asMessageElements(content) {
|
|
|
40
41
|
if (typeof content === 'string') {
|
|
41
42
|
return [{ type: 'text', data: { text: content } }];
|
|
42
43
|
}
|
|
43
|
-
if (
|
|
44
|
+
if (Array.isArray(content))
|
|
45
|
+
return content.flatMap(item => asMessageElements(item));
|
|
46
|
+
if (isSegmentContent(content))
|
|
44
47
|
return [content];
|
|
45
|
-
|
|
46
|
-
return content.map((item) => typeof item === 'string' ? { type: 'text', data: { text: item } } : item);
|
|
48
|
+
throw new TypeError('Message elements require rendered content; JSX and component calls must use the outbound renderer');
|
|
47
49
|
}
|
|
48
50
|
export function isMediaSegmentType(type) {
|
|
49
51
|
return MEDIA_SEGMENT_TYPES.has(type);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isStrictCanonicalSegment } from './validate.js';
|
|
2
|
+
import { isActionSegment, isKeyboardSegment } from '../interactive-segments/types.js';
|
|
2
3
|
const STRICT_CANONICAL_TYPES = new Set([
|
|
3
4
|
'text', 'mention', 'image', 'audio', 'video', 'file', 'reply', 'forward',
|
|
4
5
|
'face', 'dice', 'rps', 'share',
|
|
@@ -7,7 +8,7 @@ const STRICT_CANONICAL_TYPES = new Set([
|
|
|
7
8
|
// their gradual contracts isolated here; third-party extensions must be
|
|
8
9
|
// namespaced so a future canonical type cannot silently collide with them.
|
|
9
10
|
const CORE_EXTENSION_TYPES = new Set([
|
|
10
|
-
'action', 'html', 'keyboard', 'link', 'markdown', 'qrcode', 'record', 'tts', 'voice',
|
|
11
|
+
'action', 'card', 'html', 'keyboard', 'link', 'markdown', 'qrcode', 'record', 'tts', 'voice',
|
|
11
12
|
]);
|
|
12
13
|
const extensionTypePattern = /^[a-z][a-z0-9-]*(?::[a-z][a-z0-9-]*)+$/u;
|
|
13
14
|
function isPlainObject(value) {
|
|
@@ -26,6 +27,10 @@ export function isCanonicalSegment(value) {
|
|
|
26
27
|
return false;
|
|
27
28
|
if (STRICT_CANONICAL_TYPES.has(value.type))
|
|
28
29
|
return isStrictCanonicalSegment(value);
|
|
30
|
+
if (value.type === 'keyboard')
|
|
31
|
+
return isKeyboardSegment(value);
|
|
32
|
+
if (value.type === 'action')
|
|
33
|
+
return isActionSegment(value);
|
|
29
34
|
return CORE_EXTENSION_TYPES.has(value.type) || extensionTypePattern.test(value.type);
|
|
30
35
|
}
|
|
31
36
|
export function assertCanonicalSegments(segments) {
|
|
@@ -16,7 +16,7 @@ export declare const mediaRefJsonSchema: JsonSchemaObject;
|
|
|
16
16
|
export declare const STRICT_OUTBOUND_SEGMENT_TYPES: readonly ["text", "mention", "image", "audio", "video", "file", "reply", "forward", "face", "dice", "rps", "share"];
|
|
17
17
|
/**
|
|
18
18
|
* 单条 outbound 消息段的 JSON Schema。
|
|
19
|
-
* 严格段镜像 validate.ts
|
|
19
|
+
* 严格段镜像 validate.ts,交互段镜像 interactive-segments/types.ts;其他扩展段走宽松分支。
|
|
20
20
|
*/
|
|
21
21
|
export declare const outboundSegmentJsonSchema: JsonSchemaObject;
|
|
22
22
|
/**
|
|
@@ -44,13 +44,29 @@ function strictBranch(type, data) {
|
|
|
44
44
|
function dataObject(properties, required) {
|
|
45
45
|
return { type: 'object', properties, required: [...required], additionalProperties: false };
|
|
46
46
|
}
|
|
47
|
+
/** Interactive extension guards validate known fields while preserving platform-specific extras. */
|
|
48
|
+
function interactiveDataObject(properties, required) {
|
|
49
|
+
return { type: 'object', properties, required: [...required], additionalProperties: true };
|
|
50
|
+
}
|
|
51
|
+
const buttonJsonSchema = interactiveDataObject({
|
|
52
|
+
id: { type: 'string' },
|
|
53
|
+
label: { type: 'string' },
|
|
54
|
+
payload: { type: 'string' },
|
|
55
|
+
disabled: { type: 'boolean' },
|
|
56
|
+
style: { type: 'string', enum: ['primary', 'danger', 'secondary'] },
|
|
57
|
+
mode: { type: 'string', enum: ['callback', 'command'] },
|
|
58
|
+
command: interactiveDataObject({
|
|
59
|
+
enter: { type: 'boolean' },
|
|
60
|
+
reply: { type: 'boolean' },
|
|
61
|
+
}, []),
|
|
62
|
+
}, ['id', 'label', 'payload']);
|
|
47
63
|
/** 宽松分支:未纳入严格契约的段类型,仅约束顶层形状(与 isCanonicalSegment 宽松路径一致) */
|
|
48
64
|
const looseBranch = {
|
|
49
65
|
type: 'object',
|
|
50
66
|
properties: {
|
|
51
67
|
type: {
|
|
52
68
|
type: 'string',
|
|
53
|
-
enum: ['voice', 'record', 'link', 'markdown', 'html', '
|
|
69
|
+
enum: ['voice', 'record', 'link', 'markdown', 'html', 'card'],
|
|
54
70
|
},
|
|
55
71
|
data: { type: 'object' },
|
|
56
72
|
platform: platformJsonSchema,
|
|
@@ -65,7 +81,7 @@ export const STRICT_OUTBOUND_SEGMENT_TYPES = [
|
|
|
65
81
|
];
|
|
66
82
|
/**
|
|
67
83
|
* 单条 outbound 消息段的 JSON Schema。
|
|
68
|
-
* 严格段镜像 validate.ts
|
|
84
|
+
* 严格段镜像 validate.ts,交互段镜像 interactive-segments/types.ts;其他扩展段走宽松分支。
|
|
69
85
|
*/
|
|
70
86
|
export const outboundSegmentJsonSchema = {
|
|
71
87
|
description: 'zhin 消息段 {type, data, platform?}',
|
|
@@ -129,6 +145,18 @@ export const outboundSegmentJsonSchema = {
|
|
|
129
145
|
version: { type: 'string' },
|
|
130
146
|
}, ['appid']),
|
|
131
147
|
}, ['url', 'title'])),
|
|
148
|
+
strictBranch('keyboard', interactiveDataObject({
|
|
149
|
+
rows: { type: 'array', items: { type: 'array', items: buttonJsonSchema } },
|
|
150
|
+
fallback: interactiveDataObject({
|
|
151
|
+
hint: { type: 'string' },
|
|
152
|
+
map: { type: 'object', additionalProperties: { type: 'string' } },
|
|
153
|
+
}, ['hint', 'map']),
|
|
154
|
+
}, ['rows'])),
|
|
155
|
+
strictBranch('action', interactiveDataObject({
|
|
156
|
+
id: { type: 'string' },
|
|
157
|
+
payload: { type: 'string' },
|
|
158
|
+
sourceMessageId: { type: 'string' },
|
|
159
|
+
}, ['id', 'payload'])),
|
|
132
160
|
looseBranch,
|
|
133
161
|
],
|
|
134
162
|
};
|
package/lib/feature/component.js
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
* Canonical IM messages, Endpoints, rendering, and plugin-facing core types.
|
|
3
3
|
* @module @zhin.js/core
|
|
4
4
|
*/
|
|
5
|
-
export
|
|
6
|
-
export
|
|
5
|
+
export { defineComponent, type ComponentDefinition, type ComponentContext } from '@zhin.js/component';
|
|
6
|
+
export { component, type ComponentCall, raw, type RawContent } from './plugin-runtime/im/contracts.js';
|
|
7
|
+
export type { MessageChannel, MessageType } from './message.js';
|
|
7
8
|
export { Message, type MessageBase } from './plugin-runtime/im/contracts.js';
|
|
8
9
|
export * from './im-scene.js';
|
|
9
10
|
export * from './notice.js';
|
package/lib/index.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Canonical IM messages, Endpoints, rendering, and plugin-facing core types.
|
|
3
3
|
* @module @zhin.js/core
|
|
4
4
|
*/
|
|
5
|
-
export
|
|
5
|
+
export { defineComponent } from '@zhin.js/component';
|
|
6
|
+
export { component, raw } from './plugin-runtime/im/contracts.js';
|
|
6
7
|
export { Message } from './plugin-runtime/im/contracts.js';
|
|
7
8
|
export * from './im-scene.js';
|
|
8
9
|
export * from './notice.js';
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { JSX as BaseJSX, JSXAttributes, HtmlProps } from '@zhin.js/jsx';
|
|
2
|
+
import type { MediaRef, ForwardSegment, ShareSegment } from '@zhin.js/im-contract';
|
|
3
|
+
import type { ButtonProps, ImageProps, MentionProps, FaceProps, TextProps, MarkdownProps } from './message-tags.js';
|
|
4
|
+
import type { AudioSegment, VideoSegment, FileSegment } from '../built/segment-contract/types.js';
|
|
5
|
+
import type { KeyboardSegmentData, ActionSegmentData } from '../built/interactive-segments/types.js';
|
|
6
|
+
type MediaProps = {
|
|
7
|
+
readonly media: MediaRef;
|
|
8
|
+
readonly duration?: number;
|
|
9
|
+
readonly alt?: string;
|
|
10
|
+
readonly name?: string;
|
|
11
|
+
};
|
|
12
|
+
type HtmlInputProps = Omit<HtmlProps, 'children'> & ({
|
|
13
|
+
readonly html: string;
|
|
14
|
+
readonly children?: never;
|
|
15
|
+
} | {
|
|
16
|
+
readonly children?: HtmlProps['children'];
|
|
17
|
+
readonly html?: never;
|
|
18
|
+
});
|
|
19
|
+
/** Lowercase tags mirror Segment.type and its data fields, with typed creation helpers. */
|
|
20
|
+
export interface MessageTagProps {
|
|
21
|
+
text: TextProps;
|
|
22
|
+
image: ImageProps | {
|
|
23
|
+
readonly media: MediaRef;
|
|
24
|
+
readonly alt?: string;
|
|
25
|
+
};
|
|
26
|
+
mention: MentionProps;
|
|
27
|
+
face: FaceProps;
|
|
28
|
+
audio: AudioSegment['data'];
|
|
29
|
+
video: VideoSegment['data'];
|
|
30
|
+
file: FileSegment['data'];
|
|
31
|
+
record: MediaProps;
|
|
32
|
+
voice: MediaProps;
|
|
33
|
+
reply: {
|
|
34
|
+
readonly message_id: string;
|
|
35
|
+
};
|
|
36
|
+
forward: ForwardSegment['data'];
|
|
37
|
+
share: ShareSegment['data'];
|
|
38
|
+
dice: {
|
|
39
|
+
readonly result?: number;
|
|
40
|
+
};
|
|
41
|
+
rps: {
|
|
42
|
+
readonly result?: number;
|
|
43
|
+
};
|
|
44
|
+
card: Readonly<Record<string, unknown>> & {
|
|
45
|
+
readonly children?: never;
|
|
46
|
+
};
|
|
47
|
+
button: ButtonProps;
|
|
48
|
+
keyboard: KeyboardSegmentData;
|
|
49
|
+
action: ActionSegmentData;
|
|
50
|
+
html: HtmlInputProps;
|
|
51
|
+
markdown: MarkdownProps;
|
|
52
|
+
link: {
|
|
53
|
+
readonly url: string;
|
|
54
|
+
readonly text?: string;
|
|
55
|
+
};
|
|
56
|
+
qrcode: {
|
|
57
|
+
readonly text: string;
|
|
58
|
+
readonly width?: number;
|
|
59
|
+
readonly margin?: number;
|
|
60
|
+
readonly errorCorrectionLevel?: 'L' | 'M' | 'Q' | 'H';
|
|
61
|
+
readonly small?: boolean;
|
|
62
|
+
};
|
|
63
|
+
tts: {
|
|
64
|
+
readonly text: string;
|
|
65
|
+
readonly voice?: string;
|
|
66
|
+
readonly provider?: string;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Compiler namespace shared by both production and development runtime entrypoints. */
|
|
70
|
+
export declare namespace JSX {
|
|
71
|
+
type Element = BaseJSX.Element;
|
|
72
|
+
type ElementType = BaseJSX.ElementType;
|
|
73
|
+
interface ElementChildrenAttribute {
|
|
74
|
+
children: {};
|
|
75
|
+
}
|
|
76
|
+
interface IntrinsicAttributes {
|
|
77
|
+
key?: string | number;
|
|
78
|
+
}
|
|
79
|
+
interface IntrinsicElements extends MessageTagProps {
|
|
80
|
+
[name: string]: JSXAttributes | MessageTagProps[keyof MessageTagProps];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type JSXRenderable, type JSXProps } from '@zhin.js/jsx';
|
|
2
|
+
import type { Segment } from '@zhin.js/im-contract';
|
|
3
|
+
import { type ButtonProps } from './message-tags.js';
|
|
4
|
+
/** Native and namespaced extension tags do not use ComponentIndex. */
|
|
5
|
+
export declare function isNativeMessageTag(type: string): boolean;
|
|
6
|
+
export interface MessageRenderOptions {
|
|
7
|
+
readonly maxDepth?: number;
|
|
8
|
+
readonly signal?: AbortSignal;
|
|
9
|
+
readonly bindButton?: (props: ButtonProps) => Segment;
|
|
10
|
+
readonly renderComponent?: (name: string, props: JSXProps) => Promise<JSXRenderable>;
|
|
11
|
+
}
|
|
12
|
+
/** Default JSX lays out ordered message segments. Only Html changes to HTML semantics. */
|
|
13
|
+
export declare function renderToSegments(node: JSXRenderable, options?: MessageRenderOptions): Promise<readonly Segment[]>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Fragment, Html, isJsxElement, renderToHtml } from '@zhin.js/jsx';
|
|
2
|
+
import { segment } from '../utils.js';
|
|
3
|
+
import { isSegmentContent } from '../plugin-runtime/im/contracts.js';
|
|
4
|
+
import { Image } from './message-tags.js';
|
|
5
|
+
import { isKeyboardSegment } from '../built/interactive-segments/types.js';
|
|
6
|
+
const MESSAGE_TAGS = new Set([
|
|
7
|
+
'text', 'mention', 'image', 'audio', 'video', 'file', 'reply', 'forward', 'face',
|
|
8
|
+
'dice', 'rps', 'share', 'card', 'action', 'html', 'keyboard', 'link', 'markdown',
|
|
9
|
+
'qrcode', 'record', 'tts', 'voice',
|
|
10
|
+
]);
|
|
11
|
+
/** Native and namespaced extension tags do not use ComponentIndex. */
|
|
12
|
+
export function isNativeMessageTag(type) {
|
|
13
|
+
return type === 'button' || MESSAGE_TAGS.has(type) || /^[a-z][a-z0-9-]*(?::[a-z][a-z0-9-]*)+$/.test(type);
|
|
14
|
+
}
|
|
15
|
+
/** Default JSX lays out ordered message segments. Only Html changes to HTML semantics. */
|
|
16
|
+
export async function renderToSegments(node, options = {}) {
|
|
17
|
+
const maxDepth = options.maxDepth ?? 100;
|
|
18
|
+
if (!Number.isSafeInteger(maxDepth) || maxDepth < 1)
|
|
19
|
+
throw new RangeError('JSX maxDepth must be a positive integer');
|
|
20
|
+
const ancestors = new WeakSet();
|
|
21
|
+
const output = [];
|
|
22
|
+
let lastButtonIndex = -1;
|
|
23
|
+
const render = async (value, depth) => {
|
|
24
|
+
options.signal?.throwIfAborted();
|
|
25
|
+
if (depth > maxDepth)
|
|
26
|
+
throw new RangeError(`JSX exceeds maximum render depth ${maxDepth}`);
|
|
27
|
+
if (value == null || typeof value === 'boolean')
|
|
28
|
+
return;
|
|
29
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
30
|
+
if (value !== '')
|
|
31
|
+
output.push(segment.text(String(value)));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (typeof value !== 'object')
|
|
35
|
+
throw new TypeError('Unsupported JSX child');
|
|
36
|
+
if (ancestors.has(value))
|
|
37
|
+
throw new TypeError('Circular JSX tree');
|
|
38
|
+
ancestors.add(value);
|
|
39
|
+
try {
|
|
40
|
+
if (Array.isArray(value)) {
|
|
41
|
+
for (const child of value)
|
|
42
|
+
await render(child, depth + 1);
|
|
43
|
+
}
|
|
44
|
+
else if ('then' in value && typeof value.then === 'function') {
|
|
45
|
+
const then = value.then;
|
|
46
|
+
const resolved = await new Promise((resolve, reject) => {
|
|
47
|
+
then.call(value, (child) => resolve({ node: child }), reject);
|
|
48
|
+
});
|
|
49
|
+
await render(resolved.node, depth + 1);
|
|
50
|
+
}
|
|
51
|
+
else if (isJsxElement(value)) {
|
|
52
|
+
const { type, props } = value;
|
|
53
|
+
if (type === Fragment)
|
|
54
|
+
await render(props.children, depth + 1);
|
|
55
|
+
else if (type === Html || type === 'html') {
|
|
56
|
+
if (props.html !== undefined && props.children !== undefined)
|
|
57
|
+
throw new TypeError('Html accepts html or children, not both');
|
|
58
|
+
if (props.html !== undefined && typeof props.html !== 'string')
|
|
59
|
+
throw new TypeError('Html html must be a string');
|
|
60
|
+
const html = typeof props.html === 'string' ? props.html
|
|
61
|
+
: await renderToHtml(props.children, { maxDepth: Math.max(1, maxDepth - depth) });
|
|
62
|
+
const { width, backgroundColor, fileName, text } = props;
|
|
63
|
+
if (html || text)
|
|
64
|
+
output.push(segment.html({ html,
|
|
65
|
+
...(width === undefined ? {} : { width }),
|
|
66
|
+
...(backgroundColor === undefined ? {} : { backgroundColor }),
|
|
67
|
+
...(fileName === undefined ? {} : { fileName }),
|
|
68
|
+
...(text === undefined ? {} : { text }),
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
else if (typeof type === 'function')
|
|
72
|
+
await render(type(props), depth + 1);
|
|
73
|
+
else if (type === 'image' && 'src' in props)
|
|
74
|
+
await render(Image(props), depth + 1);
|
|
75
|
+
else if (type === 'button') {
|
|
76
|
+
if (!options.bindButton)
|
|
77
|
+
throw new TypeError('Button requires an active IM runtime');
|
|
78
|
+
const button = options.bindButton(props);
|
|
79
|
+
const previous = output[lastButtonIndex];
|
|
80
|
+
if (lastButtonIndex === output.length - 1 && isKeyboardSegment(previous) && isKeyboardSegment(button)) {
|
|
81
|
+
output[lastButtonIndex] = { type: 'keyboard', data: { rows: [[...previous.data.rows.flat(), ...button.data.rows.flat()]] } };
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
output.push(button);
|
|
85
|
+
lastButtonIndex = output.length - 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (typeof type === 'string' && isNativeMessageTag(type)) {
|
|
89
|
+
const { children, ...data } = props;
|
|
90
|
+
if ((type === 'text' || type === 'markdown') && children !== undefined) {
|
|
91
|
+
const field = type === 'text' ? 'text' : 'content';
|
|
92
|
+
if (data[field] !== undefined)
|
|
93
|
+
throw new TypeError(`${type} accepts ${field} or children, not both`);
|
|
94
|
+
const start = output.length;
|
|
95
|
+
await render(children, depth + 1);
|
|
96
|
+
const parts = output.splice(start);
|
|
97
|
+
if (!parts.every((part) => part.type === 'text' && 'text' in part.data && typeof part.data.text === 'string')) {
|
|
98
|
+
throw new TypeError(`${type} children must produce text segments`);
|
|
99
|
+
}
|
|
100
|
+
const text = parts.map(part => part.data.text).join('');
|
|
101
|
+
if (text)
|
|
102
|
+
output.push(segment(type, { ...data, [field]: text }));
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
if (children !== undefined)
|
|
106
|
+
throw new TypeError(`Message tag ${type} stores content in its data props, not children`);
|
|
107
|
+
output.push(segment(type, data));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else if (typeof type === 'string' && options.renderComponent) {
|
|
111
|
+
await render(await options.renderComponent(type, props), depth + 1);
|
|
112
|
+
}
|
|
113
|
+
else
|
|
114
|
+
throw new TypeError(`Unknown message tag ${String(type)}; register a component or wrap HTML in <html>`);
|
|
115
|
+
}
|
|
116
|
+
else if (isSegmentContent(value))
|
|
117
|
+
output.push(value);
|
|
118
|
+
else
|
|
119
|
+
throw new TypeError('Unsupported JSX child: expected a JSX element or Segment');
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
ancestors.delete(value);
|
|
123
|
+
}
|
|
124
|
+
options.signal?.throwIfAborted();
|
|
125
|
+
};
|
|
126
|
+
await render(node, 0);
|
|
127
|
+
return output;
|
|
128
|
+
}
|