@delphi-ai/multi-tenant-chat
v0.1.0
Published
Multi-tenant runtime manager for the Vercel Chat SDK
Maintainers
Readme
@delphi-ai/multi-tenant-chat
Multi-tenant runtime manager for the Vercel Chat SDK. Manages per-connection Chat instances with lazy creation, LRU eviction, and AsyncLocalStorage-based connection context.
Install
# From GitHub (until published to npm)
pnpm add github:delphi-ai/multi-tenant-chat
# Peer dependency
pnpm add chatUsage
import { MultiTenantChat, type ChatConnection } from "@delphi-ai/multi-tenant-chat";
interface MyConnection extends ChatConnection {
ownerId: string;
}
const manager = new MultiTenantChat<MyConnection>({
async resolveConnection({ platform, connectionKey, request }) {
// Look up connection from your database
return { id: connectionKey, platform, ownerId: "user-123" };
},
async resolveCredentials(connection) {
// Return platform-specific credentials
return { botToken: "xoxb-..." };
},
state: yourStateAdapter,
platforms: {
slack: (credentials) => createSlackAdapter(credentials),
telegram: (credentials) => createTelegramAdapter(credentials),
},
async onDirectMessage(ctx, thread, message) {
await thread.post("Hello from multi-tenant chat!");
},
});
// In your webhook handler:
export async function POST(request: Request) {
return manager.handleWebhook(request, {
platform: "telegram",
connectionKey: "connection-id",
});
}API
MultiTenantChat<T extends ChatConnection>
Main class that manages per-connection Chat instances.
Config:
resolveConnection— resolve a connection from platform + key + requestresolveCredentials— return credentials for a connectionstate— aStateAdapter(shared across all connections, keys are auto-prefixed)platforms— map of platform name to adapter factoryonDirectMessage/onMention/onSubscribedMessage— message handlersonError— global error handlerruntimeCacheMax— max cached Chat instances (default 1000)
Methods:
handleWebhook(request, { platform, connectionKey })— route a webhook to the correct Chat instancedestroyRuntime(connectionId)— tear down a cached runtime
getConnection<T>()
Retrieve the current connection from AsyncLocalStorage context (inside a handler).
createPrefixedState(base, prefix)
Wrap any StateAdapter with per-connection key namespacing.
RuntimeCache
LRU cache of live Chat instances with lazy creation and eviction.
License
MIT
