@realnation/content-config 0.7.0 → 0.9.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 CHANGED
@@ -65,6 +65,45 @@ export default {
65
65
  - 出口规则算法在 `@realnation/content-config/exits`(`computeExits`、`checkExitRules`),模块构建与建活动 skill 用同一份。
66
66
  - 产物 `content-config.json` 多一个可选的 `dynamicExits` 键,`format` 仍是 1:读取方不认识就当没有。
67
67
 
68
+ ## 引导步骤 bootstrap(0.9.0 起)
69
+
70
+ 有的模块保存内容配置之前,要先在活动里建一套内容、拿到编号填进请求体。例如 Digital Signage:素材库与播放列表挂在活动上
71
+ (`POST /partner/contents/digital-signage/{projectId}/signages` 回 `{ signageId }`),没有活动编号建不了,出厂默认里
72
+ `detail.signageId` 只能是空的,标着 `x-fill-before-save`。配置页是第一次打开时自己调这个接口;建活动 skill 不打开配置页,
73
+ 就照说明里的 `bootstrap` 在活动建好、存内容配置之前调一次,把返回值填进请求体再存。
74
+
75
+ ```ts
76
+ export default {
77
+ endpoint: 'common',
78
+ defaultBody: () => ({ configId: 0, detail: createEmptyDetail() }), // detail.signageId 是 null
79
+ schema: { /* detail.signageId:type integer、minimum 1、'x-fill-before-save': true */ },
80
+ bootstrap: [
81
+ {
82
+ method: 'POST',
83
+ path: '/partner/contents/digital-signage/{projectId}/signages',
84
+ body: {},
85
+ fills: { 'detail.signageId': 'signageId' },
86
+ description: 'Creates the signage content set (library, playlists, screen settings) for this campaign.'
87
+ }
88
+ ]
89
+ } satisfies ContentConfigSpec;
90
+ ```
91
+
92
+ - `method`:只认 `POST`、`PUT`。
93
+ - `path`:平台接口路径,以 `/partner/` 开头、必须带 `{projectId}`;占位只认 `{projectId}`(活动编号,`p_…`)、`{versionId}`(草稿版本编号)。
94
+ 路径照配置页调的那个接口写,接口名引用代码里的同一个常量更好。
95
+ - `body`:请求体,可省(省了发 `{}`);原样发,不带占位。
96
+ - `fills`:`{ 内容配置请求体里的路径: 返回 data 里的路径 }`,都用点号分隔;返回值取平台 `ApiResponse` 的 `data`(如 `{ data: { signageId: 12 } }` 写 `signageId`)。
97
+ 请求体那一格只能沿 `properties` 走(不能在数组、以 id 为键的表里),在字段说明里要标 `x-fill-before-save`。
98
+ - `description`:可省,英文。
99
+ - skill 的做法:校验要提交的请求体时,`fills` 指的格空着也放过(别的 `x-fill-before-save` 照样拦);活动建好后只在那一格还空着
100
+ (缺席、null、空串、空数组)时调这一步,已经填了(人用 `--config-file` 给了编号)就跳过;填好再按说明严格校验一遍才存。
101
+ 模块默认(save-default)里带着的这一格是做模板那个活动的编号,skill 换回出厂的空值,再照引导步骤给这个活动新建。
102
+ - 构建时核对:键认得、方法与路径合规、`fills` 指的格在说明里且标了 `x-fill-before-save`、同一格只填一次。不过关构建就失败。
103
+ 核对函数在 `@realnation/content-config/bootstrap`(`checkBootstrap`)。
104
+ - 产物 `content-config.json` 多一个可选的 `bootstrap` 键,`format` 仍是 1:读取方不认识就当没有(老 skill 照旧在校验处停下点名)。
105
+ - 只给「没有活动就建不了」的东西用。能放进出厂默认的值照旧写进 `defaultBody`,不要拿引导步骤绕过 `x-fill-before-save`(例如要人上传的视频)。
106
+
68
107
  ## 写字段说明的规矩
69
108
 
70
109
  - **默认值与范围引用代码里的同一份常量**(默认值函数、LIMITS、枚举表),不照抄数字;改代码说明自动跟上。
@@ -105,7 +144,7 @@ import { canvasConfig } from '@realnation/content-config/canvas';
105
144
  - 键是 componentName。`kind: 'system'` 是登记在 editor_tabs.components 的;`kind: 'pushed'` 是 page-data 推的,要写 `key`、`type`(平台元件类型,按钮是 `image-text`,0.6.2 起认全部类型)、英文 `appearsWhen`,可选机器可读的 `when`(`{ 内容配置读回的请求体里的路径: 值 }`,如 `{ 'design.wheelCount': 2 }`)。
106
145
  - 面板项的类型、默认值、选项、范围不写,以后台登记的 collocation 为准;`properties` 只写模块真读的属性的英文说明(宿主统一套在外框上的位置、大小、颜色、字体不列),`ignores` 列面板上有、模块不读的。
107
146
  - `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`、`face`(盒子里真正画东西的是贴着远离某条边的正方形,如转盘的圆,`anchorsTo` 对准它的中心,`aspect` 在那条边为左右时宽高对调)。不出屏、不压角标这类平台规矩不写在这里。
147
+ - `constraints` 只写模块自己的摆放规矩:`aspect`、`minWidth`、`minHeight`、`anchorsTo`(压在谁的正中)、`overlapAllowed`(可以压在它上面的本模块元件名;0.8.0 起只写 `['*']` 表示任何元件都行,连运营自己加的也算,给本来就盖在运营元件上面的层用,如 Logo Wall 揭晓时的压暗层与白色页头)、`face`(盒子里真正画东西的是贴着远离某条边的正方形,如转盘的圆,`anchorsTo` 对准它的中心,`aspect` 在那条边为左右时宽高对调)。不出屏、不压角标这类平台规矩不写在这里。
109
148
  - `control`(0.7.0 起,可选):访客要按、要点、要靠它操作的元件写 `control: true`(左右箭头、Spin、开始、提交、点哪都算的游戏区)。建活动引擎据此不许 AI 未经用户点名拿掉、隐藏、盖住它(引擎规矩 24)。不写时引擎按出厂版面带不带键码 `key-code` 认;带键码但不算操作的写 `false`。
110
149
  - 说明文字一律英文。完整样例:体验仓 qiksense-lottery-spin-and-win-experience 的 `src/canvas-config.ts`。
111
150
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realnation/content-config",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "qiksense 模块内容配置的机器可读说明:构建时导出 content-config.json,并用字段说明校验默认请求体;0.5.0 起另导出画布说明 canvas-config.json(体验端)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -19,6 +19,10 @@
19
19
  "./canvas": {
20
20
  "types": "./src/canvas.d.ts",
21
21
  "import": "./src/canvas.mjs"
22
+ },
23
+ "./bootstrap": {
24
+ "types": "./src/bootstrap.d.ts",
25
+ "import": "./src/bootstrap.mjs"
22
26
  }
23
27
  },
24
28
  "files": [
@@ -0,0 +1,27 @@
1
+ import type { JsonSchema } from './validate';
2
+
3
+ /**
4
+ * 引导步骤(0.9.0 起):保存内容配置之前要先在活动里建一套内容的模块(如 Digital Signage 先建 signage 拿 signageId),
5
+ * 建活动 skill 在活动建好、存内容配置之前照它调一次接口,把返回 data 里的值填进请求体。fills 指的格已经填了就跳过。
6
+ */
7
+ export interface BootstrapStep {
8
+ /** 只认 POST、PUT */
9
+ method: 'POST' | 'PUT';
10
+ /** 平台接口路径,以 /partner/ 开头、带 {projectId};占位只认 {projectId}、{versionId} */
11
+ path: string;
12
+ /** 请求体,可省(省了发 {}) */
13
+ body?: Record<string, unknown>;
14
+ /** { 内容配置请求体里的路径: 返回 data 里的路径 }(点号分隔);请求体那一格要标 x-fill-before-save */
15
+ fills: Record<string, string>;
16
+ /** 英文,给人与 AI 看这一步建的是什么 */
17
+ description?: string;
18
+ }
19
+
20
+ export declare const BOOTSTRAP_METHODS: readonly ['POST', 'PUT'];
21
+ export declare const BOOTSTRAP_PLACEHOLDERS: readonly ['projectId', 'versionId'];
22
+ /** 字段说明里点号路径指的那一层(只沿 properties 走) */
23
+ export declare function schemaAt(schema: JsonSchema, path: string): JsonSchema | undefined;
24
+ /** 引导步骤要填的请求体路径 */
25
+ export declare function bootstrapTargets(steps: readonly BootstrapStep[] | undefined): string[];
26
+ /** 检查引导步骤的写法;给了字段说明时再核对 fills 指的格。返回问题清单,空数组 = 通过 */
27
+ export declare function checkBootstrap(steps: unknown, options?: { schema?: JsonSchema }): string[];
@@ -0,0 +1,73 @@
1
+ // 引导步骤(0.9.0 起):有的模块保存内容配置之前,要先在活动里建一套内容、拿到编号填进请求体
2
+ // (Digital Signage 要先 POST /partner/contents/digital-signage/{projectId}/signages 拿 signageId;signage 挂在活动上,
3
+ // 没有活动编号建不了,所以出厂默认里这一格只能是空的)。配置页是自己在第一次打开时调这个接口;
4
+ // 建活动 skill 不打开配置页,就照说明里的 bootstrap 在活动建好、存内容配置之前调一次,把返回值填进请求体。
5
+ //
6
+ // 一步写成:
7
+ // { method: 'POST', path: '/partner/contents/digital-signage/{projectId}/signages', body: {}, fills: { 'detail.signageId': 'signageId' } }
8
+ // method 只认 POST、PUT
9
+ // path 平台接口路径,以 /partner/ 开头、带 {projectId};占位只认 {projectId}、{versionId}(活动编号、草稿版本编号)
10
+ // body 请求体,可省(省了发 {});只能是原样的 JSON 对象,不带占位
11
+ // fills { 内容配置请求体里的路径: 返回 data 里的路径 }(点号分隔)。请求体那一格要在字段说明里、标了 x-fill-before-save
12
+ // description 可省,英文,给人与 AI 看这一步建的是什么
13
+ // skill 只在 fills 指的格还空着(缺席、null、空串、空数组)时调这一步;已经填了(人用 --config-file 给了编号)就跳过。
14
+
15
+ export const BOOTSTRAP_METHODS = ['POST', 'PUT']
16
+ export const BOOTSTRAP_PLACEHOLDERS = ['projectId', 'versionId']
17
+ const STEP_KEYS = new Set(['method', 'path', 'body', 'fills', 'description'])
18
+ const DOT_PATH = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$/
19
+ const PATH_CHARS = /^\/partner\/[A-Za-z0-9/_{}.-]+$/
20
+
21
+ const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v)
22
+
23
+ // 字段说明里点号路径指的那一层(只沿 properties 走;数组里的、以 id 为键的表里的格不能当引导目标)
24
+ export function schemaAt(schema, path) {
25
+ let cur = schema
26
+ for (const seg of path.split('.')) {
27
+ cur = isPlainObject(cur?.properties) ? cur.properties[seg] : undefined
28
+ if (!isPlainObject(cur)) return undefined
29
+ }
30
+ return cur
31
+ }
32
+
33
+ // 引导步骤要填的请求体路径
34
+ export const bootstrapTargets = (steps) => (Array.isArray(steps) ? steps.flatMap((s) => (isPlainObject(s?.fills) ? Object.keys(s.fills) : [])) : [])
35
+
36
+ // 检查引导步骤的写法;给了字段说明时再核对 fills 指的格在说明里、标了 x-fill-before-save。返回问题清单,空数组 = 通过
37
+ export function checkBootstrap(steps, { schema } = {}) {
38
+ const errors = []
39
+ if (!Array.isArray(steps) || !steps.length) return ['bootstrap 要写成至少一步的数组']
40
+ const seen = new Set()
41
+ steps.forEach((step, i) => {
42
+ const at = `bootstrap[${i}]`
43
+ if (!isPlainObject(step)) return errors.push(`${at}:每一步是一个对象`)
44
+ for (const k of Object.keys(step)) if (!STEP_KEYS.has(k)) errors.push(`${at}:认不出的键 ${k}(能写 ${[...STEP_KEYS].join('、')})`)
45
+ if (!BOOTSTRAP_METHODS.includes(step.method)) errors.push(`${at}.method:只认 ${BOOTSTRAP_METHODS.join('、')},收到 ${JSON.stringify(step.method)}`)
46
+ if (typeof step.path !== 'string' || !PATH_CHARS.test(step.path) || step.path.includes('..')) {
47
+ errors.push(`${at}.path:要以 /partner/ 开头,只含字母、数字、/ _ - . 与占位,收到 ${JSON.stringify(step.path)}`)
48
+ } else {
49
+ const names = [...step.path.matchAll(/\{([^{}]*)\}/g)].map((m) => m[1])
50
+ for (const n of names) if (!BOOTSTRAP_PLACEHOLDERS.includes(n)) errors.push(`${at}.path:占位只认 ${BOOTSTRAP_PLACEHOLDERS.map((p) => `{${p}}`).join('、')},收到 {${n}}`)
51
+ if (!names.includes('projectId')) errors.push(`${at}.path:要带 {projectId}(引导建的东西挂在活动上)`)
52
+ if (/[{}]/.test(step.path.replace(/\{[^{}]*\}/g, ''))) errors.push(`${at}.path:花括号没配对`)
53
+ }
54
+ if (step.body !== undefined && !isPlainObject(step.body)) errors.push(`${at}.body:要是一个 JSON 对象(可省,省了发 {})`)
55
+ if (step.description !== undefined && typeof step.description !== 'string') errors.push(`${at}.description:要是字符串`)
56
+ if (!isPlainObject(step.fills) || !Object.keys(step.fills).length) {
57
+ errors.push(`${at}.fills:写成 { 请求体里的路径: 返回 data 里的路径 },至少一项`)
58
+ return
59
+ }
60
+ for (const [target, from] of Object.entries(step.fills)) {
61
+ if (!DOT_PATH.test(target)) errors.push(`${at}.fills:请求体路径 ${JSON.stringify(target)} 要写成点号分隔的字段名(如 detail.signageId)`)
62
+ if (typeof from !== 'string' || !DOT_PATH.test(from)) errors.push(`${at}.fills.${target}:返回 data 里的路径要写成点号分隔的字段名(如 signageId),收到 ${JSON.stringify(from)}`)
63
+ if (seen.has(target)) errors.push(`${at}.fills.${target}:前面的步骤已经填这一格`)
64
+ seen.add(target)
65
+ if (schema && DOT_PATH.test(target)) {
66
+ const field = schemaAt(schema, target)
67
+ if (!field) errors.push(`${at}.fills.${target}:字段说明里沿 properties 找不到这一格`)
68
+ else if (field['x-fill-before-save'] !== true) errors.push(`${at}.fills.${target}:这一格要标 x-fill-before-save(出厂默认是空的,保存前由引导步骤填)`)
69
+ }
70
+ }
71
+ })
72
+ return errors
73
+ }
package/src/canvas.d.ts CHANGED
@@ -48,6 +48,7 @@ export interface CanvasElement {
48
48
  minWidth?: number;
49
49
  minHeight?: number;
50
50
  anchorsTo?: { element: string; at: 'center' | 'top' | 'bottom' | 'left' | 'right' };
51
+ /** 可以压在它上面的本模块元件名;只写 ['*'](0.8.0 起)表示任何元件都行,连运营自己加的也算(这一层本来就盖在运营元件上面,如揭晓时的压暗层) */
51
52
  overlapAllowed?: string[];
52
53
  /** 盒子里真正画东西的是一个正方形(如转盘的圆),贴着远离 awayFrom 属性所指那条边的一端;anchorsTo 指向它时对准正方形中心 */
53
54
  face?: { shape: 'square'; awayFrom?: string };
package/src/canvas.mjs CHANGED
@@ -154,6 +154,9 @@ export function validateCanvasConfig(spec) {
154
154
  }
155
155
  if (c.overlapAllowed !== undefined) {
156
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:写了 '*' 就只写它,不再列元件名`) }
157
160
  else for (const o of c.overlapAllowed) if (!names.has(o) || o === name) errors.push(`${w}.constraints.overlapAllowed:${o} 不是本模块的另一个元件`)
158
161
  }
159
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.d.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  import type { Plugin } from 'vite';
2
2
  import type { JsonSchema } from './validate';
3
3
  import type { DynamicExitRule } from './exits';
4
+ import type { BootstrapStep } from './bootstrap';
4
5
 
5
6
  export type { JsonSchema } from './validate';
6
7
  export { validate, SUPPORTED_KEYWORDS } from './validate';
7
8
  export type { DynamicExitRule, FixedExitRule, EachExitRule, DynamicExit } from './exits';
8
9
  export { computeExits, checkExitRules } from './exits';
10
+ export type { BootstrapStep } from './bootstrap';
11
+ export { checkBootstrap, bootstrapTargets, schemaAt, BOOTSTRAP_METHODS, BOOTSTRAP_PLACEHOLDERS } from './bootstrap';
9
12
 
10
13
  /** 模块 src/content-config.ts 默认导出的形状 */
11
14
  export interface ContentConfigSpec<Body = unknown> {
@@ -17,6 +20,8 @@ export interface ContentConfigSpec<Body = unknown> {
17
20
  schema: JsonSchema;
18
21
  /** 配置页按内容配置算出、保存时推给宿主 setAccessDynamicPorts 的出口,用数据写成的同一套算法(0.4.0 起);出口全在模块目录里的模块不写 */
19
22
  dynamicExits?: readonly DynamicExitRule[];
23
+ /** 保存前要先在活动里建一套内容的模块:建活动 skill 在活动建好、存内容配置之前照它调接口、把返回值填进请求体(0.9.0 起);不用的模块不写 */
24
+ bootstrap?: readonly BootstrapStep[];
20
25
  }
21
26
 
22
27
  /** 构建产物 content-config.json 的形状 */
@@ -27,6 +32,8 @@ export interface ContentConfigFile<Body = unknown> {
27
32
  schema: JsonSchema;
28
33
  /** 模块写了 dynamicExits 才有 */
29
34
  dynamicExits?: readonly DynamicExitRule[];
35
+ /** 模块写了 bootstrap 才有 */
36
+ bootstrap?: readonly BootstrapStep[];
30
37
  }
31
38
 
32
39
  export interface ContentConfigPluginOptions {
package/src/index.mjs CHANGED
@@ -1,3 +1,4 @@
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'
4
+ export { checkBootstrap, bootstrapTargets, schemaAt, BOOTSTRAP_METHODS, BOOTSTRAP_PLACEHOLDERS } from './bootstrap.mjs'
@@ -8,6 +8,7 @@ import { pathToFileURL } from 'node:url'
8
8
  import { validate } from './validate.mjs'
9
9
  import { checkLabels } from './labels.mjs'
10
10
  import { checkExitRules } from './exits.mjs'
11
+ import { checkBootstrap } from './bootstrap.mjs'
11
12
 
12
13
  export { checkLabels, collectLabels } from './labels.mjs'
13
14
 
@@ -53,19 +54,22 @@ export async function loadSpec(root, entry) {
53
54
  export async function buildContentConfig(root, entry = 'src/content-config.ts', { labelDirs = ['src'], labelFiles } = {}) {
54
55
  const spec = await loadSpec(root, entry)
55
56
  if (!spec || typeof spec.endpoint !== 'string' || !spec.endpoint || !spec.schema || spec.defaultBody === undefined) {
56
- throw new Error(`content-config:${entry} 要默认导出 { endpoint, defaultBody, schema }(有动态出口的再加 dynamicExits)`)
57
+ throw new Error(`content-config:${entry} 要默认导出 { endpoint, defaultBody, schema }(有动态出口的再加 dynamicExits,保存前要先建内容的再加 bootstrap)`)
57
58
  }
58
59
  const defaultBody = typeof spec.defaultBody === 'function' ? await spec.defaultBody() : spec.defaultBody
59
60
  // 出厂默认可以留空「保存前必须填」的字段(x-fill-before-save),其余照常校验
60
61
  const errors = validate(spec.schema, defaultBody, '$', { allowUnfilled: true })
61
62
  if (labelDirs.length) errors.push(...checkLabels(spec.schema, root, { dirs: labelDirs, exclude: [entry], files: labelFiles }))
62
63
  if (spec.dynamicExits !== undefined) errors.push(...checkExitRules(spec.dynamicExits, { schema: spec.schema, body: defaultBody }))
64
+ if (spec.bootstrap !== undefined) errors.push(...checkBootstrap(spec.bootstrap, { schema: spec.schema }))
63
65
  if (errors.length) {
64
66
  throw new Error(`content-config:字段说明没通过检查(${errors.length} 处)\n ${errors.join('\n ')}`)
65
67
  }
66
68
  // dynamicExits 是 0.4.0 加的可选键,格式号不变:读取方不认识就当没有(建活动 skill 读到才补出口)
67
69
  const doc = { format: FORMAT, endpoint: spec.endpoint, defaultBody, schema: spec.schema }
68
70
  if (spec.dynamicExits !== undefined) doc.dynamicExits = spec.dynamicExits
71
+ // bootstrap 是 0.9.0 加的可选键,格式号同样不变(建活动 skill 读到才在存内容配置之前照它建)
72
+ if (spec.bootstrap !== undefined) doc.bootstrap = spec.bootstrap
69
73
  return doc
70
74
  }
71
75