处理指令更新state
Turn是否有效
send_idtextobject
#4、回填pending_turn,并提交本轮对话 state.commit_pending_turn(bot_messages)
TurnPlanner构建提示词、调用大模型生成执行计划
Session: session_id: str started_at: float last_activity_at: float closed_at: float turns: List[Turn]
尝试收集槽位命令
1、获取当前用户任务
UserMessageDialogueState
判断commands类型
无处理
chat
电商后端:ecommerce-service-backend
stateflowsintents
2、设置槽位
有正执行任务
class DialogueStatedef reset_runtime_state_for_new_session(self): self.active_task = None self.active_system_task = None self.paused_tasks = [] self.focused_object = None
command
消息
ResumedSystemContext(SystemContext): resumed_flow_id:str=\"\" resumed_flow_name:str=\"\"
DialogueState对话状态
启动系统任务
class DialogueState:def start_session(self): session = ChatSession( session_id = str(uuid.uuid4()) ) self.sessions.append(session) self.current_session_id = session.session_id
2、获取目标Flow和正执行任务
TurnPlanValidationResult
class ActionFlowStep(FlowStep): font style=\"font-size: 12px; font-weight: normal;\
订单、商品
无执行任务
FlowsList:yaml 被加载为 FlowsList
恢复目标任务
TEXT
active_system_task: SystemContext 当前用户正在执行的系统任务上下文
messages
active_task:TaskContext 当前用户正在执行的用户任务上下文
设置槽位
#2、创建本轮对话Turn state.start_turn(user_message)
创建新的sesion
是
接口:商品订单查询接口
class FlowSlot: name: str type: str = \"any\" label: str = \"\" description: str = \"\"
DialogueServicedef hand_message(message:UserMessage) -> ProcessResult: state = await self._dialogue_state_repository.load_dialogue(sender_id=message.sender_id) await self._dialogue_state_repository.save_dialogue(state) return process_result
current_session_id: str 当前用户正在进行的会话ID
send_id:str 用户ID
3306
commandsstate
paused_tasks:List[TaskContext] 当前用户暂停的用户上下文
消息流程处理
接口:订单物流查询接口
class FlowStep: id: str type: FlowStepType next: List[FlowStepLink] description: str = \"\"
关闭当前session
OBJECT
Turn: turn_id: str user_message: UserMessage bot_messages: List[BotMessage]
回填更新state的FocusedObject对象
pending_turn:Turn 用户本轮对话信息-缓冲区
#1、准备session session_ = self._prepare_session(state)
挂起原任务
list[botMessage]
FlowsListfont style=\"font-size: 12px; font-weight: normal;\
CommandProcessor
10081
前端:customer-service-frontend
active_task=None?
UserMessage: sender_id:str message_id:str type: MessageType text:str object:FocusedObject
无session
chatRequestsend_idtextobject
Usermessage
获取当前session
进行澄清
判断消息类型
更新当前会话最后活跃时间
预测执行任务
闲聊轨道
2、取消正在执行的用户任务/系统任务
class Flow: id: str description: str = \"\" steps: List[FlowStep] slots: List[FlowSlot] name: str
CollectSystemContext(SystemContext): slot_name:str=\"\
18082
创建新的session
class EndFlowStep(FlowStep):
用户输入
class CollectFlowStep(FlowStep): slot_name: str # 槽位的名字 response: ResponseDefinition # 响应对象(响应内容更丰富) validate: SlotValidate | None = None # 可选的
是否超时>60分?
项目总体架构
校验轮次结果
判断意图
#5、返回processResult
InterruptedSystemContext(SystemContext): interrupted_flow_id:str = \"\" interrupted_flow_name:str = \"\" started_flow_id:str=\"\" started_flow_name:str=\"\"
class DialogueState:def get_current_session(self) -> ChatSession | None: for session in self.sessions: if session.session_id == self.current_session_id: return session return None
2、获取正执行任务
AI客服后端:customer-service-backend
class StartFlowStep(FlowStep):
chatResponsesend_idmessage_idmessages:list[BotMessage]
SystemContext flow_id: str step_id: str
有session
DialogueStateRepository:对数据查询、持久化span style=\"font-weight:normal;\
启动目标任务
恢复流程
flow_id:\"refund_requeste\"step_id:\"ask_refund_reason\"slots:{ \"order_number\":\"A001\
turnplanstateintents
分发到各处理轨道
1、command获取slots
是否为要恢复的目标任务
TurnPlan
list[BotMessage]
取消流程
charify
DialogueEngine:所有对消息的处理都在对话引擎中过
发送
准备会话
knowledge
ChatHistoryResponsessender_id: str messages: list[ChatHistoryMessage]
知识问答轨道
task任务轨道
否
汇总回复内容task/知识库/闲聊/澄清
3、启动新的系统任务
if now - session.last_activity_at >= 60* 60:
class DailogueState:def start_session(self): session = ChatSession(session_id = str(uuid.uuid4())) self.sessions.append(session) self.current_session_id = session.session_id
BotObject: text:str object:FocusedObject
接口:订单退款接口
focused_object: FocusedObject 当前用户对话聚焦的卡片信息(订单、商品)
清理运行时状态
seessions:list[session] 当前用户的所有会话记录信息
1、停止当前的系统任务
澄清轨道
DialogueState
启动流程
ProcessResult
DialogueEngine:处理文本消息方法_handle_text_message: 1、预测轮次计划turn_plan 2、轮次计划验证 3、分轨道处理
5、返回processResultsender_idmessage_idmessages
ProcessResultList[BotMessage]
1、获取要恢复的流程
结束
TurnPlanValidator:验证
是否有command
FastAPI:交互对象->领域模型
StartedSystemContext(SystemContext): started_flow_id:str = \"\" started_flow_name:str = \"\"
CanceledSystemContext(SystemContext): canceled_flow_id:str=\"\" canceled_flow_name:str=\"\"
commands:list[command]
task
TskHandler全流程分析:1、利用指令处理器处理对应的指令 CommandProcessor 2、利用流程推进器推进流程 FlowExecutor