---
spec_name: "Daily Inbox Summarizer AI Agent"
version: "1.0"
author: "Mike Kwal"
goal: "An autonomous AI agent that connects to a specified IMAP email inbox, scans unread emails from the last 24 hours, and generates a daily summary with action items."
---

## 1. Purpose
This agent's job is to save time by eliminating the need to manually check and sort through a busy inbox every morning. It should provide a clear, actionable summary that can be read in 60 seconds. Its output should be clean, concise, and ready for a human to act upon without re-reading the source emails.

## 2. Core Logic (Step-by-Step)
1.  **Connect:** Authenticate with the specified IMAP server using the provided credentials (host, user, password). Use environment variables for credentials, not hardcoded strings.
2.  **Fetch:** Search for all unread emails received in the last 24 hours. Handle timezones correctly to ensure a true 24-hour window.
3.  **Parse:** For each email, extract the sender's name and email address, the full subject line, and the plain-text body. Ignore HTML.
4.  **Analyze:** For each email, use a call to an LLM (like Claude 3.5 Sonnet) to determine the core topic and identify any explicit action items, deadlines, or questions directed at the recipient.
5.  **Categorize & Summarize:** Group the analyzed emails into three categories: 'Action Items', 'Key Information', and 'For Review (No Action Needed)'. Generate a single summary document for all processed emails.
6.  **Output:** Save the final summary as a Markdown file to a local directory named `output`. The filename should be `summary_{YYYY-MM-DD}.md`.
7.  **Mark as Read:** After a summary file is successfully generated and saved, loop back through the processed emails and mark them as read on the IMAP server.

## 3. Inputs (Required)
- IMAP_HOST
- IMAP_USER
- IMAP_PASSWORD (use an app-specific password)
- LLM_API_KEY

## 4. Output Format (Markdown)
```markdown
# Daily Email Summary - {YYYY-MM-DD}

## Action Items
* **From: John Doe (john.doe@client.com)**
  * **Subject:** Re: Q3 Proposal
  * **Action:** Needs a reply with the final numbers by EOD Friday.

* **From: Jane Smith (jane@vendor.co)**
  * **Subject:** Invoice #1234
  * **Action:** Needs to be paid.

## Key Information
* **Project Alpha Update (from team@internal.com):** The client approved the new mockups. No action is needed from you.
* **Team Update (from manager@internal.com):** The weekly sync is moved to 3 PM today.

## For Review (No Action Needed)
* Industry newsletter about AI trends.
* Marketing email from a SaaS tool.
```

## 5. Constraints & Edge Cases
- **Error Handling:** If the IMAP connection fails, log the error and terminate gracefully. Do not proceed and do not mark any emails as read.
- **No Unread Emails:** If no unread emails are found, create a summary file stating "No new messages in the last 24 hours."
- **Security:** Do not log passwords, API keys, or raw email content to the console or any public logs.
- **Rate Limiting:** Be mindful of API rate limits for the LLM. Process emails in batches if necessary.

## 6. Tech Stack Hint
- **Language:** Node.js
- **Libraries:** `imap-simple` for email, `dotenv` for environment variables, `anthropic` for LLM calls.