Files
my-v0-server/app/processor/nodes/input_node.py
T
2026-10-14 08:19:52 -07:00

59 lines
2.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:
updated_state: QueryState = state.copy()
reply = state.get("clarification_reply")
if reply is None:
# 首次请求:校验原始输入,并深拷贝隔离提交时的快照
updated_state["input_context"] = InputContext.model_validate(
state["request"]
).model_copy(deep=True)
updated_state["status"] = "running"
return updated_state
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")
updated_state["input_context"] = InputContext.model_validate(data)
# LangGraph 的状态合并不会删除省略字段,需要显式清空上一轮的结果,再重新判断意图
updated_state["clarification_reply"] = None
updated_state["intent_decision"] = None
updated_state["intent"] = None
updated_state["actions"] = None
updated_state["clarification"] = None
updated_state["answer"] = None
updated_state["suggestions"] = None
updated_state["changes"] = None
updated_state["queries"] = None
updated_state["status"] = "running"
return updated_state