Every mail-driven agent in this directory reaches its mailbox through ONE
seam: an Areev polling-trigger connector — JSON on stdin, JSON on
stdout, one process per invocation, the same contract as --tool-cmd
(docs/triggers.md §"The connector contract").
That is why each agent's keyless mock connector and the live connectors
here are interchangeable, and why none of this adds a vendor SDK to the
repo: the provider glue is a standalone script you own.
The two live connectors ship with
invoice-to-accounting/connectors/
and are written to be lifted into any other mail-driven agent unchanged:
| Provider | Script | Auth |
|---|---|---|
| Microsoft 365 / Outlook (the default — most desks run Exchange Online) | outlook_graph.py |
Entra ID app registration + device-code sign-in, or a brokered OUTLOOK_TOKEN |
| Google Workspace / Gmail (the pattern proven in a production deployment) | gmail.py |
gcloud Application Default Credentials, or a brokered GMAIL_TOKEN |
Both are Python-stdlib-only on purpose: a connector is a dumb pipe (fetch, normalize, cursor) and runs as a subprocess of whichever language your agent is written in — a Rust or TypeScript agent uses these scripts as-is, or you rewrite ~150 lines in your language against the same contract.
{"message_id": "<...>", ← the dedup key: --dedup-key /message_id
"thread": "<...>", ← what the context query binds ($session = /thread)
"mailbox": "ap@yourdesk.example",
"email": {"from": "...", "to": "...", "subject": "...", "date": "...", "body": "..."},
"attachments": [{"filename": "inv.pdf", "mime": "application/pdf", "blob": "@0"}]}Attachments ride back as blobs ({filename, mime, b64}) referenced by
"@N". The evaluator — the party already holding the memory's writer —
stores each blob in the CAS and rewrites the reference to a
cas://sha256:… address, so your parse tool reads bytes with
areev blob get (the one lock-free door into the file). Budgets: 16 MiB
per item, 48 MiB per response, enforced on decoded size; a violation
refuses the whole poll with the cursor unmoved (TRG-E011).
- Absent cursor in the request = first poll: record where the mailbox
is now, return
items: []. Declaring a trigger must never replay history. - Absent cursor in the response = "leave it where it is". Emitting
nullrewinds the source. more: true= backlog: Areev re-polls immediately instead of waiting out the interval, so a cold start drains without hammering.- An unparseable cursor (another connector's leftovers on the same trigger) should reseed loudly, not fail every poll forever.
- Overlap is safe, gaps are not: the trigger's
/message_iddedup makes a re-fetched message one recorded skip, so when in doubt fetch too much.
- App registration (Entra ID → App registrations → New): public
client. Delegated permissions:
Mail.Read(+Mail.Sendif your reply tool sends through Graph;Mail.Read.Sharedfor a shared mailbox). Enable "Allow public client flows" for device code. - Export
MS_CLIENT_ID(andMS_TENANTif notorganizations), thenpython3 connectors/outlook_graph.py login— sign in with the mailbox account; the refresh token lands in~/.config/areev/outlook-token.json(chmod 600, override viaOUTLOOK_TOKEN_CACHE). - A shared/desk mailbox: set
AP_MAILBOX=ap@yourdesk.exampleto poll/users/<address>instead of/me. - The connector polls
…/mailFolders/inbox/messages, not/messages. Graph's/messagesspans every folder, so a desk that sends its own approval mail re-ingests it out of Sent Items on the next tick — with a new message id, which is exactly what/message_iddedup cannot catch. Attachment listings deliberately carry no$select:contentBytesis declared onfileAttachment, not on the baseattachmenttype the collection is typed as, so selecting it 400s the whole request.
The production-proven shape avoids a dedicated service account and domain-wide delegation entirely: make the desk address a Google Group whose member is a real mailbox, and read that mailbox:
gcloud auth application-default loginwith scopes includinggmail.readonly(orgmail.modifyif the agent labels/archives), as the member account. Each ADC login replaces the previous grant — re-authing with fewer scopes silently breaks the mail path.- Set
GMAIL_QUOTA_PROJECT(theX-Goog-User-Project; without it Google answers 403) andAP_MAILBOXto the group address. The connector's query is{deliveredto:<group> to:<group>}— theto:leg catches your own Sent copy, which Gmail never re-delivers to the group. - Brokered setups can skip gcloud:
GMAIL_TOKENshort-circuits to a ready bearer token.
Sending the ask and reading the answer are tools, not connector work. Hard-won guidance from the production deployment:
- Mark every ask. Put a stable marker in the subject —
[areev:ap/<sha256(message_id)[:12]>]— because amailto:button cannot setIn-Reply-To(clients ignore RFC 6068's attempt), so a clicked reply may land outside the thread. Resolve replies by thread first, marker search second. - Not every message in the mailbox is work. Both connectors drop two kinds before they become items, and a hand-rolled connector must too: our own asks coming back (they carry the run marker, the reply path owns them, and a second run can only ever conclude "nothing could be extracted"), and postmaster mail — a bounce carries neither the marker nor an invoice, so it parks a run asking a human about a delivery failure, and that ask can bounce in turn. Mind the cursor when you add such a filter: advance it on everything you looked at, not on what you returned, or a batch that is entirely bounces leaves it parked and re-fetches the same mail forever.
- Email the approver, never the external sender. Getting that backwards emails your vendor an approval link.
- Classify deterministically first: strip quoted history (Gmail's
"On … wrote:", Outlook's header block), take the verb
(
approve/revise/reject), parseField: valuecorrection lines. Add an LLM interpretation leg only for what that cannot classify, and leave a genuine question unactioned for a person. - Respond as the human's principal (
run respond --as user:<who>) — the approver's identity is the audit record, and the runtime refuses the principal that started the run. In production, put approvals behind per-principal credentials (areev ui --auth).
On a deployment, prefer the credential broker to ambient env tokens:
areev trigger run --credential gmail=GMAIL_TOKEN --allow-host 'https://gmail.googleapis.com' hands the connector a broker address, not
the secret (docs/triggers.md §"Brokered
connector egress"). cmd:/vault: resolvers mint short-lived tokens per
call (docs/cookbook.md §19).