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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: open a project source from the Sources tab (#13441)
Daniel Hand7acd1dfc723
diff --git a/studio/frontend/src/features/rag/components/document-status-chip.tsx b/studio/frontend/src/features/rag/components/document-status-chip.tsxindex 1c635ee5f67..5fccdf5c777 100644--- a/studio/frontend/src/features/rag/components/document-status-chip.tsx+++ b/studio/frontend/src/features/rag/components/document-status-chip.tsx@@ -7,6 +7,7 @@ import { cn } from "@/lib/utils";import { FileEmpty02Icon, Folder02Icon } from "@hugeicons/core-free-icons";import { HugeiconsIcon } from "@hugeicons/react";import { XIcon } from "lucide-react";+import type { ReactElement, ReactNode } from "react";import type { DocumentStatus } from "../types/rag";export const STAGE_LABELS: Record<string, string> = {@@ -25,6 +26,8 @@ export function DocumentStatusChip({stage,error,onRemove,+ onOpen,+ openTrigger,onRetry,shared = false,}: {@@ -34,12 +37,27 @@ export function DocumentStatusChip({stage?: string | null;error?: string | null;onRemove?: () => void;+ /** Makes the name a button that opens the source. */+ onOpen?: () => void;+ /** Hands that button to a viewer that owns its own open state (e.g. a dialog trigger). */+ openTrigger?: (trigger: ReactElement) => ReactNode;onRetry?: () => void;/** Indexed for the whole project rather than this one chat: swap the file* glyph for a folder so the two scopes are told apart at a glance. */shared?: boolean;}) {const processing = status === "pending" || status === "running";+ const openable = status === "completed" && (onOpen || openTrigger);+ const nameButton = openable ? (+ <button+ type="button"+ onClick={onOpen}+ aria-label={`Preview ${filename}`}+ className="truncate rounded-sm text-left underline-offset-2 hover:underline focus-visible:underline focus-visible:outline-none"+ >+ {filename}+ </button>+ ) : null;return (<Badgevariant="outline"@@ -63,7 +81,15 @@ export function DocumentStatusChip({strokeWidth={2}className="size-3 shrink-0"/>- <span className="truncate">{filename}</span>+ {nameButton ? (+ openTrigger ? (+ openTrigger(nameButton)+ ) : (+ nameButton+ )+ ) : (+ <span className="truncate">{filename}</span>+ )}{status === "failed" && onRetry ? (<buttontype="button"diff --git a/studio/frontend/src/features/rag/components/project-source-chip.tsx b/studio/frontend/src/features/rag/components/project-source-chip.tsxnew file mode 100644index 00000000000..e986cf17945--- /dev/null+++ b/studio/frontend/src/features/rag/components/project-source-chip.tsx@@ -0,0 +1,61 @@+// 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 { LocalFileDialog } from "@/components/assistant-ui/attachment-document-dialog";+import { MAX_DOCUMENT_PREVIEW_BYTES } from "@/components/file-viewer";+import { attachmentFileKind } from "@/features/chat";+import { DocumentStatusChip } from "./document-status-chip";+import { useDocumentPreviewStore } from "./preview-store";+import { asViewerFile, fetchSourceFile, isViewableSource } from "./source-file";+import type { TrackedDocument } from "./use-rag-documents";++/** A Sources tab chip whose name opens the file, the same viewers as the chat with files cards:+ * PDFs in the source preview, other pages, markdown and text in the attachment viewer. */+export function ProjectSourceChip({+ doc,+ onRemove,+ onRetry,+}: {+ doc: TrackedDocument;+ onRemove?: () => void;+ onRetry?: () => void;+}) {+ const openPreview = useDocumentPreviewStore((s) => s.openPreview);+ const ready = doc.status === "completed" && !doc.id.startsWith("pending_");+ const isPdf = attachmentFileKind(doc.filename, undefined) === "pdf";+ const viewable = !isPdf && isViewableSource(doc.filename);+ const name = doc.filename.split("/").pop() ?? doc.filename;+ // Read on open; the dialog keeps the latest one in a ref, so a new identity per render is fine.+ const load = async (): Promise<Blob> => {+ const file = await fetchSourceFile(doc, MAX_DOCUMENT_PREVIEW_BYTES);+ if (!file) {+ throw new Error(`Couldn't preview ${doc.filename}`);+ }+ return asViewerFile(file);+ };+ return (+ <DocumentStatusChip+ filename={doc.filename}+ status={doc.status}+ progress={doc.progress}+ stage={doc.stage}+ error={doc.error}+ onRemove={onRemove}+ onRetry={onRetry}+ onOpen={+ ready && isPdf+ ? () => openPreview({ documentId: doc.id, filename: doc.filename })+ : undefined+ }+ openTrigger={+ ready && viewable+ ? (trigger) => (+ <LocalFileDialog name={name} load={load}>+ {trigger}+ </LocalFileDialog>+ )+ : undefined+ }+ />+ );+}diff --git a/studio/frontend/src/features/rag/components/project-sources-panel.tsx b/studio/frontend/src/features/rag/components/project-sources-panel.tsxindex c99b06c1b2d..aef92e8d024 100644--- a/studio/frontend/src/features/rag/components/project-sources-panel.tsx+++ b/studio/frontend/src/features/rag/components/project-sources-panel.tsx@@ -24,6 +24,7 @@ import {useLinkedFolderNames,} from "./linked-folder-groups";import { LinkedFoldersManager } from "./linked-folders-manager";+import { ProjectSourceChip } from "./project-source-chip";import {RAG_SOURCE_UPLOAD_ACCEPT,SUPPORTED_SOURCES_HINT,@@ -289,13 +290,9 @@ export function ProjectSourcesPanel({ projectId }: { projectId: string }) {);})}{groups.loose.map((doc) => (- <DocumentStatusChip+ <ProjectSourceChipkey={doc.id}- filename={doc.filename}- status={doc.status}- progress={doc.progress}- stage={doc.stage}- error={doc.error}+ doc={doc}onRemove={isLinkedFolderManaged(doc) ||(doc.id.startsWith("pending_") && doc.status !== "failed")diff --git a/studio/frontend/src/features/rag/components/source-file.ts b/studio/frontend/src/features/rag/components/source-file.tsnew file mode 100644index 00000000000..7723a5fc55c--- /dev/null+++ b/studio/frontend/src/features/rag/components/source-file.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++import { documentKind, isMarkdown } from "@/components/file-viewer";+import { isTextAttachment } from "@/features/chat";+import { getDocumentFileUrl } from "../api/rag-api";+import type { RagDocument } from "../types/rag";++/** The stored source as a File, or null past `maxBytes` (0 = no cap). */+export async function fetchSourceFile(+ doc: Pick<RagDocument, "id" | "filename">,+ maxBytes: number,+): Promise<File | null> {+ const response = await fetch(await getDocumentFileUrl(doc.id));+ if (!response.ok) return null;+ if (maxBytes && Number(response.headers.get("content-length")) > maxBytes) {+ void response.body?.cancel();+ return null;+ }+ const blob = await response.blob();+ if (blob.size === 0 || (maxBytes && blob.size > maxBytes)) return null;+ // Linked-folder documents are named by their relative path.+ const name = doc.filename.split("/").pop() ?? doc.filename;+ // Most types are served as octet-stream; an empty type lets the extension decide the kind.+ const type = blob.type === "application/octet-stream" ? "" : blob.type;+ return new File([blob], name, { type });+}++/** Whether the attachment viewer can show this source: pages, markdown or text. */+export function isViewableSource(name: string): boolean {+ return (+ documentKind(name) !== null ||+ isMarkdown(name) ||+ isTextAttachment(name, undefined)+ );+}++/** Served as octet-stream: the viewer reads text only when the type says so. */+export function asViewerFile(file: File): File {+ const textual =+ documentKind(file.name, file.type) === null &&+ !isMarkdown(file.name, file.type);+ return textual ? new File([file], file.name, { type: "text/plain" }) : file;+}diff --git a/studio/frontend/src/features/rag/components/thread-documents-bar.tsx b/studio/frontend/src/features/rag/components/thread-documents-bar.tsxindex 905c3159928..ba280da0223 100644--- a/studio/frontend/src/features/rag/components/thread-documents-bar.tsx+++ b/studio/frontend/src/features/rag/components/thread-documents-bar.tsx@@ -32,11 +32,7 @@ import {attachmentPreview,} from "@/components/assistant-ui/attachment-card-preview";import { LocalFileDialog } from "@/components/assistant-ui/attachment-document-dialog";-import {- MAX_DOCUMENT_PREVIEW_BYTES,- documentKind,- isMarkdown,-} from "@/components/file-viewer";+import { MAX_DOCUMENT_PREVIEW_BYTES } from "@/components/file-viewer";import { Spinner } from "@/components/ui/spinner";import {Tooltip,@@ -62,7 +58,6 @@ import {annotationsOfFile,attachmentFileKind,isPastedTextFile,- isTextAttachment,isThreadIncognito,subscribeChatHistoryUpdated,} from "@/features/chat";@@ -81,7 +76,6 @@ import {} from "@/components/ui/dropdown-menu";import {announceProjectSourcesUpdated,- getDocumentFileUrl,invalidateProjectSources,listKnowledgeBases,subscribeKnowledgeBasesChanged,@@ -111,6 +105,7 @@ import {SUPPORTED_SOURCES_HINT,isSupportedSourceName,} from "./source-drop-policy";+import { asViewerFile, fetchSourceFile, isViewableSource } from "./source-file";import { EXPIRY_GRACE_MS } from "./staged-source";import {type RagUploadItem,@@ -330,38 +325,9 @@ function forgetThumbnail(id: string): void {thumbnailSizes.delete(id);}-/** The stored source as a File, or null past `maxBytes` (0 = no cap). */-async function fetchSourceFile(- doc: TrackedDocument,- maxBytes: number,-): Promise<File | null> {- const response = await fetch(await getDocumentFileUrl(doc.id));- if (!response.ok) return null;- if (maxBytes && Number(response.headers.get("content-length")) > maxBytes) {- void response.body?.cancel();- return null;- }- const blob = await response.blob();- if (blob.size === 0 || (maxBytes && blob.size > maxBytes)) return null;- // Linked-folder documents are named by their relative path.- const name = doc.filename.split("/").pop() ?? doc.filename;- // Most types are served as octet-stream; an empty type lets the extension decide the kind.- const type = blob.type === "application/octet-stream" ? "" : blob.type;- return new File([blob], name, { type });-}-const fetchThumbnailFile = (doc: TrackedDocument) =>fetchSourceFile(doc, MAX_THUMBNAIL_BYTES);-/** Whether the attachment viewer can show this source: pages, markdown or text. */-function isViewableSource(name: string): boolean {- return (- documentKind(name) !== null ||- isMarkdown(name) ||- isTextAttachment(name, undefined)- );-}-function loadThumbnailFile(doc: TrackedDocument): Promise<File | null> {const cached = thumbnailFiles.get(doc.id);if (cached) {@@ -492,10 +458,7 @@ function DocumentCard({const file =thumbnailFile ?? (await fetchSourceFile(doc, MAX_DOCUMENT_PREVIEW_BYTES));if (!file) throw new Error(`Couldn't preview ${doc.filename}`);- const textual =- documentKind(file.name, file.type) === null && !isMarkdown(file.name, file.type);- // Served as octet-stream: the viewer reads text only when the type says so.- return textual ? new File([file], file.name, { type: "text/plain" }) : file;+ return asViewerFile(file);// The id names the stored file; the rest of the row changes with progress frames.// eslint-disable-next-line react-hooks/exhaustive-deps}, [doc.id, thumbnailFile]);diff --git a/studio/frontend/tests/attachment-preview-text.test.ts b/studio/frontend/tests/attachment-preview-text.test.tsindex 9a35f1b4fc1..f644c75f427 100644--- a/studio/frontend/tests/attachment-preview-text.test.ts+++ b/studio/frontend/tests/attachment-preview-text.test.ts@@ -2204,8 +2204,10 @@ test("a UTF-16 text source opened from the files panel previews as its text", asconst { readFile } = await import("node:fs/promises");const dialog = await readFile(new URL("../src/components/assistant-ui/attachment-document-dialog.tsx", import.meta.url), "utf8");assert.match(dialog, /blob instanceof File\s*\?\s*await readAttachmentText\(blob, source\.name, blob\.type\)/);+ const sourceFile = await readFile(new URL("../src/features/rag/components/source-file.ts", import.meta.url), "utf8");+ assert.match(sourceFile, /new File\(\[file\], file\.name, \{ type: "text\/plain" \}\)/);const bar = await readFile(new URL("../src/features/rag/components/thread-documents-bar.tsx", import.meta.url), "utf8");- assert.match(bar, /new File\(\[file\], file\.name, \{ type: "text\/plain" \}\)/);+ assert.match(bar, /return asViewerFile\(file\);/);});async function readRtf(rtf: string | Uint8Array<ArrayBuffer>): Promise<string> {diff --git a/studio/frontend/tests/project-source-chip-open.test.ts b/studio/frontend/tests/project-source-chip-open.test.tsnew file mode 100644index 00000000000..cedfad58086--- /dev/null+++ b/studio/frontend/tests/project-source-chip-open.test.ts@@ -0,0 +1,144 @@+// 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 test from "node:test";+import * as React from "react";+import * as jsxRuntime from "react/jsx-runtime";+import { renderToStaticMarkup } from "react-dom/server";++import type * as ChipModule from "../src/features/rag/components/document-status-chip.tsx";+import type * as SourceChipModule from "../src/features/rag/components/project-source-chip.tsx";+import { type StubElement, loadWithStubs, stubJsxRuntime } from "./helpers/module-stubs.ts";++const Nothing = () => null;++const chip = loadWithStubs<typeof ChipModule>(+ new URL("../src/features/rag/components/document-status-chip.tsx", import.meta.url),+ {+ react: React,+ "react/jsx-runtime": jsxRuntime,+ "@/components/assistant-ui/badge": {+ Badge: ({ children }: { children?: React.ReactNode }) =>+ React.createElement("span", { "data-badge": "" }, children),+ },+ "@/components/ui/spinner": { Spinner: Nothing },+ "@/lib/utils": { cn: (...parts: unknown[]) => parts.filter(Boolean).join(" ") },+ "@hugeicons/core-free-icons": {},+ "@hugeicons/react": { HugeiconsIcon: Nothing },+ "lucide-react": { XIcon: Nothing },+ },+);++const opened: Record<string, unknown>[] = [];+const LocalFileDialog = () => null;++const { ProjectSourceChip } = loadWithStubs<typeof SourceChipModule>(+ new URL("../src/features/rag/components/project-source-chip.tsx", import.meta.url),+ {+ "react/jsx-runtime": stubJsxRuntime(),+ "@/components/assistant-ui/attachment-document-dialog": { LocalFileDialog },+ "@/components/file-viewer": { MAX_DOCUMENT_PREVIEW_BYTES: 1 },+ "@/features/chat": {+ attachmentFileKind: (name: string) => (name.endsWith(".pdf") ? "pdf" : "file"),+ },+ "./document-status-chip": chip,+ "./preview-store": {+ useDocumentPreviewStore: (select: (s: Record<string, unknown>) => unknown) =>+ select({ openPreview: (args: Record<string, unknown>) => opened.push(args) }),+ },+ "./source-file": {+ asViewerFile: (file: File) => file,+ fetchSourceFile: async () => null,+ isViewableSource: (name: string) => /\.(md|txt|docx)$/.test(name),+ },+ },+);++function chipProps(doc: Record<string, unknown>): Record<string, unknown> {+ const element = ProjectSourceChip({+ doc: doc as unknown as Parameters<typeof ProjectSourceChip>[0]["doc"],+ }) as unknown as StubElement;+ assert.equal(element.type, chip.DocumentStatusChip);+ return element.props;+}++test("an indexed PDF source opens the source preview", () => {+ opened.length = 0;+ const props = chipProps({ id: "doc-1", filename: "handbook.pdf", status: "completed" });+ assert.equal(props.openTrigger, undefined);+ assert.equal(typeof props.onOpen, "function");+ (props.onOpen as () => void)();+ assert.deepEqual(opened, [{ documentId: "doc-1", filename: "handbook.pdf" }]);+});++test("an indexed text, markdown or Office source opens in the attachment viewer", () => {+ for (const filename of ["notes.md", "notes.txt", "Docs/report.docx"]) {+ const props = chipProps({ id: "doc-2", filename, status: "completed" });+ assert.equal(props.onOpen, undefined);+ const trigger = React.createElement("button");+ const wrapped = (props.openTrigger as (t: React.ReactElement) => StubElement)(trigger);+ assert.equal(wrapped.type, LocalFileDialog);+ assert.equal(wrapped.props.name, filename.split("/").pop());+ assert.equal(wrapped.props.children, trigger);+ }+});++test("sources still indexing, failed, optimistic or unviewable stay inert", () => {+ for (const doc of [+ { id: "doc-3", filename: "handbook.pdf", status: "running" },+ { id: "doc-3", filename: "notes.md", status: "pending" },+ { id: "doc-3", filename: "notes.md", status: "failed" },+ { id: "pending_1", filename: "notes.md", status: "completed" },+ { id: "doc-3", filename: "archive.epub", status: "completed" },+ ]) {+ const props = chipProps(doc);+ assert.equal(props.onOpen, undefined, JSON.stringify(doc));+ assert.equal(props.openTrigger, undefined, JSON.stringify(doc));+ }+});++test("remove and retry pass through to the chip unchanged", () => {+ const onRemove = () => undefined;+ const onRetry = () => undefined;+ const element = ProjectSourceChip({+ doc: { id: "doc-4", filename: "notes.md", status: "failed" } as unknown as Parameters<+ typeof ProjectSourceChip+ >[0]["doc"],+ onRemove,+ onRetry,+ }) as unknown as StubElement;+ assert.equal(element.props.onRemove, onRemove);+ assert.equal(element.props.onRetry, onRetry);+});++test("the chip name is a button only when the source can be opened", () => {+ const render = (props: Record<string, unknown>) =>+ renderToStaticMarkup(+ React.createElement(chip.DocumentStatusChip, {+ filename: "notes.md",+ status: "completed",+ ...props,+ } as Parameters<typeof chip.DocumentStatusChip>[0]),+ );+ assert.doesNotMatch(render({}), /<button/);+ assert.match(render({ onOpen: () => undefined }), /<button type="button" aria-label="Preview notes.md"/);+ assert.match(+ render({+ openTrigger: (t: React.ReactElement) => React.createElement("i", { "data-viewer": "" }, t),+ }),+ /<i data-viewer=""><button type="button" aria-label="Preview notes.md"/,+ );+ // A still-indexing row keeps its spinner and no open affordance, even when asked.+ assert.doesNotMatch(render({ status: "running", onOpen: () => undefined }), /Preview notes/);+});++test("the project Sources tab renders openable chips", () => {+ const panel = readFileSync(+ new URL("../src/features/rag/components/project-sources-panel.tsx", import.meta.url),+ "utf8",+ );+ // Each file is openable; a linked folder stays one summary chip.+ assert.match(panel, /groups\.loose\.map\(\(doc\) => \(\s*<ProjectSourceChip\s+key=\{doc\.id\}\s+doc=\{doc\}/);+});diff --git a/studio/frontend/tests/project-sources-model-without-tools.test.ts b/studio/frontend/tests/project-sources-model-without-tools.test.tsindex 3e9c1cafbf8..0128d7489fc 100644--- a/studio/frontend/tests/project-sources-model-without-tools.test.ts+++ b/studio/frontend/tests/project-sources-model-without-tools.test.ts@@ -41,6 +41,20 @@ const Nothing = () => null;const Passthrough = ({ children }: { children?: React.ReactNode }) =>React.createElement(React.Fragment, null, children);+const chatStub = {+ isThreadIncognito: () => false,+ chatHistoryClearBoundary: { capture: () => 0 },+ attachmentFileKind: (name: string) =>+ name.endsWith(".pdf") ? "pdf" : "word",+ isTextAttachment: (name: string) => name.endsWith(".txt"),+};+const fileViewerStub = {+ MAX_DOCUMENT_PREVIEW_BYTES: 50,+ documentKind: (name: string) =>+ name.endsWith(".pdf") ? "pdf" : name.endsWith(".docx") ? "docx" : null,+ isMarkdown: () => false,+};+const folderGroups = loadWithStubs<typeof FolderGroupsModule>(new URL("../src/features/rag/components/linked-folder-groups.ts", import.meta.url),{ react: React, "../api/rag-api": { listLinkedFolders: async () => [] } },@@ -92,19 +106,8 @@ const { ThreadDocumentsBar } = loadWithStubs<typeof BarModule>(useChatRuntimeStore,},"@/features/chat/hooks/use-rag-tool-disabled": gate,- "@/features/chat": {- isThreadIncognito: () => false,- chatHistoryClearBoundary: { capture: () => 0 },- attachmentFileKind: (name: string) =>- name.endsWith(".pdf") ? "pdf" : "word",- isTextAttachment: (name: string) => name.endsWith(".txt"),- },- "@/components/file-viewer": {- MAX_DOCUMENT_PREVIEW_BYTES: 50,- documentKind: (name: string) =>- name.endsWith(".pdf") ? "pdf" : name.endsWith(".docx") ? "docx" : null,- isMarkdown: () => false,- },+ "@/features/chat": chatStub,+ "@/components/file-viewer": fileViewerStub,"@/features/native-intents": {useNativeAttachmentTargetKey: () => null,useNativeIntentStore: selectorStore({ pendingAttachments: {} }),@@ -144,6 +147,14 @@ const { ThreadDocumentsBar } = loadWithStubs<typeof BarModule>("@/components/ui/alert-dialog": new Proxy({}, { get: () => Passthrough }),"./document-status-chip": { STAGE_LABELS: {} },"./knowledge-base-dialog": { KnowledgeBaseDialog: Nothing },+ "./source-file": loadWithStubs(+ new URL("../src/features/rag/components/source-file.ts", import.meta.url),+ {+ "@/components/file-viewer": fileViewerStub,+ "@/features/chat": chatStub,+ "../api/rag-api": { getDocumentFileUrl: async () => "" },+ },+ ),"./staged-source": { EXPIRY_GRACE_MS: 0 },"./use-rag-documents": {uploadItemFromIntent: () => null,diff --git a/studio/frontend/tests/rag-thread-initialization.test.ts b/studio/frontend/tests/rag-thread-initialization.test.tsindex 81c7b141c6b..c3c6bbe1cde 100644--- a/studio/frontend/tests/rag-thread-initialization.test.ts+++ b/studio/frontend/tests/rag-thread-initialization.test.ts@@ -242,6 +242,11 @@ function harness(},"./document-status-chip": {},"./knowledge-base-dialog": { KnowledgeBaseDialog: "KnowledgeBaseDialog" },+ "./source-file": {+ asViewerFile: (file: File) => file,+ fetchSourceFile: async () => null,+ isViewableSource: () => false,+ },"./staged-source": { EXPIRY_GRACE_MS: 30_000 },"./use-rag-documents": {uploadItemFromIntent: (intent: {