1
1
import asyncio
2
2
import json
3
+ import os
3
4
from typing import List
4
5
from textwrap import dedent
5
6
6
7
from googleapiclient .errors import HttpError
7
8
from markdown import markdown
8
9
from simplegmail .message import Message
9
10
10
- from google .oauth2 .credentials import Credentials
11
- from oauth2client .client import OAuth2Credentials
12
-
13
11
from npiai import App , function
14
- from npiai .app .google .gmail .client import GmailClientWrapper
15
-
16
-
17
- def convert_credentials (google_credentials : Credentials ) -> OAuth2Credentials :
18
- return OAuth2Credentials (
19
- access_token = None ,
20
- client_id = google_credentials ._client_id ,
21
- client_secret = google_credentials ._client_secret ,
22
- refresh_token = google_credentials ._refresh_token ,
23
- token_expiry = google_credentials .expiry ,
24
- token_uri = google_credentials ._token_uri ,
25
- user_agent = None
26
- )
12
+ from npiai .error import UnauthorizedError
13
+ from .client import GmailClientWrapper
27
14
28
15
29
16
class Gmail (App ):
30
17
gmail_client : GmailClientWrapper
31
18
32
- def __init__ (self , credentials : Credentials ):
19
+ def __init__ (self , cred_file : str | None = None ):
33
20
super ().__init__ (
34
21
name = 'gmail' ,
35
22
description = 'interact with Gmail using English, e.g., gmail("send an email to test@gmail.com")' ,
36
23
system_prompt = 'You are a Gmail Agent helping users to manage their emails' ,
37
24
)
25
+ if cred_file is None :
26
+ cred_file = os .environ .get ("GOOGLE_CREDENTIAL" )
27
+
28
+ if cred_file is None :
29
+ raise UnauthorizedError
38
30
39
31
self .gmail_client = GmailClientWrapper (
40
- _creds = convert_credentials ( credentials ) ,
32
+ creds_file = cred_file ,
41
33
)
42
34
43
35
def _get_messages_from_ids (self , message_ids : List [str ]) -> List [Message ]:
@@ -123,12 +115,12 @@ def remove_labels(self, message_ids: List[str], labels: List[str]) -> str:
123
115
124
116
@function
125
117
def create_draft (
126
- self ,
127
- to : str ,
128
- subject : str ,
129
- message : str = None ,
130
- cc : List [str ] = None ,
131
- bcc : List [str ] = None ,
118
+ self ,
119
+ to : str ,
120
+ subject : str ,
121
+ message : str = None ,
122
+ cc : List [str ] = None ,
123
+ bcc : List [str ] = None ,
132
124
) -> str :
133
125
"""
134
126
Create an email draft.
@@ -154,13 +146,13 @@ def create_draft(
154
146
155
147
@function
156
148
def create_reply_draft (
157
- self ,
158
- to : str ,
159
- subject : str ,
160
- recipient_id : str ,
161
- message : str = None ,
162
- cc : List [str ] = None ,
163
- bcc : List [str ] = None ,
149
+ self ,
150
+ to : str ,
151
+ subject : str ,
152
+ recipient_id : str ,
153
+ message : str = None ,
154
+ cc : List [str ] = None ,
155
+ bcc : List [str ] = None ,
164
156
):
165
157
"""
166
158
Create a draft that replies to the last email retrieved in the previous chat.
@@ -203,13 +195,13 @@ def create_reply_draft(
203
195
204
196
@function
205
197
def reply (
206
- self ,
207
- to : str ,
208
- subject : str ,
209
- recipient_id : str ,
210
- message : str = None ,
211
- cc : List [str ] = None ,
212
- bcc : List [str ] = None ,
198
+ self ,
199
+ to : str ,
200
+ subject : str ,
201
+ recipient_id : str ,
202
+ message : str = None ,
203
+ cc : List [str ] = None ,
204
+ bcc : List [str ] = None ,
213
205
):
214
206
"""
215
207
Reply to the last email retrieved in the previous chat.
@@ -268,12 +260,12 @@ def search_emails(self, query: str = None, max_results: int = 100) -> str:
268
260
269
261
@function
270
262
async def send_email (
271
- self ,
272
- to : str ,
273
- subject : str ,
274
- message : str = None ,
275
- cc : List [str ] = None ,
276
- bcc : List [str ] = None ,
263
+ self ,
264
+ to : str ,
265
+ subject : str ,
266
+ message : str = None ,
267
+ cc : List [str ] = None ,
268
+ bcc : List [str ] = None ,
277
269
):
278
270
"""
279
271
Send an email.
0 commit comments