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: link folders when creating a project (#12057)

Michael Han4c1ff717a378
diff --git a/studio/frontend/src/features/chat/components/new-project-dialog.tsx b/studio/frontend/src/features/chat/components/new-project-dialog.tsx
index 9ff26e09dba..a37a36e91d2 100644
--- a/studio/frontend/src/features/chat/components/new-project-dialog.tsx
+++ b/studio/frontend/src/features/chat/components/new-project-dialog.tsx
@@ -12,6 +12,11 @@ import {
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
+import {
+ linkStagedFolders,
+ ProjectFolderPicker,
+ type StagedFolder,
+} from "@/features/rag";
import {
ProjectSourceDropzone,
type StagedSource,
@@ -51,10 +56,13 @@ export function NewProjectDialog({
const navigate = useNavigate();
const [name, setName] = useState("");
const [staged, setStaged] = useState<StagedSource[]>([]);
+ const [folders, setFolders] = useState<StagedFolder[]>([]);
const [busy, setBusy] = useState(false);
// A desktop drop reaches `staged` only once its native registration settles. Creating before
// then would upload without the files the user just dropped.
const [stagingDrop, setStagingDrop] = useState(false);
+ // Same for a folder pick still resolving over IPC.
+ const [pickingFolder, setPickingFolder] = useState(false);
// Uploads outlive this component, so a slow one must not yank the user to the new project after
// they have navigated away.
const mounted = useRef(true);
@@ -70,7 +78,9 @@ export function NewProjectDialog({
function reset() {
setName("");
setStaged([]);
+ setFolders([]);
setStagingDrop(false);
+ setPickingFolder(false);
}
// Every close path routes through here: callers keep this mounted, so a draft left behind would
@@ -83,7 +93,7 @@ export function NewProjectDialog({
async function commitCreate() {
const trimmed = name.trim();
- if (!trimmed || busy || stagingDrop) return;
+ if (!trimmed || busy || stagingDrop || pickingFolder) return;
setBusy(true);
// Sidebar callers keep this mounted across routes, so unmounting alone cannot tell whether the
// user has moved on during a slow upload.
@@ -91,6 +101,8 @@ export function NewProjectDialog({
try {
const project = await createChatProject(trimmed);
// Upload before closing so the Sources panel lists them on first fetch.
+ // Folders first: their leases expire in minutes, and uploads can be slow.
+ await linkStagedFolders(project.id, folders);
await uploadStagedSources(project.id, staged);
if (!mounted.current) return;
const stayedOnRoute = currentRoute() === origin;
@@ -162,6 +174,12 @@ export function NewProjectDialog({
disabled={busy}
onPendingChange={setStagingDrop}
/>
+ <ProjectFolderPicker
+ folders={folders}
+ onChange={setFolders}
+ disabled={busy}
+ onPendingChange={setPickingFolder}
+ />
<DialogFooter className="flex-wrap gap-2 sm:justify-end">
<Button type="button" variant="ghost" disabled={busy} onClick={close}>
Cancel
@@ -169,9 +187,13 @@ export function NewProjectDialog({
<Button
type="button"
onClick={() => void commitCreate()}
- disabled={!name.trim() || busy || stagingDrop}
+ disabled={!name.trim() || busy || stagingDrop || pickingFolder}
>
- {busy ? "Creating…" : stagingDrop ? "Adding sources…" : submitLabel}
+ {busy
+ ? "Creating…"
+ : stagingDrop || pickingFolder
+ ? "Adding sources…"
+ : submitLabel}
</Button>
</DialogFooter>
</DialogContent>
diff --git a/studio/frontend/src/features/native-intents/api.ts b/studio/frontend/src/features/native-intents/api.ts
index 94982e86c41..be3edb2fd3b 100644
--- a/studio/frontend/src/features/native-intents/api.ts
+++ b/studio/frontend/src/features/native-intents/api.ts
@@ -22,6 +22,8 @@ export interface NativeDocumentFolderSelection {
/** Opaque directory lease. This is deliberately not a filesystem path. */
token: string;
displayName: string;
+ /** When the lease was signed to expire. Older shells omit it. */
+ expiresAtMs?: number;
}
export async function pickNativeDocumentFolder(): Promise<NativeDocumentFolderSelection | null> {
diff --git a/studio/frontend/src/features/rag/components/link-staged-folders.ts b/studio/frontend/src/features/rag/components/link-staged-folders.ts
new file mode 100644
index 00000000000..20ecff58420
--- /dev/null
+++ b/studio/frontend/src/features/rag/components/link-staged-folders.ts
@@ -0,0 +1,68 @@
+// 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 type { NativeDocumentFolderSelection } from "@/features/native-intents";
+import { toast } from "@/lib/toast";
+import {
+ announceProjectSourcesUpdated,
+ createLinkedFolder,
+ noteProjectWork,
+ watchProjectFolderJob,
+} from "../api/rag-api";
+import { markProjectSourcesPending } from "./project-source-dropzone";
+import { EXPIRY_GRACE_MS } from "./staged-source";
+
+/** LEASE_TTL in native_backend_lease.rs, for shells that do not send expiresAtMs. */
+const FOLDER_LEASE_TTL_MS = 2 * 60_000;
+
+export type StagedFolder = NativeDocumentFolderSelection & {
+ expiresAtMs: number;
+};
+
+export function stageFolder(
+ selected: NativeDocumentFolderSelection,
+): StagedFolder {
+ return {
+ ...selected,
+ expiresAtMs: selected.expiresAtMs ?? Date.now() + FOLDER_LEASE_TTL_MS,
+ };
+}
+
+/** Expired, or too close to it to survive the request. */
+export function isFolderExpired(folder: StagedFolder, now: number): boolean {
+ return folder.expiresAtMs - EXPIRY_GRACE_MS <= now;
+}
+
+/** Link folders picked before the project existed. A failed link toasts and never blocks creation. */
+export async function linkStagedFolders(
+ projectId: string,
+ folders: StagedFolder[],
+): Promise<void> {
+ if (folders.length === 0) return;
+ markProjectSourcesPending(projectId);
+ noteProjectWork(projectId, 1);
+ try {
+ for (const folder of folders) {
+ try {
+ if (isFolderExpired(folder, Date.now())) {
+ throw new Error(
+ "The selection expired. Link it again from the project.",
+ );
+ }
+ const { job } = await createLinkedFolder(
+ { type: "project", id: projectId },
+ folder.token,
+ folder.displayName,
+ );
+ watchProjectFolderJob(projectId, job.id);
+ } catch (error) {
+ toast.error(`Could not link ${folder.displayName}`, {
+ description: error instanceof Error ? error.message : String(error),
+ });
+ }
+ }
+ } finally {
+ announceProjectSourcesUpdated(projectId);
+ noteProjectWork(projectId, -1);
+ }
+}
diff --git a/studio/frontend/src/features/rag/components/linked-folders-manager.tsx b/studio/frontend/src/features/rag/components/linked-folders-manager.tsx
index c3d67209848..972368d5400 100644
--- a/studio/frontend/src/features/rag/components/linked-folders-manager.tsx
+++ b/studio/frontend/src/features/rag/components/linked-folders-manager.tsx
@@ -29,6 +29,12 @@ import { useState } from "react";
import type { FolderSyncJob, LinkedFolderScope } from "../types/rag";
import { useLinkedFolders } from "./use-linked-folders";
+/** Grouped folder card, shared with the create project dialog. */
+export const FOLDER_CARD_CLASS =
+ "overflow-hidden rounded-[16px] border border-border bg-background dark:border-transparent dark:bg-[rgb(255_255_255_/_calc(0.06*var(--contrast-wash-gain,1)))]";
+export const FOLDER_ROW_CLASS =
+ "flex min-w-0 items-center gap-3 px-3.5 py-2.5 border-t border-border/60 first:border-t-0 dark:border-[rgb(255_255_255_/_calc(0.08*var(--contrast-edge-gain,1)))]";
+
function percent(progress?: number | null): number | null {
if (progress == null || !Number.isFinite(progress)) return null;
return Math.max(0, Math.min(100, progress <= 1 ? progress * 100 : progress));
@@ -168,11 +174,10 @@ export function LinkedFoldersManager({
// One grouped card: the folders, then the row that adds another. A dialog has a name field
// above it in the same shape, so the two read as one stack rather than a panel inside a panel.
if (variant === "card") {
- const rowClass =
- "flex min-w-0 items-center gap-3 px-3.5 py-2.5 border-t border-border/60 first:border-t-0 dark:border-[rgb(255_255_255_/_calc(0.08*var(--contrast-edge-gain,1)))]";
+ const rowClass = FOLDER_ROW_CLASS;
return (
<section className="flex min-w-0 flex-col gap-2">
- <div className="overflow-hidden rounded-[16px] border border-border bg-background dark:border-transparent dark:bg-[rgb(255_255_255_/_calc(0.06*var(--contrast-wash-gain,1)))]">
+ <div className={FOLDER_CARD_CLASS}>
{manager.loading && manager.folders.length === 0 ? (
<div className={cn(rowClass, "text-ui-13 text-muted-foreground")}>
<Spinner className="size-4 shrink-0" />
diff --git a/studio/frontend/src/features/rag/components/project-folder-picker.tsx b/studio/frontend/src/features/rag/components/project-folder-picker.tsx
new file mode 100644
index 00000000000..b49de9b4551
--- /dev/null
+++ b/studio/frontend/src/features/rag/components/project-folder-picker.tsx
@@ -0,0 +1,165 @@
+// 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 { Button } from "@/components/ui/button";
+import { Spinner } from "@/components/ui/spinner";
+import {
+ pickNativeDocumentFolder,
+ useNativePathLeasesSupported,
+} from "@/features/native-intents";
+import { isTauri } from "@/lib/api-base";
+import { toast } from "@/lib/toast";
+import { cn } from "@/lib/utils";
+import {
+ Cancel01Icon,
+ FolderAddIcon,
+ FolderSyncIcon,
+} from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
+import {
+ type Dispatch,
+ type SetStateAction,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
+import { FOLDER_CARD_CLASS, FOLDER_ROW_CLASS } from "./linked-folders-manager";
+import { EXPIRY_GRACE_MS } from "./staged-source";
+import {
+ isFolderExpired,
+ type StagedFolder,
+ stageFolder,
+} from "./link-staged-folders";
+
+/** Folders to link once the project is created, in the edit dialog's card layout. */
+export function ProjectFolderPicker({
+ folders,
+ onChange,
+ disabled = false,
+ onPendingChange,
+}: {
+ folders: StagedFolder[];
+ onChange: Dispatch<SetStateAction<StagedFolder[]>>;
+ disabled?: boolean;
+ /** True while a pick is resolving, so Create waits for it. */
+ onPendingChange?: (pending: boolean) => void;
+}) {
+ const leasesSupported = useNativePathLeasesSupported();
+ const supported = isTauri && leasesSupported;
+ const [picking, setPicking] = useState(false);
+ const mounted = useRef(true);
+ useEffect(() => {
+ mounted.current = true;
+ return () => {
+ mounted.current = false;
+ };
+ }, []);
+
+ // Leases are short-lived: drop folders before they expire, as the dropzone does for drops.
+ useEffect(() => {
+ if (folders.length === 0) return;
+ const soonest = Math.min(...folders.map((f) => f.expiresAtMs));
+ const timer = setTimeout(
+ () => {
+ const expired = folders.filter((f) => isFolderExpired(f, Date.now()));
+ if (expired.length === 0) return;
+ onChange((current) => current.filter((f) => !expired.includes(f)));
+ toast.info(
+ expired.length === 1
+ ? "A picked folder expired"
+ : `${expired.length} picked folders expired`,
+ { description: "Add them again to link them." },
+ );
+ },
+ Math.max(0, soonest - EXPIRY_GRACE_MS - Date.now()),
+ );
+ return () => clearTimeout(timer);
+ }, [folders, onChange]);
+
+ async function add() {
+ if (!supported || picking || disabled) return;
+ setPicking(true);
+ onPendingChange?.(true);
+ try {
+ const selected = await pickNativeDocumentFolder();
+ // Closing the dialog unmounts this, so a late pick is dropped.
+ if (!selected || !mounted.current) return;
+ onChange((current) => [...current, stageFolder(selected)]);
+ } catch (error) {
+ toast.error("Could not add folder", {
+ description: error instanceof Error ? error.message : String(error),
+ });
+ } finally {
+ if (mounted.current) {
+ setPicking(false);
+ onPendingChange?.(false);
+ }
+ }
+ }
+
+ return (
+ <div className="space-y-2.5">
+ <p className="text-ui-15 font-medium text-foreground">Linked folders</p>
+ <div className={FOLDER_CARD_CLASS}>
+ {folders.map((folder) => (
+ <div key={folder.token} className={FOLDER_ROW_CLASS}>
+ <HugeiconsIcon
+ icon={FolderSyncIcon}
+ strokeWidth={1.75}
+ className="size-4 shrink-0 text-muted-foreground"
+ />
+ <span className="min-w-0 flex-1 truncate text-sm">
+ {folder.displayName}
+ </span>
+ <Button
+ type="button"
+ size="icon-sm"
+ variant="ghost"
+ className="shrink-0 rounded-full"
+ disabled={disabled}
+ aria-label={`Remove ${folder.displayName}`}
+ onClick={() =>
+ onChange((current) =>
+ current.filter((f) => f.token !== folder.token),
+ )
+ }
+ >
+ <HugeiconsIcon icon={Cancel01Icon} className="size-3.5" />
+ </Button>
+ </div>
+ ))}
+ <button
+ type="button"
+ disabled={!supported || picking || disabled}
+ onClick={() => void add()}
+ aria-label="Add folder"
+ title={
+ supported
+ ? "Choose a local folder"
+ : "Requires the managed desktop backend"
+ }
+ className={cn(
+ FOLDER_ROW_CLASS,
+ "w-full cursor-pointer py-3 text-left text-sm transition-colors hover:bg-muted/60 disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:bg-transparent",
+ )}
+ >
+ {picking ? (
+ <Spinner className="size-4 shrink-0" />
+ ) : (
+ <HugeiconsIcon
+ icon={FolderAddIcon}
+ strokeWidth={1.75}
+ className="size-4 shrink-0 text-muted-foreground"
+ />
+ )}
+ <span>Add folder</span>
+ </button>
+ </div>
+ <p className="px-1 text-ui-11 text-muted-foreground">
+ {supported
+ ? "Indexed and kept in sync, for every chat in this project to search."
+ : "Linking a folder needs the managed desktop backend."}
+ </p>
+ </div>
+ );
+}
diff --git a/studio/frontend/src/features/rag/index.ts b/studio/frontend/src/features/rag/index.ts
index e44bd988ebf..3abb6c7ea49 100644
--- a/studio/frontend/src/features/rag/index.ts
+++ b/studio/frontend/src/features/rag/index.ts
@@ -16,3 +16,8 @@ export { saveMarkdownAsProjectSource } from "./api/save-markdown-source";
export { useRagAvailabilityStore } from "./api/rag-availability";
export { isLinkedFolderManaged } from "./types/rag";
export type { KnowledgeBase, RagDocument, UploadedDocument } from "./types/rag";
+export {
+ linkStagedFolders,
+ type StagedFolder,
+} from "./components/link-staged-folders";
+export { ProjectFolderPicker } from "./components/project-folder-picker";
diff --git a/studio/src-tauri/src/native_intents.rs b/studio/src-tauri/src/native_intents.rs
index f6faeac9989..10a861bbc95 100644
--- a/studio/src-tauri/src/native_intents.rs
+++ b/studio/src-tauri/src/native_intents.rs
@@ -98,6 +98,7 @@ pub struct NativeIntent {
pub struct NativeDocumentFolderSelection {
token: String,
display_name: String,
+ expires_at_ms: u64,
}
#[derive(Default)]
@@ -243,6 +244,7 @@ impl NativeIntakeState {
Ok(NativeDocumentFolderSelection {
token: lease.native_path_lease,
display_name: lease.display_label,
+ expires_at_ms: lease.expires_at_ms,
})
}