Spaces:
Runtime error
Runtime error
File size: 3,423 Bytes
915e513 dd57d13 4d93715 915e513 4d93715 915e513 4d93715 915e513 4d93715 915e513 4d93715 915e513 4d93715 | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import base64
from email.mime.text import MIMEText
from langchain_core.tools import tool
from langgraph.types import interrupt
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.auth.exceptions import RefreshError
from google_auth_helpers import (
build_auth_required,
get_google_credentials,
is_auth_failure,
)
def _gmail_service(
user_id: str,
thread_id: str | None = None,
):
creds, auth_info = get_google_credentials(
user_id=user_id,
service="Gmail",
thread_id=thread_id,
)
if auth_info:
interrupt(auth_info)
if creds is None:
interrupt(
build_auth_required(
user_id=user_id,
service="Gmail",
thread_id=thread_id,
)
)
return build(
"gmail",
"v1",
credentials=creds,
)
def _create_message(
to_email: str,
subject: str,
body: str,
):
message = MIMEText(body)
message["To"] = to_email
message["Subject"] = subject
encoded_message = base64.urlsafe_b64encode(
message.as_bytes()
).decode()
return {
"raw": encoded_message
}
def _send_email(
user_id: str,
to_email: str,
subject: str,
body: str,
thread_id: str | None = None,
) -> str:
"""
Sends an email using the user's connected Gmail account.
Args:
user_id: The authenticated application user ID.
to_email: Recipient email address.
subject: Email subject.
body: Email body.
"""
service = _gmail_service(
user_id=user_id,
thread_id=thread_id,
)
try:
message = _create_message(
to_email=to_email,
subject=subject,
body=body,
)
result = (
service
.users()
.messages()
.send(
userId="me",
body=message,
)
.execute()
)
return (
f"Email successfully sent to "
f"{to_email}."
)
except RefreshError as e:
print(
f"[GMAIL AUTH] RefreshError "
f"for {user_id}: {repr(e)}"
)
interrupt(
build_auth_required(
user_id=user_id,
service="Gmail",
thread_id=thread_id,
revoke=True,
)
)
except HttpError as e:
print(
f"[GMAIL] HttpError "
f"for {user_id}: {repr(e)}"
)
if is_auth_failure(e):
interrupt(
build_auth_required(
user_id=user_id,
service="Gmail",
thread_id=thread_id,
revoke=True,
)
)
return f"Gmail send failed: {e}"
except Exception as e:
print(
f"[GMAIL] Unexpected error: "
f"{repr(e)}"
)
return f"Failed to send email: {e}"
@tool
def send_email_tool(
to_email: str,
subject: str,
body: str,
) -> str:
"""
Sends an email using the authenticated user's Gmail account.
The backend supplies the authenticated user.
"""
raise RuntimeError(
"send_email_tool must be executed through the backend."
) |