Keep every thread and message in a database you own, with a thread list sidebar, titles, archive and delete, using RemoteThreadListAdapter and a history adapter.
Your users expect their conversations to be there tomorrow, and you may need the data in your own database for compliance, search or analytics. This recipe stores every thread and message in SQLite through three small API routes, and adds a thread list sidebar where users can switch, rename, archive and delete threads. The storage code is one small module, so moving to Postgres or another store is a contained change.
Follow https://www.assistant-ui.com/docs/recipes/chat-history.md to save my assistant-ui threads and messages in my own database.What it uses
- Threads: a thread is one conversation, and the thread list holds them all. The runtime asks your adapter for the list and creates a thread row the first time a new thread gets a message.
RemoteThreadListAdaptermaps thread list actions (list, create, rename, archive, delete, title) to your API.ThreadHistoryAdapterloads and saves the messages of the open thread. WithuseChatRuntimeyou implement itswithFormatvariant, which gives you a format adapter that encodes each AI SDK message for storage and decodes it on load. Adapters lists the other adapter slots.useChatRuntimetalks to the chat route, as in a new app.
The code
The complete app is examples/recipe-chat-history. Starting from an app made with npx assistant-ui@latest create, add the database module and the thread routes, then the adapter and the page. Persistence needs no change to the chat route; the example's is a trimmed copy of the starter's.
The database: two tables, threads and messages. node:sqlite ships with Node.js, so there is nothing to install.
import { DatabaseSync } from "node:sqlite";
export type ThreadRow = {
id: string;
title: string | null;
status: "regular" | "archived";
};
export type MessageRow = {
id: string;
parent_id: string | null;
format: string;
content: Record<string, unknown>;
};
let db: DatabaseSync | undefined;
function getDb() {
if (db) return db;
db = new DatabaseSync(process.env.DATABASE_PATH ?? "chat.db");
db.exec(`
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS threads (
id TEXT PRIMARY KEY,
title TEXT,
status TEXT NOT NULL DEFAULT 'regular',
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS messages (
thread_id TEXT NOT NULL REFERENCES threads(id) ON DELETE CASCADE,
id TEXT NOT NULL,
parent_id TEXT,
format TEXT NOT NULL,
content TEXT NOT NULL,
PRIMARY KEY (thread_id, id)
);
`);
return db;
}
export const threads = {
list: () =>
getDb()
.prepare("SELECT id, title, status FROM threads ORDER BY updated_at DESC")
.all() as ThreadRow[],
get: (id: string) =>
getDb()
.prepare("SELECT id, title, status FROM threads WHERE id = ?")
.get(id) as ThreadRow | undefined,
create: (id: string) => {
getDb()
.prepare("INSERT INTO threads (id, updated_at) VALUES (?, ?)")
.run(id, Date.now());
},
update: (
id: string,
patch: {
title?: string | undefined;
status?: ThreadRow["status"] | undefined;
},
) => {
getDb()
.prepare(
"UPDATE threads SET title = coalesce(?, title), status = coalesce(?, status), updated_at = ? WHERE id = ?",
)
.run(patch.title ?? null, patch.status ?? null, Date.now(), id);
},
delete: (id: string) => {
getDb().prepare("DELETE FROM threads WHERE id = ?").run(id);
},
};
export const messages = {
// Insertion order is conversation order; the runtime rebuilds branches from parent_id.
list: (threadId: string): MessageRow[] =>
(
getDb()
.prepare(
"SELECT id, parent_id, format, content FROM messages WHERE thread_id = ? ORDER BY rowid",
)
.all(threadId) as (Omit<MessageRow, "content"> & { content: string })[]
).map((row) => ({ ...row, content: JSON.parse(row.content) })),
// An upsert, because the runtime saves a message again when it changes after its first save.
save: (threadId: string, message: MessageRow) => {
getDb()
.prepare(
`INSERT INTO messages (id, thread_id, parent_id, format, content) VALUES (?, ?, ?, ?, ?)
ON CONFLICT (thread_id, id) DO UPDATE SET format = excluded.format, content = excluded.content`,
)
.run(
message.id,
threadId,
message.parent_id,
message.format,
JSON.stringify(message.content),
);
getDb()
.prepare("UPDATE threads SET updated_at = ? WHERE id = ?")
.run(Date.now(), threadId);
},
};The thread routes. They return what the adapter reads and nothing more:
import { threads } from "@/lib/db";
export async function GET() {
return Response.json(threads.list());
}
export async function POST() {
const id = crypto.randomUUID();
threads.create(id);
return Response.json({ id });
}import { z } from "zod";
import { threads } from "@/lib/db";
type Params = { params: Promise<{ id: string }> };
const ThreadPatch = z.object({
title: z.string().optional(),
status: z.enum(["regular", "archived"]).optional(),
});
export async function GET(_req: Request, { params }: Params) {
const thread = threads.get((await params).id);
if (!thread) return new Response(null, { status: 404 });
return Response.json(thread);
}
export async function PATCH(req: Request, { params }: Params) {
const { id } = await params;
if (!threads.get(id)) return new Response(null, { status: 404 });
const patch = ThreadPatch.safeParse(await req.json().catch(() => null));
if (!patch.success) return new Response(null, { status: 400 });
threads.update(id, patch.data);
return new Response(null, { status: 204 });
}
export async function DELETE(_req: Request, { params }: Params) {
threads.delete((await params).id);
return new Response(null, { status: 204 });
}import { z } from "zod";
import { messages, threads } from "@/lib/db";
type Params = { params: Promise<{ id: string }> };
const StoredMessage = z.object({
id: z.string(),
parent_id: z.string().nullable(),
format: z.string(),
content: z.record(z.string(), z.unknown()),
});
export async function GET(_req: Request, { params }: Params) {
const { id } = await params;
if (!threads.get(id)) return new Response(null, { status: 404 });
return Response.json(messages.list(id));
}
export async function POST(req: Request, { params }: Params) {
const { id } = await params;
if (!threads.get(id)) return new Response(null, { status: 404 });
const message = StoredMessage.safeParse(await req.json().catch(() => null));
if (!message.success) return new Response(null, { status: 400 });
messages.save(id, message.data);
return new Response(null, { status: 204 });
}proxy.ts refuses cross-origin calls to the thread routes, as the chat route does for itself:
import { NextResponse, type NextRequest } from "next/server";
import { isAllowedRequestContext } from "./app/api/request-context";
// Refuses cross-origin calls to the thread routes, as the chat route does for itself.
export function proxy(req: NextRequest) {
if (!isAllowedRequestContext(req)) {
return NextResponse.json(
{ error: "Cross-origin requests are not allowed." },
{ status: 403 },
);
}
return NextResponse.next();
}
export const config = { matcher: "/api/threads/:path*" };The adapter, which calls those routes from the browser:
import {
useAui,
type RemoteThreadListAdapter,
type ThreadHistoryAdapter,
} from "@assistant-ui/react";
import { createAssistantStream } from "assistant-stream";
import { useMemo } from "react";
import type { MessageRow, ThreadRow } from "@/lib/db";
async function api<T = void>(path: string, init?: RequestInit): Promise<T> {
const res = await fetch(path, {
...init,
headers: { "content-type": "application/json" },
});
if (!res.ok)
throw new Error(`${init?.method ?? "GET"} ${path}: ${res.status}`);
return res.status === 204 ? (undefined as T) : res.json();
}
const patchThread = (id: string, patch: Partial<ThreadRow>) =>
api(`/api/threads/${id}`, { method: "PATCH", body: JSON.stringify(patch) });
const toMetadata = (thread: ThreadRow) => ({
remoteId: thread.id,
status: thread.status,
title: thread.title ?? undefined,
});
// Loads and saves the messages of whichever thread is open.
function useThreadAdapters() {
const aui = useAui();
const history = useMemo<ThreadHistoryAdapter>(
() => ({
// useChatRuntime only calls the withFormat variant below.
load: async () => ({ messages: [] }),
append: async () => {},
withFormat: (format) => {
const save = async (item: Parameters<typeof format.encode>[0]) => {
const { remoteId } = await aui.threadListItem.initialize();
const row: MessageRow = {
id: format.getId(item.message),
parent_id: item.parentId,
format: format.format,
content: format.encode(item),
};
await api(`/api/threads/${remoteId}/messages`, {
method: "POST",
body: JSON.stringify(row),
});
};
return {
async load() {
const { remoteId } = aui.threadListItem.getState();
if (!remoteId) return { messages: [] };
const rows = await api<MessageRow[]>(
`/api/threads/${remoteId}/messages`,
);
return {
messages: rows.map((row) =>
format.decode(row as Parameters<typeof format.decode>[0]),
),
};
},
append: save,
update: save,
};
},
}),
[aui],
);
return useMemo(() => ({ history }), [history]);
}
export const threadListAdapter: RemoteThreadListAdapter = {
async list() {
const rows = await api<ThreadRow[]>("/api/threads");
return { threads: rows.map(toMetadata) };
},
async initialize() {
const { id } = await api<{ id: string }>("/api/threads", {
method: "POST",
});
return { remoteId: id };
},
async fetch(remoteId) {
return toMetadata(await api<ThreadRow>(`/api/threads/${remoteId}`));
},
rename: (remoteId, title) => patchThread(remoteId, { title }),
archive: (remoteId) => patchThread(remoteId, { status: "archived" }),
unarchive: (remoteId) => patchThread(remoteId, { status: "regular" }),
delete: (remoteId) => api(`/api/threads/${remoteId}`, { method: "DELETE" }),
// Titles the thread with the start of its first message; call a model here for smarter titles.
async generateTitle(remoteId, messages) {
const first = messages[0]?.content.find((part) => part.type === "text");
const text = first?.type === "text" ? first.text : "New chat";
const title = Array.from(text).slice(0, 60).join("");
await patchThread(remoteId, { title });
return createAssistantStream((controller) => {
controller.appendText(title);
});
},
unstable_useAdapters: useThreadAdapters,
};The page wraps useChatRuntime in useRemoteThreadListRuntime, so each thread gets its own chat runtime:
"use client";
import {
AssistantRuntimeProvider,
useRemoteThreadListRuntime,
} from "@assistant-ui/react";
import { useChatRuntime } from "@assistant-ui/ai-sdk";
import { Thread } from "@/components/assistant-ui/elements/thread.aui";
import { ThreadList } from "@/components/assistant-ui/elements/thread-list.aui";
import { threadListAdapter } from "./thread-list-adapter";
export default function Home() {
const runtime = useRemoteThreadListRuntime({
runtimeHook: () => useChatRuntime(),
adapter: threadListAdapter,
});
return (
<AssistantRuntimeProvider runtime={runtime}>
<main className="grid h-dvh grid-rows-[minmax(0,30%)_minmax(0,1fr)] gap-4 p-4 md:grid-cols-[260px_1fr] md:grid-rows-1">
<div className="min-h-0 overflow-y-auto">
<ThreadList />
</div>
<Thread />
</main>
</AssistantRuntimeProvider>
);
}How it works
- The runtime calls
list()to fill the sidebar. A new thread exists only in the browser until its first message; then the history adapter callsaui.threadListItem.initialize(), which calls yourinitialize()and returns the newremoteId. Always await it before you write a message, or the first message races the thread row. withFormatreceives a format adapter. Storeformat.format,format.getId(item.message),item.parentIdandformat.encode(item)as they are, and pass the row back throughformat.decodeon load. WithuseChatRuntimethe format string is"ai-sdk/v6", also on AI SDK v7; never rename it, or stored history stops loading.parent_idrecords the message tree, so edits and regenerations come back as branches. Return rows in the order you saved them. Message ids are unique only within a thread, so the key is(thread_id, id).updaterewrites a message that changed after it was saved. The runtime uses it to finish an assistant message saved early, for example one paused on a tool approval, so make the save an upsert.generateTitleruns once the first message settles. This example titles the thread with the start of the first message; call a model there for a summary title.unstable_useAdaptersruns once per open thread. Readaui.threadListIteminside each call, not when the hook mounts, and memoize what it returns.- This example has one user. In your app, add a user id column and filter every query by the signed-in user, as Custom thread persistence does with Postgres and Auth.js.
Run it
From the repository root:
pnpm install
pnpm turbo build --filter='recipe-chat-history^...'
OPENAI_API_KEY=sk-... pnpm --filter recipe-chat-history devThe database file is chat.db in the example folder; set DATABASE_PATH to put it elsewhere. pnpm --filter recipe-chat-history test sends a message through the runtime with a scripted model, mounts the app again, and checks that the thread and its messages load back from an in-memory database. It needs no API key.