Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: find older chats by title in chat search (#11296)

Nilaybd079329a352
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-search-index.ts b/studio/frontend/src/features/chat/hooks/use-chat-search-index.ts
index 57a0fe2ec52..b9c4a039020 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-search-index.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-search-index.ts
@@ -45,6 +45,7 @@ export interface ChatSearchItem {
projectId?: string | null;
}
+// Messages are indexed for this many most recently updated threads; older chats match by title.
const THREAD_LIMIT = 200;
const SEARCH_REBUILD_DEBOUNCE_MS = 300;
// Past the dialog's 180ms exit, so releasing uncached rows never lands mid-animation.
@@ -135,9 +136,7 @@ interface ChatSearchIndexBuild {
// Exported for the bare-node cache harness: it must prove a failed read is not
// indistinguishable from a completed empty history.
export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
- const active = (
- await listStoredChatThreads({ includeArchived: false })
- ).slice(0, THREAD_LIMIT);
+ const active = await listStoredChatThreads({ includeArchived: false });
const itemThreadIds = new Map<
string,
@@ -180,17 +179,15 @@ export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
}
}
- const allThreadIds = Array.from(itemThreadIds.values()).flatMap(
- (e) => e.threadIds,
- );
- let messagesByThread = await batchListChatMessages(allThreadIds).catch(
+ const loadedThreadIds = active.slice(0, THREAD_LIMIT).map((t) => t.id);
+ let messagesByThread = await batchListChatMessages(loadedThreadIds).catch(
() => new Map<string, MessageRecord[]>(),
);
let complete = true;
// Legacy-only chats can exist before server-side history import finishes. Fill only the
// missing ids via the legacy path instead of one request per thread up front.
- const missingThreadIds = allThreadIds.filter(
+ const missingThreadIds = loadedThreadIds.filter(
(threadId) => !messagesByThread.has(threadId),
);
if (missingThreadIds.length > 0) {
@@ -219,7 +216,11 @@ export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
const arr = messagesByThread.get(tid);
if (arr) merged.push(...arr);
}
- if (merged.length === 0) {
+ // A chat read as empty is skipped; one whose messages were never loaded keeps its title row.
+ if (
+ merged.length === 0 &&
+ threadIds.every((tid) => messagesByThread.has(tid))
+ ) {
continue;
}
merged.sort((a, b) => b.createdAt - a.createdAt);
@@ -243,8 +244,9 @@ export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
return { items: results, complete };
}
-// THREAD_LIMIT bounds rows, not bytes: a tool-heavy history would otherwise hold tens of
-// megabytes behind a closed dialog. Past this the index is rebuilt on each open.
+// THREAD_LIMIT bounds threads with indexed messages, not bytes: a tool-heavy history would
+// otherwise hold tens of megabytes behind a closed dialog. Past this the index is rebuilt on
+// each open.
const MAX_CACHED_SEARCH_TEXT_CHARS = 4_000_000;
// Last built index, kept across opens so reopening paints the previous rows at once and
diff --git a/studio/frontend/tests/chat-search-index-hint.test.ts b/studio/frontend/tests/chat-search-index-hint.test.ts
index e85cfb895f4..18699975196 100644
--- a/studio/frontend/tests/chat-search-index-hint.test.ts
+++ b/studio/frontend/tests/chat-search-index-hint.test.ts
@@ -467,3 +467,54 @@ test("partial builds replace a stale empty hint with a rows answer", async () =>
);
configureChatSearchHistoryStub({});
});
+
+test("a chat past the message bound is still found by its title", async () => {
+ store.clear();
+ setAuthSessionEpochForTest(0);
+ const total = 201;
+ const threads = Array.from({ length: total }, (_, i) => ({
+ id: `thread-${i}`,
+ title: i === total - 1 ? "Zanzibar ledger" : `Chat ${i}`,
+ modelType: "text",
+ archived: false,
+ createdAt: total - i,
+ }));
+ configureChatSearchHistoryStub({
+ threads,
+ messagesByThread: new Map(
+ threads.map((thread, i) => [
+ thread.id,
+ [
+ {
+ id: `message-${i}`,
+ threadId: thread.id,
+ role: "user",
+ content: [{ type: "text", text: `body ${i} quokka` }],
+ createdAt: thread.createdAt,
+ },
+ ],
+ ]),
+ ),
+ });
+
+ const build = await buildChatSearchIndex();
+ assert.equal(build.complete, true);
+ assert.equal(build.items.length, total);
+ const oldest = build.items.find((item) => item.id === `thread-${total - 1}`);
+ assert.ok(oldest, "the oldest chat must not drop out of the index");
+ assert.equal(oldest.userSearchText, "zanzibar ledger");
+ assert.equal(
+ oldest.searchText,
+ "zanzibar ledger",
+ "its messages stay unloaded",
+ );
+ for (const i of [0, total - 2]) {
+ assert.ok(
+ build.items
+ .find((item) => item.id === `thread-${i}`)
+ ?.searchText.includes(`body ${i} quokka`),
+ `chat ${i}, inside the bound, keeps its message text`,
+ );
+ }
+ configureChatSearchHistoryStub({});
+});
diff --git a/studio/frontend/tests/helpers/store-stubs/chat-search-history.ts b/studio/frontend/tests/helpers/store-stubs/chat-search-history.ts
index 72194eab39a..19c4722e216 100644
--- a/studio/frontend/tests/helpers/store-stubs/chat-search-history.ts
+++ b/studio/frontend/tests/helpers/store-stubs/chat-search-history.ts
@@ -18,11 +18,15 @@ export function listStoredChatMessages(threadId: string): Promise<unknown[]> {
return Promise.resolve(messagesByThread.get(threadId) ?? []);
}
-export function batchListChatMessages(): Promise<Map<string, unknown[]>> {
+export function batchListChatMessages(
+ threadIds: string[],
+): Promise<Map<string, unknown[]>> {
if (batchFails) {
return Promise.reject(new Error("batch read failed"));
}
- return Promise.resolve(new Map(messagesByThread));
+ return Promise.resolve(
+ new Map(threadIds.map((id) => [id, messagesByThread.get(id) ?? []])),
+ );
}
export function configureChatSearchHistoryStub(options: {