Email Notification Service
Relevant source files
The following files were used as context for generating this wiki page:
The Email Notification Service is a critical component of the OpenChat background agent infrastructure. Its primary purpose is to provide users with automated updates and summaries when scheduled AI tasks are completed. By integrating with external email providers, the service bridges the gap between autonomous background execution and user awareness, ensuring that task outputs are delivered directly to the user's inbox.
The service is built on the Convex backend and utilizes the Resend platform for reliable email delivery. It features robust markdown-to-HTML conversion, sanitization for email client safety, and terminal-style templates designed to reflect the technical nature of AI agent execution.
System Architecture and Integration
The Email Notification Service operates as an internal mutation within the Convex environment. It is triggered by the scheduled_ai module immediately after an autonomous agent completes its assigned task.
The flow begins when a background agent finishes generating content. If the task configuration has emailNotifications enabled, the system schedules the email delivery.
Sources: convex/scheduled_ai.ts:250-261, convex/email.ts:77-85
Key Components and Functions
Email Processing Logic
The service includes several utility functions to ensure emails are safe, readable, and properly formatted for various mail clients.
| Function | Purpose | Description |
|---|---|---|
sendTaskSummaryEmail | Entry Point | Internal mutation that validates user data and triggers the send process. |
markdownToSafeHtml | Content Parser | Converts agent markdown output to HTML and strips dangerous tags (scripts, iframes). |
createMonospaceTemplate | Templating | Generates a "terminal-style" HTML email with dark/light mode support. |
escapeHtml | Security | Prevents XSS by escaping special characters in user-provided strings. |
Sources: convex/email.ts:31-75, convex/email.ts:87-115
Content Sanitization
To maintain security and ensure high deliverability, the service performs aggressive sanitization of AI-generated content before it is embedded in an email. This includes:
- Removing
<script>,<style>,<meta>,<iframe>, and<object>tags. - Stripping
on*event attributes from HTML elements. - Replacing
javascript:protocols in links with#. - Truncating content that exceeds
MAX_CONTENT_LENGTH(5,000 characters) to prevent oversized emails.
Sources: convex/email.ts:13-14, convex/email.ts:41-65, convex/email.ts:117-125
Data Flow: Scheduled Task to Inbox
The following sequence diagram illustrates the interactions between the Convex scheduler, the database, and the external Resend component during a notification event.
User Configuration and Preferences
Users control the behavior of the Email Notification Service through the UI components provided in the background task management and customization settings.
Task-Level Toggle
When creating or editing a background agent, users can toggle the emailNotifications field. This value is stored in the scheduled_tasks table.
Sources: src/components/scheduled-tasks/task-form.tsx:135-140, src/components/scheduled-tasks/task-form.tsx:289-299
Personalization Context
The service uses personalization data from the user's profile to make notifications feel more authentic. The buildSystemPrompt function can also be configured with emailMode to instruct the AI to generate content specifically formatted for email consumption (concise headers, executive summaries).
- Preferred Name: Used in the email greeting (e.g., "Hello [Name]").
- Timezone: Ensures the
executionDatereflected in the email matches the user's local time.
Sources: src/lib/prompt_config.ts:312-320, src/routes/settings/customization.tsx:190-210, convex/email.ts:130-140
Email Templates
The service currently utilizes a "Monospace Terminal" template as its active design. This template mimics a CLI environment, reinforcing the "OS Chat" branding.
Template Features:
- Terminal Header: Includes decorative window controls (red/yellow/green dots) and a "task-notification.log" title.
- Prompt Indicators: Uses
$and>symbols to distinguish between system commands and agent messages. - Responsive Design: Optimized for mobile viewing and includes a
prefers-color-schememedia query for dark mode support. - Direct Actions: Includes a "Open Dashboard" button that links directly to the specific chat session (
/c/${chatId}).
Sources: convex/email.ts:167-210, convex/email.ts:440-470, convex/email.ts:560-580
Error Handling and Performance
The sendTaskSummaryEmail function is wrapped in a try...catch block designed to be "non-breaking." If an email fails to send (e.g., due to an invalid email format or Resend API error), the service logs the error but returns a success status for the background task itself. This ensures that a delivery failure does not cause the entire AI agent execution to be marked as failed.
Sources: convex/email.ts:145-152, convex/scheduled_ai.ts:400-410