Feature: Smart Categories
Relevant source files
The following files were used as context for generating this wiki page:
- apps/web/app/(app)/[emailAccountId]/smart-categories/page.tsx
- apps/web/utils/ai/categorize-sender/ai-categorize-senders.ts
- apps/web/app/(app)/[emailAccountId]/smart-categories/setup/SetUpCategories.tsx
- apps/web/app/(app)/[emailAccountId]/assistant/onboarding/CategoriesSetup.tsx
- apps/web/utils/ai/assistant/process-user-request.ts
- apps/web/utils/ai/report/response-patterns.ts
The Smart Categories feature provides an automated way to organize an inbox by categorizing email senders. It combines user-defined categories with an AI-powered process to analyze and classify senders, facilitating bulk email management and enhancing the capabilities of the AI assistant. If a user has no categories set up, they are redirected to a setup page to begin the process.
The system uses a mix of default and custom categories. Users can manage these categories through a dedicated UI, during the initial onboarding process, or conversationally via the AI assistant. The core of the feature is an AI function that can bulk-categorize unclassified senders based on their email history.
Sources: apps/web/app/(app)/[emailAccountId]/smart-categories/page.tsx:43-44, apps/web/app/(app)/[emailAccountId]/smart-categories/setup/SetUpCategories.tsx
User Interface and Setup
The user interacts with the Smart Categories feature through several UI components, primarily for setup, management, and viewing categorized senders.
Main Categories Page
The main interface for this feature is located at /smart-categories. It consists of two main tabs:
- Categories: Displays a grouped table (
GroupedTable) of all senders that have been assigned a category. - Uncategorized: Shows a list of senders that have not yet been categorized.
This page provides several key actions:
- Bulk Categorize: Initiates an AI process to categorize uncategorized senders.
- Edit: Navigates to the category setup page to manage existing categories.
- Create Category: Opens a dialog to create a new custom category.
Sources: apps/web/app/(app)/[emailAccountId]/smart-categories/page.tsx:51-125
Category Setup and Onboarding
Users can define and manage their categories during initial onboarding or through a dedicated setup page.
Initial Setup Flow
If a user has no categories, they are directed to /smart-categories/setup. The SetUpCategories component manages this process.
- Default Categories: The user is presented with a list of default categories (e.g., "Newsletter", "Receipts").
- User Selection: The user can enable or disable these defaults and add their own custom categories.
- Saving: On submission, the
upsertDefaultCategoriesActionserver action is called to save the user's configuration. Categories can also be deleted viadeleteCategoryAction.
The diagram below illustrates the category setup flow.
Onboarding Integration
A similar, simplified category setup is part of the user onboarding flow. The CategoriesSetup component in the assistant onboarding and the OnboardingCategories component guide the user to select actions for predefined categories (e.g., "Label", "Label & skip inbox"). This configuration is then saved using the createRulesOnboardingAction.
Sources: apps/web/app/(app)/[emailAccountId]/assistant/onboarding/CategoriesSetup.tsx, apps/web/app/(app)/[emailAccountId]/onboarding/OnboardingCategories.tsx
AI-Powered Sender Categorization
The core of the "smart" functionality lies in its ability to use an LLM to automatically categorize senders.
Core Logic: aiCategorizeSenders
This function orchestrates the AI-based categorization process.
- Input: It receives the user's
emailAccount, a list ofsenders(each with recent email subjects and snippets), and the user's definedcategories. - Prompt Generation: It constructs a detailed prompt for the LLM, including a system message, the sender data, the available categories, and specific instructions.
- AI Invocation: It calls the LLM using
createGenerateObjectand expects a JSON response that conforms tocategorizeSendersSchema. - Post-processing: The function validates the AI's response. It matches the categorized senders from the response with the original sender list and filters out any results where the AI assigned a category that doesn't exist.
The following diagram shows the logic flow within the aiCategorizeSenders function.
Prompt and Schema
The AI is guided by a structured prompt and a strict output schema.
Prompt Structure
The prompt sent to the LLM is composed of several parts:
| Section | Description |
|---|---|
| System Message | Instructs the AI that it is an email management assistant. |
| Sender Data | A list of senders, each including their email address and recent email subjects/snippets, formatted within <sender> tags. |
| Categories | A list of available categories for the user, including their names and descriptions, formatted within <categories> tags. |
| Instructions | Explicit rules for the AI, such as using "Unknown" for unclear cases or REQUEST_MORE_INFORMATION_CATEGORY if more context is needed. |
| Importance Block | Emphasizes accuracy and adherence to the provided category list and JSON format. |
Sources: apps/web/utils/ai/categorize-sender/ai-categorize-senders.ts:33-72
Output Schema
The AI is required to respond in a JSON format defined by categorizeSendersSchema.
// Source: apps/web/utils/ai/categorize-sender/ai-categorize-senders.ts:11-19
const categorizeSendersSchema = z.object({
senders: z.array(
z.object({
rationale: z.string().describe("Keep it short."),
sender: z.string(),
category: z.string(), // not using enum, because sometimes the ai creates new categories
}),
),
});
Bulk Categorization Flow
When a user clicks the "Bulk Categorize" button, it triggers a sequence of events involving the UI, an API endpoint, and the AI categorization logic.
This sequence diagram illustrates the end-to-end flow.
Integration with Other Features
Smart Categories are not a standalone feature; they are integrated with other parts of the application, such as the AI Assistant and reporting.
AI Assistant
The AI Assistant can manage sender categories conversationally. The processUserRequest function provides the assistant with tools to interact with categories.
| Tool Name | Description |
|---|---|
update_sender_category | Changes the category for a specific sender. |
add_categories | Adds one or more categories to a rule. |
remove_categories | Removes one or more categories from a rule. |
This allows users to make adjustments like "move emails from sender@example.com to the 'Work' category" using natural language.
Sources: apps/web/utils/ai/assistant/process-user-request.ts:400-522
AI-Driven Category Suggestion
The system can also proactively suggest new categories. The aiAnalyzeResponsePatterns function analyzes the user's sent and received emails to identify common themes and response patterns. As part of its output, it suggests a potential categoryOrganization strategy, including new category names, descriptions, and priority levels. This helps users discover meaningful ways to organize their inbox.
Sources: apps/web/utils/ai/report/response-patterns.ts:10-37, apps/web/utils/ai/report/response-patterns.ts:60-63
Conclusion
The Smart Categories feature is a cornerstone of inbox organization within the application. It effectively merges manual user control with powerful AI automation. By allowing users to define their own organizational structure and then leveraging an LLM to apply it at scale, the feature significantly reduces the manual effort required to manage a busy inbox. Its integration with the AI Assistant further enhances usability, making category management a dynamic and conversational process.