规则模板提示词分层存储与同步 - 实现方案

规则模板提示词分层存储与同步 - 实现方案

一、现状分析

1.1 当前存储架构

┌──────────────────────────────────────┐
│  sciot_import.db (38MB, 单文件)      │
│  ├── sciot_rules_v2    (2,371条)     │
│  ├── sciot_templates   (1,205条)     │
│  ├── prompt_templates  (2,817条)     │
│  ├── sciot_business_prompts (46条)   │
│  └── sciot_item_types  (928条)      │
└──────────────────────────────────────┘
         ↑ 直接查询 (getSciotDb())
┌──────────────────────────────────────┐
│  aml.js  (204KB, 所有用户共享)        │
│  35处 sciot_rules_v2                 │
│  33处 prompt_templates               │
│  24处 sciot_templates                │
└──────────────────────────────────────┘

问题: 所有用户共享同一份规则/模板/提示词,无法个性化定制。

1.2 现有代码调用链路

用户请求 → aml.js 路由 → getSciotDb() → sciot_import.db (单例)
                                    ↓
                      sciot_rules_v2 / sciot_templates / prompt_templates

关键点: aml.js 内部通过 getSciotDb() 直接访问 sciot_import.db,未经过任何用户隔离层。

二、目标架构

┌─────────────────────────────────────────────┐
│  sciot_import.db (标准库,只读)              │
│  规则2371 + 模板1205 + 提示词2817 + 业务46  │
└──────────────┬──────────────────────────────┘
               │ sync-service.js (首次导入/增量同步)
               ▼
┌─────────────────────────────────────────────┐
│  user_library_{userId}.db (用户私有库)       │
│  ├── user_rules      (标准副本+自定义+修改)  │
│  ├── user_templates  (标准副本+自定义+修改)  │
│  ├── user_prompts    (标准副本+自定义+修改)  │
│  ├── sync_submissions(提交审核)             │
│  └── sync_state      (同步版本记录)         │
└──────────────┬──────────────────────────────┘
               │ 执行优先级: 修改版 > 自定义 > 标准副本 > 标准库回退
               ▼
┌─────────────────────────────────────────────┐
│  aml.js (修改:走 user-library 适配层)       │
│  getEffectiveRules / getEffectiveTemplates   │
│  getEffectivePrompts                         │
└─────────────────────────────────────────────┘
               │ 选择性回传
               ▼
┌─────────────────────────────────────────────┐
│  SCSAI 标准库 (审核合并)                      │
│  待定: SCSAI ItemType 创建                    │
└─────────────────────────────────────────────┘

三、SCSAI 对象类设计(服务端标准库)

3.1 需创建的 SCSAI ItemType

| ItemType | 名称 | 用途 | 对应SQLite表 |

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

| BossAgent_Rule | 标准规则 | 平台级规则定义 | sciot_rules_v2 |

| BossAgent_Template | 标准模板 | 平台级模板定义 | sciot_templates |

| BossAgent_Prompt | 标准提示词 | 平台级提示词定义 | prompt_templates |

| BossAgent_BizPrompt | 业务提示词 | 业务系统提示词 | sciot_business_prompts |

| BossAgent_RuleContribution | 规则贡献 | 用户提交审核 | sync_submissions |

3.2 ItemType 字段详细设计

#### BossAgent_Rule(规则)

字段                    SCSAI类型       对应SQLite列         说明
─────────────────────────────────────────────────────────────
item_number             string         自动编号
name                    string         name                 规则名称
bossagent_category      list           scope                validate/create_pre/identify/...
bossagent_scope         list           scope                场景: 识别/创建/验证/优化/修复/比对/巡检
bossagent_item_type     string         item_type_name       关联ItemType
bossagent_condition     text           condition            触发条件(JSON)
bossagent_action_type   string         action_type          动作类型
bossagent_action_config text           action_config        动作配置(JSON)
bossagent_priority      integer        priority             优先级 (1-100)
bossagent_severity      list           severity             info/warning/error
bossagent_is_active     boolean        is_active            启用状态
bossagent_is_builtin    boolean        is_builtin           是否内置
bossagent_source        list           source               generated/builtin/manual
bossagent_version       integer        version              版本号
bossagent_tags          string         tags                 标签
bossagent_hit_count     integer        hit_count            命中次数
bossagent_created_at    date           created_at
bossagent_updated_at    date           updated_at

#### BossAgent_Template(模板)

字段                    SCSAI类型       对应SQLite列
─────────────────────────────────────────────────────────────
item_number             string         自动编号
name                    string         item_type_name
bossagent_template_type list           template_type         object/relationship/workflow
bossagent_item_label    string         item_type_label
bossagent_aml_template  text           aml_template
bossagent_required      text           required_fields       必填字段(JSON数组)
bossagent_optional      text           optional_fields       可选字段(JSON数组)
bossagent_keyed_field   string         keyed_field
bossagent_relationships text           relationship_types    关系类型(JSON)
bossagent_lifecycle     text           lifecycle
bossagent_form          text           form
bossagent_llm_fields    text           llm_fields            LLM可生成字段
bossagent_auto_fields   text           auto_fields           自动填充字段
bossagent_generation    text           generation_rules      生成规则(JSON)

#### BossAgent_Prompt(提示词)

字段                    SCSAI类型       对应SQLite列
─────────────────────────────────────────────────────────────
item_number             string         自动编号
name                    string         name
bossagent_description   text           description
bossagent_content       text           content               提示词正文
bossagent_prompt_type   list           prompt_type           creation/revision/validation/...
bossagent_item_type     string         item_type_name        关联ItemType
bossagent_operation     list           operation_type        add/get/edit/generate
bossagent_system_prompt text           system_prompt         系统提示词
bossagent_scope         list           scope                 create/validate/identify/...
bossagent_is_builtin    boolean        is_builtin
bossagent_version       integer        version
bossagent_usage_count   integer        usage_count
bossagent_temperature   float          -                     温度(SCSAI新增)
bossagent_variables     text           variables             变量定义(JSON, SCSAI新增)

3.3 关系设计

BossAgent_Rule ──→ [bossagent_item_type] → ItemType (多对一)
BossAgent_Template ──→ [name] → ItemType (多对一)
BossAgent_Prompt ──→ [bossagent_item_type] → ItemType (多对一)

BossAgent_RuleContribution ──→ [源规则ID] → BossAgent_Rule (多对一)
BossAgent_RuleContribution ──→ [提交者] → Identity (多对一)

四、SQLite 用户私有库设计

4.1 数据库位置

server/data/user_libraries/user_{userId}.db

4.2 表结构

#### user_rules(规则)

| 列 | 类型 | 说明 |

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

| id | TEXT PK | 本地唯一ID (ur_timestamp_random) |

| standard_id | TEXT | 对应标准规则ID (sciot_rules_v2.id) |

| name | TEXT | 规则名称 |

| category | TEXT | 分类 |

| item_type_name | TEXT | 关联ItemType |

| condition | TEXT | 条件(JSON) |

| action_type | TEXT | 动作类型 |

| action_config | TEXT | 动作配置(JSON) |

| priority | INTEGER | 优先级 |

| severity | TEXT | info/warning/error |

| scope | TEXT | identify/create/validate/... |

| enabled | INTEGER | 0/1 |

| source | TEXT | standard_copy/modified/custom |

| origin_version | INTEGER | 基于标准版本号 |

| current_version | INTEGER | 当前本地版本 |

| synced_to_server | INTEGER | 是否已提交 |

| sync_status | TEXT | none/pending/approved/rejected |

| created_at | TEXT | |

| updated_at | TEXT | |

| local_modified_at | TEXT | |

索引: standard_id, source

#### user_templates(模板)

| 列 | 类型 | 说明 |

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

| id | TEXT PK | ut_timestamp_random |

| standard_id | TEXT | sciot_templates.id |

| name | TEXT | |

| type | TEXT | template_type |

| item_type_name | TEXT | |

| content | TEXT | 模板内容(JSON) |

| variables | TEXT | 变量(JSON数组) |

| category | TEXT | |

| source | TEXT | standard_copy/modified/custom |

| origin_version | INTEGER | |

| current_version | INTEGER | |

| synced_to_server | INTEGER | |

| sync_status | TEXT | |

| created_at | TEXT | |

| updated_at | TEXT | |

#### user_prompts(提示词)

| 列 | 类型 | 说明 |

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

| id | TEXT PK | up_timestamp_random |

| standard_id | TEXT | prompt_templates.id |

| name | TEXT | |

| type | TEXT | prompt_type |

| item_type_name | TEXT | |

| content | TEXT | 提示词正文 |

| system_prompt | TEXT | 系统提示词 |

| operation_type | TEXT | add/get/edit/generate |

| variables | TEXT | 变量(JSON) |

| temperature | REAL | 0.3 |

| source | TEXT | standard_copy/modified/custom |

| origin_version | INTEGER | |

| current_version | INTEGER | |

| synced_to_server | INTEGER | |

| sync_status | TEXT | |

| created_at | TEXT | |

| updated_at | TEXT | |

#### sync_submissions(提交审核)

| 列 | 类型 | 说明 |

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

| id | TEXT PK | sub_timestamp_random |

| user_id | TEXT | 提交用户 |

| object_type | TEXT | rule/template/prompt |

| object_id | TEXT | 对象ID |

| original_id | TEXT | 原标准ID |

| content | TEXT | 提交内容(JSON) |

| description | TEXT | 提交说明 |

| status | TEXT | pending/approved/rejected |

| review_comment | TEXT | 审核意见 |

| submitted_at | TEXT | |

| reviewed_at | TEXT | |

#### sync_state(同步状态)

| 列 | 类型 | 说明 |

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

| user_id | TEXT PK | |

| last_sync_at | TEXT | |

| standard_version | TEXT | |

| rules_count | INTEGER | |

| templates_count | INTEGER | |

| prompts_count | INTEGER | |

五、代码改动清单

5.1 新增文件(3个)

文件职责大小估计
server/services/user-library.js用户库SQLite管理+CRUD+优先级合并~400行
server/services/sync-service.js标准库→用户库同步~200行
server/routes/user-library-routes.jsREST API路由~280行

5.2 修改文件(2个)

文件改动
server.js注册 userLibraryRoutes 到 require + handleRequest
server/routes/aml.jssystem-status 500 修复 (try-catch sciot_item_types)

5.3 API 列表

GET  /api/user-library/stats
GET  /api/user-library/rules?category=&source=&item_type_name=&scope=
GET  /api/user-library/templates?source=&type=&item_type_name=
GET  /api/user-library/prompts?source=&type=&item_type_name=&operation_type=
GET  /api/user-library/rules/:id
POST /api/user-library/rules           { name, category, condition, ... }
PUT  /api/user-library/rules/:id       { condition, action_config, ... }
DELETE /api/user-library/rules/:id
POST /api/user-library/templates       { name, type, content, ... }
PUT  /api/user-library/templates/:id
DELETE /api/user-library/templates/:id
POST /api/user-library/prompts         { name, type, content, ... }
PUT  /api/user-library/prompts/:id
DELETE /api/user-library/prompts/:id
POST /api/user-library/sync/standard   { force?, type? }
POST /api/user-library/submit          { object_type, object_id, description }
GET  /api/user-library/submissions/pending

六、实施顺序

Phase 1: 基础层(必须按序)

  1. 修复 system-status 500 ← 阻止其他验证
  2. user-library.js ← 用户库核心
  3. sync-service.js ← 同步引擎(依赖 #2)
  4. user-library-routes.js ← REST API(依赖 #2, #3)
  5. server.js 注册 ← 路由挂载

Phase 2: SCSAI 对象类(需要时)

  1. 通过 SCSAI API 创建 BossAgent_Rule/Template/Prompt ItemType
  2. 双向同步:用户提交 → SCSAI审核 → 回写标准库

Phase 3: aml.js 适配(按需逐步)

  1. 规则执行优先级 (getEffectiveRules)
  2. 模板/提示词优先级覆盖
  3. 前端规则管理 UI

七、不做的

  • ❌ 不改 aml.js 核心逻辑(只加 try-catch 修复 500)
  • ❌ 不改 sciot_import.db 表结构(标准库保持只读)
  • ❌ 不改现有 API 响应格式(向后兼容)

确认以上方案后开始写代码。需要调整的地方?

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