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: build the chat search index from a trimmed message view (#16207)

Daniel Han23329d5adeea
diff --git a/studio/backend/routes/chat_history.py b/studio/backend/routes/chat_history.py
index 84ab60562e6..eb965c2fb01 100644
--- a/studio/backend/routes/chat_history.py
+++ b/studio/backend/routes/chat_history.py
@@ -723,6 +723,7 @@ class ChatPinsMigrationRequest(BaseModel):
class ChatMessagesBatchRequest(BaseModel):
threadIds: list[str]
+ view: Literal["full", "search"] = "full"
class ChatMessagesBatchResponse(BaseModel):
@@ -1688,16 +1689,67 @@ def get_thread_messages(thread_id: str, current_subject: str = Depends(get_curre
)
+# Part types extractText (frontend use-chat-search-index.ts) reads; a frontend test guards drift.
+_SEARCH_PART_TYPES = frozenset(("text", "reasoning", "thinking", "tool-call", "source"))
+_PASTED_TEXT_PREFIX = "<pasted_text name="
+
+
+def _search_view_attachment_part(part: Any) -> bool:
+ """Keep what attachmentsPastedText reads; malformed parts stay so the index fails as before."""
+ if not isinstance(part, dict):
+ return True
+ if part.get("type") != "text" or "text" not in part:
+ return False
+ text = part["text"]
+ return not isinstance(text, str) or text.startswith(_PASTED_TEXT_PREFIX)
+
+
+def _search_view_message(message: dict) -> dict:
+ """What the search index reads of a message: no metadata, media or document bodies."""
+ out = {
+ key: message[key]
+ for key in ("id", "threadId", "parentId", "role", "createdAt")
+ if key in message
+ }
+ # Absent stays absent, so ChatMessage fills [] exactly as it does for the full view.
+ if "content" in message:
+ content = message["content"]
+ if isinstance(content, list):
+ content = [
+ part
+ for part in content
+ if isinstance(part, dict)
+ and isinstance(part.get("type"), str)
+ and part["type"] in _SEARCH_PART_TYPES
+ ]
+ out["content"] = content
+ attachments = message.get("attachments")
+ if isinstance(attachments, list):
+ kept = []
+ for attachment in attachments:
+ if not isinstance(attachment, dict) or not isinstance(attachment.get("content"), list):
+ kept.append(attachment)
+ continue
+ parts = [part for part in attachment["content"] if _search_view_attachment_part(part)]
+ if parts:
+ kept.append({"content": parts})
+ attachments = kept
+ if attachments is not None:
+ out["attachments"] = attachments
+ return out
+
+
@router.post("/messages:batch", response_model = ChatMessagesBatchResponse)
def batch_thread_messages(
payload: ChatMessagesBatchRequest, current_subject: str = Depends(get_current_subject)
):
"""One round-trip per sidebar/search rebuild instead of N. Unknown thread ids return empty lists."""
by_thread: dict[str, list[ChatMessage]] = {tid: [] for tid in payload.threadIds}
+ search_view = payload.view == "search"
for m in list_chat_messages_for_threads(payload.threadIds):
tid = m["threadId"]
if tid in by_thread:
- by_thread[tid].append(ChatMessage(**m))
+ by_thread[tid].append(ChatMessage(**(_search_view_message(m) if search_view else m)))
return ChatMessagesBatchResponse(messagesByThreadId = by_thread)
diff --git a/studio/backend/tests/test_chat_search_view.py b/studio/backend/tests/test_chat_search_view.py
new file mode 100644
index 00000000000..5b80dea6859
--- /dev/null
+++ b/studio/backend/tests/test_chat_search_view.py
@@ -0,0 +1,187 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import json
+import os
+import sys
+
+import pytest
+
+_backend = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, _backend)
+
+from routes import chat_history
+from storage import studio_db
+
+PASTED = "<pasted_text name=notes bytes=9>\nPASTEBODY\n</pasted_text>"
+IMAGE = "data:image/png;base64," + "QUJD" * 25_000
+TEXT_PARTS = [
+ {"type": "reasoning", "text": "why"},
+ {"type": "thinking", "thinking": "deep"},
+ {
+ "type": "tool-call",
+ "toolName": "python",
+ "args": {"code": "print(1)"},
+ "result": {"stdout": "1"},
+ },
+ {"type": "source", "title": "Docs", "url": "https://example.com"},
+ {"type": "text", "text": "hello"},
+]
+
+
+def history(tmp_path, monkeypatch):
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))
+ monkeypatch.setenv("UNSLOTH_STUDIO_PROJECTS_HOME", str(tmp_path / "Projects"))
+ monkeypatch.setattr(studio_db, "_schema_ready", set())
+ studio_db.upsert_chat_thread(
+ {
+ "id": "t1",
+ "title": "Chat",
+ "modelType": "base",
+ "modelId": "m",
+ "pairId": None,
+ "archived": False,
+ "createdAt": 1_700_000_000_000,
+ }
+ )
+ messages = [
+ {
+ "id": "u1",
+ "threadId": "t1",
+ "parentId": None,
+ "role": "user",
+ "content": [{"type": "text", "text": "look"}, {"type": "image", "image": IMAGE}],
+ "attachments": [
+ {
+ "id": "a1",
+ "type": "document",
+ "name": "notes.txt",
+ "content": [{"type": "text", "text": PASTED}],
+ },
+ {
+ "id": "a2",
+ "type": "document",
+ "name": "paper.pdf",
+ "content": [{"type": "text", "text": "<attachment name=paper.pdf>\nDOCBODY"}],
+ },
+ {
+ "id": "a3",
+ "type": "image",
+ "name": "shot.png",
+ "content": [{"type": "image", "image": IMAGE}],
+ },
+ ],
+ "createdAt": 1_700_000_000_001,
+ },
+ {
+ "id": "a1",
+ "threadId": "t1",
+ "parentId": "u1",
+ "role": "assistant",
+ "content": TEXT_PARTS,
+ "metadata": {"custom": {"timings": {"predicted_n": 3}}},
+ "createdAt": 1_700_000_000_002,
+ },
+ ]
+ studio_db.sync_chat_messages("t1", messages)
+ return messages
+
+
+def _batch(view = None):
+ payload = {"threadIds": ["t1", "missing"]}
+ if view is not None:
+ payload["view"] = view
+ return chat_history.batch_thread_messages(
+ chat_history.ChatMessagesBatchRequest(**payload), current_subject = "test-user"
+ ).model_dump(mode = "json")["messagesByThreadId"]
+
+
+def test_batch_without_a_view_still_returns_full_messages(history):
+ full = _batch()
+ assert full == _batch("full")
+ assert full["missing"] == []
+ user, assistant = full["t1"]
+ assert user["content"] == history[0]["content"]
+ assert [a["id"] for a in user["attachments"]] == ["a1", "a2", "a3"]
+ assert assistant["metadata"] == history[1]["metadata"]
+ assert assistant["content"] == TEXT_PARTS
+
+
+def test_search_view_keeps_what_the_index_reads_and_drops_the_rest(history):
+ full = _batch()
+ view = _batch("search")
+ assert view["missing"] == []
+ user, assistant = view["t1"]
+ for kept, source in zip(view["t1"], full["t1"]):
+ for key in ("id", "threadId", "parentId", "role", "createdAt"):
+ assert kept[key] == source[key]
+ assert kept["metadata"] is None
+ assert user["content"] == [{"type": "text", "text": "look"}]
+ assert user["attachments"] == [{"content": [{"type": "text", "text": PASTED}]}]
+ assert assistant["content"] == TEXT_PARTS
+
+ full_bytes = len(json.dumps(full))
+ view_bytes = len(json.dumps(view))
+ assert full_bytes > 150_000
+ assert view_bytes < 5_000
+
+
+ "attachments",
+ [
+ [None],
+ [3],
+ ["s"],
+ [{"content": "string"}],
+ [{"content": {"type": "text"}}],
+ [{"content": [None]}],
+ [{"content": [{"type": "text", "text": 5}]}],
+ {"not": "a list"},
+ "a string",
+ ],
+)
+def test_search_view_passes_malformed_attachments_through(attachments):
+ """The index reads these as stored (some make it throw), so the view must not normalise them."""
+ message = {
+ "id": "m",
+ "threadId": "t",
+ "role": "user",
+ "content": [],
+ "attachments": attachments,
+ "createdAt": 1,
+ }
+ assert chat_history._search_view_message(message)["attachments"] == attachments
+
+
+def test_search_view_drops_parts_the_index_skips():
+ content = [
+ None,
+ "str",
+ ["text"],
+ {"type": ["text"], "text": "x"},
+ {"type": "Text", "text": "x"},
+ {"type": "file", "data": "QUJD"},
+ {"type": "audio", "audio": {"data": "QUJD"}},
+ {"type": "text", "text": "kept"},
+ ]
+ message = {"id": "m", "threadId": "t", "role": "user", "content": content, "createdAt": 1}
+ assert chat_history._search_view_message(message)["content"] == [
+ {"type": "text", "text": "kept"}
+ ]
+ for value in ("plain", None, {"type": "text", "text": "x"}):
+ message["content"] = value
+ assert chat_history._search_view_message(message)["content"] == value
+
+
+def test_search_view_leaves_absent_content_to_the_model_default():
+ """Without content the full view serialises [], and the index collapses pasted text only then."""
+ message = {
+ "id": "m",
+ "threadId": "t",
+ "role": "user",
+ "attachments": [{"content": [{"type": "text", "text": PASTED}]}],
+ "createdAt": 1,
+ }
+ view = chat_history.ChatMessage(**chat_history._search_view_message(message))
+ assert view.content == chat_history.ChatMessage(**message).content == []
diff --git a/studio/frontend/src/features/chat/api/chat-api.ts b/studio/frontend/src/features/chat/api/chat-api.ts
index 9575103651e..248ab1a712e 100644
--- a/studio/frontend/src/features/chat/api/chat-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-api.ts
@@ -1349,16 +1349,22 @@ export async function listChatMessages(
return data.messages;
}
-/** Batch fetch; falls back to per-thread listChatMessages on 404/405 from older servers. */
+/**
+ * Batch fetch; falls back to per-thread listChatMessages on 404/405 from older servers.
+ * view "search" trims messages to what the search index reads; older backends ignore it.
+ */
export async function batchListChatMessages(
threadIds: string[],
+ options: { view?: "search" } = {},
): Promise<Map<string, MessageRecord[]>> {
const out = new Map<string, MessageRecord[]>();
if (threadIds.length === 0) return out;
const response = await authFetch("/api/chat/messages:batch", {
method: "POST",
headers: { "content-type": "application/json" },
- body: JSON.stringify({ threadIds }),
+ body: JSON.stringify(
+ options.view ? { threadIds, view: options.view } : { threadIds },
+ ),
});
if (response.status === 404 || response.status === 405) {
const per = await Promise.all(
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 1c370e9231c..9974084946c 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
@@ -26,6 +26,10 @@ import {
forgetChatSearchHasRows,
rememberChatSearchHasRows,
} from "../utils/chat-search-history-hint";
+import {
+ collapseWhitespace,
+ stripBase64Runs,
+} from "../utils/chat-search-text.ts";
import {
formatMcpToolName,
mcpServerFromProvenance,
@@ -59,9 +63,9 @@ function searchableText(value: unknown, depth = 0, toolName?: string): string {
let text = splitMcpImages(value).text;
const cut = text.indexOf("\n__IMAGES__:");
if (cut !== -1) text = text.slice(0, cut);
- return text
- .replace(/data:[^;,\s]+;base64,[A-Za-z0-9+/=]+/g, " ")
- .replace(/[A-Za-z0-9+/]{120,}={0,2}/g, " ");
+ return stripBase64Runs(
+ text.replace(/data:[^;,\s]+;base64,[A-Za-z0-9+/=]+/g, " "),
+ );
}
if (value == null || depth > 4) return "";
if (Array.isArray(value)) {
@@ -81,6 +85,7 @@ function searchableText(value: unknown, depth = 0, toolName?: string): string {
return "";
}
+// The backend's search view (routes/chat_history.py _search_view_message) keeps only what this reads.
function extractText(message: MessageRecord): string {
const content = message.content;
const pasted = attachmentsPastedText(message.attachments);
@@ -128,7 +133,7 @@ function extractText(message: MessageRecord): string {
if (typeof v === "string") parts.push(v);
}
}
- return parts.join(" ").replace(/\s+/g, " ").trim();
+ return collapseWhitespace(parts.join(" ")).trim();
}
interface ChatSearchIndexBuild {
@@ -191,9 +196,9 @@ export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
}
const loadedThreadIds = active.slice(0, THREAD_LIMIT).map((t) => t.id);
- let messagesByThread = await batchListChatMessages(loadedThreadIds).catch(
- () => new Map<string, MessageRecord[]>(),
- );
+ let messagesByThread = await batchListChatMessages(loadedThreadIds, {
+ view: "search",
+ }).catch(() => new Map<string, MessageRecord[]>());
let complete = true;
// Legacy-only chats may predate server import; fill only missing ids via the legacy path.
diff --git a/studio/frontend/src/features/chat/utils/chat-search-text.ts b/studio/frontend/src/features/chat/utils/chat-search-text.ts
new file mode 100644
index 00000000000..bd8dffc5a73
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/chat-search-text.ts
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+const MIN_RUN = 120;
+
+function isBase64Char(c: number): boolean {
+ return (
+ (c >= 97 && c <= 122) ||
+ (c >= 65 && c <= 90) ||
+ (c >= 48 && c <= 57) ||
+ c === 43 ||
+ c === 47
+ );
+}
+
+/** text.replace(/[A-Za-z0-9+/]{120,}={0,2}/g, " ") in one pass; the regex retries inside every word. */
+export function stripBase64Runs(text: string): string {
+ let pieces: string[] | null = null;
+ let last = 0;
+ let i = 0;
+ const n = text.length;
+ while (i < n) {
+ if (!isBase64Char(text.charCodeAt(i))) {
+ i++;
+ continue;
+ }
+ const start = i;
+ while (i < n && isBase64Char(text.charCodeAt(i))) i++;
+ if (i - start < MIN_RUN) continue;
+ for (let pad = 0; pad < 2 && i < n && text.charCodeAt(i) === 61; pad++) i++;
+ pieces ??= [];
+ pieces.push(text.slice(last, start), " ");
+ last = i;
+ }
+ return pieces === null ? text : pieces.join("") + text.slice(last);
+}
+
+// A lone space needs no rewrite, so only runs that change match.
+const WHITESPACE_TO_COLLAPSE = / \s+|[^\S ]\s*/g;
+
+/** Same result as text.replace(/\s+/g, " "), without rewriting every single space. */
+export function collapseWhitespace(text: string): string {
+ return text.replace(WHITESPACE_TO_COLLAPSE, " ");
+}
diff --git a/studio/frontend/tests/chat-search-text.test.ts b/studio/frontend/tests/chat-search-text.test.ts
new file mode 100644
index 00000000000..a4d00722fd3
--- /dev/null
+++ b/studio/frontend/tests/chat-search-text.test.ts
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { register } from "node:module";
+import test from "node:test";
+
+import { installLocalStorageFake } from "./helpers/kit.ts";
+
+register("./chat-search-index-resolver.mjs", import.meta.url);
+installLocalStorageFake();
+
+const { collapseWhitespace, stripBase64Runs } = await import(
+ "../src/features/chat/utils/chat-search-text.ts"
+);
+const { buildChatSearchIndex } = await import(
+ "../src/features/chat/hooks/use-chat-search-index.ts"
+);
+const { chatSearchBatchRequests, configureChatSearchHistoryStub } =
+ await import("./helpers/store-stubs/chat-search-history.ts");
+
+function randomStrings(alphabet: string[], count: number): string[] {
+ let seed = 12345;
+ const next = () => {
+ seed = (seed * 1103515245 + 12345) & 0x7fffffff;
+ return seed;
+ };
+ const out: string[] = [];
+ for (let i = 0; i < count; i++) {
+ let s = "";
+ const len = next() % 400;
+ for (let j = 0; j < len; j++) {
+ // Long runs of one class, so 120+ runs and near misses both occur.
+ const piece = alphabet[next() % alphabet.length];
+ s += next() % 4 === 0 ? piece.repeat(1 + (next() % 130)) : piece;
+ }
+ out.push(s);
+ }
+ return out;
+}
+
+test("stripBase64Runs matches the regex it replaces", () => {
+ const regex = /[A-Za-z0-9+/]{120,}={0,2}/g;
+ const fixed = [
+ "",
+ "a".repeat(119),
+ "a".repeat(120),
+ `x ${"A".repeat(120)}=== y`,
+ `${"b/+".repeat(40)}==`,
+ `${"Q".repeat(200)}=${"Q".repeat(200)}`,
+ `data ${"9".repeat(500)} tail`,
+ "\u00e9".repeat(200),
+ ];
+ const fuzz = randomStrings(
+ ["A", "z", "0", "+", "/", "=", " ", "-", "\u00e9", "\n", "ab", "=="],
+ 3000,
+ );
+ for (const s of [...fixed, ...fuzz]) {
+ assert.equal(stripBase64Runs(s), s.replace(regex, " "), JSON.stringify(s));
+ }
+});
+
+test("collapseWhitespace matches the regex it replaces", () => {
+ const regex = /\s+/g;
+ const fixed = [
+ "",
+ " ",
+ " ",
+ " a ",
+ "a\tb",
+ "a \n b",
+ "\u00a0",
+ " \ufeff",
+ "x\u3000 y",
+ "\u2028",
+ ];
+ const fuzz = randomStrings(
+ [" ", "\t", "\n", "\r", "\u00a0", "\u2028", "\ufeff", "\u3000", "a", "bc"],
+ 3000,
+ );
+ for (const s of [...fixed, ...fuzz]) {
+ assert.equal(
+ collapseWhitespace(s),
+ s.replace(regex, " "),
+ JSON.stringify(s),
+ );
+ }
+});
+
+test("the search index asks the backend for the search view", async () => {
+ configureChatSearchHistoryStub({
+ threads: [{ id: "t1", title: "Acme", createdAt: 1 }],
+ messagesByThread: new Map([
+ [
+ "t1",
+ [
+ {
+ id: "m1",
+ threadId: "t1",
+ role: "user",
+ content: [{ type: "text", text: "Roadmap notes" }],
+ createdAt: 2,
+ },
+ ],
+ ],
+ ]),
+ });
+ const build = await buildChatSearchIndex();
+ assert.deepEqual(chatSearchBatchRequests(), [
+ { threadIds: ["t1"], options: { view: "search" } },
+ ]);
+ assert.equal(build.items[0].searchText, "acme roadmap notes");
+});
+
+test("the backend search view keeps every part type the index reads", () => {
+ const hook = readFileSync(
+ new URL(
+ "../src/features/chat/hooks/use-chat-search-index.ts",
+ import.meta.url,
+ ),
+ "utf8",
+ );
+ const start = hook.indexOf("function extractText(");
+ const body = hook.slice(start, hook.indexOf("\n}\n", start));
+ const read = new Set(
+ [...body.matchAll(/p\.type === "([^"]+)"/g)].map((m) => m[1]),
+ );
+ const route = readFileSync(
+ new URL("../../backend/routes/chat_history.py", import.meta.url),
+ "utf8",
+ );
+ const kept = /_SEARCH_PART_TYPES = frozenset\(\(([^)]*)\)\)/.exec(route)?.[1];
+ assert.ok(kept, "routes/chat_history.py must define _SEARCH_PART_TYPES");
+ const keptTypes = new Set([...kept.matchAll(/"([^"]+)"/g)].map((m) => m[1]));
+ assert.deepEqual([...keptTypes].sort(), [...read].sort());
+});
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 19c4722e216..c3eec02429b 100644
--- a/studio/frontend/tests/helpers/store-stubs/chat-search-history.ts
+++ b/studio/frontend/tests/helpers/store-stubs/chat-search-history.ts
@@ -18,9 +18,13 @@ export function listStoredChatMessages(threadId: string): Promise<unknown[]> {
return Promise.resolve(messagesByThread.get(threadId) ?? []);
}
+let batchRequests: { threadIds: string[]; options?: unknown }[] = [];
+
export function batchListChatMessages(
threadIds: string[],
+ options?: unknown,
): Promise<Map<string, unknown[]>> {
+ batchRequests.push({ threadIds, options });
if (batchFails) {
return Promise.reject(new Error("batch read failed"));
}
@@ -36,6 +40,7 @@ export function configureChatSearchHistoryStub(options: {
messageReadsFail?: boolean;
messageReadFailures?: Set<string>;
}): void {
+ batchRequests = [];
threads = options.threads ?? [];
messagesByThread = options.messagesByThread ?? new Map();
batchFails = options.batchFails ?? false;
@@ -43,6 +48,13 @@ export function configureChatSearchHistoryStub(options: {
messageReadFailures = options.messageReadFailures ?? new Set();
}
+export function chatSearchBatchRequests(): {
+ threadIds: string[];
+ options?: unknown;
+}[] {
+ return batchRequests;
+}
+
export const CHAT_HISTORY_UPDATED_EVENT = "unsloth-chat-history-updated";
// Must match the real chat-api constant: the cross-tab listener keys off it.
export const CHAT_HISTORY_REVISION_KEY = "unsloth_chat_history_revision";
diff --git a/studio/frontend/tests/mcp-app-envelope.test.ts b/studio/frontend/tests/mcp-app-envelope.test.ts
index a46ef910d7a..309c886d27d 100644
--- a/studio/frontend/tests/mcp-app-envelope.test.ts
+++ b/studio/frontend/tests/mcp-app-envelope.test.ts
@@ -16,6 +16,7 @@ import {
toolApprovalScope,
toolResultParams,
} from "../src/features/chat/mcp-apps/mcp-ui.ts";
+import { stripBase64Runs } from "../src/features/chat/utils/chat-search-text.ts";
import { splitMcpToolName } from "../src/features/chat/utils/mcp-tool-name.ts";
const read = (path: string) =>
@@ -170,8 +171,9 @@ test("the search index keeps a widget's shown text, not its seed", () => {
const searchableText = new Function(
"splitMcpImages",
"isMcpUiToolResult",
+ "stripBase64Runs",
`${js}; return searchableText;`,
- )(splitMcpImages, isMcpUiToolResult) as (
+ )(splitMcpImages, isMcpUiToolResult, stripBase64Runs) as (
value: unknown,
depth?: number,
toolName?: string,