39 lines
1.5 KiB
Python
39 lines
1.5 KiB
Python
"""暂停图并接收用户回答,不在恢复时重复调用模型"""
|
|
|
|
from langgraph.types import interrupt
|
|
from pydantic import ValidationError
|
|
|
|
from app.processor.base import BaseNode
|
|
from app.processor.state import QueryState
|
|
from app.schemas.clarification_reply import ClarificationReply
|
|
|
|
|
|
class WaitForUserNode(BaseNode):
|
|
name = "wait_for_user_node"
|
|
|
|
def process(self, state: QueryState) -> QueryState:
|
|
updated_state: QueryState = state.copy()
|
|
payload = state["clarification"].model_dump(mode="json")
|
|
while True:
|
|
# interrupt 的控制异常必须交给 LangGraph 处理
|
|
value = interrupt(payload)
|
|
try:
|
|
reply = ClarificationReply.model_validate(
|
|
{"answer": value} if isinstance(value, str) else value
|
|
)
|
|
if not reply.cancel and reply.option_id is not None:
|
|
if reply.option_id not in {
|
|
option.id for option in state["clarification"].options
|
|
}:
|
|
raise ValueError("无效选项")
|
|
break
|
|
except (ValidationError, ValueError):
|
|
payload = {
|
|
**payload,
|
|
"error": "请选择本题有效选项或填写回答,并提供有效的大屏数据;也可设置 cancel=true 取消",
|
|
}
|
|
|
|
updated_state["clarification_reply"] = reply
|
|
updated_state["status"] = "cancelled" if reply.cancel else "running"
|
|
return updated_state
|