refactor(backend): consolidate thread_id resolution into shared get_thread_id() utility (#2522)

Extract duplicated thread_id fallback logic from 11 files into a single
  deerflow.utils.runtime.get_thread_id() function with a documented 3-level
  cascade (runtime.context → runtime.config → get_config()).

  The module docstring also clarifies the __pregel_runtime injection pattern used in
  gateway mode.
This commit is contained in:
Willem Jiang
2026-04-26 10:52:37 +08:00
parent 9dc25987e0
commit a55de566b9
14 changed files with 185 additions and 84 deletions
@@ -5,12 +5,12 @@ from typing import override
from langchain.agents import AgentState
from langchain.agents.middleware import AgentMiddleware
from langgraph.config import get_config
from langgraph.runtime import Runtime
from deerflow.agents.memory.message_processing import detect_correction, detect_reinforcement, filter_messages_for_memory
from deerflow.agents.memory.queue import get_memory_queue
from deerflow.config.memory_config import get_memory_config
from deerflow.utils.runtime import get_thread_id
logger = logging.getLogger(__name__)
@@ -57,11 +57,8 @@ class MemoryMiddleware(AgentMiddleware[MemoryMiddlewareState]):
if not config.enabled:
return None
# Get thread ID from runtime context first, then fall back to LangGraph's configurable metadata
thread_id = runtime.context.get("thread_id") if runtime.context else None
if thread_id is None:
config_data = get_config()
thread_id = config_data.get("configurable", {}).get("thread_id")
# Get thread ID from runtime context
thread_id = get_thread_id(runtime)
if not thread_id:
logger.debug("No thread_id in context, skipping memory update")
return None