diff --git a/src/routers/deep_agent_chat.py b/src/routers/deep_agent_chat.py index 2cbeb30..42feafd 100644 --- a/src/routers/deep_agent_chat.py +++ b/src/routers/deep_agent_chat.py @@ -187,8 +187,9 @@ async def chat_stream(request: DeepAgentChatRequest): stream_mode=["updates", "messages", "custom"], subgraphs=True ): + _, mode, chunks = stream if is_first: - checkpoint_id = main_agent.get_state(current_config).config.get("configurable").get("checkpoint_id") + checkpoint_id = get_latest_checkpoint_id(main_agent, current_config) if not checkpoint_id: print("123") main_agent.store.put( @@ -202,7 +203,6 @@ async def chat_stream(request: DeepAgentChatRequest): logger.info(f"*******************{checkpoint_id}**********************************") yield f"data: {json.dumps({'thread_id': target_thread_id, 'is_branch': is_branching, 'status': 'start', "checkpoint_id": checkpoint_id}, ensure_ascii=False)}\n\n" is_first = False - _, mode, chunks = stream if mode == "updates": # 只做记录 不做事件返回 logger.info(f"[updates] -- {chunks}") @@ -424,3 +424,22 @@ async def get_branch_checkpoint_id(main_agent, current_config): return item.config.get('configurable', {}).get('checkpoint_id') # 没找到 return None + + +def get_latest_checkpoint_id(agent, config): + # 先尝试直接 get_state + state = agent.get_state(config) + checkpoint_id = state.config.get("configurable", {}).get("checkpoint_id") + + if checkpoint_id: + return checkpoint_id + + # 如果是 None 或空,使用 history 取最新一条(history[0] 永远是最新的) + print("checkpoint_id 为 None,使用 get_state_history 兜底...") + history = list(agent.get_state_history(config)) + if history: + checkpoint_id = history[0].config["configurable"]["checkpoint_id"] + print(f"从 history 获取到最新 checkpoint_id: {checkpoint_id}") + return checkpoint_id + + return None