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
// 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 { SidebarRowsRequest } from "../api/chat-api";
export const SIDEBAR_PAGE_SIZE = 20;
export type SidebarRowQuery = Omit<SidebarRowsRequest, "limit" | "cursor">;
export function sidebarRowQuery(
query: Partial<SidebarRowQuery>,
): SidebarRowQuery {
return {
archived: query.archived ?? false,
projectId: query.projectId ?? null,
unfiledOnly: query.unfiledOnly ?? false,
includeIds: query.includeIds ?? null,
excludeIds: [...new Set(query.excludeIds ?? [])].sort(),
sort: query.sort ?? "updated",
manualOrder: query.sort === "manual" ? (query.manualOrder ?? []) : null,
};
}
export function sidebarRowQueryKey(query: SidebarRowQuery): string {
return JSON.stringify(query);
}
interface QueryRow {
id: string;
projectId?: string | null;
threadIds?: string[];
}
/** The server's filter and order on the client, for a server without the paged route. */
export function applySidebarRowQuery<T extends QueryRow>(
rows: readonly T[],
query: SidebarRowQuery,
): T[] {
const include = query.includeIds ? new Set(query.includeIds) : null;
const exclude = new Set(query.excludeIds);
const out = rows.filter((row) => {
if (include && !include.has(row.id) && !row.threadIds?.some((id) => include.has(id))) {
return false;
}
if (exclude.has(row.id)) return false;
if (
query.projectId !== null &&
(row.projectId ?? null) !== query.projectId
) {
return false;
}
if (query.unfiledOnly && row.projectId) return false;
return true;
});
if (query.sort !== "manual" || !query.manualOrder?.length) return out;
const rank = new Map(query.manualOrder.map((id, index) => [id, index]));
return out.sort((a, b) => (rank.get(a.id) ?? -1) - (rank.get(b.id) ?? -1));
}
/** `next` orders the loaded rows; rows of `full` it omits keep their places after them. */
export function mergeManualOrderTail(
next: readonly string[],
full: readonly string[],
): string[] {
const seen = new Set(next);
const out = [...next];
for (const id of full) {
if (!seen.has(id)) {
seen.add(id);
out.push(id);
}
}
return out;
}
/** A manual list whose chats are still paging draws only what comes before its last loaded chat: a
* folder or page ranked later waits for the page that reaches it instead of sitting above chats
* that have not loaded. Unranked folders lead, as `applyManualOrder` sorts them. */
export function holdRowsPastLoadedChats<T extends { id: string; kind: string }>(
rows: readonly T[],
order: readonly string[] | undefined,
hasMore: boolean,
): T[] {
if (!hasMore) return [...rows];
const rank = new Map((order ?? []).map((id, index) => [id, index]));
const key = (row: T) => rank.get(row.id) ?? -1;
const chats = rows.filter((row) => row.kind === "chat");
const edge = chats.length ? key(chats[chats.length - 1]) : -1;
return rows.filter(
(row) =>
row.kind === "chat" || (key(row) === -1 && row.kind === "project") || key(row) < edge,
);
}
/** The order a drag saves once a paged list's ids are all known: the dragged rows, then the
* unloaded chats with no rank (they sort first), then everything else where it was, so a row
* held back past the loaded chats keeps its place among them. */
export function settledManualOrder(
dragged: readonly string[],
all: readonly string[],
current: readonly string[],
): string[] {
const ranked = new Set(current);
return mergeManualOrderTail(
mergeManualOrderTail(dragged, all.filter((id) => !ranked.has(id))),
current,
);
}