Skip to content

Commit bb202f4

Browse files
committed
feat: optimize google authorization
1 parent 3521474 commit bb202f4

File tree

4 files changed

+78
-49
lines changed

4 files changed

+78
-49
lines changed

npiai/app/google/calendar/app.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,35 @@
22

33
import datetime
44
import json
5+
import os
56

6-
from google.oauth2.credentials import Credentials
77
from googleapiclient.discovery import build
8+
from oauth2client import file
89

910
from npiai import function, App
10-
11+
from npiai.error import UnauthorizedError
1112

1213
# https://developers.google.com/calendar/quickstart/python
1314
# API Reference: https://developers.google.com/calendar/api/v3/reference
1415

1516

1617
class GoogleCalendar(App):
17-
def __init__(self, credentials: Credentials):
18+
def __init__(self, cred_file: str | None = None):
1819
super().__init__(
1920
name='google_calendar',
2021
description='Manage events on Google Calendar',
21-
system_prompt='You are an assistant interacting with Google Calendar API. Your job is the selecting the best function based the tool list.',
22+
system_prompt='You are an assistant interacting with Google Calendar API. '
23+
'Your job is the selecting the best function based the tool list.',
2224
)
25+
if cred_file is None:
26+
cred_file = os.environ.get("GOOGLE_CREDENTIAL")
27+
28+
if cred_file is None:
29+
raise UnauthorizedError
2330

31+
store = file.Storage(cred_file)
2432
self.service = build(
25-
'calendar', 'v3', credentials=credentials
33+
'calendar', 'v3', credentials=store.get()
2634
)
2735

2836
@function

npiai/app/google/gmail/app.py

+36-44
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
11
import asyncio
22
import json
3+
import os
34
from typing import List
45
from textwrap import dedent
56

67
from googleapiclient.errors import HttpError
78
from markdown import markdown
89
from simplegmail.message import Message
910

10-
from google.oauth2.credentials import Credentials
11-
from oauth2client.client import OAuth2Credentials
12-
1311
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
2714

2815

2916
class Gmail(App):
3017
gmail_client: GmailClientWrapper
3118

32-
def __init__(self, credentials: Credentials):
19+
def __init__(self, cred_file: str | None = None):
3320
super().__init__(
3421
name='gmail',
3522
description='interact with Gmail using English, e.g., gmail("send an email to test@gmail.com")',
3623
system_prompt='You are a Gmail Agent helping users to manage their emails',
3724
)
25+
if cred_file is None:
26+
cred_file = os.environ.get("GOOGLE_CREDENTIAL")
27+
28+
if cred_file is None:
29+
raise UnauthorizedError
3830

3931
self.gmail_client = GmailClientWrapper(
40-
_creds=convert_credentials(credentials),
32+
creds_file=cred_file,
4133
)
4234

4335
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:
123115

124116
@function
125117
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,
132124
) -> str:
133125
"""
134126
Create an email draft.
@@ -154,13 +146,13 @@ def create_draft(
154146

155147
@function
156148
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,
164156
):
165157
"""
166158
Create a draft that replies to the last email retrieved in the previous chat.
@@ -203,13 +195,13 @@ def create_reply_draft(
203195

204196
@function
205197
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,
213205
):
214206
"""
215207
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:
268260

269261
@function
270262
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,
277269
):
278270
"""
279271
Send an email.

npiai/error/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .auth import UnauthorizedError
2+
3+
__all__ = [
4+
UnauthorizedError,
5+
]

playground/main.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pydantic import BaseModel
2+
from fastapi import FastAPI
3+
4+
5+
class ChatRequest(BaseModel):
6+
thread_id: str
7+
tool_id: str
8+
instruction: str
9+
action_id: str
10+
action_result: str
11+
pass
12+
13+
14+
pg = FastAPI()
15+
16+
17+
@pg.get("/getScreen")
18+
def get_web_screen():
19+
pass
20+
21+
22+
@pg.post("/chat")
23+
def chat(req: ChatRequest):
24+
pass

0 commit comments

Comments
 (0)