Files
my-v0-server/app/processor/nodes/input_node.py
T

96 lines
3.5 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:
reply = state.get("clarification_reply")
if reply is not None:
data = state["input_context"].model_dump(mode="json")
turn = {
"question": state["clarification"].question,
"answer": reply.answer,
}
if reply.option_id is not None:
option = next(
option
for option in state["clarification"].options
if option.id == reply.option_id
)
turn["selected_option"] = option.model_dump(mode="json")
data["clarification_history"].append(turn)
if reply.screen is not None:
data["screen"] = reply.screen.model_dump(mode="json")
# 新快照不沿用可能已经失效的选中节点
if reply.context is None:
data["context"]["selected_node_ids"] = []
data["context"]["screen_revision"] = None
if reply.context is not None:
data["context"] = reply.context.model_dump(mode="json")
context = InputContext.model_validate(data)
# 显式清空旧推导结果,LangGraph 的状态合并不会删除省略字段
return {
**state,
"input_context": context,
"clarification_reply": None,
"intent_decision": None,
"intent": None,
"requirement": None,
"clarification": None,
"status": "running",
}
# 深拷贝隔离原始请求,后续修改上下文不会改变提交时的快照
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))