Fix issue with non-list To header in GmailSendMessage Tool (#6242)

Fixing the problem of feeding `str` instead of `List[str]` to the email
tool.

Fixes #6234 
---------

Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
This commit is contained in:
Jakub Misiło 2023-06-21 10:25:49 +02:00 committed by GitHub
parent 94c7899257
commit 5d149e4d50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
import base64 import base64
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional, Union
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
@ -18,7 +18,7 @@ class SendMessageSchema(BaseModel):
..., ...,
description="The message to send.", description="The message to send.",
) )
to: List[str] = Field( to: Union[str, List[str]] = Field(
..., ...,
description="The list of recipients.", description="The list of recipients.",
) )
@ -26,11 +26,11 @@ class SendMessageSchema(BaseModel):
..., ...,
description="The subject of the message.", description="The subject of the message.",
) )
cc: Optional[List[str]] = Field( cc: Optional[Union[str, List[str]]] = Field(
None, None,
description="The list of CC recipients.", description="The list of CC recipients.",
) )
bcc: Optional[List[str]] = Field( bcc: Optional[Union[str, List[str]]] = Field(
None, None,
description="The list of BCC recipients.", description="The list of BCC recipients.",
) )
@ -45,22 +45,22 @@ class GmailSendMessage(GmailBaseTool):
def _prepare_message( def _prepare_message(
self, self,
message: str, message: str,
to: List[str], to: Union[str, List[str]],
subject: str, subject: str,
cc: Optional[List[str]] = None, cc: Optional[Union[str, List[str]]] = None,
bcc: Optional[List[str]] = None, bcc: Optional[Union[str, List[str]]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
"""Create a message for an email.""" """Create a message for an email."""
mime_message = MIMEMultipart() mime_message = MIMEMultipart()
mime_message.attach(MIMEText(message, "html")) mime_message.attach(MIMEText(message, "html"))
mime_message["To"] = ", ".join(to) mime_message["To"] = ", ".join(to if isinstance(to, list) else [to])
mime_message["Subject"] = subject mime_message["Subject"] = subject
if cc is not None: if cc is not None:
mime_message["Cc"] = ", ".join(cc) mime_message["Cc"] = ", ".join(cc if isinstance(cc, list) else [cc])
if bcc is not None: if bcc is not None:
mime_message["Bcc"] = ", ".join(bcc) mime_message["Bcc"] = ", ".join(bcc if isinstance(bcc, list) else [bcc])
encoded_message = base64.urlsafe_b64encode(mime_message.as_bytes()).decode() encoded_message = base64.urlsafe_b64encode(mime_message.as_bytes()).decode()
return {"raw": encoded_message} return {"raw": encoded_message}
@ -68,10 +68,10 @@ class GmailSendMessage(GmailBaseTool):
def _run( def _run(
self, self,
message: str, message: str,
to: List[str], to: Union[str, List[str]],
subject: str, subject: str,
cc: Optional[List[str]] = None, cc: Optional[Union[str, List[str]]] = None,
bcc: Optional[List[str]] = None, bcc: Optional[Union[str, List[str]]] = None,
run_manager: Optional[CallbackManagerForToolRun] = None, run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str: ) -> str:
"""Run the tool.""" """Run the tool."""
@ -90,10 +90,10 @@ class GmailSendMessage(GmailBaseTool):
async def _arun( async def _arun(
self, self,
message: str, message: str,
to: List[str], to: Union[str, List[str]],
subject: str, subject: str,
cc: Optional[List[str]] = None, cc: Optional[Union[str, List[str]]] = None,
bcc: Optional[List[str]] = None, bcc: Optional[Union[str, List[str]]] = None,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None, run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str: ) -> str:
"""Run the tool asynchronously.""" """Run the tool asynchronously."""