from pydantic import BaseModel from typing import List, Literal import gradio as gr class EmailAction(BaseModel): action_type: Literal['send', 'reply', 'archive', 'delete'] message: str = "" email_id: int = None class EmailObservation(BaseModel): emails: List[dict] ai_feedback: str class EmailEnv: def __init__(self): self.emails = [ {"id": 1, "from": "alice@example.com", "subject": "Meeting Tomorrow", "label": "Work", "status": "Unread"}, {"id": 2, "from": "bob@example.com", "subject": "Lunch Plans", "label": "Personal", "status": "Archived"}, {"id": 3, "from": "carol@example.com", "subject": "Project Update", "label": "Work", "status": "Sent"}, {"id": 4, "from": "dave@spam.com", "subject": "Win a Prize", "label": "Spam", "status": "Deleted"}, ] def step(self, action: EmailAction) -> EmailObservation: feedback = "" target = next((e for e in self.emails if e["id"] == action.email_id), None) if action.email_id else None if action.action_type == "send": new_id = max([e["id"] for e in self.emails]+[0]) + 1 self.emails.append({ "id": new_id, "from": "me@example.com", "subject": "Project Update", "label": "Work", "status": "Sent", "body": action.message }) feedback = "Action Executed: Send Email ✅" elif action.action_type == "reply" and target: new_id = max([e["id"] for e in self.emails]+[0]) + 1 self.emails.append({ "id": new_id, "from": "me@example.com", "subject": f"Re: {target['subject']}", "label": target["label"], "status": "Sent", "body": action.message }) feedback = f"Action Executed: Reply ✅ to email ID {action.email_id}" elif action.action_type == "archive" and target: target["status"] = "Archived" feedback = f"Action Executed: Archive ✅ email ID {action.email_id}" elif action.action_type == "delete" and target: target["status"] = "Deleted" feedback = f"Action Executed: Delete ✅ email ID {action.email_id}" else: feedback = "Invalid action or email ID." return EmailObservation(emails=self.emails, ai_feedback=feedback) env = EmailEnv() status_colors = { "Unread": "orange", "Sent": "green", "Archived": "blue", "Deleted": "red" } def render_table_html(emails): table_html = "
| ID | From | Subject | Label | Status |
|---|---|---|---|---|
| {e['id']} | " table_html += f"{e['from']} | " table_html += f"{e['subject']} | " table_html += f"{e['label']} | " table_html += f"{e['status']} | " table_html += "