@namitjain.india/agent-memory-postgres
v0.3.0
Published
Postgres adapter for agent-memory using pgvector
Maintainers
Readme
@namitjain.india/agent-memory-postgres
Postgres adapter for @namitjain.india/agent-memory. Provides production-scale persistence with pgvector support for semantic search.
GitHub | Report Bug | Request Feature
Installation
npm install @namitjain.india/agent-memory-postgres pg pgvectorUsage
import { AgentMemory } from "@namitjain.india/agent-memory";
import { PostgresAdapter } from "@namitjain.india/agent-memory-postgres";
const memory = new AgentMemory({
adapter: new PostgresAdapter({
connectionString: "postgresql://user:pass@localhost:5432/memory"
}),
embedding: {
embedFn: async (text) => [text.length / 100, 0.5]
}
});PostgresAdapter Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| connectionString | string | - | PostgreSQL connection string |
| client | PgClientLike | - | Custom pg client/pool instance |
| tableName | string | memory_items | Custom table name |
| autoCreateExtension | boolean | true | Auto-create vector extension |
Features
- Production Scale: Designed for high-traffic production environments
- pgvector Support: Native vector similarity search using
<=>operator - Connection Pooling: Uses pg Pool for efficient connections
- Type Support: Full support for entries, facts, and summaries
- Schema Auto-Creation: Tables and indexes created automatically
- Async Initialization: Lazy initialization with promise caching
Database Schema
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE memory_items (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
session_id TEXT NOT NULL,
timestamp BIGINT NOT NULL,
importance REAL NOT NULL DEFAULT 0.5,
role TEXT,
content TEXT,
key_name TEXT,
value_text TEXT,
embedding vector,
metadata JSONB,
from_timestamp BIGINT,
to_timestamp BIGINT,
replaced_entry_ids TEXT[]
);
CREATE INDEX idx_memory_items_session ON memory_items(session_id, timestamp);Example with Custom Table
const memory = new AgentMemory({
adapter: new PostgresAdapter({
connectionString: "postgresql://...",
tableName: "user_memory", // Custom table name
autoCreateExtension: true
}),
embedding: {
embedFn: async (text) => [/* embeddings */]
}
});Example with Custom Client
import { Pool } from "pg";
const pool = new Pool({ connectionString: "postgresql://..." });
const memory = new AgentMemory({
adapter: new PostgresAdapter({
client: pool // Reuse existing connection pool
}),
embedding: {
embedFn: async (text) => [/* embeddings */]
}
});Methods
close()
Close the connection pool.
const adapter = new PostgresAdapter({ connectionString: "postgresql://..." });
// ... use adapter ...
await adapter.close();Requirements
- Node.js >= 18.0.0
- PostgreSQL >= 14.0 (for vector support)
pg>= 8.0.0pgvector>= 0.2.0
Contributing & Collaboration
We welcome contributions, feedback, and feature requests!
- Bug Reports: Open an issue
- Feature Requests: Share it
- Pull Requests: Submit a PR
If this project helps you, please consider starring it on GitHub!
License
MIT
