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: show the retrieved passages behind a document search (#13987)

Jonathan Ye9185659dddae
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.ts
index 10f934bce86..c859d8ac6c6 100644
--- a/studio/frontend/scripts/check-bundle-budget.ts
+++ b/studio/frontend/scripts/check-bundle-budget.ts
@@ -310,8 +310,11 @@ export const BUDGET = {
// The Rosetta chat-only messages (#14374) in the eager sidebar and hardware verdict add 298 B raw / 127 B
// transfer (6,859,194 -> 6,859,492 B raw, 2,065,328 -> 2,065,455 B transfer against 34c5203ebb, one machine and
// build, Node 24, 96 chunks). Raised to that build plus the same 160 B raw / 189 B transfer margin.
- transferBytes: 2_065_644,
- rawBytes: 6_859_652,
+ // The retrieved passage viewer (#13987) in the eager chat UI adds 3,132 B raw / 939 B transfer (6,859,492 ->
+ // 6,862,624 B raw, 2,065,455 -> 2,066,394 B transfer against 98010f6eee, one machine and build, 96 chunks).
+ // Raised to that build plus the same 160 B raw / 189 B transfer margin.
+ transferBytes: 2_066_583,
+ rawBytes: 6_862_784,
};
// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering the
diff --git a/studio/frontend/src/components/assistant-ui/citation-utils.ts b/studio/frontend/src/components/assistant-ui/citation-utils.ts
index cd76fca97ce..12832f0fc8f 100644
--- a/studio/frontend/src/components/assistant-ui/citation-utils.ts
+++ b/studio/frontend/src/components/assistant-ui/citation-utils.ts
@@ -152,6 +152,88 @@ export function citedPagesPerDocument(
);
}
+/** An empty chunk would render as a blank card, so it is not a passage. */
+export function isPassage(citation: Citation): boolean {
+ return citation.text.trim().length > 0;
+}
+
+/** number: 1-based position in the search result, the `<chunk id>` the model was given. */
+export interface Passage {
+ citation: Citation;
+ number: number;
+}
+
+/**
+ * The first `limit` passages in the order the model received them. An empty chunk is skipped but keeps its
+ * number, so every passage is numbered as the model saw it, merged whole-document and project reads included.
+ */
+export function orderedPassages(
+ citations: readonly Citation[],
+ limit = Number.POSITIVE_INFINITY,
+): Passage[] {
+ const passages: Passage[] = [];
+ for (const [i, citation] of citations.entries()) {
+ if (passages.length >= limit) break;
+ if (isPassage(citation)) passages.push({ citation, number: i + 1 });
+ }
+ return passages;
+}
+
+const FENCE = /^[ \t]*```/;
+const FENCE_WITH_LANGUAGE = /^[ \t]*```\S/;
+const HEADING_MARKER = /^#{1,6}[ \t]+/;
+const PDF_FILENAME = /\.pdf$/i;
+
+/**
+ * Which lines sit inside a ``` block. pymupdf4llm puts code on the line after an opening fence and a blank line
+ * after a closing one, so a closing fence seen first means the chunk began inside a block.
+ */
+function fencedLines(lines: readonly string[]): boolean[] {
+ const fenced = lines.map(() => false);
+ let inside = false;
+ let seenFence = false;
+ for (const [i, line] of lines.entries()) {
+ if (!FENCE.test(line)) {
+ fenced[i] = inside;
+ continue;
+ }
+ const opens =
+ FENCE_WITH_LANGUAGE.test(line) || (lines[i + 1] ?? "").trim() !== "";
+ if (!(opens || seenFence)) {
+ fenced.fill(true, 0, i);
+ }
+ inside = opens;
+ seenFence = true;
+ }
+ return fenced;
+}
+
+/**
+ * Only PDFs drop `#` markers (added by their Markdown conversion), outside code blocks; code and config keep their
+ * `#` comments.
+ */
+export function passageDisplayText(text: string, filename = ""): string {
+ let lines = text.trim().split("\n");
+ if (PDF_FILENAME.test(filename)) {
+ const fenced = fencedLines(lines);
+ lines = lines.map((line, i) =>
+ fenced[i] ? line : line.replace(HEADING_MARKER, ""),
+ );
+ }
+ return lines.join("\n").replace(/\n(?:[ \t]*\n)+/g, "\n");
+}
+
+// About four lines of text-xs in the desktop chat column, so shorter passages are not clamped for nothing.
+const PASSAGE_CLAMP_CHARS = 450;
+const PASSAGE_CLAMP_LINES = 4;
+
+export function passageNeedsClamp(text: string): boolean {
+ return (
+ text.length > PASSAGE_CLAMP_CHARS ||
+ text.split("\n").length > PASSAGE_CLAMP_LINES
+ );
+}
+
/** "p.3", "p.1–5" or "pp.1–3, 7": runs of consecutive pages collapse into ranges. */
export function pageLabel(pages: readonly number[]): string {
const runs: string[] = [];
diff --git a/studio/frontend/src/components/assistant-ui/knowledge-base-passages.tsx b/studio/frontend/src/components/assistant-ui/knowledge-base-passages.tsx
new file mode 100644
index 00000000000..be2173e4943
--- /dev/null
+++ b/studio/frontend/src/components/assistant-ui/knowledge-base-passages.tsx
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"use client";
+
+import { attachmentFileKind } from "@/features/chat";
+import { RagDocumentTrigger } from "@/features/rag";
+import { FileGlyph } from "@/lib/file-icon";
+import { useMemo, useState } from "react";
+import {
+ type Citation,
+ type Passage,
+ documentKey,
+ isPassage,
+ orderedPassages,
+ passageDisplayText,
+ passageNeedsClamp,
+} from "./citation-utils";
+
+// Same ceiling as the document chips: a model-chosen top_k would otherwise mount every passage.
+const PASSAGES_SHOWN = 20;
+
+function PassageItem({
+ passage,
+ showFilename,
+}: {
+ passage: Passage;
+ /** Off when the search drew on one document: its chip already names it. */
+ showFilename: boolean;
+}) {
+ const { citation, number } = passage;
+ const text = passageDisplayText(citation.text, citation.filename);
+ const clampable = passageNeedsClamp(text);
+ // Code and config read by their indentation and alignment, which a proportional font loses.
+ const code = attachmentFileKind(citation.filename, undefined) === "code";
+ const [expanded, setExpanded] = useState(false);
+ const location = citation.page != null ? `p.${citation.page}` : null;
+
+ return (
+ <li
+ data-slot="knowledge-base-passage"
+ className="flex flex-col gap-1 rounded-md bg-muted/50 px-2.5 py-2"
+ >
+ <div className="flex min-w-0 items-center gap-2 text-xs">
+ <span
+ className="shrink-0 tabular-nums"
+ title="Its number in the search result the model was given"
+ >
+ #{number}
+ </span>
+ {showFilename ? (
+ <span className="flex min-w-0 items-center gap-1 font-medium text-foreground">
+ <FileGlyph className="size-3 shrink-0" />
+ <span className="truncate" title={citation.filename}>
+ {citation.filename}
+ </span>
+ </span>
+ ) : null}
+ {citation.documentId ? (
+ <RagDocumentTrigger
+ documentId={citation.documentId}
+ filename={citation.filename}
+ page={citation.page ?? undefined}
+ >
+ <button
+ type="button"
+ className="shrink-0 underline-offset-2 hover:text-foreground hover:underline"
+ >
+ {location ? `Open at ${location}` : "Open document"}
+ </button>
+ </RagDocumentTrigger>
+ ) : location ? (
+ <span className="shrink-0">{location}</span>
+ ) : null}
+ </div>
+ <p
+ className={`whitespace-pre-wrap break-words text-xs leading-relaxed text-foreground/90 ${
+ code ? "font-mono" : ""
+ } ${clampable && !expanded ? "line-clamp-4" : ""}`}
+ >
+ {text}
+ </p>
+ {clampable ? (
+ <button
+ type="button"
+ aria-expanded={expanded}
+ onClick={() => setExpanded((v) => !v)}
+ className="self-start text-xs underline-offset-2 hover:text-foreground hover:underline"
+ >
+ {expanded ? "Show less" : "Show more"}
+ </button>
+ ) : null}
+ </li>
+ );
+}
+
+export function KnowledgeBasePassages({
+ citations,
+}: {
+ citations: readonly Citation[];
+}) {
+ const total = useMemo(() => citations.filter(isPassage).length, [citations]);
+ const [showAll, setShowAll] = useState(false);
+ const shown = useMemo(
+ () =>
+ orderedPassages(
+ citations,
+ showAll ? Number.POSITIVE_INFINITY : PASSAGES_SHOWN,
+ ),
+ [citations, showAll],
+ );
+ const showFilename = useMemo(
+ () => new Set(citations.filter(isPassage).map(documentKey)).size > 1,
+ [citations],
+ );
+ const shownCount = shown.length;
+
+ return (
+ <div data-slot="knowledge-base-passages" className="flex flex-col gap-1.5">
+ <ol className="flex flex-col gap-1.5">
+ {shown.map((passage) => (
+ <PassageItem
+ key={passage.citation.id}
+ passage={passage}
+ showFilename={showFilename}
+ />
+ ))}
+ </ol>
+ {shownCount < total ? (
+ <button
+ type="button"
+ onClick={() => setShowAll(true)}
+ className="self-start text-xs underline-offset-2 hover:text-foreground hover:underline"
+ >
+ Show {total - shownCount} more passage
+ {total - shownCount === 1 ? "" : "s"}
+ </button>
+ ) : null}
+ </div>
+ );
+}
diff --git a/studio/frontend/src/components/assistant-ui/tool-ui-knowledge-base.tsx b/studio/frontend/src/components/assistant-ui/tool-ui-knowledge-base.tsx
index 78890122163..369de55a202 100644
--- a/studio/frontend/src/components/assistant-ui/tool-ui-knowledge-base.tsx
+++ b/studio/frontend/src/components/assistant-ui/tool-ui-knowledge-base.tsx
@@ -13,7 +13,7 @@ import { HugeiconsIcon } from "@hugeicons/react";
import { Spinner } from "@/components/ui/spinner";
import { useToolAwaitingApproval } from "@/features/chat";
-import { RagDocumentTrigger } from "@/features/rag/components/rag-document-trigger";
+import { RagDocumentTrigger } from "@/features/rag";
import { FileGlyph } from "@/lib/file-icon";
import { stringifyToolResult } from "@/lib/strip-ansi";
import { type ComponentProps, memo, useMemo, useState } from "react";
@@ -36,8 +36,10 @@ import {
citationLabel,
citedPagesPerDocument,
documentKey,
+ isPassage,
topCitationPerDocument,
} from "./citation-utils";
+import { KnowledgeBasePassages } from "./knowledge-base-passages";
import { ScrollPane } from "./scroll-pane";
export function CitationBadge({
@@ -121,6 +123,12 @@ const KnowledgeBaseToolUIImpl: ToolCallMessagePartComponent = ({
);
const [showAll, setShowAll] = useState(false);
const shown = showAll ? documents : documents.slice(0, DOCUMENTS_SHOWN);
+ // Empty chunks are not passages, so this can sit below the summary's count.
+ const passageCount = useMemo(
+ () => citations.filter(isPassage).length,
+ [citations],
+ );
+ const [showPassages, setShowPassages] = useState(false);
const hasText = useAuiState(({ message }) =>
partsHaveNonEmptyText(message.content),
@@ -183,6 +191,21 @@ const KnowledgeBaseToolUIImpl: ToolCallMessagePartComponent = ({
Show {documents.length - shown.length} more
</button>
) : null}
+ {passageCount > 0 ? (
+ <button
+ type="button"
+ aria-expanded={showPassages}
+ onClick={() => setShowPassages((v) => !v)}
+ className="self-start text-xs underline-offset-2 hover:text-foreground hover:underline"
+ >
+ {showPassages
+ ? "Hide passages"
+ : `Show passages (${passageCount})`}
+ </button>
+ ) : null}
+ {showPassages ? (
+ <KnowledgeBasePassages citations={citations} />
+ ) : null}
</>
) : resultText ? (
<ScrollPane
diff --git a/studio/frontend/src/features/rag/index.ts b/studio/frontend/src/features/rag/index.ts
index a22b2349766..92625e04f8e 100644
--- a/studio/frontend/src/features/rag/index.ts
+++ b/studio/frontend/src/features/rag/index.ts
@@ -26,3 +26,4 @@ export {
type StagedFolder,
} from "./components/link-staged-folders";
export { ProjectFolderPicker } from "./components/project-folder-picker";
+export { RagDocumentTrigger } from "./components/rag-document-trigger";
diff --git a/studio/frontend/tests/knowledge-base-passages.test.ts b/studio/frontend/tests/knowledge-base-passages.test.ts
new file mode 100644
index 00000000000..5b9c5b762ea
--- /dev/null
+++ b/studio/frontend/tests/knowledge-base-passages.test.ts
@@ -0,0 +1,439 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// The retrieved passages a document search shows on request (#13788): in the order the model received
+// them, numbered as it saw them, opening at their page, long ones clamped, and capped like the document chips.
+
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import test from "node:test";
+import * as React from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import * as jsxRuntime from "react/jsx-runtime";
+import * as citationUtils from "../src/components/assistant-ui/citation-utils.ts";
+import type * as PassagesModule from "../src/components/assistant-ui/knowledge-base-passages.tsx";
+import { attachmentFileKind } from "../src/features/chat/lib/attachment-file-kind.ts";
+import { loadWithStubs } from "./helpers/module-stubs.ts";
+
+const {
+ parseCitations,
+ passageDisplayText,
+ orderedPassages,
+ passageNeedsClamp,
+} = citationUtils;
+
+const { KnowledgeBasePassages } = loadWithStubs<typeof PassagesModule>(
+ new URL(
+ "../src/components/assistant-ui/knowledge-base-passages.tsx",
+ import.meta.url,
+ ),
+ {
+ react: React,
+ "react/jsx-runtime": jsxRuntime,
+ "./citation-utils": citationUtils,
+ "@/features/chat": { attachmentFileKind },
+ "@/lib/file-icon": { FileGlyph: () => null },
+ // Marks where each passage opens, so the markup shows the document and page it was given.
+ "@/features/rag": {
+ RagDocumentTrigger: ({
+ documentId,
+ page,
+ children,
+ }: {
+ documentId: string;
+ page?: number;
+ children: React.ReactNode;
+ }) =>
+ React.createElement(
+ "span",
+ { "data-opens": `${documentId}@${page ?? "start"}` },
+ children,
+ ),
+ },
+ },
+);
+
+// A search result as the backend stores it on the tool call: rendered chunks, then the source map.
+const RADIUM =
+ "Chapter 3: Radiation limits. The maximum allowed level of combined radium-226 and\nradium-228 in drinking water is 5 picocuries per liter (pCi/L).";
+const RERUN =
+ "Any result above 4 pCi/L is re-run from the retained duplicate before it is reported.";
+const SCOPE =
+ "This handbook sets out how the Authority samples and tests drinking water.";
+const searchResult = (rows: unknown[]) =>
+ `<chunk id="1">…</chunk>\n__RAG_SOURCES__:${JSON.stringify(rows)}`;
+const SEARCH = searchResult([
+ {
+ citationId: 1,
+ chunkId: "h:2",
+ documentId: "h",
+ filename: "handbook.pdf",
+ page: 3,
+ text: RADIUM,
+ score: 0.0328,
+ },
+ {
+ citationId: 2,
+ chunkId: "l:1",
+ documentId: "l",
+ filename: "lab.md",
+ page: null,
+ text: RERUN,
+ score: 0.0323,
+ },
+ {
+ citationId: 3,
+ chunkId: "h:0",
+ documentId: "h",
+ filename: "handbook.pdf",
+ page: 1,
+ text: SCOPE,
+ score: 0.032,
+ },
+ {
+ citationId: 4,
+ chunkId: "h:9",
+ documentId: "h",
+ filename: "handbook.pdf",
+ page: 5,
+ text: " \n ",
+ score: 0.03,
+ },
+]);
+
+const render = (result: unknown) =>
+ renderToStaticMarkup(
+ React.createElement(KnowledgeBasePassages, {
+ citations: parseCitations(result),
+ }),
+ );
+
+test("passages keep the search's order, numbered as the model saw them, across documents", () => {
+ const passages = orderedPassages(parseCitations(SEARCH));
+ assert.deepEqual(
+ passages.map((p) => [p.number, p.citation.filename, p.citation.page]),
+ [
+ [1, "handbook.pdf", 3],
+ [2, "lab.md", null],
+ [3, "handbook.pdf", 1],
+ ],
+ );
+});
+
+test("an empty chunk is not a passage, and the passages after it keep their own numbers", () => {
+ const passages = orderedPassages(
+ parseCitations(
+ searchResult([
+ {
+ chunkId: "a:0",
+ documentId: "a",
+ filename: "a.pdf",
+ page: 1,
+ text: "first",
+ score: 0.9,
+ },
+ {
+ chunkId: "a:1",
+ documentId: "a",
+ filename: "a.pdf",
+ page: 2,
+ text: " \n ",
+ score: 0.8,
+ },
+ {
+ chunkId: "a:2",
+ documentId: "a",
+ filename: "a.pdf",
+ page: 3,
+ text: "third",
+ score: 0.7,
+ },
+ ]),
+ ),
+ );
+ assert.deepEqual(
+ passages.map((p) => [p.number, p.citation.text]),
+ [
+ [1, "first"],
+ [3, "third"],
+ ],
+ );
+});
+
+// The backend appends ranked project passages after a whole-document read under one numbering
+// (render_sources), and the model cites them by that numbering.
+test("passages merged after a whole-document read continue its numbering", () => {
+ const passages = orderedPassages(
+ parseCitations(
+ searchResult([
+ {
+ chunkId: "a:0",
+ documentId: "a",
+ filename: "a.txt",
+ page: null,
+ text: "whole 1",
+ score: null,
+ },
+ {
+ chunkId: "a:1",
+ documentId: "a",
+ filename: "a.txt",
+ page: null,
+ text: "whole 2",
+ score: null,
+ },
+ {
+ chunkId: "p:4",
+ documentId: "p",
+ filename: "p.pdf",
+ page: 4,
+ text: "best match",
+ score: 0.033,
+ },
+ {
+ chunkId: "p:9",
+ documentId: "p",
+ filename: "p.pdf",
+ page: 9,
+ text: "next match",
+ score: 0.031,
+ },
+ ]),
+ ),
+ );
+ assert.deepEqual(
+ passages.map((p) => [p.number, p.citation.text]),
+ [
+ [1, "whole 1"],
+ [2, "whole 2"],
+ [3, "best match"],
+ [4, "next match"],
+ ],
+ );
+});
+
+test("each passage shows its exact text, its number, and opens its document at its own page", () => {
+ const html = render(SEARCH);
+ assert.equal(html.match(/data-slot="knowledge-base-passage"/g)?.length, 3);
+ // The chunk text itself, line breaks and spacing kept for whitespace-pre-wrap to show.
+ assert.ok(html.includes("radium-226 and\nradium-228"));
+ assert.ok(html.includes(RERUN) && html.includes(SCOPE));
+ assert.ok(html.includes("#1") && html.includes("#2") && html.includes("#3"));
+ // The answer's passage opens at its page, not at the document's first cited page.
+ assert.match(html, /data-opens="h@3"><button[^>]*>Open at p\.3</);
+ assert.match(html, /data-opens="h@1"><button[^>]*>Open at p\.1</);
+ // A file without pages still opens, from the start.
+ assert.match(html, /data-opens="l@start"><button[^>]*>Open document</);
+ // Rendered in the search's order, so lab.md's #2 sits between handbook.pdf's #1 and #3.
+ const numbers = [
+ ...html.matchAll(/model was given">#(?:<!-- -->)?(\d+)</g),
+ ].map((m) => Number(m[1]));
+ assert.deepEqual(numbers, [1, 2, 3]);
+ // With more than one document, every passage names its own.
+ assert.equal(html.match(/title="handbook\.pdf"/g)?.length, 2);
+ assert.equal(html.match(/title="lab\.md"/g)?.length, 1);
+});
+
+test("a single-document search leaves the filename to its chip", () => {
+ const html = render(
+ searchResult([
+ {
+ chunkId: "h:2",
+ documentId: "h",
+ filename: "handbook.pdf",
+ page: 3,
+ text: RADIUM,
+ score: 0.9,
+ },
+ {
+ chunkId: "h:0",
+ documentId: "h",
+ filename: "handbook.pdf",
+ page: 1,
+ text: SCOPE,
+ score: 0.8,
+ },
+ ]),
+ );
+ assert.equal(html.match(/data-slot="knowledge-base-passage"/g)?.length, 2);
+ assert.ok(!html.includes('title="handbook.pdf"'));
+});
+
+test("a source with no document id shows its page but offers nothing to open", () => {
+ const html = render([
+ { text: "legacy passage", filename: "old.pdf", page: 2, score: 0.5 },
+ ]);
+ assert.ok(html.includes("legacy passage") && html.includes("p.2"));
+ assert.ok(!html.includes("data-opens") && !html.includes("Open "));
+});
+
+test("a long passage starts clamped behind Show more; a short one shows whole", () => {
+ assert.equal(passageNeedsClamp("short"), false);
+ // Under about four lines at text-xs in the chat column, a passage shows whole.
+ assert.equal(passageNeedsClamp("x".repeat(450)), false);
+ assert.equal(passageNeedsClamp("x".repeat(451)), true);
+ assert.equal(passageNeedsClamp("a\nb\nc\nd\ne"), true);
+ const long = render(
+ searchResult([
+ {
+ chunkId: "d:0",
+ documentId: "d",
+ filename: "d.pdf",
+ page: 1,
+ text: "word ".repeat(120),
+ score: 1,
+ },
+ ]),
+ );
+ assert.match(long, /line-clamp-4/);
+ assert.match(long, /aria-expanded="false"[^>]*>Show more</);
+ const short = render(SEARCH);
+ assert.ok(!short.includes("line-clamp-4") && !short.includes("Show more"));
+});
+
+test("a large search shows the first 20 passages and holds the rest behind Show N more", () => {
+ const rows = Array.from({ length: 23 }, (_, i) => ({
+ chunkId: `d${i % 2}:${i}`,
+ documentId: `d${i % 2}`,
+ filename: `doc${i % 2}.pdf`,
+ page: i + 1,
+ text: `passage ${i + 1}`,
+ score: 1 - i / 100,
+ }));
+ const html = render(searchResult(rows));
+ assert.equal(html.match(/data-slot="knowledge-base-passage"/g)?.length, 20);
+ // The first 20 in the search's order, across both documents.
+ const numbers = [
+ ...html.matchAll(/model was given">#(?:<!-- -->)?(\d+)</g),
+ ].map((m) => Number(m[1]));
+ assert.deepEqual(
+ numbers,
+ Array.from({ length: 20 }, (_, i) => i + 1),
+ );
+ assert.match(html, />Show 3 more passages</);
+});
+
+test("the search card keeps one chip per document and opens the passages only on request", () => {
+ const kb = readFileSync(
+ new URL(
+ "../src/components/assistant-ui/tool-ui-knowledge-base.tsx",
+ import.meta.url,
+ ),
+ "utf8",
+ );
+ assert.match(
+ kb,
+ /const \[showPassages, setShowPassages\] = useState\(false\)/,
+ );
+ assert.match(kb, /aria-expanded=\{showPassages\}/);
+ assert.match(
+ kb,
+ /\{showPassages \? \(\s*<KnowledgeBasePassages citations=\{citations\} \/>/,
+ );
+ assert.match(kb, /`Show passages \(\$\{passageCount\}\)`/);
+});
+
+test("a PDF passage drops the heading markers its conversion adds; every passage folds only empty lines", () => {
+ assert.equal(
+ passageDisplayText(
+ "# Chapter 3: Radiation limits\n\nThe limit is 5 pCi/L.",
+ "standard.pdf",
+ ),
+ "Chapter 3: Radiation limits\nThe limit is 5 pCi/L.",
+ );
+ assert.equal(
+ passageDisplayText(
+ " ## Sample custody\nBottles are labelled. ",
+ "Lab.PDF",
+ ),
+ "Sample custody\nBottles are labelled.",
+ );
+ // Only a heading at a line's start: a hash inside a sentence, or a tag, stays.
+ assert.equal(
+ passageDisplayText("Issue #13788 and #hashtag", "a.pdf"),
+ "Issue #13788 and #hashtag",
+ );
+ assert.equal(passageDisplayText("#nospace", "a.pdf"), "#nospace");
+ // Line breaks inside the text stay; only empty lines fold.
+ assert.equal(passageDisplayText("a\nb\n\n \n\nc"), "a\nb\nc");
+});
+
+// pymupdf4llm fences a PDF's code listing; its `#` comments are content, not headings. A chunk can start inside a
+// listing, since the chunker splits at "\n# ".
+test("PDF code listings keep their # comments", () => {
+ const page =
+ "# Retry Policy\n\nRetries are configured below.\n\n```\n# retry limit\nMAX_RETRIES = 3\n## backoff seconds\nBACKOFF = 2\n\n```\n\n# Logging";
+ assert.equal(
+ passageDisplayText(page, "client.pdf"),
+ "Retry Policy\nRetries are configured below.\n```\n# retry limit\nMAX_RETRIES = 3\n## backoff seconds\nBACKOFF = 2\n```\nLogging",
+ );
+ assert.equal(
+ passageDisplayText(
+ "# backoff seconds\nBACKOFF = 2\n\n```\n\n# Logging",
+ "client.pdf",
+ ),
+ "# backoff seconds\nBACKOFF = 2\n```\nLogging",
+ );
+});
+
+// Text sources (code, config, Markdown) are indexed as written, so their # lines are content.
+test("code and config passages keep their # comments and indentation", () => {
+ const python =
+ "# retry limit\nretries = 3\n\ndef f():\n x = 1\n\n return x";
+ assert.equal(
+ passageDisplayText(python, "worker.py"),
+ "# retry limit\nretries = 3\ndef f():\n x = 1\n return x",
+ );
+ assert.equal(
+ passageDisplayText("# Sample custody\nBottles are labelled.", "lab.md"),
+ "# Sample custody\nBottles are labelled.",
+ );
+ const html = render(
+ searchResult([
+ {
+ chunkId: "w:0",
+ documentId: "w",
+ filename: "worker.py",
+ page: null,
+ text: python,
+ score: 1,
+ },
+ ]),
+ );
+ assert.ok(html.includes("# retry limit"));
+ assert.ok(html.includes("\n x = 1\n return x"));
+ // pre-wrap keeps the indentation on screen; pre-line would collapse it.
+ assert.match(html, /whitespace-pre-wrap/);
+});
+
+test("code and config passages render in a monospace font; documents and notes do not", () => {
+ const passage = (filename: string) =>
+ render(
+ searchResult([
+ {
+ chunkId: `${filename}:0`,
+ documentId: filename,
+ filename,
+ page: null,
+ text: "retries = 3",
+ score: 1,
+ },
+ ]),
+ );
+ for (const filename of [
+ "worker.py",
+ "config.yaml",
+ "settings.toml",
+ "query.sql",
+ ]) {
+ assert.match(passage(filename), /font-mono/, filename);
+ }
+ for (const filename of [
+ "handbook.pdf",
+ "lab.md",
+ "notes.txt",
+ "report.docx",
+ ]) {
+ assert.ok(!passage(filename).includes("font-mono"), filename);
+ }
+});
diff --git a/studio/frontend/tests/tool-fallback-label.test.ts b/studio/frontend/tests/tool-fallback-label.test.ts
index ed4ca869813..d54ebedf0bc 100644
--- a/studio/frontend/tests/tool-fallback-label.test.ts
+++ b/studio/frontend/tests/tool-fallback-label.test.ts
@@ -121,8 +121,9 @@ test("the document search header stays one line, with the summary first and the
assert.match(fallback, /const wrapWhenOpen = !singleLine;/);
});
-// A search lists each document once in its expanded card, with the pages it cited, not every
-// passage; documents open in Studio's browser; no separate Document Sources strip or PDF sheet.
+// A search lists each document once in its expanded card, with the pages it cited; its passages
+// wait behind "Show passages" (#13788); documents open in Studio's browser; no separate Document
+// Sources strip or PDF sheet.
test("a document search lists each document once in the card, with its cited pages", () => {
const read = (path: string) =>
readFileSync(new URL(`../src/${path}`, import.meta.url), "utf8");
@@ -131,7 +132,12 @@ test("a document search lists each document once in the card, with its cited pag
kb,
/shown\.map\(\(citation, i\) => \(\s*<CitationBadge[\s\S]*?pages=\{pagesByDocument\.get\(/,
);
- assert.ok(!kb.includes("{citation.text}"), "passage snippets are not shown");
+ assert.ok(!kb.includes("{citation.text}"), "the chips carry no passage text");
+ assert.match(
+ kb,
+ /\{showPassages \? \(\s*<KnowledgeBasePassages/,
+ "passages render only once asked for",
+ );
// A model-chosen top_k has no ceiling: the rest wait behind "Show N more".
assert.match(kb, /documents\.slice\(0, DOCUMENTS_SHOWN\)/);
assert.match(kb, /Show \{documents\.length - shown\.length\} more/);