Spaces:
Runtime error
Runtime error
File size: 1,744 Bytes
c6421b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import re
from langchain_core.messages import ToolMessage
from app.state.state import EmailAgentState
def parse_response_node(state: EmailAgentState) -> dict:
messages = state["messages"]
updates = {}
# 1. Find the successful SEND message to get the ID
# We scan backwards for the first ToolMessage from 'send_draft_by_id'
send_tool_msg = next((m for m in reversed(messages)
if isinstance(m, ToolMessage) and m.name == "send_draft_by_id"), None)
if send_tool_msg and "SUCCESS" in send_tool_msg.content.upper():
id_match = re.search(r'<id>(.*?)</id>', send_tool_msg.content)
if id_match:
updates["sent_message_id"] = id_match.group(1)
# 2. Find the most recent successful DRAFT creation
# This is the "lookback" that ignores all the rejections and rewrites
draft_tool_msg = next((m for m in reversed(messages)
if isinstance(m, ToolMessage) and m.name == "create_gmail_draft"
and "Successfully" in m.content), None)
if draft_tool_msg:
# Extract the content from the tags you defined in the tool
subject_match = re.search(r'<subject>(.*?)</subject>', draft_tool_msg.content)
body_match = re.search(r'<body>(.*?)</body>', draft_tool_msg.content, re.DOTALL)
if subject_match:
updates["reply_subject"] = subject_match.group(1)
if body_match:
updates["reply_email_body"] = body_match.group(1)
# 3. Final Safety: If we still don't have a body, don't return 'None'
if not updates.get("reply_email_body"):
updates["reply_email_body"] = "Content could not be retrieved from history."
return updates |