Feature: AI Assistant & Rules Engine
Relevant source files
The following files were used as context for generating this wiki page:
- apps/web/utils/ai/rule/prompt-to-rules.ts
- apps/web/utils/ai/assistant/process-user-request.ts
- apps/web/app/(app)/[emailAccountId]/assistant/ProcessRules.tsx
- apps/web/tests/ai-prompt-to-rules.test.ts
- apps/web/utils/ai/assistant/chat.ts
- ARCHITECTURE.md
- apps/web/tests/ai-choose-rule.test.ts
- apps/web/utils/ai/knowledge/extract.ts
- apps/web/utils/ai/rule/generate-rules-prompt.ts
The AI Personal Assistant is a core feature of Inbox Zero, designed to automate email management. It allows users to define how their inbox should be handled using natural language prompts, which are then converted into a structured set of rules. These rules are stored in a database and are used to process incoming emails, performing actions like labeling, archiving, drafting replies, and more.
The system features a two-way synchronization mechanism between a user-defined prompt file and the rules stored in the database. While this allows for detailed tracking and precise action definition, it introduces complexity. The architecture has evolved, and a one-way data flow from the prompt file might be considered in the future to simplify the system. The assistant also includes an interactive chat interface for creating and modifying rules.
Sources: ARCHITECTURE.md, README.md
Architecture Overview
The AI Assistant and Rules Engine automates email processing based on user-defined logic. The general flow begins when a new email arrives, which triggers the rule execution engine. The engine selects the most appropriate rule, generates any dynamic content required for actions, and then executes those actions via the email provider's API.
This diagram illustrates the high-level data flow for processing an incoming email.
Sources: ARCHITECTURE.md
Rule Creation
Rules can be created in several ways: by converting a user's natural language prompt file, by suggesting rules based on email behavior, or interactively through the AI assistant chat.
From User Prompts
The primary method for rule creation is converting a user's plain English "prompt file" into structured rules. The aiPromptToRules function is responsible for this conversion. It sends the user's prompt to an LLM, which then returns a structured JSON array of rules that conform to a predefined Zod schema.
The system prompt for the LLM includes examples to guide it in creating rules with conditions (AI instructions, static filters, categories) and actions (archive, label, forward, reply).
Sources: apps/web/utils/ai/rule/prompt-to-rules.ts
This sequence diagram shows the process of converting a prompt to rules.
From Email Behavior Analysis
The system can also proactively suggest rules by analyzing a user's email history. The aiGenerateRulesPrompt function analyzes the user's last 50 sent emails, existing labels, and any saved "about me" information to generate a list of 3-10 broadly applicable rules. This helps users automate common, recurring tasks without having to write the rules from scratch.
Sources: apps/web/utils/ai/rule/generate-rules-prompt.ts:28-124
Rule Structure
A rule is the fundamental unit of the automation engine. Each rule consists of a name, a condition that triggers it, and a set of actions to be performed.
| Component | Description | Source File |
|---|---|---|
name | A unique, descriptive name for the rule. | apps/web/utils/ai/rule/create-rule-schema.ts:13 |
condition | The logic that determines if the rule should be applied to an email. It can be a combination of AI instructions, static filters, and category filters. | apps/web/utils/ai/rule/create-rule-schema.ts:14-46 |
actions | An array of actions to execute when the condition is met. | apps/web/utils/ai/rule/create-rule-schema.ts:47-106 |
enabled | A boolean flag to enable or disable the rule. | apps/web/utils/ai/assistant/chat.ts:77-79 |
automate | A boolean flag to determine if the rule should run automatically or require manual confirmation. | apps/web/utils/ai/assistant/chat.ts:80 |
Conditions
A rule's condition can be composed of several parts, linked by a logical operator (AND or OR).
| Condition Type | Description |
|---|---|
aiInstructions | Natural language instructions for the LLM to determine if the rule matches (e.g., "Match emails about product updates"). |
static | Simple filters based on email fields like from, to, or subject. |
categories | Filters based on "Smart Categories" assigned to the sender (e.g., "Newsletters", "Job Applications"). |
Sources: apps/web/utils/ai/rule/create-rule-schema.ts:14-46, apps/web/utils/ai/rule/prompt-to-rules.ts:70-85
Actions
When a rule's condition is met, it executes one or more actions.
| Action Type | Description |
|---|---|
ARCHIVE | Moves the email to the archive. |
LABEL | Applies a specified Gmail/Outlook label. |
DRAFT_EMAIL / REPLY | Creates a draft or sends a reply. Can use templates with variables like {{name}}. |
FORWARD / SEND_EMAIL | Forwards the email or sends a new one. |
MARK_READ / MARK_SPAM | Marks the email as read or as spam. |
CALL_WEBHOOK | Sends a POST request to a specified URL. |
Sources: apps/web/utils/ai/assistant/chat.ts:300-317
Rule Execution
When an email is processed, the system must choose the correct rule and execute its actions.
Choosing a Rule
The aiChooseRule function is responsible for selecting the best rule for a given email. It provides the LLM with the email's content and a list of all enabled rules, each with its instructions. The LLM then determines which rule is the most appropriate match. Tests demonstrate its ability to distinguish between various real-world scenarios like recruiter emails, legal documents, and technical issues.
Sources: apps/web/tests/ai-choose-rule.test.ts:10-216
Generating Action Arguments
Rule actions can contain dynamic content using template variables (e.g., {{Write a joke}}). The getParameterFieldsForAction function parses these templates and creates a Zod schema for the LLM. The LLM then generates the content for each variable, which is injected back into the action's fields before execution.
Sources: apps/web/utils/ai/choose-rule/ai-choose-args.test.ts:10-80
AI Assistant Chat & Rule Management
Users can interact with the AI Assistant through a chat interface to create, view, and modify their rules and settings. The assistant is powered by an agent that uses a set of predefined tools to perform actions on behalf of the user.
Processing User Requests
The processUserRequest and aiProcessAssistantChat functions orchestrate the assistant's behavior. When a user sends a message (e.g., "Create a rule to archive newsletters"), the system invokes an LLM-based agent. This agent has access to a suite of tools to fulfill the request.
This diagram shows the flow of a user request to modify a rule.
Available Tools
The AI assistant uses a variety of tools to manage rules, user settings, and knowledge. These tools are exposed to the LLM, which decides which one to call based on the user's request.
| Tool Name | Description | Source File |
|---|---|---|
getUserRulesAndSettings | Retrieves all existing rules and user settings. | apps/web/utils/ai/assistant/chat.ts:21-93 |
createRule | Creates a new rule. | apps/web/utils/ai/assistant/chat.ts:153-211 |
updateRuleConditions | Updates the conditions (AI instructions, static filters) of an existing rule. | apps/web/utils/ai/assistant/chat.ts:241-300 |
updateRuleActions | Replaces the actions of an existing rule. | apps/web/utils/ai/assistant/chat.ts:302-387 |
updateLearnedPatterns | Adds or removes "learned patterns" (e.g., specific sender emails or subjects) to a rule. | apps/web/utils/ai/assistant/chat.ts:389-478 |
update_sender_category | Changes the category assigned to a specific sender. | apps/web/utils/ai/assistant/process-user-request.ts:672-731 |
updateAbout | Updates the user's "about" information, which provides context to the AI. | apps/web/utils/ai/assistant/chat.ts:480-508 |
addToKnowledgeBase | Adds new content to the user's personal knowledge base for drafting replies. | apps/web/utils/ai/assistant/chat.ts:510-542 |
reply | Sends a final text response back to the user in the chat interface. | apps/web/utils/ai/assistant/process-user-request.ts:659-666 |
Knowledge Base Integration
The AI Assistant can leverage a personal knowledge base to help draft email replies. This is particularly useful for rules with a DRAFT_REPLY action that do not have pre-set content.
The aiExtractRelevantKnowledge function is key to this process. When a reply needs to be drafted, this function is called with the email content and the user's knowledge base entries. It uses an LLM to identify and extract only the most relevant pieces of information from the knowledge base that can help answer the query in the email. This extracted content is then passed to another AI agent responsible for drafting the final reply.
This flow ensures that replies are contextual and draw from the user's specific knowledge, such as pricing, availability, or standard procedures.
This diagram shows the knowledge extraction process.
Conclusion
The AI Assistant and Rules Engine is a sophisticated system that combines structured rule-based automation with the flexibility of large language models. It empowers users to manage their inbox efficiently by translating natural language instructions into executable actions, handling interactive modifications via a chat interface, and leveraging a personal knowledge base for intelligent reply drafting. The architecture is designed to be extensible, allowing for new actions, conditions, and AI capabilities to be integrated over time.