@realnation/content-config 0.6.2 → 0.8.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 +2 -1
- package/package.json +50 -50
- package/src/canvas.d.ts +3 -0
- package/src/canvas.mjs +6 -1
- package/src/exits.d.ts +38 -38
- package/src/exits.mjs +129 -129
- package/src/index.mjs +3 -3
package/README.md
CHANGED
|
@@ -105,7 +105,8 @@ import { canvasConfig } from '@realnation/content-config/canvas';
|
|
|
105
105
|
- 键是 componentName。`kind: 'system'` 是登记在 editor_tabs.components 的;`kind: 'pushed'` 是 page-data 推的,要写 `key`、`type`(平台元件类型,按钮是 `image-text`,0.6.2 起认全部类型)、英文 `appearsWhen`,可选机器可读的 `when`(`{ 内容配置读回的请求体里的路径: 值 }`,如 `{ 'design.wheelCount': 2 }`)。
|
|
106
106
|
- 面板项的类型、默认值、选项、范围不写,以后台登记的 collocation 为准;`properties` 只写模块真读的属性的英文说明(宿主统一套在外框上的位置、大小、颜色、字体不列),`ignores` 列面板上有、模块不读的。
|
|
107
107
|
- `layout`、`layoutVariants`、`key` 都从模块代码 import 同一份常量(如 placeholders.ts),不抄数字。按配置里的列表一条推一个的元件(如 Video Player 每个叠加位一个),`key` 写模板 `vp-slot-{n}`(`{n}` 是模块编的序号数字,多数从 1 起,有的从 0 起,在 appearsWhen 写明),核对时查 `{n}` 前面那一截。`layout` 0.6.0 起可选:位置写在模块代码里的(模块自己会挪元件)才写;只登记在平台(collocation 的 values)的不写,默认位置以登记为准,不抄一份进来。推送元件的 key、componentName 与 `module` 要在构建用到的源码里原样找得到(另加 `src/page-events.ts`),对不上构建失败。
|
|
108
|
-
- `constraints` 只写模块自己的摆放规矩:`aspect`、`minWidth`、`minHeight`、`anchorsTo`(压在谁的正中)、`overlapAllowed
|
|
108
|
+
- `constraints` 只写模块自己的摆放规矩:`aspect`、`minWidth`、`minHeight`、`anchorsTo`(压在谁的正中)、`overlapAllowed`(可以压在它上面的本模块元件名;0.8.0 起只写 `['*']` 表示任何元件都行,连运营自己加的也算,给本来就盖在运营元件上面的层用,如 Logo Wall 揭晓时的压暗层与白色页头)、`face`(盒子里真正画东西的是贴着远离某条边的正方形,如转盘的圆,`anchorsTo` 对准它的中心,`aspect` 在那条边为左右时宽高对调)。不出屏、不压角标这类平台规矩不写在这里。
|
|
109
|
+
- `control`(0.7.0 起,可选):访客要按、要点、要靠它操作的元件写 `control: true`(左右箭头、Spin、开始、提交、点哪都算的游戏区)。建活动引擎据此不许 AI 未经用户点名拿掉、隐藏、盖住它(引擎规矩 24)。不写时引擎按出厂版面带不带键码 `key-code` 认;带键码但不算操作的写 `false`。
|
|
109
110
|
- 说明文字一律英文。完整样例:体验仓 qiksense-lottery-spin-and-win-experience 的 `src/canvas-config.ts`。
|
|
110
111
|
|
|
111
112
|
## 其他用法
|
package/package.json
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@realnation/content-config",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "qiksense 模块内容配置的机器可读说明:构建时导出 content-config.json,并用字段说明校验默认请求体;0.5.0 起另导出画布说明 canvas-config.json(体验端)",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"exports": {
|
|
7
|
-
".": {
|
|
8
|
-
"types": "./src/index.d.ts",
|
|
9
|
-
"import": "./src/index.mjs"
|
|
10
|
-
},
|
|
11
|
-
"./validate": {
|
|
12
|
-
"types": "./src/validate.d.ts",
|
|
13
|
-
"import": "./src/validate.mjs"
|
|
14
|
-
},
|
|
15
|
-
"./exits": {
|
|
16
|
-
"types": "./src/exits.d.ts",
|
|
17
|
-
"import": "./src/exits.mjs"
|
|
18
|
-
},
|
|
19
|
-
"./canvas": {
|
|
20
|
-
"types": "./src/canvas.d.ts",
|
|
21
|
-
"import": "./src/canvas.mjs"
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
"files": [
|
|
25
|
-
"src",
|
|
26
|
-
"README.md"
|
|
27
|
-
],
|
|
28
|
-
"scripts": {
|
|
29
|
-
"test": "node --test \"test/*.test.mjs\""
|
|
30
|
-
},
|
|
31
|
-
"peerDependencies": {
|
|
32
|
-
"esbuild": ">=0.19",
|
|
33
|
-
"vite": ">=5"
|
|
34
|
-
},
|
|
35
|
-
"peerDependenciesMeta": {
|
|
36
|
-
"esbuild": {
|
|
37
|
-
"optional": true
|
|
38
|
-
},
|
|
39
|
-
"vite": {
|
|
40
|
-
"optional": true
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
"engines": {
|
|
44
|
-
"node": ">=18"
|
|
45
|
-
},
|
|
46
|
-
"publishConfig": {
|
|
47
|
-
"access": "public"
|
|
48
|
-
},
|
|
49
|
-
"license": "UNLICENSED"
|
|
50
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@realnation/content-config",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "qiksense 模块内容配置的机器可读说明:构建时导出 content-config.json,并用字段说明校验默认请求体;0.5.0 起另导出画布说明 canvas-config.json(体验端)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"import": "./src/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"./validate": {
|
|
12
|
+
"types": "./src/validate.d.ts",
|
|
13
|
+
"import": "./src/validate.mjs"
|
|
14
|
+
},
|
|
15
|
+
"./exits": {
|
|
16
|
+
"types": "./src/exits.d.ts",
|
|
17
|
+
"import": "./src/exits.mjs"
|
|
18
|
+
},
|
|
19
|
+
"./canvas": {
|
|
20
|
+
"types": "./src/canvas.d.ts",
|
|
21
|
+
"import": "./src/canvas.mjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"src",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "node --test \"test/*.test.mjs\""
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"esbuild": ">=0.19",
|
|
33
|
+
"vite": ">=5"
|
|
34
|
+
},
|
|
35
|
+
"peerDependenciesMeta": {
|
|
36
|
+
"esbuild": {
|
|
37
|
+
"optional": true
|
|
38
|
+
},
|
|
39
|
+
"vite": {
|
|
40
|
+
"optional": true
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=18"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"license": "UNLICENSED"
|
|
50
|
+
}
|
package/src/canvas.d.ts
CHANGED
|
@@ -20,6 +20,8 @@ export interface CanvasElement {
|
|
|
20
20
|
tab: string;
|
|
21
21
|
/** pushed 必填:page-data 里的 type,平台元件类型(共用 SDK 的 EditorType:按钮 image-text、热区 button、平台拼写 compnent),另加图片变体 image-ratio、image-clip、image-ratio-clip */
|
|
22
22
|
type?: string;
|
|
23
|
+
/** 0.7.0 起:访客要按、要点、要靠它操作的元件(左右箭头、Spin、开始、提交、点哪都算的游戏区)写 true;建活动引擎不许 AI 未经用户点名拿掉、隐藏或盖住它(规矩 24)。不写就按出厂版面带不带键码 key-code 认,写 false 表示带键码但不算 */
|
|
24
|
+
control?: boolean;
|
|
23
25
|
/** 英文,给 AI:访客看到什么、模块拿它做什么、运营通常怎么改 */
|
|
24
26
|
description: string;
|
|
25
27
|
/** pushed 必填,英文:什么配置下才推 */
|
|
@@ -46,6 +48,7 @@ export interface CanvasElement {
|
|
|
46
48
|
minWidth?: number;
|
|
47
49
|
minHeight?: number;
|
|
48
50
|
anchorsTo?: { element: string; at: 'center' | 'top' | 'bottom' | 'left' | 'right' };
|
|
51
|
+
/** 可以压在它上面的本模块元件名;只写 ['*'](0.8.0 起)表示任何元件都行,连运营自己加的也算(这一层本来就盖在运营元件上面,如揭晓时的压暗层) */
|
|
49
52
|
overlapAllowed?: string[];
|
|
50
53
|
/** 盒子里真正画东西的是一个正方形(如转盘的圆),贴着远离 awayFrom 属性所指那条边的一端;anchorsTo 指向它时对准正方形中心 */
|
|
51
54
|
face?: { shape: 'square'; awayFrom?: string };
|
package/src/canvas.mjs
CHANGED
|
@@ -25,7 +25,7 @@ const PUSHED_TYPES = new Set([
|
|
|
25
25
|
'progress', 'sound-toggle', 'motion-preview', 'cursor-area', 'evoucher-pin', 'evoucher-submit',
|
|
26
26
|
'image-ratio', 'image-clip', 'image-ratio-clip',
|
|
27
27
|
])
|
|
28
|
-
const ELEMENT_KEYS = new Set(['kind', 'key', 'tab', 'type', 'description', 'appearsWhen', 'when', 'layout', 'layoutVariants', 'styleFrom', 'properties', 'ignores', 'assets', 'constraints'])
|
|
28
|
+
const ELEMENT_KEYS = new Set(['kind', 'key', 'tab', 'type', 'description', 'appearsWhen', 'when', 'layout', 'layoutVariants', 'styleFrom', 'properties', 'ignores', 'assets', 'constraints', 'control'])
|
|
29
29
|
const BOX_KEYS = ['x-position', 'y-position', 'width', 'height']
|
|
30
30
|
const ASSET_SOURCES = new Set(['from config', 'operator', 'none'])
|
|
31
31
|
const ANCHOR_AT = new Set(['center', 'top', 'bottom', 'left', 'right'])
|
|
@@ -95,6 +95,8 @@ export function validateCanvasConfig(spec) {
|
|
|
95
95
|
if (el.appearsWhen !== undefined || el.when !== undefined) errors.push(`${w}:系统元件新建节点就有,不写 appearsWhen、when`)
|
|
96
96
|
}
|
|
97
97
|
if (el.when !== undefined) checkWhen(errors, `${w}.when`, el.when)
|
|
98
|
+
// 0.7.0 起:访客要按、要点、要靠它操作的元件(箭头、开始、提交、点哪都算的游戏区)写 control: true,建活动引擎不许 AI 未经用户点名拿掉、隐藏、盖住(规矩 24)
|
|
99
|
+
if (el.control !== undefined && typeof el.control !== 'boolean') errors.push(`${w}.control:写 true(操作元件)或 false,不写就按出厂版面带不带键码认`)
|
|
98
100
|
// 0.6.0 起可选:位置写在模块代码里的(模块自己挪元件,如 Spin and Win)照写;只登记在平台的不写,以登记为准
|
|
99
101
|
if (el.layout !== undefined) {
|
|
100
102
|
if (!isObj(el.layout)) errors.push(`${w}.layout:写两档默认版面 { v: {…}, h: {…} },位置只登记在平台的就不写`)
|
|
@@ -152,6 +154,9 @@ export function validateCanvasConfig(spec) {
|
|
|
152
154
|
}
|
|
153
155
|
if (c.overlapAllowed !== undefined) {
|
|
154
156
|
if (!Array.isArray(c.overlapAllowed)) errors.push(`${w}.constraints.overlapAllowed:写元件名的数组`)
|
|
157
|
+
// '*'(0.8.0 起):任何元件压在下面都行,连运营自己加的也算。给本来就盖在运营元件上面的层用(揭晓时的压暗层、
|
|
158
|
+
// 白色页头,Logo Wall 首例);单独写,不和元件名混写
|
|
159
|
+
else if (c.overlapAllowed.includes('*')) { if (c.overlapAllowed.length > 1) errors.push(`${w}.constraints.overlapAllowed:写了 '*' 就只写它,不再列元件名`) }
|
|
155
160
|
else for (const o of c.overlapAllowed) if (!names.has(o) || o === name) errors.push(`${w}.constraints.overlapAllowed:${o} 不是本模块的另一个元件`)
|
|
156
161
|
}
|
|
157
162
|
}
|
package/src/exits.d.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import type { JsonSchema } from './validate';
|
|
2
|
-
|
|
3
|
-
type Scalar = string | number | boolean | null;
|
|
4
|
-
|
|
5
|
-
/** 固定出口:when 里的路径(点号分隔,从请求体算起)都等于给的值才有这个出口;模板只能写原文 */
|
|
6
|
-
export interface FixedExitRule {
|
|
7
|
-
id: string;
|
|
8
|
-
event: string;
|
|
9
|
-
name: string | readonly string[];
|
|
10
|
-
when?: Record<string, Scalar>;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 列表里每一项一个出口。each 是请求体里列表的路径(如 'detail.numbers'),where 里的字段都相等的项才算。
|
|
15
|
-
* id、event、name 是模板:{字段} 换成这一项的字段值(去掉首尾空白),{index} 是在整个列表里的序号,{n} 是在通过 where 的项里的序号(从 1 起);
|
|
16
|
-
* 有一处换成空串整个模板就算空,name 给数组时取第一个不空的。
|
|
17
|
-
*/
|
|
18
|
-
export interface EachExitRule {
|
|
19
|
-
each: string;
|
|
20
|
-
where?: Record<string, Scalar>;
|
|
21
|
-
id: string;
|
|
22
|
-
event: string;
|
|
23
|
-
name: string | readonly string[];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type DynamicExitRule = FixedExitRule | EachExitRule;
|
|
27
|
-
|
|
28
|
-
/** 算出来的一个出口,对应配置页推给宿主 setAccessDynamicPorts 的 { key, event, name } */
|
|
29
|
-
export interface DynamicExit {
|
|
30
|
-
id: string;
|
|
31
|
-
event: string;
|
|
32
|
-
name: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** 按规则从(保存后读回的)请求体算出口;id 为空、event 为空、id 重复的跳过,与 Builder 相同 */
|
|
36
|
-
export declare function computeExits(rules: readonly DynamicExitRule[] | undefined, body: unknown): DynamicExit[];
|
|
37
|
-
/** 检查规则写法;给了字段说明与出厂请求体时再核对路径、字段与算出的出口 id。返回问题清单,空数组 = 通过 */
|
|
38
|
-
export declare function checkExitRules(rules: unknown, options?: { schema?: JsonSchema; body?: unknown }): string[];
|
|
1
|
+
import type { JsonSchema } from './validate';
|
|
2
|
+
|
|
3
|
+
type Scalar = string | number | boolean | null;
|
|
4
|
+
|
|
5
|
+
/** 固定出口:when 里的路径(点号分隔,从请求体算起)都等于给的值才有这个出口;模板只能写原文 */
|
|
6
|
+
export interface FixedExitRule {
|
|
7
|
+
id: string;
|
|
8
|
+
event: string;
|
|
9
|
+
name: string | readonly string[];
|
|
10
|
+
when?: Record<string, Scalar>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 列表里每一项一个出口。each 是请求体里列表的路径(如 'detail.numbers'),where 里的字段都相等的项才算。
|
|
15
|
+
* id、event、name 是模板:{字段} 换成这一项的字段值(去掉首尾空白),{index} 是在整个列表里的序号,{n} 是在通过 where 的项里的序号(从 1 起);
|
|
16
|
+
* 有一处换成空串整个模板就算空,name 给数组时取第一个不空的。
|
|
17
|
+
*/
|
|
18
|
+
export interface EachExitRule {
|
|
19
|
+
each: string;
|
|
20
|
+
where?: Record<string, Scalar>;
|
|
21
|
+
id: string;
|
|
22
|
+
event: string;
|
|
23
|
+
name: string | readonly string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type DynamicExitRule = FixedExitRule | EachExitRule;
|
|
27
|
+
|
|
28
|
+
/** 算出来的一个出口,对应配置页推给宿主 setAccessDynamicPorts 的 { key, event, name } */
|
|
29
|
+
export interface DynamicExit {
|
|
30
|
+
id: string;
|
|
31
|
+
event: string;
|
|
32
|
+
name: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 按规则从(保存后读回的)请求体算出口;id 为空、event 为空、id 重复的跳过,与 Builder 相同 */
|
|
36
|
+
export declare function computeExits(rules: readonly DynamicExitRule[] | undefined, body: unknown): DynamicExit[];
|
|
37
|
+
/** 检查规则写法;给了字段说明与出厂请求体时再核对路径、字段与算出的出口 id。返回问题清单,空数组 = 通过 */
|
|
38
|
+
export declare function checkExitRules(rules: unknown, options?: { schema?: JsonSchema; body?: unknown }): string[];
|
package/src/exits.mjs
CHANGED
|
@@ -1,129 +1,129 @@
|
|
|
1
|
-
// 动态出口规则(0.4.0 起):有的模块的出口不是写死在模块目录里的,而是配置页按内容配置算出来、
|
|
2
|
-
// 保存时经宿主 setAccessDynamicPorts 推给 Builder 的(Dial Pad 每个号码一个、Access Time 每个放行规则组一个……)。
|
|
3
|
-
// 模块在 content-config.ts 里用数据把同一套算法写出来(dynamicExits),建活动 skill 拿保存后读回的请求体照着算,
|
|
4
|
-
// 不执行模块代码,也就不用打开配置页。
|
|
5
|
-
//
|
|
6
|
-
// 一条规则是下面两种之一:
|
|
7
|
-
// { id, event, name, when? } 固定出口。when 是 { 请求体里的路径: 值 },这些路径上的值都等于给的值才有这个出口
|
|
8
|
-
// { each, where?, id, event, name } 列表里每一项一个出口。each 是请求体里列表的路径(点号分隔,如 detail.numbers);
|
|
9
|
-
// where 是 { 项里的字段: 值 },都相等的项才算
|
|
10
|
-
// id、event、name 都是模板:{字段} 换成这一项里那个字段的值(转成字符串、去掉首尾空白),{index} 是这一项在整个列表里的
|
|
11
|
-
// 序号,{n} 是它在通过 where 的项里的序号(都从 1 起)。固定出口的模板只能写原文。模板里只要有一处换成了空串,
|
|
12
|
-
// 整个模板就算空;name 可以给数组,按顺序取第一个不空的。
|
|
13
|
-
// 出口按规则的顺序、列表的顺序排;id 为空、event 为空、id 与前面重复的跳过(与 Builder 收到出口时的做法相同,
|
|
14
|
-
// qik-sense-partner src/views/flow-items/content-config.vue 的 normalizeAccessDynamicPorts)。
|
|
15
|
-
|
|
16
|
-
const PLACEHOLDER = /\{([^{}]+)\}/g
|
|
17
|
-
const RULE_KEYS = new Set(['each', 'where', 'when', 'id', 'event', 'name'])
|
|
18
|
-
|
|
19
|
-
function at(obj, path) {
|
|
20
|
-
let cur = obj
|
|
21
|
-
for (const seg of path.split('.')) {
|
|
22
|
-
if (cur === null || typeof cur !== 'object') return undefined
|
|
23
|
-
cur = cur[seg]
|
|
24
|
-
}
|
|
25
|
-
return cur
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const matches = (obj, cond, get) => Object.entries(cond ?? {}).every(([k, v]) => get(obj, k) === v)
|
|
29
|
-
|
|
30
|
-
function fill(template, item, index, n) {
|
|
31
|
-
let empty = false
|
|
32
|
-
const out = template.replace(PLACEHOLDER, (_, field) => {
|
|
33
|
-
const v = field === 'index' ? index : field === 'n' ? n : item?.[field]
|
|
34
|
-
const s = v === undefined || v === null ? '' : String(v).trim()
|
|
35
|
-
if (!s) empty = true
|
|
36
|
-
return s
|
|
37
|
-
})
|
|
38
|
-
return empty ? '' : out.trim()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const firstFilled = (names, item, index, n) => {
|
|
42
|
-
for (const t of Array.isArray(names) ? names : [names]) {
|
|
43
|
-
const s = fill(t, item, index, n)
|
|
44
|
-
if (s) return s
|
|
45
|
-
}
|
|
46
|
-
return ''
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// 按规则算出口:[{ id, event, name }],与配置页推给 Builder 的 key、event、name 一一对应
|
|
50
|
-
export function computeExits(rules, body) {
|
|
51
|
-
const out = []
|
|
52
|
-
const seen = new Set()
|
|
53
|
-
const push = (id, event, name) => {
|
|
54
|
-
if (!id || !event || seen.has(id)) return
|
|
55
|
-
seen.add(id)
|
|
56
|
-
out.push({ id, event, name: name || event })
|
|
57
|
-
}
|
|
58
|
-
for (const rule of rules ?? []) {
|
|
59
|
-
if (rule.each === undefined) {
|
|
60
|
-
if (matches(body, rule.when, at)) push(fill(rule.id), fill(rule.event), firstFilled(rule.name))
|
|
61
|
-
continue
|
|
62
|
-
}
|
|
63
|
-
const list = at(body, rule.each)
|
|
64
|
-
if (!Array.isArray(list)) continue
|
|
65
|
-
let n = 0
|
|
66
|
-
list.forEach((item, i) => {
|
|
67
|
-
if (!matches(item, rule.where, (o, k) => o?.[k])) return
|
|
68
|
-
n += 1
|
|
69
|
-
push(fill(rule.id, item, i + 1, n), fill(rule.event, item, i + 1, n), firstFilled(rule.name, item, i + 1, n))
|
|
70
|
-
})
|
|
71
|
-
}
|
|
72
|
-
return out
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// 字段说明里某个路径(点号分隔)对应的子说明;走不下去时给 undefined
|
|
76
|
-
function schemaAt(schema, path) {
|
|
77
|
-
let cur = schema
|
|
78
|
-
for (const seg of path.split('.')) {
|
|
79
|
-
cur = cur?.properties?.[seg]
|
|
80
|
-
if (!cur) return undefined
|
|
81
|
-
}
|
|
82
|
-
return cur
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// 检查规则的写法;给了字段说明与出厂请求体,再核对路径与字段都对得上、出厂请求体能算出合法的出口。返回问题清单
|
|
86
|
-
export function checkExitRules(rules, { schema, body } = {}) {
|
|
87
|
-
const errors = []
|
|
88
|
-
if (!Array.isArray(rules)) return ['dynamicExits 要写成数组']
|
|
89
|
-
rules.forEach((rule, i) => {
|
|
90
|
-
const at$ = `dynamicExits[${i}]`
|
|
91
|
-
if (!rule || typeof rule !== 'object' || Array.isArray(rule)) return errors.push(`${at$} 要写成对象`)
|
|
92
|
-
for (const k of Object.keys(rule)) if (!RULE_KEYS.has(k)) errors.push(`${at$} 有不认识的键 ${k}`)
|
|
93
|
-
const isEach = rule.each !== undefined
|
|
94
|
-
if (isEach && typeof rule.each !== 'string') errors.push(`${at$}.each 要写列表的路径(字符串)`)
|
|
95
|
-
if (isEach && rule.when !== undefined) errors.push(`${at$} 按列表出的出口用 where,不用 when`)
|
|
96
|
-
if (!isEach && rule.where !== undefined) errors.push(`${at$} 固定出口用 when,不用 where`)
|
|
97
|
-
for (const k of ['where', 'when']) {
|
|
98
|
-
const c = rule[k]
|
|
99
|
-
if (c === undefined) continue
|
|
100
|
-
if (!c || typeof c !== 'object' || Array.isArray(c)) { errors.push(`${at$}.${k} 要写成 { 字段: 值 }`); continue }
|
|
101
|
-
for (const [f, v] of Object.entries(c)) if (v !== null && typeof v === 'object') errors.push(`${at$}.${k}.${f} 的值只能是字符串、数字、布尔或 null`)
|
|
102
|
-
}
|
|
103
|
-
const names = Array.isArray(rule.name) ? rule.name : [rule.name]
|
|
104
|
-
const templates = [['id', rule.id], ['event', rule.event], ...names.map((t, j) => [Array.isArray(rule.name) ? `name[${j}]` : 'name', t])]
|
|
105
|
-
if (!names.length) errors.push(`${at$}.name 不能是空数组`)
|
|
106
|
-
for (const [k, t] of templates) {
|
|
107
|
-
if (typeof t !== 'string' || !t.trim()) { errors.push(`${at$}.${k} 要写成非空字符串`); continue }
|
|
108
|
-
const fields = [...t.matchAll(PLACEHOLDER)].map((m) => m[1])
|
|
109
|
-
if (!isEach && fields.length) errors.push(`${at$}.${k} 是固定出口,不能用 {${fields[0]}}`)
|
|
110
|
-
if (isEach && schema && typeof rule.each === 'string') {
|
|
111
|
-
const props = schemaAt(schema, rule.each)?.items?.properties
|
|
112
|
-
for (const f of fields) if (props && f !== 'index' && f !== 'n' && !props[f]) errors.push(`${at$}.${k} 里的 {${f}} 不是 ${rule.each} 列表项的字段`)
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (isEach && schema && typeof rule.each === 'string') {
|
|
116
|
-
const list = schemaAt(schema, rule.each)
|
|
117
|
-
if (!list) errors.push(`${at$}.each:字段说明里没有 ${rule.each}`)
|
|
118
|
-
else if (!list.items) errors.push(`${at$}.each:${rule.each} 在字段说明里不是列表`)
|
|
119
|
-
const props = list?.items?.properties
|
|
120
|
-
for (const f of Object.keys(rule.where ?? {})) if (props && !props[f]) errors.push(`${at$}.where 里的 ${f} 不是 ${rule.each} 列表项的字段`)
|
|
121
|
-
}
|
|
122
|
-
if (!isEach && schema) for (const p of Object.keys(rule.when ?? {})) if (!schemaAt(schema, p)) errors.push(`${at$}.when:字段说明里没有 ${p}`)
|
|
123
|
-
})
|
|
124
|
-
if (!errors.length && body !== undefined) {
|
|
125
|
-
// 连线的键按「源-模块-端口-目标」用横杠拆开(Builder 与后端都这样读),所以出口 id 里不能有横杠
|
|
126
|
-
for (const e of computeExits(rules, body)) if (e.id.includes('-')) errors.push(`出厂请求体算出的出口 id「${e.id}」里有横杠,连线的键会拆错`)
|
|
127
|
-
}
|
|
128
|
-
return errors
|
|
129
|
-
}
|
|
1
|
+
// 动态出口规则(0.4.0 起):有的模块的出口不是写死在模块目录里的,而是配置页按内容配置算出来、
|
|
2
|
+
// 保存时经宿主 setAccessDynamicPorts 推给 Builder 的(Dial Pad 每个号码一个、Access Time 每个放行规则组一个……)。
|
|
3
|
+
// 模块在 content-config.ts 里用数据把同一套算法写出来(dynamicExits),建活动 skill 拿保存后读回的请求体照着算,
|
|
4
|
+
// 不执行模块代码,也就不用打开配置页。
|
|
5
|
+
//
|
|
6
|
+
// 一条规则是下面两种之一:
|
|
7
|
+
// { id, event, name, when? } 固定出口。when 是 { 请求体里的路径: 值 },这些路径上的值都等于给的值才有这个出口
|
|
8
|
+
// { each, where?, id, event, name } 列表里每一项一个出口。each 是请求体里列表的路径(点号分隔,如 detail.numbers);
|
|
9
|
+
// where 是 { 项里的字段: 值 },都相等的项才算
|
|
10
|
+
// id、event、name 都是模板:{字段} 换成这一项里那个字段的值(转成字符串、去掉首尾空白),{index} 是这一项在整个列表里的
|
|
11
|
+
// 序号,{n} 是它在通过 where 的项里的序号(都从 1 起)。固定出口的模板只能写原文。模板里只要有一处换成了空串,
|
|
12
|
+
// 整个模板就算空;name 可以给数组,按顺序取第一个不空的。
|
|
13
|
+
// 出口按规则的顺序、列表的顺序排;id 为空、event 为空、id 与前面重复的跳过(与 Builder 收到出口时的做法相同,
|
|
14
|
+
// qik-sense-partner src/views/flow-items/content-config.vue 的 normalizeAccessDynamicPorts)。
|
|
15
|
+
|
|
16
|
+
const PLACEHOLDER = /\{([^{}]+)\}/g
|
|
17
|
+
const RULE_KEYS = new Set(['each', 'where', 'when', 'id', 'event', 'name'])
|
|
18
|
+
|
|
19
|
+
function at(obj, path) {
|
|
20
|
+
let cur = obj
|
|
21
|
+
for (const seg of path.split('.')) {
|
|
22
|
+
if (cur === null || typeof cur !== 'object') return undefined
|
|
23
|
+
cur = cur[seg]
|
|
24
|
+
}
|
|
25
|
+
return cur
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const matches = (obj, cond, get) => Object.entries(cond ?? {}).every(([k, v]) => get(obj, k) === v)
|
|
29
|
+
|
|
30
|
+
function fill(template, item, index, n) {
|
|
31
|
+
let empty = false
|
|
32
|
+
const out = template.replace(PLACEHOLDER, (_, field) => {
|
|
33
|
+
const v = field === 'index' ? index : field === 'n' ? n : item?.[field]
|
|
34
|
+
const s = v === undefined || v === null ? '' : String(v).trim()
|
|
35
|
+
if (!s) empty = true
|
|
36
|
+
return s
|
|
37
|
+
})
|
|
38
|
+
return empty ? '' : out.trim()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const firstFilled = (names, item, index, n) => {
|
|
42
|
+
for (const t of Array.isArray(names) ? names : [names]) {
|
|
43
|
+
const s = fill(t, item, index, n)
|
|
44
|
+
if (s) return s
|
|
45
|
+
}
|
|
46
|
+
return ''
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 按规则算出口:[{ id, event, name }],与配置页推给 Builder 的 key、event、name 一一对应
|
|
50
|
+
export function computeExits(rules, body) {
|
|
51
|
+
const out = []
|
|
52
|
+
const seen = new Set()
|
|
53
|
+
const push = (id, event, name) => {
|
|
54
|
+
if (!id || !event || seen.has(id)) return
|
|
55
|
+
seen.add(id)
|
|
56
|
+
out.push({ id, event, name: name || event })
|
|
57
|
+
}
|
|
58
|
+
for (const rule of rules ?? []) {
|
|
59
|
+
if (rule.each === undefined) {
|
|
60
|
+
if (matches(body, rule.when, at)) push(fill(rule.id), fill(rule.event), firstFilled(rule.name))
|
|
61
|
+
continue
|
|
62
|
+
}
|
|
63
|
+
const list = at(body, rule.each)
|
|
64
|
+
if (!Array.isArray(list)) continue
|
|
65
|
+
let n = 0
|
|
66
|
+
list.forEach((item, i) => {
|
|
67
|
+
if (!matches(item, rule.where, (o, k) => o?.[k])) return
|
|
68
|
+
n += 1
|
|
69
|
+
push(fill(rule.id, item, i + 1, n), fill(rule.event, item, i + 1, n), firstFilled(rule.name, item, i + 1, n))
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
return out
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 字段说明里某个路径(点号分隔)对应的子说明;走不下去时给 undefined
|
|
76
|
+
function schemaAt(schema, path) {
|
|
77
|
+
let cur = schema
|
|
78
|
+
for (const seg of path.split('.')) {
|
|
79
|
+
cur = cur?.properties?.[seg]
|
|
80
|
+
if (!cur) return undefined
|
|
81
|
+
}
|
|
82
|
+
return cur
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 检查规则的写法;给了字段说明与出厂请求体,再核对路径与字段都对得上、出厂请求体能算出合法的出口。返回问题清单
|
|
86
|
+
export function checkExitRules(rules, { schema, body } = {}) {
|
|
87
|
+
const errors = []
|
|
88
|
+
if (!Array.isArray(rules)) return ['dynamicExits 要写成数组']
|
|
89
|
+
rules.forEach((rule, i) => {
|
|
90
|
+
const at$ = `dynamicExits[${i}]`
|
|
91
|
+
if (!rule || typeof rule !== 'object' || Array.isArray(rule)) return errors.push(`${at$} 要写成对象`)
|
|
92
|
+
for (const k of Object.keys(rule)) if (!RULE_KEYS.has(k)) errors.push(`${at$} 有不认识的键 ${k}`)
|
|
93
|
+
const isEach = rule.each !== undefined
|
|
94
|
+
if (isEach && typeof rule.each !== 'string') errors.push(`${at$}.each 要写列表的路径(字符串)`)
|
|
95
|
+
if (isEach && rule.when !== undefined) errors.push(`${at$} 按列表出的出口用 where,不用 when`)
|
|
96
|
+
if (!isEach && rule.where !== undefined) errors.push(`${at$} 固定出口用 when,不用 where`)
|
|
97
|
+
for (const k of ['where', 'when']) {
|
|
98
|
+
const c = rule[k]
|
|
99
|
+
if (c === undefined) continue
|
|
100
|
+
if (!c || typeof c !== 'object' || Array.isArray(c)) { errors.push(`${at$}.${k} 要写成 { 字段: 值 }`); continue }
|
|
101
|
+
for (const [f, v] of Object.entries(c)) if (v !== null && typeof v === 'object') errors.push(`${at$}.${k}.${f} 的值只能是字符串、数字、布尔或 null`)
|
|
102
|
+
}
|
|
103
|
+
const names = Array.isArray(rule.name) ? rule.name : [rule.name]
|
|
104
|
+
const templates = [['id', rule.id], ['event', rule.event], ...names.map((t, j) => [Array.isArray(rule.name) ? `name[${j}]` : 'name', t])]
|
|
105
|
+
if (!names.length) errors.push(`${at$}.name 不能是空数组`)
|
|
106
|
+
for (const [k, t] of templates) {
|
|
107
|
+
if (typeof t !== 'string' || !t.trim()) { errors.push(`${at$}.${k} 要写成非空字符串`); continue }
|
|
108
|
+
const fields = [...t.matchAll(PLACEHOLDER)].map((m) => m[1])
|
|
109
|
+
if (!isEach && fields.length) errors.push(`${at$}.${k} 是固定出口,不能用 {${fields[0]}}`)
|
|
110
|
+
if (isEach && schema && typeof rule.each === 'string') {
|
|
111
|
+
const props = schemaAt(schema, rule.each)?.items?.properties
|
|
112
|
+
for (const f of fields) if (props && f !== 'index' && f !== 'n' && !props[f]) errors.push(`${at$}.${k} 里的 {${f}} 不是 ${rule.each} 列表项的字段`)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (isEach && schema && typeof rule.each === 'string') {
|
|
116
|
+
const list = schemaAt(schema, rule.each)
|
|
117
|
+
if (!list) errors.push(`${at$}.each:字段说明里没有 ${rule.each}`)
|
|
118
|
+
else if (!list.items) errors.push(`${at$}.each:${rule.each} 在字段说明里不是列表`)
|
|
119
|
+
const props = list?.items?.properties
|
|
120
|
+
for (const f of Object.keys(rule.where ?? {})) if (props && !props[f]) errors.push(`${at$}.where 里的 ${f} 不是 ${rule.each} 列表项的字段`)
|
|
121
|
+
}
|
|
122
|
+
if (!isEach && schema) for (const p of Object.keys(rule.when ?? {})) if (!schemaAt(schema, p)) errors.push(`${at$}.when:字段说明里没有 ${p}`)
|
|
123
|
+
})
|
|
124
|
+
if (!errors.length && body !== undefined) {
|
|
125
|
+
// 连线的键按「源-模块-端口-目标」用横杠拆开(Builder 与后端都这样读),所以出口 id 里不能有横杠
|
|
126
|
+
for (const e of computeExits(rules, body)) if (e.id.includes('-')) errors.push(`出厂请求体算出的出口 id「${e.id}」里有横杠,连线的键会拆错`)
|
|
127
|
+
}
|
|
128
|
+
return errors
|
|
129
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { default, buildContentConfig, loadSpec, checkLabels, collectLabels, FORMAT } from './vite-plugin.mjs'
|
|
2
|
-
export { validate, SUPPORTED_KEYWORDS } from './validate.mjs'
|
|
3
|
-
export { computeExits, checkExitRules } from './exits.mjs'
|
|
1
|
+
export { default, buildContentConfig, loadSpec, checkLabels, collectLabels, FORMAT } from './vite-plugin.mjs'
|
|
2
|
+
export { validate, SUPPORTED_KEYWORDS } from './validate.mjs'
|
|
3
|
+
export { computeExits, checkExitRules } from './exits.mjs'
|