BossAgents 规则模板管理系统架构文档

BossAgents 规则模板管理系统架构文档

版本: 1.0 | 更新日期: 2026-05-31


1. 系统架构概览

1.1 核心理念

BossAgents 采用 "规则驱动 + LLM增强" 的架构思想:

  • 规则是确定性逻辑,负责快速判断和标准操作
  • 模板定义数据结构,确保输出格式一致
  • 提示词是LLM交互协议,让AI理解业务上下文
  • 三者共同驱动基础数字员工能力,再由业务数字员工组合调用

1.2 整体架构图

┌─────────────────────────────────────────────────────────────────────────┐
│                         业务数字员工层 (Digital Staff)                   │
│  ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐    │
│  │ DS-ECR-001   │ │ DS-VEN-001   │ │ DS-COST-001  │ │ DS-SYS-001   │    │
│  │ ECR审核员    │ │ 供应商管家   │ │ 成本优化师   │ │ 系统运维师   │    │
│  └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘    │
├─────────┼────────────────┼────────────────┼──────────────────────────────┤
│         ▼                ▼                ▼                             │
│  ┌──────────────────────────────────────────────────────────────────┐   │
│  │                 基础数字员工能力层 (Capabilities)                   │   │
│  │  ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐            │   │
│  │  │ Identify │ │ Create   │ │ Repair   │ │ Optimize │            │   │
│  │  │ 识别能力 │ │ 创建能力 │ │ 修复能力 │ │ 优化能力 │            │   │
│  │  └──────────┘ └──────────┘ └──────────┘ └──────────┘            │   │
│  │  ┌──────────┐ ┌──────────┐                                     │   │
│  │  │ Compare  │ │ Generate │   ← 统一通过 Rule Engine 执行        │   │
│  │  │ 比对能力 │ │ 生成能力 │                                     │   │
│  │  └──────────┘ └──────────┘                                     │   │
│  └──────────────────────────────────────────────────────────────────┘   │
├──────────────────────────────────────────────────────────────────────────┤
│  ┌──────────────────────────────────────────────────────────────────┐   │
│  │           配置层 ── sciot_import.db (统一配置库)                   │   │
│  │  ┌────────────────┐ ┌────────────────┐ ┌────────────────┐       │   │
│  │  │ sciot_rules_v2 │ │ sciot_templates│ │ prompt_templates│       │   │
│  │  │ 统一规则引擎    │ │ 对象模板库     │ │ 提示词模板库   │       │   │
│  │  └────────────────┘ └────────────────┘ └────────────────┘       │   │
│  │  ┌────────────────┐ ┌────────────────┐ ┌────────────────┐       │   │
│  │  │ sciot_properties│ │sciot_sequences│ │sciot_relationships│    │   │
│  │  │ 对象属性定义    │ │ 编号序列      │ │ 关系类型定义   │       │   │
│  │  └────────────────┘ └────────────────┘ └────────────────┘       │   │
│  └──────────────────────────────────────────────────────────────────┘   │
├──────────────────────────────────────────────────────────────────────────┤
│                          数据源层                                         │
│  ┌────────────────┐ ┌────────────────┐ ┌────────────────┐               │
│  │ PLM/AML XML    │ │ SCSAI 实时API   │ │ 手动创建       │               │
│  │ 导入解析       │ │ 在线同步       │ │ UI维护         │               │
│  └────────────────┘ └────────────────┘ └────────────────┘               │
└──────────────────────────────────────────────────────────────────────────┘

2. 数据流图

2.1 对象创建完整流程

用户描述 "创建项目X"
    │
    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ 1. 获取Schema                                                           │
│    GET /api/aml/unified/schema/Project                                  │
│    返回: { llmFields: [...], autoFields: [...], properties: [...] }     │
└─────────────────────────────────────────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ 2. 获取模板                                                             │
│    GET /api/sciot/type-template/Project                                 │
│    返回: { properties, relationships, list_values, lifecycle, ... }     │
└─────────────────────────────────────────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ 3. 获取/构建提示词                                                      │
│    GET /api/aml/sciot/prompts  → 优先使用预生成的提示词                  │
│    或 POST /api/rule-engine/pregenerate-prompt  → 动态生成              │
└─────────────────────────────────────────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ 4. 调用LLM生成JSON                                                      │
│    POST /api/llm/chat                                                   │
│    输入: 提示词 + 用户描述                                               │
│    输出: { item_type, properties, relationships, ... }                  │
└─────────────────────────────────────────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ 5. 规则引擎处理                                                         │
│    POST /api/rule-engine/create-item                                    │
│    流程: validate → create_pre → 组装AML → create_post                  │
└─────────────────────────────────────────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ 6. 提交SCSAI                                                             │
│    POST /SCSAI-api/ApplyItem                                             │
│    输入: AML XML                                                        │
│    输出: { Item: { id: "..." } }                                        │
└─────────────────────────────────────────────────────────────────────────┘

2.2 规则引擎执行流程

execute(scope, context, options)
    │
    ├── 1. getRules({ scope, item_type, is_active: true })
    │       └── 从 sciot_rules_v2 表查询规则
    │
    ├── 2. 按 priority 排序(P0 → P1 → P2 → P3)
    │
    ├── 3. 逐条执行
    │       ├── _evaluateCondition(rule, context) → match?
    │       │   └── 检查 condition.field + condition.operator
    │       │
    │       └── if (match) _executeAction(rule, context)
    │               ├── block → 返回 { blocked: true }
    │               ├── warn → 记录警告
    │               ├── suggest → 可能调用LLM
    │               └── auto_fix → 修改context数据
    │
    ├── 4. 记录历史 _recordHistory()
    │
    └── 5. 冲突解决
            ├── first_match → break(只执行第一条匹配)
            ├── merge → continue(执行所有匹配)
            └── stop → break(有命中就停止)

3. 核心数据表

3.1 sciot_rules_v2(统一规则表)

| 字段 | 类型 | 说明 |

|------|------|------|

| id | TEXT | 规则ID(主键) |

| name | TEXT | 规则名称 |

| scope | TEXT | 作用域:identify/validate/create_pre/create_post/repair/optimize/compare |

| item_type_name | TEXT | 适用对象类型(null=通用) |

| severity | TEXT | 严重级别:error/warning/info/hint |

| priority | INTEGER | 优先级:0=P0(阻塞), 1=P1(高), 2=P2(中), 3=P3(低) |

| condition | TEXT | 条件JSON:{ field, operator, value } |

| action_type | TEXT | 动作类型:block/warn/suggest/auto_fix |

| action_script | TEXT | 动作脚本(JS代码) |

| conflict_strategy | TEXT | 冲突策略:first_match/merge/stop |

| is_active | INTEGER | 是否启用 |

| is_builtin | INTEGER | 是否内置规则 |

scope 说明

| scope | 用途 | 执行时机 |

|-------|------|----------|

| validate | 校验规则 | 创建前校验,失败则阻止创建 |

| create_pre | 创建前预处理 | 校验通过后,填充默认值、生成编号 |

| create_post | 创建后处理 | SCSAI提交成功后,创建关联对象、发送通知 |

| repair | 修复规则 | 修复能力调用 |

| optimize | 优化规则 | 优化能力调用 |

3.2 sciot_templates(对象模板表)

| 字段 | 类型 | 说明 |

|------|------|------|

| item_type_name | TEXT | 对象类型名称 |

| llm_fields | TEXT | LLM可生成字段(JSON数组) |

| auto_fields | TEXT | 系统自动填充字段(JSON数组) |

| generation_rules | TEXT | 子对象生成规则(JSON) |

| aml_template | TEXT | AML模板XML |

字段分类用途

  • llm_fields: 出现在提示词中,让LLM生成值
  • auto_fields: 不出现在提示词中,由系统自动处理(编号、创建时间等)

3.3 sciot_properties(对象属性定义表)

| 字段 | 类型 | 说明 |

|------|------|------|

| item_type_name | TEXT | 所属对象类型 |

| name | TEXT | 属性名 |

| data_type | TEXT | 数据类型:string/integer/date/list/item/foreign/sequence |

| is_required | INTEGER | 是否必填 |

| is_hidden | INTEGER | 是否隐藏(UI不显示) |

| readonly | INTEGER | 是否只读 |

| default_value | TEXT | 默认值 |

| data_source | TEXT | list类型的数据源 |

data_type 关键类型

  • list: 枚举类型,可选值从 sciot_list_values 获取
  • item: 引用类型,指向另一个Item(如 wbs_id → WBS Element)
  • foreign: 外键类型
  • sequence: 自动编号类型

3.4 prompt_templates(提示词模板表)

字段类型说明
item_type_nameTEXT适用对象类型
prompt_typeTEXT提示词类型:creation/identify/repair/optimize
contentTEXT提示词内容(支持 {{变量}})
versionINTEGER版本号
statusTEXT状态:active/deprecated/testing

3.5 sciot_sequences(编号序列表)

| 字段 | 类型 | 说明 |

|------|------|------|

| name | TEXT | 序列名称(通常是对象类型名 + " Number") |

| prefix | TEXT | 编号前缀 |

| current_value | INTEGER | 当前值 |

| pad_to | INTEGER | 补零位数 |

编号生成prefix + String(current_value + 1).padStart(pad_to, '0')


4. 核心API

4.1 规则引擎API

方法路由功能
GET/api/rule-engine/rules获取规则列表
POST/api/rule-engine/rules创建规则
PUT/api/rule-engine/rules/:id更新规则
DELETE/api/rule-engine/rules/:id删除规则
POST/api/rule-engine/execute/:scope执行指定范围的规则
POST/api/rule-engine/validate验证对象
POST/api/rule-engine/create-item创建对象(完整生命周期)
POST/api/rule-engine/pregenerate-prompt预生成提示词
GET/api/rule-engine/stats规则统计
GET/api/rule-engine/history执行历史

4.2 SCIOT数据API

方法路由功能
GET/api/aml/unified/schema/:type获取对象Schema(含llmFields/autoFields)
GET/api/sciot/type-template/:type获取对象完整模板
GET/api/aml/sciot/prompts获取所有提示词
GET/api/aml/sciot/templates获取所有模板

5. 故障排查指南

5.1 问题定位流程

问题发生
    │
    ├── 1. 浏览器控制台 → 前端错误、API调用失败
    │
    ├── 2. 服务端日志 → server-log.txt
    │
    ├── 3. 检查数据库 → sqlite3 sciot_import.db
    │
    └── 4. 规则执行历史 → GET /api/rule-engine/history

5.2 常见问题

| 问题 | 可能原因 | 定位方法 | 解决方案 |

|------|----------|----------|----------|

| 提示词生成失败 | sciot_templates 无数据 | SELECT COUNT(*) FROM sciot_templates | 运行AML导入 |

| LLM返回空 | 提示词过长或格式错误 | 查看LLM响应日志 | 检查提示词模板 |

| 创建失败 | 校验规则阻塞 | 查看执行历史 | 检查validate规则 |

| 编号重复 | 序列值未更新 | SELECT * FROM sciot_sequences | 更新current_value |

| SCSAI提交失败 | 字段类型错误 | 查看AML内容 | 检查data_type定义 |

5.3 调试SQL

-- 查看规则统计
SELECT scope, COUNT(*) as count, SUM(CASE WHEN is_active=1 THEN 1 ELSE 0 END) as active
FROM sciot_rules_v2 GROUP BY scope;

-- 查看模板覆盖率
SELECT t.name, 
  CASE WHEN tmpl.id IS NOT NULL THEN 'YES' ELSE 'NO' END as has_template,
  CASE WHEN pt.id IS NOT NULL THEN 'YES' ELSE 'NO' END as has_prompt
FROM sciot_item_types t
LEFT JOIN sciot_templates tmpl ON t.name = tmpl.item_type_name
LEFT JOIN prompt_templates pt ON t.name = pt.item_type_name;

-- 查看执行历史
SELECT r.name, h.item_type, h.action, h.duration_ms, h.executed_at
FROM sciot_rule_history h
JOIN sciot_rules_v2 r ON h.rule_id = r.id
ORDER BY h.executed_at DESC LIMIT 50;

-- 查看高误报率规则
SELECT name, hit_count, user_correction_count,
  ROUND(user_correction_count * 100.0 / NULLIF(hit_count, 0), 2) as correction_rate
FROM sciot_rules_v2 WHERE hit_count > 10 ORDER BY correction_rate DESC;

6. 优化建议

6.1 已识别问题

| 问题 | 严重程度 | 建议 |

|------|----------|------|

| 两套规则表并存(sciot_rules vs sciot_rules_v2) | 高 | 统一迁移到sciot_rules_v2 |

| 数据库连接无单例 | 中 | 创建统一SciotDatabase单例 |

| AMLEngine不消费规则引擎 | 高 | 统一使用规则引擎 |

6.2 性能优化

  1. sciot_rules_v2 添加索引:(scope, item_type_name, is_active, priority)
  2. 规则执行支持并行(无依赖的规则)
  3. 实现缓存预热:启动时预加载常用规则

6.3 功能增强

  1. 规则测试沙箱:POST /api/rule-engine/test
  2. 规则自优化:分析执行历史,自动调整优先级
  3. 提示词A/B测试:通过parent_id和score字段支持

7. 文件索引

文件用途
server/core/rule-engine.js统一规则引擎核心(2268行)
server/routes/rule-engine.js规则引擎API路由(722行)
server/routes/aml.jsSCIOT数据API(4847行)
src/views/RulesAndTemplates.vue规则模板管理UI
src/views/StaffCapabilities.vue基础能力操作UI

文档更新时间: 2026-05-31

← 返回案例列表
分享:
🤖 Try Now →
🤖
🎁