Skip to content

Agent Zero AI framework

improve(email): extract body preview func and remove thread_id from dispatcher prompt

linuztx1aa1190de12e
diff --git a/plugins/_email_integration/helpers/dispatcher.py b/plugins/_email_integration/helpers/dispatcher.py
index 157d84dce..60c01e79a 100644
--- a/plugins/_email_integration/helpers/dispatcher.py
+++ b/plugins/_email_integration/helpers/dispatcher.py
@@ -22,6 +22,8 @@ CTX_EMAIL_REFERENCES = "email_references"
# Transient — consumed per-reply, not persisted
CTX_EMAIL_ATTACHMENTS = "_email_response_attachments"
+BODY_PREVIEW_MAX_CHARS: int = 2000
+
DispatchAction = Literal["new_chat", "continue_chat", "intervene_soft", "intervene_hard"]
@@ -58,14 +60,20 @@ def build_chat_summary(context_id: str, data: dict) -> dict:
# Dispatcher prompt builders
# ------------------------------------------------------------------
+def truncate_body(body: str) -> str:
+ if len(body) <= BODY_PREVIEW_MAX_CHARS:
+ return body
+ return body[:BODY_PREVIEW_MAX_CHARS] + "... (truncated)"
+
+
def format_chats_list(existing_chats: list[dict]) -> str:
if not existing_chats:
return "No existing chats for this handler."
sections = []
for c in existing_chats[:20]:
header = (
- f"- context_id={c['context_id']} thread_id={c.get('thread_id', '')} "
- f"sender={c.get('sender', '')} subject={c.get('subject', '')}"
+ f"- context_id={c['context_id']}"
+ f" sender={c.get('sender', '')} subject={c.get('subject', '')}"
)
preview = c.get("history_preview", "")
if preview:
diff --git a/plugins/_email_integration/helpers/handler.py b/plugins/_email_integration/helpers/handler.py
index 7d768b333..8d2cde586 100644
--- a/plugins/_email_integration/helpers/handler.py
+++ b/plugins/_email_integration/helpers/handler.py
@@ -182,7 +182,7 @@ async def _call_dispatcher(
msg: InboundMessage,
existing_chats: list[dict],
) -> disp.DispatchDecision:
- body_preview = msg.body[:2000] if len(msg.body) > 2000 else msg.body
+ body_preview = disp.truncate_body(msg.body)
chats_text = disp.format_chats_list(existing_chats)
prompt = agent.read_prompt(
diff --git a/plugins/_email_integration/prompts/fw.email.dispatcher_system.md b/plugins/_email_integration/prompts/fw.email.dispatcher_system.md
index 2276d9fbf..43932d4a6 100644
--- a/plugins/_email_integration/prompts/fw.email.dispatcher_system.md
+++ b/plugins/_email_integration/prompts/fw.email.dispatcher_system.md
@@ -1,19 +1,19 @@
You route inbound emails to agent chats.
-You receive: the new email (sender, subject, body) and a list of existing chats for this email handler.
+You receive: the new email (sender, subject, body) and a list of existing chats with conversation history.
Rules:
-- If the email subject contains a thread ID marker [a0-XXXX] AND a matching chat exists, reply with CONTINUE and that chat's context_id
-- If no thread ID but the email content clearly relates to an existing chat (same topic, same sender), reply with CONTINUE and the best matching context_id
+- If the email content clearly relates to an existing chat (same topic, same sender), reply with CONTINUE and that chat's context_id
- If the email is a status question or low-priority follow-up on a running chat, reply with INTERVENE_SOFT and the context_id — the agent will receive it after current step
- If the email is urgent and requires immediate attention on a running chat, reply with INTERVENE_HARD and the context_id
- Otherwise reply with NEW_CHAT
+Use conversation history to determine topic relevance when matching emails to chats.
+
Respond with EXACTLY one line in this format:
ACTION context_id reason
Examples:
NEW_CHAT _ new request from user
-CONTINUE ctx_abc123 thread ID matches
-CONTINUE ctx_def456 same sender discussing AWS deployment
+CONTINUE ctx_abc123 same sender discussing AWS deployment
INTERVENE_SOFT ctx_abc123 user asking for status update
INTERVENE_HARD ctx_abc123 urgent correction needed