Skip to content

Agent Zero AI framework

fix: maintain full references chain for thread grouping and append quoted previous message

linuztxf2b8ae44a943
diff --git a/plugins/_email_integration/helpers/dispatcher.py b/plugins/_email_integration/helpers/dispatcher.py
index e590da51b..a6284175c 100644
--- a/plugins/_email_integration/helpers/dispatcher.py
+++ b/plugins/_email_integration/helpers/dispatcher.py
@@ -19,6 +19,7 @@ CTX_EMAIL_THREAD_ID = "email_thread_id"
CTX_EMAIL_SUBJECT = "email_subject"
CTX_EMAIL_MESSAGE_ID = "email_message_id"
CTX_EMAIL_REFERENCES = "email_references"
+CTX_EMAIL_LAST_BODY = "email_last_body"
# Transient — consumed per-reply, not persisted
CTX_EMAIL_ATTACHMENTS = "_email_response_attachments"
diff --git a/plugins/_email_integration/helpers/handler.py b/plugins/_email_integration/helpers/handler.py
index 4c23a166d..7f7b70019 100644
--- a/plugins/_email_integration/helpers/handler.py
+++ b/plugins/_email_integration/helpers/handler.py
@@ -269,8 +269,18 @@ async def _start_new_chat(agent: Agent, handler_cfg: dict, msg: InboundMessage):
context.data[disp.CTX_EMAIL_SENDER] = msg.sender
context.data[disp.CTX_EMAIL_THREAD_ID] = thread_id
context.data[disp.CTX_EMAIL_SUBJECT] = msg.subject
+ context.data[disp.CTX_EMAIL_LAST_BODY] = msg.body
context.data[disp.CTX_EMAIL_MESSAGE_ID] = msg.message_id
- context.data[disp.CTX_EMAIL_REFERENCES] = msg.references
+
+ refs_list = []
+ if msg.references:
+ for r in msg.references.split():
+ if r not in refs_list:
+ refs_list.append(r)
+ if msg.message_id and msg.message_id not in refs_list:
+ refs_list.append(msg.message_id)
+
+ context.data[disp.CTX_EMAIL_REFERENCES] = " ".join(refs_list)
project = handler_cfg.get("project", "")
if project:
@@ -302,8 +312,20 @@ async def _route_to_chat(
return
context.data[disp.CTX_EMAIL_MESSAGE_ID] = msg.message_id
+ context.data[disp.CTX_EMAIL_LAST_BODY] = msg.body
+
+ refs = context.data.get(disp.CTX_EMAIL_REFERENCES, "")
+ refs_list = refs.split() if refs else []
+
if msg.references:
- context.data[disp.CTX_EMAIL_REFERENCES] = msg.references
+ for r in msg.references.split():
+ if r not in refs_list:
+ refs_list.append(r)
+
+ if msg.message_id and msg.message_id not in refs_list:
+ refs_list.append(msg.message_id)
+
+ context.data[disp.CTX_EMAIL_REFERENCES] = " ".join(refs_list)
user_msg = _build_user_message(agent, msg, handler_cfg)
mq.log_user_message(context, user_msg, msg.attachments or [], source=" (email)")
@@ -423,6 +445,11 @@ async def send_email_reply(
# Read attachment files via RFC (they live in the execution runtime)
attachment_data = await _read_attachments_via_rfc(attachments)
+ last_body = context.data.get(disp.CTX_EMAIL_LAST_BODY, "").strip()
+ if last_body:
+ quoted = "\n> " + "\n> ".join(last_body.splitlines())
+ response_text = f"{response_text}\n\nOn previous message:\n{quoted}"
+
return await send_reply(
config=smtp_cfg,
to=sender,