"""输入解析与校验,可直接运行本文件修改示例进行调试""" 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 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 的状态合并不会删除省略字段 updated_state["input_context"] = context updated_state["clarification_reply"] = None updated_state["intent_decision"] = None updated_state["intent"] = None updated_state["requirement"] = None updated_state["change_scope"] = None updated_state["screen_plan"] = None updated_state["component_resources"] = None updated_state["actions"] = None updated_state["validation_passed"] = None updated_state["validation_errors"] = [] updated_state["simulated_screen"] = None updated_state["retry_count"] = 0 updated_state["repair_plan"] = None updated_state["failure_reason"] = None updated_state["clarification"] = None updated_state["answer"] = None updated_state["status"] = "running" return updated_state # 深拷贝隔离原始请求,后续修改上下文不会改变提交时的快照 context = InputContext.model_validate(state["request"]).model_copy(deep=True) updated_state["input_context"] = context updated_state["retry_count"] = 0 updated_state["repair_plan"] = None updated_state["failure_reason"] = None # 首次请求进入处理状态,恢复澄清时也使用同一状态 updated_state["status"] = "running" 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))