Rules/Templates/Prompts — Layered Storage Complete Implementation Plan

Rules/Templates/Prompts — Layered Storage Complete Implementation Plan

Goal: Upgrade BossAgents' 2,371 rules + 1,205 templates + 2,817 prompts from a single local SQLite to a three-layer storage architecture (SCSAI Standard Library → Local Cache → User Private Library), supporting standard data synchronization, user personalization overrides, and review submission workflows.


Part 1: Current Full Workflow (Code Level)

I. Generation — Three Script Pipelines

1.1 import-aml-v2.js (1672 lines, offline CLI)

node scripts/import-aml-v2.js               # 增量
node scripts/import-aml-v2.js --full        # 全量重建
node scripts/import-aml-v2.js --patch       # 修补
node scripts/import-aml-v2.js --generate-prompts  # 仅提示词

扫描目录:PLM/Import/(标准 PLM AML)+ SCIOT/SCIOT/(按业务模块组织,约 636 个 .xml)

Full Process (main() → L1456):

| Stage | Function | Output (Row Count) |

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

| 1 | importItemType() | sciot_item_types(928) / sciot_properties(40854) / sciot_methods / sciot_relationships(587) / sciot_permissions / sciot_lists / sciot_list_values |

| 2 | Associate Method→ItemType (name prefix matching) | — |

| 3 | crossFileLinking() | Associations for Lifecycle/LCM/Form/Method→ItemType |

| 4 | backfillTemplates() | sciot_templates(1205) — derived from properties+relationships+lifecycle+forms tables |

| 5 | generateEnhancedRules() | sciot_rules_v2 — Generate by attribute + lifecycle + relationship |

| 6 | generateDynamicPrompts() | sciot_prompts(638) — Dynamic templates with {{}} placeholders |

| 7 | pregeneratePromptTemplates() | prompt_templates(2817) — Complete LLM prompts |

| 7' | generateBusinessPrompts() | sciot_business_prompts(46) |

Core data flow:

sciot_item_types ──→┬──→ sciot_properties ──→ backfillTemplates() ──→ sciot_templates (派生)
                     ├──→ sciot_relationships ─┘
                     ├──→ sciot_lifecycle_maps ─┘
                     └──→ sciot_forms ────────────┘

sciot_templates     ──→ pregeneratePromptTemplates() ──→ prompt_templates (派生)
sciot_properties    ──→ generateEnhancedRules() ──────→ sciot_rules_v2 (派生)

1.2 generate-operation-rules.js(725行,离线 CLI)

node scripts/generate-operation-rules.js
node scripts/generate-operation-rules.js --full
node scripts/generate-operation-rules.js --dry-run

依赖:必须在 import-aml-v2.js --full 完成后运行。

| Phase | Input → Output |

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

| 1 generateRelationshipRules() | sciot_relationships → sciot_rules_v2 (relationship rules) |

| 2 generateMethodTriggerRules() | sciot_method_rules → sciot_rules_v2 (auto-numbering/creation rules) |

| 3 generateValidationRules() | sciot_properties → sciot_rules_v2 (validation/default value rules) |

| 3b generateMissingTemplates() | sciot_item_types → sciot_templates (fill missing) |

| 4 generateOperationPrompts() | sciot_templates → prompt_templates (add/edit/get prompt templates) |

| 5 Complete meta fields | operation_type, scope, etc. |

1.3 Special DDL for prompt_templates table

No unified DDL contract, created independently in three places:

  1. generate-operation-rules.js L423: PRAGMA table_info → ALTER TABLE ADD COLUMN
  2. aml.js L706: Runtime lazy creation:
sciotDb.exec(`CREATE TABLE IF NOT EXISTS prompt_templates (
  id TEXT PRIMARY KEY, name TEXT, content TEXT,
  prompt_type TEXT DEFAULT 'creation', item_type_name TEXT,
  version INTEGER DEFAULT 1, status TEXT DEFAULT 'active',
  created_at TEXT, updated_at TEXT
)`);
  1. rule-engine.js: _ensureTables() 中同样有创建逻辑

二、Save — 数据位置

2.1 物理存储

| 文件 | 大小 | 位置 |

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

| sciot_import.db | ~38MB | server/data/sciot_import.db |

2.2 Core Table List

TableRow CountData NatureUpstream Dependency
sciot_item_types928Original (AML XML)—
sciot_properties40,854Original—
sciot_relationships587Original—
sciot_lifecycle_maps~191Original—
sciot_forms631Original—
sciot_templates1,205Derived ← Four-table joinproperties+relationships+lifecycle+forms
sciot_rules_v22,371Derived ← properties+relationships+methodsVarious original tables
prompt_templates2,817Derived ← sciot_templatessciot_templates
sciot_prompts638Legacy prompts (legacy)—
sciot_business_prompts46Business Promptsbusiness_systems + business_objects

2.3 Data Derivation Hierarchy Diagram

                                    import-aml-v2.js
                                          │
     ┌────────────────────────────────────┼─────────────────────┐
     │                │                   │                     │
sciot_item_types  sciot_properties   sciot_relationships  lifecycle_maps+forms
     │                │                   │                     │
     └────────────────┼───────────────────┼─────────────────────┘
                      │                   │
              backfillTemplates()   generateRelationshipRules()
                      │                   │
                sciot_templates     sciot_rules_v2 (部分)
                 (1,205)             (2,371)
                      │
             pregeneratePromptTemplates()
                      │
                prompt_templates    ←── generate-operation-rules.js
                 (2,817)

三、管理 — 后端 API 双线运营

3.1 aml.js(7002行)

规则 CRUD:   GET/POST /api/aml/sciot/rules[/add|/batch-toggle|/batch-delete]
模板读取:     GET       /api/aml/sciot/templates[/:name]
提示词管理:   GET/POST  /api/aml/sciot/prompts|/optimize-prompt/:typeName|/business-prompts[/regenerate]
组装器:       GET/POST  /api/aml/assemble[/:name|/validate]
统计+状态:    GET       /api/aml/sciot/stats|/status|/types

3.2 rule-engine.js(1203行)— 第二套并行 API

规则 CRUD:   GET/POST/PUT/DELETE /api/rule-engine/rules[/:id]
               POST /api/rule-engine/rules/batch-toggle|/batch-delete|/export|/import
规则执行:     POST /api/rule-engine/execute/:scope|/identify|/validate|/repair|/pregenerate-prompt

前端调用:两套 API 同时在使用

  • fetch('/api/aml/sciot/rules?...') — SCIOT Tab
  • fetch('/api/rules/list?...') — Rules Engine Tab

IV. Usage — Frontend Consumption Path

4.1 Core Object Creation Path (app.js L5460-5800) — The Most Critical Path

① 加载面板数据
   GET /api/aml/sciot/rules/stats
   GET /api/aml/sciot/business-prompts
   GET /api/aml/sciot/templates

② 选 ItemType → 读模板 + 规则
   GET /api/aml/sciot/templates/:name   → schema(llm_fields/auto_fields)
   GET /api/aml/sciot/rules?item_type=   → 该类型所有规则
   前端组装 schema(客户端完成)

③ 前端验证 validateBySchema()
④ 前端组装 AML objToAmlAdd()

⑤ 前端直调 SCSAI(绕过 BossAgents 后端)
   SCSAIApplyAML(aml) → bomService._SCSAIApiRequest('ApplyItem', aml)
   → POST https://ylxt.chat/scplm/server/InnovatorServer.aspx

⑥ 如果是 ItemType,继续创建属性/关系/权限(同样前端直调)

关键点:BossAgents 后端在第②步后不再参与,第③-⑥步全在前端完成。创建结果不写回 sciot_import.db。

4.2 Rules Engine Usage Path (AI Workbench)

用户描述需求
  → 读 prompt_templates(按 operation_type=add/edit/get)
  → 结构化组合提示词(含 rules 描述)
  → 调 LLM Generate AML
  → aml-assembler 验证 + 规则引擎校验
  → 调 SCSAI AML API

五、SCSAI 服务端现状

| 项目 | 值 |

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

| Entry Point | https://ylxt.chat/scplm |

| Authentication | Basic (root) |

| Database | SCPLM |

BossAgents Related ItemType Scan Results:

| SCSAI ItemType | Data Volume | Relationship with BossAgents |

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

| Rule | 1 (empty) | For SCSAI platform self-management, unrelated to the 2371 entries in sciot_rules_v2 |

| Template | 15 | SCSAI native templates (Action/Form/Identity), unrelated to the 1205 entries in sciot_templates |

| RuleDefinition | 0 | — |

| TemplateDefinition | 0 | — |

| RuleExecutionHistory | 0 | — |

| Template_Rule_Link | 1 record | For many-to-many association mode, refer to |

Conclusion: All 2,371+1,205+2,817+46 records of BossAgents data reside in the local SQLite, with zero data on SCSAI. Five new custom ItemTypes need to be created.


Part II: SCSAI Hierarchical Storage Implementation Plan

VI. Core Design Decisions

Decision PointConclusionRationale
SCSAI PositioningPrimary repository. Standard data takes SCSAI as the source of truth, sciot_import.db is demoted to a cachePersistence + Disaster Recovery
Derived Data Synchronization StrategySynchronize the final results (independent sciot_templates data), not the source tablesHigh complexity of derivation logic; recalculating on both ends easily leads to inconsistency
Pipeline ModificationKeep local generation unchanged, add a --push-SCSAI option to push results to SCSAIMinimize intrusion
AuthenticationPhase 1 fixed userId='default'Currently no login system
Two parallel APIsBoth add userId parameter, both support getEffectiveRules() mergingCompatible with frontend dual Tab calls
Frontend bypass issueRule validation goes through backend POST /api/rule-engine/validateOnly backend has getEffectiveRules()

6.1 Layered Architecture

┌─────────────────────────────────────────────────────────────────────┐
│  SCSAI 服务端 (ylxt.chat/scplm) — 标准库                       │
│                                                                      │
│  BossAgent_Rule          存标准规则(可被用户覆盖)                   │
│  BossAgent_Template      存标准模板                                   │
│  BossAgent_Prompt        存标准提示词                                 │
│  BossAgent_BizPrompt     存业务提示词                                 │
│  BossAgent_RuleContribution  存用户提交审核                            │
└────────────────┬────────────────────────────────────────────────────┘
                 │ AML get/add/edit (SOAP)
                 ▼
┌─────────────────────────────────────────────────────────────────────┐
│  sciot_import.db — 本地标准缓存 + 派生数据工厂                       │
│                                                                      │
│  [不变] 现有 23 张基础表 + sciot_rules_v2 / sciot_templates         │
│         / prompt_templates / sciot_business_prompts                   │
│  [新增] sync_state: 记录 SCSAI 同步版本                               │
│         sync_pending: 本地修改待同步 SCSAI                             │
└────────────────┬────────────────────────────────────────────────────┘
                 │ sync-service 增量同步
                 ▼
┌─────────────────────────────────────────────────────────────────────┐
│  user_libraries/user_{userId}.db — 用户私有库                         │
│                                                                      │
│  user_rules:      source = standard_copy | modified | custom         │
│  user_templates:  同上                                                │
│  user_prompts:    同上                                                │
│  sync_submissions: 待审核修改                                         │
│  sync_state:      同步状态                                            │
└────────────────┬────────────────────────────────────────────────────┘
                 │ getEffectiveRules() 优先级合并
                 ▼
┌─────────────────────────────────────────────────────────────────────┐
│  适配层 (aml.js / rule-engine.js)                                     │
│  getEffectiveRules(userId) → 合并后规则列表                           │
└─────────────────────────────────────────────────────────────────────┘

6.2 执行优先级

自定义 (custom)  >  用户修改版 (modified)  >  标准副本 (standard_copy)  >  SCSAI 标准  >  默认回退

规则:
  1. 同一 id:modified → standard_copy → SCSAI 标准(依次覆盖)
  2. 不同 id:全量合并(不排他)
  3. custom:直接追加,不与标准去重

七、SCSAI ItemType 字段设计

7.1 BossAgent_Rule(标准规则)

| 字段名 | SCSAI 类型 | SQLite 对应列 | 说明 |

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

| item_number | string(32, auto) | id | Auto-numbering |

| name | string(256, REQ) | name | Rule name |

| description | text | description | Rule description |

| bossagent_scope | list | scope | validate/create_pre/identify/… |

| bossagent_item_type | string(128) | item_type_name | Associated object class |

| bossagent_severity | list | severity | info/warning/error/hint |

| bossagent_category | list | category | data/relationship/lifecycle/method/permission/workflow |

| bossagent_priority | string(10) | priority | P0/P1/P2/P3 |

| bossagent_generated_aml | text | generated_aml | Refer to Rule.generated_aml |

| bossagent_natural_language | text | natural_language | Refer to Rule.natural_language_input |

| bossagent_condition_script | text | condition_script | Condition script |

| bossagent_action_type | list | action_type | auto_fix/suggest/block/warn/reference/generate |

| bossagent_action_config | text | action_config | JSON |

| bossagent_is_active | boolean | is_active | Enabled |

| bossagent_is_builtin | boolean | is_builtin | Built-in Rule |

| bossagent_source | list | source | generated/manual/auto |

| bossagent_version | integer | version | Version Number |

| bossagent_tags | string(512) | tags | Tags |

7.2 BossAgent_Template (Standard Template)

Field NameSCSAI TypeSQLite ColumnDescription
item_numberstring(32, auto)idAuto Number
namestring(128, REQ)item_type_nameTemplate Name
bossagent_labelstring(128)item_type_labelDisplay Name
bossagent_template_typelisttemplate_typeobject/relationship
bossagent_categorylist—Category (refer to Template.category)
bossagent_aml_contenttextaml_templateTemplate AML (refer to Template.aml_content)
bossagent_variablestextllm_fields + auto_fieldsJSON (refer to Template.variables)
bossagent_required_fieldstextrequired_fieldsJSON
bossagent_optional_fieldstextoptional_fieldsJSON
bossagent_relationship_typestextrelationship_typesJSON
bossagent_lifecyclestring(128)lifecycleDefault Lifecycle
bossagent_formstring(128)formDefault Form
bossagent_generation_rulestextgeneration_rulesComplete JSON
bossagent_versionintegerversionVersion Number
bossagent_is_builtinbooleanis_builtinBuilt-in Template

7.3 BossAgent_Prompt (Standard Prompt)

Field NameSCSAI TypeSQLite Corresponding Column
item_numberstring(32, auto)id
namestring(256, REQ)name
bossagent_contenttextcontent
bossagent_system_prompttextsystem_prompt
bossagent_item_typestring(128)item_type_name
bossagent_operationlistoperation_type (add/edit/get/generate)
bossagent_scopelistscope
bossagent_versionintegerversion
bossagent_is_builtinbooleanis_builtin
bossagent_statusliststatus (active/deprecated/testing)

7.4 BossAgent_BizPrompt (Business Prompt)

Field NameSCSAI TypeSQLite Column
item_numberstring(32, auto)system_id
namestring(128, REQ)system_name
bossagent_system_idstring(128)system_id
bossagent_system_prompttextprompt
bossagent_context_jsontextcontext_json
bossagent_capabilitiestext—

7.5 BossAgent_RuleContribution (Review Process)

Field NameSCSAI TypeDescription
item_numberstring(32, auto)Item Number
bossagent_submitterstring(128)Submitter
bossagent_object_typelistrule/template/prompt
bossagent_original_idstring(256)Original Standard ID
bossagent_contenttextFull JSON
bossagent_descriptiontextDescription
bossagent_statuslistpending/approved/rejected
bossagent_review_commenttextReview Comments

8. Synchronization Mechanism — Three Modes

8.1 Mode A: SCSAI → Local (Standard Cache Refresh)

触发:  cron 每日 2:00 | 手动 | 启动时
方向:  单向 SCSAI → sciot_import.db

流程:
  1. 查询 SCSAI 各 BossAgent_* ItemType 的 modified_on
  2. 对比本地 sync_state.last_sync_at
  3. 有更新 → AML get 拉取增量
  4. 只更新 sciot_import.db 同步副本(不影响原始表)
  5. 更新 sync_state

8.2 模式 B:Local → User(增量推送)

触发:  用户登录 | 手动点击同步
方向:  sciot_import.db → user_libraries/user_{userId}.db

流程:
  1. 用户库不存在 → 全量初始化
  2. 对比标准副本(source='standard_copy')
  3. 新增记录 → 插入
  4. 标准有更新 → 用户未修改时覆盖;用户已修改(source='modified')跳过

8.3 模式 C:User → SCSAI(提交审核)

触发:  用户点击"提交审核"
方向:  user_libraries → SCSAI(含审核流程)

流程:
  1. 用户修改规则 → 本地 source='modified'
  2. 提交审核 → 写入 sync_submissions(before/after + 说明)
  3. 管理员审核(diff 对比 + 审批)
     - approve: AML edit → SCSAI + 更新用户库 source='approved'
     - reject:  写 review_comment,用户可重提

九、改动明细

9.1 生成层

目标:流水线不变,加 --push-SCSAI 选项

import-aml-v2.js (+50 lines):

// main() 最后
if (process.argv.includes('--push-SCSAI')) {
  await pushToSCSAI();
}

async function pushToSCSAI() {
  const sync = require('../server/services/sync-service');
  await sync.batchWriteToSCSAI('BossAgent_Rule',
    db.prepare('SELECT * FROM sciot_rules_v2 WHERE source = ?').all('generated'), 500);
  await sync.batchWriteToSCSAI('BossAgent_Template',
    db.prepare('SELECT * FROM sciot_templates').all(), 500);
  // 同 prompt_templates → BossAgent_Prompt, sciot_business_prompts → BossAgent_BizPrompt
}

generate-operation-rules.js(+20行):同模式

9.2 Persistence Layer

  • Unchanged: Existing table structure of sciot_import.db
  • Added sync_state: sync version records / sync_pending: pending write-back changes

9.3 Management API Layer

#### aml.js

// GET /api/aml/sciot/rules 支持 ?userId=
async function handleSciotRules(req, res, pathname, query, bodyStr) {
  const stdRules = sciotDb.prepare('SELECT * FROM sciot_rules_v2 WHERE ...').all(params);
  if (query.userId && query.userId !== '') {
    const userLib = require('../services/user-library');
    rules = userLib.getEffectiveRules(query.userId, stdRules);
  }
  // 返回合并后结果
}

/api/aml/sciot/templates/ 和 /api/aml/sciot/prompts:同模式

#### rule-engine.js

// GET /api/rule-engine/rules?userId=
if (filters.userId && filters.userId !== 'default') {
  const stdRules = engine.getRules({ ...filters, userId: null });
  filters.effectiveRules = userLib.getEffectiveRules(filters.userId, stdRules);
}

POST /api/rule-engine/execute/:scope 同样支持 ?userId=

9.4 Frontend Layer

#### Creation Panel (+50 lines)

// 改前: validateBySchema(schema, values) → ok → objToAmlAdd → SCSAIApplyAML
// 改后:
//   1. POST /api/rule-engine/validate?userId=default  { item_type, values }
//   2. 后端返回 { valid, errors, fixed_values }
//   3. valid → objToAmlAdd(fixed_values) → SCSAIApplyAML
//             → 客户端+后端双重验证

#### 规则引擎 Tab 扩展(+100行)

  • Existing 6 Tabs remain unchanged
  • Added 7th panel "My Library": View/Modify/Customize/Delete rules, submit for review

#### Sync Button (+30 lines)

  • Sync button at top-right of Rules Engine Tab → POST /api/user-library/default/sync/standard

9.5 New Files

FileLinesResponsibility
server/services/user-library.js~600User Library SQLite Management: init / getEffectiveRules / CRUD / sync
server/services/sync-service.js~500Three-layer sync: SCSAIToLocal / localToUser / userToSCSAI
server/routes/user-library-routes.js~40014 REST APIs (CRUD + sync + review)

9.6 Data Flow Comparison

改前:
  生成脚本 → sciot_import.db
  API 直读 sciot_import.db
  前端读 API → 前端验证 → 前端装 AML → 直调 SCSAI

改后:
  生成脚本 → sciot_import.db(不变)
            + --push-SCSAI → SCSAI
  启动时自检 → 对比 sync_state → 拉 SCSAI 更新 → 写回 sciot_import.db
  API 读 sciot_import.db + user_libraries(getEffectiveRules 合并)
  前端读 API(含 userId)→ 调 validate(走后端合并规则)→ 前端装 AML → 调 SCSAI
  用户修改 → user_libraries → 提交审核 → 管理员审批 → 写 SCSAI

十、实施阶段

| Phase | 内容 | 预估 |

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

| 0 Preparation | Confirm SCSAI connection / DB structure / ports / 5 ItemType names are conflict-free | Half day |

| 1 SCSAI Table Creation + Full Import | Create 5 ItemTypes + Lists + field definitions; batch read (500 records/batch) and write to SCSAI; retry 3 times; verify | 1 day |

| 2 sync-service sync layer | SCSAIToLocal() → localToUser() → userToSCSAI() three directions | 1 day |

| 3 Adaptation layer + API modification | user-library.js + aml.js/rule-engine.js modifications + user-library-routes.js | 1.5 days |

| 4 Frontend Revamp | Validate flow moved to backend + "My Library" panel + Review panel + Sync button | 1 day |

| 5 Productionization | Scheduled sync cron + Startup self-check + Offline degradation + Monitoring alerts | 0.5 days |


XI. Out of Scope

❌ 不改 sciot_import.db 现有表结构
❌ 不改三条生成流水线的核心逻辑
❌ 不改前端现有 6 个 Tab 面板(只做扩展)
❌ 不改现有 API 的返回格式(新增 userId 参数可选)
❌ 不做一个完整的用户认证系统(Phase 1 固定 userId='default')
❌ 不做 UI 级版本对比工具(简化为 JSON diff)
❌ 不修改 SCSAI 已有的 Rule/Template ItemType(保持原生用途)

十二、风险与应对

| 风险 | 概率 | 影响 | 应对 |

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

| SCSAI disk full/downtime | Medium | Sync paused | Direct read from local cache + store in sync_pending, write back after recovery |

| AML batch write timeout | Medium | Initial import interrupted | 500 records/batch, retry 3 times, log failed IDs |

| Derived data inconsistency between ends | Medium | Standard library and local cache mismatch | Record version number in sync_state, full resync on mismatch |

| User data overwrite rules | Low | Personalization loss | strict priority + user-modified versions not passively overwritten |

| Frontend validation bypass | Medium | Personalization rules not effective | Validate API processed via backend merge, cannot be skipped by frontend |

| prompt_templates lacks unified DDL | Low | Schema inconsistency during sync | sync-service ensures table structure consistency before sync |

← 返回案例列表

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 →
🤖
🎁