Spaces:
Sleeping
Sleeping
refactor: Reduce verbosity in agent outputs and enhance message formatting in chat interface
b58981e
| import os | |
| from typing import List, Dict, Any | |
| from smolagents import CodeAgent, InferenceClientModel, tool | |
| from src.tools.gmail_mcp_client import get_recent_emails as _get_recent_emails | |
| from src.tools.gmail_mcp_client import search_emails_simple as _search_emails_simple | |
| from src.tools.gmail_mcp_client import read_email_content as _read_email_content | |
| def get_recent_emails(max_results: int = 10) -> List[Dict[str, str]]: | |
| """ | |
| Get the most recent emails from allowed senders (habib.adoum01@gmail.com and news@alphasignal.ai). | |
| This is the main tool for browsing recent emails. | |
| Args: | |
| max_results: Number of recent emails to fetch, between 1 and 50 (default: 10). | |
| Returns: | |
| List of email dictionaries with 'id', 'subject', 'sender', 'date', and 'snippet' fields. | |
| Returns empty list if no emails found. Each email can be read in detail using read_email_content. | |
| """ | |
| if max_results < 1: | |
| max_results = 1 | |
| elif max_results > 50: | |
| max_results = 50 | |
| return _get_recent_emails(max_results) | |
| def search_emails(query: str, max_results: int = 10) -> List[Dict[str, str]]: | |
| """ | |
| Search for emails containing specific keywords from allowed senders only. | |
| Use simple keywords - no need for complex Gmail search operators. | |
| Args: | |
| query: Search keywords (examples: "AI", "newsletter", "update", "coding"). Simple terms work best. | |
| max_results: Maximum number of results to return, between 1 and 50 (default: 10). | |
| Returns: | |
| List of email dictionaries with 'id', 'subject', 'sender', 'date', and 'snippet' fields. | |
| Returns empty list if no matching emails found. Use read_email_content to get full email text. | |
| """ | |
| if not query or not query.strip(): | |
| print("Error: Empty search query provided") | |
| return [{"error": "Search query cannot be empty"}] | |
| if max_results < 1: | |
| max_results = 1 | |
| elif max_results > 50: | |
| max_results = 50 | |
| return _search_emails_simple(query.strip(), max_results) | |
| def read_email_content(message_id: str) -> Dict[str, Any]: | |
| """ | |
| Read the full content of a specific email by its ID. | |
| Use this after getting email IDs from get_recent_emails or search_emails. | |
| Args: | |
| message_id: The email ID from get_recent_emails or search_emails (example: "197421416350ce1a"). | |
| Returns: | |
| Dictionary with 'id', 'subject', 'sender', 'date', 'snippet', and 'body' fields. | |
| The 'body' contains the full email text content. | |
| Returns error dict if email not accessible or not from allowed sender. | |
| """ | |
| if not message_id or not message_id.strip(): | |
| return {"error": "Message ID cannot be empty"} | |
| return _read_email_content(message_id.strip()) | |
| gmail_agent = CodeAgent( | |
| model=InferenceClientModel( | |
| provider="nebius", | |
| token=os.environ["HF_TOKEN"], | |
| ), | |
| tools=[get_recent_emails, search_emails, read_email_content], | |
| name="gmail_agent", | |
| description="Gmail agent specialized in reading and searching emails from habib.adoum01@gmail.com and news@alphasignal.ai only", | |
| max_steps=10, | |
| additional_authorized_imports=["json"], | |
| stream_outputs=False, | |
| max_print_outputs_length=300, | |
| ) | |