PAIPAI

createMongoThreadStore()

Production thread storage. Mongo replica set required.

createMongoThreadStore is the production ThreadStore. Threads, runs, and messages survive process restart.

Mongo uses transactions. Run a replica set, even locally.

import { createMongoThreadStore } from "@pai/storage-mongo";

const storage = createMongoThreadStore({
  url: process.env.MONGO_URL!,
});
await storage.initialize();

export const pai = createPai({
  agents: { main: assistantAgent },
  scopeKey: (identity) => identity.workspaceId,
  storage,
});

Call initialize() at boot so connection and index errors fail startup, not the first request.

Import

import { createMongoThreadStore } from "@pai/storage-mongo";

Signature

function createMongoThreadStore(options: {
  url: string;
  dbName?: string;
  collectionName?: string;
  onWarning?: MongoThreadStoreWarningHandler;
}): ThreadStore & { initialize(): Promise<void>; close(): Promise<void> };

Parameters

options: MongoThreadStoreOptions

url: string

MongoDB connection string. Database name may come from the URI path.

dbName?: string

Defaults to pai.

collectionName?: string

Defaults to pai_threads.

onWarning?: (warning) => void

Invalid documents skipped during listing.

Returns

A MongoThreadStore.

Pass as createPai({ storage }). close() shuts the provider-owned client.

Local / tests: createInMemoryThreadStore().

Learn more

On this page