34 lines
1.7 KiB
Markdown
34 lines
1.7 KiB
Markdown
---
|
||
name: script_engine
|
||
description: 逻辑编程(脚本/规则引擎)模块——脚本表 CRUD + execute_script/validate_script 接口,通过 load_script_engine() 挂载。
|
||
---
|
||
|
||
# script_engine 模块
|
||
|
||
## 概述
|
||
逻辑编程(脚本/规则引擎)模块。管理 `script_engine` 表(脚本/规则),提供脚本执行与语法校验接口。依赖 world、scene、entity 模块。
|
||
|
||
## 数据模型
|
||
表 `script_engine`:`id`、`world_id`(→world)、`scene_id`(→scene)、`entity_id`(→entity)、`name`、`code`(unique)、`script_type`、`trigger_event`、`content`(text)、`status`、`created_at`、`updated_at`。
|
||
|
||
## 关键接口
|
||
- `api/execute_script.dspy`:输入 `script_id` / `context_json`(可选),按 `script_type` 选择执行器执行 `content`,返回 `{success, result}`。
|
||
- `api/validate_script.dspy`:输入 `content` / `script_type`,语法/格式校验,返回 `{valid, message}`。
|
||
|
||
## 脚本类型编码(appcodes script_type)
|
||
- `0` = Python脚本(受限命名空间,通过 `result` 变量返回结果)
|
||
- `1` = 规则(JSON)(解析 JSON 作为结果)
|
||
- `2` = 表达式(eval 求值)
|
||
|
||
## 状态编码(appcodes script_status)
|
||
- `0` = 禁用、`1` = 启用
|
||
|
||
## 挂载
|
||
宿主应用入口 `init()` 调用 `load_script_engine()`,向 ServerEnv 注册 `execute_script` / `validate_script` / `create_script` / `update_script` / `delete_script`。
|
||
|
||
## 陷阱
|
||
- 脚本执行使用受限命名空间(`__builtins__={}`),Python 脚本须通过 `result` 变量返回结果。
|
||
- `content` 为 `text` 字段:列表页隐藏(`browserfields.exclouded`),编辑页显示。
|
||
- `code` 唯一索引,create 时校验重复。
|
||
- 取库名统一 `ServerEnv().get_module_dbname('script_engine')`,禁止硬编码 DBNAME。
|