Email Search Index (storage/search.ts)

gitingest_agenticmail_agenticmail · packages/core/src/storage/search.ts · typescript · MIT
search, email, database, tag:fts5

Indexes searchable email fields in SQLite FTS5 and prevents duplicate entries, enabling fast full‑text queries for agents.

// Source: https://github.com/agenticmail/agenticmail | License: MIT | Copyright 2026 Ope Olatunji
// Provided via justclaw.it.com; keep this notice with the code.
import type { Database } from './db.js';

export interface SearchableEmail {
  agentId: string;
  messageId: string;
  subject: string;
  fromAddress: string;
  toAddress: string;
  bodyText: string;
  receivedAt: string;
}

export class EmailSearchIndex {
  constructor(private db: Database) {}

  index(email: SearchableEmail): void {
    // FTS5 tables have no UNIQUE constraint — guard against duplicate entries
    if (email.messageId) {
      const existing = this.db.prepare(
        'SELECT rowid FROM email_search WHERE agent_id = ? AND message_id = ?',
      ).get(email.agentId, email.messageId);
      if (existing) return;


... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key