Skip to main content
Experimental API - Context Connectors is experimental and subject to breaking changes.
Build a custom indexer to fetch content from any source (API, database, CMS) and index it with DirectContext.

Basic Example

import { Indexer, FilesystemStore } from "@augmentcode/context-connectors";
import type { Source, FileEntry, FileChanges, SourceMetadata, FileInfo } from "@augmentcode/context-connectors";

// Implement the Source interface for your data source
class ApiSource implements Source {
  readonly type = "custom" as const;

  async fetchAll(): Promise<FileEntry[]> {
    // Replace with your actual data source (API, database, CMS, etc.)
    const response = await fetch("https://api.example.com/docs");
    const docs = await response.json();
    return docs.map((doc: any) => ({
      path: doc.path,
      contents: doc.content,
    }));
  }

  async fetchChanges(previous: SourceMetadata): Promise<FileChanges | null> {
    // Return null to always do full re-index
    // Or implement incremental updates based on your data source
    return null;
  }

  async getMetadata(): Promise<SourceMetadata> {
    return {
      type: "custom",
      identifier: "api-docs",
      ref: new Date().toISOString(),
      syncedAt: new Date().toISOString(),
    };
  }

  async listFiles(directory?: string): Promise<FileInfo[]> {
    const files = await this.fetchAll();
    return files.map(f => ({ path: f.path, type: "file" as const }));
  }

  async readFile(path: string): Promise<string | null> {
    const files = await this.fetchAll();
    return files.find(f => f.path === path)?.contents ?? null;
  }
}

// Usage
const source = new ApiSource();
const store = new FilesystemStore({ basePath: "./indexes" });
const indexer = new Indexer();

const result = await indexer.index(source, store, "my-docs");
console.log(`Indexed ${result.filesIndexed} files (${result.type})`);
Index Layout: The indexer saves two files:
  • state.json - Full state including file path list (for incremental indexing)
  • search.json - Optimized state without file list (smaller, for search clients)
Search clients should load search.json. Indexers need state.json for incremental updates.

Data Source Examples

REST API:
async fetchAll(): Promise<FileEntry[]> {
  const response = await fetch("https://api.example.com/docs");
  const docs = await response.json();
  return docs.map((doc: any) => ({ path: doc.path, contents: doc.content }));
}
Database:
async fetchAll(): Promise<FileEntry[]> {
  const docs = await db.query("SELECT path, content FROM documents");
  return docs.map(doc => ({ path: doc.path, contents: doc.content }));
}

Automation

Cron (Node.js):
0 * * * * cd /path/to/project && npx tsx indexer.ts
Cron (Python):
0 * * * * cd /path/to/project && python indexer.py
GitHub Actions: See GitHub Actions Auto-Indexing

Next Steps