From ed9406732d5be7c7063efc2695e2fd6a6a6c1b7d Mon Sep 17 00:00:00 2001 From: zcr Date: Mon, 30 Mar 2026 16:05:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dcheckpoint=5Fid=20=E5=81=B6?= =?UTF-8?q?=E5=8F=91=E8=8E=B7=E5=8F=96=E4=B8=8D=E5=88=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routers/deep_agent_chat.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) 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