61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
"""输入解析与校验,可直接运行本文件修改示例进行调试"""
|
|
|
|
from app.processor.base import BaseNode
|
|
from app.processor.state import QueryState
|
|
from app.schemas.input_context import InputContext
|
|
|
|
|
|
class InputNode(BaseNode):
|
|
"""纯输入节点,不依赖模型配置;校验失败保留 ValidationError"""
|
|
|
|
name = "input_node"
|
|
|
|
def process(self, state: QueryState) -> QueryState:
|
|
# 深拷贝隔离原始请求,后续修改上下文不会改变提交时的快照
|
|
context = InputContext.model_validate(state["request"]).model_copy(deep=True)
|
|
updated_state: QueryState = {**state, "input_context": context}
|
|
return updated_state
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import json
|
|
|
|
# 1. 创建 state:可将 screen 替换成前端的完整大屏 JSON
|
|
state: QueryState = {
|
|
"request": {
|
|
"prompt": "把销售额统计向右移动 50px",
|
|
"screen": {
|
|
"canvas": {
|
|
"width": 1920,
|
|
"height": 1080,
|
|
"backgroundColor": "#ffffff10",
|
|
},
|
|
"nodes": [
|
|
{
|
|
"id": "sales-chart",
|
|
"type": "bar-chart",
|
|
"name": "销售额统计",
|
|
"layout": {"x": 26, "y": 34.8, "width": 400, "height": 260},
|
|
"props": {"option": {"title": {"text": "销售额统计"}}},
|
|
}
|
|
],
|
|
"dataSource": [],
|
|
},
|
|
"context": {"selected_node_ids": ["sales-chart"]},
|
|
"attachments": [],
|
|
}
|
|
}
|
|
|
|
# 2. 执行节点
|
|
node = InputNode()
|
|
final_state = node.process(state)
|
|
|
|
# 3. 打印完整状态
|
|
printable_state = {
|
|
**final_state,
|
|
"input_context": final_state["input_context"].model_dump(
|
|
mode="json", exclude_unset=True
|
|
),
|
|
}
|
|
print(json.dumps(printable_state, ensure_ascii=False, indent=2))
|