Skip to content

Custom Adapter โ€‹

The adapter system lets you bind external infrastructure (databases, caches, vector stores, queues) to the framework via a single registry. Modules auto-pick the right adapter by category โ€” swap backends at deploy time without touching agent code.

ts
import {
  createAdapterRegistry,
  InMemoryCacheAdapter,
  InMemoryVectorAdapter,
} from 'confused-ai/adapters';

Quick start โ€‹

ts
import { createAdapterRegistry, InMemoryCacheAdapter } from 'confused-ai/adapters';
import { createAgent } from 'confused-ai';

const registry = createAdapterRegistry();

// Register adapters (in-memory for dev)
registry.register(new InMemoryCacheAdapter());
registry.register(new InMemoryVectorAdapter());

await registry.connectAll();

const agent = createAgent({
  name: 'my-agent',
  instructions: '...',
  model: 'gpt-4o-mini',
  apiKey: process.env.OPENAI_API_KEY!,
  adapters: registry,   // modules auto-resolve their adapter
});

Built-in adapters (zero dependencies) โ€‹

These ship with the framework and require no external services:

AdapterClassDescription
CacheInMemoryCacheAdapterIn-process TTL cache
VectorInMemoryVectorAdapterCosine similarity search
SQLInMemorySqlAdapterIn-process relational store
NoSQLInMemoryNoSqlAdapterDocument store
SearchInMemorySearchAdapterFull-text search
Object storageInMemoryObjectStorageAdapterBlob / file store
GraphInMemoryGraphAdapterNode-edge graph
Message queueInMemoryMessageQueueAdapterPub/sub queue
SessionInMemorySessionStoreAdapterConversation history
MemoryInMemoryMemoryStoreAdapterAgent long-term memory
RAGInMemoryRagAdapterVector retrieval
Rate limitInMemoryRateLimitAdapterToken-bucket limiter
Audit logInMemoryAuditLogAdapterAudit trail

Production preset โ€‹

Wire all core adapters in one call using createProductionSetup:

ts
import { createProductionSetup } from 'confused-ai/adapters';

const { registry } = await createProductionSetup({
  postgres: { connectionString: process.env.DATABASE_URL! },
  redis:    { url: process.env.REDIS_URL! },
  pinecone: { apiKey: process.env.PINECONE_API_KEY! },
});

const agent = createAgent({ adapters: registry, ... });

Implement a custom adapter โ€‹

Pick the category interface that matches your backend. All adapters share the same base:

ts
import type { CacheAdapter, Adapter } from 'confused-ai/adapters';

class RedisCacheAdapter implements CacheAdapter {
  readonly category = 'cache' as const;
  readonly name = 'redis';

  private client: Redis;

  constructor(config: { url: string }) {
    this.client = new Redis(config.url);
  }

  async connect()    { await this.client.ping(); }
  async disconnect() { await this.client.quit(); }
  async health()     { return { connected: true }; }

  async get(key: string)                                           { return JSON.parse(await this.client.get(key) ?? 'null'); }
  async set(key: string, value: unknown, ttlSeconds?: number)     { await this.client.set(key, JSON.stringify(value), 'EX', ttlSeconds ?? 3600); }
  async del(key: string)                                           { await this.client.del(key); }
  async flush(pattern: string)                                     { const keys = await this.client.keys(pattern); if (keys.length) await this.client.del(...keys); return keys.length; }
}

// Register
registry.register(new RedisCacheAdapter({ url: process.env.REDIS_URL! }));

Adapter categories โ€‹

CategoryInterfaceUse for
sqlSqlAdapterRelational data, joins, transactions
nosqlNoSqlAdapterDocument collections
vectorVectorAdapterEmbedding similarity search
cacheCacheAdapterTTL key-value cache
searchSearchAdapterFull-text / keyword search
object-storageObjectStorageAdapterFile/blob storage (S3, GCS)
time-seriesTimeSeriesAdapterMetrics, sensor data
graphGraphAdapterGraph traversal
message-queueMessageQueueAdapterPub/sub, task queues
observabilityObservabilityAdapterLogs, traces, metrics
embeddingEmbeddingAdapterText โ†’ vector
sessionSessionStoreAdapterConversation history
memoryMemoryStoreAdapterAgent long-term memory
ragRagAdapterRetrieve + augment
guardrailGuardrailAdapterContent safety
authAuthAdapterAuthentication
rate-limitRateLimitAdapterRate limiting
auditAuditLogAdapterAudit trail

AdapterRegistry interface โ€‹

ts
interface AdapterRegistry {
  register(adapter: AnyAdapter, opts?: { replace?: boolean }): void;
  unregister(category: AdapterCategory, name: string): boolean;
  resolve<T>(category: AdapterCategory, name?: string): T;
  connectAll(): Promise<void>;
  disconnectAll(): Promise<void>;
  health(): Promise<Record<string, AdapterHealth>>;
  list(): AnyAdapter[];
}

Where to go next โ€‹

  • Storage โ€” key-value store built on top of the adapter system.
  • Session โ€” conversation persistence via SessionStoreAdapter.
  • Secret manager โ€” fetch credentials for adapter configuration.

Released under the MIT License.