---
name: email-sync
description: >
  Sync sent emails from macOS Mail into WALL-E's brain so it can learn
  the owner's communication style. Always syncs all sent mail with body
  content. Inbox sync is optional and filters to only emails addressed
  directly to the owner (on To/Cc line). Uses JXA to read Mail.app.
version: 1.2.0
author: wall-e
execution: script
entry: run.js
args: ["--days-back", "3"]
trigger:
  type: manual
config:
  days_back:
    type: number
    default: 3
    description: "How many days back to sync (used as fallback when incremental has no prior data)"
  sync_inbox:
    type: boolean
    default: false
    description: "Also sync inbox emails addressed directly to owner"
  incremental:
    type: boolean
    default: false
    description: "Query brain for latest email and only fetch since then (days_back used as fallback for first run)"
tags: [email, gmail, mail, sent, sync, learning]
permissions:
  - mail:read
  - brain:write
---
# Email Sync

## What This Skill Does

Syncs sent emails from macOS Mail into WALL-E's brain so it can learn the
owner's writing style, communication patterns, and topics of interest.
Optionally syncs inbox emails that are addressed directly to the owner.

## How It Works

1. **Incremental mode** (`incremental: true`): queries brain for the latest email timestamp, computes `days_back` dynamically (+1 day overlap). Falls back to configured `days_back` on first run (no prior data).
2. Run JXA script via `osascript` to read sent messages from all accounts
3. Sent mail always includes body content (first 2000 chars) for learning
4. Inbox is opt-in (`--sync-inbox`), filtered to emails where owner is on To/Cc
5. Dedup by `source_id` = `mail:{messageId}` — skip unchanged, update modified
6. Store as memories with `source: 'email'`, `source_channel: <account name>`

## Memory Format

Each email is stored as a memory:
- **source**: `email`
- **source_channel**: account name (e.g., `owner@example.com`)
- **memory_type**: `email_sent` or `email_received`
- **subject**: email subject
- **content**: human-readable summary with recipients + body for sent mail
- **metadata**: JSON with messageId, sender, recipients, dates
- **timestamp**: date sent/received (ISO8601)
- **importance**: 0.6 for sent (learning), 0.4 for received

## Prerequisites

macOS Mail.app must be configured with your email accounts and running.
The terminal must have Automation access for Mail.app:
**System Settings > Privacy & Security > Automation > [terminal] > Mail**

## Output

Returns: `{ inserted, updated, skipped, filtered, errors, total }`
