SH.
Back to The Lab
The Lab · 05

Email Agent

A scheduled NestJS email triage service that polls inboxes via IMAP or mock feeds and classifies incoming messages by priority and category. Explores pluggable OpenAI, Ollama, and rule-based classification backed by SQLite.

01. Project Overview

Email Agent is a NestJS backend service that periodically reads unread emails, classifies whether each message requires immediate attention, and stores structured alerts in SQLite for consumption through a REST dashboard API. It focuses on inbox reading, triage, and notification storage rather than drafting or sending replies.

02. Problem & Exploration

Explores a lightweight, provider-agnostic email triage pipeline that can switch cleanly between cloud LLMs, local Ollama models, and deterministic rules while preventing duplicate alerts.

What This Experiment Explores

Explores NestJS dependency injection for swapping AI and ingestion providers via environment configuration, scheduled background polling, and idempotent message processing.

03. Implemented Capabilities

  • Scheduled inbox polling via @nestjs/schedule (every two minutes by default) alongside an on-demand trigger endpoint (POST /api/dashboard/poll).
  • Pluggable email source layer supporting live IMAP connections (node-imap and mailparser for UNSEEN messages) and a local JSON mock dataset.
  • Abstract AiService classification layer with interchangeable OpenAI (gpt-4o-mini JSON mode), local Ollama (/api/generate), and zero-dependency rule-based implementations.
  • Duplicate-message tracking and alert persistence in SQLite using TypeORM entities (ProcessedEmail and Notification).
  • REST dashboard endpoints for listing notifications, viewing priority/category stats, marking items as read, and clearing notifications.

04. Technical Approach & Stack

Structured as modular NestJS services (EmailModule, AiModule, ClassificationModule, NotificationModule, DashboardModule, and DatabaseModule). ClassificationProcessor fetches messages from the configured source, checks ProcessedEmail by messageId to avoid duplicates, invokes the active AiService provider, persists important messages as Notification records, and marks each message processed.

Verified Technology Stack

  • TypeScript
  • NestJS
  • TypeORM
  • SQLite
  • IMAP
  • OpenAI API
  • Ollama
  • Docker

05. Known Limitations & Notes

  • Implements inbox reading, classification, and notification storage only; it does not draft or send email replies.
  • Backend REST API only; no frontend dashboard UI is included in the repository.