fix: inject thread_id into LangGraph context for compact conversation (#112)

This commit is contained in:
Xi Zhang
2026-03-27 21:46:12 +01:00
committed by GitHub
parent 4a9562229f
commit 6e23e98cca
+10
View File
@@ -306,12 +306,22 @@ async def compact_conversation(agent: Any, thread_id: str | None) -> CompactResu
# Generate summary (LLM call)
summary = await middleware._acreate_summary(to_summarize)
# Inject thread_id into LangGraph contextvar so _get_thread_id() finds it
# (compact runs outside a runnable context, so get_config() would fail
# and the middleware would generate a random "session_xxx" filename instead
# of reusing the real thread_id).
from langgraph.config import var_child_runnable_config
_token = var_child_runnable_config.set(config)
# Offload old messages to backend
file_path: str | None = None
try:
file_path = await middleware._aoffload_to_backend(backend, to_summarize)
except Exception:
pass # non-fatal — proceed without offloaded history
finally:
var_child_runnable_config.reset(_token)
summary_msg = middleware._build_new_messages_with_path(summary, file_path)[0]