增加简单模式root键

This commit is contained in:
2025-09-25 21:57:32 +08:00
parent 8e333ac03f
commit bef742db1b
13 changed files with 26 additions and 24 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 200 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

@@ -3,14 +3,14 @@
输出要求(必须遵守):
- 只输出一个JSON对象不要任何解释或多余文本。
- JSON结构
{"mode":"simple","action":{"name":"<action_name>","params":{...}}}
{"root":{"type":"action","name":"<action_name>","params":{...}}}
- <action_name> 与参数定义、取值范围必须与“复杂模式”提示词system_prompt.txt中的定义完全一致。
- 简单模式下不包含任何行为树结构与安全监控并行,仅输出单一原子动作
- 简单模式下root节点必须是action类型节点不能是控制流节点
示例:
- “起飞到10米” → {"mode":"simple","action":{"name":"takeoff","params":{"altitude":10.0}}}
- “移动到(120,80,20)” → {"mode":"simple","action":{"name":"fly_to_waypoint","params":{"x":120.0,"y":80.0,"z":20.0,"acceptance_radius":2.0}}}
- “飞机自检” → {"mode":"simple","action":{"name":"preflight_checks","params":{"check_level":"comprehensive"}}}
- “起飞到10米” → {"root":{"type":"action","name":"takeoff","params":{"altitude":10.0}}}
- “移动到(120,80,20)” → {"root":{"type":"action","name":"fly_to_waypoint","params":{"x":120.0,"y":80.0,"z":20.0,"acceptance_radius":2.0}}}
- “飞机自检” → {"root":{"type":"action","name":"preflight_checks","params":{"check_level":"comprehensive"}}}
—— 可用节点定义——
```json

View File

@@ -337,28 +337,35 @@ def _generate_pytree_schema(allowed_actions: set, allowed_conditions: set) -> di
def _generate_simple_mode_schema(allowed_actions: set) -> dict:
"""
生成简单模式JSON Schema{"mode":"simple","action":{...}}
生成简单模式JSON Schema{"root":{"type":"action","name":"<action_name>","params":{...}}}
仅校验动作名称在允许集合内,以及基本结构完整性;参数按对象形状放宽,由上游提示词与运行时再约束。
"""
# 使用复杂模式Schema中的node定义但限制root节点必须是action类型
node_definition = {
"type": "object",
"properties": {
"type": {"type": "string", "const": "action"},
"name": {"type": "string", "enum": sorted(list(allowed_actions))},
"params": {"type": "object"}
},
"required": ["type", "name"],
"additionalProperties": False
}
schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "SimpleMode",
"definitions": {
"node": node_definition
},
"type": "object",
"properties": {
"mode": {"type": "string", "const": "simple"},
"action": {
"type": "object",
"properties": {
"name": {"type": "string", "enum": sorted(list(allowed_actions))},
"params": {"type": "object"}
},
"required": ["name"],
"additionalProperties": True
}
"root": { "$ref": "#/definitions/node" }
},
"required": ["mode", "action"],
"required": ["root"],
"additionalProperties": False
}
return schema
def _validate_pytree_with_schema(pytree_instance: dict, schema: dict) -> bool:
@@ -689,16 +696,11 @@ class PyTreeGenerator:
# 附加元信息并生成简单可视化(单动作)
plan_id = str(uuid.uuid4())
pytree_dict['plan_id'] = plan_id
# 简单模式可视化:构造一个简化节点
# 简单模式可视化:直接使用root节点
try:
vis_filename = "py_tree.png"
vis_path = os.path.join(self.vis_dir, vis_filename)
simple_node = {
"type": "action",
"name": pytree_dict.get('action', {}).get('name', 'action'),
"params": pytree_dict.get('action', {}).get('params', {})
}
_visualize_pytree(simple_node, os.path.splitext(vis_path)[0])
_visualize_pytree(pytree_dict['root'], os.path.splitext(vis_path)[0])
pytree_dict['visualization_url'] = f"/static/{vis_filename}"
except Exception as e:
logging.warning(f"简单模式可视化失败: {e}")

Binary file not shown.