Spaces:
Runtime error
Runtime error
File size: 1,689 Bytes
9cb712d | 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 41 42 43 44 45 46 47 48 49 50 51 52 53 | from app.database.models import User, ReceivedEmail, SentEmail
from sqlalchemy.orm import Session
def get_or_create_user(session, email: str):
user = session.query(User).filter_by(email=email).first()
if not user:
user = User(email=email)
session.add(user)
session.commit()
session.refresh(user)
return user
def save_received_email(session: Session, owner_id: int,thread_id: str, state: dict) -> ReceivedEmail:
"""
Persists the incoming email and the agent's initial triage results.
"""
email = ReceivedEmail(
owner_id = owner_id,
thread_id = thread_id,
sender_email = state.get("sender_email_id"),
subject = state.get("sender_subject"),
body = state.get("sender_email_body"),
is_safe = state.get("is_safe"),
)
session.add(email)
# session.flush() # Use flush to get the ID without closing the transaction
session.commit() # Only commit here if this is a standalone operation
return email
def save_sent_email(session: Session, sender_id: int, thread_id: str,state: dict) -> SentEmail:
"""
Persists the final outbound reply sent by the agent.
"""
# Ensure we don't save an empty email if the agent decided not to reply
email = SentEmail(
sender_id = sender_id,
thread_id = thread_id,
recipient_email = state.get("sender_email_id"),
subject = state.get("reply_subject") or f"Re: {state.get('sender_subject')}",
body = state.get("reply_email_body"),
)
session.add(email)
session.commit()
return email
|