0.0.2 • Published 5 months ago

@steerprotocol/langchain-pglite-store v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@steerprotocol/langchain-pglite-store

Implementation of a LangChain.js chat message history store that uses PGlite for local, in-memory, or browser-based PostgreSQL storage.

Features

  • 🚀 Works in Node.js, Bun, Deno, and browsers
  • 💾 Supports in-memory, file system, and IndexedDB storage
  • 🔄 No external PostgreSQL server required
  • 🌐 Perfect for local development and browser-based applications

Installation

npm install @steerprotocol/langchain-pglite-store

Usage

import { PostgresChatMessageHistory } from "@steerprotocol/langchain-pglite-store";
import { HumanMessage, AIMessage } from "@langchain/core/messages";

// Create an in-memory chat history
const history = new PostgresChatMessageHistory({
  sessionId: "chat-session-1",
  poolConfig: {
    dataDir: "memory://chat-db"
  }
});

// Add messages
await history.addMessage(new HumanMessage("Hello! How are you?"));
await history.addMessage(new AIMessage("I'm doing great! How can I help you today?"));

// Retrieve messages
const messages = await history.getMessages();
console.log(messages);

// Clear messages
await history.clear();

// Don't forget to close the connection when done
await history.end();

Storage Options

PGlite supports multiple storage backends:

In-Memory (Ephemeral)

const history = new PostgresChatMessageHistory({
  sessionId: "chat-session",
  poolConfig: {
    dataDir: "memory://my-db"
  }
});

File System (Node.js/Bun)

const history = new PostgresChatMessageHistory({
  sessionId: "chat-session",
  poolConfig: {
    dataDir: "file://./path/to/data"
  }
});

IndexedDB (Browser)

const history = new PostgresChatMessageHistory({
  sessionId: "chat-session",
  poolConfig: {
    dataDir: "idb://my-database"
  }
});

Configuration Options

The PostgresChatMessageHistory constructor accepts the following options:

type PostgresChatMessageHistoryInput = {
  // Name of the table to use (defaults to "langchain_chat_histories")
  tableName?: string;
  
  // Required session ID to separate different chat histories
  sessionId: string;
  
  // PGlite configuration options
  poolConfig?: PGliteOptions;
  
  // Or provide an existing PGlite instance
  pool?: PGlite;
  
  // Whether to escape table names (defaults to false)
  escapeTableName?: boolean;
};

Testing

The tests use in-memory databases, so no setup is required. Just run:

npm test

License

MIT

0.0.2

5 months ago

0.0.1

5 months ago