-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.py
More file actions
194 lines (161 loc) · 6.03 KB
/
Copy pathtools.py
File metadata and controls
194 lines (161 loc) · 6.03 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""Gmail LangChain ``@tool`` functions.
Pure HTTP integration against Gmail REST v1 (the legacy implementation
also uses raw httpx rather than the Google Python SDK — preserved
verbatim to keep the dependency surface tiny). Token-based runtime
convention with paired oauth2 + bearer_token auth schemas.
Scope-trimmed to send + label management only: ``send_message`` builds
a base64url MIME message locally, ``list_labels`` reads the account's
labels. Read/search/modify actions were removed alongside the
``gmail.readonly`` and ``gmail.modify`` OAuth scopes.
"""
from __future__ import annotations
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from typing import Any
import httpx
from langchain_core.tools import tool
from pydantic import BaseModel, Field
from modulex_integrations import serialize_pydantic_return
from modulex_integrations.tools.gmail.outputs import (
ListLabelsOutput,
SendMessageOutput,
)
__all__ = [
"list_labels",
"send_message",
]
_API_BASE = "https://www.googleapis.com/gmail/v1"
_TIMEOUT = 30.0
_SEND_TIMEOUT = 60.0
def _headers(auth_type: str, auth_data: dict[str, Any]) -> dict[str, str]:
headers = {"Content-Type": "application/json", "Accept": "application/json"}
if auth_type == "oauth2":
token = auth_data.get("access_token")
elif auth_type == "bearer_token":
token = auth_data.get("token") or auth_data.get("bearer_token")
else:
token = None
if token:
headers["Authorization"] = f"Bearer {token}"
return headers
def _validate(auth_data: dict[str, Any], name: str) -> str | None:
if not (
auth_data.get("access_token")
or auth_data.get("token")
or auth_data.get("bearer_token")
):
return (
f"Gmail access token missing for {name}. "
"Configure a valid credential."
)
return None
def _build_raw_message(
to: str,
subject: str,
body: str,
cc: str | None = None,
bcc: str | None = None,
is_html: bool = False,
) -> str:
"""Build a base64url-encoded MIME message for the Gmail API."""
if is_html:
message: Any = MIMEMultipart("alternative")
message.attach(MIMEText(body, "html"))
else:
message = MIMEText(body)
message["to"] = to
message["subject"] = subject
if cc:
message["cc"] = cc
if bcc:
message["bcc"] = bcc
return base64.urlsafe_b64encode(message.as_bytes()).decode("utf-8")
def _api_err(action: str, response: httpx.Response) -> str:
return f"{action} failed: {response.status_code} - {response.text}"
# --- Input schemas ---------------------------------------------------------
class _AuthFields(BaseModel):
auth_type: str = Field(description="Authentication type (oauth2 or bearer_token)")
auth_data: dict[str, Any] = Field(description="Auth data carrying access_token/token")
class SendMessageInput(_AuthFields):
to: str = Field(description="Recipient email address")
subject: str = Field(description="Email subject line")
body: str = Field(description="Email body content")
cc: str | None = Field(default=None, description="CC recipients (comma-separated)")
bcc: str | None = Field(default=None, description="BCC recipients (comma-separated)")
is_html: bool = Field(default=False, description="Whether body is HTML")
class ListLabelsInput(_AuthFields):
pass
# --- Tools -----------------------------------------------------------------
@tool(args_schema=SendMessageInput)
@serialize_pydantic_return
async def send_message(
auth_type: str,
auth_data: dict[str, Any],
to: str,
subject: str,
body: str,
cc: str | None = None,
bcc: str | None = None,
is_html: bool = False,
) -> SendMessageOutput:
"""Send a new email via Gmail."""
err = _validate(auth_data, "send_message")
if err:
return SendMessageOutput(success=False, error=err)
raw = _build_raw_message(to, subject, body, cc, bcc, is_html)
try:
async with httpx.AsyncClient(timeout=_SEND_TIMEOUT) as client:
response = await client.post(
f"{_API_BASE}/users/me/messages/send",
headers=_headers(auth_type, auth_data),
json={"raw": raw},
)
if response.status_code != 200:
return SendMessageOutput(
success=False, error=_api_err("send_message", response)
)
data = response.json() or {}
except Exception as exc:
return SendMessageOutput(success=False, error=f"send_message failed: {exc}")
return SendMessageOutput(
success=True,
id=data.get("id"),
thread_id=data.get("threadId"),
label_ids=data.get("labelIds") or [],
)
@tool(args_schema=ListLabelsInput)
@serialize_pydantic_return
async def list_labels(
auth_type: str, auth_data: dict[str, Any]
) -> ListLabelsOutput:
"""List all labels in the Gmail account."""
err = _validate(auth_data, "list_labels")
if err:
return ListLabelsOutput(success=False, error=err)
try:
async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
response = await client.get(
f"{_API_BASE}/users/me/labels",
headers=_headers(auth_type, auth_data),
)
if response.status_code != 200:
return ListLabelsOutput(
success=False, error=_api_err("list_labels", response)
)
data = response.json() or {}
except Exception as exc:
return ListLabelsOutput(success=False, error=f"list_labels failed: {exc}")
raw_labels = data.get("labels") or []
labels = [
{
"id": label.get("id"),
"name": label.get("name"),
"type": label.get("type"),
"message_list_visibility": label.get("messageListVisibility"),
"label_list_visibility": label.get("labelListVisibility"),
}
for label in raw_labels
if isinstance(label, dict)
]
return ListLabelsOutput(success=True, labels=labels, total=len(labels))