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.

Unsloth Studio: keep chat search fast on long histories by not shipping tool images and widget seeds to the index (#16848)

Leo Borcherding5cc85726128c
diff --git a/studio/backend/routes/chat_history.py b/studio/backend/routes/chat_history.py
index eb965c2fb01..2000f7ab8bc 100644
--- a/studio/backend/routes/chat_history.py
+++ b/studio/backend/routes/chat_history.py
@@ -4,6 +4,7 @@
"""Chat history API; sync SQLite handlers, except mixed ones that use Starlette's threadpool."""
import asyncio
+import json
import os
import re
import sqlite3
@@ -1704,6 +1705,110 @@ def _search_view_attachment_part(part: Any) -> bool:
return not isinstance(text, str) or text.startswith(_PASTED_TEXT_PREFIX)
+# Mirrors searchableText: tool output keeps only text it would index, so images and widget seeds stay home.
+# ASCII: JavaScript's /i never folds a non-ASCII letter such as "ı" onto an ASCII one.
+_SEARCH_SKIPPED_KEY = re.compile(r"b64|base64|^(?:images?|audio|video)\Z", re.IGNORECASE | re.ASCII)
+_MCP_IMAGES_MARKER = "\n__MCP_IMAGES__:"
+_IMAGES_MARKER = "\n__IMAGES__:"
+
+
+def _reject_json_constant(name: str) -> Any:
+ raise ValueError(name)
+
+
+def _split_mcp_images(text: str) -> str:
+ """splitMcpImages: only a valid image envelope after the last marker is cut."""
+ at = text.rfind(_MCP_IMAGES_MARKER)
+ if at == -1:
+ return text
+ try:
+ # parse_int: JSON.parse takes integers past Python's digit limit.
+ images = json.loads(
+ text[at + len(_MCP_IMAGES_MARKER) :],
+ parse_constant = _reject_json_constant,
+ parse_int = float,
+ )
+ except ValueError:
+ return text
+ valid = (
+ isinstance(images, list)
+ and len(images) > 0
+ and all(
+ isinstance(image, dict)
+ and isinstance(image.get("data"), str)
+ and isinstance(image.get("mimeType"), str)
+ for image in images
+ )
+ )
+ return text[:at] if valid else text
+
+
+# JavaScript's \s, which differs from Python's (no \x1c-\x1f or \x85, adds \ufeff).
+_JS_SPACE = "\t\n\v\f\r \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"
+_DATA_URL = re.compile(f"data:[^;,{_JS_SPACE}]+;base64,[A-Za-z0-9+/=]+")
+
+
+def _searchable_string(text: str) -> str:
+ """searchableText on a string, minus stripBase64Runs, which is far slower in Python."""
+ text = _split_mcp_images(text)
+ at = text.find(_IMAGES_MARKER)
+ if at != -1:
+ text = text[:at]
+ return _DATA_URL.sub(" ", text)
+
+
+def _search_view_string(text: str) -> str:
+ try:
+ cut = _searchable_string(text)
+ # Sent only when the browser would read it back unchanged, else the original.
+ stable = _searchable_string(cut) == cut
+ except RecursionError:
+ # JSON.parse may still read nesting this deep, so leave the string to the browser.
+ return text
+ return cut if stable else text
+
+
+def _search_view_value(
+ value: Any,
+ depth: int = 0,
+ tool_name: Any = None,
+) -> Any:
+ if isinstance(value, str):
+ return _search_view_string(value)
+ if value is None or depth > 4:
+ return None
+ if isinstance(value, list):
+ return [_search_view_value(v, depth + 1) for v in value]
+ if isinstance(value, dict):
+ ui = value.get("ui")
+ if (
+ depth == 0
+ and isinstance(tool_name, str)
+ and tool_name.startswith("mcp__")
+ and isinstance(value.get("text"), str)
+ and isinstance(ui, dict)
+ and isinstance(ui.get("resourceUri"), str)
+ ):
+ return {"text": _search_view_string(value["text"])}
+ return {
+ k: _search_view_value(v, depth + 1)
+ for k, v in value.items()
+ if not _SEARCH_SKIPPED_KEY.search(k)
+ }
+ return value
+
+
+def _search_view_tool_call(part: dict) -> dict:
+ out = {key: part[key] for key in ("type", "toolName", "provenance") if key in part}
+ if isinstance(part.get("argsText"), str):
+ out["argsText"] = _search_view_string(part["argsText"])
+ elif "args" in part:
+ out["args"] = _search_view_value(part["args"])
+ if "result" in part:
+ out["result"] = _search_view_value(part["result"], 0, part.get("toolName"))
+ return out
+
+
def _search_view_message(message: dict) -> dict:
"""What the search index reads of a message: no metadata, media or document bodies."""
out = {
@@ -1716,7 +1821,7 @@ def _search_view_message(message: dict) -> dict:
content = message["content"]
if isinstance(content, list):
content = [
- part
+ _search_view_tool_call(part) if part["type"] == "tool-call" else part
for part in content
if isinstance(part, dict)
and isinstance(part.get("type"), str)
diff --git a/studio/backend/tests/test_chat_search_view.py b/studio/backend/tests/test_chat_search_view.py
index 5b80dea6859..234305ee351 100644
--- a/studio/backend/tests/test_chat_search_view.py
+++ b/studio/backend/tests/test_chat_search_view.py
@@ -185,3 +185,95 @@ def test_search_view_leaves_absent_content_to_the_model_default():
}
view = chat_history.ChatMessage(**chat_history._search_view_message(message))
assert view.content == chat_history.ChatMessage(**message).content == []
+
+
+def _tool_call(**part):
+ message = {
+ "id": "m",
+ "threadId": "t",
+ "role": "assistant",
+ "content": [{"type": "tool-call", "toolCallId": "c", **part}],
+ "createdAt": 1,
+ }
+ return chat_history._search_view_message(message)["content"][0]
+
+
+def test_search_view_drops_tool_output_the_index_skips():
+ envelope = json.dumps([{"data": "QUJD" * 1000, "mimeType": "image/png"}])
+ # JSON.parse reads an integer past Python's 4300 digit limit, so this envelope is valid too.
+ big = envelope[:-2] + ', "n": ' + "1" * 5000 + "}]"
+ part = _tool_call(
+ toolName = "python",
+ args = {"code": "plot()", "image_b64": "QUJD"},
+ argsText = '{"code": "plot()"}',
+ result = {
+ "stdout": "done\n__IMAGES__:" + "QUJD" * 1000,
+ "images": [IMAGE],
+ "nested": {
+ "audio": "QUJD",
+ "log": "kept\n__MCP_IMAGES__:" + envelope,
+ "big": "kept\n__MCP_IMAGES__:" + big,
+ "\u0131mage": "dotless i is no binary key to JavaScript",
+ },
+ "deep": [[[[[{"gone": "too deep to index"}]]]]],
+ },
+ )
+ assert part == {
+ "type": "tool-call",
+ "toolName": "python",
+ "argsText": '{"code": "plot()"}',
+ "result": {
+ "stdout": "done",
+ "nested": {
+ "log": "kept",
+ "big": "kept",
+ "\u0131mage": "dotless i is no binary key to JavaScript",
+ },
+ "deep": [[[[None]]]],
+ },
+ }
+
+
+def test_search_view_keeps_only_the_text_of_a_widget_result():
+ ui = {"resourceUri": "ui://w", "content": [{"type": "text", "data": "<div>" * 10_000}]}
+ widget = _tool_call(toolName = "mcp__w__show", result = {"text": "shown", "ui": ui})
+ assert widget["result"] == {"text": "shown"}
+ # Not a widget to the index without the mcp__ name, so every key is read.
+ plain = _tool_call(toolName = "show", result = {"text": "shown", "ui": ui})
+ assert plain["result"] == {"text": "shown", "ui": ui}
+
+
+def test_search_view_strips_data_urls_the_index_drops():
+ run = "QUJD" * 40
+ text = f"plot data:image/png;base64,{run}== saved {run}= ok"
+ # The bare run is the browser's to strip.
+ assert _tool_call(toolName = "python", result = {"stdout": text})["result"] == {
+ "stdout": f"plot saved {run}= ok"
+ }
+ # JavaScript's \s has U+FEFF and not U+001C, so only the second ends the media type.
+ odd = f"data:a\x1cb;base64,QQ data:a\ufeffb;base64,QQ"
+ assert _tool_call(toolName = "python", result = odd)["result"] == " data:a\ufeffb;base64,QQ"
+
+
+ "text",
+ [
+ "no marker",
+ "text\n__MCP_IMAGES__:not json",
+ "text\n__MCP_IMAGES__:[]",
+ 'text\n__MCP_IMAGES__:[{"data": "", "mimeType": "", "x": NaN}]',
+ pytest.param("text\n__MCP_IMAGES__:" + "[" * 100_000, id = "too-deep-for-python"),
+ pytest.param(
+ 'data:a;base64,QQ\n__MCP_IMAGES__:[{"data": "", "mimeType": "", "x": '
+ + "[" * 100_000
+ + "]" * 100_000
+ + "}]",
+ id = "valid-but-too-deep-for-python",
+ ),
+ 'text\n__MCP_IMAGES__:[{"data": 1, "mimeType": "x"}]',
+ # Cutting the last envelope would expose an earlier valid one the browser never strips.
+ 'a\n__MCP_IMAGES__:[{"data": "", "mimeType": ""}]\n__MCP_IMAGES__:[{"data": "", "mimeType": ""}]',
+ ],
+)
+def test_search_view_leaves_strings_the_browser_would_read_differently(text):
+ assert _tool_call(toolName = "mcp__x", result = text)["result"] == text
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 9974084946c..2637a941923 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
@@ -52,6 +52,7 @@ export interface ChatSearchItem {
// Messages are indexed for the newest threads only; older chats match by title.
const THREAD_LIMIT = 200;
+const THREADS_PER_BATCH = 20;
const SEARCH_REBUILD_DEBOUNCE_MS = 300;
// Past the dialog's 180ms exit, so releasing rows never lands mid-animation.
const ROW_RELEASE_DELAY_MS = 300;
@@ -196,9 +197,23 @@ export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
}
const loadedThreadIds = active.slice(0, THREAD_LIMIT).map((t) => t.id);
- let messagesByThread = await batchListChatMessages(loadedThreadIds, {
- view: "search",
- }).catch(() => new Map<string, MessageRecord[]>());
+ // One response for every thread can pass V8's string cap and then fails whole.
+ const chunks: string[][] = [];
+ for (let i = 0; i < loadedThreadIds.length; i += THREADS_PER_BATCH) {
+ chunks.push(loadedThreadIds.slice(i, i + THREADS_PER_BATCH));
+ }
+ let messagesByThread = new Map<string, MessageRecord[]>();
+ for (const batch of await Promise.all(
+ chunks.map((ids) =>
+ batchListChatMessages(ids, { view: "search" }).catch(
+ () => new Map<string, MessageRecord[]>(),
+ ),
+ ),
+ )) {
+ for (const [threadId, messages] of batch) {
+ messagesByThread.set(threadId, messages);
+ }
+ }
let complete = true;
// Legacy-only chats may predate server import; fill only missing ids via the legacy path.
diff --git a/studio/frontend/tests/chat-search-text.test.ts b/studio/frontend/tests/chat-search-text.test.ts
index a4d00722fd3..beaf394011b 100644
--- a/studio/frontend/tests/chat-search-text.test.ts
+++ b/studio/frontend/tests/chat-search-text.test.ts
@@ -113,6 +113,63 @@ test("the search index asks the backend for the search view", async () => {
assert.equal(build.items[0].searchText, "acme roadmap notes");
});
+test("the search index reads messages a few threads per request", async () => {
+ const threads = Array.from({ length: 45 }, (_, i) => ({
+ id: `t${i}`,
+ title: `Chat ${i}`,
+ createdAt: 45 - i,
+ }));
+ // Per-thread reads fail, so a batch result that is dropped leaves the build incomplete.
+ configureChatSearchHistoryStub({
+ threads,
+ messagesByThread: new Map(
+ threads.map((t) => [
+ t.id,
+ [
+ {
+ id: `${t.id}m`,
+ threadId: t.id,
+ role: "user",
+ content: [{ type: "text", text: `note ${t.id}` }],
+ createdAt: 1,
+ },
+ ],
+ ]),
+ ),
+ messageReadsFail: true,
+ });
+ const build = await buildChatSearchIndex();
+ const requests = chatSearchBatchRequests();
+ assert.deepEqual(
+ requests.map((r) => r.threadIds.length),
+ [20, 20, 5],
+ );
+ assert.deepEqual(
+ requests.flatMap((r) => r.threadIds),
+ threads.map((t) => t.id),
+ );
+ assert.ok(requests.every((r) => (r.options as { view?: string }).view === "search"));
+ assert.equal(build.complete, true);
+ assert.equal(build.items.length, 45);
+ assert.ok(build.items.every((i) => i.searchText.endsWith(`note ${i.id}`)));
+});
+
+test("a failed chunk falls back without dropping the others", async () => {
+ const threads = Array.from({ length: 45 }, (_, i) => ({
+ id: `t${i}`,
+ title: `Chat ${i}`,
+ createdAt: 45 - i,
+ }));
+ configureChatSearchHistoryStub({
+ threads,
+ batchFailures: new Set(["t0"]),
+ // Only the failed chunk may fall back to per-thread reads.
+ messageReadFailures: new Set(threads.slice(20).map((t) => t.id)),
+ });
+ const build = await buildChatSearchIndex();
+ assert.equal(build.complete, true);
+});
+
test("the backend search view keeps every part type the index reads", () => {
const hook = readFileSync(
new URL(
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 c3eec02429b..9d6b1c1adcb 100644
--- a/studio/frontend/tests/helpers/store-stubs/chat-search-history.ts
+++ b/studio/frontend/tests/helpers/store-stubs/chat-search-history.ts
@@ -4,6 +4,7 @@
let threads: unknown[] = [];
let messagesByThread = new Map<string, unknown[]>();
let batchFails = false;
+let batchFailures = new Set<string>();
let messageReadsFail = false;
let messageReadFailures = new Set<string>();
@@ -25,7 +26,7 @@ export function batchListChatMessages(
options?: unknown,
): Promise<Map<string, unknown[]>> {
batchRequests.push({ threadIds, options });
- if (batchFails) {
+ if (batchFails || threadIds.some((id) => batchFailures.has(id))) {
return Promise.reject(new Error("batch read failed"));
}
return Promise.resolve(
@@ -37,6 +38,8 @@ export function configureChatSearchHistoryStub(options: {
threads?: unknown[];
messagesByThread?: Map<string, unknown[]>;
batchFails?: boolean;
+ // A batch holding any of these threads fails.
+ batchFailures?: Set<string>;
messageReadsFail?: boolean;
messageReadFailures?: Set<string>;
}): void {
@@ -44,6 +47,7 @@ export function configureChatSearchHistoryStub(options: {
threads = options.threads ?? [];
messagesByThread = options.messagesByThread ?? new Map();
batchFails = options.batchFails ?? false;
+ batchFailures = options.batchFailures ?? new Set();
messageReadsFail = options.messageReadsFail ?? false;
messageReadFailures = options.messageReadFailures ?? new Set();
}