improve: include compressed conversation history in dispatcher context
linuztx committed
Mar 15, 2026 at 22:08 UTC
22e4ad911fee74a23dff0c7e2273fa9b90257bf8
2 files changed
+26
-4
plugins/_email_integration/helpers/dispatcher.py
+7
-3
@@ -61,13 +61,17 @@ def build_chat_summary(context_id: str, data: dict) -> dict:
61
def format_chats_list(existing_chats: list[dict]) -> str:
62
if not existing_chats:
63
return "No existing chats for this handler."
64
- lines = []
64
+ sections = []
65
for c in existing_chats[:20]:
66
- lines.append(
66
+ header = (
67
f"- context_id={c['context_id']} thread_id={c.get('thread_id', '')} "
68
f"sender={c.get('sender', '')} subject={c.get('subject', '')}"
69
)
70
- return "\n".join(lines)
70
+ preview = c.get("history_preview", "")
71
+ if preview:
72
+ header += f"\n conversation:\n {preview}"
73
+ sections.append(header)
74
+ return "\n".join(sections)
75
76
77
def parse_dispatcher_response(response: str) -> DispatchDecision:
plugins/_email_integration/helpers/handler.py
+19
-1
@@ -300,6 +300,9 @@ async def _route_to_chat(
300
# Chat discovery
301
# ------------------------------------------------------------------
302
303
+HISTORY_PREVIEW_MAX_CHARS: int = 500
304
+
305
+
306
def _find_handler_chats(handler_name: str, sender: str) -> list[dict]:
307
results = []
308
for ctx_id, ctx in AgentContext._contexts.items():
@@ -310,12 +313,27 @@ def _find_handler_chats(handler_name: str, sender: str) -> list[dict]:
313
continue
314
if data.get(disp.CTX_EMAIL_SENDER, "").lower() != sender.lower():
315
continue
313
- results.append(disp.build_chat_summary(ctx_id, data))
316
+ summary = disp.build_chat_summary(ctx_id, data)
317
+ summary["history_preview"] = _get_history_preview(ctx)
318
+ results.append(summary)
319
320
results.sort(key=lambda c: c["context_id"], reverse=True)
321
return results[:20]
322
323
324
+def _get_history_preview(ctx: AgentContext) -> str:
325
+ try:
326
+ history = ctx.agent0.history
327
+ text = history.output_text(human_label="user", ai_label="agent")
328
+ if not text:
329
+ return "(empty)"
330
+ if len(text) > HISTORY_PREVIEW_MAX_CHARS:
331
+ return "..." + text[-HISTORY_PREVIEW_MAX_CHARS:]
332
+ return text
333
+ except Exception:
334
+ return "(unavailable)"
335
+
336
+
337
# ------------------------------------------------------------------
338
# Message builders
339
# ------------------------------------------------------------------