Skip to content

Use Case Plugin

Practical Use Cases: Email Draft Processor Plugin

The Email Draft Processor Plugin is a tool in the MindFlight AI Server that helps automate email drafting. It is particularly useful for scenarios where AI needs to generate or assist with email replies.

The flexibility of the MFO framework allows you to write this type of plugin that can act autonomously and be activated for different tasks in any workflow.


What is the Email Draft Processor?

This plugin is located at:

/pkg/providers/email/_draft/_preparator

Its main role is to:

  • Process incoming emails.
  • Prepare draft replies (optionally enriched with AI, such as LLM prompts).
  • Integrate with email systems to send or store drafts.

Metaphor:
Think of it as your personal AI secretary: it reads your emails, understands the context, and drafts the perfect reply for you to review or send.


Key Files & Their Roles

File Name Purpose
adapter.go Sets up the Provider interface and exposes its tools.
mailer Handles sending emails (e.g., via SMTP or APIs).
processor.go Core logic for preparing drafts (includes AI prompt processing).
read_emails.go Reads emails and prepares them for processing (parsing & validation).

How It Works

1️⃣ Email Fetched:
The processor receives an email (often triggered by the Unipile Provider's event).

2️⃣ Read & Analyze:
The read_emails.go file parses the email content, extracting useful data like subject, sender, and message body.

3️⃣ Draft Generation:
The processor.go uses AI (LLM) to create a suggested reply, ensuring tone and context match.

4️⃣ Mailer Integration:
The draft can be saved as a draft or sent directly, depending on configuration. The mailer handles the final step.


Tool Exposure (API)

The plugin exposes API tools you can call programmatically:

Tool Name What it does
email_draft_prepare Prepares a draft for a specific email.
email_draft_list Lists all available email drafts.
email_draft_send Sends a prepared draft via email.

These tools are accessible via endpoints like:

/api/tools/email/_draft/_prepare
/api/tools/email/_draft/_list
/api/tools/email/_draft/    _send

Example: Drafting a Reply (Step-by-Step)

Here's an example flow when you want to draft a reply to an email.

1️⃣ Prepare the Draft

curl -X POST http://localhost:8080/api/tools/email_draft_prepare \
    -H "Authorization: Bearer YOUR_TOKEN_HERE" \
    -H "Content-Type: application/json" \
    -d '{"email_id": "abc123", "tone": "friendly"}'

Response:

{
    "draft_id": "draft_456",
    "status": "processing"
}

👉 This may trigger a background job if AI enrichment is involved.


2️⃣ Check Draft Status (If Asynchronous)

curl -X GET http://localhost:8080/api/jobs/status/draft_456 \
    -H "Authorization: Bearer YOUR_TOKEN_HERE"

Response:

{
    "draft_id": "draft_456",
    "status": "completed",
    "draft_content": "Hello John, thank you for your email..."
}

3️⃣ Send the Draft

curl -X POST http://localhost:8080/api/tools/email_draft_send \
    -H "Authorization: Bearer YOUR_TOKEN_HERE" \
    -H "Content-Type: application/json" \
    -d '{"draft_id": "draft_456"}'

Response:

{
    "status": "sent",
    "timestamp": "2025-05-09T10:30:00Z"
}

Integration with Unipile

The Email Draft Processor often works hand-in-hand with the Unipile Provider:

  • When Unipile fires the unipile.email.received event, it can trigger the draft processor automatically.
  • This creates a seamless workflow: 1️⃣ New email arrives → 2️⃣ Draft is prepared → 3️⃣ Human reviews & sends.

Metaphor: Imagine Unipile as your inbox manager and the Draft Processor as your ghostwriter—they collaborate to keep your communication flowing smoothly.


Mermaid Diagram: Drafting Flow

sequenceDiagram
    participant Unipile as Unipile Provider
    participant DraftProcessor as Draft Processor
    participant JobManager as Job Manager
    participant Client as Client App

    Unipile->>DraftProcessor: Trigger draft preparation (email.received)
    DraftProcessor->>JobManager: Queue AI drafting job
    JobManager->>DraftProcessor: Complete draft & save
    Client->>DraftProcessor: Fetch or send draft via API

Quick Recap

Step Action
1️⃣ Prepare Draft /api/tools/email_draft_prepare prepares a draft for a specific email.
2️⃣ Check Status /api/jobs/status/{job_id} checks if the draft is ready.
3️⃣ Send Draft /api/tools/email_draft_send sends the prepared draft.
Bonus: Auto-trigger Unipile can trigger draft preparation automatically on new email received.