Skip to content

Agent Zero AI framework

improve: propagate send errors back to agent for retry on SMTP failures

linuztx3dcffe662c36
diff --git a/plugins/_email_integration/extensions/python/process_chain_end/_55_email_reply.py b/plugins/_email_integration/extensions/python/process_chain_end/_55_email_reply.py
index 9eeed9b5c..4d5d08a89 100644
--- a/plugins/_email_integration/extensions/python/process_chain_end/_55_email_reply.py
+++ b/plugins/_email_integration/extensions/python/process_chain_end/_55_email_reply.py
@@ -3,7 +3,7 @@
import asyncio
from helpers.extension import Extension
from helpers.print_style import PrintStyle
-from agent import AgentContext, LoopData
+from agent import AgentContext, LoopData, UserMessage
from plugins._email_integration.helpers.dispatcher import CTX_EMAIL_HANDLER, CTX_EMAIL_ATTACHMENTS
@@ -30,7 +30,9 @@ class EmailAutoReply(Extension):
self, context: AgentContext, response_text: str, attachments: list[str],
):
from plugins._email_integration.helpers.handler import send_email_reply
- await send_email_reply(context, response_text, attachments)
+ error = await send_email_reply(context, response_text, attachments)
+ if error:
+ _notify_agent_of_failure(context, error)
# ------------------------------------------------------------------
@@ -46,3 +48,10 @@ def _extract_last_response(context: AgentContext) -> str:
if item.type == "response":
return item.content or ""
return ""
+
+
+def _notify_agent_of_failure(context: AgentContext, error: str):
+ from plugins._email_integration.helpers.handler import _read_fw
+ msg = _read_fw("fw.email.send_failed.md", error=error)
+ context.log.log(type="error", heading="Email send failed", content=error)
+ context.communicate(UserMessage(message="", system_message=[msg]))
diff --git a/plugins/_email_integration/helpers/handler.py b/plugins/_email_integration/helpers/handler.py
index 6376042b3..a26367d53 100644
--- a/plugins/_email_integration/helpers/handler.py
+++ b/plugins/_email_integration/helpers/handler.py
@@ -333,15 +333,14 @@ async def send_email_reply(
context: AgentContext,
response_text: str,
attachments: list[str] | None = None,
-):
+) -> str | None:
handler_name = context.data.get(disp.CTX_EMAIL_HANDLER)
if not handler_name:
- return
+ return "No email handler configured"
cfg = _get_handler_config(handler_name)
if not cfg:
- PrintStyle.error(f"Email: handler config not found for '{handler_name}'")
- return
+ return f"Handler config not found for '{handler_name}'"
sender = context.data.get(disp.CTX_EMAIL_SENDER, "")
original_subject = context.data.get(disp.CTX_EMAIL_SUBJECT, "")
@@ -361,7 +360,7 @@ async def send_email_reply(
# Read attachment files via RFC (they live in the execution runtime)
attachment_data = await _read_attachments_via_rfc(attachments)
- await send_reply(
+ return await send_reply(
config=smtp_cfg,
to=sender,
subject=subject,
diff --git a/plugins/_email_integration/helpers/smtp_client.py b/plugins/_email_integration/helpers/smtp_client.py
index c7a3b53a5..fc8fbba64 100644
--- a/plugins/_email_integration/helpers/smtp_client.py
+++ b/plugins/_email_integration/helpers/smtp_client.py
@@ -41,7 +41,7 @@ async def send_reply(
in_reply_to: str = "",
references: str = "",
attachments: list[tuple[str, bytes]] | None = None,
-) -> bool:
+) -> str | None:
loop = asyncio.get_event_loop()
def _sync_send():
@@ -82,7 +82,8 @@ async def send_reply(
try:
await loop.run_in_executor(None, _sync_send)
PrintStyle.success(f"Email sent to {to}: {subject}")
- return True
+ return None
except Exception as e:
- PrintStyle.error(f"Email send failed: {format_error(e)}")
- return False
+ error = format_error(e)
+ PrintStyle.error(f"Email send failed: {error}")
+ return error
diff --git a/plugins/_email_integration/prompts/fw.email.send_failed.md b/plugins/_email_integration/prompts/fw.email.send_failed.md
new file mode 100644
index 000000000..203ee1765
--- /dev/null
+++ b/plugins/_email_integration/prompts/fw.email.send_failed.md
@@ -0,0 +1,3 @@
+email send failed: {{error}}
+previous response was NOT delivered to user
+retry without problematic content or inform user via alternative means
diff --git a/plugins/_email_integration/tools/email_update.py b/plugins/_email_integration/tools/email_update.py
index 647fdbee4..4257a2b75 100644
--- a/plugins/_email_integration/tools/email_update.py
+++ b/plugins/_email_integration/tools/email_update.py
@@ -28,7 +28,15 @@ class EmailUpdate(Tool):
attachments = list(self.args.get("attachments", []))
from plugins._email_integration.helpers.handler import send_email_reply
- await send_email_reply(self.agent.context, text, attachments or None)
+ error = await send_email_reply(self.agent.context, text, attachments or None)
+
+ if error:
+ return Response(
+ message=self.agent.read_prompt(
+ "fw.email.update_error.md", error=error,
+ ),
+ break_loop=False,
+ )
return Response(
message=self.agent.read_prompt("fw.email.update_ok.md"),