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.

8.3 KB
Raw Blame
// 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 { sandboxSessionIdFor } from "@/components/assistant-ui/sandbox-files";
import { revealSandbox } from "@/components/assistant-ui/sandbox-reveal";
import {
  MAX_SANDBOX_UPLOAD_BYTES,
  uploadToSandbox,
} from "@/components/assistant-ui/sandbox-upload";
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { Upload01Icon } from "@hugeicons/core-free-icons";
import { FolderOpenIcon } from "@/lib/folder-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { useT } from "@/i18n";
import { isTauri } from "@/lib/api-base";
import { openFilePicker } from "@/lib/open-file-picker";
import { toast } from "@/lib/toast";
import { useState, type ComponentProps, type ComponentType } from "react";
import type { SidebarItem } from "../hooks/use-chat-sidebar-items";
import { getSidebarItemThreadIds, sandboxSessionIdsHolding } from "./chat-row-menu";

/** A 404 means no folder yet (made on a tool's first write), not a failure. */
function reportRevealError(
  error: unknown,
  t: ReturnType<typeof useT>,
  kind: "chat" | "project",
): void {
  if ((error as { status?: number } | null)?.status === 404) {
    toast.info(t(`library.chats.folder.${kind}Missing` as const), {
      description: t("library.chats.folder.missingDescription"),
    });
    return;
  }
  toast.error(t(`library.chats.folder.${kind}Failed` as const), {
    description: error instanceof Error ? error.message : String(error),
  });
}

/** "Open chat folder" (desktop only). Compare chats outside a project get no item. */
/** None for a compare chat outside a project. */
export function chatFolderSessionId(item: SidebarItem): string | undefined {
  return item.type === "single" || item.projectId
    ? sandboxSessionIdFor(getSidebarItemThreadIds(item)[0] ?? item.id, item.projectId)
    : undefined;
}

export function OpenChatFolderItem({
  item,
  Item = DropdownMenuItem,
}: {
  item: SidebarItem;
  Item?: ComponentType<ComponentProps<typeof DropdownMenuItem>>;
}) {
  const t = useT();
  const threadIds = getSidebarItemThreadIds(item);
  const sandboxSessionId = chatFolderSessionId(item);
  if (!sandboxSessionId) return null;
  if (!isTauri) return <OpenChatFolderUnavailableItem Item={Item} />;
  return (
    <Item
      title={t("library.chats.folder.openChatTitle")}
      onSelect={() => {
        void (async () => {
          try {
            // Read from history: a chat moved between projects keeps its old sandbox.
            const ids = threadIds.length > 0 ? threadIds : [item.id];
            const distinct = await sandboxSessionIdsHolding(ids);
            if (distinct.length > 1) {
              toast.error(t("library.chats.folder.manyFolders"), {
                description: t("library.chats.folder.manyFoldersDescription"),
              });
              return;
            }
            await revealSandbox(distinct[0] ?? sandboxSessionId);
          } catch (error) {
            reportRevealError(error, t, "chat");
          }
        })();
      }}
    >
      <HugeiconsIcon icon={FolderOpenIcon} strokeWidth={1.75} className="size-icon" />
      <span>{t("library.chats.folder.openChat")}</span>
    </Item>
  );
}

/**
 * "Upload to chat folder": files go to the folder the chat's next tool call runs in, without
 * entering the conversation. The bytes travel to the backend, so a browser session can use it too.
 */
export function UploadToChatFolderItem({
  item,
  Item = DropdownMenuItem,
}: {
  item: SidebarItem;
  Item?: ComponentType<ComponentProps<typeof DropdownMenuItem>>;
}) {
  const t = useT();
  const sandboxSessionId = chatFolderSessionId(item);
  if (!sandboxSessionId) return null;
  return (
    <Item
      title={t("library.chats.folder.uploadTitle")}
      onSelect={() =>
        openFilePicker("*", (files) => void uploadToChatFolder(sandboxSessionId, files, t))
      }
    >
      <HugeiconsIcon icon={Upload01Icon} strokeWidth={1.75} className="size-icon" />
      <span>{t("library.chats.folder.upload")}</span>
    </Item>
  );
}

async function uploadToChatFolder(
  sessionId: string,
  files: File[],
  t: ReturnType<typeof useT>,
  overwrite = false,
): Promise<void> {
  const added: string[] = [];
  const taken: File[] = [];
  for (const file of files) {
    const failed = (description: string) =>
      toast.error(t("library.chats.folder.uploadFailed", { name: file.name }), { description });
    if (file.size > MAX_SANDBOX_UPLOAD_BYTES) {
      failed(t("library.toast.uploadTooLarge", { name: file.name }));
      continue;
    }
    try {
      added.push((await uploadToSandbox(sessionId, file, overwrite)).name);
    } catch (error) {
      // A 409 on a replace is a folder holding the name: offering Replace again would loop.
      if (!overwrite && (error as { status?: number } | null)?.status === 409) taken.push(file);
      else failed(error instanceof Error ? error.message : String(error));
    }
  }
  if (added.length > 0) {
    toast.success(
      added.length === 1
        ? t("library.chats.folder.uploaded", { name: added[0] })
        : t("library.chats.folder.uploadedMany", { count: added.length }),
      { description: t("library.chats.folder.uploadedDescription") },
    );
  }
  if (taken.length > 0) {
    toast.info(
      taken.length === 1
        ? t("library.chats.folder.uploadExists", { name: taken[0].name })
        : t("library.chats.folder.uploadExistsMany", { count: taken.length }),
      {
        action: {
          label: t("library.chats.folder.replace"),
          onClick: () => void uploadToChatFolder(sessionId, taken, t, true),
        },
      },
    );
  }
}

export function OpenProjectFolderItem({
  projectId,
  Item = DropdownMenuItem,
}: {
  projectId: string;
  Item?: ComponentType<ComponentProps<typeof DropdownMenuItem>>;
}) {
  const t = useT();
  const sandboxSessionId = sandboxSessionIdFor(undefined, projectId);
  if (!sandboxSessionId) return null;
  if (!isTauri) {
    return (
      <OpenChatFolderUnavailableItem
        Item={Item}
        label={t("library.chats.folder.openProject")}
        hint={t("library.chats.folder.projectHint")}
      />
    );
  }
  return (
    <Item
      title={t("library.chats.folder.openProjectTitle")}
      onSelect={() => {
        void revealSandbox(sandboxSessionId).catch((error: unknown) =>
          reportRevealError(error, t, "project"),
        );
      }}
    >
      <HugeiconsIcon icon={FolderOpenIcon} strokeWidth={1.75} className="size-icon" />
      <span>{t("library.chats.folder.openProject")}</span>
    </Item>
  );
}

/**
 * Browser-session variant. Radix `disabled` blocks pointer events, so the row stays enabled,
 * refuses select itself, and drives a controlled tooltip; `title` covers screen readers.
 */
export function OpenChatFolderUnavailableItem({
  // The sidebar renders this into its context menu, a different Radix set.
  Item = DropdownMenuItem,
  label,
  hint: hintOverride,
}: {
  Item?: ComponentType<ComponentProps<typeof DropdownMenuItem>>;
  label?: string;
  hint?: string;
} = {}) {
  const t = useT();
  const [hintOpen, setHintOpen] = useState(false);
  const hint = hintOverride ?? t("library.chats.folder.chatHint");

  return (
    <Item
      aria-disabled={true}
      title={hint}
      className="relative opacity-50"
      onSelect={(event) => {
        event.preventDefault();
        setHintOpen(true);
      }}
      onPointerEnter={() => setHintOpen(true)}
      onPointerLeave={() => setHintOpen(false)}
      onFocus={() => setHintOpen(true)}
      onBlur={() => setHintOpen(false)}
    >
      <HugeiconsIcon icon={FolderOpenIcon} strokeWidth={1.75} className="size-icon" />
      <span>{label ?? t("library.chats.folder.openChat")}</span>
      <Tooltip open={hintOpen}>
        {/* Our wrapper, not the raw primitive: it registers the trigger element,
            without which the tooltip counts itself blocked by the open menu. */}
        <TooltipTrigger asChild={true}>
          <span
            aria-hidden={true}
            className="pointer-events-none absolute inset-y-0 right-0 w-0"
          />
        </TooltipTrigger>
        <TooltipContent side="right" className="max-w-[calc(220px*var(--ui-space-scale,1))]">
          {hint}
        </TooltipContent>
      </Tooltip>
    </Item>
  );
}