Skip to content

Agent Zero AI framework

fix: reuse background context instead of AgentContext.first()

linuztxf840b8489d5c
diff --git a/plugins/_email_integration/helpers/handler.py b/plugins/_email_integration/helpers/handler.py
index 37441a0de..4c23a166d 100644
--- a/plugins/_email_integration/helpers/handler.py
+++ b/plugins/_email_integration/helpers/handler.py
@@ -143,8 +143,14 @@ async def _fetch_exchange(
async def _dispatch_all(handler_cfg: dict, messages: list[InboundMessage]):
own_address = (handler_cfg.get("username") or "").lower()
- # Need an agent for dispatcher AI calls — use first available or create temp
- ctx = AgentContext.first()
+ # Need an agent for dispatcher AI calls
+ # find existing dispatcher or create new background context
+ ctx = None
+ for c in AgentContext._contexts.values():
+ if isinstance(c, AgentContext) and c.name == "Email Dispatcher":
+ ctx = c
+ break
+
if not ctx:
agent_config = initialize_agent()
ctx = AgentContext(agent_config, name="Email Dispatcher",