Skip to content

Agent Zero AI framework

improve: include compressed conversation history in dispatcher context

linuztx22e4ad911fee
diff --git a/plugins/_email_integration/helpers/dispatcher.py b/plugins/_email_integration/helpers/dispatcher.py
index d0eed45bc..157d84dce 100644
--- a/plugins/_email_integration/helpers/dispatcher.py
+++ b/plugins/_email_integration/helpers/dispatcher.py
@@ -61,13 +61,17 @@ def build_chat_summary(context_id: str, data: dict) -> dict:
def format_chats_list(existing_chats: list[dict]) -> str:
if not existing_chats:
return "No existing chats for this handler."
- lines = []
+ sections = []
for c in existing_chats[:20]:
- lines.append(
+ header = (
f"- context_id={c['context_id']} thread_id={c.get('thread_id', '')} "
f"sender={c.get('sender', '')} subject={c.get('subject', '')}"
)
- return "\n".join(lines)
+ preview = c.get("history_preview", "")
+ if preview:
+ header += f"\n conversation:\n {preview}"
+ sections.append(header)
+ return "\n".join(sections)
def parse_dispatcher_response(response: str) -> DispatchDecision:
diff --git a/plugins/_email_integration/helpers/handler.py b/plugins/_email_integration/helpers/handler.py
index bc273e80c..7d768b333 100644
--- a/plugins/_email_integration/helpers/handler.py
+++ b/plugins/_email_integration/helpers/handler.py
@@ -300,6 +300,9 @@ async def _route_to_chat(
# Chat discovery
# ------------------------------------------------------------------
+HISTORY_PREVIEW_MAX_CHARS: int = 500
+
+
def _find_handler_chats(handler_name: str, sender: str) -> list[dict]:
results = []
for ctx_id, ctx in AgentContext._contexts.items():
@@ -310,12 +313,27 @@ def _find_handler_chats(handler_name: str, sender: str) -> list[dict]:
continue
if data.get(disp.CTX_EMAIL_SENDER, "").lower() != sender.lower():
continue
- results.append(disp.build_chat_summary(ctx_id, data))
+ summary = disp.build_chat_summary(ctx_id, data)
+ summary["history_preview"] = _get_history_preview(ctx)
+ results.append(summary)
results.sort(key=lambda c: c["context_id"], reverse=True)
return results[:20]
+def _get_history_preview(ctx: AgentContext) -> str:
+ try:
+ history = ctx.agent0.history
+ text = history.output_text(human_label="user", ai_label="agent")
+ if not text:
+ return "(empty)"
+ if len(text) > HISTORY_PREVIEW_MAX_CHARS:
+ return "..." + text[-HISTORY_PREVIEW_MAX_CHARS:]
+ return text
+ except Exception:
+ return "(unavailable)"
+
+
# ------------------------------------------------------------------
# Message builders
# ------------------------------------------------------------------