capabilities-and-rule-engine

AI 五大基础能力实现、规则引擎关系与现存问题

版本: v1.0 | 更新: 2026-06-08 | 适用范围: SCSAI PLM BossAgents


1. 架构总览

1.1 两条调用路径

路径能力前后端链路说明
Capability API识别/修复/优化/比对/生成/api/capability/* -> CapabilityRuntime -> UnifiedRuleEngine/LLMBrain后端处理,规则引擎优先
前端自组装创建Composable: Schema->LLM->JSON->AML->SCSAI前端构建 AML 后直接 ApplyItem
巡检巡检/api/ai-inspector/inspect-streamSSE 流式,独立于 CapabilityRuntime
Schema 直显识别(无输入)/api/aml/unified/schema/:itemType不调 LLM,直接展示属性

1.2 能力分布

能力前端 Composable后端 Runtime规则 ScopeLLM 降级
识别useCapabilityIdentifyidentify()identifythinkJson(systemPrompt, filters)
创建useCapabilityCreate前端自组装(无)N/A(前端直接调LLM)
修复useCapabilityRepairrepair()repairthinkJson(data+issues)
优化useCapabilityOptimizeoptimize()optimizethinkJson(data)
比对useCapabilityComparecompare()comparethinkJson(a+b)
生成useCapabilityGenerategenerate()transformthinkJson(data)
巡检useInspectionai-inspector.js硬编码+数据库规则不调LLM

2. 五大能力实现

2.1 识别

前端: useCapabilityIdentify.js

后端: CapabilityRuntime.identify() -> UnifiedRuleEngine.executeIdentify() / LLMBrain.thinkJson()

无输入模式: 直接 GET /api/aml/unified/schema/:itemType, 解析 properties(Object.entries)+relationships, 展示属性/关系列表。不调用 LLM。

有输入模式: POST /api/capability/identify -> 规则引擎优先 -> LLM 降级 -> 返回 matchedType, confidence, aiAnalysis。

2.2 修复

前端调 /api/capability/repair -> repair() -> executeRepair() -> scope=repair 规则 -> 逐条 eval -> LLM 降级。返回 issues(severity, field, description, suggestion)。

2.3 优化

与修复一致但 scope=optimize,侧重改善建议。返回 original, optimized, changes。

2.4 比对

POST /api/capability/compare -> executeCompare() -> 返回差异: added, removed, modified, details, totalDifferences。

2.5 内容生成

POST /api/capability/generate -> generate() -> scope=transform -> LLM 降级 -> 返回 markdown 文档。

2.6 创建 (架构特殊: 前端自组装)

与前述5个能力不同,创建不走 CapabilityRuntime,全在前端完成:

  1. GET /api/aml/unified/schema/:itemType -> 获取字段 Schema
  2. GET /api/sciot/type-template/:itemType -> 模板(404 时跳过)
  3. buildCreationPrompt(fields, relationships, template) -> 构建 prompt
  4. POST /api/llm/chat(system_prompt, userMsg) -> 调 LLM
  5. JSON 解析: 四级 fallback (直接parse -> 标记提取 -> 清理markdown -> 报错)
  6. sanitizeProperties(rule-validator.js) -> 字段清理
  7. buildAML(AmlBuilder.js) -> AML 组装
  8. precreateNestedItem -> 预创建 item_properties 子对象
  9. _SCSAIApiRequest('ApplyItem') -> 提交 SCSAI
  10. 返回 createId + aml

3. 规则引擎关系

3.1 Rules-First, LLM-Fallback 模式

所有能力(除创建/巡检)共享:

  1. UnifiedRuleEngine.getRules(scope, item_type) -> 从 sciot_rules_v2 查询
  2. 逐条 eval conditionScript / condition JSON -> 匹配后执行 action
  3. 全部规则跑完无有效结果 -> LLMBrain.thinkJson(systemPrompt, data)
  4. 返回规则结果或 LLM 结果

3.2 数据表

sciot_rules_v2: id, name, scope, item_type_name, severity, priority, condition, action_type, is_active

sciot_properties: id, item_type_name, name, label, data_type, is_required, is_keyed

sciot_relationships: id, name, related_item_type, relationship_item_type, behavior

sciot_templates: id, item_type_name, llm_fields, auto_fields, generation_rules

prompt_templates: id, name, content, prompt_type, item_type_name

3.3 Scope 对应

Scope能力触发
validate通用校验创建前/操作前
identify识别用户请求识别
repair修复用户请求修复
optimize优化用户请求优化
compare比对用户请求比对
transform生成用户请求生成
create_pre/create_post创建(后端)创建前后

4. 前端链路

AiCapabilityBar.vue(349行) -> 6+1 composable -> HTTP API / LLM / SCSAI

Props: itemType(string), selectedItems(array), showInspect(boolean=true)

Emits: created({id, type})

11个业务页面已集成,itemType 或为固定值或随标签切换(通过 computed 绑定):

  • 固定: ProductList(Part), BomAssistant(Part), Vendor(Vendor), Customer(Customer), Order(Sales Order), Document(Document), Project(Project), Inventory(stk_stock_setup)
  • 动态: ChangeManagement(ECR/Express ECO/ECN/PR), QualityManagement(DFMEA/Audit/FishBone Analysis), WorkflowManagement(Workflow Map/Workflow Process)

5. 现存问题

5.1 规则引擎层

[BUG-1] confinity 拼写错误 (rule-engine.js:1000)

_shouldCallLlm() 使用 condition.confinity 而非 confidence,LLM 门控条件永假。

[BUG-2] executeRepair() 参数不匹配 (rule-engine.js:~1096)

getRules(RULE_SCOPES.REPAIR, item_type) 但签名是 getRules(options)。

[BUG-3] ai-inspector.js 的 req.method 引用

handleAIInspector(pathname, body, ...) 无 req 参数,规则 CRUD 路由引用 req.method 会 ReferenceError。

[债务-1] 两套规则表并行

旧表 sciot_rules(14+处引用) + 新表 sciot_rules_v2。ai-inspector 写入 v2 时用了旧表列名。

[债务-2] 大部分 ItemType 无规则配置

BUSINESS_MODULES 有大量空数组,规则引擎直接降级 LLM。

5.2 CapabilityRuntime 层

[架构] 仅在 PM2 进程内单例,多进程时缓存各自独立。

[冗余] create() 在 engine.createItem() 返回失败后仍尝试 LLM 降级,导致错误信息混乱。

5.3 前端 Composable 层

[已修复] buildCreationPrompt 参数传错 - 6独立参数 vs 单params对象。

[已修复] Schema properties 类型 - Object.values 兜底。

[已修复] ItemType 名称修正 - 8个页面已更正。

[设计] LLM JSON 解析依赖正则提取对 markdown 格式脆弱。

5.4 Prompt 与 LLM 层

[问题] Schema/type-template 缺失时 LLM 易返回长篇大论而非 JSON。

[缺失] 无 LLM 输出结果的结构化校验。

5.5 数据配置层

[缺失] type-template 配置不全,大部分 ItemType 返回 404。

[缺失] 规则配置不全,大部分业务对象无生效规则。

[问题] AML Generator UUID 30位不标准(aml-generator.js:390)。


6. 文件清单

文件行数职责
src/components/ai/AiCapabilityBar.vue349能力栏+巡检,11页面通用
src/composables/useCapabilityCreate.js264创建主流程(自组装)
src/composables/useCapabilityIdentify.js63识别(无LLM+API)
src/composables/useCapabilityRepair.js47修复
src/composables/useCapabilityOptimize.js45优化
src/composables/useCapabilityCompare.js45比对
src/composables/useCapabilityGenerate.js45内容生成
src/composables/useInspection.js148巡检(SSE+阻塞)
src/utils/AmlBuilder.js~500AML 组装
src/utils/PromptBuilder.js~440创建 prompt 构建
src/utils/rule-validator.js~200字段校验
server/core/capability-runtime.js4556+1 能力运行时
server/core/rule-engine.js1439UnifiedRuleEngine
server/core/unified-schema.js503SchemaBuilder
server/core/aml-generator.js454AML Generator
server/routes/capability-api.js143/api/capability/*
server/routes/unified-aml.js192/api/aml/unified/*
server/routes/ai-inspector.js1141巡检+规则CRUD
server/routes/rule-engine.js829规则引擎 API
← 返回案例列表
分享:
🤖 Try Now →
🤖
🎁