Open History to see the last commit
1import { sqlitePersistence } from './sqlite-persistence'
2
3/** Stable thread id for the single-conversation demo. */
4export const PERSISTENT_CHAT_THREAD_ID = 'persistent-chat'
5
6let instance: ReturnType<typeof sqlitePersistence> | undefined
7
8/**
9 * One SQLite-backed persistence store for the persistent-chat demo, shared by
10 * the API route (POST writes the transcript, GET replays / reconstructs it) and
11 * the history server function the page loader calls. Lazily opened so importing
12 * this module (e.g. from a server-fn module that a client route also imports)
13 * never opens the database in the browser bundle. `migrate: true` creates the
14 * TanStack AI tables on first open. The `sqlitePersistence` implementation lives
15 * in `./sqlite-persistence` — a self-contained `node:sqlite` backend built on the
16 * `@tanstack/ai-persistence` core, demonstrating how to roll your own. `.data/`
17 * is gitignored.
18 */
19export function persistentChatPersistence() {
20 return (instance ??= sqlitePersistence({
21 url: './.data/persistent-chat.db',
22 migrate: true,
23 }))
24}