Rule Template Management System — Optimization Understanding Document

Rule Template Management System — Optimization Understanding Document

In-depth Understanding and Optimization Suggestions Based on rules-templates-prompts-system.md

Last Updated: 2026-05-31


1. Closed-Loop Core Understanding

1.1 Correct Data Flow

AML文件 (SCIOT/*.xml)
    │
    ├── 导入 → sciot_properties (属性定义)
    ├── 导入 → sciot_relationships (关系定义)
    ├── 导入 → sciot_sequences (编号规则)
    └── 导入 → sciot_methods (客户端方法)
    │
    ▼
配置层 (人工维护)
    │
    ├── sciot_templates (字段分类: llm_fields / auto_fields)
    ├── sciot_rules_v2 (规则: validate / create_pre / create_post)
    └── sciot_business_prompts (业务系统级提示词)
    │
    ▼
预生成层 (确定性脚本拼接)
    │
    └── POST /api/rule-engine/pregenerate-prompt
        │
        └── prompt_templates (版本化提示词库)
            │
            ├── prompt_type='creation' → 用于Create场景
            ├── prompt_type='identify' → 用于Identify场景
            ├── prompt_type='repair'   → 用于Repair场景
            ├── prompt_type='optimize' → 用于Optimize场景
            └── prompt_type='compare'  → 用于Compare场景
    │
    ▼
消费层 (六大基础能力)
    │
    ├── Create   → 读取creation提示词 + create规则 + 模板字段分类
    ├── Assemble → 读取creation提示词 + assemble规则 + 模板generation_rules
    ├── Repair   → 读取repair提示词   + repair规则   + 模板字段定义
    ├── Optimize → 读取optimize提示词 + optimize规则 + 模板字段定义
    ├── Compare  → 读取compare提示词  + compare规则  + 模板字段定义
    └── Identify → 读取identify提示词 + identify规则 + item_type列表
    │
    ▼
反馈修复 (闭环关键)
    │
    ├── 创建成功 → 规则模板提示词 OK
    ├── 组装失败 → 检查规则模板定义 → 修改 → 重新预生成 → 重试
    ├── 修复不准 → 检查repair规则优先级 → 调整 → 重新预生成
    └── 优化效果差 → 检查optimize规则条件 → 修正 → A/B测试

1.2 关键认知

| 概念 | 正确理解 | 常见误区 |

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

| AML File | The single source of truth, immutable; changes require re-import | Manually modifying the database |

| Rule Template | Manually maintained configuration layer that determines system behavior | Considered as hardcoded in code |

| Prompt | Repeatably generated artifact, not AI-generated | Considered as randomly generated by LLM |

| Pre-generation | Deterministic script concatenation, same input → same output | Considered as non-deterministic |

| Assembly Failure | Most likely due to incorrect rule template definition | Considered as a code bug |


2. Analysis of Current Implementation Issues

2.1 Issue 1: AML Assembly Engine Uses Old Rule Table

Current State:

  • server/aml-assembly-engine.js uses sciot_rules (old table)
  • server/core/rule-engine.js uses sciot_rules_v2 (new table)
  • The two rule sets are not synchronized, causing assembly behavior to be disconnected from rule management

Impact:

  • Rules modified in RulesAndTemplates.vue do not take effect
  • Cannot troubleshoot assembly failures via the rule engine

Fix Solution:

// aml-assembly-engine.js 应该调用 UnifiedRuleEngine
const { UnifiedRuleEngine } = require('./core/rule-engine');
const engine = new UnifiedRuleEngine({ db });

// 组装时执行规则
const result = await engine.execute('assemble', {
  item_type: type,
  properties: fieldsData,
  template: templateData
});

2.2 问题2:StaffCapabilities.vue 直接组装AML

现状:

  • Lines 1782-1900 directly traverse fields to assemble AML
  • Use propDefMap (backend Schema) to determine field types
  • The createItem method without using the rules engine

Impact:

  • Bypasses the validate / create_pre / create_post processes of the rules engine
  • Built-in rules (such as auto-generating numbers) do not take effect
  • Inaccurate field type determination (goals treated as string instead of text)

Correct approach:

// 应该调用规则引擎
createResultRaw = await engine.createItem({
  item_type: 'Project',
  properties: standardData.properties,
  item_properties: standardData.item_properties,
  relationships: standardData.relationships,
  applyAML: async (aml) => {
    // 通过统一代理提交
    return await bomService._SCSAIApiRequest('ApplyItem', aml);
  }
});

2.3 问题3:字段类型判断不一致

现状:

  • Prompt generation uses templatePropsMap (accurate)
  • AML assembly uses propDefMap (inaccurate, backend Schema)
  • Both have inconsistent understanding of the data_type for goals

Fix:

  • Unify all field type determinations using templatePropsMap
  • propDefMap is only used to obtain metadata like readonly

2.4 Issue 4: Incorrect handling of item-type fields

Current situation:

  • wbs_id is an item type, generated as an object by the LLM
  • Pre-creation logic attempts to create a WBS Element but fails
  • After failure, the object is inlined into the AML, and SCSAI rejects it

Correct process:

1. LLMGenerate wbs_id: { name: "...", ... }
2. 预创建 WBS Element → 获取 ID
3. 如果预创建成功 → mainValues.wbs_id = "ID字符串"
4. 如果预创建失败 → 跳过该字段(SCSAI会使用默认值)
5. AML组装时 → <wbs_id>ID字符串</wbs_id> 或 省略

3. 正确的组装流程

3.1 理想的数据流

用户输入 "创建项目X"
    │
    ▼
┌─────────────────────────────────────┐
│ 1. 获取Schema和模板                  │
│    GET /api/aml/unified/schema/Project
│    GET /api/sciot/type-template/Project
└─────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────┐
│ 2. 获取预生成的提示词                │
│    GET /api/aml/sciot/prompts/Project
│    prompt_type='creation'
└─────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────┐
│ 3. 调用LLMGenerateJSON                   │
│    POST /api/llm/chat               │
│    输入: 提示词 + 用户描述           │
│    输出: { properties, relationships }
└─────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────┐
│ 4. 预创建item类型字段                │
│    遍历 properties                  │
│    如果 field.data_type === 'item'  │
│      → 创建嵌套Item                 │
│      → 成功: 替换为ID               │
│      → 失败: 删除该字段(用默认值)  │
└─────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────┐
│ 5. 调用规则引擎 createItem           │
│    POST /api/rule-engine/create-item │
│    流程:                             │
│      validate → 校验必填、格式等     │
│      create_pre → 填充默认值、编号   │
│      组装AML → 返回AML文本           │
│      create_post → (提交后处理)    │
└─────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────┐
│ 6. SubmitAML到SCSAI                     │
│    POST /SCSAI-api/ApplyItem          │
│    输入: 规则引擎返回的AML           │
│    输出: { Item: { id: "..." } }     │
└─────────────────────────────────────┘
    │
    ▼
成功 / 失败

3.2 规则引擎在组装中的角色

| 阶段 | 规则引擎作用 | 输入 | 输出 |

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

| validate | Validate data integrity | properties | { blocked: true/false, errors: [] } |

| create_pre | Fill default values, generate numbers | properties | Modified properties |

| AML Assembly | Build XML from template | properties + template | AML text |

| create_post | Create related objects, notify | item_id + context | Result of related object creation |


4. Fix Priority

P0 - Fix immediately (blocking issues)

  1. Item type field handling
  • Should not inline objects into AML when pre-creation fails
  • Should skip this field and let SCSAI use default values
  1. Unify field type judgment
  • AML assembly uniformly uses templatePropsMap
  • No longer relies on propDefMap.data_type

P1 - Important fixes (feature improvement)

  1. StaffCapabilities.vue integrates with rule engine
  • Call createItem instead of directly assembling AML
  • Enable validate / create_pre / create_post to take effect
  1. AML assembly engine uses new rule table
  • Deprecate sciot_rules (old table)
  • Uniformly use sciot_rules_v2 + UnifiedRuleEngine

P2 - Architecture optimization (long-term)

  1. Database connection singleton
  • Create SciotDatabase singleton
  • Avoid multiple independent connection creations
  1. LLMBrain eliminates hardcoding
  • Pre-generate all business prompts
  • Migrate all to Path 1 (rule engine loading)

5. Fault Troubleshooting Quick Reference (Optimized Version)

SymptomLocation LevelWhat to CheckFix Method
Incorrect fields generated by LLMLayer 2 Templatesciot_templates.llm_fieldsAdjust field categorization → Regenerate
Required fields not validatedLayer 3 Rulessciot_rules_v2 scope='validate'Add/modify validation rules → Regenerate
Number not auto-generatedLayer 3 Rulessciot_rules_v2 scope='create_pre'Check action_script → Regenerate
AML submission failedLayer 4 Prompt/AssemblyView actual AML contentCheck field type definitions → Modify template → Regenerate
Associated object not createdLayer 3 Rulessciot_rules_v2 scope='create_post'Add create_post rule → Regenerate
Prompt content outdatedLayer 4 Promptprompt_templates.updated_atRun pregenerate-prompt

6. Key File Relationship Diagram

server/
├── core/
│   ├── rule-engine.js          ← 统一规则引擎(核心)
│   │   ├── 规则CRUD
│   │   ├── 规则执行 (execute)
│   │   ├── 对象创建 (createItem)
│   │   └── 内置规则 (builtin-rules)
│   │
│   └── unified-schema.js       ← Schema构建器
│       └── 运行时提示词生成
│
├── routes/
│   ├── rule-engine.js          ← 规则引擎API路由
│   │   ├── /api/rule-engine/rules
│   │   ├── /api/rule-engine/execute/:scope
│   │   ├── /api/rule-engine/create-item
│   │   └── /api/rule-engine/pregenerate-prompt
│   │
│   └── aml.js                  ← SCIOT数据API
│       ├── /api/aml/sciot/types
│       ├── /api/aml/sciot/templates
│       ├── /api/aml/sciot/prompts
│       └── /api/aml/unified/schema/:type
│
└── aml-assembly-engine.js      ← ⚠️ 应接入rule-engine.js

src/
├── views/
│   ├── RulesAndTemplates.vue   ← 规则模板管理UI
│   │   ├── Tab3: 在线规则引擎
│   │   ├── Tab6: 模板库
│   │   └── Tab4: 对象提示词
│   │
│   └── StaffCapabilities.vue   ← ⚠️ 应调用rule-engine/create-item
│       ├── 识别面板
│       ├── 创建面板
│       ├── 修复面板
│       └── ...
│
└── composables/
    └── useRuleEngine.js        ← 前端规则引擎封装

data/
└── sciot_import.db             ← 配置主库
    ├── sciot_rules_v2          ← 统一规则表 ★
    ├── sciot_templates         ← 对象模板表 ★
    ├── prompt_templates        ← 提示词模板库 ★
    ├── sciot_properties        ← 属性定义(从AML导入)
    ├── sciot_relationships     ← 关系定义(从AML导入)
    └── ...

7. 总结

核心原则

  1. AML是源头 - 所有基础数据从AML导入,变更需重新导入
  2. 规则模板是配置 - 人工维护,决定系统行为
  3. 提示词是产物 - 可重复生成,相同输入→相同输出
  4. 组装失败看规则模板 - 不是代码bug,是配置问题
  5. 修复后重新预生成 - 修改规则/模板后必须重新预生成提示词

当前状态

  • ✅ 规则引擎核心成熟(2268行)
  • ✅ 规则引擎API完整(22+端点)
  • ✅ 前端规则管理UI就绪
  • ⚠️ AML组装未接入规则引擎(使用旧表)
  • ⚠️ StaffCapabilities.vue 直接组装AML(未调用createItem)
  • ⚠️ 字段类型判断不一致(templatePropsMap vs propDefMap)

下一步行动

  1. Repair item 类型字段处理(预创建失败时跳过)
  2. 统一字段类型判断(全部使用 templatePropsMap)
  3. StaffCapabilities.vue 接入规则引擎 createItem
  4. AML组装引擎接入 UnifiedRuleEngine
  5. 废弃 sciot_rules(旧表)

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

← 返回案例列表

Try 左帮右臂 Industrial Agent — your digital workforce

Available as a WeChat mini program, web app (ylxt.chat) and desktop client. Start from whichever fits your workflow.

🌐 Web App 💻 Desktop Download 📞 Call 189-8606-6876
WeChat mini program QR
Scan for the WeChat mini program
Share:
🤖 Try Now →
🤖
🎁