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
@@ -7,6 +7,7 @@ from langgraph.runtime import Runtime
from deerflow.agents.thread_state import SandboxState, ThreadDataState
from deerflow.sandbox import get_sandbox_provider
from deerflow.utils.runtime import get_thread_id
logger = logging.getLogger(__name__)
@@ -56,7 +57,7 @@ class SandboxMiddleware(AgentMiddleware[SandboxMiddlewareState]):
# Eager initialization (original behavior)
if "sandbox" not in state or state["sandbox"] is None:
thread_id = (runtime.context or {}).get("thread_id")
thread_id = get_thread_id(runtime)
if thread_id is None:
return super().before_agent(state, runtime)
sandbox_id = self._acquire_sandbox(thread_id)