规则模板系统完整文档
一、脚本流水线
所有脚本位于 scripts/ 目录,按执行顺序排列:
| 脚本 | 用途 | 执行时机 |
|------|------|---------|
| import-aml-v2.js | 基础 AML 导入(ItemType/Method/Relationship/Permission/List/Lifecycle/Sequence/Workflow/Form) | 首次初始化 |
| extract-extended-aml.js | 扩展提取(Identity/PermissionAccess/WorkflowActivities/LifecycleTransitions/Variables/Revisions/FormFields/ItemTypeBindings/MethodRules/RelationshipViews) | 基础导入后 |
| generate-operation-rules.js | 规则操作分类 + 关系规则 + 方法触发规则 + 操作提示词生成 | 扩展提取后 |
1.1 import-aml-v2.js
# 全量重建(清空16张主表后重新导入)
node scripts/import-aml-v2.js --full
# 增量更新(只处理新文件)
node scripts/import-aml-v2.js
# 仅修补跨文件关联和模板回填
node scripts/import-aml-v2.js --patch
# 仅生成业务系统提示词
node scripts/import-aml-v2.js --generate-prompts
生成的表(23张):
sciot_item_types— 对象类型定义sciot_properties(含 data_source/data_source_type/data_source_name/item_behavior 列)— 属性定义sciot_methods— 方法定义sciot_relationships(含 create_related/is_dependent/rel_item_type_label/rel_properties)— 关系类型sciot_permissions— 权限定义sciot_lists+sciot_list_values— 列表枚举sciot_lifecycle_maps+sciot_lifecycle_states— 生命周期sciot_sequences— 编号规则sciot_workflow_maps— 工作流映射sciot_forms— 表单定义sciot_rules— 自动生成的验证规则sciot_templates(含 lifecycle/lifecycle_start_state/generation_rules/relationship_types)— 对象创建模板sciot_meta_models+sciot_meta_model_properties+sciot_meta_model_events— 元模型sciot_business_systems+sciot_business_objects+sciot_business_prompts— 业务系统sciot_creation_logs— 创建日志
规则生成(generateEnhancedRules):对每个有属性的 ItemType:
- 必填规则(is_required=1 → severity='error')
- 数据类型验证规则(severity='warning')
- 长度限制规则(stored_length>0 → severity='warning')
- 模式验证规则(pattern 非空 → severity='info')
- 生命周期规则(每条一条)
- 关系规则(每条关系一条)
模板生成(backfillTemplates):对每个 ItemType 回填 sciot_templates:
- 字段分类(LLM可填字段/系统自动字段/必填字段/可选字段)
- 生命周期绑定
- 关系类型列表
- generation_rules JSON(含 child_objects/nested_depth/sequence_name)
1.2 extract-extended-aml.js
# 全量重建(删除13张扩展表后重建)
node scripts/extract-extended-aml.js --full
# 仅处理特定类型
node scripts/extract-extended-aml.js --type identity
生成的表(13张):
| 表名 | 说明 |
|------|------|
| sciot_identities | 用户/角色/组定义 |
| sciot_permission_access | 权限访问规则(can_get/update/delete/discover/change_access) |
| sciot_workflow_activities | 工作流活动节点 |
| sciot_workflow_paths | 工作流路径 |
| sciot_activity_assignments | 活动分配 |
| sciot_activity_transitions | 活动触发转换 |
| sciot_lifecycle_transitions | 生命周期状态转换 |
| sciot_variables | 全局变量 |
| sciot_revisions | 版本规则 |
| sciot_form_fields | 表单字段布局 |
| sciot_itemtype_bindings | ItemType 绑定(Life Cycle/Workflow/Permission/Can Add/TOC Access/View) |
| sciot_method_rules | 方法业务逻辑解析(12种规则模式) |
| sciot_relationship_views | 关系视图 |
规则生成(generateRulesFromExtendedData):从 Permission/Lifecycle/Workflow/Method 数据生成规则写入 sciot_rules。
1.3 generate-operation-rules.js
# 全量重建(清理旧分类和规则后重新生成)
node scripts/generate-operation-rules.js --full
# 仅分析不写入
node scripts/generate-operation-rules.js --dry-run
执行步骤:
tagExistingRules()— 给sciot_rules打operation_type标签(rule_type→add/edit/get 映射)generateRelationshipRules()— 从sciot_relationships生成关系处理规则generateMethodTriggerRules()— 从sciot_method_rules生成方法触发规则generateOperationPrompts()— 为每个 ItemType × 3种操作类型生成预定义提示词到prompt_tablestagExistingPrompts()— 更新现有prompt_templates的operation_type
--full 模式清理逻辑:
- 删除
sciot_rules中 category='relationship' 和 category='method' 的规则 - 重置
sciot_rules的operation_type为空 - 删除
prompt_templates中 id 以 'prompt-' 开头的自动生成提示词
二、数据库表结构
2.1 sciot_rules(主规则表)
| 列名 | 类型 | 说明 |
|---|---|---|
| id | INTEGER PK | 自增主键 |
| rule_type | TEXT | 规则类型:required/pattern/validation/range/length/lifecycle/relationship/inspect/auto_create_related/state_transition/permission_check/notification/data_calculation/copy_clone/file_attachment/query_search/relationship_operation |
| data_type | TEXT | 关联数据类型 |
| property_name | TEXT | 关联属性名 |
| property_label | TEXT | 属性标签 |
| rule_pattern | TEXT | 正则/值列表模式 |
| description | TEXT | 规则描述(前端展示) |
| llm_instruction | TEXT | LLM 指令 |
| item_type_name | TEXT | 所属对象类型 |
| priority | INTEGER | 优先级 |
| severity | TEXT | error/warning/info |
| category | TEXT | data/relationship/lifecycle/method/permission/workflow |
| is_active | INTEGER | 1=启用 0=禁用 |
| operation_type | TEXT | 逗号分隔的操作类型:add/edit/get |
2.2 prompt_templates(提示词表,替代废弃的 sciot_prompts)
| 列名 | 类型 | 说明 |
|---|---|---|
| id | TEXT PK | 'prompt-{opType}-{typeName}' 格式 |
| name | TEXT | 提示词名称 |
| content | TEXT | 提示词内容(对应前端 user_prompt_template) |
| prompt_type | TEXT | creation/edit/get |
| item_type_name | TEXT | 所属对象类型 |
| operation_type | TEXT | add/edit/get |
| version | INTEGER | 版本号 |
| status | TEXT | active/inactive |
三、后端 API
3.1 统一 Schema API
GET /api/aml/unified/schema/:itemType
返回:
{
"itemType": "Project",
"properties": { "name": { "type": "string", "isRequired": true, ... }, ... },
"llmFields": ["name", "description", ...],
"autoFields": ["id", "created_by_id", ...],
"requiredFields": ["name"],
"rules": [
{
"id": 1,
"rule_type": "required",
"property_name": "name",
"rule_description": "名称不能为空",
"description": "名称不能为空",
"severity": "error",
"pattern": null,
"is_active": 1,
"operation_type": "add,edit,get"
}
],
"template": { "generation_rules": {...}, ... }
}
3.2 提示词 API
GET /api/aml/sciot/prompts?operation_type=add&item_type=Project&page=1&limit=50
operation_type过滤:匹配operation_type = 'add'或在逗号分隔列表中的任意位置- 返回分页数据
POST /api/aml/sciot/prompts/update — 写入 prompt_templates.content
{ "item_type_name": "Project", "user_prompt_template": "..." }
3.3 规则 CRUD API
GET /api/aml/sciot/rules?item_type=Project&operation_type=add&page=1&limit=50
POST /api/aml/sciot/rules/add — 新增规则到 sciot_rules
POST /api/aml/sciot/rules/update — 更新规则
DELETE /api/aml/sciot/rules?id=X — 删除规则
3.4 规则统计
GET /api/aml/sciot/rules/stats
返回 total/active/by_type/by_severity/by_category
四、前端使用
4.1 创建流程(StaffCapabilities.vue → runCreate())
1. fetch Schema → /api/aml/unified/schema/:itemType
2. fetch TypeTemplate → 属性/关系类型
3. fetch Prompts → /api/aml/sciot/prompts(仅一次)
4. 组装 Prompt → 发给 LLM
5. sanitizeProperties → 修正字段值(list/integer/date 等)
6. validateProperties → 规则验证
7. 生成 AML
8. 创建关系(仅 create_related=1 的关系)
4.2 规则验证(rule-validator.js → validateProperties)
支持的 rule_type:
| 类型 | 行为 | 示例 |
|------|------|------|
| required | 值缺失时报 error | 名称必填 |
| pattern | 值不匹配模式时 warning | 格式不正确 |
| validation | 输出 rule_description 作为 warning | 自定义验证逻辑 |
| range | 有值时输出 warning | 值范围约束 |
| length | 超出字符限制时 warning | 长度不能超过 N |
| inspect | 有值时输出 warning | 需人工检查 |
| lifecycle | 输出 rule_description | 生命周期规则 |
| state_transition | 输出 rule_description | 状态转换规则 |
| permission_check | 输出 rule_description | 权限检查规则 |
4.3 create_related 过滤
创建流程 Step 8 仅自动创建 create_related=1 的关系。LLM 输出的关系与 relTypes(从 typeTemplate API 获取)中定义的 createRelated 字段匹配。
五、数据流向
SCIOT/ 目录 AML 文件
↓
import-aml-v2.js(基础导入:23张表 + 规则 + 模板)
↓
extract-extended-aml.js(扩展提取:13张表 + 额外规则)
↓
generate-operation-rules.js(operation_type分类 + 关系规则 + 方法规则 + 提示词生成)
↓
前端使用(StaffCapabilities.vue)
├─ /api/aml/unified/schema/:type → properties + rules + template
├─ /api/aml/sciot/prompts?operation_type=add → 提示词
├─ sanitizeProperties → 字段修正
└─ validateProperties → 规则验证(required/pattern/validation/range/length/inspect...)
六、快速参考
6.1 全量重建
node scripts/import-aml-v2.js --full
node scripts/extract-extended-aml.js --full
node scripts/generate-operation-rules.js --full
6.2 常用 SQL
-- 某类型所有规则(按操作类型分组)
SELECT operation_type, rule_type, COUNT(*)
FROM sciot_rules WHERE item_type_name = 'Project' AND is_active = 1
GROUP BY operation_type, rule_type;
-- 某类型的关系规则
SELECT * FROM sciot_rules
WHERE item_type_name = 'Project' AND category = 'relationship';
-- 某类型的提示词
SELECT operation_type, name, LENGTH(content) as len
FROM prompt_templates
WHERE item_type_name = 'Project' AND status = 'active';
-- 某类型可自动创建的关系
SELECT * FROM sciot_relationships
WHERE (name = 'Project' OR related_item_type = 'Project')
AND create_related = 1;
6.3 新增操作类型
如需支持新的操作类型(如 delete):
- 修改
scripts/generate-operation-rules.js中的ruleToOp映射 - 在
generateOperationPrompts()中添加新操作类型的提示词模板 node scripts/generate-operation-rules.js --full
BossAgents