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.

Trim comments in studio/frontend chat, model picker and media features (#10114)

Daniel Han75809358056f
diff --git a/studio/frontend/src/features/audio/audio-page-policy.ts b/studio/frontend/src/features/audio/audio-page-policy.ts
index b1f489caf77..375641491bc 100644
--- a/studio/frontend/src/features/audio/audio-page-policy.ts
+++ b/studio/frontend/src/features/audio/audio-page-policy.ts
@@ -25,7 +25,7 @@ export type AudioGenerationPresentation = {
};
/** Project request-lifetime phases into truthful UI copy. Audio generation has no
- * browser-visible numeric progress, so these labels never imply a fraction or ETA. */
+ * browser-visible numeric progress, so these labels never imply a fraction or ETA. */
export function audioGenerationPresentation(
phase: AudioGenerationPhase,
): AudioGenerationPresentation | null {
@@ -191,9 +191,9 @@ export interface SttDownloadedArtifact {
engine: SttEngine;
}
-/** Engine-qualified picker artifacts for every locally loadable checkpoint. Whisper
- * uses one short key for distinct Transformers and whisper.cpp downloads, so engine
- * provenance must survive this boundary. */
+/** Engine-qualified picker artifacts for every locally loadable checkpoint. Whisper uses one
+ * short key for distinct Transformers and whisper.cpp downloads, so engine provenance must
+ * survive this boundary. */
export function sttDownloadedArtifacts(
status: SttDownloadedStatus,
repoIdForSidecarKey: (sidecarKey: string, engine: SttEngine) => string,
@@ -218,9 +218,8 @@ export function sttDownloadedArtifacts(
return artifacts;
}
-/** Catalog contracts win; uncurated ASR rows route from the inventory task in picker
- * metadata. Unknown and TTS-tagged community repos keep the main-slot path for
- * load-time capability validation. */
+/** Catalog contracts win; uncurated ASR rows route from the inventory task in picker metadata.
+ * Unknown and TTS-tagged community repos keep the main-slot path for load-time validation. */
export function resolveAudioPickTask(
catalogTask: AudioPickTask,
pipelineTag?: string | null,
@@ -231,8 +230,8 @@ export function resolveAudioPickTask(
);
}
-/** Generation can be cancelled as part of a mode transition. Model lifecycle
- * and transcription operations must settle before their controls disappear. */
+/** Generation can be cancelled as part of a mode transition. Model lifecycle and transcription
+ * operations must settle before their controls disappear. */
export function canTransitionAudioMode(
busy: AudioBusy,
generationPhase: AudioGenerationPhase = busy === "generating"
@@ -244,9 +243,9 @@ export function canTransitionAudioMode(
);
}
-/** A managed TTS completion owns auto-load only while the same staging generation is
- * selected in Speak. Downloads continue globally after ownership changes, but their
- * completion must not mutate the main slot. */
+/** A managed TTS completion owns auto-load only while the same staging generation is selected
+ * in Speak. Downloads continue globally after ownership changes, but their completion must
+ * not mutate the main slot. */
export function stagedTtsLoadIsOwned(
pendingGeneration: number | null,
currentGeneration: number,
@@ -259,8 +258,8 @@ export function stagedTtsLoadIsOwned(
);
}
-/** Cached rows sometimes know only the quant label; remote staging always
- * supplies the exact filename. Both are valid backend GGUF selectors. */
+/** Cached rows sometimes know only the quant label; remote staging always supplies the exact
+ * filename. Both are valid backend GGUF selectors. */
export function exactGgufLoadSelector(
meta: Pick<ModelSelectorChangeMeta, "ggufFilename" | "ggufVariant">,
): string | null {
@@ -298,8 +297,8 @@ export function isGgufTtsTarget({
export type MacTtsPickAction = "allow" | "use-gguf-sibling" | "reject";
-/** MLX has no codec TTS decoder. Curated native PyTorch audio models bypass MLX;
- * other Mac picks still need GGUF or a family GGUF sibling. */
+/** MLX has no codec TTS decoder. Curated native PyTorch audio models bypass MLX; other Mac
+ * picks still need GGUF or a family GGUF sibling. */
export function macTtsPickAction({
isMac,
isGguf,
@@ -324,9 +323,8 @@ export interface AutoGgufVariant {
partial?: boolean;
}
-/** Prefer a complete cached quant, then the repo's declared default, then the first
- * exact file: instant Mac fallback when a runnable sibling is present, deterministic
- * otherwise. */
+/** Prefer a complete cached quant, then the repo's declared default, then the first exact file:
+ * instant Mac fallback when a runnable sibling is present, deterministic otherwise. */
export function selectAutoGgufVariant<T extends AutoGgufVariant>(
variants: readonly T[],
defaultVariant: string | null | undefined,
@@ -360,13 +358,10 @@ export function expectedGgufDownloadBytes(variant: AutoGgufVariant): number {
: variant.size_bytes;
}
-/** Fold a freshly fetched first page into the list already on screen.
- *
- * The page is authoritative for the newest `page.length` clips and any scrollback below
- * it is kept; replacing outright collapsed a paginated History on every delete and
- * generate, and reselected a different clip. `removedId` drops a clip this client just
- * deleted, which the page can no longer report. `hasMore` is the page's own report of
- * whether the server holds anything older. */
+/** Fold a freshly fetched first page into the list already on screen. The page is authoritative
+ * for the newest `page.length` clips and any scrollback below it is kept; replacing outright
+ * collapsed a paginated History on every delete and reselected a different clip.
+ * `removedId` drops a clip this client just deleted; `hasMore` is the server's own report. */
export function mergeGalleryPage<T extends { id: string }>(
page: readonly T[],
cached: readonly T[],
@@ -376,19 +371,18 @@ export function mergeGalleryPage<T extends { id: string }>(
const inPage = new Set(page.map((clip) => clip.id));
// An empty page means the server holds nothing: a clear from anywhere, not scrollback.
if (page.length === 0) return { clips: [], stitched: false };
- // A complete first page IS everything the server holds, so there is no scrollback to
- // keep: a cached clip below it was deleted by another client or pruned by the size cap,
- // and stitching it back rendered a row that could never be played again.
+ // A complete first page IS everything the server holds, so there is no scrollback to keep: a
+ // cached clip below it was deleted by another client or pruned by the size cap, and
+ // stitching it back rendered a row that could never be played again.
if (hasMore === false) return { clips: [...page], stitched: false };
- // The page is authoritative over the window it covers, so a cached clip inside that
- // window and absent from the page was deleted by another client and must go. Only what
- // sits BELOW the page's oldest entry is scrollback. Keying on the position of that
- // entry, since a clip record carries no cursor of its own.
+ // The page is authoritative over the window it covers, so a cached clip inside that window and
+ // absent from the page was deleted by another client and must go. Only what sits BELOW the
+ // page's oldest entry is scrollback, keyed on that entry's position.
const oldestInPage = cached.findIndex(
(clip) => clip.id === page[page.length - 1].id,
);
- // Without that boundary the cache cannot prove where safe scrollback begins: an external archive
- // can shift one unseen row into the page while every earlier row still overlaps.
+ // Without that boundary the cache cannot prove where safe scrollback begins: an external
+ // archive can shift one unseen row into the page while every earlier row still overlaps.
if (oldestInPage === -1 && cached.length > 0) {
return { clips: [...page], stitched: false };
}
@@ -399,8 +393,7 @@ export function mergeGalleryPage<T extends { id: string }>(
return { clips: [...page, ...tail], stitched: tail.length > 0 };
}
-/** Match the gallery record returned by this generation, never another
- * client's concurrently persisted clip. */
+/** Match the gallery record returned by this generation, never another client's concurrently persisted clip. */
export function persistedClipForGeneration<T extends { id: string }>(
clipId: string | null | undefined,
refreshed: readonly T[],
@@ -434,10 +427,10 @@ type SttResidencyStatus = SttEngineResidency & {
mtmd?: SttEngineResidency;
};
-/** Resolve the resident model from the engine-aware status shape. The legacy top-level
- * fields mirror Transformers only, so reading them for Qwen3-ASR or whisper.cpp clears a
- * model that is actually ready. While the selected engine is pending, do not let an
- * older model on another engine steal the selector. */
+/** Resolve the resident model from the engine-aware status shape. The legacy top-level fields
+ * mirror Transformers only, so reading them for Qwen3-ASR or whisper.cpp clears a model that
+ * is actually ready. While the selected engine is pending, an older model on another engine
+ * must not steal the selector. */
export function resolveSttLoadedModel(
status: SttResidencyStatus,
selectedEngine: SttEngine | null,
@@ -453,19 +446,17 @@ export interface SttResidency {
engine: SttEngine;
}
-/** Resolve model and owning engine together; equal Whisper short keys do not
- * identify which runtime is resident. */
+/** Resolve model and owning engine together; equal Whisper short keys do not identify which runtime is resident. */
export function resolveSttResidency(
status: SttResidencyStatus,
selectedEngine: SttEngine | null,
preserveSelected: boolean,
): SttResidency | null {
- // A whisper.cpp pick on a host without whisper-server is deliberately served, and
- // loaded, through Transformers, so its residency lives in that block. Same fallback
- // sttEngineStatusFor applies; the engine reported stays the selected one, because that
- // is what the user picked and what the backend routes. Without this the refresh that
- // completes the load found nothing (it runs while preserveSelected is true) and the
- // Transcribe controls stayed disabled until the page was left and revisited.
+ // A whisper.cpp pick on a host without whisper-server is deliberately served through
+ // Transformers, so its residency lives in that block. The engine reported stays the selected
+ // one, since that is what the user picked and what the backend routes; the same
+ // sttEngineStatusFor fallback applies. Without this the refresh completing the load found
+ // nothing, since it runs while preserveSelected is true, and Transcribe stayed disabled.
const selectedStatus =
selectedEngine === "transformers" ||
(selectedEngine === "gguf" && status.gguf?.available === false)
@@ -492,8 +483,8 @@ export function resolveSttResidency(
return null;
}
-/** Reconcile the picker selection with the sidecar's authoritative status.
- * Preserve a selection only while its load/download is genuinely pending. */
+/** Reconcile the picker selection with the sidecar's authoritative status. Preserve a selection
+ * only while its load/download is genuinely pending. */
export function reconcileSttSelection({
selectedRepo,
loadedModel,
@@ -526,8 +517,8 @@ export function reconcileSttSelection({
return preservePending ? selectedRepo : null;
}
-/** Permission prompts cannot be aborted, so freshness is checked immediately
- * after getUserMedia resolves and stale streams are stopped before recording. */
+/** Permission prompts cannot be aborted, so freshness is checked immediately after
+ * getUserMedia resolves and stale streams are stopped before recording. */
export function micStreamRequestIsCurrent(
requestGeneration: number,
currentGeneration: number,
diff --git a/studio/frontend/src/features/audio/audio-page.tsx b/studio/frontend/src/features/audio/audio-page.tsx
index 37634e0f283..3ada13607d2 100644
--- a/studio/frontend/src/features/audio/audio-page.tsx
+++ b/studio/frontend/src/features/audio/audio-page.tsx
@@ -1,9 +1,8 @@
// 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 Audio page: Generate (TTS via the main inference slot) and Transcribe (STT
-// via the dictation sidecar). Training lives on the Train page. The page stays
-// mounted across tab switches (see __root.tsx), so `active` gates polling,
+// The Audio page: Generate (TTS via the main inference slot) and Transcribe (STT via the
+// dictation sidecar). The page stays mounted across tab switches, so `active` gates polling,
// popovers and the recorder rather than lifecycle.
import { TestTubeOutlineIcon } from "@/lib/hugeicons-derived";
@@ -160,8 +159,8 @@ const MODELS_BY_MODE: Record<CreateMode, ModelOption[]> = {
transcribe: audioModelsForTask("stt"),
};
-/** What to call a model on screen. A Hub repo is its id; a checkpoint trained here
- * is an output directory, and the full path in a toast reads as a bug. */
+/** What to call a model on screen. A Hub repo is its id; a checkpoint trained here is an output
+ * directory, and the full path in a toast reads as a bug. */
function audioModelLabel(id: string): string {
if (!/^(?:[a-zA-Z]:[\\/]|[\\/]|~)/.test(id)) return id;
const leaf = id.split(/[\\/]/).filter(Boolean).pop() ?? id;
@@ -183,23 +182,21 @@ const HUB_TASKS_BY_MODE = {
const PAGE_SIZE = 50;
// The list route clamps a page to 200, so asking for more silently gets 200 back.
const MAX_PAGE_SIZE = 200;
-// Mirrors the STT sidecar's own limits (_MAX_AUDIO_SECONDS, STT_AUDIO_B64_MAX_CHARS), so a
-// recording is stopped at the boundary rather than uploaded and refused.
+// Mirrors the STT sidecar's own limits, so a recording is stopped at the boundary rather than uploaded and refused.
+// The two it mirrors are _MAX_AUDIO_SECONDS and STT_AUDIO_B64_MAX_CHARS.
const RECORDING_MAX_SECONDS = 30 * 60;
-// STT_AUDIO_RAW_MAX_BYTES in utils/upload_limits.py. A larger client cap let a dense codec
-// build a recording the raw route then refused with 413.
+// STT_AUDIO_RAW_MAX_BYTES in utils/upload_limits.py. A larger client cap let a dense codec build
+// a recording the raw route then refused with 413.
const RECORDING_MAX_BYTES = 25 * 1024 * 1024;
const RECORDING_CHUNK_MS = 1000;
const TTS_MAX_TOKENS = 8192;
-// Max tokens caps the OUTPUT, and the prompt's own tokens sit in the same context window, so
-// loading at exactly TTS_MAX_TOKENS made the advertised maximum unreachable for any nonempty
-// prompt. Load with room for both.
+// Max tokens caps the OUTPUT and the prompt's own tokens sit in the same context window, so
+// loading at exactly TTS_MAX_TOKENS made the advertised maximum unreachable.
const TTS_PROMPT_CONTEXT_RESERVE = 2048;
// WAV clips run a few MB a minute; 64 MB keeps a healthy scrollback resident.
const CLIP_BLOB_BUDGET_BYTES = 64 * 1024 * 1024;
-// Module scope so a tab switch re-renders the gallery instantly (the page stays mounted, but a
-// remount after an unlikely unmount still reuses fetched clips).
+// Module scope so a tab switch re-renders the gallery instantly.
const galleryCache: {
clips: AudioGalleryClip[];
hasMore: boolean;
@@ -249,8 +246,8 @@ function Field({
);
}
-/** Per-row actions for a history clip, in a dots menu so rows keep one line.
- * Mirrors the model rows' MoreVertical pattern. */
+/** Per-row actions for a history clip, in a dots menu so rows keep one line. Mirrors the model
+ * rows' MoreVertical pattern. */
function ClipRowMenu({
clip,
onDownload,
@@ -273,8 +270,8 @@ function ClipRowMenu({
type="button"
onClick={(event) => event.stopPropagation()}
aria-label={`Actions for ${clip.prompt || "clip"}`}
- // Hidden until the row is hovered or the menu is open, so a long list
- // stays quiet; keyboard focus reveals it too.
+ // Hidden until the row is hovered or the menu is open, so a long list stays quiet; keyboard
+ // focus reveals it too.
className="flex size-5 shrink-0 items-center justify-center rounded-md text-muted-foreground/60 opacity-0 transition-colors hover:bg-black/5 hover:text-foreground focus-visible:opacity-100 group-hover:opacity-100 data-[state=open]:opacity-100 dark:hover:bg-white/10"
>
<HugeiconsIcon
@@ -344,16 +341,15 @@ export function AudioPage({
);
const generationPresentation = audioGenerationPresentation(generationPhase);
- // --- TTS (main inference slot) -----------------------------------------
const [status, setStatus] = useState<InferenceStatusResponse | null>(null);
const [prompt, setPrompt] = useState("");
const [audioInstructions, setAudioInstructions] = useState("");
const [audioLanguage, setAudioLanguage] = useState("");
const [temperature, setTemperature] = useState(0.6);
// Sending temperature unconditionally puts it in the request's model_fields_set, which the
- // backend reads as an explicit client override and which then beats the per-model
- // recommendation (Spark-TTS wants 0.8, OuteTTS 0.4). Only send it once the user has moved
- // the slider, so an untouched page gets the model's own sampling.
+ // backend reads as an explicit client override that beats the per-model recommendation, so
+ // only send it once the user has moved the slider.
+ // Spark-TTS wants 0.8, OuteTTS 0.4, so only send it once the user has moved the slider.
const [temperatureEdited, setTemperatureEdited] = useState(false);
const handleTemperatureChange = useCallback((value: number) => {
setTemperatureEdited(true);
@@ -395,7 +391,6 @@ export function AudioPage({
requestStarted: boolean;
} | null>(null);
- // --- STT (dictation sidecar) -------------------------------------------
const [selectedSttRepo, setSelectedSttRepo] = useState<string | null>(null);
const [sttLoadedModel, setSttLoadedModel] = useState<string | null>(null);
const [sttLoadedEngine, setSttLoadedEngine] = useState<
@@ -409,16 +404,14 @@ export function AudioPage({
const [transcriptError, setTranscriptError] = useState<string | null>(null);
const [isRecording, setIsRecording] = useState(false);
const [micRequestPending, setMicRequestPending] = useState(false);
- /** Safari and other WebKit builds ship no MediaRecorder, and an http LAN origin
- * (`-H 0.0.0.0`) is not a secure context, so navigator.mediaDevices is undefined.
- * Same check the chat composer gates its microphone on. */
+ /** Safari and other WebKit builds ship no MediaRecorder, and an http LAN origin is not a secure
+ * context, so navigator.mediaDevices is undefined. Same check the chat composer uses. */
const recordingSupported = useMemo(
() => StudioModelDictationAdapter.isSupported(),
[],
);
- /** Audio the server produced that the gallery is not showing yet: either it could not
- * be persisted at all, or it was persisted and this refresh missed it. Kept so the
- * generation is playable either way. Cleared once its real clip lands. */
+ /** Audio the server produced that the gallery is not showing yet: either it could not be
+ * persisted, or this refresh missed it. Kept so the generation is playable either way. */
const [fallbackClip, setFallbackClip] = useState<{
url: string;
prompt: string;
@@ -441,15 +434,11 @@ export function AudioPage({
const sttStatusRefreshGeneration = useRef(0);
const sttLoadGeneration = useRef(0);
const sttLoadingGeneration = useRef<number | null>(null);
- // Residency is not ownership: the activation resync adopts whatever a sidecar already
- // holds, including a model chat dictation loaded, and that must survive a mode switch.
- // The identity, not a boolean: another surface can replace the sidecar's model while
- // Audio is inactive, and a bare flag then claimed that replacement too, so Eject
- // unloaded a model this page never loaded. Keyed on the model alone, deliberately: a
- // "gguf" pick on a host without whisper-server is served by the Transformers fallback
- // and comes back resident under that engine, so comparing the REQUESTED engine never
- // matched and leaked the sidecar instead. The registry keeps one model resident, so the
- // model id identifies it; the unload call resolves the serving engine server-side.
+ // Residency is not ownership: the activation resync adopts whatever a sidecar already holds,
+ // including a model chat dictation loaded. The identity, not a boolean, since another
+ // surface can replace the sidecar's model while Audio is inactive and a bare flag would claim
+ // that too. Keyed on the model alone, since a "gguf" pick without whisper-server comes back
+ // resident under the Transformers fallback.
const sttLoadedByThisPage = useRef<string | null>(null);
const sttLoadAbort = useRef<AbortController | null>(null);
const deferredSttLoad = useRef<{
@@ -480,8 +469,8 @@ export function AudioPage({
recordStreamRef.current = null;
}, []);
const stopAndDiscardRecording = useCallback(() => {
- // Also invalidates a getUserMedia request that has not resolved yet. Its
- // eventual stream is stopped before a MediaRecorder can be created.
+ // Also invalidates a getUserMedia request that has not resolved yet; its eventual stream is
+ // stopped before a MediaRecorder can be created.
micRequestGeneration.current += 1;
micPendingGeneration.current = null;
setMicRequestPending(false);
@@ -495,7 +484,6 @@ export function AudioPage({
stopRecordStream();
}, [stopRecordStream]);
- // --- Gallery ------------------------------------------------------------
const [clips, setClips] = useState<AudioGalleryClip[]>(galleryCache.clips);
const [hasMore, setHasMore] = useState(galleryCache.hasMore);
const [selectedId, setSelectedId] = useState<string | null>(
@@ -544,8 +532,8 @@ export function AudioPage({
setStatus(next);
} catch {
if (generation !== ttsStatusRefreshGeneration.current) return;
- // Do not leave Generate enabled against residency the backend can no
- // longer confirm. A later refresh adopts the recovered runtime.
+ // Do not leave Generate enabled against residency the backend can no longer confirm. A later
+ // refresh adopts the recovered runtime.
setStatus(null);
}
}, []);
@@ -612,8 +600,8 @@ export function AudioPage({
repoIdForSidecarKey: sttRepoIdForSidecarKey,
engineForRepo: sttEngineForRepoId,
});
- // Shared sidecar: an adopted model strands the previous pick's transcript
- // under it. null is the 5-minute idle unload, which must not delete it.
+ // Shared sidecar: an adopted model strands the previous pick's transcript under it. null is the
+ // 5-minute idle unload, which must not delete it.
if (reconciled !== null && reconciled !== selectedSttRepoRef.current)
clearTranscript();
selectedSttRepoRef.current = reconciled;
@@ -655,11 +643,9 @@ export function AudioPage({
forget();
return;
}
- // Forget only once the sidecar is actually released: clearing first left a failed
- // unload with the model in VRAM, no selection, and no Eject to retry with.
- // Scoped to the model this page claimed: `owned` was decided here, and another
- // surface can switch the same engine before the request lands, so an unscoped
- // unload would tear down a model this page never owned.
+ // Forget only once the sidecar is actually released: clearing first left a failed unload with the
+ // model in VRAM and no Eject to retry with. Scoped to the model this page claimed, since
+ // another surface can switch the same engine before the request lands.
await unloadSttModel(sttEngineForRepoId(selected), claim);
forget();
await refreshSttStatus();
@@ -676,8 +662,8 @@ export function AudioPage({
const load = (async () => {
try {
const fetched = await fetchClipObjectUrl(clip.url);
- // A delete can finish while protected bytes are in flight. Do not revive its
- // cache entry after the row is already gone.
+ // A delete can finish while protected bytes are in flight. Do not revive its cache entry after
+ // the row is already gone.
if (!galleryCache.clips.some((candidate) => candidate.id === clip.id)) {
URL.revokeObjectURL(fetched.url);
return;
@@ -714,9 +700,8 @@ export function AudioPage({
const page = await listAudioGallery(0, asked);
// The caller's own fetch: a generation whose clip persisted must not be told otherwise.
if (generation !== galleryRefreshGeneration.current) return page.audio;
- // A window past the route's cap cannot be covered in one page, and stitching the old
- // scrollback back on keeps a cursor that starts BELOW it, stranding whatever was restored
- // into the gap. Reset to what was fetched: shorter scrollback, nothing unreachable.
+ // A window past the route's cap cannot be covered in one page, and stitching the old scrollback
+ // back on keeps a cursor that starts BELOW it, stranding whatever was restored.
const { clips: merged, stitched } =
wanted > asked
? { clips: [...page.audio], stitched: false }
@@ -737,9 +722,8 @@ export function AudioPage({
}
setClips(merged);
setHasMore(galleryCache.hasMore);
- // The response audio was kept only until its record showed up. Left mounted, deleting
- // the now-visible clip made the "saved, waiting for the gallery" copy reappear from a
- // stale data URL as though the delete had not happened.
+ // The response audio was kept only until its record showed up: left mounted, deleting the
+ // now-visible clip made the "saved, waiting for the gallery" copy reappear.
if (
fallbackClipRef.current &&
galleryCache.selectedId &&
@@ -776,8 +760,8 @@ export function AudioPage({
);
const loadMore = useCallback(async () => {
- // Repeated scroll events near the bottom would otherwise each fire with the
- // same offset and append the same page, duplicating clips and React keys.
+ // Repeated scroll events near the bottom would otherwise each fire with the same offset and
+ // append the same page, duplicating clips and React keys.
if (loadingMoreRef.current) return;
loadingMoreRef.current = true;
const refreshGeneration = galleryRefreshGeneration.current;
@@ -844,9 +828,8 @@ export function AudioPage({
refreshSttStatus,
]);
- // Activation alone is not enough: the loaded-models indicator can eject the TTS model
- // out from under a page that stays active, leaving Generate enabled against an empty
- // slot. Same reason Images and Video listen here.
+ // Activation alone is not enough: the loaded-models indicator can eject the TTS model out from
+ // under a page that stays active, leaving Generate enabled against an empty slot.
useEffect(() => {
if (!active) return;
return subscribeModelLifecycle(({ runtime, loading }) => {
@@ -876,9 +859,9 @@ export function AudioPage({
if (clip) void ensureClipSrc(clip);
}, [clips, selectedId, ensureClipSrc]);
- /** `keepFallback` is for the one case where the id is not in `clips` yet: the server
- * persisted the clip but this refresh missed it, so the response audio has to stay
- * mounted or the player falls through to the empty state. */
+ /** `keepFallback` is for the one case where the id is not in `clips` yet: the server persisted the
+ * clip but this refresh missed it, so the response audio has to stay mounted or the player
+ * falls through to the empty state. */
const selectClip = useCallback(
(id: string, keepFallback = false) => {
galleryCache.selectedId = id;
@@ -890,25 +873,22 @@ export function AudioPage({
[ensureClipSrc],
);
- // --- Model selection ----------------------------------------------------
const loadTtsModel = useCallback(
async (
repoId: string,
ggufFilename?: string | null,
- // Where the weights actually are. A row cached in a NON-ACTIVE HF cache is loadable
- // only by its snapshot path, which the picker supplies as meta.loadId; sending the
- // display repo id instead failed offline, or re-downloaded into the active cache.
- // Chat threads the same field (chat-page.tsx).
+ // Where the weights actually are: a row cached in a NON-ACTIVE HF cache is loadable only by its
+ // snapshot path, which the picker supplies as meta.loadId, and sending the display repo id
+ // instead failed offline or re-downloaded into the active cache.
loadId?: string | null,
audioType?: string | null,
remoteCodeApproval?: RemoteCodeApproval,
// The catalog's answer: the ids alone miss a GGUF repo that does not spell it.
isGguf?: boolean | null,
) => {
- // A routed pick that arrives while a previous load is still tearing down would
- // otherwise be dropped here, and the route effect has already cleared ?model=, so
- // nothing retries it. Remembered and replayed from the finally below instead.
+ // A routed pick arriving while a previous load is still tearing down would otherwise be dropped,
+ // and the route effect has already cleared ?model=, so replay it from the finally below.
if (ttsLoadInFlight.current || busyRef.current === "generating") {
pendingRoutedTtsPick.current = {
repoId,
@@ -920,13 +900,12 @@ export function AudioPage({
};
return;
}
- // A load stops every chat on the shared llama-server, so ask the way Chat does
- // instead of dead-ending on the backend's 409. Claimed before the await: a routed
- // pick arriving while the dialog is open must queue, not start a second load.
+ // A load stops every chat on the shared llama-server, so ask the way Chat does instead of
+ // dead-ending on the backend's 409. Claimed before the await: a routed pick arriving while
+ // the dialog is open must queue.
ttsLoadInFlight.current = true;
- // Chat's gate, held across the question and the load. Without it a queue can
- // materialize while the dialog is open, outside the snapshot the answer was given
- // for, and a generation started in that window brings the 409 back.
+ // Chat's gate, held across the question and the load. Without it a queue can materialize while
+ // the dialog is open, outside the snapshot the answer was given for.
const lifecycleLease = useChatRuntimeStore.getState().beginModelLoading();
if (lifecycleLease === null) {
ttsLoadInFlight.current = false;
@@ -944,9 +923,8 @@ export function AudioPage({
pendingRoutedTtsPick.current = null;
return;
}
- // The page can go away while the dialog is open. pendingTtsLoad is still null then,
- // so the deactivation effect has nothing to abort and a hidden load would replace
- // the model the now-visible page is using. Queue it for the activation replay.
+ // The page can go away while the dialog is open, and pendingTtsLoad is still null then, so the
+ // deactivation effect has nothing to abort. Queue it for the activation replay.
if (!activeRef.current) {
releaseLifecycle();
ttsLoadInFlight.current = false;
@@ -966,10 +944,9 @@ export function AudioPage({
const pending = {
generation,
repoId,
- // What the request actually sent. Cancelling under the display id works only when
- // the load target is a standard HF cache snapshot, because the backend can map
- // models--org--name back to a repo id; a pinned directory elsewhere does not match
- // and _cancel_scoped_load_attempt then refuses, leaving the load running.
+ // What the request actually sent. Cancelling under the display id works only when the load target
+ // is a standard HF cache snapshot; a pinned directory elsewhere does not match and
+ // _cancel_scoped_load_attempt then refuses.
loadTarget: loadId || repoId,
loadRequestId,
controller,
@@ -1038,11 +1015,9 @@ export function AudioPage({
runtime: "tts",
onRequestStart: () => {
pending.requestStarted = true;
- // Queued prompts would otherwise start on the model this load replaces.
- // Only once /load is actually going out: loadModel returns without sending
- // when a stored token is invalid and the user picks replace or dismisses
- // the warning, and cancelling earlier threw away accepted sends and queued
- // prompts for a swap that never happened.
+ // Queued prompts would otherwise start on the model this load replaces. Only once /load is
+ // actually going out: loadModel returns without sending when a stored token is invalid, and
+ // cancelling earlier threw away accepted sends for a swap that never happened.
cancelPreStreamRunReservations(stopDecision.preStreamRunTokens);
requestLocalPromptQueueStop(stopDecision.promptQueueThreadIds);
},
@@ -1087,22 +1062,21 @@ export function AudioPage({
releaseLifecycle();
busyRef.current = null;
setBusy(null);
- // Only while Audio is visible. Replaying unconditionally started a load with
- // activeRef already false, and since the deactivation effect had run it never saw
- // that attempt to cancel it, so a hidden page could replace the model Chat had
- // loaded. The pick stays queued instead and the activation effect replays it.
+ // Only while Audio is visible: replaying unconditionally started a load with activeRef already
+ // false, which the deactivation effect never saw to cancel, so a hidden page could replace
+ // the model Chat had loaded.
if (activeRef.current) replayQueuedTtsPick();
}
},
[refreshStatus, audioDevice],
);
- // Stage uncached Hub GGUFs through the shared manager so Audio gets the same
- // progress, cancellation, resume and disk preflight behavior as Chat/Images/Video.
+ // Stage uncached Hub GGUFs through the shared manager so Audio gets the same progress,
+ // cancellation, resume and disk preflight as Chat/Images/Video.
const loadTtsModelRef = useRef(loadTtsModel);
loadTtsModelRef.current = loadTtsModel;
- /** Start a pick that lost the race with a load still settling. Visible pages only:
- * a load started while hidden outlives the deactivation effect that would cancel it. */
+ /** Start a pick that lost the race with a load still settling. Visible pages only: a load started
+ * while hidden outlives the deactivation effect that would cancel it. */
const replayQueuedTtsPick = useCallback(() => {
const queued = pendingRoutedTtsPick.current;
if (!queued) return;
@@ -1147,9 +1121,8 @@ export function AudioPage({
setMode(nextMode);
// Held through Generate, the sidecar keeps a dictation model in VRAM beside the speech one.
if (mode === "transcribe") {
- // Resolves to whether the sidecar is actually gone. Swallowing the rejection made
- // a failed unload look like a successful release, so the speech load went ahead
- // with the dictation model still resident, which OOMs a device that fits one.
+ // Resolves to whether the sidecar is actually gone: swallowing the rejection made a failed unload
+ // look like a release, so the speech load went ahead with the dictation model and OOMed.
const release = releaseTranscribeSelection().then(
() => true,
(error) => {
@@ -1161,15 +1134,14 @@ export function AudioPage({
return false;
},
);
- // Recorded so a TTS load can wait for the teardown: allocating while the sidecar
- // still holds its model is what OOMs a device that fits either one alone.
+ // Recorded so a TTS load can wait for the teardown: allocating while the sidecar still holds its
+ // model is what OOMs a device that fits either one alone.
pendingTranscribeRelease.current = release;
void release.then((released) => {
if (pendingTranscribeRelease.current !== release) return;
pendingTranscribeRelease.current = null;
- // The sidecar is still holding its model, so the page must not sit in Speak
- // claiming otherwise: back to Transcribe, where Eject can retry the unload.
- // Only if nothing has moved on since, so a user who switched again wins.
+ // The sidecar is still holding its model, so the page must not sit in Speak claiming otherwise:
+ // back to Transcribe, where Eject can retry. Only if nothing has moved on since.
if (!released && modeRef.current === "speak") setMode("transcribe");
});
}
@@ -1199,8 +1171,8 @@ export function AudioPage({
stagedTtsLoadDeferred.current = false;
return;
}
- // Audio stays mounted across tabs. Loading from a hidden page would evict
- // the visible page's model; likewise, wait for an active generation to end.
+ // Audio stays mounted across tabs, so loading from a hidden page would evict the visible page's
+ // model; likewise, wait for an active generation to end.
if (!active || busyRef.current !== null) {
stagedTtsLoadDeferred.current = true;
return;
@@ -1289,10 +1261,9 @@ export function AudioPage({
}
}
- // Hub TTS picks use the same managed path as Chat. Native models can also
- // depend on a second codec repository, so the selected repo's downloaded
- // badge is not enough: the cache-aware backend plan owns every missing
- // checkpoint, config and companion file before model loading begins.
+ // Hub TTS picks use the same managed path as Chat. Native models can also depend on a second
+ // codec repository, so the selected repo's downloaded badge is not enough: the cache-aware
+ // backend plan owns every missing file.
if (meta.source === "hub" && !ggufFilename) {
let plan;
try {
@@ -1370,8 +1341,8 @@ export function AudioPage({
return;
}
- // A cached/local/direct pick supersedes any staged auto-load. The manager
- // may keep downloading globally, but its old completion cannot load here.
+ // A cached/local/direct pick supersedes any staged auto-load: the manager may keep downloading
+ // globally, but its old completion cannot load here.
void loadTtsModelRef.current(
repoId,
ggufFilename,
@@ -1400,10 +1371,9 @@ export function AudioPage({
selectedSttRepoRef.current === repoId;
setBusy("loading");
- // Ownership is claimed only once the requested model is actually resident. Claiming it
- // up front meant a cancelled or failed download left the flag set while the backend kept
- // the previous resident model, so leaving Transcribe then unloaded another surface's
- // model. A failure leaves the previous value alone, which is what it should be.
+ // Ownership is claimed only once the requested model is actually resident: claiming it up front
+ // meant a cancelled download left the flag set while the backend kept the previous model, so
+ // leaving Transcribe unloaded another surface's model.
const toastId = toast.loading(`Preparing ${sidecarKey}…`);
try {
try {
@@ -1413,25 +1383,22 @@ export function AudioPage({
if (!(error instanceof SttModelNotDownloadedError)) throw error;
if (!isCurrent()) return;
await startSttDownload(sidecarKey, hfApiToken(getHfToken()), engine);
- // STT owns its specialized transfer, but the existing mirror gives
- // it the same global Downloads row, progress and Cancel action as
- // every other Unsloth model download. Do not reset an adopted row.
+ // STT owns its specialized transfer, but the existing mirror gives it the same global Downloads
+ // row, progress and Cancel as every other model download. Do not reset an adopted row.
if (!isTrackingSttDownload(sidecarKey, engine)) {
trackSttDownload(sidecarKey, {
- // Audio owns the final load through its active/selection
- // generation guards. The Voice-settings mirror must not warm a
- // stale sidecar behind those guards when this transfer lands.
+ // Audio owns the final load through its active/selection generation guards, so the
+ // Voice-settings mirror must not warm a stale sidecar behind those guards.
warmSelectedVoiceModelOnComplete: false,
engine,
repoId,
});
}
- // The shared Downloads panel now owns transfer progress. Keep this
- // toast for the short model-load phase after the bytes land.
+ // The shared Downloads panel now owns transfer progress; keep this toast for the short model-load
+ // phase after the bytes land.
toast.dismiss(toastId);
- // A completed download may outlive this page or selection. Re-check
- // ownership around every await so an old pick cannot replace a newer
- // sidecar when its poll finally finishes.
+ // A completed download may outlive this page or selection, so re-check ownership around every
+ // await or an old pick could replace a newer sidecar.
for (;;) {
await new Promise((resolve) => setTimeout(resolve, 1000));
if (!isCurrent()) return;
@@ -1442,9 +1409,8 @@ export function AudioPage({
if (!isCurrent()) return;
const block = sttEngineStatusFor(stt, sidecarKey, engine);
const download = block?.download;
- // Cancel comes from the shared Downloads row. It is terminal for
- // this preparation attempt, not permission to try loading a
- // partial checkpoint.
+ // Cancel comes from the shared Downloads row. It is terminal for this preparation attempt, not
+ // permission to load a partial checkpoint.
if (download?.cancelled) return;
if (download?.error) throw new Error(download.error);
if (!download?.downloading) break;
@@ -1484,9 +1450,8 @@ export function AudioPage({
[refreshSttStatus],
);
- // A hidden page may let the shared download continue, but it must not load
- // the sidecar. Returning to the same still-selected repo resumes preparation
- // once; a different selection/eject clears this deferred ownership.
+ // A hidden page may let the shared download continue, but it must not load the sidecar. Returning
+ // to the same still-selected repo resumes preparation once; a different selection clears it.
useEffect(() => {
if (!active) {
ttsPickGeneration.current += 1;
@@ -1526,8 +1491,8 @@ export function AudioPage({
return;
}
- // A pick queued behind a settling load, held back because the page went away before
- // the load finished. Now that Audio is visible again the attempt is cancellable.
+ // A pick queued behind a settling load, held back because the page went away before the load
+ // finished. Now that Audio is visible again the attempt is cancellable.
replayQueuedTtsPick();
const deferred = deferredSttLoad.current;
@@ -1546,9 +1511,8 @@ export function AudioPage({
deferred.sidecarKey,
deferred.engine,
)?.download;
- // `model` is null once the download thread has stopped, so a cancellation the
- // user made while this page was hidden matched nothing here and the deferred
- // load restarted the whole multi-GB download.
+ // `model` is null once the download thread has stopped, so a cancellation made while this page
+ // was hidden matched nothing here and the deferred load restarted the whole download.
if (
download?.cancelled &&
(download.model ?? download.cancelled_model) === deferred.sidecarKey
@@ -1574,8 +1538,8 @@ export function AudioPage({
const handleModelSelect = useCallback(
async (id: string, meta: ModelSelectorChangeMeta) => {
if (busyRef.current !== null) return;
- // Catalog first; an uncurated Hub pick falls back to its pipeline tag, or
- // every community ASR repo would load into the TTS slot.
+ // Catalog first; an uncurated Hub pick falls back to its pipeline tag, or every community ASR
+ // repo would load into the TTS slot.
const task = resolveAudioPickTask(audioTaskFor(id), meta.pipelineTag);
const musicPick =
task !== "stt" && isMusicGenerationModel(id, meta.audioType);
@@ -1591,9 +1555,8 @@ export function AudioPage({
return;
}
}
- // Selecting a different artifact while recording is a lifecycle change
- // even when it stays in Transcribe mode; never let the old capture submit
- // against a sidecar that this pick is replacing.
+ // Selecting a different artifact while recording is a lifecycle change even in Transcribe mode;
+ // never let the old capture submit against a sidecar this pick is replacing.
stopAndDiscardRecording();
deferredSttLoad.current = null;
if (task === "stt") {
@@ -1612,8 +1575,11 @@ export function AudioPage({
if (!transitionMode("speak")) return;
// Serialize against a Transcribe release started by that transition.
const releaseInFlight = pendingTranscribeRelease.current;
- // A release that failed leaves the sidecar resident, so do not stack a speech model
- // on top of it. Back to Transcribe, where Eject can retry the unload.
+ // A release that failed leaves the sidecar resident, so do not stack a speech model on top of
+ // it. Back to Transcribe, where Eject can retry.
+ // Claimed before the await below, not after: the button only disables on `busy`, so a slow release
+ // let several clicks through, each resuming into its own generateAudio while generateAbort
+ // tracked only the last.
if (releaseInFlight && !(await releaseInFlight)) {
setMode("transcribe");
return;
@@ -1646,9 +1612,8 @@ export function AudioPage({
`Loading the GGUF build of ${id}. MLX has no text-to-speech decoder, so the safetensors build cannot generate on this Mac.`,
{ duration: 7000 },
);
- // Resolving the sibling is part of the model load lifecycle. Reserve
- // the slot so Generate cannot run the old resident model and then be
- // evicted by this inspection's eventual completion.
+ // Resolving the sibling is part of the model load lifecycle. Reserve the slot so Generate cannot
+ // run the old resident model and then be evicted by this inspection's completion.
ttsInspectionGeneration.current = selectionGeneration;
busyRef.current = "loading";
setBusy("loading");
@@ -1726,8 +1691,7 @@ export function AudioPage({
const wanted = routeSearch.model;
if (!wanted) {
handledRouteModel.current = null;
- // A task with no model is a mode intent from Settings; without it the page keeps
- // whatever mode it was left in.
+ // A task with no model is a mode intent from Settings; without it the page keeps whatever mode it was left in.
const task = routeSearch.task;
if (!task) return;
const intended =
@@ -1739,8 +1703,8 @@ export function AudioPage({
}
const key = `${wanted}|${routeSearch.quant ?? ""}|${routeSearch.ggufQuant ?? ""}|${routeSearch.task ?? ""}|${routeSearch.audioType ?? ""}|${routeSearch.loadId ?? ""}`;
if (handledRouteModel.current === key) return;
- // The persistent Audio page may still be finishing hidden work. Keep the
- // handoff in the URL and retry it when that work releases the lifecycle.
+ // The persistent Audio page may still be finishing hidden work, so keep the handoff in the URL
+ // and retry it when that work releases the lifecycle.
if (busyRef.current !== null) return;
handledRouteModel.current = key;
handleModelSelect(wanted, {
@@ -1750,8 +1714,8 @@ export function AudioPage({
ggufVariant: routeSearch.ggufQuant ?? undefined,
loadId: routeSearch.loadId ?? undefined,
audioType: routeSearch.audioType ?? undefined,
- // Chat-to-Audio routing cannot preserve the inventory flag, so stage the
- // exact forwarded GGUF. An already-cached job completes immediately.
+ // Chat-to-Audio routing cannot preserve the inventory flag, so stage the exact forwarded GGUF.
+ // An already-cached job completes immediately.
isDownloaded: routeSearch.loadId
? true
: routeSearch.quant
@@ -1775,7 +1739,6 @@ export function AudioPage({
transitionMode,
]);
- // --- Speak --------------------------------------------------------------
const ttsLoaded = Boolean(
status?.active_model &&
@@ -1794,8 +1757,8 @@ export function AudioPage({
return;
}
- // Eject also owns unresolved permission requests. Invalidating here makes
- // their eventual streams self-discard instead of recording for an old STT pick.
+ // Eject also owns unresolved permission requests. Invalidating here makes their eventual streams
+ // self-discard instead of recording for an old STT pick.
stopAndDiscardRecording();
if (mode === "transcribe") {
@@ -1833,8 +1796,8 @@ export function AudioPage({
const activeModel = status?.active_model;
if (!activeModel) return;
- // Chat's gate, taken before the question so a queue cannot materialize while the
- // dialog is open and then be stopped by the blanket queue stop below.
+ // Chat's gate, taken before the question so a queue cannot materialize while the dialog is open
+ // and then be stopped by the blanket queue stop.
const lifecycleLease = useChatRuntimeStore.getState().beginModelLoading();
if (lifecycleLease === null) {
toast.info("Wait for the current model to finish loading.");
@@ -1845,9 +1808,9 @@ export function AudioPage({
setBusy("unloading");
void (async () => {
try {
- // Ejecting stops every chat on the shared llama-server. Unforced, the backend
- // refused with a 409 the user could only read. Nothing is torn down until the
- // answer is in, so declining leaves the page as it was.
+ // Ejecting stops every chat on the shared llama-server, and unforced the backend refused with a
+ // 409 the user could only read. Nothing is torn down until the answer is in, so declining
+ // leaves the page as it was.
const stopDecision = await confirmStopRunningChatsIfNeeded(
"Unloading the model",
"unload",
@@ -1857,8 +1820,8 @@ export function AudioPage({
return;
}
- // An old managed completion must not immediately replace the model the
- // user just ejected. The global download may continue for later use.
+ // An old managed completion must not immediately replace the model the user just ejected. The
+ // global download may continue for later use.
invalidatePendingStagedTts();
stageTtsDownload([]);
@@ -1902,15 +1865,11 @@ export function AudioPage({
const handleGenerate = useCallback(async () => {
const text = prompt.trim();
if (!text) return;
- // Same gate the TTS load path uses. Switching straight from Transcribe with a speech
- // model already resident needs no load, so nothing else waits for the sidecar
- // teardown, and generating beside a dictation model OOMs a device that fits either
- // one alone. A release that failed leaves it resident, so go back to Transcribe,
- // where Eject can retry the unload.
- // Claimed before the await below, not after: the button only disables on `busy`, so
- // a slow release let several clicks through and each resumed into its own
- // generateAudio while generateAbort tracked only the last, and either finally
- // cleared the busy state out from under the other.
+ // Same gate the TTS load path uses: switching straight from Transcribe with a speech model
+ // already resident needs no load, so nothing else waits for the sidecar teardown, and
+ // generating beside a dictation model OOMs a device that fits either alone. Claimed before
+ // the await below, since the button only disables on `busy` and a slow release let several
+ // clicks each resume into their own generateAudio.
if (busyRef.current) return;
busyRef.current = "generating";
setBusy("generating");
@@ -1961,11 +1920,9 @@ export function AudioPage({
setFallbackClip(null);
selectClip(generatedClip.id);
} else if (generated.clip_id) {
- // The server did persist it; only this refresh missed it (refreshGallery swallows a
- // transient failure and returns the stale cache). Select the id so a later refresh
- // shows the real record, but keep the response audio too: selectedClip resolves
- // against `clips`, so an id that is not there yet would render the empty state and
- // strand a generation the user just paid for.
+ // The server did persist it; only this refresh missed it. Select the id so a later refresh shows
+ // the real record, but keep the response audio too: selectedClip resolves against `clips`,
+ // so an id that is not there yet would render the empty state.
setFallbackClip({
url: `data:audio/wav;base64,${generated.audio.data}`,
prompt: text,
@@ -1974,9 +1931,8 @@ export function AudioPage({
});
selectClip(generated.clip_id, true);
} else {
- // Gallery persistence is best-effort server-side, so a full or unwritable
- // disk still returns the audio. Play it from the response rather than
- // dropping an expensive generation on the floor.
+ // Gallery persistence is best-effort server-side, so a full disk still returns the audio. Play it
+ // from the response rather than dropping an expensive generation.
galleryCache.selectedId = null;
setSelectedId(null);
setFallbackClip({
@@ -2022,12 +1978,10 @@ export function AudioPage({
selectClip,
]);
- // Only unmount aborts. RootLayout keeps this page mounted precisely so leaving
- // the tab does not cancel synthesis, and the clip is persisted server-side, so
- // it is still waiting in the gallery on return.
+ // Only unmount aborts. RootLayout keeps this page mounted precisely so leaving the tab does not
+ // cancel synthesis, and the clip is persisted server-side.
useEffect(() => () => generateAbort.current?.abort(), []);
- // --- Transcribe ---------------------------------------------------------
const runTranscription = useCallback(
async (blob: Blob, name: string) => {
@@ -2104,9 +2058,8 @@ export function AudioPage({
}
recordStreamRef.current = stream;
const recorder = createAudioRecorder(stream);
- // WAV is uncompressed, so on the PCM path the byte cap is reached long
- // before the 30 minute one. Express it as a duration the timer below
- // already enforces, rather than letting the upload be refused.
+ // WAV is uncompressed, so on the PCM path the byte cap is reached long before the 30 minute one.
+ // Express it as a duration the timer below already enforces.
const maxSeconds =
recorder instanceof PcmRecorder
? Math.min(
@@ -2117,9 +2070,8 @@ export function AudioPage({
const chunks: Blob[] = [];
let recordedBytes = 0;
let limitHit: "duration" | "size" | null = null;
- // The sidecar rejects anything past 30 minutes, and a timeslice keeps the chunks in
- // our array rather than inside the browser, so an over-long recording can be stopped
- // at the limit instead of being buffered whole and refused after the upload.
+ // The sidecar rejects anything past 30 minutes, and a timeslice keeps the chunks in our array
+ // rather than inside the browser, so an over-long recording can be stopped at the limit.
const stopAtLimit = (reason: "duration" | "size") => {
if (limitHit) return;
limitHit = reason;
@@ -2140,8 +2092,7 @@ export function AudioPage({
);
recorder.addEventListener("dataavailable", (event) => {
if (event.data.size > 0) {
- // Stop before appending the chunk that crosses the limit, so what is uploaded is
- // always inside it rather than one chunk over.
+ // Stop before appending the chunk that crosses the limit, so what is uploaded is always inside it.
if (recordedBytes + event.data.size > RECORDING_MAX_BYTES) {
stopAtLimit("size");
return;
@@ -2163,14 +2114,13 @@ export function AudioPage({
if (!discard && blob.size > 0) void runTranscription(blob, "Recording");
});
recorderRef.current = recorder;
- // A timeslice is what makes the byte cap observable: with none, some browsers hold
- // the whole recording internally and only emit it on stop.
+ // A timeslice is what makes the byte cap observable: with none, some browsers hold the whole
+ // recording internally and only emit it on stop.
recorder.start(RECORDING_CHUNK_MS);
setIsRecording(true);
} catch {
- // getUserMedia may have succeeded even if MediaRecorder construction or
- // start failed. Release that acquired stream instead of leaving the mic
- // live with no recorder UI capable of stopping it.
+ // getUserMedia may have succeeded even if MediaRecorder construction failed, so release that
+ // stream instead of leaving the mic live with no recorder UI to stop it.
recorderRef.current = null;
setIsRecording(false);
stopRecordStream();
@@ -2190,9 +2140,8 @@ export function AudioPage({
}
}, [isRecording, runTranscription, stopRecordStream]);
- // Release the microphone on unmount AND whenever the page goes inactive: the
- // page stays mounted across tab switches, so unmount alone left a hidden
- // recorder capturing until the user came back and stopped it.
+ // Release the microphone on unmount AND whenever the page goes inactive: the page stays mounted
+ // across tab switches, so unmount alone left a hidden recorder capturing.
useEffect(() => {
if (!active) {
stopAndDiscardRecording();
@@ -2226,18 +2175,17 @@ export function AudioPage({
anchor.href = url;
anchor.download = `${(transcribedName ?? "transcript").replace(/\.[^.]+$/, "")}.txt`;
anchor.click();
- // Deferred like the gallery download below: browsers that resolve the synthetic
- // navigation asynchronously were left with a revoked URL and no file.
+ // Deferred like the gallery download below: browsers that resolve the synthetic navigation
+ // asynchronously were left with a revoked URL and no file.
window.setTimeout(() => URL.revokeObjectURL(url), 0);
}, [transcript, transcribedName]);
- // --- Gallery actions ----------------------------------------------------
const dropClip = useCallback((id: string) => {
galleryCache.srcById.delete(id);
setSrcById(galleryCache.srcById.toRecord());
- // Drop the row now, as the clear-all path does: refreshGallery swallows a failed GET and
- // returns the cache without setClips, leaving the row up against an already-revoked URL.
+ // Drop the row now, as the clear-all path does: refreshGallery swallows a failed GET and returns
+ // the cache without setClips, leaving the row up against an already-revoked URL.
galleryCache.clips = galleryCache.clips.filter((clip) => clip.id !== id);
setClips(galleryCache.clips);
if (galleryCache.selectedId === id) {
@@ -2292,7 +2240,7 @@ export function AudioPage({
// This page stays mounted across route changes, so a restore from the Settings archive would not
// reach History until a reload. Refresh the loaded window, not just the first page: a clip
- // re-enters at its own age, and the kept cursor already starts past anything below that page.
+ // re-enters at its own age.
useEffect(
() =>
subscribeGalleryChanged("audio", () => {
@@ -2306,12 +2254,11 @@ export function AudioPage({
await clearAudioGallery();
galleryCache.srcById.clear();
galleryCache.selectedId = null;
- // Drop the cached list first: refreshGallery merges the fetched page into it, so an
- // empty page would otherwise leave every cleared row on screen.
+ // Drop the cached list first: refreshGallery merges the fetched page into it, so an empty page
+ // would leave every cleared row on screen.
galleryCache.clips = [];
- // React state too, not just the cache: refreshGallery swallows a failed GET and
- // returns the cache without calling setClips, which left every cleared row rendered
- // against a revoked object URL until some later refresh.
+ // React state too, not just the cache: refreshGallery swallows a failed GET and returns the cache
+ // without calling setClips, which left cleared rows rendered against a revoked URL.
setClips([]);
setSrcById({});
setSelectedId(null);
@@ -2360,9 +2307,8 @@ export function AudioPage({
} catch {
toast.error("Could not download the clip.");
} finally {
- // A history-row download does not need to become resident playback state.
- // Revoke it after the browser has consumed the synthetic click instead of
- // bypassing the persistent gallery cache's 64 MB budget.
+ // A history-row download does not need to become resident playback state, so revoke it after the
+ // browser has consumed the synthetic click rather than bypassing the 64 MB cache budget.
if (temporaryUrl) {
const url = temporaryUrl;
window.setTimeout(() => URL.revokeObjectURL(url), 0);
@@ -2379,9 +2325,8 @@ export function AudioPage({
}
}, []);
- // Trained TTS checkpoints. A scan row carries no modality until the backend tags
- // it, so without this the Audio page offers only catalog models and a checkpoint
- // you just fine-tuned here is unreachable.
+ // Trained TTS checkpoints. A scan row carries no modality until the backend tags it, so without
+ // this a checkpoint you just fine-tuned here is unreachable.
const [trainedTtsModels, setTrainedTtsModels] = useState<ModelOption[]>([]);
useEffect(() => {
if (!active) return;
@@ -2391,8 +2336,8 @@ export function AudioPage({
if (cancelled) return;
setTrainedTtsModels(
res.loras
- // Merged native speech checkpoints bypass MLX through the portable
- // audio worker. Other safetensors exports still need a GGUF build on Mac.
+ // Merged native speech checkpoints bypass MLX through the portable audio worker. Other
+ // safetensors exports still need a GGUF build on Mac.
.filter(
(lora) =>
!isMac ||
@@ -2401,9 +2346,8 @@ export function AudioPage({
lora.export_type,
),
)
- // The GGUF flag matters: GGUF_TTS_AUDIO_TYPES leaves csm out because llama.cpp has
- // no CSM decoder, so a csm LoRA exported to GGUF fails at load. Without it the
- // wider Transformers list answered and the row was offered anyway.
+ // The GGUF flag matters: GGUF_TTS_AUDIO_TYPES leaves csm out because llama.cpp has no CSM
+ // decoder, so a csm LoRA exported to GGUF fails at load.
.filter((lora) =>
isTtsAudioType(lora.audio_type, lora.export_type === "gguf"),
)
@@ -2430,7 +2374,6 @@ export function AudioPage({
};
}, [active, isMac]);
- // --- Render -------------------------------------------------------------
const selectedClip = clips.find((c) => c.id === selectedId) ?? null;
const selectedClipSrc = selectedClip ? srcById[selectedClip.id] : undefined;
@@ -2483,8 +2426,8 @@ export function AudioPage({
return (
<div className="@container flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden pt-[var(--studio-content-top-inset,0px)]">
- {/* Keep the tabs centered over the preview region at every width. The model rail
- holds at 408px when space permits and shrinks only to preserve the controls. */}
+ {/* Keep the tabs centered over the preview at every width. The model rail holds at 408px when
+ space permits and shrinks only to preserve the controls. */}
<div className="pointer-events-none relative z-40 grid h-[48px] shrink-0 grid-cols-[minmax(0,408px)_minmax(13rem,1fr)]">
<div className="pointer-events-none flex h-full min-w-0 items-start overflow-hidden pl-[var(--studio-media-header-left-inset,1.5rem)] @[50rem]:border-r @[50rem]:border-border/60">
{/* A long resident model name must yield to the mode pill instead of painting over it. */}
@@ -2508,8 +2451,8 @@ export function AudioPage({
triggerLabelClassName="text-ui-14 @[68rem]:text-ui-16"
task={HUB_TASKS_BY_MODE[mode]}
catalog={AUDIO_CATALOG}
- // TTS/ASR come from the checkpoint's own tokenizer, not a curated
- // recipe, so any publisher's audio repo loads here.
+ // TTS/ASR come from the checkpoint's own tokenizer, not a curated recipe, so any publisher's
+ // audio repo loads here.
communityModelPolicy="search-only"
placeholder="Select audio model"
open={active && selectorOpen}
@@ -2556,8 +2499,8 @@ export function AudioPage({
</div>
</div>
</div>
- {/* Below 50rem the panes stack and the page scrolls as one column, matching Images and
- Video: side by side, the 408px rail plus a usable preview needs more width than that. */}
+ {/* Below 50rem the panes stack and the page scrolls as one column, matching Images and Video:
+ side by side, the 408px rail plus a usable preview needs more width. */}
<div className="flex min-h-0 w-full min-w-0 flex-1 flex-col overflow-y-auto overflow-x-hidden @[50rem]:flex-row @[50rem]:overflow-hidden">
<div className="flex w-full shrink-0 flex-col border-b border-border/60 @[50rem]:w-[408px] @[50rem]:overflow-hidden @[50rem]:border-r @[50rem]:border-b-0">
<div
@@ -2571,8 +2514,7 @@ export function AudioPage({
settingsFadeClass,
)}
>
- {/* Same heading treatment as the Images and Video Create panes, so
- the media panes stay level (#7986). */}
+ {/* Same heading treatment as the Images and Video Create panes, so the media panes stay level (#7986). */}
<div className="mb-2 grid gap-1.5">
<h2 className="flex items-center gap-2 font-heading text-xl font-medium leading-none text-foreground">
<HugeiconsIcon
@@ -2945,9 +2887,8 @@ export function AudioPage({
<p className="line-clamp-2 text-ui-13 text-muted-foreground">
{selectedClip.prompt}
</p>
- {/* Auth-protected bytes, so mount a fresh player only once this clip's
- object URL exists. Reusing one media element while src is empty or
- changing left History switches showing stale/broken controls. */}
+ {/* Auth-protected bytes, so mount a fresh player only once this clip's object URL exists: reusing
+ one media element while src is changing left History switches showing broken controls. */}
{selectedClipSrc ? (
<audio
key={selectedClip.id}
diff --git a/studio/frontend/src/features/chat/adapters/dictation-level.ts b/studio/frontend/src/features/chat/adapters/dictation-level.ts
index e38060ba0e1..0d2c2e9228d 100644
--- a/studio/frontend/src/features/chat/adapters/dictation-level.ts
+++ b/studio/frontend/src/features/chat/adapters/dictation-level.ts
@@ -20,10 +20,8 @@ function publishLevel(level: number): void {
}
}
-/**
- * Start a lightweight Web Audio meter for the shared recording waveform and
- * optional voice-activity detection. Returns an idempotent cleanup function.
- */
+/** Start a lightweight Web Audio meter for the shared recording waveform and optional
+ * voice-activity detection. Returns an idempotent cleanup function. */
export function startDictationLevelMeter(
source: MediaStream,
onFrame?: FrameListener,
diff --git a/studio/frontend/src/features/chat/adapters/dictation-outcome.ts b/studio/frontend/src/features/chat/adapters/dictation-outcome.ts
index 0eaabfbf1d4..a0f88a50fb9 100644
--- a/studio/frontend/src/features/chat/adapters/dictation-outcome.ts
+++ b/studio/frontend/src/features/chat/adapters/dictation-outcome.ts
@@ -1,14 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * What the dictation that just ended actually produced.
- *
- * The recording bar's send needs both answers, and cannot get them from the
- * composer: text can change while recording (the plus menu can insert a saved
- * prompt), and a partial transcript still lands there. Module state, not
- * session state, because the bar reads it once the session is gone.
- */
+/** What the dictation that just ended actually produced. The recording bar's send needs both
+ * answers and cannot get them from the composer: text can change while recording (the plus menu
+ * can insert a saved prompt), and a partial transcript still lands there. Module state, not
+ * session state, because the bar reads it once the session is gone. */
let producedTranscript = false;
let failed = false;
@@ -28,10 +24,8 @@ export function dictationProducedTranscript(): boolean {
return producedTranscript;
}
-/**
- * A transcript chunk, or the session itself, failed. Both engines can still
- * publish what did transcribe, so the text is partial rather than absent.
- */
+/** A transcript chunk, or the session itself, failed. Both engines can still publish what did
+ * transcribe, so the text is partial rather than absent. */
export function markDictationFailed(): void {
failed = true;
}
diff --git a/studio/frontend/src/features/chat/adapters/pcm-recorder.ts b/studio/frontend/src/features/chat/adapters/pcm-recorder.ts
index 6c8b149a154..db9d5b31185 100644
--- a/studio/frontend/src/features/chat/adapters/pcm-recorder.ts
+++ b/studio/frontend/src/features/chat/adapters/pcm-recorder.ts
@@ -1,24 +1,19 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Microphone capture for engines whose MediaRecorder cannot encode audio.
- *
- * WebKitGTK -- the webview the Linux desktop build runs in -- ships
- * MediaRecorder and resolves `audio/mp4`, but never builds a GStreamer audio
- * encoding profile for it, so every recording comes back with zero bytes and
- * dictation silently transcribes nothing (#9543). Reading raw PCM off a Web
- * Audio graph instead sidesteps the encoder entirely, and WAV is the one
- * container the STT backend forwards untouched rather than transcoding.
- */
+/** Microphone capture for engines whose MediaRecorder cannot encode audio. WebKitGTK, the
+ * webview the Linux desktop build runs in, ships MediaRecorder and resolves `audio/mp4` but
+ * never builds a GStreamer audio encoding profile for it, so every recording comes back with
+ * zero bytes (#9543). Reading raw PCM off a Web Audio graph sidesteps the encoder, and WAV
+ * is the one container the STT backend forwards untouched. */
/** Only the field the recording call sites read off a `dataavailable` event. */
export interface RecordedDataEvent {
readonly data: Blob;
}
-/** The slice of MediaRecorder the recording call sites drive, so a PCM
- * recorder can stand in for one without changing how they wire it up. */
+/** The slice of MediaRecorder the recording call sites drive, so a PCM recorder can stand in
+ * for one without changing how they wire it up. */
export interface SegmentRecorder {
readonly state: RecordingState;
readonly mimeType: string;
@@ -38,16 +33,13 @@ export interface SegmentRecorder {
// A container the engine can only advertise if it has a working audio encoder.
const OPUS_MIME_TYPES = ["audio/webm;codecs=opus", "audio/ogg;codecs=opus"];
-// Apple's WebKit builds also advertise audio/mp4 alone, and there MediaRecorder
-// does encode, so the platform is what separates them from WebKitGTK.
+// Apple's WebKit builds also advertise audio/mp4 alone, and there MediaRecorder does encode,
+// so the platform is what separates them from WebKitGTK.
const APPLE_WEBKIT = /iPad|iPhone|iPod|Macintosh|Mac OS X/;
-/** Whether this engine's MediaRecorder can be trusted to encode audio.
- *
- * Kept a capability test rather than a Linux check so every engine that does
- * work -- Chrome, Edge/WebView2, Firefox, Safari -- keeps recording exactly as
- * it does today, and only the broken one takes the PCM path.
- */
+/** Whether this engine's MediaRecorder can be trusted to encode audio. Kept a capability test
+ * rather than a Linux check so every engine that does work keeps recording as it does
+ * today, and only the broken one takes the PCM path. */
export function mediaRecorderCanEncodeAudio(
isTypeSupported: (type: string) => boolean = (type) =>
typeof MediaRecorder !== "undefined" && MediaRecorder.isTypeSupported(type),
@@ -61,18 +53,15 @@ export function mediaRecorderCanEncodeAudio(
// Whisper's own rate: the smallest WAV that costs the backend no resample.
const TARGET_SAMPLE_RATE = 16_000;
-// ~256ms of audio per callback at the target rate: infrequent enough not to
-// keep waking the main thread, short enough that a stop cuts promptly.
+// ~256ms of audio per callback at the target rate: infrequent enough not to keep waking the
+// main thread, short enough that a stop cuts promptly.
const BUFFER_FRAMES = 4096;
const BYTES_PER_SAMPLE = 2;
const WAV_HEADER_BYTES = 44;
-/** Encode mono float32 samples as a 16-bit PCM WAV.
- *
- * The backend forwards `RIFF....WAVE` to llama-server untouched and decodes it
- * with no format sniffing on the Transformers path, so this is both the
- * cheapest and the most widely accepted thing to upload.
- */
+/** Encode mono float32 samples as a 16-bit PCM WAV. The backend forwards `RIFF....WAVE` to
+ * llama-server untouched and decodes it with no format sniffing on the Transformers path,
+ * so this is both the cheapest and the most widely accepted thing to upload. */
export function encodeWav(
samples: Float32Array,
sampleRate: number,
@@ -99,8 +88,8 @@ export function encodeWav(
writeAscii(36, "data");
view.setUint32(40, dataBytes, true);
for (let index = 0; index < samples.length; index += 1) {
- // Clamp first: Web Audio samples are nominally -1..1 but may overshoot, and
- // an out-of-range value wraps to the opposite sign as a loud click.
+ // Clamp first: Web Audio samples are nominally -1..1 but may overshoot, and an out-of-range
+ // value wraps to the opposite sign as a loud click.
const sample = Math.min(1, Math.max(-1, samples[index]));
view.setInt16(
WAV_HEADER_BYTES + index * BYTES_PER_SAMPLE,
@@ -119,22 +108,17 @@ function createAudioContext(): AudioContext {
try {
return new Ctx({ sampleRate: TARGET_SAMPLE_RATE });
} catch {
- // Some engines only open a context at the device rate. The backend
- // resamples whatever it is given, so that is still correct -- just larger
- // on the wire, which is what secondsWithin() accounts for.
+ // Some engines only open a context at the device rate. The backend resamples whatever it is
+ // given, so that is still correct, just larger on the wire, which secondsWithin() accounts
+ // for.
return new Ctx();
}
}
-/**
- * Records a MediaStream as a single WAV blob, emitted as one `dataavailable`
- * followed by `stop`, the way MediaRecorder reports a finished recording.
- *
- * A ScriptProcessorNode rather than an AudioWorklet: a worklet needs its
- * processor loaded as a separate module URL, and at one 4096-frame callback per
- * quarter second for a single mono channel there is no main-thread cost worth
- * that build-time asset.
- */
+/** Records a MediaStream as a single WAV blob, emitted as one `dataavailable` then `stop`, the
+ * way MediaRecorder reports a finished recording. A ScriptProcessorNode rather than an
+ * AudioWorklet: a worklet needs its processor as a separate module URL, and at one
+ * 4096-frame callback per quarter second there is no main-thread cost worth that asset. */
export class PcmRecorder implements SegmentRecorder {
readonly mimeType = "audio/wav";
readonly sampleRate: number;
@@ -159,13 +143,13 @@ export class PcmRecorder implements SegmentRecorder {
this.processor.addEventListener("audioprocess", (event) => {
if (this.recordingState !== "recording") return;
const input = event.inputBuffer.getChannelData(0);
- // getChannelData hands back a buffer the engine reuses for the next
- // callback, so it has to be copied rather than retained.
+ // getChannelData hands back a buffer the engine reuses for the next callback, so it has to be
+ // copied rather than retained.
this.chunks.push(new Float32Array(input));
this.frames += input.length;
});
- // A ScriptProcessorNode only runs while it reaches a destination, so route
- // it through a silent gain rather than playing the microphone back.
+ // A ScriptProcessorNode only runs while it reaches a destination, so route it through a silent
+ // gain rather than playing the microphone back.
this.sink = this.context.createGain();
this.sink.gain.value = 0;
this.source.connect(this.processor);
@@ -177,8 +161,8 @@ export class PcmRecorder implements SegmentRecorder {
return this.recordingState;
}
- /** Seconds of audio that fit in `maxBytes` once encoded, less a second of
- * slack for the buffer still in flight when a caller stops on this. */
+ /** Seconds of audio that fit in `maxBytes` once encoded, less a second of slack for the buffer
+ * still in flight when a caller stops on this. */
secondsWithin(maxBytes: number): number {
const bytesPerSecond = this.sampleRate * BYTES_PER_SAMPLE;
return Math.max(
@@ -209,14 +193,12 @@ export class PcmRecorder implements SegmentRecorder {
this.stopListeners.push({ listener, once: options?.once === true });
}
- /** `timesliceMs` is accepted for MediaRecorder parity and ignored: the
- * samples are already buffered here rather than inside the engine, which is
- * the only thing a timeslice bought the callers. */
+ /** `timesliceMs` is accepted for MediaRecorder parity and ignored: the samples are already
+ * buffered here rather than inside the engine, which is all a timeslice bought the callers. */
start(_timesliceMs?: number): void {
if (this.recordingState !== "inactive") return;
this.recordingState = "recording";
- // Started before a user gesture on some engines; a suspended context
- // delivers no audioprocess callbacks at all.
+ // Started before a user gesture on some engines; a suspended context delivers no audioprocess callbacks at all.
this.context.resume().catch(() => {
// Already running, or resumed on its own once the mic was granted.
});
@@ -234,9 +216,8 @@ export class PcmRecorder implements SegmentRecorder {
this.chunks.length = 0;
this.frames = 0;
this.teardown();
- // A tap too short to collect a callback reports nothing rather than a
- // header with no samples, so the callers read it as the silence they
- // already read an empty MediaRecorder buffer as.
+ // A tap too short to collect a callback reports nothing rather than a header with no samples,
+ // so the callers read it as the silence an empty MediaRecorder buffer already reads as.
const data =
samples.length === 0
? new Blob([])
@@ -247,8 +228,8 @@ export class PcmRecorder implements SegmentRecorder {
for (const listener of this.dataListeners) listener(event);
const stopEvent = new Event("stop");
const listeners = this.stopListeners.slice();
- // Drop the one-shot listeners before dispatching: a `stop` handler is where
- // the callers start the next segment, which registers more of them.
+ // Drop the one-shot listeners before dispatching: a `stop` handler is where the callers start
+ // the next segment, which registers more of them.
for (let index = this.stopListeners.length - 1; index >= 0; index -= 1) {
if (this.stopListeners[index].once) this.stopListeners.splice(index, 1);
}
@@ -265,9 +246,8 @@ export class PcmRecorder implements SegmentRecorder {
}
}
-/** A recorder for `stream`: MediaRecorder where it encodes, PCM where it does
- * not. `mimeType` is the MediaRecorder preference and is unused on the PCM
- * path, which always produces WAV. */
+/** A recorder for `stream`: MediaRecorder where it encodes, PCM where it does not. `mimeType`
+ * is the MediaRecorder preference and is unused on the PCM path, which always makes WAV. */
export function createAudioRecorder(
stream: MediaStream,
mimeType?: string,
diff --git a/studio/frontend/src/features/chat/adapters/stt-errors.ts b/studio/frontend/src/features/chat/adapters/stt-errors.ts
index 197a2c9043e..1cc6cdbfaf4 100644
--- a/studio/frontend/src/features/chat/adapters/stt-errors.ts
+++ b/studio/frontend/src/features/chat/adapters/stt-errors.ts
@@ -9,14 +9,11 @@ export class SttModelNotDownloadedError extends Error {
}
}
-/** The error for a failed STT response, typed by what its status and detail say.
- *
- * Shared by the load and transcribe calls: a segment can be the first thing to
- * learn the model is missing, when a short recording ends before the
- * fire-and-forget preload rejects, and it has to open the download prompt just
- * as the preload would. 409 also covers a load cancelled for training and a
- * model switch mid-request, so the detail is what separates them.
- */
+/** The error for a failed STT response, typed by what its status and detail say. Shared by the load
+ * and transcribe calls: a segment can be the first thing to learn the model is missing, when a
+ * short recording ends before the fire-and-forget preload rejects, and it has to open the
+ * download prompt just as the preload would. 409 also covers a load cancelled for training and a
+ * model switch mid-request, so the detail is what separates them. */
export function sttRequestError(status: number, detail: string): Error {
return status === 409 && /not downloaded/i.test(detail)
? new SttModelNotDownloadedError(detail)
diff --git a/studio/frontend/src/features/chat/adapters/studio-dictation-adapter.tsx b/studio/frontend/src/features/chat/adapters/studio-dictation-adapter.tsx
index 2277a745876..ee997b7f879 100644
--- a/studio/frontend/src/features/chat/adapters/studio-dictation-adapter.tsx
+++ b/studio/frontend/src/features/chat/adapters/studio-dictation-adapter.tsx
@@ -19,9 +19,9 @@ import {
StudioWebSpeechDictationAdapter,
} from "./studio-web-speech-dictation-adapter";
-// The one live dictation session, so Escape can discard it without going
-// through assistant-ui (which only exposes stop, i.e. transcribe). Cancelling
-// emits no transcript, so composer text is untouched.
+// The one live dictation session, so Escape can discard it without going through assistant-ui,
+// which only exposes stop. Cancelling emits no transcript, so composer text is untouched.
+// assistant-ui exposes only stop, i.e. transcribe, which is why Escape needs its own path.
let activeSession: StudioDictationSession | null = null;
/** Discard the current dictation without transcribing. Safe to call when idle. */
@@ -31,10 +31,8 @@ export function cancelActiveStudioDictation(): void {
session?.cancel();
}
-/**
- * Routes dictation to the engine chosen in Voice settings, resolved at listen()
- * time so switching engines applies without reloading the chat runtime.
- */
+/** Routes dictation to the engine chosen in Voice settings, resolved at listen() time so
+ * switching engines applies without reloading the chat runtime. */
/** local and custom transcription record through the media recorder. */
function usesRecordedAudio(dictationEngine: DictationEngine): boolean {
return dictationEngine !== "browser";
@@ -54,8 +52,8 @@ function customSttConfigured(): boolean {
}
export class StudioDictationAdapter implements DictationAdapter {
- // Chat linked in Recent dictations. undefined follows the active single chat;
- // null records no chat (composers outside it, e.g. Compare).
+ // Chat linked in Recent dictations. undefined follows the active single chat; null records no
+ // chat (composers outside it, e.g. Compare).
private readonly chatId: string | null | undefined;
constructor(options: { chatId?: string | null } = {}) {
@@ -76,9 +74,8 @@ export class StudioDictationAdapter implements DictationAdapter {
listen(): StudioDictationSession {
const session = this.createSession();
- // A second entry point (chat, Compare, settings test) replaces the active
- // session; cancel the old one so it cannot keep the mic open or save a
- // transcript with no discard button pointing at it.
+ // A second entry point (chat, Compare, settings test) replaces the active session; cancel the
+ // old one so it cannot keep the mic open or save a transcript with no discard button.
cancelActiveStudioDictation();
activeSession = session;
// Forget the session once it ends so a later cancel is a no-op.
@@ -148,23 +145,21 @@ export function notifyStudioDictationUnavailable(
toast.error("Voice recording isn't available in this browser.");
return;
}
- // Browser Web Speech is missing (e.g. Firefox). Local dictation is the only
- // way to type by voice here, so offer it rather than describing it.
+ // Browser Web Speech is missing (e.g. Firefox). Local dictation is the only way to type by
+ // voice here, so offer it rather than describing it.
void offerLocalDictation();
}
-/**
- * Move a browser with no speech service onto local dictation. Already
- * downloaded means one switch; otherwise the same confirmation the mic raises,
- * which flips the engine only if it is accepted.
- */
+/** Move a browser with no speech service onto local dictation. Already downloaded means one
+ * switch; otherwise the same confirmation the mic raises, which flips the engine only if
+ * accepted. */
async function offerLocalDictation(): Promise<void> {
const { sttModel, setDictationEngine } = useVoiceSettingsStore.getState();
try {
const status = await fetchSttStatus(undefined, sttModel);
const engine = sttEngineStatusFor(status, sttModel);
- // An engine with no runtime installed cannot load what it downloads, so
- // say what is missing rather than asking for gigabytes first.
+ // An engine with no runtime installed cannot load what it downloads, so say what is missing
+ // rather than asking for gigabytes first.
if (engine && !engine.available) {
toast.error("Local transcription isn't installed on this server.", {
description:
diff --git a/studio/frontend/src/features/chat/adapters/studio-model-dictation-adapter.ts b/studio/frontend/src/features/chat/adapters/studio-model-dictation-adapter.ts
index c612f5d196a..2e0841fe76f 100644
--- a/studio/frontend/src/features/chat/adapters/studio-model-dictation-adapter.ts
+++ b/studio/frontend/src/features/chat/adapters/studio-model-dictation-adapter.ts
@@ -38,17 +38,16 @@ import {
// Fine timeslice so the buffer is ready the moment a segment is cut or stopped.
const SEGMENT_TIMESLICE_MS = 250;
-// Whisper pads input to 30s. Short dictation stays one clip; long dictation cuts
-// at the first pause after 20s or before the 30s boundary.
+// Whisper pads input to 30s. Short dictation stays one clip; long dictation cuts at the first
+// pause after 20s or before the 30s boundary.
const MIN_SEGMENT_MS = 20_000;
const MAX_SEGMENT_MS = 28_000;
const SILENCE_CUT_MS = 280;
-// Raw RMS (0..1) above which a frame counts as speech (well above the room floor
-// after noise suppression).
+// Raw RMS (0..1) above which a frame counts as speech (well above the room floor after noise suppression).
const VOICE_RMS = 0.015;
-// Prefer Opus (small, widely supported); fall back to whatever the browser
-// records. The backend decodes any of these with PyAV.
+// Prefer Opus (small, widely supported); fall back to whatever the browser records. The
+// backend decodes any of these with PyAV.
const PREFERRED_MIME_TYPES = [
"audio/webm;codecs=opus",
"audio/webm",
@@ -70,9 +69,8 @@ const stopStream = (stream: MediaStream | null) => {
}
};
-/** Backend STT engine, decided by the model: Whisper ids run GGML through
- * whisper.cpp, mtmd ids run through llama.cpp, and a custom HF repo is
- * safetensors on Transformers. */
+/** Backend STT engine, decided by the model: Whisper ids run GGML through whisper.cpp, mtmd
+ * ids run through llama.cpp, and a custom HF repo is safetensors on Transformers. */
export type SttEngine = "transformers" | "gguf" | "mtmd";
export function sttEngineFor(model: string): SttEngine {
@@ -211,8 +209,8 @@ export interface SttDownloadStatus {
error: string | null;
/** The last download was stopped by the user rather than failing. */
cancelled?: boolean;
- /** Which model that cancellation applies to. `model` goes null once the worker thread
- * stops, so this is the only way to tell a settled cancellation from an unrelated one. */
+ /** Which model that cancellation applies to. `model` goes null once the worker thread stops,
+ * so this is the only way to tell a settled cancellation from an unrelated one. */
cancelled_model?: string | null;
bytes_total: number | null;
bytes_done: number | null;
@@ -244,8 +242,7 @@ export interface SttStatus {
mtmd?: SttEngineStatus;
}
-// Keep load/unload requests ordered so a new recording cannot race an unload
-// still finishing for the previous one.
+// Keep load/unload requests ordered so a new recording cannot race an unload still finishing for the previous one.
let sttLifecycle: Promise<void> = Promise.resolve();
function queueSttLifecycle(operation: () => Promise<void>): Promise<void> {
@@ -254,8 +251,8 @@ function queueSttLifecycle(operation: () => Promise<void>): Promise<void> {
return result;
}
-/** Report whether STT is installed and which model, if any, is resident.
- * Passing a model extends the downloaded check to custom repos. */
+/** Report whether STT is installed and which model, if any, is resident. Passing a model
+ * extends the downloaded check to custom repos. */
export async function fetchSttStatus(
refreshKey?: number,
model?: string,
@@ -271,9 +268,9 @@ export async function fetchSttStatus(
return (await response.json()) as SttStatus;
}
-/** The engine block that owns `model`. A curated Whisper prefers whisper.cpp,
- * but without whisper-server the backend serves it through Transformers, so
- * that is the fallback. mtmd models run nowhere else. */
+/** The engine block that owns `model`. A curated Whisper prefers whisper.cpp, but without
+ * whisper-server the backend serves it through Transformers, so that is the fallback.
+ * mtmd models run nowhere else. */
export function sttEngineStatusFor(
status: SttStatus,
model: string,
@@ -361,8 +358,8 @@ export async function startSttDownload(
}
}
-/** Stop an in-flight model download. Partial files stay cached, so starting the
- * same download again resumes from where it stopped. */
+/** Stop an in-flight model download. Partial files stay cached, so starting the same download
+ * again resumes from where it stopped. */
export async function cancelSttDownload(
model: string,
engine?: SttEngine,
@@ -381,10 +378,9 @@ export async function cancelSttDownload(
}
/** Release the local STT model and its RAM/VRAM allocations. */
-/** Release the dictation sidecar. `model` scopes the release to the model the caller
- * claims: another surface can switch the same engine between the ownership check and
- * this request arriving, and the backend compares under the sidecar's own lock rather
- * than releasing whatever is resident by then. */
+/** Release the dictation sidecar. `model` scopes the release to the model the caller claims:
+ * another surface can switch the same engine between the ownership check and this request,
+ * so the backend compares under the sidecar's own lock. */
export function unloadSttModel(
engine?: SttEngine,
model?: string,
@@ -411,11 +407,8 @@ export function unloadSttModel(
});
}
-/**
- * recorded-audio dictation. short recordings use one pass; long ones split near
- * whisper's 30s window. confirm keeps text, discard removes it, and either
- * releases the microphone immediately.
- */
+/** Recorded-audio dictation. Short recordings use one pass; long ones split near whisper's 30s
+ * window. Confirm keeps text, discard removes it, and either releases the microphone. */
export class StudioModelDictationAdapter implements DictationAdapter {
private readonly chatId: string | null | undefined;
@@ -437,9 +430,8 @@ export class StudioModelDictationAdapter implements DictationAdapter {
throw new Error("Recording is not supported in this browser.");
}
- // Pin the model, language, and linked chat chosen when recording began, so a
- // mid-session settings change or thread switch cannot affect later segments
- // or relink the saved transcript.
+ // Pin the model, language and linked chat chosen when recording began, so a mid-session
+ // settings change or thread switch cannot affect later segments or relink the transcript.
const settings = useVoiceSettingsStore.getState();
const usesExternalEndpoint = settings.dictationEngine === "custom";
const sessionProviderId = usesExternalEndpoint
@@ -488,9 +480,8 @@ export class StudioModelDictationAdapter implements DictationAdapter {
resolveEnded = resolve;
});
- // --- Background transcription pipeline ---------------------------------
- // Each segment is a self-contained clip transcribed on its own; results are
- // stored by index so the final text keeps its order.
+ // Background transcription pipeline. Each segment is a self-contained clip transcribed on its
+ // own; results are stored by index so the final text keeps its order.
type Segment = {
index: number;
chunks: Blob[];
@@ -517,8 +508,8 @@ export class StudioModelDictationAdapter implements DictationAdapter {
if (reportedTranscriptionError || cancelled || ended) return;
reportedTranscriptionError = true;
console.error("STT transcription error:", error);
- // An undownloaded model is the ordinary first-run state, not a failure.
- // Point at the download; never start it here.
+ // An undownloaded model is the ordinary first-run state, not a failure. Point at the
+ // download; never start it here.
if (
!usesExternalEndpoint &&
error instanceof SttModelNotDownloadedError
@@ -537,10 +528,9 @@ export class StudioModelDictationAdapter implements DictationAdapter {
onClick: () => useSettingsDialogStore.getState().openDialog("voice"),
},
});
- // The preload runs cache-only, so nothing it reports is transient: a
- // missing runtime or a load refused for training means no segment of
- // this session can be transcribed. End it rather than let the user keep
- // speaking into a recorder whose audio is already lost.
+ // The preload runs cache-only, so nothing it reports is transient: a missing runtime or a
+ // load refused for training means no segment of this session can be transcribed. End it
+ // rather than let the user keep speaking into a recorder whose audio is already lost.
if (stage === "preload") finishSession("cancelled");
};
@@ -611,9 +601,8 @@ export class StudioModelDictationAdapter implements DictationAdapter {
if (!cancelled) results[item.index] = text;
} catch (error) {
if (!cancelled && !abortController.signal.aborted) {
- // Keep transcribed segments, but never hide that part was lost.
- // Only a lost segment is partial: the model preload shares this
- // reporter and can fail without costing any audio.
+ // Keep transcribed segments, but never hide that part was lost. Only a lost segment is
+ // partial: the model preload shares this reporter and can fail without costing any audio.
markDictationFailed();
reportTranscriptionError(error);
}
@@ -624,10 +613,9 @@ export class StudioModelDictationAdapter implements DictationAdapter {
})();
};
- // Every non-empty recording is transcribed. The RMS meter only shapes
- // segment boundaries; a quiet microphone or suspended AudioContext can keep
- // it below VOICE_RMS for real speech, so it must never discard audio.
- // Whisper returns an empty transcript for genuine silence.
+ // Every non-empty recording is transcribed. The RMS meter only shapes segment boundaries; a
+ // quiet microphone or suspended AudioContext can keep it below VOICE_RMS for real speech,
+ // so it must never discard audio. Whisper returns an empty transcript for genuine silence.
const enqueueSegment = (index: number, blob: Blob) => {
if (blob.size > 0) {
queue.push({ index, blob });
@@ -674,8 +662,8 @@ export class StudioModelDictationAdapter implements DictationAdapter {
}
};
- // Close the current segment at a pause and open the next, so recording stays
- // continuous while each clip is independently decodable.
+ // Close the current segment at a pause and open the next, so recording stays continuous while
+ // each clip is independently decodable.
const cutSegment = () => {
const seg = currentSeg;
if (cutting || !seg || finalizing) return;
@@ -700,8 +688,8 @@ export class StudioModelDictationAdapter implements DictationAdapter {
startSegment();
};
- // Pause detector: mark voiced frames. Short dictations stay one segment; long
- // ones cut at a pause after the target duration, or at the hard limit.
+ // Pause detector: mark voiced frames. Short dictations stay one segment; long ones cut at a
+ // pause after the target duration, or at the hard limit.
onAudioFrame = (rawRms, now) => {
const seg = currentSeg;
if (!seg || finalizing) {
@@ -728,12 +716,11 @@ export class StudioModelDictationAdapter implements DictationAdapter {
stop: async () => {
if (!ended && !finalizing) {
finalizing = true;
- // Stop publishing zero-valued frames at once so the UI can switch to
- // its transcription shimmer.
+ // Stop publishing zero-valued frames at once so the UI can switch to its transcription shimmer.
stopLevelMeter();
const seg = currentSeg;
- // Cut the final segment (its buffer survives) so only the short tail is
- // left to transcribe, then release the mic immediately.
+ // Cut the final segment (its buffer survives) so only the short tail is left to transcribe,
+ // then release the mic immediately.
if (seg && seg.recorder.state !== "inactive") {
seg.recorder.addEventListener(
"stop",
diff --git a/studio/frontend/src/features/chat/adapters/studio-speech-synthesis-adapter.ts b/studio/frontend/src/features/chat/adapters/studio-speech-synthesis-adapter.ts
index be91a7d0630..cc480112cb7 100644
--- a/studio/frontend/src/features/chat/adapters/studio-speech-synthesis-adapter.ts
+++ b/studio/frontend/src/features/chat/adapters/studio-speech-synthesis-adapter.ts
@@ -10,9 +10,9 @@ import { getExternalProviderApiKey } from "../external-providers";
import { stripSearchImageTokens } from "../search-images/search-images";
import { useExternalProvidersStore } from "../stores/external-providers-store";
-/** Voice for a stored voiceURI. "default" resolves to the voice the platform
- * marks as its default, so the "System default" choice means what it says
- * instead of falling back to a curated pick. Undefined lets the browser pick. */
+/** Voice for a stored voiceURI. "default" resolves to the voice the platform marks as its
+ * default, so the "System default" choice means what it says instead of falling back to a
+ * curated pick. Undefined lets the browser pick. */
export function findTtsVoice(
voiceURI: string,
): SpeechSynthesisVoice | undefined {
@@ -65,8 +65,8 @@ function voiceBaseName(voice: SpeechSynthesisVoice): string {
return name;
}
-// Well-known natural English voices, best first. Breaks ties when the name
-// carries no quality hint, so basic voices are not just alphabetical.
+// Well-known natural English voices, best first. Breaks ties when the name carries no quality
+// hint, so basic voices are not just alphabetical.
const PREFERRED_VOICE_NAMES = [
"samantha",
"alex",
@@ -119,11 +119,9 @@ function langBase(tag: string): string {
const MAX_CURATED_VOICES = 20;
-/**
- * Keep the best, most relevant voices: drop low-quality ones, keep English,
- * the browser language, and the dictation language, rank by quality hints,
- * and cap the list. The selected voice is always kept.
- */
+/** Keep the best, most relevant voices: drop low-quality ones, keep English, the browser
+ * language and the dictation language, rank by quality hints, and cap the list. The selected
+ * voice is always kept. */
export function curateSystemVoices(
voices: SpeechSynthesisVoice[],
selectedVoiceURI?: string,
@@ -137,8 +135,8 @@ export function curateSystemVoices(
wantedLangs.add(langBase(dictationLanguage));
}
- // WebKit and Linux engines report voices with empty or duplicate voiceURIs;
- // drop them so the Radix Select never gets an empty or colliding value.
+ // WebKit and Linux engines report voices with empty or duplicate voiceURIs; drop them so the
+ // Radix Select never gets an empty or colliding value.
const seenVoiceURIs = new Set<string>();
const kept = voices.filter((voice) => {
if (!voice.voiceURI || seenVoiceURIs.has(voice.voiceURI)) return false;
@@ -159,9 +157,8 @@ export function curateSystemVoices(
return a.name.localeCompare(b.name);
});
- // macOS reports some voices twice (compact + enhanced) under one name. Keep
- // one per name and language, preferring the selected voice then the best
- // ranked, so no duplicates show.
+ // macOS reports some voices twice (compact + enhanced) under one name. Keep one per name and
+ // language, preferring the selected voice then the best ranked.
const keyOf = (voice: SpeechSynthesisVoice) =>
`${voiceBaseName(voice)}|${voice.lang.toLowerCase()}`;
const winners = new Map<string, string>();
@@ -188,10 +185,8 @@ export function curateSystemVoices(
return curated;
}
-/**
- * Best voice when none is chosen. The browser default on macOS is often a
- * robotic legacy voice, so fall back to the top curated voice instead.
- */
+/** Best voice when none is chosen. The browser default on macOS is often a robotic legacy
+ * voice, so fall back to the top curated voice instead. */
function defaultTtsVoice(): SpeechSynthesisVoice | undefined {
if (typeof window === "undefined" || !window.speechSynthesis) {
return undefined;
@@ -275,8 +270,8 @@ export async function generateCustomTtsAudio(
"Custom TTS is not configured. Pick a connection and model in Settings → Voice.",
);
}
- // A browser whose key migration failed keeps the connection selectable on a
- // retained legacy key; send it like chat and STT do, or this call is unauthenticated.
+ // A browser whose key migration failed keeps the connection selectable on a retained legacy
+ // key; send it like chat and STT do, or this call is unauthenticated.
const provider = providersState.providers.find(
(candidate) => candidate.id === ttsProviderId,
);
@@ -293,9 +288,9 @@ export async function generateCustomTtsAudio(
? await encryptProviderApiKey(legacyApiKey)
: "";
- // Encryption and auth refresh both yield. Reuse this check before the first
- // request and every authFetch retry so neither path can release assistant text
- // or a retained key after the frontend-only connection policy changes.
+ // Encryption and auth refresh both yield. Reuse this check before the first request and every
+ // authFetch retry so neither path can release assistant text or a retained key after the
+ // frontend-only connection policy changes.
const assertConnectionSnapshot = () => {
const currentProvidersState = useExternalProvidersStore.getState();
if (!currentProvidersState.connectionsEnabled) {
@@ -398,8 +393,8 @@ function speakWithBackendAudio(
audio = new Audio(url);
audio.playbackRate = ttsRate;
audio.volume = ttsVolume;
- // Some browsers reset playbackRate to 1 once the source loads; reapply
- // it on loadedmetadata so the speed setting reliably takes effect.
+ // Some browsers reset playbackRate to 1 once the source loads; reapply it on loadedmetadata so
+ // the speed setting reliably takes effect.
audio.addEventListener("loadedmetadata", () => {
if (audio) audio.playbackRate = ttsRate;
});
@@ -431,10 +426,8 @@ function speakWithBackendAudio(
};
}
-/**
- * Text-to-speech for assistant messages. Reads Voice settings at speak time.
- * Engines: "system" (speechSynthesis), "studio" (local TTS model), "custom" (a connection).
- */
+/** Text-to-speech for assistant messages. Reads Voice settings at speak time. Engines:
+ * "system" (speechSynthesis), "studio" (local TTS model), "custom" (a connection). */
export class StudioSpeechSynthesisAdapter implements SpeechSynthesisAdapter {
/** Web Speech synthesis, used by the "system" engine. */
static systemVoicesSupported(): boolean {
@@ -445,8 +438,8 @@ export class StudioSpeechSynthesisAdapter implements SpeechSynthesisAdapter {
);
}
- // The "studio" engine only needs fetch + Audio playback, so a WebView
- // without Web Speech synthesis can still read aloud through the backend.
+ // The "studio" engine only needs fetch plus Audio playback, so a WebView without Web Speech
+ // synthesis can still read aloud through the backend.
static isSupported(): boolean {
return (
StudioSpeechSynthesisAdapter.systemVoicesSupported() ||
@@ -464,8 +457,8 @@ export class StudioSpeechSynthesisAdapter implements SpeechSynthesisAdapter {
error?: unknown,
) => {
if (res.status.type === "ended") return;
- // Surface genuine read-aloud failures; a cancelled/interrupted utterance
- // is a normal stop, not an error, and must not toast.
+ // Surface genuine read-aloud failures; a cancelled or interrupted utterance is a normal stop,
+ // not an error, and must not toast.
if (
reason === "error" &&
error !== "interrupted" &&
@@ -502,8 +495,8 @@ export class StudioSpeechSynthesisAdapter implements SpeechSynthesisAdapter {
},
};
- // Fall back to the backend model when the runtime lacks Web Speech
- // synthesis (e.g. an audio-only WebView), so read-aloud still works.
+ // Fall back to the backend model when the runtime lacks Web Speech synthesis, so read-aloud
+ // still works.
if (
ttsEngine !== "system" ||
!StudioSpeechSynthesisAdapter.systemVoicesSupported()
@@ -514,8 +507,8 @@ export class StudioSpeechSynthesisAdapter implements SpeechSynthesisAdapter {
: generateStudioTtsAudio;
const session = speakWithBackendAudio(generate, text, handleEnd, () => {
if (res.status.type === "ended") return;
- // Notify subscribers of the async starting -> running transition;
- // the adapter contract drives UI state off these subscribe callbacks.
+ // Notify subscribers of the async starting -> running transition; the adapter contract drives
+ // UI state off these subscribe callbacks.
res.status = { type: "running" };
for (const handler of subscribers) handler();
});
@@ -527,8 +520,8 @@ export class StudioSpeechSynthesisAdapter implements SpeechSynthesisAdapter {
utterance.addEventListener("end", () => handleEnd("finished"));
utterance.addEventListener("error", (e) => handleEnd("error", e.error));
- // Chrome silently drops speak() while another utterance is queued from a
- // cancelled run; clearing first keeps read-aloud deterministic.
+ // Chrome silently drops speak() while another utterance is queued from a cancelled run;
+ // clearing first keeps read-aloud deterministic.
window.speechSynthesis.cancel();
window.speechSynthesis.speak(utterance);
res.status = { type: "running" };
diff --git a/studio/frontend/src/features/chat/adapters/studio-web-speech-dictation-adapter.ts b/studio/frontend/src/features/chat/adapters/studio-web-speech-dictation-adapter.ts
index a8ff10e4778..ad6aee0c895 100644
--- a/studio/frontend/src/features/chat/adapters/studio-web-speech-dictation-adapter.ts
+++ b/studio/frontend/src/features/chat/adapters/studio-web-speech-dictation-adapter.ts
@@ -24,10 +24,8 @@ export function activeDictationChatId(): string | undefined {
return useChatRuntimeStore.getState().activeThreadId ?? undefined;
}
-/**
- * Resolve the chat a saved dictation links to. undefined falls back to the
- * active single chat; null (composers without one, e.g. Compare) means none.
- */
+/** Resolve the chat a saved dictation links to. undefined falls back to the active single chat;
+ * null (composers without one, e.g. Compare) means none. */
export function resolveDictationChatId(
chatId: string | null | undefined,
): string | undefined {
@@ -38,14 +36,12 @@ export function resolveDictationChatId(
// Reused id so repeated network failures replace, not stack, the same toast.
const NETWORK_TOAST_ID = "dictation-network-offline";
-// Short grace for the browser to finalize its latest interim hypothesis. If it
-// doesn't, promote that text instead of stalling on a finalization spinner.
+// Short grace for the browser to finalize its latest interim hypothesis. If it does not,
+// promote that text instead of stalling on a finalization spinner.
const STOP_FINALIZATION_GRACE_MS = 350;
-/**
- * A dictation session with an extra onEnd hook (not part of the assistant-ui
- * interface) so non-runtime callers can reset when the session ends by itself.
- */
+/** A dictation session with an extra onEnd hook (not part of the assistant-ui interface) so
+ * non-runtime callers can reset when the session ends by itself. */
export type StudioDictationSession = DictationAdapter.Session & {
onEnd?: (callback: () => void) => () => void;
};
@@ -136,8 +132,8 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
static isSupported(): boolean {
return (
typeof window !== "undefined" &&
- // WKWebView exposes the API but Apple's service refuses it
- // (service-not-allowed); WebView2 and webkit2gtk have no engine at all.
+ // WKWebView exposes the API but Apple's service refuses it (service-not-allowed); WebView2 and
+ // webkit2gtk have no engine at all.
!isTauri &&
window.isSecureContext &&
getSpeechRecognitionAPI() !== undefined &&
@@ -156,8 +152,8 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
recognition.lang = this.language ?? resolveDictationLanguage();
recognition.continuous = this.continuous;
recognition.interimResults = this.interimResults;
- // Pin the linked chat now so a thread switch during finalization cannot
- // relink the transcript to the newly opened chat.
+ // Pin the linked chat now so a thread switch during finalization cannot relink the transcript
+ // to the newly opened chat.
const sessionChatId = resolveDictationChatId(this.chatId);
const speechStartCallbacks = new Set<() => void>();
@@ -193,8 +189,8 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
stopping = true;
stopRequestedAt = performance.now();
recognition.stop();
- // Ending the track now gives the browser an audio endpoint to finalize
- // and releases the mic without waiting on its remote speech service.
+ // Ending the track now gives the browser an audio endpoint to finalize and releases the mic
+ // without waiting on its remote speech service.
stopLevelMeter();
stopStream(stream);
stream = null;
@@ -232,8 +228,8 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
};
},
- // Beyond DictationAdapter: lets callers reset UI when the session ends on
- // its own (silence, error), not just via stop().
+ // Beyond DictationAdapter: lets callers reset UI when the session ends on its own (silence,
+ // error), not just via stop().
onEnd: (callback: () => void) => {
endCallbacks.add(callback);
return () => {
@@ -293,8 +289,7 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
}
recordRecentDictation(transcript, sessionChatId);
}
- // assistant-ui uses this callback to leave dictation mode; required even
- // for silence and cancelled recordings.
+ // assistant-ui uses this callback to leave dictation mode; required even for silence and cancelled recordings.
for (const callback of speechEndCallbacks) {
callback({ transcript });
}
@@ -326,8 +321,8 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
if (result.isFinal) {
interimParts.delete(i);
const corrected = applyDictationDictionary(transcript);
- // Join final chunks with a single space so recorded transcripts do
- // not merge words when a browser omits leading whitespace.
+ // Join final chunks with a single space so recorded transcripts do not merge words when a
+ // browser omits leading whitespace.
const trimmed = corrected.trim();
if (trimmed) {
finalTranscript = finalTranscript
@@ -370,8 +365,8 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
);
console.error("Dictation error:", errorEvent.error, errorEvent.message);
if (errorEvent.error === "network") {
- // Online speech service unreachable; point the user to the offline
- // local engine (the toast opens Voice settings).
+ // Online speech service unreachable; point the user to the offline local engine (the toast
+ // opens Voice settings).
toast.error("No internet connection", {
id: NETWORK_TOAST_ID,
description:
@@ -405,9 +400,8 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
: baseAudio,
});
} catch (error) {
- // Saved mic may be unplugged; fall back to the default device.
- // Firefox and WebKit throw OverconstrainedError objects that are
- // not DOMException instances, so match on the error name.
+ // Saved mic may be unplugged; fall back to the default device. Firefox and WebKit throw
+ // OverconstrainedError objects that are not DOMException instances, so match on the name.
if (micDeviceId !== "default" && isMissingDeviceError(error)) {
stream = await navigator.mediaDevices.getUserMedia({
audio: baseAudio,
@@ -432,9 +426,8 @@ export class StudioWebSpeechDictationAdapter implements DictationAdapter {
try {
recognition.start(audioTrack);
} catch (error) {
- // Older engines expose only start(); retry without the experimental
- // track overload. Recognition then captures from the default device,
- // so release the selected-device stream instead of holding it open.
+ // Older engines expose only start(); retry without the experimental track overload. Recognition
+ // then captures from the default device, so release the selected-device stream.
console.debug(
"Dictation start(audioTrack) failed; retrying start().",
error,
diff --git a/studio/frontend/src/features/chat/api-provider-logo.tsx b/studio/frontend/src/features/chat/api-provider-logo.tsx
index 6813163819b..7ac3ac94dc2 100644
--- a/studio/frontend/src/features/chat/api-provider-logo.tsx
+++ b/studio/frontend/src/features/chat/api-provider-logo.tsx
@@ -6,10 +6,8 @@ import { DashboardSquare01Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { isCustomProviderType } from "./external-providers";
-/**
- * Registry logos at `public/provider-logos/{provider_type}.{ext}`; key matches
- * `PROVIDER_REGISTRY` (lowercase). Extension varies per asset (svg preferred).
- */
+/** Registry logos at `public/provider-logos/{provider_type}.{ext}`; key matches `PROVIDER_REGISTRY`
+ * (lowercase). Extension varies per asset, svg preferred. */
const PROVIDER_LOGO_EXT: Record<string, "svg" | "png" | "jpg"> = {
openai: "svg",
mistral: "svg",
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index cb4e4531be9..10bfa0813e2 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -307,8 +307,7 @@ import {
supportsChatGenerationRuns,
} from "./chat-generation-api";
-// Small models (<=9B) answer from memory instead of calling search, so "auto"
-// forces retrieval for them and leaves it to larger ones.
+// Small models (<=9B) answer from memory, so "auto" forces retrieval for them.
const AUTOINJECT_AUTO_MAX_SIZE_B = 9;
class ChatGenerationTerminalError extends Error {
@@ -327,7 +326,6 @@ function resolveAutoInject(mode: RagAutoInject, checkpoint: string): boolean {
if (mode === "on") return true;
if (mode === "off") return false;
const size = parseParamCountB(checkpoint);
- // Unknown size -> enable.
return size === null || size <= AUTOINJECT_AUTO_MAX_SIZE_B;
}
@@ -336,8 +334,7 @@ interface ServerUsage {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
- // External prompt-cache fields (see _build_usage_chunk in
- // external_provider.py); cache_creation is Anthropic-only.
+ // External prompt-cache fields (external_provider.py); cache_creation is Anthropic-only.
prompt_tokens_details?: {
cached_tokens?: number;
};
@@ -365,8 +362,7 @@ interface ServerTimings {
diffusion_prompt_prepare_ms?: number;
diffusion_decode_ms?: number;
diffusion_wall_ms?: number;
- // Honest throughput, matching the standalone diffusion CLI:
- // effective = canvas*blocks/wall, parallel = canvas/per_step, output = answer tokens/wall.
+ // effective = canvas*blocks/wall, parallel = canvas/per_step, output = answer tokens/wall.
diffusion_effective_tok_s?: number;
diffusion_parallel_tok_s?: number;
diffusion_output_tok_s?: number;
@@ -411,9 +407,7 @@ type OpenAIStreamAdapterOptions = {
/** Tracks which user messages were sent with an audio file (messageId → filename). */
export const sentAudioNames = new Map<string, string>();
-// Synthetic provider-side tool names; backend stamps args._server_tool so
-// user functions with the same name aren't dropped. Mirror of backend
-// _SERVER_SIDE_BUILTIN_TOOL_NAMES.
+// Synthetic provider-side tool names; mirror of backend _SERVER_SIDE_BUILTIN_TOOL_NAMES.
const SERVER_SIDE_BUILTIN_TOOL_NAMES = new Set<string>([
"web_search",
"web_fetch",
@@ -421,12 +415,8 @@ const SERVER_SIDE_BUILTIN_TOOL_NAMES = new Set<string>([
"image_generation",
]);
-/**
- * Whether a persisted tool-call part is provider-side synthetic and should
- * be stripped from outbound history. Matches on the args._server_tool marker
- * or a Gemini native_part payload (no shape heuristic, since user functions
- * can legitimately share a name).
- */
+/** Whether a persisted tool-call part is provider-side synthetic (args._server_tool or Gemini
+ * native_part), and so must be stripped from outbound history. */
function isServerSideBuiltinToolPart(
toolNameLower: string,
_argsObj: Record<string, unknown> | null,
@@ -452,12 +442,8 @@ function wait(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
-/**
- * Best-effort partial parse of a live tool_args stream into a tool part's
- * `args`, so cards render the payload while the model is still writing it. The
- * structured path streams raw arguments JSON; the text path wraps it in call
- * markup, unwrapped here. Returns null until something parses; never throws.
- */
+/** Best-effort partial parse of a live tool_args stream so cards render while the model writes.
+ * Returns null until something parses; never throws. */
function parseLiveToolArgs(
raw: string,
): { args: Record<string, unknown>; argsText: string } | null {
@@ -471,11 +457,9 @@ function parseLiveToolArgs(
| Record<string, unknown>
| undefined;
if (!parsed || typeof parsed !== "object") return null;
- // Call envelope from the text path: unwrap to the arguments payload.
const inner = parsed.arguments ?? parsed.parameters;
if (typeof parsed.name === "string" && inner !== undefined) {
if (typeof inner === "string") {
- // Stringified arguments: partial-parse the inner JSON string.
const innerParsed = parsePartialJsonObject(inner) as
| Record<string, unknown>
| undefined;
@@ -505,7 +489,6 @@ function parseSystemVariablesMap(raw: string): Record<string, unknown> {
return parsed as Record<string, unknown>;
}
} catch {
- // Invalid JSON: keep unresolved placeholders in output prompt.
}
return {};
}
@@ -644,33 +627,17 @@ export function useThreadAutosaveHandle(): ThreadAutosaveHandle {
return ThreadAutosaveHandle;
}
-/**
- * Match error messages indicating the request filled (or would fill) the KV
- * cache, so the UI can toast a pointer at the ``Context Length`` setting.
- *
- * Two wordings reach the client and both must hit:
- * 1. Raw llama-server text when ``--no-context-shift`` trips:
- * "the request exceeds the available context size (N tokens)".
- * 2. The friendly rewrite from
- * ``backend/routes/inference.py::_friendly_error``: "Message too long:
- * ... context window. ..." (the one most users see on the streaming GGUF
- * path). Its advice branches, so only the lead-in is stable to match on.
- *
- * Match substrings, not full regexes: both layers have drifted across
- * versions (llama.cpp phrasing and _friendly_error copy edits).
- */
+/** Match errors meaning the KV cache filled: llama-server's "exceeds the available context
+ * size" and _friendly_error's "Message too long". Substrings, since both have drifted. */
export function isContextLimitError(message: string): boolean {
if (!message) return false;
const m = message.toLowerCase();
return (
- // Raw llama-server wording.
m.includes("context size") ||
m.includes("context shift") ||
m.includes("exceeds the available context") ||
- // Backend _friendly_error rewrite.
m.includes("message too long") ||
m.includes("context window") ||
- // n_ctx mentions that carry an "exceed"/"full" signal.
(m.includes("n_ctx") && (m.includes("exceed") || m.includes("full")))
);
}
@@ -688,12 +655,8 @@ async function updateStoredChatThreadEventually(
}
}
-/**
- * Return ``raw`` when it is a safe-to-navigate http(s) URL, else "".
- * Rejects non-string input, CR/LF (header injection), and non-http(s)
- * schemes (``javascript:`` / ``data:`` / ``vbscript:``) so provider/tool-
- * controlled strings cannot land in an <a href>.
- */
+/** Return `raw` when it is a safe http(s) URL, else "": rejects CR/LF and javascript:/data:/
+ * vbscript: so provider strings cannot land in an <a href>. */
function isSafeNavigableSourceUrl(raw: unknown): string {
if (typeof raw !== "string") return "";
const value = raw.trim();
@@ -704,7 +667,6 @@ function isSafeNavigableSourceUrl(raw: unknown): string {
return value;
}
} catch {
- // Fall through.
}
return "";
}
@@ -728,19 +690,15 @@ function documentCitationToSource(
"";
const docIndex =
typeof cit.document_index === "number" ? cit.document_index : undefined;
- // Only treat ``source`` as navigable when it is real http(s);
- // search_result_location can carry a free-form id (e.g. ``kb-doc-42``)
- // or a hostile scheme. Fall back to a stable doc anchor otherwise.
+ // search_result_location `source` can be a free-form id or a hostile scheme; fall back to a
+ // doc anchor unless it is real http(s).
const url =
isSafeNavigableSourceUrl(source) ||
`#anthropic-doc-${docIndex ?? fallbackIdx}`;
const title = docTitle || source || `Document ${fallbackIdx + 1}`;
const cited = typeof cit.cited_text === "string" ? cit.cited_text.trim() : "";
- // Trim the cited snippet so the Sources panel stays scannable.
const description = cited.length > 240 ? `${cited.slice(0, 240)}...` : cited;
- // Anthropic numbers inline [N] per citation, not per source URL.
- // Fold citation type + position-bearing fields into the id so distinct
- // citations on the same source keep separate Sources entries.
+ // Anthropic numbers inline [N] per citation, so fold citation type and position into the id.
const citationType = typeof cit.type === "string" ? String(cit.type) : "";
const positionParts = [
cit.search_result_index,
@@ -792,9 +750,7 @@ function parseSourcesFromResult(raw: string): {
const urlMatch = block.match(/URL:\s*(.+)/);
const snippetMatch = block.match(/Snippet:\s*(.+)/);
if (titleMatch && urlMatch) {
- // Drop blocks whose ``URL:`` is not safe http(s); provider/tool
- // output is attacker-controllable, so a hostile scheme must not
- // reach the Sources panel <a href>.
+ // Provider output is attacker-controllable: a non-http(s) URL must not reach the Sources panel <a href>.
const url = isSafeNavigableSourceUrl(urlMatch[1]);
if (!url) continue;
const snippet = snippetMatch?.[1]?.trim();
@@ -958,10 +914,8 @@ function toOpenAIImageEditReferenceMessage(
return { role: "assistant", content };
}
-// Refusal flag stamped on assistant metadata when the backend emits the
-// `anthropic_refusal` _toolEvent. We drop the refused pair from the next
-// request body (Anthropic: leaving refusals in context keeps refusing).
-// Using metadata, not text, prevents content from spoofing a reset.
+// Refusal marker on metadata, not text, which content could spoof: Anthropic keeps refusing
+// while refusals stay in context.
function isAnthropicRefusalMessage(message: RunMessage): boolean {
if (message.role !== "assistant") return false;
const metadata = (message as { metadata?: unknown }).metadata as
@@ -982,12 +936,8 @@ type SerializedMessage = {
}>;
tool_call_id?: string;
name?: string;
- /**
- * Gemini text-part thoughtSignature stashed during streaming on the
- * last text MessagePart. Backend reads
- * `extra_content.google.thought_signature` and attaches it to the
- * matching Gemini text part on the outbound turn.
- */
+ /** Gemini text-part thoughtSignature stashed while streaming; the backend reads it from
+ * extra_content.google.thought_signature. */
extra_content?: unknown;
};
@@ -1097,9 +1047,8 @@ function serializeAssistantToolCallPart(
arguments: argumentsStr,
},
};
- // Promote args.google to extra_content.google so the backend
- // native_part replay branch can find it. The backend only inspects
- // extra_content, not function.arguments.
+ // Promote args.google to extra_content.google: the backend replay branch only inspects extra_content.
+ // The backend inspects extra_content, not function.arguments.
if (tc.extra_content !== undefined) {
entry.extra_content = tc.extra_content;
} else if (argsGoogle) {
@@ -1113,13 +1062,8 @@ export interface McpImageToolResult {
images: { data: string; mimeType: string }[];
}
-/**
- * The text the model actually saw, for a result that may be wrapped.
- *
- * Chat replay and every export path have to agree on this: exports feed
- * fine-tuning datasets, so a wrapper serialized whole would train on the card's
- * sessionId/images/files instead of the tool's output.
- */
+/** The text the model actually saw, for a result that may be wrapped. Exports feed fine-tuning
+ * datasets, so a serialized wrapper would train on the card's metadata. */
export function toolResultModelText(
result: unknown,
toolName?: string,
@@ -1134,13 +1078,8 @@ export function toolResultModelText(
return result;
}
-/**
- * A wrapper this app put around a result, rather than a result shaped like one.
- *
- * The shape is only that: an MCP or custom tool answering with text, sessionId
- * and images is someone else's, and unwrapping it drops every other field it
- * returned. The backend gates the same strip on the tool name.
- */
+/** A wrapper this app added, not a result merely shaped like one: unwrapping someone else's
+ * MCP result drops every other field it returned. */
function isSandboxWrapper(
result: unknown,
toolName?: string,
@@ -1176,7 +1115,6 @@ function serializeToolResultPart(
const result = (tc as { result?: unknown }).result;
const { isServerSideBuiltin } = getToolPartReplayMetadata(tc);
- // Skip provider-side builtins; see isServerSideBuiltinToolPart.
if (isServerSideBuiltin) {
return null;
}
@@ -1184,21 +1122,15 @@ function serializeToolResultPart(
let content: string;
if (typeof result === "string") {
- // Backend ChatMessage validator rejects role="tool" with empty
- // content; serialise a sentinel JSON so legitimately empty tool
- // outputs still round-trip the follow-up turn to the provider.
+ // Backend ChatMessage rejects role="tool" with empty content; a sentinel JSON round-trips it.
content = result.length > 0 ? result : JSON.stringify({ result: "" });
} else if (
isMcpImageToolResult(result) ||
isSearchImagesToolResult(result) ||
isSandboxWrapper(result, tc.toolName ?? "")
) {
- // Replay the stdout the model saw, not the card's sessionId/images/files:
- // stringifying the wrapper feeds it internal metadata instead of the output.
- // The image tokens go with them: a token resolves against the message whose
- // search produced it, so a model copying one out of replayed history names
- // an id the next message cannot resolve and the picture silently does not
- // render. Without them it searches again, and that token does resolve.
+ // Replay the stdout the model saw, not the wrapper; image tokens go with it, since a token
+ // resolves only against the message whose search produced it.
const replayText = isSearchImagesToolResult(result)
? stripSearchImageTokens(result.text)
: result.text;
@@ -1220,10 +1152,8 @@ function serializeToolResultPart(
}
function canReplayToolCallWithoutRoleTool(part: ToolCallMessagePart): boolean {
- // Gemini/OpenAI provider-native builtin cards replay through
- // extra_content/native parts and intentionally do not produce role="tool"
- // messages. Local/user tool calls must have a concrete tool result before
- // they are replayed ahead of later assistant text.
+ // Provider-native builtin cards replay via extra_content and produce no role="tool" message;
+ // local tool calls must have a concrete result first.
return getToolPartReplayMetadata(part).isServerSideBuiltin;
}
@@ -1248,8 +1178,7 @@ function studioToolHistoryRequestFieldsAfterReplay(
}
function sanitizeAssistantReplayText(text: string): string {
- // Same reason as the tool result above: the tokens the model wrote last turn
- // are renderer markup scoped to that message, not prose it should repeat.
+ // The tokens the model wrote last turn are renderer markup scoped to that message, not prose to repeat.
return stripSearchImageTokens(text).replace(
/data:audio\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+/g,
"[audio]",
@@ -1328,8 +1257,7 @@ function serializeAssistantReplayMessages(
includeReasoningContent = false,
): SerializedMessage[] {
if (isAnthropicRefusalMessage(message)) {
- // Prune refused assistant turn from outbound history; the
- // rendered transcript still shows the user-visible notice.
+ // Prune the refused assistant turn from outbound history; the transcript still shows the notice.
return [];
}
@@ -1378,9 +1306,7 @@ function serializeAssistantReplayMessages(
};
if (hasToolCalls) {
assistantMessage.tool_calls = pendingToolCalls;
- // OpenAI requires content === null on assistant turns whose
- // payload is entirely tool_calls (matches the wire shape Gemini
- // expects for the next functionCall replay).
+ // OpenAI requires content === null on assistant turns whose payload is entirely tool_calls.
if (!hasContent) {
assistantMessage.content = null;
}
@@ -1507,36 +1433,25 @@ function toOpenAIMessages(
];
}
-/** Payload the turn carries in its own parts, so that a turn holding real history is kept
- * even when the replay serialiser emits nothing for it.
- *
- * Tool calls are deliberately absent. A call the serialiser can carry leaves `tool_calls` on
- * the wire and is caught by the shape check below; a call it cannot carry (a local call
- * Stopped before its result arrived, which OpenAI forbids replaying without a responding
- * `role="tool"` message) reaches the provider as nothing. Counting the second as payload keeps
- * a turn the backend then drops (`_drop_empty_assistant_sentinels`), so
- * `_coalesce_consecutive_user_turns` merges the two user turns, resending the cancelled
- * prompt and inviting the tool request again. */
+/** Payload the turn carries in its own parts. Tool calls are deliberately absent: counting an
+ * uncarryable call as payload keeps a turn the backend drops, merging two user turns. */
function assistantTurnCarriesPayload(message: RunMessage): boolean {
for (const part of message.content ?? []) {
- // sanitizeAssistantReplayText only substitutes, never empties, so the raw part is
- // equivalent here and keeps the scan off that regex.
+ // sanitizeAssistantReplayText only substitutes, never empties, so the raw part is equivalent here.
if (part.type === "text" && part.text.trim().length > 0) return true;
}
if (collectImageParts(message).length > 0) return true;
return "attachments" in message && (message.attachments?.length ?? 0) > 0;
}
-/** Whitespace is not content: `_build_external_messages` (studio/backend/routes/inference.py)
- * drops any assistant turn whose string content trims away, so counting it here would keep a
- * pair the backend then splits apart again. */
+/** Whitespace is not content: _build_external_messages drops any assistant turn whose content trims away. */
function hasReplayContent(content: OpenAIMessageContent | null): boolean {
if (typeof content === "string") return content.trim().length > 0;
return Boolean(content);
}
-/** `status` is session state only (a reloaded thread comes back stamped complete), so the
- * persisted marker is read alongside it. */
+/** `status` is session state only (a reloaded thread comes back complete), so the persisted
+ * marker is read alongside it. */
function assistantTurnEndedEarly(message: RunMessage): boolean {
return (
message.status?.type === "incomplete" ||
@@ -1544,16 +1459,16 @@ function assistantTurnEndedEarly(message: RunMessage): boolean {
);
}
-/** A Stop that landed before the turn produced anything: it serialises to a lone empty
- * assistant message, which every backend drops, stranding two user turns in a row. */
+/** A Stop before the turn produced anything serialises to a lone empty assistant message,
+ * stranding two user turns in a row. */
function isAbandonedAssistantTurn(
message: RunMessage,
includeReasoningContent: boolean,
): boolean {
if (message.role !== "assistant") return false;
if (assistantTurnCarriesPayload(message)) return false;
- // A turn that finished on reasoning alone is a reply; on an external request it serialises
- // empty only because reasoning is stripped there, which must not read as abandoned.
+ // A turn that finished on reasoning alone is a reply, even though external requests strip
+ // reasoning and serialise it empty.
if (
!assistantTurnEndedEarly(message) &&
(message.content ?? []).some((part) => part.type === "reasoning")
@@ -1570,8 +1485,8 @@ function isAbandonedAssistantTurn(
);
}
-/** Drop refused and abandoned assistant turns along with the user prompt that triggered
- * them: a refusal re-triggers the classifier, an abandoned turn breaks role alternation. */
+/** Drop refused and abandoned assistant turns with their prompt: a refusal re-triggers the
+ * classifier, an abandoned turn breaks role alternation. */
function pruneOutboundHistory(
messages: RunMessages,
includeReasoningContent: boolean,
@@ -1580,8 +1495,7 @@ function pruneOutboundHistory(
const abandoned = history.map((message) =>
isAbandonedAssistantTurn(message, includeReasoningContent),
);
- // A prompt is only stranded when something survives after it, so a thread whose last turn
- // was abandoned keeps its trailing prompt: that is the history the next send needs.
+ // A prompt is only stranded when something survives after it, so a trailing abandoned turn keeps its prompt.
let lastSurviving = -1;
for (let index = history.length - 1; index >= 0; index -= 1) {
if (!abandoned[index] && !isAnthropicRefusalMessage(history[index])) {
@@ -1624,7 +1538,6 @@ function findLatestUserImageBase64(messages: RunMessages): string | undefined {
continue;
}
- // Image in message.content (e.g. compare view).
for (const part of message.content ?? []) {
if (part.type === "image" && "image" in part) {
const encoded = extractImageBase64(part.image);
@@ -1632,7 +1545,6 @@ function findLatestUserImageBase64(messages: RunMessages): string | undefined {
}
}
- // Image in message.attachments (e.g. chat composer).
if ("attachments" in message && (message.attachments?.length ?? 0) > 0) {
for (const attachment of message.attachments ?? []) {
for (const part of attachment.content ?? []) {
@@ -1666,9 +1578,7 @@ function extractAudioPartBase64(
return raw.startsWith("data:") ? raw.split(",")[1] : raw;
}
-// Exported for tests.
-// Whether any message carries an image, by the rule collectImageParts pushes one. A predicate,
-// not that helper: building the parts would copy the base64 this exists to avoid touching.
+// A predicate rather than collectImageParts: building the parts would copy the base64 this exists to avoid touching.
export function messagesContainImage(messages: RunMessages): boolean {
const isImage = (part: { type: string }) =>
part.type === "image" &&
@@ -1689,13 +1599,9 @@ export function messagesContainImage(messages: RunMessages): boolean {
return false;
}
-// A recording or a clip the user supplied is their own content in exactly the sense the
-// text-attachment rule already covers: the model answers from what is inside it, so the
-// subjects of that answer must not reach the image engines either. Matched on the part
-// TYPE rather than through the base64 extractors above -- a clip whose payload does not
-// parse is still a clip, and a predicate that fails open on it is the wrong way round.
-// Video arrives as a file part carrying a video mime type, the shape
-// extractVideoPartBase64 reads.
+// Matched on the part TYPE, not the base64 extractors: a clip whose payload does not parse is
+// still user content and must not reach the image engines.
+// Video arrives as a file part carrying a video mime type, the shape extractVideoPartBase64 reads.
function isPrivateMediaPart(part: { type: string }): boolean {
if (part.type === "audio") return true;
return (
@@ -1734,13 +1640,11 @@ export function findLatestUserAudioBase64(
const message = messages[i];
if (!message || message.role !== "user") continue;
- // Message content parts (from compare view's CompareMessagePart with type: "audio")
for (const part of message.content ?? []) {
const base64 = extractAudioPartBase64(part);
if (base64) return base64;
}
- // Attachment content parts (from AudioAttachmentAdapter)
if ("attachments" in message) {
for (const attachment of message.attachments ?? []) {
for (const part of attachment.content ?? []) {
@@ -1750,15 +1654,13 @@ export function findLatestUserAudioBase64(
}
}
- // Only the newest user message counts. audio_base64 switches the
- // backend onto the audio generation path, so replaying audio from an
- // older turn would hijack text follow-ups (Whisper would retranscribe
- // the stale clip). Matches the consumed-on-send semantics of the
- // legacy pendingAudio path.
+ // Only the newest user message counts: audio_base64 switches the backend onto the audio path,
+ // so a stale clip would hijack text follow-ups.
+ // Replaying audio from an older turn would hijack text follow-ups, since Whisper would
+ // retranscribe the stale clip. Matches the consumed-on-send semantics of the legacy pendingAudio.
break;
}
- // Runtime store (main composer's audio upload).
const pendingAudio = includePendingAudio
? useChatRuntimeStore.getState().pendingAudioBase64
: null;
@@ -1777,9 +1679,7 @@ function extractVideoPartBase64(
: filePart.data;
}
-/** Base64 of the clip on the newest user turn. Only the newest counts, like
- * audio: replaying an older one would re-sample it into frames and spend the
- * context of every text follow-up. */
+/** Base64 of the clip on the newest user turn only; replaying an older one would re-sample it into frames. */
export function findLatestUserVideoBase64(
messages: RunMessages,
): string | undefined {
@@ -1803,18 +1703,14 @@ export function findLatestUserVideoBase64(
return undefined;
}
-// The Canvas instructions createOpenAIStreamAdapter appends, named so the recount prices the same
-// text the request carries.
+// The Canvas instructions createOpenAIStreamAdapter appends, so the recount prices the same text.
export const CANVAS_TOOL_INSTRUCTION =
"When the user asks for an HTML, CSS, or JavaScript canvas, call render_html once with one complete self-contained HTML document in the code argument. Embed CSS and JavaScript inside the document. After render_html succeeds, do not call it again in the same response unless the user asks for changes. Future user requests for new canvases may call render_html once.";
export const CANVAS_FALLBACK_INSTRUCTION =
"When the user asks for an HTML, CSS, or JavaScript canvas, return one complete self-contained fenced html code block. Embed CSS and JavaScript inside the document. Do not emit tool-call syntax.";
-/**
- * The OpenAI-form history fields a completion would send. Mirrors the prune + system-prompt half
- * of createOpenAIStreamAdapter; the tool catalog is priced server-side instead, since
- * --enable-tools can inject schemas the client cannot see.
- */
+/** The OpenAI-form history a completion would send. The tool catalog is priced server-side,
+ * since --enable-tools can inject schemas the client cannot see. */
export async function buildLocalTokenCountHistory(
messages: RunMessages,
threadId: string | undefined,
@@ -1856,8 +1752,7 @@ export async function buildLocalTokenCountHistory(
});
}
- // Canvas appends one of these on every request, schema or not, so a count without it reads low.
- // The adapter's image gate is never why render_html is off here: the count route refuses images.
+ // Canvas appends one of these on every request, so a count without it reads low.
const canvasInstruction = artifactsEnabled
? supportsTools
? CANVAS_TOOL_INSTRUCTION
@@ -1883,11 +1778,8 @@ export async function buildLocalTokenCountHistory(
};
}
-/**
- * The reasoning fields a completion would send. llama-server falls back to the load-time
- * `--chat-template-kwargs` only for keys a request omits, so a count sending none renders the
- * template in whatever mode the model was LOADED in and misreports any prefilled thinking block.
- */
+/** The reasoning fields a completion would send: llama-server falls back to load-time
+ * --chat-template-kwargs only for omitted keys, so sending none misreports the mode. */
export function buildLocalTokenCountReasoning(): Record<string, unknown> {
const {
supportsReasoning,
@@ -1898,14 +1790,12 @@ export function buildLocalTokenCountReasoning(): Record<string, unknown> {
supportsPreserveThinking,
preserveThinking,
} = useChatRuntimeStore.getState();
- // Same clamp the request build applies: a level the loaded template does not offer would be
- // dropped by the backend and counted against the template default.
+ // Same clamp the request build applies: a level the loaded template does not offer is dropped by the backend.
const localReasoningEffort = clampReasoningEffortToLevels(
reasoningEffort,
reasoningEffortLevels,
);
return {
- // enable_thinking, not the Anthropic-style `thinking` block: the backend normalizes it anyway.
...(supportsReasoning
? reasoningStyle === "enable_thinking_effort"
? reasoningEnabled
@@ -1923,10 +1813,7 @@ export function buildLocalTokenCountReasoning(): Record<string, unknown> {
};
}
-/**
- * The tool flags a completion would send, so the count includes the schemas and the action nudge.
- * Same gates the adapter applies; the render_html image gate is left to the server.
- */
+/** The tool flags a completion would send, so the count includes the schemas and the action nudge. */
export async function buildLocalTokenCountExtras(
threadId: string | undefined,
): Promise<Record<string, unknown>> {
@@ -1970,13 +1857,8 @@ export async function buildLocalTokenCountExtras(
!ragOn &&
!deepResearchEnabled
) {
- // Explicit false, not an omitted field: the server defaults tools on for a
- // request that never mentions them, so every pill being off has to say so.
- // The permission level rides along because `--enable-tools` still outranks
- // that false in _effective_enable_tools, so a CLI policy can inject
- // python/terminal into a pill-less request and the count would otherwise
- // price sandboxed schemas against an unsandboxed completion. Inert whenever
- // the false stands and no tool list is built.
+ // Explicit false, not omission: the server defaults tools on. The permission level rides
+ // along because `--enable-tools` still outranks that false in _effective_enable_tools.
return { enable_tools: false, bypass_permissions: bypassPermissions };
}
@@ -2017,8 +1899,8 @@ export async function buildLocalTokenCountExtras(
? { project_id: ragProjectId }
: {}),
}),
- // Carried as the completion carries them: an unpersisted New Chat has neither id,
- // and {} is falsy in Python, so the count alone would drop the tool and its nudge.
+ // An unpersisted New Chat has neither id, and {} is falsy in Python, so the count alone would
+ // drop the tool and its nudge.
default_top_k: ragTopK,
mode: ragMode,
// Retrieval turned off means the loop renders exactly these messages, so the
@@ -2055,8 +1937,7 @@ async function resolveUseAdapter(
if (!thread?.pairId) {
return undefined;
}
- // model1/model2 threads skip the adapter toggle — each side loads
- // its own model via /api/inference/load before generation.
+ // compare threads skip the adapter toggle: each side loads its own model via /api/inference/load.
if (thread.modelType === "model1" || thread.modelType === "model2") {
return undefined;
}
@@ -2109,20 +1990,16 @@ async function resolveChatInstructions(
.join("\n\n");
}
-// A run resolves the project separately for the sandbox, the RAG scope and the
-// instructions, and while a fresh thread's row is still being written they all
-// take the composer fallback. Answered once per thread and reused, or a
-// navigation between those calls mixes two projects into one request.
+// Answered once per thread and reused: sandbox, RAG scope and instructions each resolve the
+// project, and navigating between them would mix two projects into one request.
const composerProjectByPendingThread = new Map<string, string | null>();
-/** The project the run started in, kept for the whole run. Only the first send
- * for a thread records one: a later run finds the row and never consults it. */
+/** The project the run started in, kept for the whole run; only a thread's first send records one. */
function rememberComposerProjectForRun(
threadId: string,
projectId: string | null,
): void {
- // An incognito thread never has a row, so resolveProjectId answers before it
- // reads the map: an entry here would be one nothing ever removes.
+ // An incognito thread never has a row, so resolveProjectId answers before it reads the map.
if (isThreadIncognito(threadId)) return;
if (!composerProjectByPendingThread.has(threadId)) {
composerProjectByPendingThread.set(threadId, projectId);
@@ -2132,14 +2009,11 @@ function rememberComposerProjectForRun(
export async function resolveProjectId(
threadId: string | undefined,
readThreadRecord?: ThreadRecordReader,
- // A caller that gates on the answer (the queue's indexing probe) has to tell
- // "no project" from "could not read the row": one sends, the other waits. It
- // also names its own fallback, since the store names whichever project is on
- // screen when it polls rather than the one it is waiting for.
+ // A caller gating on the answer must tell "no project" from "could not read the row": one sends, the other waits.
opts?: { rethrowReadFailure?: boolean; composerProjectId?: string | null },
): Promise<string | null> {
- // Read before the await: a send survives navigation, so a store read after the
- // lookup could hand this request the project the user moved to.
+ // Read before the await: a send survives navigation, so a later store read could hand this
+ // request the project the user moved to.
const composerProjectId =
opts?.composerProjectId !== undefined
? opts.composerProjectId
@@ -2149,8 +2023,7 @@ export async function resolveProjectId(
try {
thread = await (readThreadRecord?.() ?? getStoredChatThread(threadId));
} catch (error) {
- // A failed lookup is not proof the row is missing, so it must not adopt
- // whichever project the composer last had open.
+ // A failed lookup is not proof the row is missing, so do not adopt the composer's project.
if (opts?.rethrowReadFailure) throw error;
return null;
}
@@ -2158,9 +2031,8 @@ export async function resolveProjectId(
composerProjectByPendingThread.delete(threadId);
return thread.projectId ?? null;
}
- // No row yet: initialize() does not await the write, so a fresh chat's first
- // send can read ahead of it and drop the project's sources, instructions and
- // sandbox. An incognito thread is never persisted, so its miss is the answer.
+ // initialize() does not await the row write, so a fresh chat's first send can read ahead of
+ // it; an incognito thread is never persisted, so its miss is the answer.
if (isThreadIncognito(threadId)) {
return null;
}
@@ -2168,9 +2040,7 @@ export async function resolveProjectId(
if (pending !== undefined) {
return pending;
}
- // Not recorded here: the send records it, and this also runs off a run (the
- // queue probe, the token-count extras), where a poll landing mid-navigation
- // would pin the run to whichever project is on screen.
+ // The send records it, not this: a poll landing mid-navigation would pin the run to whichever project is on screen.
}
return composerProjectId ?? null;
}
@@ -2201,11 +2071,8 @@ function waitForModelReady(abortSignal?: AbortSignal): Promise<void> {
});
}
-/**
- * Auto-load the smallest downloaded model when the user chats without
- * selecting one. Prefers GGUF (smallest cached variant), then smallest
- * cached safetensors model.
- */
+/** Auto-load the smallest downloaded model when the user chats without picking one; GGUF
+ * first, then cached safetensors. */
// Cap cascade so broken cached repos can't spam /api/inference/load.
const MAX_AUTO_LOAD_ATTEMPTS = 3;
const BIG_ENDIAN_GGUF_FILENAME_RE = /(^|[-_])be(?:[._-]|$)/gi;
@@ -2221,8 +2088,7 @@ type AutoLoadCandidate = {
successLabel: string;
};
-// Same case rules as autoLoadSourceKey: folding a POSIX target would let a
-// failed /models/Foo mark a distinct /models/foo as already tried.
+// Same case rules as autoLoadSourceKey: folding a POSIX target would conflate /models/Foo and /models/foo.
function autoLoadCandidateKey(
kind: LastLocalModelKind,
id: string,
@@ -2293,9 +2159,11 @@ type QueuedResolvedModelRuntime = {
type ChatRuntimeState = ReturnType<typeof useChatRuntimeStore.getState>;
-// A background auto-load may enrich models[] with the loaded catalog entry,
-// but every field describing the model visible in the current chat must return
-// to its prior value after the queued run captures its private runtime.
+// A background auto-load may enrich models[], but every field describing the visible chat's
+// model must return to its prior value.
+// Read them from the same per-model config that fed effectiveMaxSeqLength: on a background
+// auto-load the live store holds session defaults, not the saved Manual mode, layer pin or GPU
+// pick. The saved GPU pick is reconciled against the GPUs present now.
const VISIBLE_MODEL_RUNTIME_KEYS = [
"activeLoadId",
"activeGgufVariant",
@@ -2359,8 +2227,8 @@ const VISIBLE_MODEL_RUNTIME_KEYS = [
"chatTemplateOverride",
"loadedChatTemplateOverride",
"chatTemplateOverrideReason",
- // The rest of the group mlxRuntimeStateFrom writes, or a background autoload
- // leaves its width and verdict on the restored model.
+ // Or a background autoload leaves its width and verdict on the restored model.
+ // The rest of the group mlxRuntimeStateFrom writes.
"mlxKvBits",
"loadedMlxKvBitsRequested",
"mlxKvQuantReason",
@@ -2408,8 +2276,7 @@ function restoreVisibleModelState(snapshot: VisibleModelStateSnapshot): void {
...snapshot.runtime,
...snapshot.settings,
params: { ...snapshot.settings.params },
- // A visible provider run can finish while the background model loads.
- // Preserve those newer per-thread totals instead of restoring stale usage.
+ // A visible provider run can finish while the background model loads; keep the newer per-thread totals.
contextUsage: liveUsage.contextUsage,
contextUsageByThreadId: liveUsage.contextUsageByThreadId,
});
@@ -2438,8 +2305,8 @@ function queuedResolvedModelFromStore(
? {
isVision: activeModel.isVision,
isGguf: activeModel.isGguf,
- // The queued run mints its own summary for a model with no catalog row, and
- // the sampling seed is gated on this.
+ // The queued run mints its own summary for a model with no catalog row, and the sampling seed
+ // is gated on this.
isMlx: activeModel.isMlx,
isAudio: activeModel.isAudio,
audioType: activeModel.audioType,
@@ -2486,19 +2353,16 @@ function isChattableCachedRepo(repo: {
return (
repo.partial !== true &&
repo.capabilities?.can_chat !== false &&
- // Same rule isAutoLoadableLocalRow applies locally: an adapter has no base
- // weights, so /load fetches the base from the Hub when it is not cached,
- // and can_chat is reported true for the format on file layout alone.
+ // An adapter has no base weights, so /load fetches the base from the Hub and can_chat is
+ // reported true on file layout alone.
repo.model_format !== "adapter" &&
// Cached diffusion and speech repos report can_chat true on file format alone.
!NON_CHAT_TASKS.has(repo.task ?? "")
);
}
-// Chat models are tagged "text-generation" or left null, so this is a list rather than
-// a "has a task" test. The picker routes these rows to their own page on click; a
-// background load has no routing step. For the audio tasks the backend answers a chat
-// completion with synthesized speech rather than refusing it.
+// Chat models are tagged "text-generation" or left null, so this is a list rather than a "has
+// a task" test; the audio tasks answer a chat completion with speech.
const NON_CHAT_TASKS: ReadonlySet<string> = new Set([
"text-to-image",
"text-to-video",
@@ -2509,42 +2373,29 @@ const NON_CHAT_TASKS: ReadonlySet<string> = new Set([
"automatic-speech-recognition",
]);
-// Scan folders the picker exposes. hf_cache is already in the cached lists.
-// ollama stays out by policy: its rows load through the picker, but the API
-// auto-switch index never resolves them (local_model_resolver.py skips the
-// Ollama scanner), so auto-loading one would promise an API identity that
-// cannot be reached.
+// ollama stays out by policy: local_model_resolver.py skips its scanner, so auto-loading one
+// promises an API identity that cannot be reached.
const AUTO_LOAD_LOCAL_SOURCES: ReadonlySet<string> = new Set([
"models_dir",
"lmstudio",
"custom",
]);
-/**
- * Picker policy for a background pick. Adapters and checkpoints are excluded:
- * an adapter resolves its base model (a Hub fetch when the base is uncached),
- * and a scan-folder checkpoint is a pickle with no Hub security scan.
- */
+/** Picker policy for a background pick; adapters (base fetched from the Hub) and scan-folder
+ * checkpoints (pickle, no Hub scan) are excluded. */
function isAutoLoadableLocalRow(
row: LocalModelInfo,
- // Set when a cached lookup failed: the excluded hf_cache rows are then the
- // only evidence of that cache, and dropping them left a device whose one
- // model lives there with nothing to load.
+ // Set when a cached lookup failed: the excluded hf_cache rows are then the only evidence of that cache.
admitHfCache = false,
): boolean {
return (
(AUTO_LOAD_LOCAL_SOURCES.has(row.source) ||
(admitHfCache && row.source === "hf_cache")) &&
- // Absent capabilities means unclassified, not "not chat": requiring true
- // skipped the row and fell through to downloading the default.
+ // Absent capabilities means unclassified, not "not chat": requiring true fell through to downloading the default.
row.capabilities?.can_chat !== false &&
row.partial !== true &&
- // An allowlist, because an exclusion is only as good as the classification:
- // the backend sends "unknown" when it cannot tell a format apart and an
- // older one omits the field, so both slipped past a `!== "checkpoint"` test
- // and made a pickle eligible. A checkpoint directory looks like a
- // safetensors one, so this has to fail closed; GGUF is still recognised by
- // suffix, so an unclassified .gguf row keeps loading.
+ // An allowlist, since the backend sends "unknown" when it cannot classify and older ones omit
+ // the field, so a `!== "checkpoint"` test made a pickle eligible.
(isGgufLocalRow(row) || row.model_format === "safetensors") &&
!NON_CHAT_TASKS.has(row.task ?? "") &&
runsOnThisPlatform(row) &&
@@ -2552,22 +2403,18 @@ function isAutoLoadableLocalRow(
);
}
-// model_format is optional, so an older backend omits it on a direct .gguf row.
-// Reading only the field made such a row a Transformers source, which sends the
-// safetensors context length to /load and remembers the wrong kind.
+// model_format is optional on an older backend, so reading only the field made a direct .gguf
+// row a Transformers source.
function isGgufLocalRow(row: LocalModelInfo): boolean {
return (
row.model_format === "gguf" || row.path.toLowerCase().endsWith(".gguf")
);
}
-/** Chat-only installs run GGUF anywhere and MLX on a Mac; the picker hides
- * every other local format there. */
+/** Chat-only installs run GGUF anywhere and MLX on a Mac; the picker hides every other local format there. */
function runsOnThisPlatform(row: LocalModelInfo): boolean {
const platform = usePlatformStore.getState();
- // Until the backend reports it, chatOnly is a BROWSER guess: a Mac browser on
- // a remote Linux Unsloth would hide every local safetensors model and fetch
- // the default. Failing open is safe: validation refuses an ineligible pick.
+ // chatOnly is a BROWSER guess until the backend reports it; fail open, since validation refuses an ineligible pick.
if (!platform.fetched || !platform.isChatOnly()) return true;
if (isGgufLocalRow(row)) {
return true;
@@ -2580,15 +2427,13 @@ function runsOnThisPlatform(row: LocalModelInfo): boolean {
);
}
-/** The picker hides cached non-GGUF rows outright on a chat-only install
- * (visibleCachedModelRows). Same wait-for-the-server rule as above. */
+/** The picker hides cached non-GGUF rows outright on a chat-only install; same wait-for-the-server rule as above. */
function cachedModelsRunOnThisPlatform(): boolean {
const platform = usePlatformStore.getState();
return !platform.fetched || !platform.isChatOnly();
}
-/** One loadable thing on this device, from any inventory. `listVariants` is
- * lazy, so only the repos the cascade reaches are scanned. */
+/** One loadable thing on this device; `listVariants` is lazy, so only the repos the cascade reaches are scanned. */
type AutoLoadSource = {
kind: LastLocalModelKind;
/** Catalog id: per-model settings, toasts, remembered-model matching. */
@@ -2601,10 +2446,10 @@ type AutoLoadSource = {
listVariants: (() => Promise<GgufVariantDetail[]>) | null;
};
-// Case-sensitive targets keep their case (Linux distinguishes /models/Foo from
-// /models/foo; \\wsl$\ reaches the same ext4). Repo ids and Windows paths fold,
-// separators included, so C:\a\m.gguf and C:/a/m.gguf are one key. NFC first:
-// macOS returns decomposed names for the same file.
+// Case-sensitive targets keep their case; repo ids and Windows paths fold, separators
+// included. NFC first: macOS returns decomposed names.
+// Linux distinguishes /models/Foo from models/foo, and \\wsl$\ reaches the same ext4; separators
+// fold, so C:\a\m.gguf and C:/a/m.gguf are one key. NFC first: macOS returns decomposed names.
function normalizeTarget(value: string): string {
const target = value.trim().normalize("NFC");
if (/^[A-Za-z]:[\\/]/.test(target) || target.startsWith("\\\\")) {
@@ -2614,9 +2459,8 @@ function normalizeTarget(value: string): string {
return /^[/~]/.test(target) ? target : target.toLowerCase();
}
-// The load target alone, not the kind: a repo holding both GGUF and safetensors
-// yields a row in each list, but the backend resolves one target to one model,
-// so keeping both would spend a second attempt on the same files.
+// The load target alone, not the kind: one target is one model, so a repo in both lists must
+// not spend two attempts.
function autoLoadSourceKey(source: AutoLoadSource): string {
return normalizeTarget(source.loadId);
}
@@ -2683,8 +2527,7 @@ function isRememberedSource(
remembered: { id: string; kind: LastLocalModelKind },
): boolean {
if (source.kind !== remembered.kind) return false;
- // Same case rules as the dedupe key: folding a POSIX path would mark two
- // models differing only by case as both being the remembered one.
+ // Same case rules as the dedupe key.
const target = normalizeTarget(remembered.id);
return (
normalizeTarget(source.id) === target ||
@@ -2704,15 +2547,13 @@ function orderAutoLoadSources(
// Unknown sizes sort last so a sizeless row cannot shadow a real one.
const size = (source: AutoLoadSource): number =>
source.sizeBytes > 0 ? source.sizeBytes : Number.MAX_SAFE_INTEGER;
- // Same-target twins are kept, just ordered behind the preferred row: dropping
- // them here lost a loadable safetensors row whenever its GGUF twin resolved
- // no quant, so the cascade skips a twin at run time instead.
+ // Same-target twins sort behind the preferred row rather than being dropped: dropping lost a
+ // loadable safetensors row whenever its GGUF twin resolved no quant.
return [...sources].sort((a, b) => rank(a) - rank(b) || size(a) - size(b));
}
-/** The candidate to attempt, resolving a GGUF quant when one is needed:
- * remembered quant first, then smallest, skipping quants already tried so one
- * bad file cannot sink a whole repo. null when nothing here is loadable. */
+/** The candidate to attempt: remembered quant first, then smallest, skipping quants already
+ * tried. null when nothing here is loadable. */
async function resolveAutoLoadCandidate(
source: AutoLoadSource,
rememberedVariant: string | null,
@@ -2767,9 +2608,8 @@ function formatDownloadBytes(bytes: number): string {
: `${Math.max(1, Math.round(bytes / 1024 ** 2))} MB`;
}
-/** Fetch the default through the Hub download manager rather than letting
- * /api/inference/load pull it inline: the manager gives it a panel entry, live
- * progress, and a working Cancel. "ready" means the bytes are on disk. */
+/** Fetch the default through the Hub download manager rather than inline in /load: that gives
+ * a panel entry, live progress and a working Cancel. */
async function ensureDefaultModelDownloaded(
hfToken: string | null,
abortSignal: AbortSignal | undefined,
@@ -2784,7 +2624,6 @@ async function ensureDefaultModelDownloaded(
const variant = listing.variants.find(
(entry) => entry.quant?.toLowerCase() === variantKey,
);
- // Pulled by an earlier run: nothing to download.
if (variant?.downloaded && variant.partial !== true) return "ready";
expectedBytes = variant?.download_size_bytes || variant?.size_bytes || 0;
} catch {
@@ -2792,10 +2631,8 @@ async function ensureDefaultModelDownloaded(
}
abortSignal?.throwIfAborted();
- // startJob sends the stored token raw, with none of the recovery validateModel
- // and loadModel get, so an expired one would fail the download of a public
- // repo the lookup above just read anonymously. After the on-disk check, so
- // nothing prompts when there is nothing to download.
+ // startJob sends the stored token raw, so an expired one would fail a public repo's download;
+ // placed after the on-disk check so nothing prompts needlessly.
const prepared = await prepareHfTokenForUse(hfToken);
abortSignal?.throwIfAborted();
if (!prepared.proceed) return "cancelled";
@@ -2807,15 +2644,12 @@ async function ensureDefaultModelDownloaded(
expectedBytes,
};
const jobKey = jobKeyOf(request.kind, request.repoId, request.variant);
- // requestStart runs transport preflights before the job exists, and cancel()
- // no-ops on a missing key, so remember the click and replay it once the job
- // appears or the first Cancel is silently swallowed.
+ // requestStart runs preflights before the job exists and cancel() no-ops on a missing key, so
+ // remember the click and replay it once the job appears.
let cancelRequested = false;
let cancelInFlight = false;
let cancelEverIssued = false;
- // Cancelling patches the job and wakes the subscription below, so this must
- // not re-enter while a request is outstanding. The latch clears when the
- // request settles, since a failed cancel leaves the job running for a retry.
+ // The latch clears when the request settles, since a failed cancel leaves the job running for a retry.
const issueCancel = (): boolean => {
const active = selectActiveJob(
useDownloadManagerStore.getState(),
@@ -2863,8 +2697,7 @@ async function ensureDefaultModelDownloaded(
// Before the start request, so a fast completion cannot be missed.
cleanups.push(
subscribeJobListeners(request.kind, request.repoId, {
- // A cancel can fail and the transfer finish anyway; the user asked for
- // it to stop, so the bytes are not handed to a load.
+ // A cancel can fail and the transfer finish anyway; the bytes are still not handed to a load.
onComplete: (variant) =>
isOurVariant(variant) &&
finish(cancelRequested ? "cancelled" : "ready"),
@@ -2872,14 +2705,11 @@ async function ensureDefaultModelDownloaded(
onError: (variant) => isOurVariant(variant) && finish("failed"),
}),
);
- // Live progress in the title; the panel shows the same job with its bar.
cleanups.push(
useDownloadManagerStore.subscribe((state) => {
const job = state.jobs[jobKey];
if (!job) return;
- // Cancelled during the preflights: stop it as soon as it exists. The
- // deferred first attempt only; a retry comes from another click, not
- // from every store tick.
+ // Cancelled during the preflights: stop it as soon as it exists, once, not on every store tick.
if (cancelRequested) {
if (!cancelEverIssued) issueCancel();
return;
@@ -2895,8 +2725,7 @@ async function ensureDefaultModelDownloaded(
);
}),
);
- // Stopping the send stops the wait, not the transfer: it keeps running and
- // stays cancellable in the panel.
+ // Stopping the send stops the wait, not the transfer.
if (abortSignal) {
const onAbort = () => finish("cancelled");
abortSignal.addEventListener("abort", onAbort, { once: true });
@@ -2914,8 +2743,7 @@ async function ensureDefaultModelDownloaded(
finish("cancelled");
return;
}
- // "conflict" and "busy" need the Hub download card; "error" already
- // toasted.
+ // "conflict" and "busy" need the Hub download card; "error" already toasted.
finish("failed");
},
() => finish("failed"),
@@ -3085,16 +2913,14 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
: undefined,
});
};
- // Like the interactive load toast: an auto-load that lost GPU acceleration
- // must not read as a plain success.
+ // An auto-load that lost GPU acceleration must not read as a plain success.
const showAutoLoadSuccess = (
message: string,
cpuFallbackReason?: CpuFallbackReason | null,
mmprojFallbackReason?: MmprojFallbackReason | null,
): void => {
- // Both reasons, composed. Nesting them as `mmproj ? ... : cpu ? ...` dropped the
- // CPU message whenever both were set, which is reachable and is the case this
- // feature exists for -- see loadFallbackNotice.
+ // Both reasons composed: nesting them as `mmproj ? ... : cpu ? ...` dropped the CPU message.
+ // That combination is reachable and is the case this feature exists for; see loadFallbackNotice.
const notice = loadFallbackNotice(
message,
cpuFallbackReason,
@@ -3130,8 +2956,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
error instanceof Error && error.message.trim()
? error.message.trim()
: "";
- // loadModel also rejects before /api/inference/load is sent (dismissed token dialog, dead
- // backend): those stop the Hub download, but must not blame the model.
+ // loadModel can reject before /api/inference/load is sent (dismissed token dialog, dead
+ // backend); those must not blame the model.
const marker = error as {
unslothTransportFailure?: boolean;
unslothUserCancelled?: boolean;
@@ -3154,21 +2980,17 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
max_seq_length: number;
is_lora: boolean;
gguf_variant?: string | null;
- // GGUF-only: scopes the training guard to the same placement policy /load
- // will use. Manual mode must match because it makes placement user-owned.
- // The layer/MoE/split knobs are deliberately not sent: Auto mode's guard
- // sizes conservatively, while Manual mode bypasses that estimate.
- // The safetensors fallback omits both fields and uses HF auto-placement.
+ // GGUF-only: the guard must be told the placement policy /load will use, since Auto sizes
+ // conservatively and Manual bypasses the estimate. Layer/MoE/split are not sent.
gpu_ids?: number[];
gpu_memory_mode?: "auto" | "manual";
cache_type_kv?: string | null;
tensor_parallel?: boolean | null;
- // The projector is part of what the guard has to size: a load that skips
- // it needs ~1 GB less, and charging for it would refuse loads that fit.
+ // The projector is part of what the guard sizes: charging for a skipped one refuses loads that fit.
+ // A load that skips the projector needs ~1 GB less.
disable_vision?: boolean | null;
- // The estimate charges a drafter whose size differs by mode (a DSpark
- // sidecar is ~11 GB, and Auto reaches it), so this preflight has to be told
- // what the load will send or it sizes a different model.
+ // The estimate charges a drafter whose size differs by mode, so the preflight must be told what the load will send.
+ // A DSpark sidecar is ~11 GB, and Auto reaches it.
speculative_type?: string | null;
spec_draft_n_max?: number | null;
}): Promise<boolean> {
@@ -3180,8 +3002,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
trust_remote_code: trustRemoteCode,
});
options?.abortSignal?.throwIfAborted();
- // Background auto-load never runs a repo's custom code or loads Hub-flagged unsafe
- // files on its own; both are deferred to the explicit consent dialog instead.
+ // A background auto-load never runs custom code or Hub-flagged unsafe files; both need the
+ // explicit consent dialog.
if (
validation.requires_trust_remote_code ||
validation.requires_security_review
@@ -3198,13 +3020,13 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
}
function recordCandidateFailure(label: string, error: unknown): void {
- // Every rejection is recorded: the sweep's catches are bare, so one left unrecorded reads as
- // "nothing was cached" and fetches the default. Only cancelling ends the sweep.
+ // Every rejection is recorded: the sweep's catches are bare, so an unrecorded one reads as
+ // "nothing was cached" and fetches the default.
const marker = error as { unslothUserCancelled?: boolean };
noteLoadFailure(label, error);
if (marker?.unslothUserCancelled === true) {
- // The next candidate would reopen the same dialog, so stop rather than ask per repo. A
- // transport failure deliberately does NOT: the backend can come back mid-sweep.
+ // Stop rather than reopen the same dialog per repo; a transport failure does not, since the
+ // backend can come back mid-sweep.
autoLoadCancelled = true;
}
}
@@ -3216,8 +3038,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
try {
return await canAutoLoad(payload);
} catch (error) {
- // validateModel prepares the token too, so a dismissed dialog or dead backend surfaces here,
- // where the sweep's bare catches would drop it and hit the Hub download.
+ // validateModel prepares the token too, so a dismissed dialog or dead backend surfaces here
+ // rather than in the sweep's bare catches.
recordCandidateFailure(label, error);
throw error;
}
@@ -3256,20 +3078,14 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
defaultMaxSeqLength: candidate.maxSeqLength,
presetSource: currentStore.activePresetSource,
});
- // The GPU knobs are per-model, so read them from the same per-model config
- // that fed effectiveMaxSeqLength -- on a background auto-load the live store
- // holds session defaults, not the saved Manual mode / layer pin / GPU pick.
- // Absent fields fall back like the interactive restore: the mode to the store
- // (a persisted standing preference), the per-model knobs to their defaults.
- // The saved GPU pick is reconciled against the GPUs present now.
+ // The GPU knobs are per-model: on a background auto-load the live store holds session
+ // defaults, not the saved Manual mode / layer pin / GPU pick.
const effectiveGpuMemoryMode =
config.gpuMemoryMode ?? currentStore.gpuMemoryMode;
const effectiveGpuLayers = config.gpuLayers ?? GPU_LAYERS_AUTO;
const effectiveNCpuMoe = config.nCpuMoe ?? 0;
if (config.selectedGpuIds != null) {
- // Warm the device cache first: on a cold cache the reconcile passes the
- // saved pick through unvalidated, and a stale cross-host pick then fails
- // the load with the picker hidden.
+ // Warm the device cache first: on a cold cache the reconcile passes a stale cross-host pick through unvalidated.
await ensureGpuDeviceCache();
}
let isDiffusion = false;
@@ -3277,15 +3093,11 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
candidate.kind === "gguf" &&
(config.selectedGpuIds != null || config.tensorParallel === true)
) {
- // Prepare the token before this probe, exactly as validateModel/loadModel
- // (and the interactive performLoad path) do: the Hub 401s an invalid
- // Authorization header even for a public repo, so sending the raw stored
- // token here would abort this candidate before the existing anonymous/
- // replacement-token recovery flow could run.
+ // Prepare the token before this probe: the Hub 401s an invalid Authorization header even for
+ // a public repo, aborting the candidate before recovery can run.
const preparedToken = await prepareHfTokenForUse(hfToken);
if (!preparedToken.proceed) {
- // Raised before loadModel, so route it through the same helper or every later candidate
- // reopens the dialog.
+ // Raised before loadModel, so route it through the same helper or every later candidate reopens the dialog.
const cancelled = Object.assign(new Error("Model load cancelled."), {
unslothUserCancelled: true,
});
@@ -3304,14 +3116,9 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
})
).isDiffusion;
}
- // The stored override can live only on the server (written through the API, or
- // from another browser), and this config comes from local storage. Nothing is
- // resident at startup, so /load's omission path has nothing to inherit from, and
- // the model would come up without the arguments that were saved for it.
- //
- // Sanitized like every other hydration: this becomes an EXPLICIT list, which
- // /load validates strictly rather than putting through the carry-over paths that
- // drop a newly denied flag quietly.
+ // The stored override can live only on the server while this config is local, and nothing is
+ // resident at startup for /load's omission path to inherit from. Sanitized like every
+ // hydration, so it becomes an EXPLICIT list /load validates strictly.
let resolvedExtraArgs = config.llamaExtraArgs;
if (candidate.kind === "gguf" && !isDiffusion) {
try {
@@ -3324,9 +3131,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
if (resolvedExtraArgs === undefined) {
const stored = await fetchLoadExtraArgs(
modelPath,
- // The advertised repository id as well as the path this load resolves
- // to: cached inventory can hand back a different loadId, and the row
- // was written under whichever of the two the user was looking at.
+ // Both the advertised repo id and the path this load resolves to: cached inventory can hand
+ // back a different loadId.
candidate.id,
candidate.ggufVariant ?? null,
);
@@ -3336,24 +3142,20 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
resolvedExtraArgs = cleaned;
}
} else if (stored.explicit) {
- // A row carrying an EMPTY list is a cleared box, not an absent one, and
- // the difference decides what /load does: omitting the field lets it
- // carry the resident model's arguments over, which is what was cleared.
+ // An EMPTY list is a cleared box, not an absent one: omitting the field lets /load carry over
+ // the arguments that were cleared.
resolvedExtraArgs = [];
}
} else if (resolvedExtraArgs !== null && resolvedExtraArgs.length > 0) {
- // The local copy gets the same treatment as the fetched one. It was
- // written by whatever build was running then, so a flag added to the
- // managed set since would be sent explicitly and answered with a 400,
- // and the remembered model would not come up at all.
+ // Same for the local copy: a flag added to the managed set since it was written would be sent
+ // explicitly and answered with a 400.
const cleaned = clean(resolvedExtraArgs);
if (cleaned.length !== resolvedExtraArgs.length) {
resolvedExtraArgs = cleaned.length > 0 ? cleaned : [];
}
}
} catch {
- // An overrides outage must not stop a startup load: without this the model
- // comes up as it did before the feature existed.
+ // An overrides outage must not stop a startup load.
}
}
const effectiveTensorParallel = isDiffusion ? false : config.tensorParallel;
@@ -3367,10 +3169,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
isDiffusion,
)
: null;
- // Under Manual GPU memory + Auto layers, llama.cpp's --fit owns context
- // sizing, so send 0 (or the pinned length). GGUF-only; a no-op otherwise.
- // The context pin is per-model too, so it comes from the saved config, not
- // the live store.
+ // Under Manual GPU memory with Auto layers llama.cpp's --fit owns context sizing, so send 0
+ // or the per-model pin (GGUF-only).
const fitMaxSeqLength = resolveFitMaxSeqLength(
candidate.kind === "gguf",
effectiveGpuMemoryMode,
@@ -3394,27 +3194,22 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
cache_type_kv: config.kvCacheDtype,
tensor_parallel: effectiveTensorParallel,
disable_vision: effectiveDisableVision,
- // The same values the load below sends.
speculative_type: effectiveSpeculativeType,
spec_draft_n_max: effectiveSpecDraftNMax,
- // The same remembered-derived GPU pick the load below sends.
...(candidate.kind === "gguf"
? {
gpu_ids: effectiveGpuIds ?? undefined,
gpu_memory_mode: effectiveGpuMemoryMode,
- // Sized like the load below: a remembered manual DiffusionGemma
- // split (0 especially) must not be refused as a full-GGUF occupant.
+ // A remembered manual DiffusionGemma split (0 especially) must not be refused as a full-GGUF occupant.
gpu_layers: effectiveGpuLayers,
n_parallel: config.nParallel ?? null,
// omitted when blank: a null counts as set and strips inherited -b / -ub
...(config.nBatch != null ? { n_batch: config.nBatch } : {}),
...(config.nUbatch != null ? { n_ubatch: config.nUbatch } : {}),
- // Same omit-when-blank rule, and the same values the load sends:
- // the draft cache dtype changes what the estimate charges the
- // drafter, so a preflight without it disagrees with the launch.
+ // The draft cache dtype changes what the estimate charges the drafter, so a preflight without
+ // it disagrees with the launch.
...serverTuningLoadPayload(config),
- // Checked with the same arguments the load below sends, or a list
- // the backend refuses would pass this gate and fail the launch.
+ // Checked with the same arguments the load sends, or a list the backend refuses would pass this gate.
...(resolvedExtraArgs !== undefined
? { llama_extra_args: resolvedExtraArgs ?? [] }
: {}),
@@ -3425,8 +3220,7 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
skippedAutoLoadCandidates.add(
autoLoadCandidateKey(
candidate.kind,
- // A cached repo and a local row can alias the same files, so
- // blocking one must block the other.
+ // A cached repo and a local row can alias the same files, so blocking one must block the other.
candidate.loadId ?? candidate.id,
candidate.ggufVariant,
),
@@ -3459,10 +3253,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
spec_draft_n_max: effectiveSpecDraftNMax,
tensor_parallel: effectiveTensorParallel,
disable_vision: effectiveDisableVision,
- // GGUF-only: the safetensors fallback loads via HF auto-placement (no
- // explicit pins). The split ratio is deliberately never remembered
- // (positionally bound to an exact GPU set), so auto-load leaves llama.cpp's
- // free-VRAM default in charge rather than sending a stale store value.
+ // GGUF-only; the split ratio is never remembered (it is bound to an exact GPU set), so
+ // llama.cpp's free-VRAM default stays in charge.
...(candidate.kind === "gguf"
? {
gpu_memory_mode: effectiveGpuMemoryMode,
@@ -3473,14 +3265,10 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
n_parallel: config.nParallel ?? null,
...(config.nBatch != null ? { n_batch: config.nBatch } : {}),
...(config.nUbatch != null ? { n_ubatch: config.nUbatch } : {}),
- // Remembered like the rest of this block, or the auto-load reverts
- // an override the user asked to be remembered.
+ // Remembered like the rest of this block, or the auto-load reverts the override.
...serverTuningLoadPayload(config),
- // Remembered pass-through arguments, for the same reason as the rest of
- // this block: nothing is resident at startup, so the omission path has
- // nothing to inherit them from and the model would come up without the
- // flags the user asked to be remembered. Undefined means this config
- // predates the field; a cleared list is an explicit none.
+ // Remembered pass-through args: nothing is resident at startup to inherit them from.
+ // Undefined predates the field; a cleared list is an explicit none.
...(resolvedExtraArgs !== undefined
? { llama_extra_args: resolvedExtraArgs ?? [] }
: {}),
@@ -3491,22 +3279,19 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
noteLoadFailure(failureLabel, error);
throw error;
});
- // Do not apply this load to the visible runtime after its queue was
- // cancelled. We still await /load so the model lifecycle remains
- // serialized before a foreground selection can begin.
+ // Do not apply this load to the visible runtime once its queue was cancelled; still await
+ // /load so the lifecycle stays serialized.
options?.abortSignal?.throwIfAborted();
applyAutoLoadRuntimeState(options, () => {
- // Only persist the global preference when the value came from the global
- // settings. A per-model config's choice must stay load-local, or autoloading
- // a remembered model on startup would rewrite the global default.
+ // Persist the global preference only when the value came from global settings, or autoloading
+ // a remembered model rewrites the default.
if (config.speculativeType == null) {
saveSpeculativeType(effectiveSpeculativeType);
}
// Self-gates on is_gguf (skips diffusion), so persists only for a real GGUF load.
persistGpuMemoryModeOnLoad(loadResp, effectiveGpuMemoryMode);
const loadedModelId = loadResp.model || modelPath;
- // The identity stays the backend's, per the inactive-cache contract; the
- // pin is recorded alongside it so a later reload finds the same directory.
+ // The identity stays the backend's; the pin is recorded alongside it so a later reload finds the same directory.
useChatRuntimeStore.setState({
activeLoadId: modelPath === candidate.id ? null : modelPath,
});
@@ -3551,20 +3336,17 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
),
},
);
- // Upsert: a pre-load catalog entry has no backend-derived audio
- // capability, and the load response is the authority on it.
+ // Upsert: the load response is the authority on audio capability, which a pre-load catalog entry lacks.
syncModelCapabilities(loadedModelId, {
...loadResp,
display_name: loadResp.display_name ?? candidate.id,
is_gguf: loadResp.is_gguf ?? candidate.kind === "gguf",
});
if (candidate.kind === "gguf") {
- // The Context Length saved for this model, not fitMaxSeqLength: the wire
- // value is Auto-resolved for a same-model reload, so pinning it would
- // convert Auto into a number the user never set (see resolveExplicitCtxPin).
+ // The saved Context Length, not fitMaxSeqLength: the wire value is Auto-resolved on a
+ // same-model reload, so pinning it turns Auto into a number the user never set.
const keepCustomCtx = resolveExplicitCtxPin(config.customContextLength);
- // Slots this auto-load committed. Diffusion ignores --parallel, so a count
- // there would mint a phantom override a saved preset carries onto a GGUF.
+ // Diffusion ignores --parallel, so counting slots there would mint a phantom override.
const committedSlots =
(loadResp.is_diffusion ?? false) ? null : (config.nParallel ?? null);
// same rule for the batch sizes
@@ -3594,11 +3376,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
nUbatch: committedNUbatch,
loadedNUbatch: committedNUbatch,
...committedServerTuningState(config, loadResp.is_diffusion ?? false),
- // What this launch is running, for a later rollback. The status applier
- // cannot seed it: the model-loading lease is held for the whole of this
- // load, which is exactly the guard that stops a mid-switch poll writing
- // here. Without it an immediate switch snapshots the previous model's
- // list, and a failed switch restores this one with the wrong arguments.
+ // What this launch is running, for a later rollback: the status applier cannot seed it while
+ // the model-loading lease is held, and a failed switch would restore the wrong args.
loadedLlamaExtraArgs:
loadResp.requested_llama_extra_args !== undefined
? (loadResp.requested_llama_extra_args ?? [])
@@ -3606,8 +3385,7 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
tensorParallel: loadResp.tensor_parallel ?? false,
loadedTensorParallel: loadResp.tensor_parallel ?? false,
loadedDisableVision: loadResp.disable_vision ?? false,
- // Repaired from the echo alongside tensorParallel: this load picked its
- // own model and config, so a stale true would show Vision off over a
+ // Repaired from the echo alongside tensorParallel: a stale true would show Vision off over a
// loaded projector, and the next Apply would send it.
disableVision: loadResp.disable_vision ?? false,
loadedVisionDisabledByUser: loadResp.vision_disabled_by_user ?? false,
@@ -3638,8 +3416,7 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
kvCacheDtype: loadResp.cache_type_kv ?? null,
loadedKvCacheDtype: loadResp.cache_type_kv ?? null,
...mlxRuntimeStateFrom(loadResp),
- // GGUF-only and never sent here: a staged override would be saved for
- // a model that cannot use it.
+ // GGUF-only and never sent here: a staged override would be saved for a model that cannot use it.
nParallel: null,
loadedNParallel: null,
nBatch: null,
@@ -3647,20 +3424,18 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
nUbatch: null,
loadedNUbatch: null,
...clearedServerTuningState(),
- // Same reason, and the baseline has to be cleared rather than left: a
- // rollback to THIS model must not resend a GGUF's arguments.
+ // Same reason, and the baseline must clear so a rollback to THIS model does not resend a
+ // GGUF's arguments.
loadedLlamaExtraArgs: null,
tensorParallel: loadResp.tensor_parallel ?? false,
loadedTensorParallel: loadResp.tensor_parallel ?? false,
loadedDisableVision: loadResp.disable_vision ?? false,
- // Cleared, not carried, like the GGUF-only knobs above: a safetensors
- // load has no projector, and a stale true would stage it for the next
- // model that has one.
+ // Cleared, not carried: a safetensors load has no projector, and a stale true would stage it
+ // for the next model that has one.
disableVision: false,
loadedVisionDisabledByUser:
loadResp.vision_disabled_by_user ?? false,
- // Non-GGUF response: clears any stale GPU baseline a prior manual-GPU
- // GGUF load left, matching the interactive/status sibling load paths.
+ // Non-GGUF response: clears any stale GPU baseline a prior manual-GPU GGUF load left.
...loadedGpuMemoryFields(loadResp),
defaultChatTemplate: loadResp.chat_template ?? null,
chatTemplateOverride: effectiveChatTemplateOverride,
@@ -3696,21 +3471,18 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
return true;
}
try {
- // Fail closed: these three lists are the only evidence of what is on the
- // device, and `.catch(() => [])` turned one flaky request into "the user
- // has nothing", downloading a model over a loadable local one. Both cached
- // calls take the run signal because they are raw fetches with no timeout of
- // their own; listLocalModels has its own 30s bound.
+ // Fail closed: `.catch(() => [])` turned one flaky request into "the user has nothing" and
+ // downloaded a model over a loadable local one.
+ // These three lists are the only evidence of what is on the device. Both cached calls take the run
+ // signal, being raw fetches with no timeout of their own; listLocalModels has its own 30s bound.
const inventory = await Promise.allSettled([
listCachedGguf(options?.abortSignal),
listCachedModels(hfToken, options?.abortSignal),
listLocalModels(),
]);
options?.abortSignal?.throwIfAborted();
- // Settled, not all: a rejection is one unknown source, not an empty device.
- // Failing the whole batch threw away the lists that did arrive, so a broken
- // local scan skipped a loadable cached model. Keep what came back and let
- // the gap block only the default download below.
+ // Settled, not all: a rejection is one unknown source, not an empty device, and failing the
+ // batch threw away the lists that did arrive.
const [ggufSettled, modelsSettled, localSettled] = inventory;
const allGgufRepos =
ggufSettled.status === "fulfilled" ? ggufSettled.value : [];
@@ -3719,14 +3491,13 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
const localRows =
localSettled.status === "fulfilled" ? localSettled.value.models : [];
const inventoryIncomplete = inventory.some((r) => r.status === "rejected");
- // Only the two managed-cache lookups: /api/hub/local also reports hf_cache
- // rows, so when either of these failed that response covers the gap.
+ // Only the two managed-cache lookups: /api/hub/local also reports hf_cache rows.
const cachedInventoryFailed =
ggufSettled.status === "rejected" || modelsSettled.status === "rejected";
if (inventoryIncomplete) hadNonTrustFailure = true;
- // Managed cache plus everything the picker indexes on disk. Reading only
- // the cache lists is what made a local model invisible here.
+ // Managed cache plus everything the picker indexes on disk; reading only the cache lists made
+ // a local model invisible.
const sources = orderAutoLoadSources(
buildAutoLoadSources(
allGgufRepos.filter(isChattableCachedRepo),
@@ -3742,10 +3513,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
lastLoaded,
);
- // One load target can appear in more than one list. Once a row for it has
- // produced a candidate its twin is the same files and must not spend a
- // second attempt, so a twin is reached only if the preferred row resolved
- // nothing at all.
+ // One load target can appear in more than one list, so a twin is reached only if the preferred
+ // row resolved nothing at all.
const candidateResolvedFor = new Set<string>();
for (const source of sources) {
if (autoLoadCancelled || loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS) break;
@@ -3763,9 +3532,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
),
);
try {
- // A repo can hold several downloaded quants: each failure marks that
- // quant tried and comes back for the next, so one corrupt file does not
- // cost the whole repo. The attempt cap bounds the loop.
+ // A repo can hold several downloaded quants: each failure marks that quant tried, so one
+ // corrupt file does not cost the whole repo.
while (!autoLoadCancelled && loadAttempts < MAX_AUTO_LOAD_ATTEMPTS) {
const candidate = await resolveAutoLoadCandidate(
source,
@@ -3788,8 +3556,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
} catch {
options?.abortSignal?.throwIfAborted();
hadNonTrustFailure = true;
- // loadAutoLoadCandidate records a refusal but not a rejection, so
- // mark it here or the next pass resolves the same quant forever.
+ // loadAutoLoadCandidate records a refusal but not a rejection, so mark it here or the next
+ // pass resolves the same quant forever.
skippedAutoLoadCandidates.add(
autoLoadCandidateKey(
candidate.kind,
@@ -3806,10 +3574,10 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
}
}
- // Cap also gates the default download, so total /api/inference/load
- // budget across cached + fallback is MAX_AUTO_LOAD_ATTEMPTS, not +1.
- // A tried-and-failed cached model stops here: that reason beats an unrelated Hub default.
- // An empty cache never sets loadFailure and still falls through.
+ // The cap gates the default download too, so the whole /load budget is MAX_AUTO_LOAD_ATTEMPTS.
+ // A tried-and-failed cached model stops here; an empty cache falls through.
+ // Cap also gates the default download, so the total /load budget across cached plus fallback is
+ // MAX_AUTO_LOAD_ATTEMPTS, not +1. An empty cache never sets loadFailure.
if (loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS || loadFailure.current) {
toast.dismiss(toastId);
if (loadFailure.current) {
@@ -3834,15 +3602,13 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
};
}
- // Only an empty *complete* inventory means the device has nothing; with a
- // source unknown, downloading risks duplicating a model already here.
+ // Only an empty *complete* inventory means the device has nothing.
if (inventoryIncomplete) {
toast.dismiss(toastId);
return { loaded: false, blockedByTrustRemoteCode: false };
}
- // Nothing on the device: fetch the default as a managed download job, and
- // only hand it to /api/inference/load once the bytes are here.
+ // Fetch the default as a managed download job and hand it to /api/inference/load only once the bytes are here.
try {
const rt = useChatRuntimeStore.getState();
if (rt.selectedGpuIds != null) {
@@ -3852,18 +3618,15 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
rt.selectedGpuIds,
rt.selectedGpuIndexKind,
);
- // Preflight BEFORE the transfer: active training or the GPU placement
- // guard can refuse this model, and learning that after several gigabytes
- // costs the user bandwidth and a long wait for nothing. The load below
- // reuses this snapshot, so it sends exactly what was cleared here.
+ // Preflight BEFORE the transfer: training or the placement guard can refuse this model, and
+ // learning that after gigabytes costs a long wait. The load reuses this snapshot.
if (
!(await canAutoLoad({
model_path: DEFAULT_CHAT_MODEL_REPO,
max_seq_length: 0,
is_lora: false,
gguf_variant: DEFAULT_CHAT_MODEL_VARIANT,
- // The same live-store GPU pick the load below sends (a fresh default
- // model has no remembered settings to prefer).
+ // The same live-store GPU pick the load below sends.
gpu_ids: defaultGpuIds ?? undefined,
gpu_memory_mode: rt.gpuMemoryMode,
speculative_type: specSettings.speculativeType,
@@ -3904,9 +3667,7 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
const loadResp = await loadModel({
model_path: DEFAULT_CHAT_MODEL_REPO,
hf_token: hfToken,
- // Model default under both modes: Auto layers + no pin means
- // resolveFitMaxSeqLength returns 0 for every mode (the canAutoLoad
- // preflight above sends the same).
+ // Model default under both modes: Auto layers + no pin means resolveFitMaxSeqLength returns 0.
max_seq_length: 0,
load_in_4bit: true,
is_lora: false,
@@ -3914,13 +3675,10 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
trust_remote_code: trustRemoteCode,
speculative_type: specSettings.speculativeType,
spec_draft_n_max: specSettings.specDraftNMax,
- // GPU Memory mode is a standing preference, so honor it on auto-load.
- // The layer/MoE/split knobs and the context pin are per-model: the live
- // store may hold edits drafted for a staged pick, and a fresh default
- // model has no remembered settings, so those stay at their defaults like
- // the cached-candidate path. The GPU pick deliberately differs (it's the
- // picker's current on-screen selection, which the canAutoLoad preflight
- // above already committed to).
+ // GPU Memory mode is a standing preference; the per-model layer/MoE/split knobs and context
+ // pin stay at their defaults, and the GPU pick is the on-screen one the preflight used.
+ // The GPU pick deliberately differs: it is the picker's on-screen selection, which the canAutoLoad
+ // preflight above already committed to.
gpu_memory_mode: rt.gpuMemoryMode,
gpu_layers: GPU_LAYERS_AUTO,
n_cpu_moe: 0,
@@ -3983,8 +3741,8 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
kvCacheDtype: loadResp.cache_type_kv ?? null,
loadedKvCacheDtype: loadResp.cache_type_kv ?? null,
...mlxRuntimeStateFrom(loadResp),
- // The request above omits n_parallel: a staged override left from a
- // preset would read as applied and be re-sent by the next Apply.
+ // The request above omits n_parallel: a staged override would read as applied and be re-sent
+ // by the next Apply.
nParallel: null,
loadedNParallel: null,
nBatch: null,
@@ -3995,14 +3753,12 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{
tensorParallel: loadResp.tensor_parallel ?? false,
loadedTensorParallel: loadResp.tensor_parallel ?? false,
loadedDisableVision: loadResp.disable_vision ?? false,
- // The request above omits disable_vision, so the echo is what the load
- // ran with; adopting it stops a previous model's Vision-off carrying over.
+ // The request above omits disable_vision, so the echo is what the load ran with.
disableVision: loadResp.disable_vision ?? false,
loadedVisionDisabledByUser:
loadResp.vision_disabled_by_user ?? false,
...loadedGpuMemoryFields(loadResp),
- // Drives the GPU Memory controls' diffusion gate; set alongside the
- // GPU fields on every load path so the gate can't read stale.
+ // Drives the GPU Memory controls' diffusion gate; set on every load path so the gate cannot read stale.
loadedIsDiffusion: loadResp.is_diffusion ?? false,
defaultChatTemplate: loadResp.chat_template ?? null,
chatTemplateOverride: null,
@@ -4061,8 +3817,9 @@ async function resolveQueuedEmptyLocalModel(abortSignal: AbortSignal): Promise<{
abortSignal.throwIfAborted();
const visibleState = useChatRuntimeStore.getState();
if (isExternalModelId(visibleState.params.checkpoint)) {
- // Hold the lifecycle lease across the probe. Its response cannot become
- // stale behind a foreground or sibling queued load, and only this owner
+ // Hold the lifecycle lease across the probe. Fail closed: a transient status error is not
+ // evidence that the local server is empty.
+ // Its response cannot become stale behind a foreground or sibling queued load, and only this owner
// may clear modelLoading afterward.
// A failed probe is not evidence the local server is empty, and neither is one read
// mid-replacement: fail closed, or the outgoing model stands in for the incoming one.
@@ -4178,15 +3935,8 @@ export function createOpenAIStreamAdapter(
unstable_threadId,
unstable_assistantMessageId,
}) {
- // Before the first await: hydration and a model load both run ahead of the
- // first resolveProjectId, and a send survives navigation. Only consulted
- // while the thread's own row is still missing.
- //
- // ...which for a fresh send is exactly the window this has to be right in, and the store
- // is no longer a safe reading of it: send() awaits document extraction, and initialize()
- // does not await its row write, so the user may be in another project by now and the run
- // would take its instructions, RAG sources and sandbox from one the message was never
- // sent to. A stamp OF null still wins -- a send from outside any project.
+ // Before the first await: send() awaits document extraction and initialize() does not await
+ // its row write, so the store is no longer a safe reading of the project. Null still wins.
const creationClaim = unstable_threadId
? readThreadCreationClaim(unstable_threadId)
: undefined;
@@ -4194,15 +3944,8 @@ export function createOpenAIStreamAdapter(
? creationClaim.projectId
: (useChatRuntimeStore.getState().activeProjectId ?? null);
await useChatRuntimeStore.getState().hydratePersistedSettings();
- // After the hydrate, not before: the backend reads some settings out of
- // SQLite at call time rather than taking them from the request -- Search
- // images picks the web_search schema that way -- and the mirror is a
- // trailing-edge debounce, so sending inside that window would run on the
- // value from before the toggle. Edits made while the initial GET was still
- // out are held back and only reach the debounce when hydration replays
- // them, so a flush ahead of it would find nothing queued and miss exactly
- // the first message after a startup edit. No wait at all unless something
- // is actually queued or in flight.
+ // After the hydrate: the backend reads some settings out of SQLite at call time and the
+ // mirror is a trailing debounce, so sending inside that window uses pre-toggle values.
await flushPendingChatSettings();
// And the migration a model pick may have just scheduled: an external
// selection gets no load or status callback, so this is the only join
@@ -4217,18 +3960,15 @@ export function createOpenAIStreamAdapter(
// resume and read B's settings for A.
const runThreadId =
unstable_threadId ?? useChatRuntimeStore.getState().activeThreadId;
- // Refused rather than run on whatever the store holds now: the wait only runs out
- // for a chat the user left mid-read, and the settings on screen are then some
- // other chat's. The run is recoverable by reopening the chat; a message sent with
- // another chat's tools and permission level is not.
+ // Refused rather than run on whatever the store holds now: the wait only runs out for a chat
+ // the user left, and a message sent with another chat's tools is not recoverable.
if (!(await awaitThreadScopedPairing(runThreadId))) {
throw new Error(
"This chat's settings could not be loaded, so the message was not sent. Reopen the chat and try again.",
);
}
let runtime = useChatRuntimeStore.getState();
- // Capture the thread ID once so it stays stable even if the user
- // switches chats while waiting for model load / auto-load.
+ // Capture the thread ID once so it stays stable across chat switches during model load.
const resolvedThreadId =
(runThreadId ?? runtime.activeThreadId) || undefined;
if (resolvedThreadId) {
@@ -4283,8 +4023,8 @@ export function createOpenAIStreamAdapter(
runtime = useChatRuntimeStore.getState();
}
}
- // Started only when the model hands this turn off, never on the toggle alone: arming
- // research offers it the tool, and it decides whether the message wants researching.
+ // Started only when the model hands this turn off: arming research offers the tool, the model
+ // decides whether to use it.
const startDeepResearch = async function* (
researchQuestion: string,
transitionSignal: AbortSignal = abortSignal,
@@ -4324,11 +4064,8 @@ export function createOpenAIStreamAdapter(
throw new Error("Load a model first.");
}
}
- // The turn's own settings, not the store's now. This generator runs AFTER the
- // model's stream, and the model selector stays usable while a turn is streaming: a
- // live read researched the question model A handed off with whichever model B had
- // since been picked, or failed outright when B runs no Studio tools. The one thing
- // that legitimately happened after the send is the empty-model resolution above.
+ // The turn's own settings, not the store's now: the model selector stays usable while a turn
+ // streams, so a live read would research with whichever model was picked since.
const sendTimeRuntime = runtime;
const withResolvedModel = <T extends typeof sendTimeRuntime>(base: T): T =>
queuedEmptyModelRuntime === null
@@ -4362,8 +4099,7 @@ export function createOpenAIStreamAdapter(
: withResolvedModel({
...sendTimeRuntime,
...queuedRunSettings,
- // The queued snapshot carries no model of its own; without a resolution
- // this turn's own is the answer.
+ // The queued snapshot carries no model of its own.
params: {
...queuedRunSettings.params,
checkpoint: sendTimeRuntime.params.checkpoint,
@@ -4470,10 +4206,8 @@ export function createOpenAIStreamAdapter(
: undefined;
const threadKey = resolvedThreadId;
- // The run is durable on the server, but Stop, archive and delete reach a background
- // thread only through this map: without a handle the supervisor kept planning against
- // a deleted conversation. Registered before the run exists, since the thread can be
- // stopped while createResearchRun is still in flight.
+ // Stop, archive and delete reach a background thread only through this map; registered before
+ // the run exists, since the thread can be stopped mid-create.
let researchRunId: string | null = null;
let researchStopRequested = false;
const researchServerCancel = () => {
@@ -4500,8 +4234,8 @@ export function createOpenAIStreamAdapter(
once: true,
});
try {
- // The normal history adapter persists messages after model execution,
- // but research validates the user message before it can start.
+ // Research validates the user message before it can start, unlike the history adapter, which
+ // persists after execution.
const storedUserMessage = (
await listStoredChatMessages(resolvedThreadId)
).find((message) => message.id === userMessage.id);
@@ -4525,8 +4259,8 @@ export function createOpenAIStreamAdapter(
userMessageId: userMessage.id,
assistantMessageId: unstable_assistantMessageId,
inferenceRequest,
- // Omitted when empty rather than sent as "": CreateResearchRun forbids unknown
- // fields, so an unconditional send 422s against a backend that predates it.
+ // Omitted when empty: CreateResearchRun forbids unknown fields, so an unconditional send 422s
+ // an older backend.
...(researchQuestion ? { question: researchQuestion } : {}),
...(researchInstructions ? { instructions: researchInstructions } : {}),
...(ragScope ? { ragScope } : {}),
@@ -4540,8 +4274,8 @@ export function createOpenAIStreamAdapter(
});
researchRunId = createdRun.id;
if (researchStopRequested) {
- // Stopped while createResearchRun was still in flight, so the handle had no
- // id to act on. Replay it rather than following a run the user already ended.
+ // Stopped while createResearchRun was in flight, so the handle had no id; replay it rather
+ // than follow a run the user ended.
void cancelResearchRun(createdRun.id).catch(() => {});
return;
}
@@ -4549,8 +4283,7 @@ export function createOpenAIStreamAdapter(
createdRun,
detachResearchFollow,
);
- // The toggle stays on through the run: it is what the user turned on, and it is what
- // is happening. The composer takes it away only once the chat's research is spent.
+ // The toggle stays on through the run; the composer takes it away once the chat's research is spent.
if (abortSignal.aborted) {
const detached = Boolean(
(abortSignal.reason as { detach?: boolean } | undefined)?.detach,
@@ -4613,8 +4346,9 @@ export function createOpenAIStreamAdapter(
!options.pairId &&
(options.modelType === undefined || options.modelType === "base");
const toolConfirmationIdsByBackendId = new Map<string, string>();
- // Local tool ids ("call_0") repeat across turns, panes and conversations, so scope by pane
- // AND thread. unstable_threadId alone, no activeThreadId fallback: the reader has only
+ // Local tool ids ("call_0") repeat across turns and panes, so scope by pane AND thread; the
+ // reader has only threadListItem.remoteId.
+ // unstable_threadId alone, with no activeThreadId fallback: the reader has only
// threadListItem.remoteId, which is exactly this value.
const toolOutputPaneScope = toolThreadScope(
toolPaneScope(options.modelType, options.pairId),
@@ -4624,8 +4358,7 @@ export function createOpenAIStreamAdapter(
toolOutputKey(toolOutputPaneScope, id);
const runToolLiveOutputKeys = new Set<string>();
const resolvedThreadKey = resolvedThreadId ?? null;
- // Which conversation was on screen when this run started. A first turn has no id yet, so
- // this is the only way to tell later whether the user has switched away from it.
+ // Which conversation was on screen when this run started; a first turn has no id yet.
const activeThreadIdAtRunStart =
useChatRuntimeStore.getState().activeThreadId ?? null;
const pendingImageEditReferenceForRun = runtime.pendingImageEditReference;
@@ -4651,7 +4384,6 @@ export function createOpenAIStreamAdapter(
store.clearPendingImageEditReference();
}
};
- // Wait for in-progress model load before inferring.
if (runtime.modelLoading) {
toast.info("Waiting for model to finish loading…");
try {
@@ -4674,7 +4406,6 @@ export function createOpenAIStreamAdapter(
}
queuedEmptyModelRuntime = resolution.modelRuntime;
if (!resolution.loaded) {
- // A reported failure already names the model; generic advice buries it.
if (!resolution.loadFailureReported) {
toast.error(
resolution.blockedByTrustRemoteCode
@@ -4692,7 +4423,6 @@ export function createOpenAIStreamAdapter(
}
}
- // Re-read store after auto-load / model-ready wait.
const liveRuntime = useChatRuntimeStore.getState();
runtime = queuedRunSettings
? queuedRunSettings.params.checkpoint
@@ -4790,9 +4520,9 @@ export function createOpenAIStreamAdapter(
runtime = { ...runtime, deepResearchEnabled: false };
toast.info("Deep Research needs a model that supports tools");
}
- // Project sources auto-scope: a chat inside a project retrieves from the
- // project's indexed sources even when the Docs pill is off. The probe is
- // cached, so this is one round trip per project every ~30s at most.
+ // A chat inside a project retrieves from its indexed sources even with the Docs pill off;
+ // the probe is cached.
+ // The probe is cached, so this is one round trip per project every ~30s at most.
const ragProjectId = await resolveProjectId(
resolvedThreadId,
readThreadRecord,
@@ -4870,8 +4600,7 @@ export function createOpenAIStreamAdapter(
throw new Error("Missing connection API key.");
}
- // Image-generation flag (OpenAI cloud + Responses-capable model);
- // computed first so Gemini image mode can suppress Search/Code.
+ // Image-generation flag; computed first so Gemini image mode can suppress Search/Code.
const imageGenerationEnabledForThisTurn = Boolean(
externalProvider &&
externalSelection &&
@@ -4882,8 +4611,7 @@ export function createOpenAIStreamAdapter(
externalProvider.baseUrl,
),
);
- // Per-model Search/Code allowances live in
- // providerSupportsBuiltin*; this flag just signals image-mode.
+ // Per-model Search/Code allowances live in providerSupportsBuiltin*; this flag only signals image-mode.
const geminiImageModeForThisTurn =
externalProvider?.providerType === "gemini" &&
imageGenerationEnabledForThisTurn;
@@ -4908,9 +4636,8 @@ export function createOpenAIStreamAdapter(
externalProvider.baseUrl,
),
);
- // Fetch pill is independent of Search (Anthropic bills web_fetch
- // separately). Sourced from `webFetchToolsEnabled`; on providers
- // without web_fetch the toggle is forced off in chat-page setState.
+ // Fetch is independent of Search (Anthropic bills web_fetch separately); forced off in
+ // chat-page setState where unsupported.
const webFetchEnabledForThisTurn = Boolean(
externalProvider &&
webFetchToolsEnabled &&
@@ -4920,11 +4647,8 @@ export function createOpenAIStreamAdapter(
externalProvider &&
providerSupportsBuiltinWebFetch(externalProvider.providerType),
);
- // Which side of the connection the Code pill runs code on. Hosted
- // `code_execution` and local `python` / `terminal` are two trust
- // boundaries, not two spellings of one feature, so the stored pill keeps
- // meaning the provider's sandbox wherever it meant that before the Unsloth
- // loop reached these providers. See code-tool-placement.ts.
+ // Hosted `code_execution` and local `python`/`terminal` are two trust boundaries, not two
+ // spellings of one feature. See code-tool-placement.ts.
const {
local: studioLocalCodeTools,
hosted: hostedCodeToolsForThisTurn,
@@ -4949,9 +4673,8 @@ export function createOpenAIStreamAdapter(
messages,
!isExternalRequest,
);
- // toOpenAIMessages emits assistant tool_calls + role="tool"
- // follow-ups; the backend Gemini translator rebuilds the
- // functionCall / functionResponse parts (with thoughtSignature).
+ // toOpenAIMessages emits assistant tool_calls plus role="tool" follow-ups; the backend Gemini
+ // translator rebuilds the functionCall/functionResponse parts.
const outboundMessages = survivingMessages
.flatMap((message) => toOpenAIMessages(message, !isExternalRequest))
.filter((message): message is NonNullable<typeof message> =>
@@ -4976,9 +4699,8 @@ export function createOpenAIStreamAdapter(
break;
}
}
- // OpenAIChatMessage is a structural superset of SerializedMessage
- // on the role/content axis; cast through unknown since
- // referenceMessage carries no tool_calls (plain assistant turn).
+ // OpenAIChatMessage is a structural superset of SerializedMessage on the role/content axis;
+ // referenceMessage carries no tool_calls.
outboundMessages.splice(
insertAt,
0,
@@ -4986,21 +4708,20 @@ export function createOpenAIStreamAdapter(
);
}
- // The run's messages stop at the user turn (this is a sibling branch), so the
- // partial is appended here for the backend to resume.
+ // The run's messages stop at the user turn, so the partial is appended here for the backend to resume.
if (continuation) {
outboundMessages.push({
role: "assistant",
content: continuation.partial,
});
- // The original assistant message is not in this branch, so without its
- // signature the model history goes back unsigned.
+ // The original assistant message is not in this branch, so without its signature the history
+ // goes back unsigned.
attachAssistantThoughtSignature(
outboundMessages,
continuation.thoughtSignature,
);
- // Anthropic 400s when the last message is an assistant turn, so it gets an
- // instruction turn after the partial instead of a prefill.
+ // Anthropic 400s when the last message is an assistant turn, so append an instruction turn
+ // instead of a prefill.
if (rejectsAssistantPrefill(externalProvider?.providerType)) {
outboundMessages.push({
role: "user",
@@ -5030,9 +4751,7 @@ export function createOpenAIStreamAdapter(
const webLabel = providerShipsWebFetch
? "web search or web fetch"
: "web search";
- // Treat search and fetch as one "any web tool" axis so the guard
- // only warns when neither pill is on; checking webSearch alone
- // mis-fired when only Fetch was on and suppressed web_fetch.
+ // Treat search and fetch as one "any web tool" axis: checking webSearch alone mis-fired when only Fetch was on.
const anyWebEnabledForThisTurn =
webSearchEnabledForThisTurn || webFetchEnabledForThisTurn;
if (
@@ -5108,11 +4827,10 @@ export function createOpenAIStreamAdapter(
targetMessages.unshift({ role: "system", content: text });
}
- // Scan post-prune history so a refused user turn's image/audio
- // doesn't gate or mis-attribute the next turn.
+ // Scan post-prune history so a refused user turn's image/audio does not gate or mis-attribute the next turn.
const imageBase64 = findLatestUserImageBase64(survivingMessages);
- // A continuation resumes the turn as it was sent: picking up a clip staged in the
- // composer since would switch it onto the audio path, which cannot be continued.
+ // A continuation resumes the turn as it was sent: a clip staged since would switch it onto
+ // the uncontinuable audio path.
const audioBase64 = findLatestUserAudioBase64(
survivingMessages,
!queuedRunSettings && !continuation,
@@ -5120,9 +4838,7 @@ export function createOpenAIStreamAdapter(
const videoBase64 = findLatestUserVideoBase64(survivingMessages);
const hasOutboundImage = Boolean(imageBase64);
- // Keep render_html local-only and mirror the backend image-turn gate.
- // Canvas is independent of Search/Code: a local tool-capable model
- // with Canvas on exposes render_html even with no other pills active.
+ // Canvas is independent of Search/Code: render_html stays local-only and mirrors the backend image-turn gate.
const renderHtmlToolEnabledForThisTurn = Boolean(
!isExternalRequest &&
supportsTools &&
@@ -5141,10 +4857,8 @@ export function createOpenAIStreamAdapter(
addSystemInstruction(outboundMessages, effectiveDisabledToolGuard);
addSystemInstruction(outboundMessages, artifactInstruction);
- // Block when ANY image is in the outbound payload (current or prior
- // turns) and the loaded model can't process images. Once a chat
- // contains an image, a non-vision model can't respond — the user
- // starts a new chat to switch models.
+ // Block when ANY image is in the outbound payload and the loaded model cannot process images;
+ // switching models means starting a new chat.
if (imageBase64) {
const activeModel = runtime.models.find(
(m) => m.id === params.checkpoint,
@@ -5165,14 +4879,10 @@ export function createOpenAIStreamAdapter(
});
if (imageGateReason) {
toast.error(imageGateReason);
- // Flip the per-thread running flag on→off so compare-mode
- // waitForRunEnd resolves instead of hanging: this gate fires
- // before the streaming path's setThreadRunning(true).
+ // Flip the per-thread running flag on->off so compare-mode waitForRunEnd resolves: this gate
+ // fires before the streaming path's setThreadRunning(true).
const gatedThreadKey = resolvedThreadId || "__default";
- // Own token: siblings share "__default", so an ownerless clear would drop their
- // entries while they are still generating. Marked as the gate's, so a reader that
- // means "this thread's run started" is not told one did by a pair of flags that
- // stand for a request nobody made.
+ // Own token: siblings share "__default", so an ownerless clear would drop entries that are still generating.
const gateOwner = createImageGateRunOwner();
runtime.setThreadRunning(gatedThreadKey, true, { owner: gateOwner });
runtime.setThreadRunning(gatedThreadKey, false, { owner: gateOwner });
@@ -5180,7 +4890,6 @@ export function createOpenAIStreamAdapter(
throw new Error(imageGateReason);
}
}
- // Clear pending audio from store after extracting (consumed on send).
if (audioBase64 && !queuedRunSettings) {
const audioName = runtime.pendingAudioName;
if (audioName) {
@@ -5198,9 +4907,7 @@ export function createOpenAIStreamAdapter(
);
const threadKey = resolvedThreadId || "__default";
- // A first turn files its handles under "__default"; autosave then assigns a real id and
- // adoptDefaultThreadRun re-keys them mid-run. Resolve per use so later writes and the
- // final clear follow the run instead of stranding entries behind.
+ // A first turn files its handles under "__default" and autosave re-keys them mid-run, so resolve per use.
const liveThreadKey = (owner: () => void) =>
threadKey === "__default"
? useChatRuntimeStore.getState().runKeyForOwner(threadKey, owner)
@@ -5212,14 +4919,11 @@ export function createOpenAIStreamAdapter(
? crypto.randomUUID()
: `${Date.now()}-${Math.random().toString(36).slice(2)}`;
- // Per-run abort, chained to assistant-ui's signal. cancelByThreadId only holds the visible
- // thread's cancelRun(), so this controller is the only way to end a backgrounded chat's
- // request; the cancel POST below reaches llama-server only.
+ // Per-run abort chained to assistant-ui's signal: cancelByThreadId only holds the visible thread's cancelRun().
const runAbort = new AbortController();
const runSignal = runAbort.signal;
const forwardAbort = () => runAbort.abort(abortSignal.reason);
- // Declared here, not at its registration below: it doubles as this run's identity token
- // on the per-thread maps (see registerThreadServerCancel).
+ // Declared here because it doubles as this run's identity token on the per-thread maps.
const serverCancel = () => runAbort.abort();
if (abortSignal.aborted) {
forwardAbort();
@@ -5227,7 +4931,6 @@ export function createOpenAIStreamAdapter(
abortSignal.addEventListener("abort", forwardAbort, { once: true });
}
- // ── Audio model path (non-streaming) ─────────────────────
const activeModel = runtime.models.find(
(m) => m.id === params.checkpoint,
);
@@ -5241,10 +4944,8 @@ export function createOpenAIStreamAdapter(
!imageBase64 &&
!audioBase64 &&
!videoBase64 &&
- // Continue seeds the partial into the sibling assistant and yields it before the
- // request starts, so the autosave can land before admission does. Admission
- // refuses a substantive placeholder with a 409, which is not a fallback error,
- // and the turn would fail outright. Continuations keep the legacy stream.
+ // Continue yields the seeded partial before the request starts so the autosave lands before
+ // admission, which 409s a substantive placeholder. Continuations keep the legacy stream.
!continuation &&
resolvedThreadId &&
!isThreadIncognito(resolvedThreadId) &&
@@ -5260,9 +4961,8 @@ export function createOpenAIStreamAdapter(
let generationFirstChunkAt: number | undefined;
let generationChunkCount = 0;
let generationStopRequested = false;
- // Set when the no-progress deadline fires on THIS stream rather than on a recovery
- // follower. Both must persist the marker, or the next reload attaches another
- // follower and blocks the composer for a further deadline.
+ // Both this stream and a recovery follower must persist the marker, or the next reload
+ // attaches another follower and blocks the composer for a further deadline.
let generationStalled = false;
const generationCustom = () =>
generationRunId
@@ -5295,9 +4995,8 @@ export function createOpenAIStreamAdapter(
{
model: params.checkpoint,
messages: outboundMessages,
- // Same run in both registries: without it the backend files this under no
- // thread, and the stop-chats prompt counts the named local run and the
- // unnamed backend one as two.
+ // Same run in both registries: otherwise the backend files it under no thread and the
+ // stop-chats prompt counts one run as two.
...(resolvedThreadId ? { thread_id: resolvedThreadId } : {}),
stream: false,
temperature: params.temperature,
@@ -5370,63 +5069,47 @@ export function createOpenAIStreamAdapter(
if (runSignal.aborted) return;
runtime.setGeneratingStatus("waiting");
}, warmupDelayMs);
- // Flagged local/external so the model-swap gate only counts the chats a reload ends; the
- // backend leaves external-provider runs out of active_generations for the same reason.
+ // Flagged local/external so the model-swap gate counts only the chats a reload ends; the
+ // backend excludes external runs from active_generations for the same reason.
releaseCurrentPreStreamRun();
runtime.setThreadRunning(threadKey, true, {
local: !isExternalRequest,
owner: serverCancel,
});
- // Seeded with the partial so the bubble reads as one response; the boundary lets
- // the finalizers re-derive the new output and repair a repeat/restart.
+ // Seeded with the partial so the bubble reads as one response; the boundary lets the
+ // finalizers repair a repeat or restart.
let cumulativeText = continuation ? continuation.partial : "";
- // Reading `cumulativeText` at all costs O(reply): each `+=` builds a cons
- // string and the first read of it flattens the whole reply, so one
- // charCodeAt per arrival is as expensive as a scan. Everything below that
- // used to consult the buffer once per arrival is fed the delta instead,
- // through `appendCumulative`, and the buffer is only read where the reply
- // is actually published.
- //
- // Answers "does the text end inside <think>" from what each arrival adds.
+ // Reading `cumulativeText` costs O(reply): each `+=` builds a cons string that the first read
+ // flattens, so one charCodeAt per arrival is as expensive as a scan. Everything below is fed
+ // the delta through `appendCumulative` and the buffer is read only where the reply is
+ // published. This tracks "does the text end inside <think>".
const thinkTags = createThinkTagTracker();
- // Answers "could the trailing ${...} strip cut anything" the same way, so
- // the strip itself runs only on an arrival that ends in a fragment.
+ // Same for "could the trailing ${...} strip cut anything", so the strip only runs on an
+ // arrival that ends in a fragment.
const placeholderWatch = createTrailingPlaceholderWatch();
- // What the cap is measured against: only grows, unlike cumulativeText,
- // and counts tool-argument deltas, which never reach it.
+ // What the cap is measured against: only grows, and counts tool-argument deltas.
let streamedChars = 0;
- // Whether this run appended reply text of its own. A continuation is
- // SEEDED with the previous run's partial, and that partial is the middle
- // of a reply someone is still writing rather than the end of a finished
- // one, so a run that adds nothing to it must not have its tail trimmed.
- // Read by the trailing-fragment strip after the stream.
+ // Whether this run appended reply text of its own: a continuation is SEEDED with the previous
+ // run's partial, so a run that adds nothing must not have its tail trimmed.
let producedReplyText = false;
const continuationPartial = continuation?.partial ?? "";
- // Local backends (and a self-hosted vLLM / llama-server, which get the flags)
- // resume at the exact token boundary, so their output is already the rest of the
- // answer and trimming could only delete words the model meant to write. The repair
- // is for providers that may ignore the prefill and repeat or restart.
+ // Local backends resume at the exact token boundary, so trimming could only delete words the
+ // model meant; the repair is for providers that repeat or restart.
const repairContinuation =
isExternalRequest && !resumesExactly(externalProvider?.providerType);
- // Streamed publishes get the cumulative text unchanged; the repairs run once, on
- // the finished turn. Both of them re-decide as the continuation grows, so running
- // them per arrival published a SHORTER text than the arrival before it, and the
- // restart branch published only the continuation, dropping the whole partial the
- // reader was already looking at.
+ // The repairs run once, on the finished turn: both re-decide as the continuation grows, so
+ // per-arrival runs published a shorter text than the arrival before.
const mergeContinuation = createContinuationMerger(
continuationPartial,
repairContinuation,
);
- // The parse of everything already streamed, extended by each delta.
- // The final merge can rewrite the prefix it is handed, and a rewritten prefix is
- // exactly what an extend-only parse cannot follow, so that one path keeps
- // reparsing the whole reply as before. It is a continuation of an external
- // provider that may repeat itself, not the streaming case.
+ // The parse of everything streamed so far, extended by each delta. The final merge can rewrite
+ // the prefix it is handed, which an extend-only parse cannot follow, so it reparses.
const segmentedText = createSegmentedAssistantText({
trustAppends: !(continuationPartial && repairContinuation),
});
- // The single place `cumulativeText` grows, so everything derived from it
- // sees the same characters in the same order.
+ // The single place `cumulativeText` grows, so everything derived from it sees the same
+ // characters in the same order.
const appendCumulative = (text: string): void => {
if (!text) {
return;
@@ -5436,24 +5119,21 @@ export function createOpenAIStreamAdapter(
thinkTags.append(text);
placeholderWatch.append(text);
};
- // A resumed turn starts with the partial already in the buffer. One read
- // of it, once per turn, is what puts the trackers in step with it.
+ // A resumed turn starts with the partial already in the buffer; one read of it puts the trackers in step.
if (cumulativeText) {
thinkTags.append(cumulativeText);
placeholderWatch.append(cumulativeText);
}
- // Every streamed yield carries the repaired text, not just the terminal ones:
- // assistant-ui drops whatever is yielded after an abort, so on Stop the last
- // STREAMED yield is what gets saved.
+ // Every streamed yield carries the repaired text: assistant-ui drops whatever is yielded after
+ // an abort, so on Stop the last STREAMED yield is what gets saved.
let codexReasoningLedger: CodexReasoningLedger = { byToolCall: {} };
let codexRoundToolCallIds: string[] = [];
let contextTruncation: OpenAIChatChunk["context_truncated"];
const liveAssistantContent = () =>
buildAssistantContent(mergeContinuation(cumulativeText));
- // Provisional reason on every streamed yield: an abort skips the terminal yields
- // and a reload rebuilds messages as "complete", so a stopped turn would otherwise
- // lose why it was short. The terminal yields overwrite or clear it.
+ // Provisional reason on every streamed yield: an abort skips the terminal yields and a reload
+ // rebuilds messages as "complete".
const liveCustom = () => ({
...reasoningDurationTracker.metadata(),
openaiCodexReasoning: codexReasoningLedger,
@@ -5463,14 +5143,12 @@ export function createOpenAIStreamAdapter(
});
// Why this turn stopped early. Drives the Continue affordance.
let incompleteReason: IncompleteReason | null = null;
- // MLX reports finish_reason "stop" even at the cap, so an exhausted budget is its
- // only truncation signal. Everyone else reports "length" honestly.
+ // MLX reports finish_reason "stop" even at the cap, so an exhausted budget is its only truncation signal.
let requestedMaxTokens: number | undefined;
const isMlxRequest = !isExternalRequest && activeModel?.isMlx === true;
const reasoningDurationTracker = createReasoningDurationTracker();
- // True while wrapping a `delta.reasoning_content` stream in
- // <think>...</think> for parseAssistantContent. Lives outside the
- // SSE loop because the close tag fires when content arrives.
+ // True while wrapping a `delta.reasoning_content` stream in <think> for parseAssistantContent;
+ // outside the SSE loop because the close tag fires when content arrives.
let reasoningContentOpen = false;
type ToolCallProvenance = {
source?: string;
@@ -5486,7 +5164,7 @@ export function createOpenAIStreamAdapter(
textCursor?: number;
_delta_index?: number;
_has_stable_id?: boolean;
- // No `arguments` field at all; a zero-parameter tool sends """".
+ // No `arguments` field at all; a zero-parameter tool sends "".
_announced_only?: boolean;
_resend_suspect?: boolean;
_superseded?: boolean;
@@ -5494,15 +5172,12 @@ export function createOpenAIStreamAdapter(
extra_content?: unknown;
provenance?: ToolCallProvenance;
};
- // Tool call parts, cumulative; result lands on tool_end.
const toolCallParts: PositionedToolCallPart[] = [];
- // Ids for calls the stream gave none (issue #9807). A card is minted
- // before its part joins `toolCallParts`, so without this a batch opening
- // three calls mints one id three times.
+ // Ids for calls the stream gave none (#9807): a card is minted before its part joins
+ // `toolCallParts`, so a batch of three would mint one id three times.
const reservedToolCallIds = new Set<string>();
- // Of those, the ones the provider sent. `_has_stable_id` cannot say which:
- // a split marks every card but the last with it, to keep a late id off the
- // calls already spoken for.
+ // Of those, the ones the provider sent: a split marks every card but the last, keeping a late
+ // id off calls already spoken for.
const providerSentToolCallIds = new Set<string>();
const boundaryScans = new Map<
string,
@@ -5510,18 +5185,15 @@ export function createOpenAIStreamAdapter(
>();
const isPlainRecord = (v: unknown): v is Record<string, unknown> =>
typeof v === "object" && v !== null && !Array.isArray(v);
- // Forks whose last object never closed, the backend's open_tail_keys.
- // `{"a":1}{` is not marked truncated, so the lone brace would persist as a
- // card nothing completes.
+ // Forks whose last object never closed (the backend's open_tail_keys): `{"a":1}{` is not
+ // marked truncated, so the lone brace would persist as a card nothing completes.
const openTailIds = new Set<string>();
- // Metadata from a delta repeating a closed card's name. That name is
- // either that call's, resent, or the next call to the same tool announcing
- // itself, and only the object that follows tells them apart.
+ // Metadata from a delta repeating a closed card's name: only the object that follows tells a
+ // resend from the next call to the same tool.
const pendingExtraByPartId = new Map<string, Record<string, unknown>>();
const endProviderTurn = (): boolean => {
let changed = false;
- // _call_is_finished, on the arguments alone: the backend holds the
- // fork back whatever id reached it.
+ // _call_is_finished, on the arguments alone: the backend holds the fork back whatever id reached it.
for (const tailId of openTailIds) {
const at = toolCallParts.findIndex((p) => p.toolCallId === tailId);
if (at === -1) continue;
@@ -5532,9 +5204,8 @@ export function createOpenAIStreamAdapter(
releaseStreamedCard(part.toolCallId);
changed = true;
}
- // An announcement another call took over, and a name that only looked like
- // a resent, were never calls. A lone announcement stays: a zero-parameter
- // tool looks like that.
+ // An announcement another call took over was never a call; a lone one stays, since a
+ // zero-parameter tool looks like that.
for (let at = toolCallParts.length - 1; at >= 0; at -= 1) {
const part = toolCallParts[at] as PositionedToolCallPart;
if (
@@ -5544,8 +5215,8 @@ export function createOpenAIStreamAdapter(
part.argsText
)
continue;
- // Its metadata goes to the call it was mistaken for: Gemini stows a
- // thought signature there and rejects a replay without one.
+ // Its metadata goes to the call it was mistaken for: Gemini stows a thought signature there
+ // and rejects a replay without one.
const previous = toolCallParts[at - 1] as
| PositionedToolCallPart
| undefined;
@@ -5559,9 +5230,8 @@ export function createOpenAIStreamAdapter(
releaseStreamedCard(part.toolCallId);
changed = true;
}
- // _normalized_call drops a nameless call before reserving a card id, so a
- // card kept here holds a number the backend gives the next round, whose
- // events then land on this blank card.
+ // _normalized_call drops a nameless call before reserving a card id, so a card kept here
+ // holds a number the backend gives the next round.
for (let at = toolCallParts.length - 1; at >= 0; at -= 1) {
const part = toolCallParts[at] as PositionedToolCallPart;
if (part.toolName || part._delta_index === undefined) continue;
@@ -5570,8 +5240,7 @@ export function createOpenAIStreamAdapter(
changed = true;
}
for (const [partId, waiting] of pendingExtraByPartId) {
- // No object ever came, so the repeated name was that call's after all
- // and so is the metadata that rode it.
+ // No object ever came, so the repeated name was that call's after all, and so is its metadata.
const at = toolCallParts.findIndex((part) => part.toolCallId === partId);
if (at === -1) continue;
const part = toolCallParts[at] as PositionedToolCallPart;
@@ -5585,13 +5254,11 @@ export function createOpenAIStreamAdapter(
changed = true;
}
pendingExtraByPartId.clear();
- // The backend numbers the calls that survive: a card dropped above leaves
- // a gap, and a claim displaced one for what turned out not to be a call.
- // Both are settled here, where the backend settles them.
+ // The backend numbers the calls that survive, so gaps from dropped and displaced cards are
+ // settled here, where the backend settles them.
if (changed) renumberMintedCards();
openTailIds.clear();
- // The next round opens at index 0 again, and without this "B" then
- // "C" across the boundary named one call "BC".
+ // The next round opens at index 0 again, and without this "B" then "C" across the boundary named one call "BC".
for (let at = 0; at < toolCallParts.length; at += 1) {
const part = toolCallParts[at] as PositionedToolCallPart;
if (part._delta_index === undefined) continue;
@@ -5615,18 +5282,15 @@ export function createOpenAIStreamAdapter(
reservedToolCallIds.add(partId);
bindStreamedToolCallCard(toolPartIdByBackendId, partId);
};
- // Raw tool_args accumulator per card: the backend forwards arguments while
- // the model is still WRITING them, and the partial parse below feeds the
- // card's args so the code renders live.
+ // Raw tool_args accumulator per card: the backend forwards arguments while the model is still
+ // writing them, and the partial parse feeds the card's args.
const liveArgsTextById = new Map<string, string>();
- // A dropped card gives its id back: the backend never reserved it, so
- // holding it makes the next round's mint skip a number it then reuses.
+ // A dropped card gives its id back: holding it makes the next round's mint skip a number it then reuses.
const releaseStreamedCard = (partId: string): void => {
reservedToolCallIds.delete(partId);
toolPartIdByBackendId.delete(partId);
- // A card that took a late id answers to a run-unique part id, so the id
- // the provider sent is a separate key pointing at it. Left behind, it makes
- // the next round's mint skip a number the backend reuses.
+ // A card that took a late id answers to a run-unique part id, so the provider's id is a
+ // separate key pointing at it; left behind it makes the mint skip a number.
for (const [backendId, mapped] of [...toolPartIdByBackendId]) {
if (mapped !== partId) continue;
toolPartIdByBackendId.delete(backendId);
@@ -5638,8 +5302,9 @@ export function createOpenAIStreamAdapter(
liveArgsTextById.delete(partId);
pendingExtraByPartId.delete(partId);
};
- // The backend reserves provider ids before it mints; the client can
- // only move a card aside once the claim lands.
+ // The backend reserves provider ids before it mints; a card can only move aside once the claim lands.
+ // Renumber in the order the backend walks them, or tool_start reaches the wrong card: three calls
+ // in one delta and a later `tool_call_1` cross-wire the second and third.
const renameStreamedCard = (from: string, to: string): void => {
reservedToolCallIds.delete(from);
toolPartIdByBackendId.delete(from);
@@ -5650,8 +5315,8 @@ export function createOpenAIStreamAdapter(
const live = liveArgsTextById.get(from);
if (live !== undefined) liveArgsTextById.set(to, live);
liveArgsTextById.delete(from);
- // The turn-end sweep claims parked metadata by the part id the card holds
- // then, so it has to travel with the rename or the sweep finds nothing.
+ // The turn-end sweep claims parked metadata by the part id the card holds then, so it must
+ // travel with the rename.
const pending = pendingExtraByPartId.get(from);
if (pending) pendingExtraByPartId.set(to, pending);
pendingExtraByPartId.delete(from);
@@ -5659,16 +5324,13 @@ export function createOpenAIStreamAdapter(
if (at !== -1) codexRoundToolCallIds[at] = to;
paintStreamedCard(to);
};
- // The backend reserves provider ids before minting any card id, so a claim
- // mid-response moves every card already numbered. Renumber in the order the
- // backend walks them, or tool_start reaches the wrong card: three calls in
- // one delta and a later `tool_call_1` cross-wire the second and third.
+ // The backend reserves provider ids before minting, so a claim mid-response moves every
+ // numbered card. Renumber in the backend's walk order, or tool_start hits the wrong card.
const renumberMintedCards = (claimed?: string): void => {
const minted = toolCallParts.filter(
(part) =>
!providerSentToolCallIds.has(part.toolCallId) &&
- // This round's cards only. Earlier ones may carry a result, and the
- // backend's ledger is append-only: it never renumbers, so neither may this.
+ // This round's cards only: the backend's ledger is append-only and never renumbers, so neither may this.
(part as PositionedToolCallPart)._delta_index !== undefined,
) as PositionedToolCallPart[];
const carried = minted.map((part) => ({
@@ -5686,13 +5348,12 @@ export function createOpenAIStreamAdapter(
liveArgsTextById.delete(held.from);
openTailIds.delete(held.from);
pendingExtraByPartId.delete(held.from);
- // Cleared before any is minted again: the mint reads the ids the parts
- // hold, and a stale one makes the first card skip the backend's number.
+ // Cleared before any is minted again: the mint reads the ids the parts hold, and a stale one
+ // makes the first card skip the backend's number.
const at = toolCallParts.indexOf(held.part);
if (at !== -1) toolCallParts[at] = { ...held.part, toolCallId: "" };
}
- // Held back only now: the loop above frees every minted id, and one of
- // them is the spelling being claimed.
+ // Held back only now: the loop above frees every minted id, and one of them is the spelling being claimed.
if (claimed !== undefined) reservedToolCallIds.add(claimed);
for (const held of carried) {
const to = mintStreamedCardId(held.part._delta_index);
@@ -5709,10 +5370,8 @@ export function createOpenAIStreamAdapter(
}
}
};
- /**
- * Parts for the calls after the first in a slot holding several. Nothing
- * per-call is copied: the thought signature goes only to the last.
- */
+ /** Parts for the calls after the first in a slot holding several. Nothing per-call is copied:
+ * the thought signature goes only to the last. */
const bornSplitToolCalls = (
extraSegments: string[],
toolName: string,
@@ -5747,13 +5406,9 @@ export function createOpenAIStreamAdapter(
...(deltaIndex !== undefined ? { _delta_index: deltaIndex } : {}),
};
});
- // Backend tool ids ("call_0", ...) restart every response, so a bare id as
- // store key lets a later turn's stream overwrite the preserved output an
- // earlier still-mounted finished card reads (the tool_start stale-clear
- // only guards the forward direction). Mint one per-run-unique part id per
- // backend id (confirmation ids already synthesize their own) so each card
- // key is unique; every tool_start/output/args/end resolves the same id via
- // this map, dropped at tool_end.
+ // Backend tool ids ("call_0", ...) restart every response, so a bare id as store key lets a
+ // later turn's stream overwrite a finished card's output. Mint one run-unique part id per
+ // backend id, resolved through this map and dropped at tool_end.
const toolPartIdByBackendId = new Map<string, string>();
const resolveToolPartId = (backendToolCallId: string): string =>
resolveToolCallPartId(
@@ -5763,8 +5418,8 @@ export function createOpenAIStreamAdapter(
toolCallParts[toolCallParts.length - 1]?.toolCallId ?? "",
() => `${backendToolCallId}:${crypto.randomUUID()}`,
);
- // Latest Gemini text-part thoughtSignature; pinned onto the final
- // text MessagePart so next-turn replay carries it.
+ // Latest Gemini text-part thoughtSignature, pinned onto the final text MessagePart so the
+ // next turn's replay carries it.
let latestTextThoughtSignature: string | undefined;
const pinTextThoughtSignature = <T extends { type: string }>(
parts: T[],
@@ -5796,9 +5451,7 @@ export function createOpenAIStreamAdapter(
})
.sort((a, b) => a.cursor - b.cursor || a.index - b.index);
- // The distinct cursors, which are where the text is cut into runs. The
- // runs before them never change again once a later one exists, so only
- // the last one is still growing.
+ // The distinct cursors, where the text is cut into runs: only the last one is still growing.
const boundaries: number[] = [];
for (const positioned of positionedTools) {
if (boundaries[boundaries.length - 1] !== positioned.cursor) {
@@ -5828,8 +5481,8 @@ export function createOpenAIStreamAdapter(
return pinTextThoughtSignature(assembled);
};
- // Yielded before the request starts: an abort during load skips the partial-content
- // yield below, saving an empty message and stranding the resumed text.
+ // Yielded before the request starts: an abort during load skips the partial-content yield
+ // below, saving an empty message.
if (continuation) {
yield {
content: buildAssistantContent(cumulativeText),
@@ -5871,8 +5524,7 @@ export function createOpenAIStreamAdapter(
}
reasoningDurationTracker.finishGroup();
};
- // Anthropic document_citations payload, converted to Sources-panel
- // parts at end-of-stream so inline [N] markers have matching entries.
+ // Anthropic document_citations, converted at end-of-stream so inline [N] markers have matching entries.
const documentCitationParts: Array<{
type: "source";
sourceType: "url";
@@ -5881,20 +5533,18 @@ export function createOpenAIStreamAdapter(
title: string;
metadata?: { description: string };
}> = [];
- // Latched on the `anthropic_refusal` tool event; stamped onto final
- // assistant metadata as `custom.anthropicRefusal` to drive the
- // history-prune above.
+ // Latched on the `anthropic_refusal` tool event and stamped onto final metadata as
+ // `custom.anthropicRefusal` to drive the history prune.
let anthropicRefusalSeen = false;
let serverMetadata: {
usage?: ServerUsage;
timings?: ServerTimings;
} | null = null;
- // Colab-style proxies can swallow fetch aborts, so also POST
- // /inference/cancel explicitly on abort.
+ // Colab-style proxies can swallow fetch aborts, so also POST /inference/cancel explicitly.
const onAbortCancel = () => {
- // assistant-ui aborts with detach=true when a runtime unmounts and detach=false for an
- // explicit Stop. Only a real Stop cancels the backend run; runSignal forwards the reason.
+ // assistant-ui aborts with detach=true on unmount and false for an explicit Stop; only a real
+ // Stop cancels the backend run.
if ((runSignal.reason as { detach?: boolean } | undefined)?.detach) {
return;
}
@@ -5909,12 +5559,12 @@ export function createOpenAIStreamAdapter(
if (!stopPlan.postLegacyCancel) return;
const body: Record<string, string> = { cancel_id: cancelId };
if (sandboxSessionId) body.session_id = sandboxSessionId;
- // Plain fetch, not authFetch: authFetch redirects to login on
- // 401, which would kick the user out mid-stop.
+ // Plain fetch, not authFetch: authFetch redirects to login on 401, kicking the user out mid-stop.
const token = getAuthToken();
- // Use apiUrl so the cancel POST reaches the right origin in Tauri
- // production builds (webview origin != backend at 127.0.0.1:<port>).
- // Browser/dev builds get the empty base, so the path is unchanged.
+ // apiUrl so the cancel POST reaches the right origin in Tauri builds (webview origin is not
+ // the backend); browser builds get the empty base.
+ // The webview origin is not the backend at 127.0.0.1:<port>. Browser and dev builds get the empty
+ // base, so the path is unchanged.
void fetch(apiUrl("/api/inference/cancel"), {
method: "POST",
headers: {
@@ -5927,9 +5577,7 @@ export function createOpenAIStreamAdapter(
};
// Stop handle for when this conversation is not the visible one, which cancelByThreadId
- // cannot reach. Aborting this run's own controller closes just its request, and the
- // listener above posts its cancel_id so llama-server stops decoding too. For an
- // external provider the abort is the stop, since its cancel_id is never registered.
+ // cannot reach. For an external provider the abort IS the stop: no cancel_id is registered.
runtime.registerThreadServerCancel(threadKey, serverCancel);
try {
if (runSignal.aborted) {
@@ -6035,11 +5683,9 @@ export function createOpenAIStreamAdapter(
reasoningEffort,
externalReasoningCaps.reasoningEffortLevels,
) as RequestReasoningEffort;
- // Clamp to the loaded local model's advertised levels so a stale value
- // (e.g. "max" carried over from an external model, or a level this model
- // lacks) becomes one the backend will honor instead of being dropped:
- // gpt-oss-style reasoning_effort gets low|medium|high, GLM-style
- // enable_thinking_effort gets high|max.
+ // Clamp to the loaded model's advertised levels so a stale value becomes one the backend
+ // honors: gpt-oss takes low|medium|high, GLM enable_thinking_effort high|max.
+ // gpt-oss-style reasoning_effort gets low|medium|high, GLM-style enable_thinking_effort high|max.
const localReasoningEffort = clampReasoningEffortToLevels(
reasoningEffort,
reasoningEffortLevels,
@@ -6050,14 +5696,14 @@ export function createOpenAIStreamAdapter(
forceRefreshPublicKey = false,
): Promise<OpenAIChatCompletionsRequest> => {
if (externalSelection && externalProvider) {
- // Per-thread container reuse; empty/undefined falls back to
- // container_auto. Anthropic uses anthropicCodeExecContainerId.
+ // Per-thread container reuse; empty falls back to container_auto. Anthropic uses its own key.
+ // Anthropic uses anthropicCodeExecContainerId.
let openaiCodeExecContainerId: string | null = null;
let anthropicCodeExecContainerId: string | null = null;
if (codeExecEnabledForThisTurn && resolvedThreadId) {
try {
- // Container selection can change while this run waits for model loading,
- // so read it at payload construction instead of reusing run-start metadata.
+ // Container selection can change while this run waits for model loading, so read it when the
+ // payload is built.
const thread = await getStoredChatThread(resolvedThreadId);
openaiCodeExecContainerId =
thread?.openaiCodeExecContainerId ?? null;
@@ -6067,9 +5713,8 @@ export function createOpenAIStreamAdapter(
openaiCodeExecContainerId = null;
anthropicCodeExecContainerId = null;
}
- // Pre-send container validation (OpenAI). Stale ids drop
- // silently and fall through to lazy-create; on list-call
- // failure, rely on the backend's retry path.
+ // Pre-send container validation (OpenAI): stale ids fall through to lazy-create, and a failed
+ // list call relies on the backend's retry.
let activeContainerIds: Set<string> | null = null;
if (externalProvider.providerType === "openai") {
try {
@@ -6094,8 +5739,8 @@ export function createOpenAIStreamAdapter(
openaiCodeExecContainerId = null;
}
}
- // Cross-thread inheritance: reuse the most recent container
- // from any other thread; opt-out via the picker.
+ // Cross-thread inheritance: reuse the newest container from any other thread; opt out in the
+ // picker.
if (
!openaiCodeExecContainerId &&
externalProvider.providerType === "openai"
@@ -6107,8 +5752,7 @@ export function createOpenAIStreamAdapter(
for (const t of others) {
if (t.id === resolvedThreadId) continue;
if (!t.openaiCodeExecContainerId) continue;
- // Skip ids not in active set; null the source thread so
- // the next pass doesn't re-pick a dead id.
+ // Skip ids not in the active set; null the source thread so the next pass does not re-pick it.
if (
activeContainerIds &&
!activeContainerIds.has(t.openaiCodeExecContainerId)
@@ -6128,9 +5772,8 @@ export function createOpenAIStreamAdapter(
/* fall through to lazy-create below */
}
}
- // Pre-create our own container (vs container_auto) so it shows
- // in the picker with a friendly name and the configured TTL.
- // Falls back to container_auto on failure.
+ // Pre-create our own container rather than container_auto, so it shows in the picker with a
+ // friendly name and the configured TTL; falls back on failure.
if (
!openaiCodeExecContainerId &&
externalProvider.providerType === "openai"
@@ -6146,9 +5789,7 @@ export function createOpenAIStreamAdapter(
baseUrl: externalProvider.baseUrl || null,
},
{
- // Friendly English-word name so the container is
- // human-readable in the picker (e.g. "kestrel-3f9c")
- // instead of a thread-id slug or blank default.
+ // A friendly English-word name, so the container reads as more than a thread-id slug.
name: pickFriendlyContainerName(),
ttlMinutes: ttlToUse,
},
@@ -6158,9 +5799,7 @@ export function createOpenAIStreamAdapter(
openaiCodeExecContainerId: created.id,
}).catch(() => {});
} catch {
- // Fall back to the backend's container_auto path on
- // failure — keeps the chat moving (the auto-created
- // container is unnamed); the next turn can retry.
+ // Fall back to the backend's container_auto path on failure; the next turn can retry.
openaiCodeExecContainerId = null;
}
}
@@ -6169,19 +5808,17 @@ export function createOpenAIStreamAdapter(
model: externalSelection.modelId,
messages: outboundMessages,
stream: true,
- // Never forwarded upstream (the proxy sends an explicit field list);
- // the trailing assistant turn is what asks a provider to continue.
+ // Never forwarded upstream (the proxy sends an explicit field list); the trailing assistant
+ // turn is what asks a provider to continue.
...(continuation ? { continue_final_message: true } : {}),
- // Reasoning-class models (OpenAI gpt-5.x / o3) reject
- // temperature and top_p; forward only when supported.
+ // Reasoning-class models (OpenAI gpt-5.x / o3) reject temperature and top_p; forward only when supported.
...(externalCapabilities?.temperature !== false
? { temperature: params.temperature }
: {}),
...(externalCapabilities?.topP !== false
? { top_p: params.topP }
: {}),
- // Floor at the provider's documented min (Kimi thinking
- // needs >=16k); clamp at the per-model max.
+ // Floor at the provider's documented min (Kimi thinking needs >=16k); clamp at the per-model max.
max_tokens: Math.min(
Math.max(
params.maxTokens,
@@ -6197,29 +5834,20 @@ export function createOpenAIStreamAdapter(
...(externalUsesStudioTools && resolvedThreadId
? { thread_id: resolvedThreadId }
: {}),
- // Forward only sampling knobs the provider accepts.
...(externalCapabilities?.topK ? { top_k: params.topK } : {}),
...(externalCapabilities?.presencePenalty
? { presence_penalty: params.presencePenalty }
: {}),
- // Unsloth executes the calls for any provider that advertises the
- // capability. Providers that do not keep their provider-hosted
- // tool envelope in the branch below.
- // studioLocalCodeTools, not codeToolsEnabled: a Code pill that
- // resolved to the provider's own sandbox is a hosted request and
- // belongs in the branch below. Sending this body for it would
- // attach permission_mode to a passthrough turn, which the route
- // answers with a 400.
+ // studioLocalCodeTools, not codeToolsEnabled: a Code pill that resolved to the provider's
+ // sandbox is a hosted request and belongs below, where this body would 400 on permission_mode.
...(supportsStudioToolsForThisTurn &&
(toolsEnabled ||
studioLocalCodeTools.length > 0 ||
mcpEnabledForChat ||
ragEnabled ||
projectRagEnabled ||
- // Armed research needs Studio's loop for the same reason the local body
- // does: deep_research is appended past every tool filter, but only for a
- // request that asked for the loop at all. Without it the turn proxies
- // through, the model is never offered the tool, and arming does nothing.
+ // Armed research needs Studio's loop: deep_research is appended past every tool filter, but
+ // only for a request that asked for the loop at all.
deepResearchArmed)
? {
enable_tools: true,
@@ -6229,15 +5857,9 @@ export function createOpenAIStreamAdapter(
: []),
...(toolsEnabled ? ["web_search"] : []),
...studioLocalCodeTools,
- // Hosted tools Unsloth has no local stand-in for. Their
- // pills stay lit whether or not an Unsloth tool is on, so
- // listing only the local names here would silently drop
- // Images (or Fetch) the moment Search, Code, MCP or a
- // project's automatic RAG selected this branch. Search
- // deliberately does not ride along: that is the one
- // Unsloth runs itself just above. Code rides along only
- // when it resolved to the provider's sandbox, which is
- // mutually exclusive with the local names above.
+ // Hosted tools with no local stand-in; their pills stay lit regardless, so listing only local
+ // names dropped Images/Fetch whenever another tool selected this branch. Search is excluded
+ // (Unsloth runs it above); Code rides along only when it resolved to the provider's sandbox.
...(imageGenerationEnabledForThisTurn
? ["image_generation"]
: []),
@@ -6255,18 +5877,13 @@ export function createOpenAIStreamAdapter(
runtime.toolCallTimeout >= 9999
? 9999
: runtime.toolCallTimeout * 60,
- // Self-hosted models often write a call as text rather than
- // emitting structured tool_calls, so the external loop heals
- // like the local one. Omitting this left the backend on its
- // process default, which is not what the user set in Settings.
+ // Self-hosted models often write a call as text rather than emitting structured tool_calls, so
+ // the external loop heals like the local one; omitting this left the process default.
auto_heal_tool_calls: runtime.autoHealToolCalls,
- // Keep the external server-side loop under the same user
- // setting as the local inference paths.
+ // Keep the external server-side loop under the same user setting as the local paths.
nudge_tool_calls: runtime.nudgeToolCalls,
- // This branch runs the tools here, so say so by name:
- // enabled_tools ["web_search"] is byte-identical to what an
- // older bundle sent meaning hosted search, so without this
- // flag Search silently stayed hosted.
+ // This branch runs the tools here, so say so by name: enabled_tools ["web_search"] is
+ // byte-identical to an older bundle's hosted search.
run_tools_locally: true,
...(sandboxSessionId
? { session_id: sandboxSessionId }
@@ -6323,12 +5940,12 @@ export function createOpenAIStreamAdapter(
: []),
],
}
- : // Explicit false: an omitted field falls back to the
- // server's tools-on default, which would bill provider
- // server tools.
+ // Explicit false: an omitted field falls back to the server's tools-on default, which
+ // would bill provider server tools.
+ :
{ enable_tools: false }),
- // Also on this body, not just the local one: a provider whose models run Studio
- // tools can hand off too, and omitting it here makes arming research a no-op.
+ // Also on this body: a provider whose models run Studio tools can hand off too, and omitting
+ // it makes arming research a no-op.
...(deepResearchArmed ? { deep_research_armed: true } : {}),
provider_id: externalProvider.id,
provider_type: externalBackendProviderType,
@@ -6367,8 +5984,7 @@ export function createOpenAIStreamAdapter(
isPromptCacheTtl(externalProvider.promptCacheTtl)
? { prompt_cache_ttl: externalProvider.promptCacheTtl }
: {}),
- // Anthropic fast mode (Opus 4.6 / 4.7 only); backend
- // silently drops on unsupported models as a backstop.
+ // Anthropic fast mode (Opus 4.6 / 4.7 only); the backend drops it on unsupported models as a backstop.
...(params.fastMode &&
providerSupportsFastMode(
externalProvider.providerType,
@@ -6402,8 +6018,7 @@ export function createOpenAIStreamAdapter(
...studioToolHistoryRequestFieldsAfterReplay(
survivingMessages as unknown as ToolHistoryMessage[],
),
- // Opt into the trailing usage chunk so the context-usage bar
- // and tok/s readout populate (backend gates it on include_usage).
+ // Opt into the trailing usage chunk so the context bar and tok/s populate (backend gates it).
stream_options: { include_usage: true },
...ggufCompactionRequestFields({
isGguf: activeModel?.isGguf === true,
@@ -6418,9 +6033,8 @@ export function createOpenAIStreamAdapter(
min_p: params.minP,
repetition_penalty: params.repetitionPenalty,
presence_penalty: params.presencePenalty,
- // Omitted when unset so the server keeps drawing a fresh seed, and when the
- // backend does not read one, so a pin left over from a GGUF is not sent
- // invisibly after a switch to transformers.
+ // Omitted when unset so the server draws a fresh seed, and when the backend reads none, so a
+ // GGUF pin is not sent invisibly after a switch.
...(params.seed == null || !modelReadsSamplingSeed(activeModel)
? {}
: { seed: params.seed }),
@@ -6433,9 +6047,10 @@ export function createOpenAIStreamAdapter(
...(useAdapter === undefined ? {} : { use_adapter: useAdapter }),
...(supportsReasoning
? reasoningStyle === "enable_thinking_effort"
- ? // GLM-5.2-style: on/off gate plus an effort level. Disabling
- // sends enable_thinking=false (a real disable); enabling sends
- // the chosen level (e.g. high|max).
+ // GLM-5.2-style gate plus level: disabling sends enable_thinking=false, enabling sends
+ // the chosen level.
+ // Enabling sends the chosen level, e.g. high|max.
+ ?
reasoningEnabled
? {
enable_thinking: true,
@@ -6455,14 +6070,9 @@ export function createOpenAIStreamAdapter(
...(supportsPreserveThinking
? { preserve_thinking: preserveThinking }
: {}),
- // Permission level for local tool calls is sent for every local
- // chat, not only when a tool pill is on: a process policy
- // (unsloth run --enable-tools) can open the tool loop with no pill,
- // and the backend must still see the selected gate. "auto" OMITS
- // confirm_tool_calls: an explicit true would make the backend treat
- // every auto request as needing a stream and defeat the safe-only
- // no-stream exception. "ask" sends true; off/full send false (full
- // also drops the sandbox).
+ // Sent for every local chat, since `unsloth run --enable-tools` can open the tool loop with
+ // no pill lit. "auto" OMITS confirm_tool_calls (an explicit true would force a stream and
+ // defeat the safe-only exception); "ask" sends true, off/full send false.
permission_mode: permissionMode,
...(permissionMode === "auto"
? {}
@@ -6493,9 +6103,8 @@ export function createOpenAIStreamAdapter(
: []),
],
mcp_enabled: mcpEnabledForChat,
- // Scope: thread_id = this thread's docs, kb_id = a KB,
- // project_id = the thread's project sources (auto-on whenever
- // the project has indexed sources, no Docs pill needed).
+ // Scope: thread_id = this thread's docs, kb_id = a KB, project_id = the thread's project
+ // sources (auto-on whenever the project has indexed sources).
...(ragEnabled || projectRagEnabled
? {
rag_scope: {
@@ -6536,9 +6145,7 @@ export function createOpenAIStreamAdapter(
return mins >= 9999 ? 9999 : mins * 60;
})(),
}
- : // Explicit false, not an omitted field: the server defaults tools
- // on for a request that never mentions them, so a model with no
- // tool pill lit has to say so.
+ : // Explicit false, not omission: the server defaults tools on for a request that never mentions them.
{ enable_tools: false }),
};
};
@@ -6566,8 +6173,7 @@ export function createOpenAIStreamAdapter(
requestPayload.enable_tools === true ||
(Array.isArray(clientTools) && clientTools.length > 0)
) {
- // Confirmation and browser-executed tool chains still use the
- // subscriber-owned stream in this PR.
+ // Confirmation and browser-executed tool chains still use the subscriber-owned stream.
generationDecision = "legacy";
} else {
const admission = explicitStopSignal(runSignal);
@@ -6579,14 +6185,9 @@ export function createOpenAIStreamAdapter(
generationDecision = "legacy";
} else {
generationDecision = "durable";
- // Before admission, not after. The run id is ours (`cancelId` is passed as
- // `runId`), and the run becomes visible through /active as soon as the POST
- // lands, so a visibility, pageshow, online or history-load trigger during
- // this await could otherwise start a recovery that the later claim would
- // not stop: the scheduler only tests ownership at startup.
- // Provisional: it owns recovery without marking the thread bounded.
- // The await below can outlast the checkpoint cap, and a capped thread
- // is dropped from the schedule for good.
+ // Before admission, not after: the run is visible through /active as soon as the POST lands,
+ // so a recovery started during this await would escape the later claim. Provisional, since
+ // the await can outlast the checkpoint cap and a capped thread is dropped for good.
claimLiveGenerationRun(cancelId, resolvedThreadId!, {
provisional: true,
});
@@ -6605,20 +6206,19 @@ export function createOpenAIStreamAdapter(
if (!isLegacyFallbackChatGenerationAdmissionError(error)) {
throw error;
}
- // Durable recovery does not yet replay server-side tool events.
- // Use the subscriber-owned stream for this policy-forced case.
+ // Durable recovery does not yet replay server-side tool events, so this policy-forced case
+ // uses the subscriber-owned stream.
generationDecision = "legacy";
- // Dropped here rather than in the outer finally: this stream is about
- // to become subscriber-owned, and leaving the claim would let the cap
- // fire on a stream whose only persistence is those checkpoints.
+ // Dropped here rather than in the outer finally: leaving the claim would let the cap fire on a
+ // stream whose only persistence is those checkpoints.
releaseLiveGenerationRun(cancelId);
}
if (!generationRun) {
if (generationDecision === "durable") return;
} else {
generationRunId = generationRun.id;
- // Normally the same id we claimed above; claimed again in case the server
- // ever echoes a different one. Both are released in the finally below.
+ // Normally the same id claimed above; claimed again in case the server echoes a different one.
+ // Both are released in the finally.
claimLiveGenerationRun(generationRunId, resolvedThreadId!);
generationStatus = generationRun.status;
if (generationStopRequested) {
@@ -6659,13 +6259,11 @@ export function createOpenAIStreamAdapter(
}
} catch (error) {
if (!(error instanceof ChatGenerationStalledError)) throw error;
- // End the stream rather than rethrow, keeping everything replayed so
- // far, as the recovery follower does. The checks below stay quiet
- // because a stalled run is still non-terminal.
+ // End the stream rather than rethrow, keeping everything replayed so far; a stalled run is
+ // still non-terminal, so the checks below stay quiet.
generationStalled = true;
- // Different job from the marker: this is what makes the final yield
- // carry `incomplete`, without which assistant-ui reads the partial reply
- // as finished and offers no Continue until a reload rebuilds the reason.
+ // Makes the final yield carry `incomplete`, without which assistant-ui reads the partial reply
+ // as finished and offers no Continue.
incompleteReason = "interrupted";
}
if (generationStatus === "failed") {
@@ -6682,10 +6280,8 @@ export function createOpenAIStreamAdapter(
);
}
};
- // The window is passed so a length-stop can tell a Max Tokens the user chose
- // from the backend's stand-in for "Max" (the whole context length). The two
- // need opposite advice, and "Increase Max Tokens" cannot be acted on when it
- // is already unlimited.
+ // The window is passed so a length-stop can tell a user-chosen Max Tokens from the backend's
+ // stand-in for "Max"; the two need opposite advice.
const stream =
generationDecision === "durable"
? durableStream()
@@ -6723,13 +6319,12 @@ export function createOpenAIStreamAdapter(
responseModelId = chunkModel;
}
- // Handle tool status events
const toolStatusText = (
chunk as unknown as { _toolStatus?: string }
)._toolStatus;
if (toolStatusText !== undefined) {
- // The one boundary every round has: only-disabled rounds emit
- // no card and a [DONE] upstream sends no finish_reason.
+ // The one boundary every round has: only-disabled rounds emit no card and a [DONE] upstream
+ // sends no finish_reason.
if (!toolStatusText) {
endProviderTurn();
}
@@ -6747,10 +6342,9 @@ export function createOpenAIStreamAdapter(
chunk.context_truncated,
);
const activeThreadId = useChatRuntimeStore.getState().activeThreadId;
- // What must stay silent is a fit that returned the ORIGINAL messages:
- // toasting "older turns were removed" is untrue there, and burns the
- // once-per-thread flag. Not `fits`, which is also false for a shortened
- // prompt that really was sent.
+ // What must stay silent is a fit that returned the ORIGINAL messages: "older turns were
+ // removed" would be untrue and burns the once-per-thread flag. Not `fits`, which is also
+ // false for a shortened prompt that was sent.
const reallyCompacted = promptWasShortened(chunk.context_truncated);
if (
reallyCompacted &&
@@ -6759,8 +6353,7 @@ export function createOpenAIStreamAdapter(
!rollingContextNoticeThreads.has(resolvedThreadId)
) {
rollingContextNoticeThreads.add(resolvedThreadId);
- // Once per thread per page load; the persistent record is the notice on
- // the assistant turn that compacted.
+ // Once per thread per page load; the durable record is the notice on the turn that compacted.
const archived = contextTruncation?.archived_messages ?? 0;
toast.info("This conversation was compacted", {
description: archived
@@ -6775,9 +6368,7 @@ export function createOpenAIStreamAdapter(
continue;
}
- // Local GGUF sends server-timed reasoning duration. Guard the type
- // so a malformed or proxied chunk (string/null/NaN duration) can
- // never turn the label into NaN.
+ // Guard the type so a malformed or proxied chunk (string/null/NaN duration) cannot make NaN.
const reasoningMs = (
chunk as { _reasoningDurationMs?: number } | null | undefined
)?._reasoningDurationMs;
@@ -6785,9 +6376,8 @@ export function createOpenAIStreamAdapter(
continue;
}
- // Diffusion frame: a transient canvas snapshot. Route it to the transient
- // store (the in-bubble renderer reads it) and skip it; it has no assistant
- // text, so it never enters the transcript or the counters below.
+ // Diffusion frame: a transient canvas snapshot routed to the transient store and skipped; it
+ // has no assistant text, so it never enters the transcript.
const diffusionFrame = (
chunk as unknown as {
_diffusionFrame?: {
@@ -6799,8 +6389,7 @@ export function createOpenAIStreamAdapter(
}
)._diffusionFrame;
if (diffusionFrame !== undefined) {
- // Keyed by thread so a background run's frames stay out of the visible chat
- // instead of overwriting the frame it is painting.
+ // Keyed by thread so a background run's frames stay out of the visible chat.
runtime.setActiveDiffusionCanvas(liveThreadKey(serverCancel), {
block: diffusionFrame.block ?? 0,
step: diffusionFrame.step ?? 0,
@@ -6810,23 +6399,18 @@ export function createOpenAIStreamAdapter(
continue;
}
- // Emit tool-call content parts for assistant-ui.
- // tool_start: add a part (renders "running").
- // tool_end: set result on the part (transitions to "complete").
+ // tool_start adds a part (renders "running"), tool_end sets its result (transitions to "complete").
const toolEvent = (
chunk as unknown as { _toolEvent?: Record<string, unknown> }
)._toolEvent;
if (toolEvent !== undefined) {
- // Unsloth's own tool events end the turn that asked for them; finish_reason
- // alone is not enough. A hosted tool runs INSIDE the turn and rides a whole
- // chunk, where Unsloth's are bare {"type": "tool_start"} frames.
+ // Unsloth's own tool events end the turn that asked for them; finish_reason alone is not
+ // enough, since a hosted tool runs INSIDE the turn and rides a whole chunk.
if (!chunk.choices) {
endProviderTurn();
}
- // Deep Research is an ordinary tool to every loop that runs it, so the handoff
- // is read off the events they all publish rather than a bespoke frame. An
- // ungated pair is not rendered: the research card is the reply, a tool pill
- // would just precede it saying the same thing.
+ // Deep Research is an ordinary tool to every loop that runs it, so the handoff is read off the
+ // shared events. An ungated pair is not rendered: the research card is the reply.
if (
toolEvent.tool_name === "deep_research" &&
readDeepResearchToolEvent(deepResearchHandoff, toolEvent)
@@ -6851,8 +6435,7 @@ export function createOpenAIStreamAdapter(
continue;
}
if (toolEvent.type === "document_citations") {
- // Convert citations_delta footnotes into Sources-panel
- // entries matching the inline [N] markers.
+ // Convert citations_delta footnotes into Sources entries matching the inline [N] markers.
const cits = toolEvent.citations;
if (Array.isArray(cits)) {
cits.forEach((entry, idx) => {
@@ -6884,15 +6467,13 @@ export function createOpenAIStreamAdapter(
continue;
}
if (toolEvent.type === "anthropic_refusal") {
- // Latch the backend refusal signal so final message
- // metadata can drive the prune.
+ // Latch the backend refusal signal so final metadata can drive the prune.
anthropicRefusalSeen = true;
continue;
}
if (toolEvent.type === "tool_output") {
- // Incremental stdout from a running tool: append to the live
- // store so the card renders it while the spinner runs. Final
- // result arrives via tool_end.
+ // Incremental stdout from a running tool: append to the live store so the card renders it.
+ // The final result arrives via tool_end.
const backendToolCallId =
(toolEvent.tool_call_id as string) || "";
const liveId = resolveToolPartId(backendToolCallId);
@@ -6908,10 +6489,8 @@ export function createOpenAIStreamAdapter(
continue;
}
if (toolEvent.type === "tool_args") {
- // The model is still WRITING this call's arguments: accumulate
- // the raw stream and feed a partial parse into the part's args
- // so the card shows the code live. tool_start later replaces
- // args with the authoritative parse.
+ // The model is still WRITING this call's arguments: accumulate the raw stream and feed a
+ // partial parse so the card shows the code live; tool_start replaces it authoritatively.
const backendToolCallId =
(toolEvent.tool_call_id as string) || "";
const liveId = resolveToolPartId(backendToolCallId);
@@ -6935,9 +6514,8 @@ export function createOpenAIStreamAdapter(
args: partial.args as ToolCallMessagePart["args"],
argsText: partial.argsText,
};
- // A preview: it repeats per argument delta and
- // tool_start replaces it. Tool events carry state, so
- // they stay ungated.
+ // A preview: it repeats per argument delta and tool_start replaces it. Tool events carry
+ // state, so they stay ungated.
if (canPublish(streamedChars)) {
yield {
content: liveAssistantContent(),
@@ -6965,8 +6543,8 @@ export function createOpenAIStreamAdapter(
const approvalId = (toolEvent.approval_id as string) || "";
const awaitingConfirmation =
toolEvent.awaiting_confirmation === true;
- // Reuse a provisional card's part id, else the confirmation-scoped id
- // opens a second card and the first spins "Running" forever.
+ // Reuse a provisional card's part id, else the confirmation-scoped id opens a second card and
+ // the first spins forever.
const openPartId = backendToolCallId
? toolPartIdByBackendId.get(backendToolCallId)
: undefined;
@@ -6982,8 +6560,8 @@ export function createOpenAIStreamAdapter(
if (awaitingConfirmation && backendToolCallId) {
toolConfirmationIdsByBackendId.set(backendToolCallId, id);
}
- // "call_0" restarts every response: drop stale live/preserved
- // output under this key, else the card shows the previous call's.
+ // "call_0" restarts every response: drop stale live/preserved output under this key, else the
+ // card shows the previous call's.
const staleKey = scopedToolOutputKey(id);
useChatRuntimeStore.getState().clearToolLiveOutput(staleKey);
useChatRuntimeStore.getState().clearToolFullOutput(staleKey);
@@ -7040,11 +6618,8 @@ export function createOpenAIStreamAdapter(
toolPartIdByBackendId.delete(backendToolCallId);
}
useChatRuntimeStore.getState().clearToolConfirmation(id);
- // The result replaces the live output, but if the stream
- // captured MORE than the truncated result, preserve it so the
- // finished card keeps everything. Uses the shared predicate,
- // not a length compare (footer / "Exit code N:" / __IMAGES__
- // tail can make the result longer by byte).
+ // If the stream captured MORE than the truncated result, preserve it. Uses the shared
+ // predicate, not a length compare: a footer or __IMAGES__ tail can make the result longer.
const liveKey = scopedToolOutputKey(id);
const liveOutput =
useChatRuntimeStore.getState().toolLiveOutput[liveKey] ??
@@ -7068,9 +6643,8 @@ export function createOpenAIStreamAdapter(
);
if (idx !== -1) {
const rawEvent = (toolEvent.result as string) ?? "";
- // Pulled out first, ahead of __IMAGES__, so the image
- // slice below is unchanged. Only from the tools that emit
- // it: elsewhere that line is content, not an envelope.
+ // Pulled out first, ahead of __IMAGES__, so the image slice below is unchanged. Only from the
+ // tools that emit it: elsewhere that line is content.
const { text: rawResult, files: createdFiles } =
SANDBOX_FILE_TOOLS.has(toolCallParts[idx].toolName ?? "")
? extractCreatedFiles(rawEvent)
@@ -7103,9 +6677,8 @@ export function createOpenAIStreamAdapter(
prompt?: string;
};
const imageB64 = toolEvent.image_b64 as string | undefined;
- // A valid MCP image envelope wins; an invalid marker falls
- // through so a sandbox __IMAGES__ suffix still renders and
- // legit text round-trips unchanged.
+ // A valid MCP image envelope wins; an invalid marker falls through so a sandbox __IMAGES__
+ // suffix still renders.
let mcpImages: McpImageToolResult | null = null;
if (mcpImgIdx !== -1) {
try {
@@ -7127,8 +6700,7 @@ export function createOpenAIStreamAdapter(
typeof imageB64 === "string" &&
imageB64
) {
- // Backend keeps base64 on separate image_b64 /
- // image_mime fields so logs stay small; repackage here.
+ // The backend keeps base64 on separate image_b64 / image_mime fields to keep logs small.
parsedResult = {
image_b64: imageB64,
image_mime:
@@ -7143,8 +6715,7 @@ export function createOpenAIStreamAdapter(
parsedResult = mcpImages;
} else if (imgIdx !== -1) {
const text = rawResult.slice(0, imgIdx);
- // Fall back to "_default" to match the backend sandbox
- // dir used when no session_id (see tools.py _get_workdir).
+ // Fall back to "_default", the backend sandbox dir used when there is no session_id.
const sessionId = sandboxSessionId || "_default";
try {
const images = JSON.parse(
@@ -7163,14 +6734,9 @@ export function createOpenAIStreamAdapter(
createdFiles.length > 0 ||
SANDBOX_FILE_TOOLS.has(toolCallParts[idx].toolName ?? "")
) {
- // Structured with files, for the download card, and with
- // neither, because the session is the only record of WHERE
- // this call ran: _created_file_sentinels emits nothing
- // when a concurrent call shared the directory, so a run
- // that wrote files can arrive bare and a moved chat would
- // then name a folder from its current scope. Downstream is
- // unaffected: both toolResultModelText and the outbound
- // translator unwrap a sandbox wrapper to this same .text.
+ // Structured even with neither files nor images, because the session is the only record of
+ // WHERE this call ran: _created_file_sentinels emits nothing when a concurrent call shared
+ // the directory, so a moved chat would name a folder from its current scope.
parsedResult = {
text: rawResult,
images: [],
@@ -7182,7 +6748,6 @@ export function createOpenAIStreamAdapter(
} else {
parsedResult = rawResult;
}
- // Merge tool_end args first, then Gemini native_part.
const nextArgs =
toolEvent.arguments &&
typeof toolEvent.arguments === "object"
@@ -7221,9 +6786,8 @@ export function createOpenAIStreamAdapter(
string,
unknown
>;
- // Extract part entries from parts:[...] or legacy
- // single-object native_part. Legacy thoughtSignature
- // always belongs on executableCode.
+ // Extract part entries from parts:[...] or a legacy single-object native_part; a legacy
+ // thoughtSignature always belongs on executableCode.
const collectParts = (
native: Record<string, unknown>,
): Record<string, unknown>[] => {
@@ -7302,8 +6866,7 @@ export function createOpenAIStreamAdapter(
continue;
}
- // OpenAI usage may arrive either in an empty trailing chunk or on
- // the terminal Codex chunk that also carries reasoning metadata.
+ // OpenAI usage may arrive in an empty trailing chunk or on the terminal Codex chunk.
if (chunk.usage) {
serverMetadata = {
usage: chunk.usage,
@@ -7315,15 +6878,13 @@ export function createOpenAIStreamAdapter(
}
totalChunks += 1;
- // Latched, not read off the last chunk: a provider can send the
- // terminal reason ahead of its usage chunk.
+ // Latched, not read off the last chunk: a provider can send the terminal reason ahead of its usage chunk.
if (chunk.choices?.[0]?.finish_reason === "length") {
incompleteReason = "length";
} else if (chunk.choices?.[0]?.finish_reason) {
incompleteReason = null;
}
- // Latch the chunk's `model` field so the openrouter/free chip
- // shows the chosen underlying model.
+ // Latch the chunk's `model` field so the openrouter/free chip shows the underlying model.
if (
isExternalRequest &&
externalProvider?.providerType === "openrouter" &&
@@ -7345,15 +6906,13 @@ export function createOpenAIStreamAdapter(
// Normalize structured delta.content (mistral magistral).
const { text: delta, structuredReasoningContinues } =
extractDeltaText(rawDelta);
- // Latest Gemini text-part thoughtSignature for next-turn replay.
const deltaExtraContent = (
chunk.choices?.[0]?.delta as
| { extra_content?: unknown }
| undefined
)?.extra_content;
- // Replay state reaches the message only through a yield, so a
- // Stop while the gate holds one persists a turn that cannot
- // replay. Pace previews, never state.
+ // Replay state reaches the message only through a yield, so a Stop while the gate holds one
+ // persists a turn that cannot replay. Pace previews, never state.
let replayStateChanged = false;
if (deltaExtraContent && typeof deltaExtraContent === "object") {
const extraRecord = deltaExtraContent as Record<
@@ -7386,15 +6945,13 @@ export function createOpenAIStreamAdapter(
if (chunk.choices?.[0]?.finish_reason) {
codexRoundToolCallIds = [];
}
- // Kimi / DeepSeek stream thinking via delta.reasoning_content;
- // wrap inline as <think>...</think> for parseAssistantContent.
+ // Kimi / DeepSeek stream thinking via delta.reasoning_content; wrap it inline as <think>.
const rawReasoning = (
chunk.choices?.[0]?.delta as
| { reasoning_content?: unknown }
| undefined
)?.reasoning_content;
- // OpenRouter ships reasoning as delta.reasoning_details[]
- // regardless of provider; merge into the same wrap path.
+ // OpenRouter ships reasoning as delta.reasoning_details[] whatever the provider; same wrap.
const rawReasoningDetails = (
chunk.choices?.[0]?.delta as
| { reasoning_details?: unknown }
@@ -7412,9 +6969,8 @@ export function createOpenAIStreamAdapter(
const reasoning =
(typeof rawReasoning === "string" ? rawReasoning : "") +
reasoningFromDetails;
- // OpenAI delta.tool_calls: streams fragments by index;
- // accumulate into one part. extra_content carries Gemini 3
- // thoughtSignature for replay.
+ // OpenAI delta.tool_calls streams fragments by index; accumulate into one part. extra_content
+ // carries the Gemini 3 thoughtSignature.
const rawDeltaToolCalls = (
chunk.choices?.[0]?.delta as
| { tool_calls?: unknown }
@@ -7425,8 +6981,7 @@ export function createOpenAIStreamAdapter(
rawDeltaToolCalls.length > 0
) {
closeReasoningContent();
- // Extending a call's arguments is a preview; introducing one
- // is state, so it always publishes.
+ // Extending a call's arguments is a preview; introducing one is state, so it always publishes.
let addedToolCall = false;
for (const tc of rawDeltaToolCalls) {
if (!tc || typeof tc !== "object") continue;
@@ -7439,26 +6994,21 @@ export function createOpenAIStreamAdapter(
const idx =
typeof call.index === "number" ? call.index : undefined;
const stableId = call.id;
- // The chunk is cast, not validated, and llama-server has
- // shipped `arguments` as a decoded object.
+ // The chunk is cast, not validated, and llama-server has shipped `arguments` as a decoded object.
const deltaArgs =
typeof call.function?.arguments === "string"
? call.function.arguments
: "";
- // Unsloth's local Codex loop follows the OpenAI tool-call delta with
- // tool_start/tool_end events. Resolve the backend id now so all three
- // event shapes update one run-unique card instead of leaving the raw
- // provisional card beside a second execution card.
- // Before resolving: a provider claiming a minted spelling
- // would resolve onto that card and merge the two calls. The
- // backend's reserve-then-mint order lands on the same pair.
+ // Unsloth's local Codex loop follows the OpenAI tool-call delta with tool_start/tool_end, so
+ // resolve the backend id now to keep all three shapes on one card. Before resolving, since
+ // a provider claiming a minted spelling would merge two calls.
if (
stableId &&
!providerSentToolCallIds.has(stableId) &&
toolCallParts.some((part) => part.toolCallId === stableId)
) {
- // Renumber first: a minted card holds the claimed spelling until this runs,
- // and marking it provider-sent earlier would exempt it from the move.
+ // Renumber first: a minted card holds the claimed spelling until this runs, and marking it
+ // provider-sent earlier would exempt it from the move.
renumberMintedCards(stableId);
providerSentToolCallIds.add(stableId);
reservedToolCallIds.add(stableId);
@@ -7466,16 +7016,14 @@ export function createOpenAIStreamAdapter(
const stablePartId = stableId
? resolveToolPartId(stableId)
: undefined;
- // Reserved before any card id is minted, so a provider that
- // happens to spell an id `tool_call_0` keeps it to itself.
+ // Reserved before any card id is minted, so a provider spelling an id `tool_call_0` keeps it.
if (stableId) {
reservedToolCallIds.add(stableId);
providerSentToolCallIds.add(stableId);
}
if (stablePartId) providerSentToolCallIds.add(stablePartId);
- // match by resolved id when the fragment carries one, else by
- // index slot; streams that send neither get a minted
- // tool_call_<n> id.
+ // Match by resolved id when the fragment carries one, else by index slot; streams with
+ // neither get a minted tool_call_<n>.
const existingIndex = findStreamedToolCallPartIndex(
toolCallParts,
stablePartId,
@@ -7485,8 +7033,8 @@ export function createOpenAIStreamAdapter(
existingIndex === -1
? undefined
: toolCallParts[existingIndex];
- // A closed object takes no more content, so a name or arguments reaching it
- // open the next call. Splitting the text alone is too late.
+ // A closed object takes no more content, so a name or arguments reaching it open the next
+ // call; splitting the text alone is too late.
const slotIsClosed = (() => {
if (!matched?.argsText) return false;
const held = scanArgsText(
@@ -7495,44 +7043,37 @@ export function createOpenAIStreamAdapter(
);
return held.complete.length > 0 && !held.tail;
})();
- // A fragment repeating the id this part holds continues it
- // however complete the arguments look.
+ // A fragment repeating the id this part holds continues it however complete the arguments look.
const namesThisCall =
!!stablePartId && matched?.toolCallId === stablePartId;
- // A next call opens with its own "{"; cutting on anything
- // else runs the tool twice on a stray scalar suffix.
+ // A next call opens with its own "{"; cutting on anything else runs the tool twice on a stray
+ // scalar suffix.
const bringsArgs = deltaArgs.trim().startsWith("{");
const closedSlot = slotIsClosed && !namesThisCall;
- // An id naming a DIFFERENT call opens the next even before
- // its arguments arrive; alone it is that call's, stamped late.
+ // An id naming a DIFFERENT call opens the next even before its arguments arrive.
const idNamesAnotherCall =
!!stablePartId &&
!!call.function?.name &&
!!matched?.toolName &&
- // Not a prefix test: a catalog holds both "web" and
- // "web_search".
+ // Not a prefix test: a catalog holds both "web" and "web_search".
call.function.name !== matched.toolName;
- // A snapshot provider repeats the finished call verbatim
- // once it has an id. Exact repeats only, so parallel calls
- // differing anywhere still open separately.
+ // A snapshot provider repeats the finished call verbatim once it has an id; exact repeats
+ // only, so parallel calls still open separately.
const resendsThisCall =
!!stablePartId &&
!!matched &&
!matched._has_stable_id &&
call.function?.name === matched.toolName &&
deltaArgs === matched.argsText;
- // A name at a closed slot announces the next call: names
- // grow before the arguments, so nothing is left to extend. A
- // shared prefix is no proof ("web" after "web_search" is a
- // second call), but the SAME name is that call's, resent, and
- // llama-server and vLLM both resend it.
+ // A name at a closed slot announces the next call. A shared prefix is no proof ("web" after
+ // "web_search" is a second call), but the SAME name is that call resent, as llama-server
+ // and vLLM both do.
const namesNextCall =
!!call.function?.name &&
!!matched?.toolName &&
call.function.name !== matched.toolName;
- // An announcement has no object to close, so the rule above
- // cannot reach it. A different name bringing an object is the
- // next call; gluing gave "A_longB", which matches no tool.
+ // An announcement has no object to close, so the rule above cannot reach it; a different name
+ // bringing an object is the next call, and gluing gave "A_longB".
const announcesOverAnnouncement =
((matched as PositionedToolCallPart | undefined)
?._announced_only === true ||
@@ -7546,11 +7087,8 @@ export function createOpenAIStreamAdapter(
if (announcesOverAnnouncement && matched) {
(matched as PositionedToolCallPart)._superseded = true;
}
- // A name repeating the one a closed card holds says nothing
- // new about that call, so metadata riding it belongs to
- // whichever call the next object opens; merged now it
- // overwrites the signature the closed call arrived with and
- // leaves the next call unsigned.
+ // A name repeating a closed card's says nothing new about that call, so metadata riding it
+ // belongs to whichever call the next object opens; merging now overwrites its signature.
const extraIsAmbiguous =
closedSlot &&
!!call.function?.name &&
@@ -7575,42 +7113,35 @@ export function createOpenAIStreamAdapter(
argsFragment.length + (call.function?.name?.length ?? 0);
if (existing) {
const prevName = existing.toolName ?? "";
- // Two dialects, and either alone breaks the other:
- // llama-server resends the whole name as it grows, OpenAI
- // streams it in fragments. Same rule as the backend.
+ // Two dialects, and either alone breaks the other: llama-server resends the whole name as it
+ // grows, OpenAI streams it in fragments. Same rule as the backend.
const nameFragment = call.function?.name ?? "";
- // Never once the object has closed AND a name is set: that
- // would put one tool's arguments under another's name.
- // Naming a card that has none is not a rename, and servers
- // do send the arguments first.
+ // Never once the object has closed AND a name is set: that would put one tool's arguments
+ // under another's name. Naming a card that has none is not a rename.
const nextName =
!nameFragment || (closedSlot && prevName)
? prevName
: nameFragment.startsWith(prevName)
? nameFragment
: prevName + nameFragment;
- // A snapshot repeated to carry the id adds nothing;
- // appending gives `{"a":1}{"a":1}` and splits it in two.
+ // A snapshot repeated to carry the id adds nothing; appending gives `{"a":1}{"a":1}`.
const merged = resendsThisCall
? (existing.argsText ?? "")
: (existing.argsText ?? "") + argsFragment;
- // A slot holding two objects is holding two calls: this is
- // how vLLM's id-less deltas glue `{"url":"a"}` and
- // `{"url":"b"}` into one unparsable string (issue #9807).
- // Cut on the object boundary, since the same tool twice has
- // no name to cut on; a delta with an id addresses its own.
+ // A slot holding two objects is holding two calls: this is how vLLM's id-less deltas glue two
+ // argument objects into one unparsable string (#9807). Cut on the object boundary, since
+ // the same tool twice has no name to cut on.
const split = stablePartId
? { complete: [], tail: "" }
: scanArgsText(existing.toolCallId, merged);
- // Whether the last segment is still open decides who may
- // go on writing to it.
+ // Whether the last segment is still open decides who may go on writing to it.
const splitTailIsOpen = split.tail.length > 0;
const segments = splitTailIsOpen
? [...split.complete, split.tail]
: split.complete;
const isSplit = segments.length > 1;
- // The slot keeps the object it opened with, under the name
- // and id it had; the rest are calls this delta introduced.
+ // The slot keeps the object it opened with, under the name and id it had; the rest are calls
+ // this delta introduced.
const slotText = isSplit ? segments[0] : merged;
let parsedArgs: ToolCallMessagePart["args"] =
existing.args ?? {};
@@ -7627,9 +7158,8 @@ export function createOpenAIStreamAdapter(
}
const prevExtra = (existing as PositionedToolCallPart)
.extra_content;
- // Merged, not replaced: a signature announced with the
- // name and one arriving with the arguments are different
- // fields of one call.
+ // Merged, not replaced: a signature announced with the name and one arriving with the
+ // arguments are different fields of one call.
if (extraIsAmbiguous && isPlainRecord(call.extra_content)) {
pendingExtraByPartId.set(existing.toolCallId, {
...(pendingExtraByPartId.get(existing.toolCallId) ?? {}),
@@ -7645,14 +7175,13 @@ export function createOpenAIStreamAdapter(
incomingExtra !== undefined &&
JSON.stringify(incomingExtra) !== JSON.stringify(prevExtra)
) {
- // Gemini puts the thought signature on the call, and
- // the next turn is rejected without it.
+ // Gemini puts the thought signature on the call, and the next turn is rejected without it.
replayStateChanged = true;
}
const updated: PositionedToolCallPart = {
...(existing as PositionedToolCallPart),
- // a late id claims the slot its id-less opening fragment
- // created, so tool_start and tool_end find the same card.
+ // A late id claims the slot its id-less opening fragment created, so tool_start and tool_end
+ // find the same card.
...(stablePartId
? { toolCallId: stablePartId, _has_stable_id: true }
: {}),
@@ -7665,8 +7194,7 @@ export function createOpenAIStreamAdapter(
? { extra_content: prevExtra }
: {}),
...(idx !== undefined ? { _delta_index: idx } : {}),
- // Any arguments field, "" included, means the call has
- // started, so the card survives the boundary sweep.
+ // Any arguments field, "" included, means the call started, so the card survives the sweep.
_announced_only:
(existing as PositionedToolCallPart)._announced_only ===
true && call.function?.arguments === undefined,
@@ -7676,33 +7204,26 @@ export function createOpenAIStreamAdapter(
true && !slotText,
};
toolCallParts[existingIndex] = updated;
- // The card answers to its late id from here on, so what was
- // keyed on the provisional one moves and the id itself goes
- // back: the backend never reserved it for a call the
- // provider went on to name, and holding it makes the next
- // mint skip the number the backend hands out.
+ // The card answers to its late id from here on, so keys move and the provisional id goes
+ // back: the backend never reserved it, and holding it makes the next mint skip a number.
if (stablePartId && stablePartId !== existing.toolCallId) {
renameStreamedCard(existing.toolCallId, stablePartId);
}
if (isSplit) {
- // The slot keeps one segment, not the whole string, so
- // the resumable scan no longer describes it.
+ // The slot keeps one segment, not the whole string, so the resumable scan no longer describes it.
boundaryScans.delete(existing.toolCallId);
boundaryScans.delete(updated.toolCallId);
- // Appended, not inserted beside the slot, so a call
- // opened third reads third whichever index it reused.
- // This delta's own metadata, not the merge: the merged
- // fields belong to the call the slot was holding, and
- // Gemini validates a signature against the functionCall
- // part it was returned on.
+ // Appended, not inserted beside the slot, so a call opened third reads third whichever index
+ // it reused. This delta's own metadata, not the merge: Gemini validates a signature against
+ // the functionCall part it was returned on.
const born = bornSplitToolCalls(
segments.slice(1),
nextName,
idx,
call.extra_content,
);
- // The last is the object still being written, if one is:
- // kept for later fragments, dropped if it never closes.
+ // The last is the object still being written, if any: kept for later fragments, dropped if it
+ // never closes.
if (splitTailIsOpen && born.length > 0) {
openTailIds.add(born[born.length - 1].toolCallId);
}
@@ -7719,8 +7240,7 @@ export function createOpenAIStreamAdapter(
if (!codexRoundToolCallIds.includes(callId)) {
codexRoundToolCallIds.push(callId);
}
- // vLLM bundles several calls into one delta when the model
- // writes them in one pass. Same boundary as above.
+ // vLLM bundles several calls into one delta when the model writes them in one pass.
const freshSplit = stablePartId
? { complete: [], tail: "" }
: splitTopLevelJsonObjects(argsFragment);
@@ -7731,13 +7251,10 @@ export function createOpenAIStreamAdapter(
const freshIsSplit = freshSegments.length > 1;
const nameFragment = call.function?.name ?? "";
const heldName = matched?.toolName ?? "";
- // A second call to the same tool can arrive with no name,
- // the first delta having given it; blank names nothing.
+ // A second call to the same tool can arrive with no name, the first delta having given it.
const freshName = nameFragment || heldName;
- // Across several calls the metadata belongs to the one
- // this delta closes, the last. Same as the backend.
- // The object proved the repeated name announced this call,
- // so what was waiting on the card it reached is this one's.
+ // Across several calls the metadata belongs to the one this delta closes, the last, as in the
+ // backend; the object proved the repeated name announced this call.
const waiting = matched
? pendingExtraByPartId.get(matched.toolCallId)
: undefined;
@@ -7785,13 +7302,11 @@ export function createOpenAIStreamAdapter(
...(call.function?.arguments === undefined && freshName
? { _announced_only: true }
: {}),
- // A fork's guess, or a slot the provider opened itself.
- // Only the provider's own announcement runs unfilled.
+ // A fork's guess, or a slot the provider opened itself: only the provider's own announcement
+ // runs unfilled.
...(matched ? { _from_fork: true } : {}),
- // A name extending the one this fork left behind is most
- // likely it, resent. It still opens a card (the prefix is
- // no proof) but gives way rather than gluing
- // "alpha_longbeta".
+ // A name extending the one this fork left behind is most likely it, resent; it still opens a
+ // card (the prefix is no proof) but gives way rather than gluing them.
...(freshName &&
heldName &&
freshName !== heldName &&
@@ -7807,8 +7322,7 @@ export function createOpenAIStreamAdapter(
bornExtra,
)
: [];
- // As above: the fork whose object is still open is held
- // only for the rest of the turn.
+ // As above: the fork whose object is still open is held only for the rest of the turn.
if (freshTailIsOpen && born.length > 0) {
openTailIds.add(born[born.length - 1].toolCallId);
}
@@ -7816,11 +7330,10 @@ export function createOpenAIStreamAdapter(
addedToolCall = true;
}
}
- // After this chunk's deltas: a provider can put finish_reason
- // on the same chunk as the turn's last name-only delta.
+ // After this chunk's deltas: a provider can put finish_reason on the same chunk as the turn's
+ // last name-only delta.
if (chunk.choices?.[0]?.finish_reason) {
- // Ending the turn drops cards, so the publish below has to
- // see it rather than wait for the pacing gate.
+ // Ending the turn drops cards, so the publish below must see it rather than wait for the pacing gate.
replayStateChanged ||= endProviderTurn();
}
if (
@@ -7845,9 +7358,8 @@ export function createOpenAIStreamAdapter(
if (chunk.choices?.[0]?.finish_reason) {
replayStateChanged ||= endProviderTurn();
}
- // extra_content can arrive with no content at all: a Gemini
- // thoughtSignature fragment, or the codex reasoning ledger on a
- // terminal delta. The skip below would drop both.
+ // extra_content can arrive with no content at all (a Gemini thoughtSignature fragment, or the
+ // codex reasoning ledger); the skip below would drop both.
if (replayStateChanged && !delta && !reasoning) {
const replayContent = liveAssistantContent();
if (replayContent.length > 0) {
@@ -7894,10 +7406,8 @@ export function createOpenAIStreamAdapter(
}
streamedChars += reasoning.length + delta.length;
producedReplyText = true;
- // The trailing ${...} strip used to run here, once per arrival.
- // It now runs once, on the finished reply, below the loop. See
- // the comment there. Nothing on this path reads the buffer any
- // more, so no arrival can flatten it.
+ // The trailing ${...} strip runs once on the finished reply, below the loop; nothing on this
+ // path reads the buffer, so no arrival can flatten it.
const textEndsInsideThink = thinkTags.endsInsideThink();
const assistantContent = liveAssistantContent();
@@ -7912,10 +7422,8 @@ export function createOpenAIStreamAdapter(
);
}
if (parsedReasoningGroupCount > 0) {
- // Providers that close every reasoning block atomically
- // (structured parts wrapped as <think>..</think>) end the group
- // on each chunk. Reopen while the reasoning text is still
- // growing so the timer spans the whole pass.
+ // Providers that close every reasoning block atomically end the group on each chunk; reopen
+ // while the reasoning text is still growing so the timer spans the whole pass.
reasoningDurationTracker.resumeGroup(
parsedReasoningGroupCount - 1,
lastReasoningGroupTextLength(assistantContent),
@@ -7930,14 +7438,9 @@ export function createOpenAIStreamAdapter(
reasoningDurationTracker.finishGroup();
}
- // Everything above runs on every arrival; only the publish is
- // coalesced. The cost being removed is downstream of the yield
- // (assistant-ui, React, markdown, paint), not the rebuild, which
- // is tens of milliseconds across a whole reply.
- //
- // A chunk with nothing new is skipped rather than paced: the gate
- // would spend this cycle on an identical publish and hold the
- // next real one.
+ // Only the publish is coalesced: the cost removed is downstream of the yield (assistant-ui,
+ // React, markdown, paint), not the rebuild. A chunk with nothing new is skipped rather than
+ // paced, or the gate would spend its cycle on an identical publish.
if (
!replayStateChanged &&
(assistantContent.length === 0 ||
@@ -7973,10 +7476,8 @@ export function createOpenAIStreamAdapter(
retriedWithRefreshedKey = true;
continue;
}
- // The tool ran, so the run it announced is owed whatever the acknowledgement
- // did next: a small model that stalls on the follow-up sentence, a timeout, a
- // dropped connection. Its card replaces this reply anyway, so nothing the
- // failed acknowledgement would have said is lost. Not after Stop.
+ // The tool ran, so the run it announced is owed whatever the acknowledgement did next; its
+ // card replaces this reply anyway. Not after Stop.
if (deepResearchHandoff.question !== null && !runSignal.aborted) {
try {
yield* startDeepResearch(deepResearchHandoff.question, runSignal);
@@ -7993,8 +7494,8 @@ export function createOpenAIStreamAdapter(
throw streamError;
}
}
- // The model asked for Deep Research, so the run takes over from here and its card
- // replaces this reply. Not after Stop: the user ended the turn before it got there.
+ // The model asked for Deep Research, so its card replaces this reply. Not after Stop: the
+ // user ended the turn first.
if (deepResearchHandoff.question !== null && !runSignal.aborted) {
try {
yield* startDeepResearch(deepResearchHandoff.question, runSignal);
@@ -8007,32 +7508,11 @@ export function createOpenAIStreamAdapter(
}
return;
}
- // Strip a trailing ${...} template-literal fragment from external
- // streams (mistral magistral occasionally emits one at the end of an
- // otherwise complete answer).
- //
- // Once, on the finished reply. "Ends with ${...}" is a property of the
- // completed answer, and running the strip on every arrival tested it
- // against every prefix of that answer instead: the one arrival whose
- // buffer happened to end at `...${name}` was cut, and reassigning the
- // result made the cut permanent, so "return `Hi, ${name}!`" arrived as
- // "return `Hi,!`". Any reply containing a template literal lost text.
- // See #9098.
- //
- // Only where the stream ran to completion. An abort leaves more text
- // still to come, so its tail is a prefix again and stripping it would
- // be the same bug; that path keeps the buffer whole and this runs on
- // the resumed reply instead. `producedReplyText` is the same case one
- // step in: a continuation that finishes without a text or reasoning
- // delta, having emitted only a tool call, leaves the buffer holding
- // nothing but the seeded partial, and a partial is a prefix too.
- //
- // The watch still gates the scan, and now saves the whole reply from
- // being flattened rather than one arrival's worth: a reply that does
- // not end in a brace is rejected without the buffer being read at all.
- // Before the <think> close below, so a fragment at the end of an
- // unterminated reasoning block is still the end of the reply when it
- // is tested.
+ // Strip a trailing ${...} template-literal fragment from external streams (mistral magistral
+ // emits one). Once, on the finished reply: running it per arrival tested every prefix, so
+ // "return `Hi, ${name}!`" arrived as "return `Hi,!`" (#9098). Completed streams only, since an
+ // abort leaves a prefix again and `producedReplyText` is that case one step in. Before the
+ // <think> close, so a fragment inside reasoning still counts as the end.
if (
isExternalRequest &&
producedReplyText &&
@@ -8041,21 +7521,17 @@ export function createOpenAIStreamAdapter(
const stripped = stripTrailingTemplatePlaceholder(cumulativeText);
if (stripped.length !== cumulativeText.length) {
cumulativeText = stripped;
- // A suffix went away, so both trackers have to be told; the parse
- // notices by itself, from the length.
+ // A suffix went away, so both trackers have to be told; the parse notices by itself, from the length.
thinkTags.retract(cumulativeText);
placeholderWatch.retract(cumulativeText);
}
}
- // If the stream ended while we were still inside a
- // delta.reasoning_content block (Kimi / DeepSeek path), close
- // the open <think> tag so the reasoning panel parses cleanly.
+ // If the stream ended inside a delta.reasoning_content block, close the open <think> tag.
closeReasoningContent();
settleFirstTokenOk();
- // Extract source parts from completed web_search and web_fetch
- // calls. Both emit the same `Title:` / `URL:` / `Snippet:` block
- // shape, so the parser need not branch on tool name.
+ // web_search and web_fetch emit the same `Title:` / `URL:` / `Snippet:` block shape, so the
+ // parser need not branch on tool name.
const sourceParts = toolCallParts.flatMap((tc) => {
if (
(tc.toolName !== "web_search" && tc.toolName !== "web_fetch") ||
@@ -8082,11 +7558,10 @@ export function createOpenAIStreamAdapter(
// Anthropic-only (billed at the write premium).
const cacheWriteTokens = meta?.usage?.cache_creation_input_tokens ?? 0;
- // Gate on the captured checkpoint so a late completion from provider A cannot populate
- // the bar after a mid-stream switch to B, and on the captured thread so a background
- // run finishing after New Chat cannot repaint another chat's usage. An unresolved run
- // has no id to compare, so compare what was on screen when it started. A first turn is
- // adopted onto an id mid-run and autosave moves activeThreadId with it, so read the
+ // Gate on the captured checkpoint and thread so a late completion from provider A cannot
+ // repaint the bar after a switch to B. A first turn is adopted onto an id mid-run, so read
+ // the adopted key or the bar stays blank for life.
+ // A first turn is adopted onto an id mid-run and autosave moves activeThreadId with it, so read the
// adopted key, or the run stays "unresolved" for life and the bar stays blank.
const usageKey = liveThreadKey(serverCancel);
const usageThreadKey = usageKey === "__default" ? null : usageKey;
@@ -8106,8 +7581,8 @@ export function createOpenAIStreamAdapter(
cachedTokens,
cacheWriteTokens,
};
- // File it under this run's own thread even when the gate below blocks the visible
- // write, so switching back re-applies it.
+ // File it under this run's own thread even when the gate blocks the visible write, so
+ // switching back re-applies it.
if (usageThreadKey !== null) {
useChatRuntimeStore
.getState()
@@ -8136,28 +7611,19 @@ export function createOpenAIStreamAdapter(
// Before the lookup below: its network time is not generation time.
const finishedAt = Date.now();
- // Small models list things and never ask for their pictures, so fetch the
- // missing ones and record the web_search call they should have made.
- // Local tool loop only (a hosted provider would be replayed a tool it never
- // saw), never over the user's own documents, never behind an approval prompt.
- // The THREAD, not this turn and not the current toggle state. A follow-up
- // answers from a search_knowledge_base result replayed in context without
- // retrieving again, and turning RAG off afterwards clears both flags while
- // those names are still sitting in the conversation -- so "list those product
- // names" could hand private-document subjects to the image engines. Once a
- // chat has retrieved private documents it stays ineligible.
+ // Small models list things and never ask for their pictures, so fetch the missing ones and
+ // record the web_search call they should have made. Local tool loop only, never over the
+ // user's documents, never behind an approval prompt. Gated on the THREAD, not the turn: a
+ // follow-up answers from a search_knowledge_base result replayed in context, so once a chat
+ // has retrieved private documents it stays ineligible even after RAG is switched off.
const answerUsedPrivateDocs =
ragEnabled ||
projectRagEnabled ||
messagesUsePrivateContent(messages) ||
toolCallParts.some((part) => part.toolName === "search_knowledge_base");
- // This run's own values, destructured from the runtime it started with,
- // not the store as it stands now. Both are per-chat, and a run finishing
- // after the user moved to a chat on "auto" would read that chat's
- // permission level and look images up for an answer whose own chat had
- // asked to be consulted first. (searchImages stays a live read: it
- // describes the installation, so navigating cannot change it, and a user
- // who has just switched it off should not get one last lookup.)
+ // This run's own values, not the store as it stands now: both are per-chat, and a run
+ // finishing after a move to a chat on "auto" would read that chat's permission level.
+ // searchImages stays a live read, since it describes the installation.
const toolCallsNeedApproval = confirmToolCalls && permissionMode === "ask";
if (
incompleteReason === null &&
@@ -8174,18 +7640,15 @@ export function createOpenAIStreamAdapter(
);
const subjects = missingListSubjects(answerText, toolCallParts);
if (subjects.length > 0) {
- // Bounded so a slow engine cannot hold a finished answer open.
- // Linked by hand: AbortSignal.any is newer than the Safari floor.
+ // Bounded so a slow engine cannot hold a finished answer open; linked by hand, since
+ // AbortSignal.any is newer than the Safari floor.
const lookupAbort = new AbortController();
const onRunAbort = () => lookupAbort.abort();
runAbort.signal.addEventListener("abort", onRunAbort, { once: true });
const lookupTimer = setTimeout(() => lookupAbort.abort(), 15_000);
try {
- // authFetch, not a raw fetch: an access token that expired during a long
- // generation is refreshed and the call retried, where a stale bearer just
- // 401s into the catch below and the pictures never arrive. Unlike the
- // cancel POST above, a login redirect here is right -- the answer is
- // already finished, so nothing is interrupted by it.
+ // authFetch, not a raw fetch: a token that expired during a long generation is refreshed and
+ // retried. A login redirect is fine here, unlike the cancel POST: the answer is finished.
const response = await authFetch(
"/api/inference/search-images/lookup",
{
@@ -8207,8 +7670,8 @@ export function createOpenAIStreamAdapter(
const args = { image_queries: subjects };
toolCallParts.push({
type: "tool-call" as const,
- // No colon: replayed ids must satisfy ^[a-zA-Z0-9_-]+$.
- // Guarded: randomUUID is undefined over plain HTTP on a LAN.
+ // No colon: replayed ids must satisfy ^[a-zA-Z0-9_-]+$. Guarded, since randomUUID is
+ // undefined over plain HTTP on a LAN.
toolCallId: `auto-images-${
typeof crypto !== "undefined" && "randomUUID" in crypto
? crypto.randomUUID()
@@ -8244,7 +7707,6 @@ export function createOpenAIStreamAdapter(
finalTokPerSec,
);
- // Finalize reasoning-only streams.
reasoningDurationTracker.finishGroup();
yield {
content: [
@@ -8290,10 +7752,8 @@ export function createOpenAIStreamAdapter(
const msg = err instanceof Error ? err.message : String(err);
if (err instanceof GenerationLengthError) {
toast.error("Response ran out of tokens", {
- // The error already chose between the Max Tokens and Context Length
- // remedies from the cap and the prompt size. Repeating the Max Tokens
- // advice here overrode that choice in the one place the user reads,
- // sending them to a setting that is already at its maximum.
+ // The error already chose between the Max Tokens and Context Length remedies; repeating the
+ // Max Tokens advice here overrode that choice in the one place the user reads.
description:
msg ||
"The model used the full Max Tokens budget while thinking " +
@@ -8311,8 +7771,7 @@ export function createOpenAIStreamAdapter(
});
}
} else if (err instanceof StreamInterruptedError) {
- // Connection dropped mid-turn: surface it explicitly (the rethrow
- // below also marks the message with an inline error + Retry).
+ // Connection dropped mid-turn: surface it explicitly (the rethrow also marks the message).
toast.error("Response interrupted", {
description:
"The connection dropped before the model finished. " +
@@ -8320,28 +7779,22 @@ export function createOpenAIStreamAdapter(
duration: 8000,
});
} else if (isContextLimitError(msg)) {
- // `fits: false` means everything evictable was evicted and the request STILL
- // does not fit, so the message just sent is the problem and the usual advice
- // is wrong: the history is already gone. Say which part is too long.
+ // `fits: false` means everything evictable was evicted and it still does not fit, so the
+ // message just sent is the problem and "start a new chat" is the wrong advice.
const irreducible =
contextTruncation?.fits === false ? contextTruncation : null;
- // Against prompt_target, not context_length: the fit reserves up to a quarter
- // of the window for the reply, so a 3,500-token message cannot fit a 4,096
- // context, and comparing with the raw window would blame the conversation and
- // send the user to a new chat that fails identically.
+ // Against prompt_target, not context_length: the fit reserves up to a quarter of the window
+ // for the reply, so the raw window would blame the conversation.
const budget =
irreducible?.prompt_target ?? irreducible?.context_length ?? 0;
- // Both the gate and the number below take the shared prompt floor off the turn
- // first: `latest_turn_tokens` prices a whole rendered prompt, so on a
- // tool-enabled request it carries the entire tool catalogue, and quoting it
- // told the user a 6-token message was thousands of tokens "on its own".
+ // Both the gate and the number take the shared prompt floor off the turn first:
+ // `latest_turn_tokens` prices a whole rendered prompt, tool catalogue included.
const oneTurnIsTheProblem = latestTurnIsTheProblem(
irreducible,
budget,
);
- // Whose turn it is decides the advice: in a tool loop the offending turn is
- // often output the user never wrote and cannot edit, so "shorten this
- // message" names the wrong thing and offers no remedy.
+ // Whose turn it is decides the advice: in a tool loop the offending turn is often output the
+ // user never wrote and cannot edit.
const userCanShortenIt =
(irreducible?.latest_turn_role ?? "user") === "user";
const tooLong =
@@ -8360,19 +7813,17 @@ export function createOpenAIStreamAdapter(
'Raise "Context Length" in the chat Settings panel (⚙ in the top-right), ' +
"or ask for less output from that tool."
: historyCannotHelp(irreducible)
- ? // No one turn to name, and the bulk is in the parts eviction never
- // touches, so "start a new chat" opens a chat that fails identically.
- // Both levers are named, neither claimed as the cause: the floor
- // bundles the template wrapper with the catalogue, so a large one does
- // not prove there are tools.
+ // No one turn to name, and the bulk is in the parts eviction never touches. Both levers
+ // are named, neither claimed as the cause.
+ ?
"Even with every earlier turn dropped, this prompt would still be " +
"too long, so shortening the conversation will not help. " +
'Raise "Context Length" in the chat Settings panel (⚙ in the top-right), ' +
"or reduce what every request carries: the system prompt and any " +
"tools that are enabled."
- : // llama-server runs with --no-context-shift, returning a hard
- // error instead of silently dropping old KV-cache turns. Point
- // the user at the control that raises the ceiling.
+ // llama-server runs with --no-context-shift, a hard error instead of silently dropping
+ // old KV turns, so point at the control that raises the ceiling.
+ :
"The conversation has filled the model's context window. " +
'Increase "Context Length" in the chat Settings panel (⚙ in the top-right), ' +
"or start a new chat.",
@@ -8423,13 +7874,12 @@ export function createOpenAIStreamAdapter(
}
throw err;
} finally {
- // Unconditional, and both ids: the pre-admission claim uses `cancelId`, and a run
- // left claimed after its stream died is one this tab would never recover.
+ // Unconditional, and both ids: the pre-admission claim uses `cancelId`, and a run left
+ // claimed after its stream died would never be recovered.
releaseLiveGenerationRun(cancelId);
if (generationRunId) releaseLiveGenerationRun(generationRunId);
- // A durable run that finished here is no longer active, and only a later
- // history load would otherwise say so. Until then the thread reads as durable
- // and a subscriber-owned stream started on it next would be capped.
+ // A durable run that finished here is no longer active, and until a history load says so the
+ // thread reads as durable and the next subscriber-owned stream would be capped.
if (
generationRunId &&
(generationStatus === "completed" ||
@@ -8447,13 +7897,10 @@ export function createOpenAIStreamAdapter(
confirmStore.clearToolConfirmation(part.toolCallId);
}
runtime.setGeneratingStatus(null);
- // Scoped by thread AND by run: a global clear wiped every other running chat's badge,
- // and an unowned one wiped a concurrent run's badge behind the same key.
+ // Scoped by thread AND by run: a global clear wiped every other running chat's badge.
runtime.setToolStatus(cleanupKey, null, serverCancel);
- // Clear only this run's live keys (a concurrent pane owns its own). A
- // key still here streamed stdout but never reached tool_end (SSE drop or
- // cancel), so promote it to full output first, else the partial
- // diagnostics the user was watching vanish from the card.
+ // Clear only this run's live keys. A key still here streamed stdout but never reached
+ // tool_end, so promote it to full output first or the diagnostics vanish from the card.
for (const liveKey of runToolLiveOutputKeys) {
const store = useChatRuntimeStore.getState();
const liveOutput = store.toolLiveOutput[liveKey] ?? "";
@@ -8463,8 +7910,8 @@ export function createOpenAIStreamAdapter(
store.clearToolLiveOutput(liveKey);
}
runToolLiveOutputKeys.clear();
- // Drop the transient denoising canvas so the finished bubble shows only the committed
- // answer. Scoped: a global clear wiped another denoising chat's frame.
+ // Drop the transient denoising canvas so the finished bubble shows only the committed answer;
+ // scoped, since a global clear wiped another chat's frame.
runtime.clearActiveDiffusionCanvasForThread(cleanupKey);
clearTimeout(warmupTimer);
if (waitingFirstChunk) {
@@ -8476,8 +7923,7 @@ export function createOpenAIStreamAdapter(
settleFirstTokenErr(new Error("No tokens received"));
}
}
- // serverCancel narrows both clears: runs with no resolved thread id share the "__default"
- // key, so a blind clear could drop a sibling's entry.
+ // serverCancel narrows both clears: runs with no resolved thread id share the "__default" key.
runtime.setThreadRunning(cleanupKey, false, { owner: serverCancel });
runtime.clearThreadServerCancel(cleanupKey, serverCancel);
}
diff --git a/studio/frontend/src/features/chat/api/chat-api.ts b/studio/frontend/src/features/chat/api/chat-api.ts
index caa70a7e5d6..81c74d78e97 100644
--- a/studio/frontend/src/features/chat/api/chat-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-api.ts
@@ -51,8 +51,8 @@ import { assertCompletedPaddedBody } from "./padded-response";
import { maxTokensIsTheLimit } from "./generation-length.ts";
export const CHAT_HISTORY_UPDATED_EVENT = "unsloth-chat-history-updated";
-// Bumped alongside that event so other tabs, which never receive it, can drop caches
-// they built from a history this one has just changed.
+// Bumped alongside that event so other tabs, which never receive it, can drop caches they built
+// from a history this one has just changed.
export { CHAT_HISTORY_REVISION_KEY } from "../utils/chat-history-revision";
export const CHAT_PROJECTS_UPDATED_EVENT = "unsloth-chat-projects-updated";
@@ -78,8 +78,7 @@ async function threadWriteFetch(
timeout.dispose();
}
}
- // Either reason ends the request. Linked by hand rather than with AbortSignal.any,
- // which Safari only got in 17.4.
+ // Either reason ends the request. Linked by hand rather than with AbortSignal.any, which Safari only got in 17.4.
const controller = new AbortController();
const abort = () => controller.abort();
if (caller.aborted) abort();
@@ -93,10 +92,8 @@ async function threadWriteFetch(
}
}
-/**
- * Thrown when the chat SSE stream ends without a terminal signal (`[DONE]` or a finish_reason
- * chunk): the connection dropped mid-generation, surfaced as an explicit interrupted state.
- */
+/** Thrown when the chat SSE stream ends without a terminal signal (`[DONE]` or a finish_reason
+ * chunk): the connection dropped mid-generation, surfaced as an interrupted state. */
export class StreamInterruptedError extends Error {
constructor() {
super(
@@ -107,27 +104,13 @@ export class StreamInterruptedError extends Error {
}
}
-/**
- * Thrown when a reasoning model consumes its output budget before emitting any standard
- * content, so the chat UI can explain a completed stream holding only a thinking panel.
- */
+/** Thrown when a reasoning model consumes its output budget before emitting any standard content,
+ * so the chat UI can explain a completed stream holding only a thinking panel. */
export class GenerationLengthError extends Error {
- /**
- * @param maxTokensWasSet whether the user actually configured a Max Tokens value.
- *
- * With Max Tokens left on "Max" the backend already requests the WHOLE context length
- * (`payload["max_tokens"] = self._effective_context_length`), so generation stops at the
- * context wall rather than at any setting, and "Increase Max Tokens" is advice that
- * cannot be followed: it is at its maximum and raising it changes nothing. Observed on a
- * 4096-token window where the prompt left roughly a thousand tokens to answer in, which
- * medium thinking spent before writing anything.
- *
- * The false branch is NOT only that case. A finite cap the prompt left no room for
- * lands here too (cap 2048, prompt 3000, window 4096), and the context-length remedy
- * is right there as well -- which is why the wording says raising the cap cannot
- * create room, rather than describing the cap itself as having no limit. That
- * description would contradict the finite value the user can see in Settings.
- */
+ /** @param maxTokensWasSet whether the user actually configured a Max Tokens value. With Max Tokens
+ * on "Max" the backend already requests the whole context length, so generation stops at the
+ * context wall and "Increase Max Tokens" cannot be followed. The false branch also covers a
+ * finite cap the prompt left no room for, hence the wording about raising the cap. */
constructor(maxTokensWasSet = true) {
super(
maxTokensWasSet
@@ -142,12 +125,9 @@ export class GenerationLengthError extends Error {
}
}
-/**
- * Announces a history change to this document and, through localStorage, to the others.
- *
- * `coalesce` is for the per-chunk streaming path alone: it holds the cross-tab write until
- * the writes stop. Structural changes must not use it.
- */
+/** Announces a history change to this document and, through localStorage, to the others.
+ * `coalesce` is for the per-chunk streaming path alone: it holds the cross-tab write until
+ * the writes stop. Structural changes must not use it. */
export function notifyChatHistoryUpdated(
detail: ChatHistoryUpdatedDetail = {},
): void {
@@ -175,10 +155,8 @@ function parseErrorText(status: number, body: unknown): string {
return formatApiErrorBody(body) ?? `Request failed (${status})`;
}
-/**
- * `/api/inference/load` and `/unload` pad their body so a proxy cannot time the request out,
- * which commits the status early: a later failure can only arrive in-band as `_deferred_error`.
- */
+/** `/api/inference/load` and `/unload` pad their body so a proxy cannot time the request out,
+ * which commits the status early: a later failure can only arrive as `_deferred_error`. */
function deferredError(
body: unknown,
): { status: number; message: string } | null {
@@ -199,9 +177,8 @@ function deferredError(
};
}
-/**
- * `paddedLabel` opts a caller into `assertCompletedPaddedBody`; only the two padded routes may,
- * since a truncated body means unfinished there but is legitimate elsewhere. */
+/** `paddedLabel` opts a caller into `assertCompletedPaddedBody`; only the two padded routes may,
+ * since a truncated body means unfinished there but is legitimate elsewhere. */
async function parseJsonOrThrow<T>(
response: Response,
paddedLabel?: string,
@@ -263,19 +240,17 @@ export async function clearApiMonitor(): Promise<void> {
export interface ActiveGenerationsResponse {
count: number;
- /** Conversations with a generation in flight. Shorter than `count` when a
- * first turn started before its thread id was persisted. */
+ /** Conversations with a generation in flight. Shorter than `count` when a first turn started
+ * before its thread id was persisted. */
thread_ids: string[];
- /** One entry per in-flight request. `kind` is "chat" unless it is an
- * embeddings / completions / audio call, which has no conversation. */
+ /** One entry per in-flight request. `kind` is "chat" unless it is an embeddings / completions /
+ * audio call, which has no conversation. */
active?: { thread_id: string | null; kind?: string }[];
parallel_slots: number;
}
-/**
- * Chats generating on the backend right now. Authoritative where `runningByThreadId` is not:
- * that map is per-tab, empty after a reload and blind to a second tab, and /load 409s on these.
- */
+/** Chats generating on the backend right now. Authoritative where `runningByThreadId` is not: that
+ * map is per-tab, empty after a reload and blind to a second tab, and /load 409s on these. */
export async function getActiveGenerations(): Promise<ActiveGenerationsResponse> {
const response = await authFetch("/api/inference/active-generations");
return parseJsonOrThrow<ActiveGenerationsResponse>(response);
@@ -286,8 +261,8 @@ export async function loadModel(
options?: {
signal?: AbortSignal;
onRequestStart?: () => void;
- /** What is taking the slot. Chat ignores its own loads when reconciling, so an
- * Audio load announced as "chat" left chat naming a model it had evicted. */
+ /** What is taking the slot. Chat ignores its own loads when reconciling, so an Audio load
+ * announced as "chat" left chat naming a model it had evicted. */
runtime?: ModelRuntime;
},
): Promise<LoadModelResponse> {
@@ -300,9 +275,8 @@ export async function loadModel(
if (options?.signal?.aborted)
throw options.signal.reason ?? new DOMException("Aborted", "AbortError");
options?.onRequestStart?.();
- // Announced after the token prompt, so a cancelled load never shows a row.
- // The indicator otherwise had nothing to show until its next 5s poll, while
- // the toast reported the load immediately.
+ // Announced after the token prompt, so a cancelled load never shows a row. The indicator
+ // otherwise had nothing to show until its next 5s poll.
return withModelLoadNotice(
options?.runtime ?? "chat",
payload.model_path ?? null,
@@ -379,8 +353,8 @@ export async function validateModel(
gpu_layers: payload.gpu_layers,
// Slots scale the KV estimate; keep validate sized like the load.
n_parallel: payload.n_parallel,
- // A --ctx-size or cache override in here changes the estimate, so a preflight
- // that dropped them would approve a different command from the one that runs.
+ // A --ctx-size or cache override in here changes the estimate, so a preflight that dropped them
+ // would approve a different command from the one that runs.
...(payload.llama_extra_args !== undefined
? // biome-ignore lint/style/useNamingConvention: API schema
{ llama_extra_args: payload.llama_extra_args }
@@ -388,9 +362,8 @@ export async function validateModel(
// batch sizes scale the same estimate; omitted when blank so they never read as set
...(payload.n_batch != null ? { n_batch: payload.n_batch } : {}),
...(payload.n_ubatch != null ? { n_ubatch: payload.n_ubatch } : {}),
- // The estimate charges a drafter whose size differs by kind (a DSpark
- // sidecar is ~11 GB), so omitting the mode makes this preflight disagree
- // with /load in both directions.
+ // The estimate charges a drafter whose size differs by kind (a DSpark sidecar is ~11 GB), so
+ // omitting the mode makes this preflight disagree with /load in both directions.
speculative_type: payload.speculative_type ?? null,
spec_draft_n_max: payload.spec_draft_n_max ?? null,
}),
@@ -398,12 +371,9 @@ export async function validateModel(
return parseJsonOrThrow<ValidateModelResponse>(response);
}
-/**
- * Read a GGUF's header dims (native context length, layer count, MoE expert-layer count) from
- * its local file (no GPU load, no download). All null when the file isn't downloaded, isn't a
- * GGUF, or is gated. For a native (drag-drop) file, pass `nativePathToken`. Used by the
- * deferred-load staging flow to size the context, GPU-layers and MoE sliders before the load.
- */
+/** Read a GGUF's header dims (native context length, layer count, MoE expert-layer count) from its
+ * local file, with no GPU load or download. All null when the file is not downloaded, is not
+ * a GGUF, or is gated. For a native drag-drop file, pass `nativePathToken`. */
export async function fetchGgufStagedMetadata(payload: {
model_path: string;
gguf_variant?: string | null;
@@ -414,8 +384,8 @@ export async function fetchGgufStagedMetadata(payload: {
layerCount: number | null;
moeLayerCount: number | null;
isDiffusion: boolean;
- /** Unclassifiable, so `isDiffusion: false` above means "not known to be diffusion":
- * callers picking a GPU split must assume possibly-diffusion. */
+ /** Unclassifiable, so `isDiffusion: false` above means "not known to be diffusion": callers
+ * picking a GPU split must assume possibly-diffusion. */
diffusionUnknown: boolean;
}> {
let nativePathLease: string | null = null;
@@ -425,7 +395,8 @@ export async function fetchGgufStagedMetadata(payload: {
await consumeNativePathToken(payload.nativePathToken, "validate-model")
).nativePathLease;
} catch {
- // Lease expired / revoked: degrade to no metadata (the load can re-mint). Nothing was read, so diffusion is unknown, not false.
+ // Lease expired or revoked: degrade to no metadata (the load can re-mint). Nothing was read, so
+ // diffusion is unknown, not false.
return {
contextLength: null,
layerCount: null,
@@ -466,11 +437,9 @@ export async function unloadModel(payload: UnloadModelRequest): Promise<void> {
await parseJsonOrThrow<unknown>(response, "Model unload");
}
-/**
- * Allow or deny a tool call paused awaiting user confirmation. Identified by the backend
- * ``approvalId`` echoed in the tool_start event; ``sessionId`` is a scope check. Resolves to
- * ``true`` only when the backend matched a pending call, so a stale post can offer a retry.
- */
+/** Allow or deny a tool call paused awaiting user confirmation, identified by the backend
+ * `approvalId` echoed in the tool_start event, with `sessionId` as a scope check. Resolves to
+ * true only when the backend matched a pending call. */
export async function resolveToolConfirmation(
sessionId: string,
approvalId: string,
@@ -496,14 +465,13 @@ export interface CachedGgufRepo {
cache_path: string;
/** epoch seconds of the newest downloaded quant; optional for older backends. */
last_modified?: number;
- /** True when the repo ships an mmproj adapter (image inputs). Optional for
- * older-backend compatibility. */
+ /** True when the repo ships an mmproj adapter (image inputs). Optional for older-backend compatibility. */
has_vision?: boolean;
- /** HF pipeline task inferred from the GGUF architecture ("text-to-image" for diffusion), so the Images picker can show only diffusion GGUFs. Optional for older backends. */
+ /** HF pipeline task inferred from the GGUF architecture ("text-to-image" for diffusion), so the
+ * Images picker can show only diffusion GGUFs. */
task?: string | null;
audio_type?: string | null;
- /** True when some quant has a download manifest or cancel marker. Optional
- * for older-backend compatibility. */
+ /** True when some quant has a download manifest or cancel marker. Optional for older-backend compatibility. */
has_variant_state?: boolean;
partial?: boolean;
/** Whether that partial can be continued byte for byte. False on a GGUF repo row by design:
@@ -541,19 +509,15 @@ export async function getGgufDownloadProgress(
export interface DownloadProgressResponse {
downloaded_bytes: number;
- /**
- * Finalized-blob bytes only. Bytes still landing in a `.incomplete` blob count
- * toward `downloaded_bytes` but not here, so the two are equal exactly when
- * nothing is in flight.
- */
+ /** Finalized-blob bytes only. Bytes still landing in a `.incomplete` blob count toward
+ * `downloaded_bytes` but not here, so the two are equal exactly when nothing is in flight. */
completed_bytes: number;
/** True once the backend verified a usable snapshot on disk; `progress` is capped at 0.99 until then. */
complete_on_disk: boolean;
expected_bytes: number;
progress: number;
- /**
- * On-disk path of the snapshot dir (or cache repo root if no snapshot yet); null when
- * nothing has been written to the cache for this repo. */
+ /** On-disk path of the snapshot dir (or the cache repo root if there is no snapshot yet); null
+ * when nothing has been written to the cache for this repo. */
cache_path: string | null;
}
@@ -583,19 +547,16 @@ export async function getDatasetDownloadProgress(
export type ModelLoadPhase = "mmap" | "ready" | null;
export interface LoadProgressResponse {
- /**
- * Load phase: "mmap" while llama-server pages weight shards into RAM, "ready" once
- * healthy, or null when no load is in flight. */
+ /** Load phase: "mmap" while llama-server pages weight shards into RAM, "ready" once healthy, or
+ * null when no load is in flight. */
phase: ModelLoadPhase;
bytes_loaded: number;
bytes_total: number;
fraction: number;
}
-/**
- * Fetch the active GGUF load's mmap/upload progress. Complements the download progress
- * endpoints for the "download complete" -> "chat ready" window, minutes for large MoE models.
- */
+/** Fetch the active GGUF load's mmap/upload progress. Complements the download progress endpoints
+ * for the "download complete" to "chat ready" window, minutes for large MoE models. */
export async function getLoadProgress(): Promise<LoadProgressResponse> {
const response = await authFetch("/api/inference/load-progress");
return parseJsonOrThrow(response);
@@ -612,7 +573,7 @@ export interface LocalModelInfo {
// Set when a cached snapshot holds an incomplete download, so consumers skip unloadable weights.
partial?: boolean;
updated_at?: number | null;
- // HF pipeline task inferred from the GGUF architecture, so the Images picker can filter to diffusion ("text-to-image"). Optional for older backends.
+ // HF pipeline task inferred from the GGUF architecture, so the Images picker can filter to diffusion.
task?: string | null;
/** Detected output-audio architecture or codec used by Audio runtime policy. */
audio_type?: string | null;
@@ -644,25 +605,26 @@ export interface CachedModelRepo {
repo_id: string;
load_id?: string | null;
size_bytes: number;
- /** Weights format; "adapter" is a LoRA with no base weights of its own.
- * Optional for older-backend compatibility. */
+ /** Weights format; "adapter" is a LoRA with no base weights of its own. Optional for older-backend compatibility. */
model_format?: string | null;
/** epoch seconds of the newest downloaded weight; optional for older backends. */
last_modified?: number;
- /** HF pipeline task: "text-to-image" for a cached diffusers pipeline repo (model_index.json present), so the chat picker can hide it. Absent = chat. */
+ /** HF pipeline task: "text-to-image" for a cached diffusers pipeline repo, so the chat picker can
+ * hide it. Absent = chat. */
task?: string | null;
/** Detected output-audio architecture or codec used by Audio runtime policy. */
audio_type?: string | null;
- /** True when the snapshot is incomplete (a cancelled/partial download): such a repo must not count as downloaded, or a click re-downloads the full weights. */
+ /** True when the snapshot is incomplete: such a repo must not count as downloaded, or a click
+ * re-downloads the full weights. */
partial?: boolean;
/** Whether that partial can be continued byte for byte, rather than restarting its file. */
partial_resumable?: boolean;
/** True for a diffusion repo with no model_index.json: a single-file checkpoint loadable only via from_single_file, so task pickers must not offer it as a pipeline load unless the curated catalog carries its artifact. */
single_file?: boolean;
- /** True for an sd.cpp companion mirror (VAE / text encoders, no denoiser): listed so it can be seen and deleted, never offered as a load. */
+ /** True for an sd.cpp companion mirror (VAE / text encoders, no denoiser): listed so it can be
+ * seen and deleted, never offered as a load. */
companion?: boolean;
- /** Owning cache dir; sent so a delete targets this copy, not the active
- * cache. Optional for older-backend compatibility. */
+ /** Owning cache dir; sent so a delete targets this copy, not the active cache. */
cache_path?: string | null;
capabilities?: CachedRepoCapabilities | null;
tags?: string[];
@@ -856,12 +818,10 @@ export async function getChatThread(
threadId: string,
options: { bounded?: boolean; timeoutMs?: number; signal?: AbortSignal } = {},
): Promise<ThreadRecord | null> {
- // Bounded for the delete reconciliation: an unbounded read there would hang the delete that
- // the write timeout exists to keep moving. Callers on a render path stay unbounded.
- // `timeoutMs` is for a caller with a deadline of its own: the settings pairing gives up
- // on a read long before the write timeout, and without a bound of its own each retry
- // left the previous attempt running against the server it is already struggling to
- // reach. `signal` ends it earlier still, which is what a chat switch wants.
+ // Bounded for the delete reconciliation: an unbounded read there would hang the delete the write
+ // timeout exists to keep moving. `timeoutMs` is for a caller with a deadline of its own,
+ // since the settings pairing gives up long before the write timeout and each retry otherwise
+ // left the previous attempt running. `signal` ends it earlier still.
const timeout =
options.bounded || options.timeoutMs !== undefined
? disposableTimeoutSignal(options.timeoutMs ?? THREAD_WRITE_TIMEOUT_MS)
@@ -913,20 +873,16 @@ export interface UpdateChatThreadOptions {
expectedTitle?: string;
/** And only while this is still the thread's opening user message. */
expectedOpeningMessageId?: string;
- /**
- * Off for one update inside a bulk action, which announces itself once at the end. Every
- * notification is a synchronous localStorage write that wakes the other tabs, so Archive
- * All would otherwise send one per thread.
- */
+ /** Off for one update inside a bulk action, which announces itself once at the end. Every
+ * notification is a synchronous localStorage write that wakes the other tabs, so Archive All
+ * would otherwise send one per thread. */
notify?: boolean;
/** Give up on the write; used to stand a superseded settings PATCH down. */
signal?: AbortSignal;
}
-/**
- * `settings` replaces the chat's whole snapshot; `settingsPatch` applies only the fields
- * it names, for a writer that knows what changed but not what else the row holds.
- */
+/** `settings` replaces the chat's whole snapshot; `settingsPatch` applies only the fields it
+ * names, for a writer that knows what changed but not what else the row holds. */
export type ChatThreadWritePatch = Partial<ThreadRecord> & {
settingsPatch?: ThreadRecord["settings"];
/** Orders a writer's snapshot writes against its own earlier ones, never across tabs. */
@@ -1101,9 +1057,8 @@ export async function listChatMessages(
return data.messages;
}
-/**
- * Fetch messages for many threads in one HTTP call. Falls back to per-thread
- * listChatMessages on 404/405 (older servers without the batch route). */
+/** Fetch messages for many threads in one HTTP call. Falls back to per-thread listChatMessages on
+ * 404/405 (older servers without the batch route). */
export async function batchListChatMessages(
threadIds: string[],
): Promise<Map<string, MessageRecord[]>> {
@@ -1142,12 +1097,9 @@ export async function getChatMessage(
return parseJsonOrThrow<MessageRecord>(response);
}
-/** The server owns this message and will reject every save of it.
- *
- * Distinct from a transient failure: retrying can never succeed, so callers must stop
- * rather than back off. Without this the per-chunk autosave treated the rejection as an
- * anonymous error and re-sent on the next chunk for the whole generation.
- */
+/** The server owns this message and will reject every save of it. Distinct from a transient
+ * failure: retrying can never succeed, so callers must stop rather than back off. Without
+ * this the per-chunk autosave re-sent on every chunk for the whole generation. */
/** Set by routes/chat_history.py; exposed through the CORS middleware in main.py. */
const CONFLICT_KIND_HEADER = "X-Unsloth-Conflict-Kind";
const CONFLICT_KIND_PROTECTED = "protected";
@@ -1180,9 +1132,8 @@ export async function saveChatMessage(
body: JSON.stringify(message),
},
);
- // Two failures share this status: a protected message, where the autosave must stop, and
- // a thread-id collision, which the caller must see or the message is lost. Only the header
- // separates them, so anything else (an older backend included) takes the normal error path.
+ // Two failures share this status: a protected message, where the autosave must stop, and a
+ // thread-id collision, which the caller must see. Only the header separates them.
if (
response.status === 409 &&
response.headers?.get(CONFLICT_KIND_HEADER) === CONFLICT_KIND_PROTECTED
@@ -1196,8 +1147,8 @@ export async function saveChatMessage(
);
}
const savedMessage = await parseJsonOrThrow<MessageRecord>(response);
- // Coalescing is the streaming autosave's alone, since it lands here per chunk. A manual
- // edit is one deliberate change and publishes at once.
+ // Coalescing is the streaming autosave's alone, since it lands here per chunk. A manual edit is
+ // one deliberate change and publishes at once.
notifyChatHistoryUpdated({ coalesce: options.coalesce === true });
return savedMessage;
}
@@ -1220,8 +1171,8 @@ export async function syncChatMessages(
},
);
const data = await parseJsonOrThrow<{ messages: MessageRecord[] }>(response);
- // Pruning is how a message is deleted, which no other tab should keep matching for a
- // whole unrelated generation. Without it this is the batched streaming autosave.
+ // Pruning is how a message is deleted, which no other tab should keep matching for a whole
+ // unrelated generation. Without it this is the batched streaming autosave.
notifyChatHistoryUpdated({ coalesce: options.pruneMissing !== true });
return data.messages;
}
@@ -1281,12 +1232,12 @@ export async function buildBackendChatExport(): Promise<{
return parseJsonOrThrow(response);
}
-// Legacy-Dexie import ledger: a server-side source of truth replacing the localStorage
-// sentinel, so a studio.db wipe keeps the import recoverable.
+// Legacy-Dexie import ledger: a server-side source of truth replacing the localStorage sentinel,
+// so a studio.db wipe keeps the import recoverable.
export async function listChatImportLedger(): Promise<Set<string>> {
const response = await authFetch("/api/chat/import-ledger");
- // Backends without this endpoint behave like an empty ledger -- the caller re-imports every
- // legacy thread, and syncChatMessages UPSERTs prevent duplicates.
+ // Backends without this endpoint behave like an empty ledger: the caller re-imports every legacy
+ // thread, and syncChatMessages UPSERTs prevent duplicates.
if (response.status === 404 || response.status === 405) return new Set();
const data = await parseJsonOrThrow<{ threadIds: string[] }>(response);
return new Set(data.threadIds);
@@ -1296,7 +1247,7 @@ export interface RecordChatImportLedgerResult {
accepted: number;
inserted: number;
// false when the backend predates /api/chat/import-ledger, so the caller does not poison the
- // localStorage perf hint; the next launch retries the (idempotent) import.
+ // localStorage perf hint; the next launch retries the idempotent import.
supported: boolean;
}
@@ -1358,7 +1309,8 @@ export async function browseFolders(
if (path !== undefined && path !== null) params.set("path", path);
if (showHidden) params.set("show_hidden", "true");
const qs = params.toString();
- // Forward the AbortSignal through authFetch so a cancelled FolderBrowser navigation also cancels the server-side walk.
+ // Forward the AbortSignal through authFetch so a cancelled FolderBrowser navigation also cancels
+ // the server-side walk.
const response = await authFetch(
`/api/models/browse-folders${qs ? `?${qs}` : ""}`,
signal ? { signal } : undefined,
@@ -1387,37 +1339,33 @@ export interface KvCacheEstimate {
native_context: number | null;
/** Extra MTP draft reserve; null for ngram or a model with no MTP head. */
spec_bytes: number | null;
- /** Context the estimate was computed at, which is the native length when the
- * request omitted one. */
+ /** Context the estimate was computed at, which is the native length when the request omitted one. */
n_ctx: number | null;
- /** Vision projector footprint, at its worst-case VRAM multiple. Null when the
- * model ships none or vision is disabled. */
+ /** Vision projector footprint, at its worst-case VRAM multiple. Null when the model ships none or
+ * vision is disabled. */
projector_bytes: number | null;
- /** True when the configured speculative mode attaches a drafter the route did
- * not price (dspark/dflash, and Auto where it promotes to one). The total is
- * then a floor, not an answer. */
+ /** True when the configured speculative mode attaches a drafter the route did not price (dspark/
+ * dflash, and Auto where it promotes to one). The total is then a floor, not an answer. */
spec_unpriced: boolean;
- /** The share of kv_bytes llama.cpp keeps in HOST heap rather than on the card:
- * the SWA checkpoint snapshots. Included in kv_bytes, so a VRAM figure has to
- * subtract it; the planner's own GPU total does exactly that. */
+ /** The share of kv_bytes llama.cpp keeps in HOST heap rather than on the card: the SWA checkpoint
+ * snapshots. Included in kv_bytes, so a VRAM figure has to subtract it. */
kv_checkpoint_bytes: number | null;
- /** The share of spec_bytes no shorter context can reduce, being the separate
- * drafter's resident weights. Auto-fit softening must not cover it. */
+ /** The share of spec_bytes no shorter context can reduce, being the separate drafter's resident
+ * weights. Auto-fit softening must not cover it. */
spec_fixed_bytes: number | null;
- /** The load planner's compute buffers, which every launch reserves on top of
- * weights and cache. Scales with slots and micro-batch. */
+ /** The load planner's compute buffers, which every launch reserves on top of weights and cache.
+ * Scales with slots and micro-batch. */
compute_bytes: number | null;
/** The planner's complete GPU-resident figure, and its everything-total. */
gpu_bytes: number | null;
total_bytes: number | null;
- /** What still lands on the card at the shortest context: the share no context
- * reduction can recover. */
+ /** What still lands on the card at the shortest context: the share no context reduction can recover. */
gpu_floor_bytes: number | null;
- /** False only when the loader is free to shrink the context to fit. An
- * inherited LLAMA_ARG_CTX_SIZE is kept, not fitted. */
+ /** False only when the loader is free to shrink the context to fit. An inherited
+ * LLAMA_ARG_CTX_SIZE is kept, not fitted. */
context_is_pinned: boolean | null;
- /** An inherited LLAMA_ARG_DEVICE confines the launch to the cards it names, so
- * an aggregate VRAM budget describes a pool it will not open. */
+ /** An inherited LLAMA_ARG_DEVICE confines the launch to the cards it names, so an aggregate VRAM
+ * budget describes a pool it will not open. */
inherited_device_pin: boolean | null;
}
@@ -1427,8 +1375,8 @@ export interface KvCacheEstimateOptions {
nParallel?: number | null;
/** Speculative mode, so an MTP draft reserve is priced into the estimate. */
speculativeType?: string | null;
- /** --spec-draft-n-max; a Hybrid Mamba target keeps one rollback state per
- * drafted token, which dominates its reserve. */
+ /** --spec-draft-n-max; a Hybrid Mamba target keeps one rollback state per drafted token, which
+ * dominates its reserve. */
specDraftNMax?: number | null;
/** Draft KV dtype, quantized independently of the main cache. */
specDraftCacheType?: string | null;
@@ -1444,10 +1392,9 @@ export interface KvCacheEstimateOptions {
signal?: AbortSignal;
}
-/** Estimate KV cache + weight + speculative bytes for a downloaded quant, for
- * the load dialog's memory warning and the picker's memory bar. Omit `nCtx` to
- * size against the model's own context length; the response says which was
- * used. */
+/** Estimate KV cache + weight + speculative bytes for a downloaded quant, for the load dialog's
+ * memory warning and the picker's memory bar. Omit `nCtx` to size against the model's own
+ * context length; the response says which was used. */
export async function estimateKvCache(
repoId: string,
quant: string,
@@ -1470,20 +1417,19 @@ export async function estimateKvCache(
const params = new URLSearchParams({ repo_id: repoId, quant });
if (nCtx && nCtx > 0) params.set("n_ctx", String(nCtx));
if (cacheTypeKv) params.set("cache_type_kv", cacheTypeKv);
- // Any positive override goes, including 1: omitting it means "use the
- // server's slot count", which now defaults to more than one.
+ // Any positive override goes, including 1: omitting it means "use the server's slot count", which
+ // now defaults to more than one.
if (nParallel && nParallel > 0) params.set("n_parallel", String(nParallel));
if (speculativeType) params.set("speculative_type", speculativeType);
- // Zero is a real choice for both of these (no rollback states, no
- // checkpoints), so they are sent whenever set rather than when truthy.
+ // Zero is a real choice for both of these, so they are sent whenever set rather than when truthy.
if (specDraftNMax != null && specDraftNMax >= 0)
params.set("spec_draft_n_max", String(specDraftNMax));
if (specDraftCacheType)
params.set("spec_draft_cache_type", specDraftCacheType);
if (ctxCheckpoints != null && ctxCheckpoints >= 0)
params.set("ctx_checkpoints", String(ctxCheckpoints));
- // The compute buffers scale with these, and the planner defaults them when
- // they are absent, which underprices a config that raised either.
+ // The compute buffers scale with these, and the planner defaults them when absent, which
+ // underprices a config that raised either.
if (nBatch && nBatch > 0) params.set("n_batch", String(nBatch));
if (nUbatch && nUbatch > 0) params.set("n_ubatch", String(nUbatch));
if (tensorParallel) params.set("tensor_parallel", "true");
@@ -1563,11 +1509,8 @@ function classifyStructuredDeltaContent(content: unknown): {
export async function* streamChatCompletions(
payload: OpenAIChatCompletionsRequest,
signal: AbortSignal,
- /**
- * The window this request is served by, when the caller knows it. Used only to tell a
- * user-chosen Max Tokens apart from the backend's stand-in for "Max", which is the whole
- * context length -- the two need opposite advice when generation stops on length.
- */
+ /** The window this request is served by, when the caller knows it. Used only to tell a user-chosen
+ * Max Tokens apart from the backend's stand-in for "Max", which is the whole context length. */
loadedContextLength?: number | null,
): AsyncGenerator<OpenAIChatChunk> {
const response = await authFetch("/v1/chat/completions", {
@@ -1595,9 +1538,8 @@ export async function* streamChatCompletions(
let terminalFinishReason: string | null = null;
let sawAssistantContent = false;
let sawReasoningContent = false;
- // Reported by the server on the final chunk. Needed to tell the two walls apart: a
- // finite Max Tokens below the context length does not mean Max Tokens is what stopped
- // the generation.
+ // Reported by the server on the final chunk. Needed to tell the two walls apart: a finite Max
+ // Tokens below the context length does not mean Max Tokens stopped the generation.
let promptTokens: number | null = null;
const throwIfReasoningOnlyLength = () => {
@@ -1606,9 +1548,9 @@ export async function* streamChatCompletions(
sawReasoningContent &&
!sawAssistantContent
) {
- // The backend substitutes the full context length when the user left Max Tokens on
- // "Max", so a payload value equal to it is indistinguishable from unset here -- and
- // both mean the same thing to the user: the setting is not the lever.
+ // The backend substitutes the full context length when the user left Max Tokens on "Max", so a
+ // payload value equal to it is indistinguishable from unset, and both mean the setting is
+ // not the lever.
throw new GenerationLengthError(
maxTokensIsTheLimit({
cap: payload.max_tokens ?? null,
@@ -1667,8 +1609,8 @@ export async function* streamChatCompletions(
separatorIndex = buffer.search(/\r?\n\r?\n/);
continue;
}
- // Diffusion frame: a per-step canvas snapshot. Custom SSE payload (not an OpenAI chunk) with
- // no assistant text, surfaced as a transient marker for the in-place renderer, never the transcript.
+ // Diffusion frame: a per-step canvas snapshot, surfaced as a transient marker for the in-place
+ // renderer and never the transcript.
if ("type" in parsed && parsed.type === "diffusion_frame") {
yield {
_diffusionFrame: parsed,
@@ -1676,8 +1618,8 @@ export async function* streamChatCompletions(
separatorIndex = buffer.search(/\r?\n\r?\n/);
continue;
}
- // tool_start/end carry full input/output; tool_output streams
- // incremental stdout and tool_args streams the call arguments live.
+ // tool_start/end carry full input/output; tool_output streams incremental stdout and tool_args
+ // streams the call arguments live.
if (
"type" in parsed &&
(parsed.type === "tool_start" ||
@@ -1741,8 +1683,9 @@ export async function* streamChatCompletions(
}
}
} finally {
- // Only abort on an early/abnormal exit: after a natural [DONE] (or server EOF) the request is logically complete and the
- // backend finalizes its api-monitor entry, so cancelling here can mark a successful request as cancelled.
+ // Only abort on an early/abnormal exit: after a natural [DONE] the request is logically complete
+ // and the backend finalizes its api-monitor entry, so cancelling here can mark a successful
+ // request as cancelled.
if (!completed) {
try {
await reader.cancel();
diff --git a/studio/frontend/src/features/chat/api/chat-generation-api.ts b/studio/frontend/src/features/chat/api/chat-generation-api.ts
index 1a7cc416478..a729f09be80 100644
--- a/studio/frontend/src/features/chat/api/chat-generation-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-generation-api.ts
@@ -79,8 +79,8 @@ const TERMINAL_STATUSES = new Set<ChatGenerationStatus>([
"failed",
]);
-/** The follower gave up on a run that stopped making progress. Distinct from the
- * caller's Stop: the backend may still be generating, so the reply is incomplete. */
+/** The follower gave up on a run that stopped making progress. Distinct from the caller's Stop:
+ * the backend may still be generating, so the reply is incomplete. */
export class ChatGenerationStalledError extends Error {
constructor(runId: string) {
super(`Chat generation run ${runId} made no progress`);
@@ -175,15 +175,12 @@ export function isTerminalChatGenerationRun(run: ChatGenerationRun): boolean {
return TERMINAL_STATUSES.has(run.status);
}
-/** Where a Stop has to be sent, given how far durable admission has got.
- *
- * Admission resolves well after the abort listener is installed: the turn first has to
- * auto-load a model, retrieve RAG, upload attachments and save history. A Stop landing
- * in that window has no run id yet and may still end up on the legacy stream, so it
- * needs the `cancel_id` POST, whose server side stashes the cancel for a generation
- * that registers afterwards. That POST is safe once the run does exist too, because the
- * durable request pins `cancel_id` to the same run id.
- */
+/** Where a Stop has to be sent, given how far durable admission has got. Admission resolves well
+ * after the abort listener is installed: the turn first has to auto-load a model, retrieve
+ * RAG, upload attachments and save history. A Stop landing in that window has no run id yet
+ * and may still end up on the legacy stream, so it needs the `cancel_id` POST, whose server
+ * side stashes the cancel for a generation that registers afterwards. That POST is safe once
+ * the run exists too, since the durable request pins `cancel_id` to the same run id. */
export function chatGenerationStopPlan(
decision: "pending" | "durable" | "legacy",
runId: string | null,
@@ -329,13 +326,10 @@ async function* streamChatGenerationEvents(
id: string,
after: number,
signal?: AbortSignal,
- /**
- * Called for each event, and for each keep-alive with that keep-alive's progress
- * stamp. A keep-alive is only progress if the stamp MOVED, which this generator cannot
- * decide: it is re-invoked on every reconnect, so a per-connection memory would treat
- * the first keep-alive after each reconnect as progress and a wedged run behind a
- * flapping proxy would rearm the caller's deadline forever.
- */
+ /** Called for each event, and for each keep-alive with that keep-alive's progress stamp. A
+ * keep-alive is only progress if the stamp MOVED, which this generator cannot decide: it is
+ * re-invoked on every reconnect, so a per-connection memory would treat the first keep-alive
+ * after each reconnect as progress and rearm the caller's deadline forever. */
onActivity?: (keepAliveStamp?: string) => void,
): AsyncGenerator<ChatGenerationEvent> {
const response = await authFetch(
@@ -360,9 +354,8 @@ async function* streamChatGenerationEvents(
const data: string[] = [];
for (const line of block.split("\n")) {
if (line.startsWith("data:")) data.push(line.slice(5).trimStart());
- // Reported, not judged. Whether this counts as progress depends on the last
- // stamp seen across ALL connections for this run, and this generator is
- // re-invoked on every reconnect, so only the caller can hold that.
+ // Reported, not judged. Whether this counts as progress depends on the last stamp seen across
+ // ALL connections for this run, and this generator is re-invoked on every reconnect.
else if (line.startsWith(KEEPALIVE_PREFIX)) {
const stamp = line.slice(KEEPALIVE_PREFIX.length).trim();
if (stamp !== "") onActivity?.(stamp);
@@ -385,18 +378,13 @@ async function* streamChatGenerationEvents(
}
}
-/**
- * How long a follower tolerates a run that makes no progress. Progress, not connectedness,
- * ends a follow: every event and every change to the run row resets it.
- *
- * A deadline on SILENCE rather than duration, which is what lets it stay short while the
- * backend tolerates far longer work. Preparation and admission waits emit no events, but
- * the keep-alive comments carry the run's progress stamp, which the lease renewals move,
- * so a two hour download rearms this while a wedged run does not. Bytes alone are
- * deliberately NOT progress: keep-alives keep arriving for as long as the socket holds, so
- * rearming on them would make this unreachable in exactly the case it exists for, a
- * backend whose sweeper is disabled or broken.
- */
+/** How long a follower tolerates a run that makes no progress. Progress, not connectedness, ends
+ * a follow: every event and every change to the run row resets it. A deadline on SILENCE
+ * rather than duration is what lets it stay short while the backend tolerates far longer
+ * work. Preparation waits emit no events, but the keep-alive comments carry the run's
+ * progress stamp, which the lease renewals move, so a two hour download rearms this while a
+ * wedged run does not. Bytes alone are deliberately NOT progress: keep-alives keep arriving
+ * for as long as the socket holds. */
export const CHAT_GENERATION_STALL_TIMEOUT_MS = 30 * 60_000;
/** Replay from the caller's applied cursor and reconnect until the run is terminal. */
@@ -413,9 +401,9 @@ export async function* followChatGenerationRun(
const { replayFrom } = options;
const stallTimeoutMs =
options.stallTimeoutMs ?? CHAT_GENERATION_STALL_TIMEOUT_MS;
- // The deadline must reach the open stream as well as the sleep between reconnects: a
- // stream that stays open and sends nothing parks the reader just as permanently. One
- // controller downstream of the caller's signal covers both.
+ // The deadline must reach the open stream as well as the sleep between reconnects: a stream
+ // that stays open and sends nothing parks the reader just as permanently. One controller
+ // downstream of the caller's signal covers both.
const deadline = new AbortController();
const callerSignal = options.signal;
const forwardAbort = () => deadline.abort(callerSignal?.reason);
@@ -426,13 +414,12 @@ export async function* followChatGenerationRun(
}
const signal = deadline.signal;
let stallTimer: ReturnType<typeof globalThis.setTimeout> | undefined;
- // Both abort `signal` but must not end the same way: a caller abort is a clean stop,
- // while the deadline means we walked away from a run the backend may still be working
- // on, so it must reach the consumer as a failure rather than a complete reply.
+ // Both abort `signal` but must not end the same way: a caller abort is a clean stop, while the
+ // deadline means we walked away from a run the backend may still be working on, so it must
+ // reach the consumer as a failure rather than a complete reply.
let stalled = false;
let settled = false;
- // Spans reconnects on purpose: see the onActivity contract in
- // streamChatGenerationEvents.
+ // Spans reconnects on purpose: see the onActivity contract in streamChatGenerationEvents.
let lastKeepAliveStamp: string | null = null;
const noteProgress = (keepAliveStamp?: string): void => {
if (keepAliveStamp !== undefined) {
@@ -523,10 +510,9 @@ export async function* followChatGenerationRun(
} finally {
if (stallTimer !== undefined) globalThis.clearTimeout(stallTimer);
callerSignal?.removeEventListener("abort", forwardAbort);
- // Every exit path funnels here, including the two `while (!signal.aborted)` loop
- // conditions, so this is the one place that catches all of them. `settled` keeps a
- // run that reached a terminal status on the same tick as the timer from being
- // reported as stalled.
+ // Every exit path funnels here, including the two `while (!signal.aborted)` loop conditions, so
+ // this is the one place that catches all of them. `settled` keeps a run that reached a
+ // terminal status on the same tick as the timer from being reported as stalled.
if (stalled && !settled) throw new ChatGenerationStalledError(id);
}
}
diff --git a/studio/frontend/src/features/chat/api/chat-settings-api.ts b/studio/frontend/src/features/chat/api/chat-settings-api.ts
index 72cf8eb8842..0418f3f740d 100644
--- a/studio/frontend/src/features/chat/api/chat-settings-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-settings-api.ts
@@ -120,9 +120,9 @@ function parseErrorText(status: number, body: unknown): string {
async function parseJsonOrThrow<T>(response: Response): Promise<T> {
const body = await response.json().catch(() => null);
if (!response.ok) {
- // Typed, not a bare Error: the settings queue has to tell a server that is
- // down (keep the patch) from a server that refuses this body (drop the
- // offending fields), and that decision needs the status and the detail.
+ // Typed, not a bare Error: the settings queue has to tell a server that is down (keep the patch)
+ // from a server that refuses this body (drop the offending fields), and that decision needs the
+ // status and the detail.
throw new ChatSettingsRequestError(
parseErrorText(response.status, body),
response.status,
@@ -149,13 +149,11 @@ export async function saveChatSettingsPatch(
method: "PUT",
headers: { "Content-Type": "application/json" },
body,
- // keepalive lets the PUT survive a tab close from the page-hidden flush,
- // but only under the Fetch standard's 64 KiB budget for in-flight keepalive
- // bodies. Over it the request fails immediately in every engine (measured in
- // Chromium, Firefox and WebKit), and researchWebsitePolicy alone can carry
- // 2000 domains of 253 characters. Sending without keepalive is a chance
- // rather than a certain failure, and on the visibilitychange flush -- where
- // the page is only hidden, not closing -- it simply succeeds.
+ // keepalive lets the PUT survive a tab close from the page-hidden flush, but only under the Fetch
+ // standard's 64 KiB budget for in-flight keepalive bodies. Over it the request fails immediately
+ // in every engine, and researchWebsitePolicy alone can carry 2000 domains of 253 characters.
+ // Sending without keepalive is a chance rather than a certain failure, and on the
+ // visibilitychange flush, where the page is only hidden, it simply succeeds.
keepalive: options.keepalive ? isUnderKeepaliveBudget(body) : undefined,
});
const data = await parseJsonOrThrow<ChatSettingsResponse>(response);
diff --git a/studio/frontend/src/features/chat/api/code-tool-placement.ts b/studio/frontend/src/features/chat/api/code-tool-placement.ts
index 3d24e078785..acf14b851f2 100644
--- a/studio/frontend/src/features/chat/api/code-tool-placement.ts
+++ b/studio/frontend/src/features/chat/api/code-tool-placement.ts
@@ -1,29 +1,15 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Which side of the connection the Code pill runs code on.
- *
- * `code_execution` runs in the provider's own sandbox and is billed by them;
- * `python` / `terminal` are Unsloth's tools and run on the machine Unsloth is
- * installed on. They are not two implementations of one feature, they are two
- * different trust boundaries, so which one a stored toggle means must not
- * change underneath the user.
- *
- * That is what made this a rule rather than a line in the adapter. Until
- * Unsloth's tool loop reached the general external providers, only openai_codex
- * carried studio_tools, so the pill on an OpenAI / Anthropic / Gemini
- * connection always resolved to the provider's sandbox. Now those providers
- * take the Unsloth branch, and the same persisted `true` would resolve to local
- * execution instead -- a relocation nobody asked for, announced nowhere.
- *
- * The rule: a connection with its own sandbox keeps it. A model on such a
- * connection that cannot use it runs nothing, exactly as before the loop
- * existed, rather than falling back to the user's machine. Unsloth's local tools
- * are for connections that have no sandbox at all -- the self-hosted presets,
- * and the cloud providers that ship none, which is what openai_codex has always
- * done, with tool cards and the permission gate to show it.
- */
+/** Which side of the connection the Code pill runs code on. `code_execution` runs in the
+ * provider's own sandbox and is billed by them; `python` / `terminal` are Unsloth's tools and
+ * run on the machine Unsloth is installed on. They are two different trust boundaries, so which
+ * one a stored toggle means must not change underneath the user. Until Unsloth's tool loop
+ * reached the general external providers, only openai_codex carried studio_tools, so the pill on
+ * an OpenAI or Anthropic connection always resolved to the provider's sandbox; now the same
+ * persisted `true` would resolve to local execution. The rule: a connection with its own sandbox
+ * keeps it, and a model there that cannot use it runs nothing rather than falling back to the
+ * user's machine. Unsloth's local tools are for connections with no sandbox at all. */
export interface CodeToolPlacementInput {
/** The composer's Code pill (persisted as unsloth_chat_code_tools_enabled). */
@@ -45,21 +31,16 @@ export function selectCodeToolNames(input: CodeToolPlacementInput): CodeToolName
if (!input.codeToolsEnabled) return { local: [], hosted: [] };
if (input.hostedCodeExecutionForThisTurn) return { local: [], hosted: ["code_execution"] };
if (input.providerHostsCodeExecution) return { local: [], hosted: [] };
- // edit_file is local-only: when the provider hosts execution the files live
- // in its sandbox, so a local editor would patch a copy nothing else sees.
+ // edit_file is local-only: when the provider hosts execution the files live in its sandbox, so a
+ // local editor would patch a copy nothing else sees.
return { local: ["python", "terminal", "edit_file"], hosted: [] };
}
-/**
- * Whether the Code pill can do anything on this connection, so the composer can
- * offer it and a stored preference can be restored.
- *
- * Read out of the placement itself rather than restated, because the two must
- * not drift: a pill offered where the placement runs nothing is a toggle the
- * user turns on to no effect and no explanation. That is the case for a model
- * on a sandbox-owning provider that cannot use it, where the rule above
- * deliberately refuses to relocate the work onto the user's machine.
- */
+/** Whether the Code pill can do anything on this connection, so the composer can offer it and a
+ * stored preference can be restored. Read out of the placement itself rather than restated,
+ * because the two must not drift: a pill offered where the placement runs nothing is a toggle
+ * the user turns on to no effect. That is the case for a model on a sandbox-owning provider
+ * that cannot use it. */
export function codeToolCanRun(input: {
hostedCodeExecutionForThisTurn: boolean;
providerHostsCodeExecution: boolean;
diff --git a/studio/frontend/src/features/chat/api/generation-length.ts b/studio/frontend/src/features/chat/api/generation-length.ts
index 1c54da7dba5..fddd78cfb74 100644
--- a/studio/frontend/src/features/chat/api/generation-length.ts
+++ b/studio/frontend/src/features/chat/api/generation-length.ts
@@ -1,21 +1,14 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Whether Max Tokens, rather than the context window, is what stopped this generation.
- *
- * Two different walls produce the same `finish_reason: "length"`, and only one of them has
- * a setting the user can raise. Comparing the cap to the whole window answers the first
- * question and not the second: with a 4096-token window, a 3000-token prompt and Max
- * Tokens 2048, generation stops after roughly 1096 tokens -- well short of the cap -- and
- * "increase Max Tokens" is then advice that cannot create any room at all.
- *
- * With Max Tokens on "Max" the backend sends the whole context length, so a cap equal to
- * it is indistinguishable from unset here, and both mean the same thing to the user.
- *
- * `promptTokens` is the server's own count from the final usage chunk. Absent, the cap
- * alone decides, which is what this did before the prompt was available.
- */
+/** Whether Max Tokens, rather than the context window, is what stopped this generation. Two
+ * different walls produce the same `finish_reason: "length"`, and only one has a setting the user
+ * can raise. Comparing the cap to the whole window answers the wrong question: with a 4096-token
+ * window, a 3000-token prompt and Max Tokens 2048, generation stops after roughly 1096 tokens,
+ * well short of the cap, and "increase Max Tokens" cannot create any room. With Max Tokens on
+ * "Max" the backend sends the whole context length, so a cap equal to it is indistinguishable
+ * from unset. `promptTokens` is the server's own count from the final usage chunk; absent, the
+ * cap alone decides. */
export function maxTokensIsTheLimit({
cap,
contextLength,
@@ -32,8 +25,7 @@ export function maxTokensIsTheLimit({
if (promptTokens === null) {
return true;
}
- // Strictly below, not at. At equality the cap and the physical context wall are hit
- // in the same token, so raising Max Tokens creates no room and the advice to do so is
- // the one thing that cannot help. The context length is the lever there.
+ // Strictly below, not at. At equality the cap and the physical context wall are hit in the same
+ // token, so raising Max Tokens creates no room. The context length is the lever there.
return promptTokens + cap < window;
}
diff --git a/studio/frontend/src/features/chat/api/gguf-variants-request.ts b/studio/frontend/src/features/chat/api/gguf-variants-request.ts
index 397a77e7804..f3b87e55a48 100644
--- a/studio/frontend/src/features/chat/api/gguf-variants-request.ts
+++ b/studio/frontend/src/features/chat/api/gguf-variants-request.ts
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// How the chat side asks for a repo's GGUF quants. Split out of chat-api so it imports
-// without the auth barrel. The picker's expander and chat auto-load both block on this
-// listing, so an unbounded request leaves the expander on "Loading variants…" forever.
+// How the chat side asks for a repo's GGUF quants. Split out of chat-api so it imports without
+// the auth barrel. The picker's expander and chat auto-load both block on this listing, so an
+// unbounded request leaves the expander on "Loading variants" forever.
import {
type PollSignal,
@@ -21,10 +21,8 @@ export interface GgufVariantsRequestOptions {
signal?: AbortSignal;
}
-/**
- * Query for GET /api/models/gguf-variants. A Hub already known to be unreachable is asked
- * for the cached answer instead of a remote listing that cannot arrive, as the Hub client does.
- */
+/** Query for GET /api/models/gguf-variants. A Hub already known to be unreachable is asked for the
+ * cached answer instead of a remote listing that cannot arrive, as the Hub client does. */
export function ggufVariantsQuery(
repoId: string,
options: GgufVariantsRequestOptions | undefined,
@@ -44,21 +42,17 @@ export function ggufVariantsQuery(
return params;
}
-/**
- * Signal every variant request carries: the caller's abort (the expander drops the request
- * when its row collapses) folded with the timeout. Callers MUST dispose once settled.
- */
+/** Signal every variant request carries: the caller's abort (the expander drops the request when
+ * its row collapses) folded with the timeout. Callers MUST dispose once settled. */
export function ggufVariantsAbort(signal?: AbortSignal): PollSignal {
return signal
? pollSignal(signal, GGUF_VARIANTS_TIMEOUT_MS)
: disposableTimeoutSignal(GGUF_VARIANTS_TIMEOUT_MS);
}
-/**
- * Runs a listing under the bound and settles on it whatever the request is doing. Handing
- * the signal to fetch alone is not enough: on a 401 authFetch awaits a shared session
- * refresh that carries no signal, so the listing could still hang there.
- */
+/** Runs a listing under the bound and settles on it whatever the request is doing. Handing the
+ * signal to fetch alone is not enough: on a 401 authFetch awaits a shared session refresh that
+ * carries no signal, so the listing could still hang there. */
export function runBoundedVariantsRequest<T>(
signal: AbortSignal | undefined,
request: (signal: AbortSignal) => Promise<T>,
diff --git a/studio/frontend/src/features/chat/api/mcp-server-mutation-tracker.ts b/studio/frontend/src/features/chat/api/mcp-server-mutation-tracker.ts
index 031f40e7c0f..8e2773750a0 100644
--- a/studio/frontend/src/features/chat/api/mcp-server-mutation-tracker.ts
+++ b/studio/frontend/src/features/chat/api/mcp-server-mutation-tracker.ts
@@ -17,9 +17,9 @@ export function subscribeToMcpServerMutationSettlements(
}
export function trackMcpServerMutation<T>(mutation: Promise<T>): Promise<T> {
- // The returned operation keeps its original result/rejection for its caller.
- // The internal settlement promise always fulfills so background waiters never
- // create an additional unhandled rejection when a component unmounts.
+ // The returned operation keeps its original result or rejection for its caller. The internal
+ // settlement promise always fulfills so background waiters never create an additional unhandled
+ // rejection when a component unmounts.
mcpServerMutationEpoch += 1;
const settlement = mutation.then(
() => undefined,
@@ -44,9 +44,8 @@ export function trackMcpServerMutation<T>(mutation: Promise<T>): Promise<T> {
export async function readMcpServerMutationSnapshot<T>(
read: () => Promise<T>,
): Promise<T> {
- // Settlement-driven consumers need the newest stable server snapshot even
- // if an unrelated, older mutation is still pending. Retry only when a
- // mutation starts or settles across the read itself.
+ // Settlement-driven consumers need the newest stable server snapshot even if an unrelated, older
+ // mutation is still pending. Retry only when a mutation starts or settles across the read itself.
while (true) {
const epochBeforeRead = mcpServerMutationEpoch;
try {
@@ -59,8 +58,8 @@ export async function readMcpServerMutationSnapshot<T>(
}
export async function waitForPendingMcpServerMutations(): Promise<void> {
- // A mutation may begin while an earlier batch is settling. Re-snapshot until
- // the module-level set is empty so an open-time refresh cannot miss it.
+ // A mutation may begin while an earlier batch is settling. Re-snapshot until the module-level set
+ // is empty so an open-time refresh cannot miss it.
while (pendingMcpServerMutations.size > 0) {
await Promise.all([...pendingMcpServerMutations]);
}
diff --git a/studio/frontend/src/features/chat/api/mcp-servers-api.ts b/studio/frontend/src/features/chat/api/mcp-servers-api.ts
index 5d7ad240833..23ca91dd46e 100644
--- a/studio/frontend/src/features/chat/api/mcp-servers-api.ts
+++ b/studio/frontend/src/features/chat/api/mcp-servers-api.ts
@@ -210,9 +210,8 @@ export function encodeMcpStdioCommand(payload: McpStdioCommand): Promise<{
});
}
-// Bulk-import servers from a standard mcpServers JSON config (Claude Desktop,
-// Cursor, Cline, VS Code). The backend skips duplicates and reports per-entry
-// errors instead of failing the whole batch.
+// Bulk-import servers from a standard mcpServers JSON config (Claude Desktop, Cursor, Cline, VS
+// Code). The backend skips duplicates and reports per-entry errors instead of failing the batch.
export function importMcpServers(
config: unknown,
): Promise<McpServerImportResult> {
diff --git a/studio/frontend/src/features/chat/api/openai-containers.ts b/studio/frontend/src/features/chat/api/openai-containers.ts
index 96eb7b95fa7..1a92026de8d 100644
--- a/studio/frontend/src/features/chat/api/openai-containers.ts
+++ b/studio/frontend/src/features/chat/api/openai-containers.ts
@@ -1,12 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Wrappers for the backend's three OpenAI shell-tool container endpoints
- * (studio/backend/routes/inference.py). Each proxies to OpenAI's
- * /v1/containers using a saved provider key or encrypted request override. Backend rejects any
- * base URL but api.openai.com (the shell tool is managed-cloud only).
- */
+/** Wrappers for the backend's three OpenAI shell-tool container endpoints (routes/inference.py).
+ * Each proxies to OpenAI's /v1/containers using a saved provider key or encrypted request
+ * override. The backend rejects any base URL but api.openai.com. */
import { authFetch } from "@/features/auth";
import { readFastApiError } from "@/lib/format-fastapi-error";
@@ -113,8 +110,8 @@ export async function deleteOpenAIContainer(
}),
},
);
- // 404 = container already gone; treat as idempotent success so a stale
- // list entry doesn't surface as a confusing error.
+ // 404 means the container is already gone; treat as idempotent success so a stale list entry does
+ // not surface as a confusing error.
if (!response.ok && response.status !== 204 && response.status !== 404) {
throw new Error(await parseError(response));
}
diff --git a/studio/frontend/src/features/chat/api/padded-response.ts b/studio/frontend/src/features/chat/api/padded-response.ts
index b21e4083235..0d0d88f593c 100644
--- a/studio/frontend/src/features/chat/api/padded-response.ts
+++ b/studio/frontend/src/features/chat/api/padded-response.ts
@@ -1,15 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * `/api/inference/load` and `/unload` pad their body so a proxy cannot time the request
- * out, committing the 200 before the work finishes (`_tunnel_safe_json` in
- * studio/backend/routes/inference.py). A proxy giving up mid-pad leaves a 200 with an
- * empty or truncated body; accepting that reports an unfinished load as done. Only
- * these two routes commit that early, so only they require a payload; elsewhere an
- * empty body is by design. Mirrored by `require_completed_padded_body` in
- * unsloth_cli/_inference.py.
- */
+/** `/api/inference/load` and `/unload` pad their body so a proxy cannot time the request out,
+ * committing the 200 before the work finishes (`_tunnel_safe_json` in routes/inference.py). A
+ * proxy giving up mid-pad leaves a 200 with an empty or truncated body, and accepting that
+ * reports an unfinished load as done. Only these two routes commit that early, so only they
+ * require a payload. Mirrored by `require_completed_padded_body` in unsloth_cli/_inference.py. */
export function assertCompletedPaddedBody(body: unknown, label: string): void {
const complete =
typeof body === "object" &&
diff --git a/studio/frontend/src/features/chat/api/providers-api.ts b/studio/frontend/src/features/chat/api/providers-api.ts
index 9b5fd69c8ad..e5d474117ac 100644
--- a/studio/frontend/src/features/chat/api/providers-api.ts
+++ b/studio/frontend/src/features/chat/api/providers-api.ts
@@ -137,10 +137,9 @@ export async function encryptProviderApiKey(
}
export async function listProviderRegistry(): Promise<ProviderRegistryEntry[]> {
- // include_hidden asks for the backend-only entries (the self-hosted presets),
- // which carry the studio-tools capability the composer gates on. An older
- // backend ignores the parameter and returns the visible entries, so the
- // capability simply reads as unknown and the pills stay closed.
+ // include_hidden asks for the backend-only entries (the self-hosted presets), which carry the
+ // studio-tools capability the composer gates on. An older backend ignores the parameter and
+ // returns the visible entries, so the capability reads as unknown and the pills stay closed.
const response = await authFetch("/api/providers/registry?include_hidden=true");
return parseJsonOrThrow<ProviderRegistryEntry[]>(response);
}
@@ -183,9 +182,8 @@ export async function deleteProviderConfig(providerId: string): Promise<void> {
const response = await authFetch(`/api/providers/${providerId}`, {
method: "DELETE",
});
- // Treat 404 as success: another tab already deleted this provider, so pruning
- // the stale cache is correct. Otherwise the caller throws and the user is stuck
- // with an entry they cannot remove from the UI.
+ // Treat 404 as success: another tab already deleted this provider, so pruning the stale cache is
+ // correct. Otherwise the caller throws and the user is stuck with an entry they cannot remove.
if (response.status === 404) {
return;
}
@@ -318,11 +316,11 @@ export async function listProviderModels(payload: {
export interface CodexSubscriptionModels {
models: ProviderModelInfo[];
- /** Every model the plan returned, offered or not: absent from this means the account
- * cannot reach it, while present-but-unoffered only means it is no longer shown. */
+ /** Every model the plan returned, offered or not: absent from this means the account cannot reach
+ * it, while present-but-unoffered only means it is no longer shown. */
known?: ProviderModelInfo[];
- /** "reauthorization_required" is a curated answer that also says the connection has
- * to be reconnected: the picker must not treat it as the plan's catalog. */
+ /** "reauthorization_required" is a curated answer that also says the connection has to be
+ * reconnected: the picker must not treat it as the plan's catalog. */
source: "subscription" | "curated" | "reauthorization_required";
}
diff --git a/studio/frontend/src/features/chat/api/research-api.ts b/studio/frontend/src/features/chat/api/research-api.ts
index 1b4b8938c5d..05f7fdb82e5 100644
--- a/studio/frontend/src/features/chat/api/research-api.ts
+++ b/studio/frontend/src/features/chat/api/research-api.ts
@@ -285,12 +285,11 @@ export async function* followResearchRun(
try {
for await (const event of streamResearchEvents(id, cursor, signal)) {
cursor = Math.max(cursor, event.id);
- // Delta-only events carry no run by design (`_DELTA_ONLY_EVENTS` in
- // routes/research_runs.py), so reuse the run we hold: report/reasoning deltas arrive
- // ~12x/s for the whole synthesis, and a fresh object per event re-renders every
- // subscriber that selects the run. The cursor lives in `session.lastAppliedSeq`, and a
- // terminal status - all `isSettledResearchRun` reads `lastEventSeq` for - comes with a
- // snapshot.
+ // Delta-only events carry no run by design (`_DELTA_ONLY_EVENTS` in routes/research_runs.py), so
+ // reuse the run we hold: report and reasoning deltas arrive ~12x/s for the whole synthesis, and
+ // a fresh object per event re-renders every subscriber that selects the run. The cursor lives in
+ // `session.lastAppliedSeq`, and a terminal status comes with a snapshot.
+ // A terminal status, which is all isSettledResearchRun reads lastEventSeq for, comes with a snapshot.
const eventRun: ResearchRun = event.run ?? currentRun;
const hydratedEvent: ResearchEvent = {
...event,
diff --git a/studio/frontend/src/features/chat/artifacts/artifact-card.tsx b/studio/frontend/src/features/chat/artifacts/artifact-card.tsx
index 531b17522de..d5902e7e8f0 100644
--- a/studio/frontend/src/features/chat/artifacts/artifact-card.tsx
+++ b/studio/frontend/src/features/chat/artifacts/artifact-card.tsx
@@ -45,8 +45,8 @@ export function ArtifactCard({
isStreaming?: boolean;
}) {
const activeThreadId = useChatRuntimeStore((state) => state.activeThreadId);
- // Canvas mode collapses the raw code in place, so offer a Code button too.
- // Diffusion keeps its code inline, so it needs no Code button.
+ // Canvas mode collapses the raw code in place, so offer a Code button too. Diffusion keeps its
+ // code inline, so it needs no Code button.
const showCodeButton = useChatRuntimeStore(
(state) => state.artifactsEnabled && !state.loadedIsDiffusion,
);
diff --git a/studio/frontend/src/features/chat/artifacts/artifact-surface.tsx b/studio/frontend/src/features/chat/artifacts/artifact-surface.tsx
index a4edc258371..ec9589f7b18 100644
--- a/studio/frontend/src/features/chat/artifacts/artifact-surface.tsx
+++ b/studio/frontend/src/features/chat/artifacts/artifact-surface.tsx
@@ -45,9 +45,9 @@ function buildHtmlFence(source: string): string {
const fence = "`".repeat(longestBacktickRun + 1);
return `${fence}html\n${source}\n${fence}`;
}
-// Sandboxed canvas iframes are deliberately outside the overlay focus trap:
-// granting same-origin sandbox privileges would weaken isolation, so reaching
-// interactive canvas content via keyboard is a known sandbox limitation.
+// Sandboxed canvas iframes are deliberately outside the overlay focus trap: granting same-origin
+// sandbox privileges would weaken isolation, so reaching interactive canvas content via keyboard
+// is a known sandbox limitation.
const FOCUSABLE_SELECTOR =
'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])';
@@ -199,10 +199,9 @@ export function ArtifactSurface({
? "artifact-panel-shell mx-2 mb-8 overflow-visible rounded-[28px] border-t border-border/70 bg-card/95"
: "h-[min(92dvh,900px)] w-[min(96vw,1200px)] overflow-hidden rounded-2xl border border-border shadow-xl",
)}
- // The chat-model notice is an absolute child of the chat content container, so
- // it spans this column too, not just the thread pane. Its height is 0 whenever
- // it is not on screen, which leaves the geometry this panel has always had.
- // Both edges move, or the panel keeps its height and overflows the bottom.
+ // The chat-model notice is an absolute child of the chat content container, so it spans this
+ // column too, not just the thread pane. Its height is 0 whenever it is off screen, which leaves
+ // the geometry this panel has always had. Both edges move, or the panel overflows the bottom.
style={
variant === "panel"
? {
@@ -269,8 +268,8 @@ export function ArtifactSurface({
className="size-8"
disabled={isLoadingArtifact || !hasArtifactCode}
onClick={() => {
- // Route through the native save dialog on desktop; the plain
- // blob-anchor download is silently dropped by the Tauri WebView2.
+ // Route through the native save dialog on desktop; the plain blob-anchor download is silently
+ // dropped by the Tauri WebView2.
void downloadFile(
artifact.code,
filename,
diff --git a/studio/frontend/src/features/chat/artifacts/html-fences.ts b/studio/frontend/src/features/chat/artifacts/html-fences.ts
index 5398cf1add6..38fdee9fd10 100644
--- a/studio/frontend/src/features/chat/artifacts/html-fences.ts
+++ b/studio/frontend/src/features/chat/artifacts/html-fences.ts
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Shared fenced-code helpers for the HTML-artifact render paths, hoisted from
-// markdown-text.tsx so the in-place collapse and the post-message auto-render
-// agree on what counts as a renderable HTML fence.
+// Shared fenced-code helpers for the HTML-artifact render paths, hoisted from markdown-text.tsx
+// so the in-place collapse and the post-message auto-render agree on what counts as a renderable
+// HTML fence.
export type CodeFence = {
language: string | null;
@@ -79,8 +79,8 @@ export function isFullHtmlDocument(source: string): boolean {
export interface HtmlFence {
source: string;
isFullDocument: boolean;
- // Plain 3-backtick unindented fence: the only form the in-place collapser
- // (CODE_FENCE_RE) recognizes, so only these may be skipped as already shown.
+ // Plain 3-backtick unindented fence: the only form the in-place collapser (CODE_FENCE_RE)
+ // recognizes, so only these may be skipped as already shown.
isPlainFence: boolean;
index: number;
}
@@ -88,9 +88,9 @@ export interface HtmlFence {
// Opening fence: up to 3 leading spaces, >=3 backticks, then a backtick-free info string.
const FENCE_OPEN_RE = /^( {0,3})(`{3,})([^`\r\n]*)$/;
-// Scan a full message for every closed ```html fence. Line-based so multiple
-// fences are found and backticks inside a <script> string never split a block
-// (a close must be its own fence line). Drops unterminated/SVG fences.
+// Scan a full message for every closed ```html fence. Line-based so multiple fences are found and
+// backticks inside a <script> string never split a block (a close must be its own fence line).
+// Drops unterminated and SVG fences.
export function extractHtmlFences(text: string): HtmlFence[] {
const lines = text.split(/\r?\n/);
const fences: HtmlFence[] = [];
diff --git a/studio/frontend/src/features/chat/artifacts/html-frame.tsx b/studio/frontend/src/features/chat/artifacts/html-frame.tsx
index 66cda923424..c15e6b1eaac 100644
--- a/studio/frontend/src/features/chat/artifacts/html-frame.tsx
+++ b/studio/frontend/src/features/chat/artifacts/html-frame.tsx
@@ -30,32 +30,29 @@ import { hashArtifactCode } from "./types";
const HTML_FRAME_DEFAULT_HEIGHT = 400;
const HTML_FRAME_MAX_HEIGHT = 900;
const BLOCKED_HOSTS_SHOWN = 3;
-// Entries are per URL, not per host, so a page pulling a whole CDN directory
-// counts each file. Still far above what a real one trips.
+// Entries are per URL, not per host, so a page pulling a whole CDN directory counts each file.
+// Still far above what a real one trips.
const BLOCKED_URIS_TRACKED = 100;
-// A report carries the full URL, so this is generous next to a real one, and it
-// bounds both the stored string and the host derived from it.
+// A report carries the full URL, so this is generous next to a real one, and it bounds both
+// the stored string and the host derived from it.
const BLOCKED_URI_MAX_CHARS = 2048;
// The only directives the network CSP leaves at 'none'. Kept in step by
-// test_the_grant_widens_everything_but_the_locked_directives, which fails if
-// the two policies stop differing exactly outside this set.
+// test_the_grant_widens_everything_but_the_locked_directives.
const GRANT_CANNOT_FIX = new Set(["object-src", "base-uri", "form-action"]);
-// A hostless blockedURI is a bare scheme, and the permissive policy widens every
-// one of them but this: its worker-src is `http: https: blob:`, with no data:,
-// so a data: Worker stays blocked after the grant. Kept in step by
-// test_the_permissive_policy_widens_every_hostless_scheme_but_one.
+// A hostless blockedURI is a bare scheme, and the permissive policy widens every one but this:
+// its worker-src is `http: https: blob:` with no data:, so a data: Worker stays blocked
+// after the grant. Kept in step by test_the_permissive_policy_widens_every_hostless_scheme_but_one.
const GRANT_CANNOT_FIX_SCHEME: Record<string, string> = { "worker-src": "data" };
type BlockedState = { code: string; uris: string[]; hosts: string[] };
const NOTHING_BLOCKED: BlockedState = { code: "", uris: [], hosts: [] };
-// Reports from before a swap belong to the old canvas, so start over rather
-// than append. The cap is checked BEFORE the duplicate scan, so past it a
-// canvas posting unique URIs cannot make the parent rescan every stored string;
-// returning `current` unchanged lets React bail out of the re-render too.
+// Reports from before a swap belong to the old canvas, so start over rather than append. The
+// cap is checked BEFORE the duplicate scan, so past it a canvas posting unique URIs cannot
+// make the parent rescan every stored string; returning `current` also lets React bail out.
function appendBlocked(
current: BlockedState,
code: string,
@@ -73,9 +70,9 @@ function appendBlocked(
};
}
-// A non-HTTP(S) violation reports a bare token ("eval", "blob"), which the
-// permissive CSP widens too. Dropping those left the canvas blank with no
-// prompt, so label them with the token itself.
+// A non-HTTP(S) violation reports a bare token ("eval", "blob"), which the permissive CSP
+// widens too. Dropping those left the canvas blank with no prompt, so label them with the
+// token itself.
const BLOCKED_KEYWORD = /^[a-z-]+$/;
function blockedHost(uri: string): string | null {
@@ -102,9 +99,8 @@ export function buildArtifactSrcDoc(code: string): string {
return `${code}\n${resizeScript}`;
}
-// Preview iframes intentionally omit allow-downloads: generated canvases can
-// offer their own UI, but downloads must go through Unsloth's explicit
-// copy/download controls outside the no-same-origin sandbox.
+// Preview iframes intentionally omit allow-downloads: generated canvases can offer their own
+// UI, but downloads must go through Unsloth's explicit controls outside the sandbox.
export function ArtifactHtmlFrame({
code,
title = "HTML canvas preview",
@@ -121,26 +117,25 @@ export function ArtifactHtmlFrame({
const t = useT();
const locale = useLocale();
const iframeRef = useRef<HTMLIFrameElement>(null);
- // Every canvas honors this, fence or tool. Off by default; the standing half
- // of the gate, alongside the per-canvas grant below.
+ // Every canvas honors this, fence or tool. Off by default; the standing half of the gate,
+ // alongside the per-canvas grant below.
const networkAccessEnabled = useChatRuntimeStore(
(state) => state.allowArtifactNetworkAccess,
);
const [height, setHeight] = useState(HTML_FRAME_DEFAULT_HEIGHT);
- // Carries the code it was reported for, so a canvas swapped in place cannot
- // inherit the previous one's banner. Clearing it from the [src] effect ran a
- // render too late, and that stale render is the one carrying the button.
+ // Carries the code it was reported for, so a canvas swapped in place cannot inherit the
+ // previous one's banner. Clearing it from the [src] effect ran a render too late, and that
+ // stale render is the one carrying the button.
const [blocked, setBlocked] = useState<BlockedState>({
code,
uris: [],
hosts: [],
});
const blockedForCanvas = blocked.code === code ? blocked : NOTHING_BLOCKED;
- // Granted by the banner button alone, and only for the code on screen when it
- // was clicked; nothing the canvas sends may set it, or a blocked page could
- // talk its way onto the network. Compared during render rather than reset in
- // an effect, which runs after the DOM is updated and so would let the first
- // render carrying new code reuse the previous canvas' allow_network=1.
+ // Granted by the banner button alone, and only for the code on screen when it was clicked;
+ // nothing the canvas sends may set it, or a blocked page could talk its way onto the
+ // network. Compared during render rather than reset in an effect, which runs after the DOM
+ // is updated and would let the first render carrying new code reuse allow_network=1.
const [grantedCode, setGrantedCode] = useState<string | null>(null);
const grantedForCanvas = grantedCode === code;
const networkAllowed = networkAccessEnabled || grantedForCanvas;
@@ -165,8 +160,8 @@ export function ArtifactHtmlFrame({
const postArtifactHtml = useCallback(() => {
if (!pendingPostRef.current) return;
pendingPostRef.current = false;
- // Sandboxed frame has an opaque origin ("null"), so a wildcard target is
- // required; the payload only reaches this iframe's contentWindow.
+ // Sandboxed frame has an opaque origin ("null"), so a wildcard target is required; the payload
+ // only reaches this iframe's contentWindow.
iframeRef.current?.contentWindow?.postMessage(
{ type: "unsloth:artifact-html", html: artifactHtml },
"*",
@@ -178,23 +173,21 @@ export function ArtifactHtmlFrame({
if (event.source !== iframeRef.current?.contentWindow) return;
if (event.origin !== "null") return;
if (event.data?.type === "unsloth:artifact-blocked") {
- // event.source survives the swap navigation, so without the frame's
- // stamp a report from the outgoing canvas would be tagged with the
- // incoming code and prompt a grant for a canvas that never hit the CSP.
+ // event.source survives the swap navigation, so without the frame's stamp a report from the
+ // outgoing canvas would be tagged with the incoming code and prompt a needless grant.
if (event.data.v !== codeVersion) return;
const uri = event.data.blockedURI;
- // A report carries the full URL, and the canvas can post these directly
- // rather than going through the CSP. The entry cap bounds how many are
- // kept but not their size, so a handful could park megabytes otherwise.
+ // A report carries the full URL, and the canvas can post these directly rather than going
+ // through the CSP. The entry cap bounds how many are kept but not their size, so a handful
+ // could park megabytes otherwise.
if (typeof uri !== "string" || uri.length > BLOCKED_URI_MAX_CHARS) {
return;
}
- // The grant cannot fix these three, and prompting anyway widens the
- // policy for nothing, then hides the banner because the grant is on,
- // leaving a broken canvas and no way back to the prompt.
+ // The grant cannot fix these three, and prompting anyway widens the policy for nothing, then
+ // hides the banner because the grant is on, leaving a broken canvas and no way back.
if (GRANT_CANNOT_FIX.has(event.data.effectiveDirective)) return;
- // Same dead end one scheme down: the grant widens worker-src to blob:
- // but not data:, so a data: Worker reports under both policies.
+ // Same dead end one scheme down: the grant widens worker-src to blob: but not data:, so a
+ // data: Worker reports under both policies.
if (GRANT_CANNOT_FIX_SCHEME[event.data.effectiveDirective] === uri) {
return;
}
@@ -213,8 +206,8 @@ export function ArtifactHtmlFrame({
};
window.addEventListener("message", handler);
return () => window.removeEventListener("message", handler);
- // `code`/`codeVersion` are listed so the handler always closes over the
- // canvas on screen, rather than relying on postArtifactHtml changing.
+ // `code`/`codeVersion` are listed so the handler always closes over the canvas on screen,
+ // rather than relying on postArtifactHtml changing.
}, [postArtifactHtml, code, codeVersion]);
const showBlockedBanner =
diff --git a/studio/frontend/src/features/chat/artifacts/types.ts b/studio/frontend/src/features/chat/artifacts/types.ts
index e3cd4ab82e6..c1957d37d01 100644
--- a/studio/frontend/src/features/chat/artifacts/types.ts
+++ b/studio/frontend/src/features/chat/artifacts/types.ts
@@ -41,9 +41,9 @@ export function hashArtifactCode(code: string): string {
return (hash >>> 0).toString(36);
}
-// The canvas source view keys its Streamdown on this. Streamdown memoizes a code
-// fence on its node's line/column span, ignoring the text, so equal-line-count
-// canvases keep the old source. Tool artifact IDs omit the code, so hash it in.
+// The canvas source view keys its Streamdown on this. Streamdown memoizes a code fence on its
+// node's line/column span, ignoring the text, so equal-line-count canvases keep the old source.
+// Tool artifact IDs omit the code, so hash it in.
export function buildArtifactSourceKey(
artifact: Pick<ChatArtifact, "id" | "code">,
): string {
@@ -53,8 +53,8 @@ export function buildArtifactSourceKey(
export function createArtifactId(input: ChatArtifactInput): string {
const threadSegment = input.threadId || "no-thread";
const messageSegment = input.sourceMessageId || "transient";
- // Backend tool call IDs (call_0, call_1, …) reset per request, so the
- // message ID is needed to scope them to a specific turn.
+ // Backend tool call IDs (call_0, call_1, ...) reset per request, so the message ID is needed to
+ // scope them to a specific turn.
const parts = [input.source, threadSegment, messageSegment];
if (input.source === "tool" && input.sourceToolCallId) {
diff --git a/studio/frontend/src/features/chat/attachment-content.ts b/studio/frontend/src/features/chat/attachment-content.ts
index 5f6bb876f80..2c43ff1c054 100644
--- a/studio/frontend/src/features/chat/attachment-content.ts
+++ b/studio/frontend/src/features/chat/attachment-content.ts
@@ -25,8 +25,8 @@ export { TEXT_ATTACHMENT_ACCEPT };
export type AttachmentText = {
label: AttachmentTextLabel | null;
text: string;
- // True when the file was only read up to the preview cap, so the dialog can
- // say so even if the extracted text ends up short.
+ // True when the file was only read up to the preview cap, so the dialog can say so even if
+ // the extracted text ends up short.
truncated: boolean;
};
@@ -44,14 +44,11 @@ const ATTACHMENT_TAG_CLOSE = "\n</attachment>";
const MAX_ATTACHMENT_WRAPPER_LENGTH = 4096;
const DOCX_MIME =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
-// mammoth picks the parts it parses out of the relationships, not out of the
-// filenames, so a target may be called anything ("payload.bin") and still be
-// inflated and parsed as XML, while an .xml part nothing points at (customXml,
-// the glossary document) is never opened at all. The bound therefore follows
-// docx-reader.js: the two package parts it always reads, the main document and
-// its relationships, and the five parts resolved out of those relationships,
-// each with mammoth's own "word/<name>.xml" fallback. Image targets stay lazy
-// and are never read by extractRawText.
+// mammoth picks the parts it parses out of the relationships, not the filenames, so a target
+// may be called anything and still be parsed as XML, while an .xml part nothing points at
+// is never opened. The bound therefore follows docx-reader.js: the two package parts it
+// always reads plus the five resolved out of those relationships, each with mammoth's own
+// "word/<name>.xml" fallback. Image targets stay lazy.
const DOCX_CONTENT_TYPES_PART = "[Content_Types].xml";
const DOCX_PACKAGE_RELATIONSHIPS = "_rels/.rels";
const DOCX_RELATIONSHIP_NAMESPACE =
@@ -64,20 +61,20 @@ const DOCX_RELATED_PART_NAMES = [
"numbering",
"styles",
];
-// readXmlFileWithBody opens the relationships of every part it reads a body
-// from, so these three carry a .rels part of their own.
+// readXmlFileWithBody opens the relationships of every part it reads a body from, so these
+// three carry a .rels part of their own.
const DOCX_BODY_PART_NAMES = new Set(["comments", "endnotes", "footnotes"]);
const DOCX_MAIN_DOCUMENT_FALLBACK = "word/document.xml";
-// A <Relationship> tag, with the element prefix mammoth's namespace mapping
-// accepts, and skipping any ">" that sits inside an attribute value.
+// A <Relationship> tag with the element prefix mammoth's namespace mapping accepts, skipping
+// any ">" that sits inside an attribute value.
const DOCX_RELATIONSHIP_TAG_RE =
/<(?:[^\s/>"'=]+:)?Relationship(?=[\s/>])(?:"[^"]*"|'[^']*'|[^"'>])*>/g;
-// Attribute names are matched by consuming whole name="value" pairs, so a
-// value that itself contains Target= cannot be mistaken for an attribute.
-// mammoth reads child.attributes.Target, which a prefixed r:Target never
-// populates, so prefixed names are deliberately not accepted here.
+// Attribute names are matched by consuming whole name="value" pairs, so a value containing
+// Target= cannot be mistaken for an attribute. mammoth reads child.attributes.Target,
+// which a prefixed r:Target never populates, so prefixed names are not accepted.
const XML_ATTRIBUTE_RE = /([^\s/>"'=]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
-/** Non-element markup: a `<Relationship>` inside a comment, CDATA section or processing instruction is text to mammoth's parser, and each ends at its first delimiter the way XML ends it. */
+/** Non-element markup: a `<Relationship>` inside a comment, CDATA section or processing
+ * instruction is text to mammoth's parser, and each ends at its first delimiter. */
const XML_NON_ELEMENT_RE =
/<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>|<\?[\s\S]*?\?>/g;
const XML_ENTITY_RE = /&(?:#(\d+)|#[xX]([\da-fA-F]+)|([a-zA-Z]+));/g;
@@ -88,16 +85,11 @@ const XML_NAMED_ENTITIES: Record<string, string> = {
quot: '"',
apos: "'",
};
-/**
- * Ceiling on everything a DOCX unpacks to, and the reason mammoth never sees
- * the file the user chose.
- *
- * jszip takes each part's size from the central directory and inflates the part
- * in full before anything can reject it, so an entry declaring 1 KB that
- * expands to 1 GB exhausts the webview. fflate allocates every entry at its
- * declared size and stops there, so mammoth is handed a repack of fflate's
- * output and can only inflate what has already been bounded here.
- */
+/** Ceiling on everything a DOCX unpacks to, and why mammoth never sees the file the user
+ * chose. jszip takes each part's size from the central directory and inflates it in full
+ * before anything can reject it, so an entry declaring 1 KB that expands to 1 GB exhausts
+ * the webview. fflate allocates at the declared size and stops, so mammoth is handed a
+ * repack of fflate's output. */
const MAX_DOCX_UNPACKED_BYTES = 2 * MAX_OPEN_DOCUMENT_ARCHIVE_BYTES;
const AUDIO_EXTENSION_MIMES: Record<string, string> = {
wav: "audio/wav",
@@ -403,15 +395,16 @@ const CODE_ATTACHMENT_LANGUAGES: Record<string, string> = {
zig: "zig",
zsh: "shellscript",
};
-// Long attachments still have to render inside a dialog, so the preview stops
-// well before the point where a single <pre> stalls the webview.
+// Long attachments still render inside a dialog, so the preview stops well before a single
+// <pre> stalls the webview.
const MAX_PREVIEW_TEXT_LENGTH = 200_000;
-// Text and HTML have no size limit at upload, so a preview reads a bounded slice
-// instead of the whole file. Five bytes per character keeps the slice past the
-// character cap for any UTF-8 input, so truncation is still detected.
+// Text and HTML have no upload size limit, so a preview reads a bounded slice. Five bytes
+// per character keeps the slice past the character cap for any UTF-8 input, so truncation
+// is still detected.
const MAX_PREVIEW_TEXT_BYTES = MAX_PREVIEW_TEXT_LENGTH * 5;
-/** Own-property lookup: an extension or entity named "constructor" would otherwise resolve to a member of Object.prototype. */
+/** Own-property lookup: an extension or entity named "constructor" would otherwise resolve to
+ * a member of Object.prototype. */
function lookUp(
table: Record<string, string>,
key: string,
@@ -429,9 +422,8 @@ export function isAudioAttachment(
);
}
-// The audio part keeps only the coarse format the backend needs ("mp3" or
-// "wav"), so the content type wins, then the extension for uploads the browser
-// typed as empty, and the part format only as a last resort.
+// The audio part keeps only the coarse format the backend needs, so the content type wins,
+// then the extension for uploads the browser typed as empty, then the part format.
export function attachmentAudioSrc(
audio: { data: string; format: string },
contentType: string | undefined,
@@ -479,9 +471,9 @@ export function isOpenDocumentAttachment(
);
}
-// CompositeAttachmentAdapter selects the first accept string that matches.
-// Text comes before the document-specific adapters, so previews must apply the
-// same MIME-or-extension match before looking at PDF/DOCX/HTML names.
+// CompositeAttachmentAdapter selects the first matching accept string. Text comes before the
+// document-specific adapters, so previews must apply the same MIME-or-extension match
+// before looking at PDF/DOCX/HTML names.
function isTextAttachment(
name: string,
contentType: string | undefined,
@@ -500,11 +492,10 @@ function isTextAttachment(
});
}
-// unpdf and mammoth decode the whole file on the main thread, so both refuse a
-// document past the ceiling the OpenDocument path already enforces, and refuse
-// it before the read rather than after. The adapters call this from add() as
-// well: the composer clears its text and attachments before it awaits send(),
-// so a throw there loses the typed message along with the file.
+// unpdf and mammoth decode the whole file on the main thread, so both refuse a document past
+// the OpenDocument ceiling, and refuse before the read. The adapters call this from add()
+// too: the composer clears text and attachments before awaiting send(), so a throw there
+// loses the typed message along with the file.
export function getDocumentAttachmentSizeError(
file: File,
label: "PDF" | "DOCX",
@@ -532,8 +523,8 @@ function joinDocxPath(basePath: string, target: string): string {
return joined.startsWith("/") ? joined.slice(1) : joined;
}
-// XML attribute values are entity-decoded by the parser mammoth hands the
-// relationships to, so a target only matches the archive once decoded.
+// XML attribute values are entity-decoded by the parser mammoth uses, so a target only
+// matches the archive once decoded.
function decodeXmlEntities(value: string): string {
return value.replace(XML_ENTITY_RE, (match, decimal, hex, name) => {
const code = decimal
@@ -548,12 +539,10 @@ function decodeXmlEntities(value: string): string {
});
}
-// The relationship parts are XML, but only their targets are needed, so they
-// are scanned rather than parsed: DOMParser is not available where this also
-// runs under test, and a malformed rels file is mammoth's to report. The scan
-// accepts every attribute form mammoth's parser resolves (both quote styles,
-// entity-encoded values, a prefixed element name, ">" inside a value), so a
-// crafted rels file cannot hide a target from the bound below.
+// The relationship parts are XML, but only their targets are needed, so they are scanned
+// rather than parsed: DOMParser is not available under test, and a malformed rels file is
+// mammoth's to report. The scan accepts every attribute form mammoth's parser resolves, so
+// a crafted rels file cannot hide a target from the bound below.
function readDocxXmlTargets(
rels: Uint8Array | undefined,
basePath: string,
@@ -595,19 +584,16 @@ function docxRelationshipsPath(path: string): string {
type DocxArchive = {
entries: Unzipped;
- /** Every name the central directory declares, unpacked entries included, so a target resolves the way findPartPath resolves it. */
+ /** Every name the central directory declares, unpacked entries included, so a target resolves
+ * the way findPartPath resolves it. */
names: Set<string>;
oversized: Set<string>;
};
-/**
- * Inflates the archive under fflate's declared-size allocation.
- *
- * An entry past the XML ceiling is left out rather than refused: mammoth opens
- * the package parts and whatever the relationships point at, so a large
- * unreferenced part, custom XML or an embedded image, must still preview.
- * `assertDocxPartSizes` refuses the ones mammoth would have parsed.
- */
+/** Inflates the archive under fflate's declared-size allocation. An entry past the XML ceiling
+ * is left out rather than refused: mammoth opens the package parts and whatever the
+ * relationships point at, so a large unreferenced part must still preview.
+ * `assertDocxPartSizes` refuses the ones mammoth would have parsed. */
function unpackDocxEntries(filename: string, bytes: Uint8Array): DocxArchive {
const names = new Set<string>();
const oversized = new Set<string>();
@@ -631,15 +617,9 @@ function unpackDocxEntries(filename: string, bytes: Uint8Array): DocxArchive {
return { entries, names, oversized };
}
-/**
- * Refuses the parts mammoth goes on to parse when they exceed the XML ceiling.
- *
- * mammoth takes no entry filter, so the set is resolved the way findPartPaths
- * resolves it: the main document out of "_rels/.rels" and the styles,
- * numbering and note parts out of the document's own .rels, each falling back
- * to a fixed name when no target resolves. Both sides read the same bytes,
- * since mammoth is handed the repack of these entries.
- */
+/** Refuses the parts mammoth goes on to parse when they exceed the XML ceiling. mammoth takes
+ * no entry filter, so the set is resolved as findPartPaths resolves it, each falling back
+ * to a fixed name when no target resolves. Both sides read the same bytes. */
function assertDocxPartSizes(filename: string, archive: DocxArchive): void {
const { entries, names, oversized } = archive;
const bound = (path: string) => {
@@ -647,8 +627,7 @@ function assertDocxPartSizes(filename: string, archive: DocxArchive): void {
throw new Error(`DOCX XML file is too large: ${filename}:${path}`);
}
};
- // findPartPath keeps the first target that exists, and every target it
- // discards is one mammoth never opens.
+ // findPartPath keeps the first target that exists, and every target it discards is one mammoth never opens.
const resolve = (targets: string[] | undefined, fallback: string) =>
targets?.find((path) => names.has(path)) ?? fallback;
@@ -681,7 +660,8 @@ function assertDocxPartSizes(filename: string, archive: DocxArchive): void {
}
}
-/** The archive mammoth is given: fflate's own output, so a part that lies about its size arrives truncated rather than inflated in full. */
+/** The archive mammoth is given: fflate's own output, so a part that lies about its size
+ * arrives truncated rather than inflated in full. */
export function repackDocxAttachmentArchive(
filename: string,
bytes: Uint8Array,
@@ -691,14 +671,9 @@ export function repackDocxAttachmentArchive(
return zipSync(archive.entries, { level: 0 });
}
-/**
- * The bytes of a view, as an ArrayBuffer, without copying when it owns one.
- *
- * jszip reads the whole buffer it is handed and looks for the end-of-directory
- * record at its tail, so a view that does not span its buffer would arrive as a
- * corrupt archive. zipSync happens to return an exact-fit array today, which is
- * not something its API promises.
- */
+/** The bytes of a view, as an ArrayBuffer, without copying when it owns one. jszip reads the
+ * whole buffer and looks for the end-of-directory record at its tail, so a view that does
+ * not span its buffer would arrive as a corrupt archive. */
function toArrayBuffer(view: Uint8Array): ArrayBuffer {
const spansBuffer =
view.byteOffset === 0 && view.byteLength === view.buffer.byteLength;
@@ -717,13 +692,9 @@ function isDocxSizeError(error: unknown): boolean {
);
}
-/**
- * The verdict add() needs before the attachment exists.
- *
- * The composer clears its text and attachments before it awaits send(), so a
- * DOCX that only fails there discards the typed message along with the file.
- * add() calls this instead, the way the audio adapter checks its own ceiling.
- */
+/** The verdict add() needs before the attachment exists. The composer clears its text and
+ * attachments before it awaits send(), so a DOCX that only fails there discards the typed
+ * message along with the file. */
export async function getDocxAttachmentError(
file: File,
): Promise<string | null> {
@@ -782,13 +753,8 @@ export function extractHtmlAttachmentText(html: string): string {
return normalizeExtractedText(collectHtmlBlockText(doc.body));
}
-/**
- * Text with the line structure the source had.
- *
- * `textContent` runs a whole page together, so every block-level element and
- * every `<br>` contributes a break of its own and the inline runs between them
- * are joined as written.
- */
+/** Text with the line structure the source had. `textContent` runs a whole page together, so
+ * every block-level element and every `<br>` contributes a break of its own. */
function collectHtmlBlockText(node: Node | null): string {
if (!node) {
return "";
@@ -892,10 +858,9 @@ async function readBoundedText(
};
}
-// A sent attachment keeps only the text its adapter produced, so the preview
-// unwraps the adapter's header or tag rather than showing it to the user. The
-// stored payload has no size limit, so the wrapper is matched on a prefix and
-// only the capped body is copied out.
+// A sent attachment keeps only the text its adapter produced, so the preview unwraps the
+// adapter's header rather than showing it. The stored payload has no size limit, so the
+// wrapper is matched on a prefix and only the capped body is copied out.
export function parseAttachmentText(raw: string): AttachmentText {
const head = raw.slice(0, MAX_ATTACHMENT_WRAPPER_LENGTH);
@@ -945,13 +910,9 @@ export function truncateAttachmentPreviewText(text: string): {
return { text: text.slice(0, MAX_PREVIEW_TEXT_LENGTH), truncated: true };
}
-/**
- * The shiki language a plain-text attachment previews as, or null for prose.
- *
- * Only the filename decides. Text pulled out of a PDF, a DOCX or a spreadsheet
- * is prose whatever the document was called, so callers pass the extracted
- * label instead of the name in that case.
- */
+/** The shiki language a plain-text attachment previews as, or null for prose. Only the
+ * filename decides; text pulled out of a PDF or DOCX is prose whatever the document was
+ * called, so callers pass the extracted label instead. */
export function attachmentTextLanguage(
name: string | undefined,
label: AttachmentTextLabel | null,
diff --git a/studio/frontend/src/features/chat/audio-attachment-adapter.ts b/studio/frontend/src/features/chat/audio-attachment-adapter.ts
index f082a949abb..65ca7579d2e 100644
--- a/studio/frontend/src/features/chat/audio-attachment-adapter.ts
+++ b/studio/frontend/src/features/chat/audio-attachment-adapter.ts
@@ -24,8 +24,8 @@ function newAttachmentId(): string {
return `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
}
-// Audio shares the "Add photos & files" picker. Like VisionImageAdapter,
-// unsupported models are rejected at add() time with a toast.
+// Audio shares the "Add photos & files" picker. Like VisionImageAdapter, unsupported models are
+// rejected at add() time with a toast.
export class AudioAttachmentAdapter implements AttachmentAdapter {
// MIME is unreliable for some containers (m4a), so also match by
// extension. No .webm extension: it would claim video/webm files; real
@@ -48,8 +48,8 @@ export class AudioAttachmentAdapter implements AttachmentAdapter {
? "The last model failed to load. Check the server logs, then load a model before adding audio files."
: "Load a model before adding audio files.";
} else if (!activeModel?.hasAudioInput) {
- // A connected provider's model has no row in `models`, so without the parse this
- // named it by its raw `external::…` id (#8405).
+ // A connected provider's model has no row in `models`, so without the parse this named it by its
+ // raw `external::` id (#8405).
const label =
activeModel?.name ||
externalModelLabel(checkpoint) ||
diff --git a/studio/frontend/src/features/chat/bypass-permissions-menu-item.tsx b/studio/frontend/src/features/chat/bypass-permissions-menu-item.tsx
index 8c9a528e667..363d0939760 100644
--- a/studio/frontend/src/features/chat/bypass-permissions-menu-item.tsx
+++ b/studio/frontend/src/features/chat/bypass-permissions-menu-item.tsx
@@ -44,9 +44,8 @@ export function BypassPermissionsMenuItem() {
</DropdownMenuSubTrigger>
<DropdownMenuSubContent className="unsloth-plus-menu w-[300px]">
<PermissionModeMenuItems
- // Defer past Radix's menu-close focus restoration: opening the
- // dialog synchronously here lets the dropdown grab focus back and
- // breaks the dialog's focus trap.
+ // Defer past Radix's menu-close focus restoration: opening the dialog synchronously here lets the
+ // dropdown grab focus back and breaks the dialog's focus trap.
onRequestFullAccess={() =>
setTimeout(() => setBypassConfirmOpen(true), 0)
}
@@ -56,10 +55,9 @@ export function BypassPermissionsMenuItem() {
);
}
-// The danger-confirmation dialog. Mounted once at the chat-page root (not inside
-// a Composer or the menu) and driven by global store state, so it works for both
-// the main and shared composers, never duplicates in Compare mode, and confirming
-// or cancelling never leaves the composer "+"/More popovers frozen open.
+// The danger-confirmation dialog. Mounted once at the chat-page root, not inside a Composer or the
+// menu, and driven by global store state, so it works for both the main and shared composers,
+// never duplicates in Compare mode, and never leaves the composer popovers frozen open.
export function BypassPermissionsConfirmDialog() {
const open = useChatRuntimeStore((s) => s.bypassConfirmOpen);
const setOpen = useChatRuntimeStore((s) => s.setBypassConfirmOpen);
diff --git a/studio/frontend/src/features/chat/chat-mcp-servers-dialog.tsx b/studio/frontend/src/features/chat/chat-mcp-servers-dialog.tsx
index 8df2a6d432e..636acbaa5b1 100644
--- a/studio/frontend/src/features/chat/chat-mcp-servers-dialog.tsx
+++ b/studio/frontend/src/features/chat/chat-mcp-servers-dialog.tsx
@@ -115,8 +115,8 @@ function headersToObject(
return Object.keys(out).length > 0 ? out : undefined;
}
-// A non-HTTP address is a local stdio command. Case-insensitive to match the
-// backend's is_stdio(), so all layers split http-vs-command identically.
+// A non-HTTP address is a local stdio command. Case-insensitive to match the backend's is_stdio(),
+// so all layers split http-vs-command identically.
function isHttpAddress(value: string): boolean {
const trimmed = value.trim().toLowerCase();
return trimmed.startsWith("http://") || trimmed.startsWith("https://");
@@ -178,8 +178,8 @@ function isValidAddress(value: string): boolean {
return false;
}
}
- // The backend owns stdio parsing and validation. In particular, the browser
- // must not split an executable or duplicate platform-specific quoting rules.
+ // The backend owns stdio parsing and validation. In particular, the browser must not split an
+ // executable or duplicate platform-specific quoting rules.
return true;
}
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsx
index ed1848764bc..aed78fc9bb8 100644
--- a/studio/frontend/src/features/chat/chat-page.tsx
+++ b/studio/frontend/src/features/chat/chat-page.tsx
@@ -550,10 +550,8 @@ function modelMatchesDeleted(
);
}
-/**
- * True when the loaded checkpoint is a LoRA, meaning a base-vs-fine-tuned
- * compare that uses the fast simultaneous adapter-toggle path.
- */
+/** True when the loaded checkpoint is a LoRA, so a base-vs-fine-tuned compare can use the fast
+ * simultaneous adapter-toggle path. */
function useIsLoraCompare(): boolean | null {
return useChatRuntimeStore((s) =>
checkpointCompareClass({
@@ -624,10 +622,8 @@ function useCompareVariant(pairId: string): {
return { state: settled ?? { status: "pending" }, retry };
}
-/**
- * The pair read failed. Its persisted shape is unknown, and picking a renderer from the
- * loaded checkpoint would relabel existing histories, so offer the read again instead.
- */
+/** The pair read failed. Its persisted shape is unknown, and picking a renderer from the loaded
+ * checkpoint would relabel existing histories, so offer the read again instead. */
function CompareUnreadable({
onRetry,
}: {
@@ -698,14 +694,9 @@ const CompareContent = memo(function CompareContent({
);
});
-/**
- * A single column in the compare layout: one ChatRuntimeProvider and one
- * Thread with hideComposer (the composer is shared across panes).
- *
- * Each pane is `flex-1 basis-0 min-h-0 min-w-0` so panes share height
- * (mobile flex-col) or width (desktop flex-row) equally. The `min-*`
- * constraints let the inner viewport scroll instead of spilling.
- */
+/** One column in the compare layout: a ChatRuntimeProvider and a Thread with hideComposer. Each
+ * pane is `flex-1 basis-0 min-h-0 min-w-0` so panes share space equally and the inner
+ * viewport scrolls instead of spilling. */
function ComparePane({
modelType,
pairId,
@@ -783,15 +774,9 @@ function useCompareReloadReadiness(pairId: string): (pane: string) => void {
);
}
-/**
- * Shared shell for both compare variants: a flex column with the two panes
- * as siblings and the shared composer docked at the bottom. Panes stack on
- * mobile (flex-col), sit side by side on desktop (md:flex-row).
- *
- * Flex, not grid, for the pane container: grid rows with 1fr triggered
- * resize thrash in assistant-ui's autoscroll on breakpoint crossings,
- * leaving it stuck in a scroll-to-bottom loop.
- */
+/** Shared shell for both compare variants: a flex column with the two panes as siblings and the
+ * shared composer docked at the bottom. Flex, not grid: grid rows with 1fr triggered resize
+ * thrash in assistant-ui's autoscroll on breakpoint crossings. */
function CompareShell({
handlesRef,
children,
@@ -846,18 +831,15 @@ const LoraCompareContent = memo(function LoraCompareContent({
const checkpoint = useChatRuntimeStore((s) => s.params.checkpoint);
const checkpointIsLora = useIsLoraCompare();
- // Global on purpose: a first compare run starts before either thread exists, so there is
- // no pair id to scope BY -- it files its handles under "__default" until initialize()
- // persists the row. And the gate has to exist at all because these ids feed ComparePane's
- // `initialThreadId`, so learning them mid-run points ThreadAutoSwitch at a thread that is
- // still generating.
+ // Global on purpose: a first compare run starts before either thread exists, so there is no pair
+ // id to scope BY. The gate exists at all because these ids feed ComparePane's
+ // `initialThreadId`, so learning them mid-run points ThreadAutoSwitch at a live thread.
const anyRunning = useChatRuntimeStore(
(s) => Object.keys(s.localRunByThreadId).length > 0,
);
- // ...but only RE-lists wait. The shared provider (#8908) keeps a base chat's run alive
- // across the switch into compare, so `anyRunning` is true on arrival for a reason that has
- // nothing to do with this pair, and gating the FIRST list on it left an existing compare on
- // blank runtimes with no later edge to recover on. That list has nothing to clobber.
+ // ...but only RE-lists wait. The shared provider (#8908) keeps a base chat's run alive across
+ // the switch into compare, so `anyRunning` is true on arrival for an unrelated reason, and
+ // gating the FIRST list on it left an existing compare on blank runtimes.
const listedPairRef = useRef<string | null>(null);
useEffect(() => {
@@ -868,9 +850,8 @@ const LoraCompareContent = memo(function LoraCompareContent({
listStoredChatThreads({ pairId })
.then((threads) => {
if (!isActive) return;
- // No model1/model2 fallback: useCompareVariant never routes a generalized
- // pair here, so adopting one could only mislabel it and write adapter
- // answers into its histories.
+ // No model1/model2 fallback: useCompareVariant never routes a generalized pair here, so adopting
+ // one could only mislabel it.
const baseThread = threads.find((t) => t.modelType === "base");
const loraThread = threads.find((t) => t.modelType === "lora");
setBaseThreadId(baseThread?.id);
@@ -972,11 +953,8 @@ const LoraCompareContent = memo(function LoraCompareContent({
);
});
-/**
- * Per-pane header (inside GeneralCompareContent) with the model selector,
- * aligned to the global topbar height. Left pane reserves room for the
- * mobile sidebar trigger; right pane for the global settings button.
- */
+/** Per-pane header with the model selector, aligned to the global topbar height. Left pane
+ * reserves room for the mobile sidebar trigger; right pane for the settings button. */
function GeneralCompareHeader({
models,
loraModels,
@@ -1007,7 +985,7 @@ function GeneralCompareHeader({
deleteDisabled?: boolean;
side: "left" | "right";
}): ReactElement {
- // Controlled so the body-portaled popover can't linger over another tab off-route.
+ // Controlled so the body-portaled popover cannot linger over another tab off-route.
const active = useChatActive();
const [selectorOpen, setSelectorOpen] = useState(false);
@@ -1078,8 +1056,7 @@ const GeneralCompareContent = memo(function GeneralCompareContent({
const globalGgufVariant = useChatRuntimeStore((s) => s.activeGgufVariant);
const globalIsDiffusion = useChatRuntimeStore((s) => s.loadedIsDiffusion);
const active = useChatActive();
- // Global, with only RE-lists waiting on it; see the note on the Lora variant above for
- // why a first compare run cannot be scoped by pair, and why the first list must not wait.
+ // Global, with only RE-lists waiting on it; see the note on the Lora variant above.
const anyRunning = useChatRuntimeStore(
(s) => Object.keys(s.runningByThreadId).length > 0,
);
@@ -1244,8 +1221,7 @@ function formatProjectChatDate(timestamp: number): string {
}).format(new Date(timestamp));
}
-// Unique thread nonce; falls back off crypto.randomUUID for non-secure
-// (HTTP LAN) contexts where it is unavailable.
+// Unique thread nonce; falls back off crypto.randomUUID for non-secure (HTTP LAN) contexts.
function createThreadNonce(): string {
if (typeof globalThis.crypto?.randomUUID === "function") {
return globalThis.crypto.randomUUID();
@@ -1351,18 +1327,17 @@ function ProjectLanding({
newThreadNonce: string;
rotateNewThreadNonce: () => void;
dataLoaded: boolean;
- // #9251 holds the reload shell until the landing can be drawn. Its provider is hoisted
- // above the view switch now (#8908), so the owner of that one reports readiness down.
+ // #9251 holds the reload shell until the landing can be drawn. Its provider is hoisted above the
+ // view switch now (#8908), so the owner of that one reports readiness down.
runtimeReady: boolean;
}): ReactElement {
const navigate = useNavigate();
- // Gates body-portaled surfaces so they can't linger or act while the landing
- // is off-route (e.g. behind another tab).
+ // Gates body-portaled surfaces so they cannot linger or act while the landing is off-route.
const active = useChatActive();
const wasActiveRef = useRef(active);
const activeThreadId = useChatRuntimeStore((s) => s.activeThreadId);
- // Captured in render, not an effect: the shared provider's ThreadNewChatSwitch is an
- // earlier sibling, so its mount effect blanks activeThreadId before an effect here runs.
+ // Captured in render, not an effect: the shared provider's ThreadNewChatSwitch is an earlier
+ // sibling, so its mount effect blanks activeThreadId before an effect here runs.
const [initialActiveThreadId] = useState(
() => useChatRuntimeStore.getState().activeThreadId,
);
@@ -1381,15 +1356,13 @@ function ProjectLanding({
Record<string, { snippet: string; date: string }>
>({});
const reloadReadySent = useRef(false);
- // Inline rename, mirroring the sidebar recent-row UX: edit the title in place,
- // commit on Enter/blur, cancel on Escape. Reuses the projectId-agnostic
+ // Inline rename, mirroring the sidebar recent-row UX. Reuses the projectId-agnostic
// renameChatItem so behavior matches the sidebar.
const [renamingId, setRenamingId] = useState<string | null>(null);
const [renameDraft, setRenameDraft] = useState("");
// Skips the input's blur-commit when Enter/Escape already handled it.
const skipRenameBlurRef = useRef(false);
- // Optimistic title shown until the debounced sidebar refresh (fired by the
- // rename) catches up, so the old name does not flash back in.
+ // Optimistic title shown until the debounced sidebar refresh catches up, so the old name does not flash back in.
const [pendingRename, setPendingRename] = useState<{
id: string;
title: string;
@@ -1435,8 +1408,8 @@ function ProjectLanding({
setDeletingProject(false);
try {
await deleteChatProject(projectId);
- // Refresh chat history so the project's now-deleted chats don't linger
- // in the sidebar, matching the sidebar delete path.
+ // Refresh chat history so the project's now-deleted chats do not linger in the sidebar, matching
+ // the sidebar delete path.
notifyChatHistoryUpdated();
useChatRuntimeStore.getState().setActiveProjectId(null);
navigate({ to: "/chat", search: { new: createThreadNonce() } });
@@ -1503,12 +1476,12 @@ function ProjectLanding({
const [confirmingDelete, setConfirmingDelete] = useState<SidebarItem | null>(
null,
);
- // Preselected from the preference, so the dialog shows what is about to
- // happen and can still be turned off for this one chat.
+ // Preselected from the preference, so the dialog shows what is about to happen and can still be
+ // turned off for this one chat.
const [deleteFilesOnDelete, setDeleteFilesOnDelete] = useState(false);
- // Landing has no active thread selected, so the onView callback here is a
- // no-op; the items list refreshes itself once storage emits its update.
+ // Landing has no active thread selected, so the onView callback is a no-op; the items list
+ // refreshes itself once storage emits its update.
const noopView = useCallback(() => {}, []);
const handleArchive = useCallback(
@@ -1587,12 +1560,12 @@ function ProjectLanding({
[projectId],
);
- // No composer ever records under this, so passing it refuses the adoption.
- // (adoptPendingProjectAttachmentTarget only adopts on an exact claim match.)
+ // No composer ever records under this, so passing it refuses the adoption
+ // (adoptPendingProjectAttachmentTarget only adopts on an exact claim match).
const NO_SUCH_CLAIM = -1;
- // The claim the composer on screen recorded its attach choice under: every
- // fresh composer shares one pending key, so only the claim tells them apart.
+ // The claim the composer on screen recorded its attach choice under: every fresh composer shares
+ // one pending key, so only the claim tells them apart.
const pendingTargetClaimRef = useRef<{
nonce: string;
claim: number;
@@ -1602,8 +1575,8 @@ function ProjectLanding({
const pending =
state.projectAttachmentTargetByThread[PENDING_CHAT_ATTACHMENT_KEY];
if (pending === undefined) return;
- // By claim, not by value: picking the same destination twice rewrites the
- // same string under a new claim, and skipping it reads as somebody else's.
+ // By claim, not by value: picking the same destination twice rewrites the same string under a
+ // new claim, and skipping it reads as somebody else's.
const claim = readPendingAttachmentTargetClaim();
const captured = pendingTargetClaimRef.current;
if (captured?.nonce === newThreadNonce && captured.claim === claim) {
@@ -1621,18 +1594,16 @@ function ProjectLanding({
}
if (!activeThreadId) {
if (resumed && pendingNewThreadId) {
- // ...unless it was deleted while another view held the screen. Nothing else clears this
- // id: it is component state, so the sidebar's global delete cannot reach it, and compare
- // does not hold the chat as the active id either. Restoring a tombstoned thread would put
- // a conversation storage no longer has back on screen with a composer pointed at it. Fall
- // through to the rotate below, which is what a landing owning no chat looks like.
+ // ...unless it was deleted while another view held the screen. Nothing else clears this id, so
+ // restoring a tombstoned thread would put a conversation storage no longer has back on
+ // screen. Fall through to the rotate below.
if (!isChatThreadDeleted(pendingNewThreadId)) {
useChatRuntimeStore.getState().setActiveThreadId(pendingNewThreadId);
return;
}
}
- // Leaving a created chat for a new one: rotate the nonce so the runtime
- // switches to a fresh thread instead of appending to the old chat.
+ // Leaving a created chat for a new one: rotate the nonce so the runtime switches to a fresh
+ // thread instead of appending to the old chat.
if (pendingNewThreadId) {
rotateNewThreadNonce();
setPendingNewThreadId(null);
@@ -1645,11 +1616,9 @@ function ProjectLanding({
) {
return;
}
- // Hand the composer's attach choice to the chat it just created: setting
- // this swaps ProjectComposer for Thread, so the bar holding the choice
- // unmounts without seeing the id and its cleanup drops it. Its own choice
- // only, or a send materializing after another composer opened would consume
- // that one's pick; an unrecognised claim is refused.
+ // Hand the composer's attach choice to the chat it just created: setting this swaps
+ // ProjectComposer for Thread, so the bar holding the choice unmounts and its cleanup drops
+ // it. Its own choice only, or a later send would consume another composer's pick.
const captured = pendingTargetClaimRef.current;
useChatRuntimeStore
.getState()
@@ -1690,8 +1659,7 @@ function ProjectLanding({
return [
item.id,
{
- // A paste-only message carries its text in the attachment, so
- // the row would otherwise be blank.
+ // A paste-only message carries its text in the attachment, so the row would otherwise be blank.
snippet: firstUserMessage
? extractMessageText(firstUserMessage.content) ||
attachmentsSample(firstUserMessage.attachments)
@@ -1867,11 +1835,9 @@ function ProjectLanding({
setRenameDraft(event.target.value)
}
onKeyDown={(event) => {
- // Ignore keydowns fired mid-IME-composition (CJK)
- // so a candidate-confirming Enter or candidate-
- // cancelling Escape does not commit/cancel the
- // rename. Guard before the key branch so Escape is
- // covered too (isComposing on WebKit, 229 on Chromium).
+ // Ignore keydowns fired mid-IME-composition (CJK) so a candidate-confirming
+ // Enter does not commit the rename. Guarded before the key branch so Escape
+ // is covered too (isComposing on WebKit, 229 on Chromium).
if (
event.nativeEvent.isComposing ||
event.keyCode === 229
@@ -2203,10 +2169,9 @@ type PendingHubAutoLoad = {
originGgufVariant: string | null;
};
-// `search` comes from RootLayout (not useSearch) so ChatPage stays mounted off-route
-// (keeping an in-flight generation alive), frozen to the last /chat search. `active`
-// is false off-route: close body-portaled surfaces and stop route-specific listeners
-// that would otherwise bleed over the visible tab.
+// `search` comes from RootLayout (not useSearch) so ChatPage stays mounted off-route, frozen to
+// the last /chat search. `active` is false off-route: close portaled surfaces and stop
+// route-specific listeners.
export function ChatPage({
search,
active,
@@ -2224,11 +2189,9 @@ export function ChatPage({
const store = useChatRuntimeStore.getState();
const wasIncognito = store.incognito;
store.setIncognito(!store.incognito);
- // On an empty scratch chat there's nothing to abandon, so flip in
- // place: navigating would remount the thread and bounce the composer
- // (it docks to the bottom before the welcome state re-centers it).
- // Otherwise start a clean chat so the temporary session can't inherit
- // or leave behind a persisted thread (matches ChatGPT / Gemini).
+ // On an empty scratch chat there is nothing to abandon, so flip in place: navigating would
+ // remount the thread and bounce the composer. Otherwise start a clean chat so the temporary
+ // session cannot inherit or leave behind a persisted thread.
const onEmptyScratchChat =
!search.thread &&
!search.compare &&
@@ -2259,8 +2222,8 @@ export function ChatPage({
}, [hydratePersistedSettings]);
useEffect(() => {
- // Skip while off-route: ChatPage stays mounted, and toast+navigate here would
- // yank the user back to chat from whatever tab they're on.
+ // Skip while off-route: ChatPage stays mounted, and toast+navigate here would yank the user back
+ // to chat from whatever tab they are on.
if (!active) return;
const threadId = search.thread;
if (!threadId) return;
@@ -2297,8 +2260,8 @@ export function ChatPage({
const [projectPickerOpen, setProjectPickerOpen] = useState(false);
const [modelSelectorLocked, setModelSelectorLocked] = useState(false);
const viewBeforeCompareRef = useRef<ChatSearch | null>(null);
- // Latest non-compare view, so exiting compare can restore it even when
- // compare was opened from a path that doesn't set viewBeforeCompareRef.
+ // Latest non-compare view, so exiting compare can restore it even when compare was opened from a
+ // path that does not set viewBeforeCompareRef.
const lastNonCompareViewRef = useRef<ChatSearch | null>(null);
useEffect(() => {
if (!search.compare) {
@@ -2390,12 +2353,9 @@ export function ChatPage({
const persistedActiveThreadId = isAssistantLocalThreadId(activeThreadId)
? null
: activeThreadId;
- // A ?new=<nonce> chat has no thread in the URL before or after its first send, so the
- // model notice never saw the row that send created. The store does learn the id, but
- // for the first render it still holds the PREVIOUS chat's, until ThreadNewChatSwitch
- // blanks it in an effect -- using it then would show the previous chat's notice here.
- // So latch on having seen it blanked for this nonce; before the first send there is
- // nothing to offer anyway.
+ // A ?new=<nonce> chat has no thread in the URL before or after its first send, and for the first
+ // render the store still holds the PREVIOUS chat's id until ThreadNewChatSwitch blanks it,
+ // so latch on having seen it blanked for this nonce.
const newChatBlankedRef = useRef<string | null>(null);
if (
search.new &&
@@ -2553,11 +2513,8 @@ export function ChatPage({
preferredEffort,
effortLevels,
);
- // Per-provider default effort. Anthropic gets the highest level since
- // Claude's adaptive thinking adjusts cost per turn (top of dial =
- // strongest answers, still skips thinking when trivial). OpenAI gets
- // "high" (gpt-5.x accept it across the board; good cost/quality for
- // Responses-API tools). Everyone else "medium". Overridable via Think.
+ // Per-provider default effort: Anthropic gets the highest level, since Claude's adaptive thinking
+ // adjusts cost per turn; OpenAI gets "high"; everyone else "medium". Overridable via Think.
const isAnthropic = provider?.providerType === "anthropic";
const isOpenAI = provider?.providerType === "openai";
const anthropicTopEffort = effortLevels.includes("xhigh")
@@ -2598,15 +2555,12 @@ export function ChatPage({
const supportsBuiltinWebFetch = providerSupportsBuiltinWebFetch(
provider?.providerType,
);
- // Kimi's k2.6/k2.5 default to thinking enabled server-side (per
- // https://platform.kimi.ai/docs/models). Mirror that so the Think pill
- // comes up clicked for Kimi models. Search stays off; the composer's
- // mutual-exclusion handlers flip the two when needed.
+ // Kimi's k2.6/k2.5 default to thinking enabled server-side, so the Think pill comes up clicked.
+ // Search stays off; the composer's mutual-exclusion handlers flip the two.
+ // Per https://platform.kimi.ai/docs/models.
const isKimi = provider?.providerType === "kimi";
- // Web search on by default only for the two providers we trust most:
- // Anthropic and OpenAI (both with structured citations). Others stay
- // off-by-default; OpenRouter and Kimi work on opt-in but are less
- // reliable, so we don't pre-enable them.
+ // Web search on by default only for Anthropic and OpenAI, both with structured citations.
+ // OpenRouter and Kimi work on opt-in but are less reliable.
const searchOnByDefault =
supportsBuiltinWebSearch &&
(provider?.providerType === "anthropic" ||
@@ -2624,20 +2578,18 @@ export function ChatPage({
const storedWebFetchToolsEnabled =
threadScopedOverride("webFetchToolsEnabled") ??
loadOptionalBool(CHAT_WEB_FETCH_TOOLS_ENABLED_KEY);
- // Unsloth runs Search and Code itself for any provider that advertises the
- // capability, so a self-hosted connection has no hosted builtin to key off.
- // Keying the pill state on the hosted flags alone discarded the user's saved
- // preference on every reload and sent enable_tools: false, even though the
- // composer left both pills clickable.
+ // Unsloth runs Search and Code itself for any provider that advertises the capability, so a
+ // self-hosted connection has no hosted builtin to key off. Keying the pill state on the
+ // hosted flags alone discarded the saved preference on every reload and sent
+ // enable_tools: false.
const supportsStudioToolsHere =
providerModelSupportsStudioTools(
provider?.providerType,
selection.modelId,
) === true;
const canSearch = supportsBuiltinWebSearch || supportsStudioToolsHere;
- // Read out of the placement rule, not off the Unsloth-tools flag: a model on
- // a sandbox-owning provider that cannot use it runs nothing either way, and
- // offering the pill there restored a preference that sent no tools at all.
+ // Read out of the placement rule, not off the Unsloth-tools flag: a model on a sandbox-owning
+ // provider that cannot use it runs nothing either way.
const canRunCode = codeToolCanRun({
hostedCodeExecutionForThisTurn: supportsBuiltinCodeExecution,
providerHostsCodeExecution: providerHostsCodeExecution(provider?.providerType),
@@ -2678,9 +2630,8 @@ export function ChatPage({
? (storedWebFetchToolsEnabled ?? false)
: false,
});
- // Reruns once settings hydrate: this normalization reads the stored pills
- // and clamps them to the model, and hydration refreshes what it reads, so
- // it has to be the one applied last.
+ // Reruns once settings hydrate: this normalization reads the stored pills and clamps them to the
+ // model, and hydration refreshes what it reads, so it has to be applied last.
}, [externalProvidersForChat, inferenceParams.checkpoint, settingsHydrated]);
const canCompare = useMemo(() => {
return Boolean(inferenceParams.checkpoint) && !isExternalModel;
@@ -2731,7 +2682,6 @@ export function ChatPage({
};
}, [search.compare, search.project, search.thread]);
- // Derive view from URL search params
const view = useMemo<ChatView>(() => {
if (search.compare) {
return {
@@ -2784,10 +2734,8 @@ export function ChatPage({
setProjectNewThreadNonce(createThreadNonce());
}, []);
- // Temporary chat only applies to a fresh single-view chat. Exit incognito
- // when we land on anything else (compare, a project, or an existing thread
- // via sidebar/deep link/back), so the toggle isn't stranded and the UI
- // never implies a saved thread is temporary.
+ // Temporary chat only applies to a fresh single-view chat, so exit incognito on anything else
+ // (compare, a project, an existing thread) rather than stranding the toggle.
useEffect(() => {
const onFreshSingleChat = view.mode === "single" && !view.threadId;
if (incognito && !onFreshSingleChat) {
@@ -2812,10 +2760,9 @@ export function ChatPage({
? "single:implicit"
: artifactViewKey;
- // Compare replaces the shared provider on screen, so the base view is kept mounted
- // behind it (hidden + inert): unmounting runs useLocalRuntime's detach(), the backend
- // cancels on the disconnect, and a project chat's run would die on opening a compare
- // chat. Frozen to the last non-compare view so the provider's props don't churn.
+ // Compare replaces the shared provider on screen, so the base view is kept mounted behind it:
+ // unmounting runs useLocalRuntime's detach(), the backend cancels on the disconnect, and a
+ // project chat's run would die. Frozen to the last non-compare view.
const keptBaseViewRef = useRef<{
view: Exclude<ChatView, { mode: "compare" }>;
attachmentTargetKey: string;
@@ -2828,9 +2775,9 @@ export function ChatPage({
keptBaseViewRef.current?.attachmentTargetKey ?? artifactViewKey;
const baseBackgrounded = view.mode === "compare";
- // #9251's reload signal, taken here because the provider is hoisted. Stored as the
- // project it belongs to, not a boolean: the hoisted provider is not remounted when the
- // project changes, so a flag would stay true and release the next landing's shell early.
+ // #9251's reload signal, taken here because the provider is hoisted. Stored as the project it
+ // belongs to, not a boolean: the hoisted provider is not remounted when the project changes,
+ // so a flag would release the next landing's shell early.
const projectLandingId =
baseView?.mode === "project" ? baseView.projectId : null;
const [projectRuntimeReadyFor, setProjectRuntimeReadyFor] = useState<
@@ -2850,7 +2797,7 @@ export function ChatPage({
useEffect(() => {
if (view.mode !== "single") return;
if (view.threadId || !selectedArtifact) return;
- // Close any canvas that doesn't belong to the active thread.
+ // Close any canvas that does not belong to the active thread.
if (
selectedArtifact.threadId &&
selectedArtifact.threadId === activeThreadId
@@ -2989,8 +2936,7 @@ export function ChatPage({
}
},
});
- // The pending auto-load's job, for the context-change effect below. Written from
- // an effect, never during render.
+ // The pending auto-load's job, for the context-change effect below. Written from an effect, never during render.
const pendingAutoLoadKeyRef = useRef<string | null>(null);
// The live context, so a start still in flight can check it is still on screen.
const chatContextKeyRef = useRef(chatContextKey);
@@ -3016,28 +2962,26 @@ export function ChatPage({
repoId: pending.selection.id,
variant: pending.selection.ggufVariant ?? null,
expectedBytes: pending.selection.expectedBytes ?? 0,
- // Notice-only: #9663 removed this surface's own toast, so it must not
- // return on an HTTP start or once the three notices are spent.
+ // Notice-only: #9663 removed this surface's own toast, so it must not return on an HTTP start or
+ // once the three notices are spent.
callerToast: {
title: "Downloading model",
description: "It'll load automatically once the download finishes.",
noticeOnly: true,
- // The cleanup below only reaches a toast that already exists; a raise
- // still in flight would promise an auto-load onComplete then refuses.
+ // The cleanup below only reaches a toast that already exists; a raise still in flight would
+ // promise an auto-load onComplete then refuses.
stillValid: () => chatContextKeyRef.current === pending.contextKey,
},
});
if (!active) return;
if (outcome === "started") {
- // No toast here, and none from the manager unless the notice folds the
- // sentence in. The auto-load runs from onComplete.
+ // No toast here, and none from the manager unless the notice folds the sentence in. The
+ // auto-load runs from onComplete.
return;
}
if (outcome === "conflict") {
- // Keep pendingHubAutoLoad bound so this surface's cleanup does not wipe
- // the conflict just recorded by requestStart (which the toast points the
- // user to); resolving it from the Hub completes the download and this
- // surface's onComplete auto-loads, mirroring the "started" branch.
+ // Keep pendingHubAutoLoad bound so this surface's cleanup does not wipe the conflict requestStart
+ // just recorded; resolving it from the Hub completes the download and onComplete auto-loads.
toast.info("Resume this download from Models", {
description:
"An earlier partial download used a different transport. Open the Model hub tab to resume or restart it.",
@@ -3054,13 +2998,13 @@ export function ChatPage({
})();
return () => {
active = false;
- // Another model was picked, so this one's completion loads nothing. A no-op
- // once the download finished, which dismisses the same id.
+ // Another model was picked, so this one's completion loads nothing. A no-op once the download
+ // finished, which dismisses the same id.
dismissStartToast(pendingKey);
};
}, [pendingHubAutoLoad]);
- // Switching thread or project keeps the pathname and pendingHubAutoLoad, so neither
- // sweep above runs, yet onComplete refuses to load into a different contextKey.
+ // Switching thread or project keeps the pathname and pendingHubAutoLoad, so neither sweep above
+ // runs, yet onComplete refuses to load into a different contextKey.
useEffect(() => {
return () => {
const pendingKey = pendingAutoLoadKeyRef.current;
@@ -3094,8 +3038,8 @@ export function ChatPage({
),
[hasActiveModel, loadNativeModelIntent],
);
- // Dropped documents go to the thread bar, which owns the RAG upload and can
- // materialize a thread id for a chat that hasn't been sent to yet.
+ // Dropped documents go to the thread bar, which owns the RAG upload and can materialize a thread
+ // id for a chat that has not been sent to yet.
const handleNativeAttachmentDrop = useCallback(
(intents: NativeIntent[]) => {
useNativeIntentStore.getState().addAttachments(artifactViewKey, intents);
@@ -3129,8 +3073,8 @@ export function ChatPage({
[artifactViewKey],
);
const nativeModelDropState = useNativeModelDrop({
- // Compare used to disable this outright, so a drop there vanished with no overlay
- // and no message (#9036). Keep listening and refuse out loud, models included.
+ // Compare used to disable this outright, so a drop there vanished with no overlay and no message
+ // (#9036). Keep listening and refuse out loud.
enabled: active,
dropsUnsupportedReason:
view.mode === "single"
@@ -3187,8 +3131,8 @@ export function ChatPage({
preferredEffort,
effortLevels,
);
- // Same per-provider default policy as the useEffect above:
- // Anthropic highest level, OpenAI "high", everyone else "medium".
+ // Same per-provider default policy as the useEffect above: Anthropic highest level, OpenAI
+ // "high", everyone else "medium".
const isAnthropic = selectedProvider?.providerType === "anthropic";
const isOpenAI = selectedProvider?.providerType === "openai";
const anthropicTopEffort = effortLevels.includes("xhigh")
@@ -3210,8 +3154,8 @@ export function ChatPage({
? "medium"
: clampedEffort
: store.reasoningEffort;
- // Clear any cached router-picked openrouter/free model unless staying
- // on openrouter/free, else the chip keeps a stale ":<chosen>" suffix.
+ // Clear any cached router-picked openrouter/free model unless staying on openrouter/free, else
+ // the chip keeps a stale ":<chosen>" suffix.
const stillOnOpenRouterFree =
selectedProvider?.providerType === "openrouter" &&
selectedExternal?.modelId === "openrouter/free";
@@ -3236,12 +3180,10 @@ export function ChatPage({
const supportsBuiltinWebFetch = providerSupportsBuiltinWebFetch(
selectedProvider?.providerType,
);
- // See sibling useEffect: Kimi's k2.x default to thinking enabled
- // (Think pill clicked). Search stays off; the composer's mutual
- // exclusion flips them.
+ // See sibling useEffect: Kimi's k2.x default to thinking enabled. Search stays off; the
+ // composer's mutual exclusion flips them.
const isKimi = selectedProvider?.providerType === "kimi";
- // Mirror of sibling useEffect: Anthropic/OpenAI get Search on by
- // default (structured citations end-to-end); others stay off.
+ // Mirror of sibling useEffect: Anthropic/OpenAI get Search on by default; others stay off.
const searchOnByDefault =
supportsBuiltinWebSearch &&
(selectedProvider?.providerType === "anthropic" ||
@@ -3259,9 +3201,8 @@ export function ChatPage({
const storedWebFetchToolsEnabled =
threadScopedOverride("webFetchToolsEnabled") ??
loadOptionalBool(CHAT_WEB_FETCH_TOOLS_ENABLED_KEY);
- // Same rule as the selection handler above: a self-hosted connection has
- // no hosted builtin, so keying the pills on those flags threw away the
- // user's saved preference every time this ran.
+ // Same rule as the selection handler above: a self-hosted connection has no hosted builtin, so
+ // keying the pills on those flags threw away the saved preference.
const supportsStudioToolsHere =
providerModelSupportsStudioTools(
selectedProvider?.providerType,
@@ -3286,8 +3227,8 @@ export function ChatPage({
...loadedContextFields(null),
activeNativePathToken: null,
activeNativePathExpiresAtMs: null,
- // Clear previous-model counters, else the relaxed external-provider render gate shows
- // stale stats. The per-thread copies go too, so a switch back cannot re-apply.
+ // Clear previous-model counters, else the relaxed external-provider render gate shows stale
+ // stats. The per-thread copies go too, so a switch back cannot re-apply.
contextUsage: null,
contextUsageByThreadId: {},
supportsReasoning: reasoningCaps.supportsReasoning,
@@ -3323,7 +3264,7 @@ export function ChatPage({
});
return;
}
- // Local model picked → drop any cached openrouter/free chosen model.
+ // Local model picked: drop any cached openrouter/free chosen model.
useChatRuntimeStore.setState({ lastOpenRouterChosenModel: null });
void (async () => {
let showImageCompatibilityWarning = false;
@@ -3383,10 +3324,8 @@ export function ChatPage({
const activeLoadId = runtime.activeLoadId;
const nativeToken = runtime.activeNativePathToken;
const nativeExpiry = runtime.activeNativePathExpiresAtMs;
- // A file-picked GGUF is reachable only via its native path token, which
- // the desktop host prunes after a TTL. Reusing an expired token makes the
- // reload fail with an opaque error, so prompt the user to re-select the
- // file instead.
+ // A file-picked GGUF is reachable only via its native path token, which the desktop host prunes
+ // after a TTL. Reusing an expired token fails with an opaque error, so prompt to re-select.
if (nativeToken && nativeExpiry != null && Date.now() >= nativeExpiry) {
toast.error("This local model file's access has expired.", {
description: "Re-select the model file to reload it.",
@@ -3399,8 +3338,7 @@ export function ChatPage({
// The checkpoint is the id, so a pinned model reloads from that same snapshot.
loadId: activeLoadId,
ggufVariant: activeGgufVariant ?? undefined,
- // Without the native token the reload validates the display label as a
- // repo and fails.
+ // Without the native token the reload validates the display label as a repo and fails.
nativePathToken: nativeToken ?? undefined,
nativePathExpiresAtMs: nativeExpiry,
isGguf: activeModelIsGguf,
@@ -3427,9 +3365,8 @@ export function ChatPage({
})();
}, [ejectModel, resetArtifacts]);
- // Pins the picker open so a stray click cannot dismiss the step under it.
- // Tour steps only: the effect below shuts anything left pinned once the tour
- // is gone, so anyone else calling this would get a flash and nothing more.
+ // Pins the picker open so a stray click cannot dismiss the step under it. Tour steps only: the
+ // effect below shuts anything left pinned once the tour is gone.
const openModelSelector = useCallback(() => {
setModelSelectorLocked(true);
setModelSelectorOpen(true);
@@ -3463,17 +3400,12 @@ export function ChatPage({
[setSettingsOpen],
);
- // --- Chat page shortcuts ----------------------------------------------
- // The controls these drive are owned by this page.
- // Both controls are the header's, and the header drops them in Compare,
- // where each pane carries its own picker instead. Without the check the
- // chord would toggle state nothing renders.
+ // Both controls are the header's, and the header drops them in Compare, where each pane carries
+ // its own picker. Without the check the chord would toggle state nothing renders.
const headerPickersShown = active && view.mode !== "compare";
- // This page stays mounted under a dialog, so `enabled` still says yes while
- // the header is inert. Without a press-time check the chord opens a popover
- // on the covered surface, or leaves the picker to reappear when the dialog
- // closes. Backgrounded, not "not in the foreground": the composer travels
- // with this header, and an unrendered layout is not a covered one.
+ // This page stays mounted under a dialog, so `enabled` still says yes while the header is inert;
+ // without a press-time check the chord opens a popover on the covered surface.
+ // Backgrounded, not "not in the foreground": an unrendered layout is not a covered one.
const chatCovered = () => isSurfaceBackgrounded(COMPOSER_INPUT_SELECTOR);
useShortcut(
"openModelPicker",
@@ -3483,8 +3415,7 @@ export function ChatPage({
},
{ enabled: headerPickersShown },
);
- // The same condition the switcher renders by, so the chord cannot open a
- // control that is not there and the reset below cannot miss a way it goes.
+ // The same condition the switcher renders by, so the chord cannot open a control that is not there.
const projectSwitcherShown = headerPickersShown && Boolean(currentProjectId);
useShortcut(
"openProjectPicker",
@@ -3494,12 +3425,9 @@ export function ChatPage({
},
{ enabled: projectSwitcherShown },
);
- // A picker left open would come back on the next visit as a ghost of the
- // last one. Off-route is one way to leave it: this page stays mounted. So is
- // entering Compare, and so is a standalone chat taking the project away,
- // both of which unmount the switcher while the page is still on screen.
- // Adjusted during render, as React prescribes for state that has to follow a
- // value it derives from.
+ // A picker left open would come back on the next visit as a ghost. Off-route is one way to leave
+ // it (this page stays mounted), and so is entering Compare or a standalone chat taking the
+ // project away. Adjusted during render, as React prescribes for derived state.
if (!projectSwitcherShown && projectPickerOpen) {
setProjectPickerOpen(false);
}
@@ -3509,8 +3437,8 @@ export function ChatPage({
(delta: number, wrap: boolean) => {
const state = useChatRuntimeStore.getState();
const levels = state.reasoningEffortLevels;
- // Levels stay populated for an enable_thinking model, whose request path
- // drops the effort. Same test as the composer's effort menu.
+ // Levels stay populated for an enable_thinking model, whose request path drops the effort. Same
+ // test as the composer's effort menu.
const isEffort =
state.reasoningStyle === "reasoning_effort" ||
state.reasoningStyle === "enable_thinking_effort";
@@ -3519,9 +3447,8 @@ export function ChatPage({
return;
}
const current = levels.indexOf(state.reasoningEffort);
- // Loading a model that drops the level in force leaves the effort set to
- // one that is gone, and indexOf gives -1. The first press picks the
- // lowest level offered rather than counting a step off a missing index.
+ // Loading a model that drops the level in force leaves the effort set to one that is gone, and
+ // indexOf gives -1, so the first press picks the lowest level offered.
if (current === -1) {
state.setReasoningEffort(levels[0]);
return;
@@ -3591,8 +3518,8 @@ export function ChatPage({
}, [currentProjectId, navigate, search]);
const exitCompare = useCallback(() => {
- // Prefer the explicit save; fall back to the last non-compare view so
- // the composer + menu path also returns where the user started.
+ // Prefer the explicit save; fall back to the last non-compare view so the composer + menu path
+ // also returns where the user started.
const saved = viewBeforeCompareRef.current ?? lastNonCompareViewRef.current;
// No saved view (compare opened by direct URL); fall back to a fresh chat.
if (!saved) {
@@ -3601,8 +3528,8 @@ export function ChatPage({
}
viewBeforeCompareRef.current = null;
navigate({ to: "/chat", search: saved });
- // Restore usage from the last assistant message, only if it matches the
- // active checkpoint, else the relaxed render gate shows stale stats.
+ // Restore usage from the last assistant message, only if it matches the active checkpoint, else
+ // the relaxed render gate shows stale stats.
const threadId =
saved.thread ?? useChatRuntimeStore.getState().activeThreadId;
if (threadId) {
@@ -3620,8 +3547,8 @@ export function ChatPage({
const store = useChatRuntimeStore.getState();
const activeCheckpoint = store.params.checkpoint;
const usageModelId = (usage as { modelId?: unknown }).modelId;
- // Scope by modelId when present; reject if no active checkpoint
- // (model-scoped usage can't be attributed to "nothing").
+ // Scope by modelId when present; reject if no active checkpoint, since model-scoped usage cannot
+ // be attributed to "nothing".
if (typeof usageModelId === "string" && usageModelId) {
if (!activeCheckpoint || usageModelId !== activeCheckpoint) {
return;
@@ -3641,9 +3568,8 @@ export function ChatPage({
) {
return;
}
- // Key by the thread this restore read, like the history loader: the await above can
- // outlast a switch away, and an unkeyed write would file this thread's usage under
- // the incoming one.
+ // Key by the thread this restore read, like the history loader: the await above can outlast a
+ // switch away, and an unkeyed write would file this usage under the incoming thread.
store.setThreadContextUsage(threadId, usage);
if (store.activeThreadId === threadId) {
store.setContextUsage(usage);
@@ -3680,14 +3606,10 @@ export function ChatPage({
)
.flatMap((provider) =>
provider.models.map((model) => {
- // For OpenRouter's free router we know which underlying free
- // model the gateway picked once a stream completes (chat-adapter
- // latches `chunk.model`). Render the chip as
- // `openrouter:<short-chosen>`, dropping the redundant `/free`
- // and the chosen id's org prefix (e.g. openrouter/free +
- // inclusionai/ring-2.6-1t-20260508:free ->
- // openrouter:ring-2.6-1t-20260508:free). The `:free` suffix
- // already conveys "free model".
+ // For OpenRouter's free router the chosen underlying model is latched from `chunk.model`, so
+ // render the chip as `openrouter:<short-chosen>`, dropping the redundant `/free` and the
+ // chosen id's org prefix: inclusionai/ring-2.6-1t-20260508:free becomes
+ // ring-2.6-1t-20260508:free. The `:free` suffix already conveys "free model".
let displayName = model;
if (
provider.providerType === "openrouter" &&
@@ -3712,13 +3634,10 @@ export function ChatPage({
),
[externalProvidersForChat, lastOpenRouterChosenModel],
);
- // `externalModels` above is flat-mapped from `provider.models`, the ids the user ticked,
- // so a model unticked in the connection dialog leaves it exactly like one the provider
- // withdrew. The connection also caches the whole fetched catalogue, which tells the two
- // apart, and the picker needs that to avoid blaming the provider for the user's own edit.
- // Depends on the store value and the gate rather than on `externalProvidersForChat`,
- // which is a plain conditional and so hands every hook that reads it a fresh array each
- // render.
+ // `externalModels` is flat-mapped from `provider.models`, the ids the user ticked, so a model
+ // unticked in the connection dialog looks exactly like one the provider withdrew; the
+ // connection's cached catalogue tells the two apart. Depends on the store value and the gate
+ // rather than on `externalProvidersForChat`, which is a fresh array each render.
const externalConnections = useMemo<ExternalConnectionRef[]>(
() =>
connectionsEnabled
@@ -3776,8 +3695,7 @@ export function ChatPage({
return [...fromLoras, ...localModels];
}, [lorasFromStore, localModels]);
- // Everything the picker can offer right now, so the chat's own model is only proposed
- // when selecting it would work: a deleted model or removed connection drops out.
+ // Everything the picker can offer right now, so the chat's own model is only proposed when selecting it would work.
const selectableModelIds = useMemo(
() =>
new Set<string>([
@@ -3788,10 +3706,9 @@ export function ChatPage({
[models, loraModels, externalModels],
);
- // The picker's own handler, reached the way the picker reaches it: with the row's
- // metadata, not the bare id. A local or fine-tuned row is in neither
- // `/api/models/list` nor the external ids, so without it the switch loads on
- // different arguments than the menu would.
+ // The picker's own handler, reached the way the picker reaches it: with the row's metadata, not
+ // the bare id. A local or fine-tuned row is in neither `/api/models/list` nor the external
+ // ids, so without it the switch loads on different arguments.
const handleSwitchBackToChatModel = useCallback(
(target: ChatModelSwitchTarget) => {
handleCheckpointChange(
@@ -3836,8 +3753,7 @@ export function ChatPage({
}, [active, modelSelectorOpen, refreshDeferredModelInventories]);
useEffect(() => {
- // ChatPage no longer remounts on navigation, so re-check the handoff whenever
- // we return to /chat (e.g. from the training progress "compare in chat" action).
+ // ChatPage no longer remounts on navigation, so re-check the handoff whenever we return to /chat.
if (!active) return;
const handoff = getTrainingCompareHandoff();
if (!handoff) return;
@@ -3867,9 +3783,8 @@ export function ChatPage({
...selection,
...(hasAppliedConfig ? { keepSpeculative: true } : {}),
previousConfig,
- // As on the Hub launch: the runtime mirror carries no launch flags, and
- // the handoff arrives from training with another model resident (or
- // none), so there is nothing for /load to inherit them from.
+ // As on the Hub launch: the runtime mirror carries no launch flags, and the handoff arrives with
+ // another model resident, so there is nothing for /load to inherit them from.
...(remembered ? { config: remembered } : {}),
});
};
@@ -3961,63 +3876,31 @@ export function ChatPage({
);
return (
- // Provides `active` to ChatRuntimeProvider (drops the message views/composers
- // while off-route, keeping the runtime alive) and to the compare chrome.
+ // Provides `active` to ChatRuntimeProvider (drops the message views while off-route, keeping the
+ // runtime alive) and to the compare chrome.
<ChatActiveContext.Provider value={active}>
<div className="flex min-h-0 min-w-0 flex-1 basis-0 overflow-hidden bg-background">
- {/* Portaled surfaces render to document.body, escaping the parent's hidden
- wrapper, so gate them on `active` to keep them off other tabs. */}
+ {/* Portaled surfaces render to document.body, escaping the parent's hidden wrapper, so gate them
+ on `active` to keep them off other tabs. */}
{active && <GuidedTour {...tour.tourProps} />}
- {/* Single app-level mount for the Bypass permissions warning. It is driven
- by global store state, so it must live at one stable root (not inside a
- Composer) -- otherwise Compare mode's multiple composers would each
- render their own copy and the shared-composer menu would have none. It
- also portals to body, so gate it on `active` like the tour above. */}
+ {/* Single app-level mount for the Bypass permissions warning: it is driven by global store state,
+ so it must live at one stable root, or Compare mode's composers would each render a copy.
+ It also portals to body, so gate it on `active`. */}
{active && <BypassPermissionsConfirmDialog />}
- {/* The MCP servers dialog: its chord has to work before MCP is switched
- on, and the pill that used to own it only renders once it is. Mounted
- through the route change, not gated on `active`, so it can close
- itself on the way out instead of returning with the tab. */}
+ {/* The MCP servers dialog: its chord has to work before MCP is switched on, and the pill that
+ used to own it only renders once it is. Mounted through the route change so it can close
+ itself on the way out. */}
<McpServersDialogMount />
- {/* `--studio-chat-notice-height` is 0 until ChatModelNotice is on screen; the
- thread viewport adds it to the top padding it reserves for the header, so
- without it the first message reads under an opaque bar. Declared on the
- nearest ancestor of BOTH so the two cannot disagree about its height.
-
- `has-[>...]`, not `has-[...]`. This element is an ancestor of every
- message in the thread, and a `:has()` with a DESCENDANT argument must
- be re-checked whenever anything is inserted or removed anywhere in its
- subtree. Answering it means walking that subtree, so here it walks the
- whole thread on every mutation.
-
- It is a traversal and not a restyle: Blink's own
- `UpdateLayoutTree.elementCount` for one inserted span is 1 once both
- this rule and the sidebar wrapper's are in their child form, 2 with
- one of them still in descendant form and 3 with both, i.e. only the
- subjects are ever restyled. That is why containment cannot help and
- why the inheriting custom property this rule declares is not the
- carrier either: the same rule declaring a non-inherited `background`
- costs the same. Related to #9328 by family, not by mechanism.
-
- Measured at the 500K rung, corpus 23cd2464, on a 357,843-element
- thread, as the cost of appending one EMPTY span inside a message: 17.5
- and 18.6 ms in two concurrent arms with this rule as it was, 9.8 and
- 9.3 ms with this rule alone deleted, and 0.10 ms with this rule and
- the one on the sidebar wrapper both deleted. The same span appended to
- <body> costs 0.10 ms either way, so the cost is the thread being under
- the subject and nothing else. Chromium only: WebKitGTK and Firefox are
- flat across all four selector forms, so this neither helps nor hurts
- the engine Unsloth uses on Linux.
-
- ChatModelNotice renders a direct child of this element (see below), so
- the child combinator matches exactly what the descendant form matched.
- If it is ever moved deeper, the notice's height stops being reserved
- and the first message reads under the bar again, which is why
- `tests/thread-ancestor-has-scope.test.ts` asserts the depth. */}
+ {/* `--studio-chat-notice-height` is 0 until ChatModelNotice is on screen; the thread viewport
+ adds it to the top padding, so without it the first message reads under an opaque bar.
+ Declared on the nearest ancestor of BOTH so the two cannot disagree. `has-[>...]`, not
+ `has-[...]`: a `:has()` with a DESCENDANT argument is re-checked on any insertion in the
+ subtree, which walks the whole thread on every mutation - 17.5 ms per append on a 357k-
+ element thread, against 0.10 ms without this rule (Chromium). ChatModelNotice renders a
+ direct child, which tests/thread-ancestor-has-scope.test.ts asserts. */}
<div className="relative flex min-h-0 min-w-0 flex-1 basis-0 flex-col overflow-hidden has-[>[data-chat-model-notice]]:[--studio-chat-notice-height:2.25rem]">
<NativeModelDropOverlay state={nativeModelDropState} />
- {/* Fade under the top bar so messages dissolve as they scroll
- beneath it, instead of a hard cut. */}
+ {/* Fade under the top bar so messages dissolve as they scroll beneath it, instead of a hard cut. */}
{view.mode !== "compare" && (
<div
aria-hidden
@@ -4063,9 +3946,8 @@ export function ChatPage({
externalModels={externalModels}
externalConnections={externalConnections}
value={inferenceParams.checkpoint}
- // Resident, not merely picked: an image or video load evicts
- // the chat model and leaves this selection behind, so the tick
- // stayed on a model the backend had already released.
+ // Resident, not merely picked: an image or video load evicts the chat model and leaves this
+ // selection behind, so the tick stayed on a released model.
loaded={chatModelLoaded({
checkpoint: inferenceParams.checkpoint,
isExternalModel: isExternalModelId(
@@ -4285,12 +4167,11 @@ export function ChatPage({
/>
)}
- {/* One provider shared by the project and single views, never keyed on thread /
- nonce / project, so switching between them reattaches the live run instead of
- remounting the runtime and aborting generation (#8908). Any key here brings
- the bug back. Compare renders as a sibling, not in its place, so it hides
- this rather than unmounting it -- ComparePane builds its own providers and
- useRemoteThreadListRuntime throws when they nest. */}
+ {/* One provider shared by the project and single views, never keyed on thread / nonce / project,
+ so switching between them reattaches the live run instead of remounting the runtime and
+ aborting generation (#8908). Any key here brings the bug back. Compare renders as a
+ sibling so it hides this rather than unmounting it, since ComparePane builds its own
+ providers and nesting throws. */}
{baseView ? (
<div
className={
diff --git a/studio/frontend/src/features/chat/chat-project-scope.ts b/studio/frontend/src/features/chat/chat-project-scope.ts
index 56ad9919344..f8c2b343d4b 100644
--- a/studio/frontend/src/features/chat/chat-project-scope.ts
+++ b/studio/frontend/src/features/chat/chat-project-scope.ts
@@ -3,12 +3,10 @@
import { createContext, useContext } from "react";
-/**
- * The project the runtime provider on this subtree is showing, for the composer to stamp
- * sends with (see `chat-thread-project-claim.ts`). Context rather than the store's
- * `activeProjectId`: this is the same value the adapter files threads under, and one source
- * for both is what makes the claim and the fallback agree.
- */
+/** The project the runtime provider on this subtree is showing, for the composer to stamp sends
+ * with (see `chat-thread-project-claim.ts`). Context rather than the store's `activeProjectId`:
+ * this is the same value the adapter files threads under, and one source for both is what makes
+ * the claim and the fallback agree. */
export const ChatProjectScopeContext = createContext<string | null>(null);
export function useChatProjectScope(): string | null {
diff --git a/studio/frontend/src/features/chat/chat-providers-dialog.tsx b/studio/frontend/src/features/chat/chat-providers-dialog.tsx
index fd5fcd0f5c4..4913dea94d1 100644
--- a/studio/frontend/src/features/chat/chat-providers-dialog.tsx
+++ b/studio/frontend/src/features/chat/chat-providers-dialog.tsx
@@ -168,19 +168,16 @@ export function codexCapabilitiesWithPlanModels(
stored: Record<string, { vision?: boolean; studio_tools?: boolean }> | undefined,
): Record<string, { vision?: boolean; studio_tools?: boolean }> | null {
if (!listed || listed.source !== "subscription") return null;
- // The registry row is what says which slugs the plan is allowed to describe and what
- // the provider-wide studio_tools answer is. Without it every seed model reads as one
- // the registry never listed, so the plan's modalities are written over the registry's
- // for slugs the registry does describe, and the wildcard entry that carries
- // studio_tools for the whole provider type is dropped rather than rewritten. Both are
- // persisted, so an editor opened before the registry finished loading, or after that
- // fetch failed, leaves the composer wrong until the next successful sync. Learning
- // nothing here is the honest answer; the next fetch with a registry in hand records it.
+ // The registry row says which slugs the plan may describe and what the provider-wide
+ // studio_tools answer is. Without it every seed model reads as unlisted, so the plan's
+ // modalities overwrite the registry's and the wildcard studio_tools entry is dropped.
+ // Both are persisted, so an editor opened before the registry loaded leaves the composer
+ // wrong until the next sync. Learning nothing here is the honest answer.
if (!entry) return null;
const registryCapabilities = entry.model_capabilities ?? {};
- // The map is keyed by provider type, not by connection, so it must start from what
- // is already learned: a second ChatGPT connection lists its own slugs, and rebuilding
- // from the registry alone would drop the first one's.
+ // The map is keyed by provider type, not connection, so it must start from what is already
+ // learned: a second ChatGPT connection lists its own slugs, and rebuilding from the
+ // registry alone would drop the first one's.
const capabilities = mergeLearnedModelCapabilities(
stored,
registryCapabilities,
@@ -189,11 +186,10 @@ export function codexCapabilitiesWithPlanModels(
// Hidden entries are described too: they stay selectable, so the composer needs their
// modalities as much as the offered ones.
for (const model of listed.known ?? listed.models) {
- // Only the plan describes a slug the registry never listed. Without it the
- // composer reads "unknown" as "allowed" and offers image attachments that the
- // backend then refuses on every send. A catalog entry carrying no modality list
- // normalizes to null upstream and the backend gate is bool(vision), so the honest
- // mirror here is false rather than recording nothing at all.
+ // Only the plan describes a slug the registry never listed. Without it the composer reads
+ // "unknown" as "allowed" and offers image attachments the backend refuses on every send.
+ // A catalog entry with no modality list normalizes to null upstream and the backend gate
+ // is bool(vision), so the honest mirror is false rather than nothing at all.
if (!(model.id in registryCapabilities)) {
capabilities[model.id] = {
...capabilities[model.id],
@@ -210,9 +206,9 @@ export function resolveCodexPickerModels(
savedModels: string[],
listed: CodexSubscriptionModels | null,
): { catalog: string[]; selected: string[] } {
- // Only the plan's own catalog can retire a saved slug. The backend answers with the
- // curated seed when it could not reach upstream, so treating that as the catalog
- // would drop a saved model and the next unrelated save would make the loss stick.
+ // Only the plan's own catalog can retire a saved slug. The backend answers with the curated
+ // seed when it could not reach upstream, so treating that as the catalog would drop a
+ // saved model and the next unrelated save would make the loss stick.
const planListed = listed?.source === "subscription" && listed.models.length > 0;
if (!planListed) {
const catalog = [...new Set([...curated, ...savedModels])];
@@ -220,9 +216,9 @@ export function resolveCodexPickerModels(
return { catalog, selected: savedModels.filter((model) => offered.has(model)) };
}
const offeredIds = listed.models.map((model) => model.id);
- // A saved slug the plan still returns is kept even when it is no longer offered:
- // "hide" retires a model from the picker, it does not revoke one already in use.
- // Only a slug the plan does not return at all is retired from the selection.
+ // A saved slug the plan still returns is kept even when no longer offered: "hide" retires a
+ // model from the picker, it does not revoke one in use. Only a slug the plan does not
+ // return at all is retired.
const known = new Set((listed.known ?? listed.models).map((model) => model.id));
const selected = savedModels.filter((model) => known.has(model));
const catalog = [...new Set([...offeredIds, ...selected])];
@@ -236,8 +232,8 @@ export function ChatProvidersSettings({
}: ChatProvidersSettingsProps) {
const providersRef = useRef(providers);
const seededProviderTypeRef = useRef<string | null>(null);
- // Latches the one-shot auto-open below. Every navigation the user drives sets
- // it too, so a slow first sync cannot pull them back into the form.
+ // Latches the one-shot auto-open below. Every user-driven navigation sets it too, so a slow
+ // first sync cannot pull them back into the form.
const autoOpenedAddFormRef = useRef(false);
const [page, setPage] = useState<"list" | "form">("list");
const [providerType, setProviderType] = useState<string>("");
@@ -281,13 +277,12 @@ export function ChatProvidersSettings({
providerType,
editingProviderId ? editingBackendProviderType : null,
);
- // llama.cpp hides the key field. Ollama and vLLM show an optional key:
- // Ollama cloud and secured vLLM need one; local servers leave it empty.
+ // llama.cpp hides the key field. Ollama and vLLM show an optional key: Ollama cloud and
+ // secured vLLM need one; local servers leave it empty.
const showReasoningToggle = supportsProviderReasoningToggle(providerType);
- // Unsloth runs Search, Code, MCP and RAG on this machine for any provider that
- // advertises the capability, with no extra opt-in. Say so where the
- // connection is created: the tool results also travel back to the provider as
- // the next turn's input, which is not obvious from "connect a model".
+ // Unsloth runs Search, Code, MCP and RAG on this machine for any provider advertising the
+ // capability, with no extra opt-in. Say so where the connection is created: tool results
+ // also travel back to the provider as the next turn's input.
const runsStudioToolsLocally =
providerModelSupportsStudioTools(
toExternalBackendProviderType(providerType),
@@ -398,9 +393,8 @@ export function ChatProvidersSettings({
}
return;
}
- // Seed default_models only for curated providers (catalog too large to
- // enumerate). Remote cloud providers and local OpenAI-compat presets stay
- // empty until the user clicks "Load available models".
+ // Seed default_models only for curated providers (catalog too large to enumerate). Remote
+ // cloud providers and local OpenAI-compat presets stay empty until "Load available models".
const seedDefaults = entry.model_list_mode === "curated";
setAvailableModels(seedDefaults ? [...entry.default_models] : []);
setSelectedModelIds([]);
@@ -432,8 +426,8 @@ export function ChatProvidersSettings({
]);
if (!isMounted) return;
syncSucceeded = true;
- // Hidden entries are fetched for their capabilities only; the dropdown
- // surfaces them through CUSTOM_PROVIDER_PRESETS instead.
+ // Hidden entries are fetched for their capabilities only; the dropdown surfaces them through
+ // CUSTOM_PROVIDER_PRESETS instead.
const selectableRegistry = registryRows.filter((entry) => !entry.hidden);
setRegistry(selectableRegistry);
setProviderType((current) => {
@@ -446,14 +440,12 @@ export function ChatProvidersSettings({
}
return registryRows[0]?.provider_type ?? "";
});
- // Trust the backend response. An empty array means every connection was
- // removed (often from another tab); mirror that locally, else stale
- // entries become un-removable here until localStorage is cleared.
+ // Trust the backend response. An empty array means every connection was removed, often from
+ // another tab; mirror that locally, else stale entries are un-removable here.
onProvidersChange(syncedProviders);
- // An empty list never says what this page is for, so open the form
- // instead. Reads the synced response, not the local snapshot, so a
- // stale empty list cannot flash the form at an existing user. Once
- // only, else the focus re-sync would pull the user back here.
+ // An empty list never says what this page is for, so open the form instead. Reads the synced
+ // response, not the local snapshot, so a stale empty list cannot flash the form at an
+ // existing user. Once only, else the focus re-sync would pull the user back here.
if (!autoOpenedAddFormRef.current) {
autoOpenedAddFormRef.current = true;
if (syncedProviders.length === 0 && selectableRegistry.length > 0) {
@@ -461,8 +453,7 @@ export function ChatProvidersSettings({
}
}
} catch (error) {
- // Only surface a toast for real failures, not for the silent
- // background re-sync on tab focus.
+ // Only surface a toast for real failures, not for the silent background re-sync on tab focus.
if (showSpinner) {
const message =
error instanceof Error ? error.message : "Unknown error";
@@ -477,9 +468,8 @@ export function ChatProvidersSettings({
return syncSucceeded;
};
void syncFromBackend();
- // Re-sync silently when the tab regains focus so deletes made in
- // another browser propagate without forcing the user to reopen the
- // dialog. Skip when the document is hidden to avoid background work.
+ // Re-sync silently on focus so deletes made in another browser propagate without reopening
+ // the dialog. Skip when the document is hidden to avoid background work.
const handleVisibilityChange = () => {
if (typeof document === "undefined" || document.hidden) return;
void syncFromBackend({ showSpinner: false });
@@ -498,9 +488,9 @@ export function ChatProvidersSettings({
}, [onProvidersChange]);
function resetForm() {
- // Any form transition retires an in-flight Codex catalog request. Its spinner goes
- // with it: the state is shared across forms, so leaving it set would hold the next
- // form's Load and Save controls disabled until the abandoned request times out.
+ // Any form transition retires an in-flight Codex catalog request, and its spinner with it:
+ // the state is shared across forms, so leaving it set would hold the next form's Load and
+ // Save disabled until the abandoned request times out.
codexCatalogRequestRef.current += 1;
setModelsLoading(false);
setEditingProviderId(null);
@@ -625,9 +615,8 @@ export function ChatProvidersSettings({
return;
}
if (providerType === "openai_codex") {
- // Registry-curated, but the real catalog comes from the plan, so this control has
- // to refetch it. Falling into the branch below would leave the only way to retry a
- // failed catalog fetch closing and reopening the form.
+ // Registry-curated, but the real catalog comes from the plan, so this control has to refetch
+ // it. The branch below would leave closing and reopening the form as the only retry.
if (!editingProviderId) {
toast.info("Connect this ChatGPT subscription to load the models it can reach.");
return;
@@ -636,17 +625,17 @@ export function ChatProvidersSettings({
(candidate) => candidate.id === editingProviderId,
);
setModelsLoading(true);
- // The live checkboxes, not the persisted list: a manual reload re-reads the
- // catalog, it does not revert edits the user has not saved yet.
+ // The live checkboxes, not the persisted list: a manual reload re-reads the catalog, it does
+ // not revert unsaved edits.
const applied = await applyCodexSubscriptionModels(
editingProviderId,
selectedModelIds,
provider?.authStatus,
true,
).catch(() => true);
- // Only the request that still owns the form clears the shared flag. An abandoned
- // one would re-enable Save while the newer request is out, letting the form be
- // saved and then mutated when that request lands.
+ // Only the request that still owns the form clears the shared flag. An abandoned one would
+ // re-enable Save while the newer request is out, letting the form be saved and then
+ // mutated when that request lands.
if (applied) setModelsLoading(false);
return;
}
@@ -679,9 +668,8 @@ export function ChatProvidersSettings({
const registryDefaults = supportsRemoteModelCatalog(providerType)
? []
: (registryByType.get(providerType)?.default_models ?? []);
- // Union of registry defaults + fetched models, defaults first so any
- // curated picks (e.g. claude-haiku-4-5) always show even when the
- // provider's /models endpoint omits them.
+ // Union of registry defaults and fetched models, defaults first so curated picks still show
+ // when the provider's /models endpoint omits them.
const modelIds = pruneProviderModelIds(providerType, [
...new Set(
[
@@ -1071,8 +1059,8 @@ export function ChatProvidersSettings({
}
}
if (request !== codexCatalogRequestRef.current) {
- // The form moved to another connection while this catalog was in flight, and
- // applying it here would save the first connection's models onto the second.
+ // The form moved to another connection while this catalog was in flight, and applying it
+ // here would save the first connection's models onto the second.
return false;
}
const capabilities = codexCapabilitiesWithPlanModels(
@@ -1082,8 +1070,8 @@ export function ChatProvidersSettings({
);
if (capabilities) setProviderModelCapabilities("openai_codex", capabilities);
if (listed?.source === "reauthorization_required") {
- // The backend already marked the bundle; resync so the connect panel offers
- // Reconnect instead of leaving the connection looking healthy.
+ // The backend already marked the bundle; resync so the connect panel offers Reconnect
+ // instead of leaving the connection looking healthy.
void syncExternalProvidersFromBackend(providersRef.current)
.then((synced) => {
providersRef.current = synced;
@@ -1093,17 +1081,16 @@ export function ChatProvidersSettings({
}
const picker = resolveCodexPickerModels(curated, savedModels, listed);
if (refresh && listed?.source !== "subscription") {
- // A curated fallback is not the account's catalog, so it retires nothing: keep
- // whatever is on screen, including a model checked while the request was out.
+ // A curated fallback is not the account's catalog, so it retires nothing: keep what is on
+ // screen, including a model checked while the request was out.
setAvailableModels((previous) => [...new Set([...picker.catalog, ...previous])]);
setManualModelIds("");
return true;
}
setAvailableModels(picker.catalog);
if (refresh) {
- // The checkboxes stay live while the request is out, so reconcile against the
- // latest selection rather than the snapshot taken when the reload began. This is
- // what the remote-catalog path above already does.
+ // The checkboxes stay live while the request is out, so reconcile against the latest
+ // selection rather than the snapshot taken when the reload began.
const offered = new Set(picker.catalog);
setSelectedModelIds((previous) => previous.filter((id) => offered.has(id)));
} else {
@@ -1114,8 +1101,8 @@ export function ChatProvidersSettings({
}
async function editProvider(provider: ExternalProviderConfig) {
- // Switching connections retires an in-flight catalog request, including on the
- // branches below that never reach applyCodexSubscriptionModels.
+ // Switching connections retires an in-flight catalog request, including on the branches
+ // below that never reach applyCodexSubscriptionModels.
codexCatalogRequestRef.current += 1;
setModelsLoading(false);
setEditingProviderId(provider.id);
@@ -1132,8 +1119,8 @@ export function ChatProvidersSettings({
setClearApiKeyRequested(false);
setShowApiKey(false);
setBaseUrlDraft(provider.baseUrl);
- // Seeded at the floor: parseMaxOutputTokens throws below it, so a row stored under
- // one would fail every unrelated edit. The resolver already reads it as the floor.
+ // Seeded at the floor: parseMaxOutputTokens throws below it, so a row stored under one would
+ // fail every unrelated edit. The resolver already reads it as the floor.
setMaxOutputTokensDraft(
provider.maxOutputTokens == null
? ""
@@ -1178,9 +1165,9 @@ export function ChatProvidersSettings({
return;
}
if (provider.authKind === "chatgpt_oauth") {
- // The form is already on screen and the reset emptied the catalog, so this fetch
- // needs the same spinner the manual reload has. Without it the editor looks like a
- // connection with no models, and Save answers "load available models first".
+ // The form is on screen and the reset emptied the catalog, so this fetch needs the same
+ // spinner the manual reload has. Without it the editor looks like a connection with no
+ // models, and Save answers "load available models first".
setModelsLoading(true);
const applied = await applyCodexSubscriptionModels(
provider.id,
@@ -1246,8 +1233,7 @@ export function ChatProvidersSettings({
const savedKey = provider.hasApiKey
? ""
: getExternalProviderApiKey(provider.id).trim();
- // Hosted registry providers require keys. Local OpenAI-compatible presets
- // may be keyless.
+ // Hosted registry providers require keys. Local OpenAI-compatible presets may be keyless.
if (
!savedKey &&
!provider.hasApiKey &&
@@ -1632,21 +1618,15 @@ export function ChatProvidersSettings({
authStatus={providers.find((provider) => provider.id === editingProviderId)?.authStatus}
ensureProvider={ensureCodexProvider}
onChanged={async () => {
- // The form can move while this sync is out, and the id below is the one
- // captured when the flow started. Every transition retires the catalog
- // generation, so a change here means this continuation is for a form the
- // user has already left.
- // Taking a generation rather than reading one, the way every other
- // transition does. Reading it shares a ticket with a catalog request that
- // is already out, and an authorization change retires that request
- // anyway: leaving it owning the form let it land, clear the shared
- // loading flag and re-enable Save while this continuation was still
- // syncing, which is the state the flag exists to prevent.
+ // The form can move while this sync is out, and the id below was captured when the flow
+ // started. Every transition retires the catalog generation, so a change here means this
+ // continuation is for a form the user has left. Taking a generation rather than reading
+ // one: sharing a ticket with an in-flight catalog request let it land, clear the shared
+ // loading flag and re-enable Save while this continuation was still syncing.
const request = ++codexCatalogRequestRef.current;
const changedProviderId = editingProviderId;
- // Save is gated on this flag. Leaving it clear here lets the connection
- // be saved with the pre-authorization seed selection before the plan's
- // own catalog arrives, and those slugs then fail on every send.
+ // Save is gated on this flag. Leaving it clear lets the connection be saved with the
+ // pre-authorization seed selection, and those slugs then fail on every send.
setModelsLoading(true);
let owned = true;
try {
@@ -1668,9 +1648,9 @@ export function ChatProvidersSettings({
);
}
} catch (error) {
- // A failed sync still has to answer the ownership question: the user
- // can cancel this form while it is out and start another request, and
- // clearing the flag then would re-enable Save during that one.
+ // A failed sync still has to answer the ownership question: the user can cancel this form
+ // while it is out and start another request, and clearing the flag then would re-enable
+ // Save during that one.
owned = request === codexCatalogRequestRef.current;
throw error;
} finally {
diff --git a/studio/frontend/src/features/chat/chat-settings-sheet.tsx b/studio/frontend/src/features/chat/chat-settings-sheet.tsx
index 7bad7254bfc..a0bf66fd400 100644
--- a/studio/frontend/src/features/chat/chat-settings-sheet.tsx
+++ b/studio/frontend/src/features/chat/chat-settings-sheet.tsx
@@ -285,17 +285,11 @@ function CollapsibleSection({
first = false,
}: {
label: string;
- /**
- * When set, the label becomes an external link (e.g. the feature's GitHub PR)
- * instead of part of the toggle. The chevron still toggles, so link and button
- * are siblings rather than an <a> nested in a <button> (invalid HTML).
- */
+ /** When set, the label becomes an external link instead of part of the toggle. The chevron
+ * still toggles, so link and button are siblings rather than an <a> inside a <button>. */
labelHref?: string;
- /**
- * Optional control rendered before the chevron (e.g. an edit icon). The
- * label and chevron become sibling toggles so the action is not a button
- * nested in a button.
- */
+ /** Optional control rendered before the chevron. The label and chevron become sibling toggles
+ * so the action is not a button nested in a button. */
headerAction?: ReactNode;
/** When set, clicking the label runs this instead of toggling collapse. */
onLabelClick?: () => void;
@@ -396,29 +390,19 @@ interface ChatSettingsPanelProps {
onParamsChange: (params: InferenceParams) => void;
modelConfig?: ReactNode;
isExternalModel?: boolean;
- /**
- * Sampling-param capabilities for the active external provider, or `null` for
- * local models (every knob rendered). Drives per-param sampling visibility.
- */
+ /** Sampling-param capabilities for the active external provider, or `null` for local models
+ * (every knob rendered). Drives per-param sampling visibility. */
providerCapabilities?: ProviderCapabilities | null;
activeExternalProvider?: ExternalProviderConfig | null;
onExternalProviderChange?: (provider: ExternalProviderConfig) => void;
- /**
- * Backend provider type for the active external model (e.g. "kimi",
- * "anthropic", "openai"), or `null` for local models. Drives the per-provider
- * Max Tokens floor in the slider.
- */
+ /** Backend provider type for the active external model, or `null` for local models. Drives the
+ * per-provider Max Tokens floor in the slider. */
externalProviderType?: string | null;
}
-/**
- * Copy for the amber "running without speculative decoding" notice. Mirrors
- * InferenceStatusResponse.spec_fallback_reason.
- *
- * Out of the JSX so the three independent dimensions (reason, drafter kind,
- * local vs remote) read as a table rather than a five-level nested ternary,
- * and so each string is directly testable.
- */
+/** Copy for the amber "running without speculative decoding" notice. Mirrors
+ * InferenceStatusResponse.spec_fallback_reason. Out of the JSX so the three dimensions
+ * read as a table rather than a five-level nested ternary, and so each string is testable. */
function specFallbackMessage({
reason,
drafter,
@@ -434,22 +418,15 @@ function specFallbackMessage({
case "mla_mtp_disabled":
return "MTP is disabled by default for this model architecture because it currently runs slower than standard decoding. Choose MTP in the model picker to force it.";
case "mtp_partial_offload":
- // Not the default copy: this build does support MTP, so telling the user to
- // update llama.cpp would name the wrong cause and the wrong remedy. Says
- // what the placement IS rather than that the model could not fit: a Manual
- // layer count is a partial placement the user picked, on a card that may
- // have room for all of it, and there the useful remedy is more layers.
- //
- // Describes the placement MTP WOULD need, not the one that ends up running.
- // The partial verdict is priced with MTP's rollback reserve still in it, so
- // on the fit path llama.cpp can put every layer on the GPU once MTP is off
- // -- claiming "only part of this model is on the GPU" would then describe a
- // placement the load does not have and recommend one it already has. This
- // wording stays true both there and at a fixed partial layer count.
+ // Not the default copy: this build does support MTP, so telling the user to update llama.cpp
+ // would name the wrong cause. Says what the placement IS rather than that the model could
+ // not fit, since a Manual layer count is a placement the user picked and more layers is the
+ // useful remedy. Describes the placement MTP WOULD need, not the one that runs: the partial
+ // verdict is priced with MTP's rollback reserve still in it.
return "With MTP on, part of this model would have to run on the CPU, where MTP's extra state costs more than the drafting wins back, so Auto turned it off for this load. Give the GPU room for every layer to get it back, or choose MTP in Settings to force it.";
case "drafter_no_vram":
- // Not "without speculative decoding": the backend puts zero-VRAM ngram-mod
- // in the drafter's place where the build has it, so only the drafter is off.
+ // Not "without speculative decoding": the backend puts zero-VRAM ngram-mod in the drafter's
+ // place where the build has it, so only the drafter is off.
return `This model fits in VRAM but its ${drafter} drafter does not, so Auto kept your context length and turned ${drafter} off for this load. Choose ${drafter} in Settings to force it, at a smaller context.`;
case "runtime_error":
return `${drafter} could not start for this model on the installed llama.cpp build, so it is running without speculative decoding.`;
@@ -495,9 +472,8 @@ export function ChatSettingsPanel({
setWidth: setSettingsWidth,
resetWidth: resetSettingsWidth,
} = useChatSettingsWidth();
- // Local models show every knob; providerCapabilities is only consulted when
- // isExternalModel. Unknown providers fall back to the OpenAI-compat shape via
- // getProviderCapabilities, so these flags never undercount support.
+ // Local models show every knob; providerCapabilities is only consulted when isExternalModel.
+ // Unknown providers fall back to the OpenAI-compat shape, so these flags never undercount.
const showTemperature =
!isExternalModel || Boolean(providerCapabilities?.temperature);
const showTopP = !isExternalModel || Boolean(providerCapabilities?.topP);
@@ -527,9 +503,9 @@ export function ChatSettingsPanel({
const activeModel = useChatRuntimeStore(
(s) => s.models.find((m) => m.id === currentCheckpoint) ?? null,
);
- // Same call the request body makes, on the same summary, so the panel cannot offer a
- // seed the body drops. An external selection carries an `external::` id that no local
- // entry matches, so the summary answers that case without a separate guard.
+ // Same call the request body makes, on the same summary, so the panel cannot offer a seed the
+ // body drops. An external selection carries an `external::` id no local entry matches, so
+ // the summary answers that case without a separate guard.
const showSeed = modelReadsSamplingSeed(activeModel);
const platformDeviceType = usePlatformStore((s) => s.deviceType);
// Unified memory, not just Darwin: an Intel Mac spills to system RAM like a PC.
@@ -540,11 +516,10 @@ export function ChatSettingsPanel({
platformDeviceType,
platformChatOnlyReason,
);
- // activeModelIsLocal is the backend's own classification and covers native
- // picks. Two things must not decide this: activeNativePathToken, which
- // status reconciliation keeps across a switch to a remote GGUF (no
- // replacement token exists), and a bare .gguf suffix, since the backend
- // reads a one-slash org/name.gguf as a repository id, not a file.
+ // activeModelIsLocal is the backend's own classification and covers native picks. Two things
+ // must not decide it: activeNativePathToken, which status reconciliation keeps across a
+ // switch to a remote GGUF, and a bare .gguf suffix, since the backend reads a one-slash
+ // org/name.gguf as a repository id, not a file.
const isLocalGguf =
isGguf && (activeModelIsLocal || isLocalModelPath(currentCheckpoint ?? ""));
const maxContextLength = useChatRuntimeStore(
@@ -644,18 +619,16 @@ export function ChatSettingsPanel({
const [systemPromptDraft, setSystemPromptDraft] = useState("");
const [systemVariablesDraft, setSystemVariablesDraft] = useState("");
const [systemVariablesOpen, setSystemVariablesOpen] = useState(false);
- // Raw keystrokes while the Seed box is being typed into, null once committed.
- // Clamping straight into params would rewrite the box mid-entry, so the commit
- // waits for blur the way NumericValueInput's does.
+ // Raw keystrokes while the Seed box is being typed into, null once committed. Clamping
+ // straight into params would rewrite the box mid-entry, so the commit waits for blur.
const [seedDraft, setSeedDraft] = useState<string | null>(null);
// What blur would commit, available before it runs. Clicking Save blurs the box during
- // mousedown, but React has not re-rendered by the time that button's onClick fires, so
- // a handler reading `params` there still sees the seed from before the entry.
- // NumericValueInput bridges the same gap with its imperative commit().
+ // mousedown, but React has not re-rendered by the time onClick fires, so a handler reading
+ // `params` still sees the seed from before the entry. NumericValueInput bridges the same gap.
const committedSeed = useMemo<number | null>(() => {
if (seedDraft === null) return params.seed ?? null;
- // Measured after the padding: a zero-padded seed is short enough to keep, and
- // truncating instead of clamping would rewrite it.
+ // Measured after the padding: a zero-padded seed is short enough to keep, and truncating
+ // instead of clamping would rewrite it.
const digits = seedDraft.replace(/^0+(?=\d)/, "");
if (digits === "") return null;
return digits.length > 10
@@ -669,8 +642,8 @@ export function ChatSettingsPanel({
: { ...params, seed: committedSeed },
[committedSeed, params],
);
- // Removing a focused element fires no blur, so a draft the user walked away from
- // would keep reporting through committedSeed with the field gone.
+ // Removing a focused element fires no blur, so a draft the user walked away from would keep
+ // reporting through committedSeed with the field gone.
useEffect(() => {
setSeedDraft(null);
}, [currentCheckpoint, showSeed]);
@@ -679,16 +652,15 @@ export function ChatSettingsPanel({
const [systemPromptOverflows, setSystemPromptOverflows] = useState(false);
const promptObserverRef = useRef<ResizeObserver | null>(null);
const measurePromptRef = useRef<() => void>(() => {});
- // The section unmounts its textarea when collapsed, so observe through a
- // callback ref: a stored observer would cling to the detached node and the
- // remounted one would never be measured.
+ // The section unmounts its textarea when collapsed, so observe through a callback ref: a
+ // stored observer would cling to the detached node and the remounted one is never measured.
const attachPromptBox = useCallback((node: HTMLTextAreaElement | null) => {
systemPromptBoxRef.current = node;
promptObserverRef.current?.disconnect();
promptObserverRef.current = null;
if (!node || typeof ResizeObserver === "undefined") return;
- // Resizing rewraps the prompt, and a drag changes the width through a
- // custom property without re-rendering, so watch the box itself.
+ // Resizing rewraps the prompt, and a drag changes the width through a custom property
+ // without re-rendering, so watch the box itself.
const observer = new ResizeObserver(() => measurePromptRef.current());
observer.observe(node);
promptObserverRef.current = observer;
@@ -844,8 +816,8 @@ export function ChatSettingsPanel({
const setSeed = set("seed");
// Lower a live Max Tokens that no longer fits the connection's cap.
- // `resolveExternalMaxTokensClamp` documents why an unresolved provider must not be
- // read as the 32,768 fallback.
+ // `resolveExternalMaxTokensClamp` documents why an unresolved provider must not be read as
+ // the 32,768 fallback.
useEffect(() => {
const clampedMaxTokens = resolveExternalMaxTokensClamp({
settingsHydrated,
@@ -879,8 +851,8 @@ export function ChatSettingsPanel({
presetParams: Parameters<typeof applyPresetParams>[1],
): InferenceParams {
const nextParams = applyPresetParams(params, presetParams);
- // Same reason the effect waits for a provider: without one `maxTokensMax` is the
- // fallback, so applying a preset here would lower the value for good.
+ // Same reason the effect waits for a provider: without one `maxTokensMax` is the fallback, so
+ // applying a preset here would lower the value for good.
if (!isExternalModel || activeExternalProvider == null) return nextParams;
return {
...nextParams,
@@ -1436,8 +1408,8 @@ export function ChatSettingsPanel({
value={currentSystemPrompt}
onChange={(e) => set("systemPrompt")(e.target.value)}
onMouseDown={(e) => {
- // Overflowing prompt: click opens the popup editor instead.
- // While focused, clicks still move the caret normally.
+ // Overflowing prompt: click opens the popup editor instead. While focused, clicks still move
+ // the caret normally.
if (
systemPromptOverflows &&
document.activeElement !== e.currentTarget
diff --git a/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts b/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts
index 2e20ef740b0..cbf25e01cce 100644
--- a/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts
+++ b/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts
@@ -83,8 +83,8 @@ export function createChatModelHistoryReader(
}
emit(chatModelSwitchTargetFromThread(thread));
},
- // every history event for this thread lands here, renames and archives included,
- // so the model is compared before a new object is handed to the caller.
+ // Every history event for this thread lands here, renames and archives included, so the model is
+ // compared before a new object is handed to the caller.
applyUpdate(thread: ChatModelThreadSnapshot): void {
if (disposed || thread.id !== threadId) {
return;
@@ -98,15 +98,12 @@ export function createChatModelHistoryReader(
};
}
-/**
- * The picker metadata a "Switch back" has to carry, or undefined when the id needs none.
- *
- * A local / fine-tuned row is in neither `/api/models/list` nor the external ids, so with
- * no metadata `isGguf` resolves false and the /load request drops the llama.cpp flags. A
- * Hub repo resolves itself, and a synthesized `source: "hub"` would only route an on-disk
- * model into the download manager, so just the variant travels. The remembered config
- * stays off both branches: `stageOrLoad` already falls back to `rememberedConfigFor`.
- */
+/** The picker metadata a "Switch back" has to carry, or undefined when the id needs none. A local
+ * or fine-tuned row is in neither `/api/models/list` nor the external ids, so with no metadata
+ * `isGguf` resolves false and the /load request drops the llama.cpp flags. A Hub repo resolves
+ * itself, and a synthesized `source: "hub"` would only route an on-disk model into the download
+ * manager, so just the variant travels. The remembered config stays off both branches:
+ * `stageOrLoad` already falls back to `rememberedConfigFor`. */
export function chatModelSwitchMeta(
target: ChatModelSwitchTarget,
loraModels: readonly LoraModelOption[],
diff --git a/studio/frontend/src/features/chat/components/chat-model-notice.tsx b/studio/frontend/src/features/chat/components/chat-model-notice.tsx
index ccf8c192f3c..7025ed5507f 100644
--- a/studio/frontend/src/features/chat/components/chat-model-notice.tsx
+++ b/studio/frontend/src/features/chat/components/chat-model-notice.tsx
@@ -20,11 +20,10 @@ import {
export function useChatCreatedModel(
threadId: string | undefined,
): ChatModelSwitchTarget | null {
- // Keyed by the chat it was read for, not a bare value. Clearing it in the
- // effect is a frame late: the effect is passive, so the first render for the
- // incoming chat has already committed with the outgoing chat's model, which
- // paints the wrong notice and moves the viewport padding under it. Answering
- // only for a matching key makes that render impossible rather than brief.
+ // Keyed by the chat it was read for, not a bare value. Clearing it in the effect is a frame late:
+ // the effect is passive, so the first render for the incoming chat has already committed with
+ // the outgoing chat's model, painting the wrong notice. Answering only for a matching key makes
+ // that render impossible rather than brief.
const [read, setRead] = useState<{
threadId: string;
model: ChatModelSwitchTarget | null;
@@ -62,13 +61,9 @@ type ChatModelNoticeProps = {
onSwitch: (target: ChatModelSwitchTarget) => void;
};
-/**
- * Offers to put a chat back on the model it was started on.
- *
- * Deliberately an offer and not an automatic switch: for a local model that
- * would evict whatever is resident and spend a multi-gigabyte load on opening a
- * chat, which is not what clicking a row in the sidebar should cost.
- */
+/** Offers to put a chat back on the model it was started on. Deliberately an offer and not an
+ * automatic switch: for a local model that would evict whatever is resident and spend a
+ * multi-gigabyte load on opening a chat. */
export function ChatModelNotice({
threadId,
checkpoint,
@@ -85,16 +80,15 @@ export function ChatModelNotice({
) {
return null;
}
- // A model that has since been deleted, or a connection that is gone: the
- // switch could not be honoured, and saying so on every open is just noise.
+ // A model that has since been deleted, or a connection that is gone: the switch could not be
+ // honoured, and saying so on every open is just noise.
if (!selectableModelIds.has(createdModel.modelId)) return null;
const label = compareModelDisplayName(createdModel.modelId);
return (
- // Positioned, not in flow. The chat header is `absolute ... z-40` with an
- // opaque `bg-background`, so an in-flow sibling starts at y=0 UNDER it and
- // the whole bar is invisible bar the 10px the header's `right-[10px]`
- // leaves uncovered. Offset by the same header height the drop overlay and
- // the header fade use, above the fade (z-20) and below the header (z-40).
+ // Positioned, not in flow. The chat header is `absolute ... z-40` with an opaque `bg-background`,
+ // so an in-flow sibling starts at y=0 UNDER it and the bar is invisible bar the 10px the
+ // header's `right-[10px]` leaves uncovered. Offset by the same header height the drop overlay
+ // and the header fade use, above the fade (z-20) and below the header (z-40).
<div
data-chat-model-notice=""
className="absolute left-0 right-[10px] top-[calc(var(--studio-content-top-inset,0px)+var(--studio-chat-header-height,48px))] z-30 flex h-[var(--studio-chat-notice-height,2.25rem)] items-center gap-2 border-b border-border/60 bg-muted px-4 text-ui-12 text-muted-foreground"
diff --git a/studio/frontend/src/features/chat/components/chat-search-dialog.tsx b/studio/frontend/src/features/chat/components/chat-search-dialog.tsx
index 755a4482489..64759e9e0b2 100644
--- a/studio/frontend/src/features/chat/components/chat-search-dialog.tsx
+++ b/studio/frontend/src/features/chat/components/chat-search-dialog.tsx
@@ -40,11 +40,10 @@ function haystackMatches(haystack: string, tokens: string[]): boolean {
return tokens.every((token) => haystack.includes(token));
}
-// We filter rows here (cmdk runs with shouldFilter=false) so we control the
-// two-tier behavior and avoid cmdk's fuzzy scorer keeping non-matches visible
-// (issue #5572): every whitespace token must be a substring. User messages are
-// searched first; expand to the full conversation only when user text alone
-// matches nothing anywhere (user messages are short, assistant replies can be huge).
+// We filter rows here (cmdk runs with shouldFilter=false) to control the two-tier behavior and
+// avoid cmdk's fuzzy scorer keeping non-matches visible (#5572): every whitespace token must
+// be a substring. User messages are searched first; expand to the full conversation only when
+// user text alone matches nothing anywhere.
export function selectVisibleChats<
T extends { userSearchText: string; searchText: string },
>(items: T[], search: string): T[] {
@@ -75,8 +74,7 @@ export function ChatSearchDialog() {
const [query, setQuery] = useState("");
// Filtering scans every conversation's text, so keep it off the keystroke path.
const deferredQuery = useDeferredValue(query);
- // An empty query needs no scan, and the deferred value must not hold a previous filter
- // over a reopened dialog.
+ // An empty query needs no scan, and the deferred value must not hold a previous filter over a reopened dialog.
const activeQuery = query === "" ? "" : deferredQuery;
const [rowLimit, setRowLimit] = useState(INITIAL_ROW_COUNT);
// Centered, so the height is decided per open and only ever relaxed compact -> fixed.
@@ -84,9 +82,9 @@ export function ChatSearchDialog() {
isCompactChatSearchList(true, chatSearchIndexHasRows()),
);
- // Reset in the opening render, not an effect: Radix mounts the portal as this render
- // commits, so an effect would trim rows only after the previous set was in the DOM.
- // Resetting on close instead would tear rows down inside the exit animation.
+ // Reset in the opening render, not an effect: Radix mounts the portal as this render commits, so
+ // an effect would trim rows only after the previous set was in the DOM. Resetting on close
+ // instead would tear rows down inside the exit animation.
const [wasOpen, setWasOpen] = useState(isOpen);
if (isOpen !== wasOpen) {
setWasOpen(isOpen);
@@ -98,8 +96,7 @@ export function ChatSearchDialog() {
} else if (
compactList !== isCompactChatSearchList(compactList, items.length > 0)
) {
- // Backstop for an open with no hint at all: the fixed height is taken when the first
- // build lands.
+ // Backstop for an open with no hint at all: the fixed height is taken when the first build lands.
setCompactList(false);
}
@@ -117,8 +114,8 @@ export function ChatSearchDialog() {
return () => clearTimeout(timer);
}, [isOpen]);
- // skipInTextFields keeps the composer's own ⌘K (and any browser find) intact
- // while the user is typing, as the hand-rolled handler did.
+ // skipInTextFields keeps the composer's own Cmd-K and any browser find intact while the user is
+ // typing, as the hand-rolled handler did.
useShortcut("searchChats", () => useChatSearchStore.getState().open(), {
skipInTextFields: true,
});
@@ -139,8 +136,8 @@ export function ChatSearchDialog() {
/>
<CommandPrimitive.Input
placeholder="Search chats..."
- // Controlled: reopening inside the exit animation reuses the mounted tree, so
- // cmdk would keep the previous text while the filter state is clear.
+ // Controlled: reopening inside the exit animation reuses the mounted tree, so cmdk would keep
+ // the previous text while the filter state is clear.
value={query}
onValueChange={setQuery}
className="flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground"
@@ -177,8 +174,8 @@ export function ChatSearchDialog() {
key={item.id}
value={item.id}
onSelect={() => {
- // The list can trail the input by a render, so a row is activatable only
- // if the live query still matches it. Scanned on activation, not per key.
+ // The list can trail the input by a render, so a row is activatable only if the live query still
+ // matches it. Scanned on activation, not per key.
if (
query !== activeQuery &&
!selectVisibleChats(items, query).some(
diff --git a/studio/frontend/src/features/chat/components/deep-research-composer-button.tsx b/studio/frontend/src/features/chat/components/deep-research-composer-button.tsx
index 10c695b2dca..11760dd1846 100644
--- a/studio/frontend/src/features/chat/components/deep-research-composer-button.tsx
+++ b/studio/frontend/src/features/chat/components/deep-research-composer-button.tsx
@@ -243,8 +243,8 @@ function DeepResearchWebsiteAccessContent({
),
),
);
- // The API accepts second-level values the minutes field cannot spell, so saving an
- // untouched control must replay the stored seconds rather than the rounded minutes.
+ // The API accepts second-level values the minutes field cannot spell, so saving an untouched
+ // control must replay the stored seconds rather than the rounded minutes.
const [timeoutEdited, setTimeoutEdited] = useState(false);
return (
@@ -318,8 +318,8 @@ function DeepResearchWebsiteAccessContent({
onClick={() => {
setPolicy(draft);
const minutes = Number(timeoutMinutes);
- // The max attribute does not stop a typed value reaching here, and falling
- // through to the default would hand someone asking for a long run a short one.
+ // The max attribute does not stop a typed value reaching here, and falling through to the default
+ // would hand someone asking for a long run a short one.
setModelTimeoutSeconds(
unlimited
? 0
diff --git a/studio/frontend/src/features/chat/components/delete-chat-files-switch.tsx b/studio/frontend/src/features/chat/components/delete-chat-files-switch.tsx
index 72b04ddf385..48f815b7677 100644
--- a/studio/frontend/src/features/chat/components/delete-chat-files-switch.tsx
+++ b/studio/frontend/src/features/chat/components/delete-chat-files-switch.tsx
@@ -4,12 +4,9 @@
import { Switch } from "@/components/ui/switch";
import { useT } from "@/i18n";
-/**
- * The file half of a chat delete, shown wherever one is confirmed.
- *
- * "Always delete files" makes the delete destructive beyond the chat itself, so
- * every confirmation has to say so and let the user turn it off for this one.
- */
+/** The file half of a chat delete, shown wherever one is confirmed. "Always delete files" makes the
+ * delete destructive beyond the chat itself, so every confirmation has to say so and let the user
+ * turn it off for this one. */
export function DeleteChatFilesSwitch({
id,
checked,
diff --git a/studio/frontend/src/features/chat/components/model-load-status.tsx b/studio/frontend/src/features/chat/components/model-load-status.tsx
index 8407db96cf0..5a4f3cd5e87 100644
--- a/studio/frontend/src/features/chat/components/model-load-status.tsx
+++ b/studio/frontend/src/features/chat/components/model-load-status.tsx
@@ -19,12 +19,9 @@ function clampProgress(value: number): number {
return Math.max(0, Math.min(100, value));
}
-/**
- * Split a composed progress label like "22.8 of 122.3 GB • 330.1 MB/s • 5m 9s
- * left" into a primary chunk (next to the percent) and a secondary chunk (its
- * own row), so neither line wraps raggedly once rate/ETA appears mid-download.
- * Labels without " • " return primary-only, so the secondary row doesn't render.
- */
+/** Split a composed progress label into a primary chunk, next to the percent, and a secondary chunk
+ * on its own row, so neither line wraps raggedly once rate and ETA appear mid-download. Labels
+ * without the separator return primary-only, so the secondary row does not render. */
function splitProgressLabel(
label: string | null | undefined,
): { primary: string; secondary: string } {
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 1ba5916d5b8..e3eb4260b58 100644
--- a/studio/frontend/src/features/chat/components/new-project-dialog.tsx
+++ b/studio/frontend/src/features/chat/components/new-project-dialog.tsx
@@ -30,9 +30,8 @@ function currentRoute(): string {
return window.location.pathname + window.location.search;
}
-// Create-project dialog for the composer, sidebar, and projects page. Creating
-// opens the new project; `onCreated` overrides that for callers with their own
-// follow-up (the sidebar's "move this chat to a new project").
+// Create-project dialog for the composer, sidebar and projects page. Creating opens the new
+// project; `onCreated` overrides that for callers with their own follow-up.
export function NewProjectDialog({
open,
onOpenChange,
@@ -53,15 +52,15 @@ export function NewProjectDialog({
const [name, setName] = useState("");
const [staged, setStaged] = useState<StagedSource[]>([]);
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.
+ // 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);
- // Uploads outlive this component, so a slow one must not yank the user to the
- // new project after they have navigated away.
+ // 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);
useEffect(() => {
- // Set on setup, not just cleared on cleanup: StrictMode replays
- // setup/cleanup/setup, which would otherwise leave this false forever.
+ // Set on setup, not just cleared on cleanup: StrictMode replays setup/cleanup/setup, which would
+ // otherwise leave this false forever.
mounted.current = true;
return () => {
mounted.current = false;
@@ -74,8 +73,8 @@ export function NewProjectDialog({
setStagingDrop(false);
}
- // Every close path routes through here: callers keep this mounted, so a draft
- // left behind would resurface (and upload) on the next project.
+ // Every close path routes through here: callers keep this mounted, so a draft left behind would
+ // resurface, and upload, on the next project.
function close() {
if (busy) return;
reset();
@@ -86,8 +85,8 @@ export function NewProjectDialog({
const trimmed = name.trim();
if (!trimmed || busy || stagingDrop) 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.
+ // Sidebar callers keep this mounted across routes, so unmounting alone cannot tell whether the
+ // user has moved on during a slow upload.
const origin = currentRoute();
try {
const project = await createChatProject(trimmed);
diff --git a/studio/frontend/src/features/chat/components/openai-code-exec-section.tsx b/studio/frontend/src/features/chat/components/openai-code-exec-section.tsx
index e7544a7d68a..1bb422a19e0 100644
--- a/studio/frontend/src/features/chat/components/openai-code-exec-section.tsx
+++ b/studio/frontend/src/features/chat/components/openai-code-exec-section.tsx
@@ -1,23 +1,12 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Settings-sheet section for OpenAI shell-tool container management. Renders
- * only when the active provider is OpenAI cloud (api.openai.com) and the model
- * is gpt-5.5 / gpt-5.5-pro (the only families wired to the shell tool today).
- *
- * Controls:
- * 1. Default container idle-timeout (minutes). Persisted on the provider
- * record; pre-fills the create dialog and feeds the chat-adapter's
- * lazy-create path on a thread's first turn.
- * 2. Container picker for the active thread (an existing OpenAI container or
- * auto-create per thread).
- * 3. Create-new-container inline form, with per-row refresh + delete.
- *
- * Persistence: TTL -> ExternalProviderConfig.openaiContainerTtlMinutes;
- * active container -> ThreadRecord.openaiCodeExecContainerId. No global stores;
- * the list is fetched on open / refresh and held in component state.
- */
+/** Settings-sheet section for OpenAI shell-tool container management. Renders only for OpenAI
+ * cloud with a gpt-5.5 / gpt-5.5-pro model. Controls: default container idle-timeout
+ * (persisted on the provider record), a container picker for the active thread, and an
+ * inline create form with per-row refresh and delete. TTL persists to
+ * ExternalProviderConfig.openaiContainerTtlMinutes and the active container to
+ * ThreadRecord.openaiCodeExecContainerId; the list is held in component state. */
"use client";
@@ -55,22 +44,20 @@ import {
const DEFAULT_TTL_MINUTES = 20;
const TTL_MIN = 1;
const TTL_MAX = 20; // OpenAI hard cap on expires_after.minutes
-// Re-fetch cadence while the section is mounted. OpenAI's TTL flips at minute
-// granularity, so 30s drops an expired container's ACTIVE pill within half a
-// minute without hammering /v1/containers.
+// Re-fetch cadence while mounted. OpenAI's TTL flips at minute granularity, so 30s drops an
+// expired container's ACTIVE pill without hammering /v1/containers.
const REFRESH_POLL_MS = 30_000;
function shortContainerId(id: string): string {
- // Mid-truncate keeps the "cntr_" prefix readable and still surfaces the
- // tail digits users sometimes copy off OpenAI's dashboard.
+ // Mid-truncate keeps the "cntr_" prefix readable and still shows the tail digits users copy
+ // off OpenAI's dashboard.
if (id.length <= 18) return id;
return `${id.slice(0, 12)}…${id.slice(-4)}`;
}
function isContainerRunning(c: OpenAIContainerSummary): boolean {
- // OpenAI reports `status: "running"` while idle TTL is valid and "expired"
- // afterward. Treat a missing status as running so older payloads without the
- // field don't false-positive.
+ // OpenAI reports `status: "running"` while idle TTL is valid and "expired" after. A missing
+ // status counts as running so older payloads do not false-positive.
return c.status == null || c.status === "running";
}
@@ -94,27 +81,24 @@ export function OpenAICodeExecSection({
const [creating, setCreating] = useState(false);
const [createOpen, setCreateOpen] = useState(false);
const [createName, setCreateName] = useState("");
- // Ids deleted this session. A tombstoned id stays hidden for the page's
- // lifetime: OpenAI's /containers list can keep returning a freshly-deleted
- // id for an undocumented, variable time, and auto-reshowing it confuses more
- // than it helps. A page refresh resets the tombstone.
+ // Ids deleted this session. A tombstoned id stays hidden for the page's lifetime: OpenAI's
+ // list can keep returning a freshly-deleted id for a variable time. A refresh resets it.
const [tombstones, setTombstones] = useState<Set<string>>(() => new Set());
- // Ids optimistically inserted after a create but not yet confirmed by a
- // /v1/containers list. That endpoint is eventually consistent (a new
- // container can be absent for several seconds), so we render the row at once
- // with a "Creating" pill and drop it once a refresh sees the id.
+ // Ids optimistically inserted after a create but not yet confirmed by a list. That endpoint
+ // is eventually consistent, so the row renders at once with a "Creating" pill and is
+ // dropped once a refresh sees the id.
const [pendingIds, setPendingIds] = useState<Set<string>>(() => new Set());
- // Ref mirror so `refresh()` reads the current pending set without re-binding
- // when it changes (the callback is a useEffect dep).
+ // Ref mirror so `refresh()` reads the current pending set without re-binding when it changes
+ // (the callback is a useEffect dep).
const pendingIdsRef = useRef<Set<string>>(pendingIds);
useEffect(() => {
pendingIdsRef.current = pendingIds;
}, [pendingIds]);
- // One-shot follow-up refresh after a create, for when the server list lags
- // the create response by a few seconds. Tracked so we clear it on unmount.
+ // One-shot follow-up refresh after a create, for when the server list lags the create
+ // response. Tracked so we clear it on unmount.
const pendingRetryRef = useRef<number | null>(null);
- // Target row for the delete-confirmation dialog. Held in state (not
- // window.confirm) so the dialog sits inside the settings sheet.
+ // Target row for the delete-confirmation dialog. Held in state, not window.confirm, so the
+ // dialog sits inside the settings sheet.
const [pendingDelete, setPendingDelete] =
useState<OpenAIContainerSummary | null>(null);
const [deleting, setDeleting] = useState(false);
@@ -148,16 +132,14 @@ export function OpenAICodeExecSection({
};
}, [activeThreadId]);
- // Hide just-deleted containers even if OpenAI's list still returns them.
- // Single chokepoint: every downstream view (sorted picker, auto-bind
- // candidate, all-containers list) derives from visibleContainers.
+ // Hide just-deleted containers even if OpenAI's list still returns them. Single chokepoint:
+ // every downstream view derives from visibleContainers.
const visibleContainers = useMemo(() => {
if (tombstones.size === 0) return containers;
return containers.filter((c) => !tombstones.has(c.id));
}, [containers, tombstones]);
- // Newest-first by lastActiveAt so the dropdown default (auto-bind target)
- // shows up first.
+ // Newest-first by lastActiveAt so the dropdown default (auto-bind target) shows up first.
const sortedContainers = useMemo(
() =>
[...visibleContainers].sort(
@@ -166,18 +148,15 @@ export function OpenAICodeExecSection({
[visibleContainers],
);
- // First running container by lastActiveAt: the auto-bind target and what we
- // surface visually before Dexie catches up.
+ // First running container by lastActiveAt: the auto-bind target and what we surface visually before Dexie catches up.
const firstRunningContainer = useMemo(
() => sortedContainers.find(isContainerRunning) ?? null,
[sortedContainers],
);
- // What the picker treats as "active" now. Decoupled from `activeContainerId`
- // (Dexie state) so the user sees the most-recent running container while the
- // auto-bind effect's async write propagates. If the Dexie-bound container
- // expired, fall back to the first running candidate; the stale-bind sweeper
- // clears Dexie shortly after.
+ // What the picker treats as "active" now. Decoupled from `activeContainerId` (Dexie state) so
+ // the user sees the most-recent running container while the auto-bind write propagates. If
+ // the bound container expired, fall back to the first running candidate.
const boundContainer = useMemo(
() => sortedContainers.find((c) => c.id === activeContainerId) ?? null,
[sortedContainers, activeContainerId],
@@ -199,8 +178,8 @@ export function OpenAICodeExecSection({
});
const serverIds = new Set(list.map((c) => c.id));
setContainers((prev) => {
- // Preserve optimistic inserts the server hasn't acknowledged yet so
- // they don't disappear on the reconciling refresh.
+ // Preserve optimistic inserts the server has not acknowledged yet so they do not disappear on
+ // the reconciling refresh.
const orphans = prev.filter(
(c) => !serverIds.has(c.id) && pendingIdsRef.current.has(c.id),
);
@@ -224,9 +203,8 @@ export function OpenAICodeExecSection({
}
}, [apiKey, hasCredential, provider.baseUrl, provider.id]);
- // Fetch on mount (or provider change), then poll on a low cadence so an
- // expired container's ACTIVE pill clears without a manual refresh. Also
- // re-fetch when the tab regains visibility (sheet left open while idle).
+ // Fetch on mount or provider change, then poll on a low cadence so an expired container's
+ // ACTIVE pill clears without a manual refresh. Also re-fetch on tab visibility.
useEffect(() => {
void refresh();
const interval = window.setInterval(() => {
@@ -250,17 +228,11 @@ export function OpenAICodeExecSection({
};
}, [refresh]);
- // Auto-bind the active thread to the most-recently-active container when the
- // thread has none and at least one container exists on the account. Sorting
- // by `lastActiveAt` matches what feels "most recent" to the user.
- //
- // `ensureThreadRecord` eagerly materializes the thread row so the bind lands
- // before the first message. This creates nothing at OpenAI (only a local
- // ThreadRecord), so a fresh OpenAI container is still not created until first
- // send.
- //
- // If no containers exist yet, this short-circuits: the picker shows an
- // empty-state hint and the chat-adapter mints the first container on send.
+ // Auto-bind the active thread to the most-recently-active container when it has none. Sorting
+ // by `lastActiveAt` matches what feels "most recent". `ensureThreadRecord` materializes the
+ // thread row so the bind lands before the first message. This creates nothing at OpenAI, so
+ // a fresh container is still not created until first send; with none, the picker shows an
+ // empty state and the adapter mints the first on send.
useEffect(() => {
if (
!activeThreadId ||
@@ -300,12 +272,9 @@ export function OpenAICodeExecSection({
const onPick = async (value: string) => {
if (!activeThreadId || !value) return;
- // value is always a container id now; "Auto-create per thread" was removed
- // in favour of defaulting to the most-recently-active container. The
- // chat-adapter still handles the no-containers case (lazy-create on send).
- //
- // ensureThreadRecord eagerly materializes the thread row (modelType "base":
- // settings sheet is single-thread only) so the update lands before send.
+ // value is always a container id now; "Auto-create per thread" was removed in favour of
+ // defaulting to the most-recently-active container. The adapter still lazy-creates when
+ // none exist. ensureThreadRecord materializes the row so the update lands before send.
try {
await ensureThreadRecord({ threadId: activeThreadId, modelType: "base" });
const updated = await updateStoredChatThread(activeThreadId, {
@@ -328,9 +297,8 @@ export function OpenAICodeExecSection({
toast.error("Container name is required");
return;
}
- // TTL inherits from the section-level "Idle timeout" control (no
- // per-container override). Read at submit time so a last-second TTL change
- // applies.
+ // TTL inherits from the section-level "Idle timeout" control. Read at submit time so a
+ // last-second change applies.
const ttlMinutes =
provider.openaiContainerTtlMinutes ?? DEFAULT_TTL_MINUTES;
setCreating(true);
@@ -342,9 +310,8 @@ export function OpenAICodeExecSection({
toast.success(`Created container ${name}`);
setCreateName("");
setCreateOpen(false);
- // Optimistic insert + "Creating" pill. The /v1/containers list is
- // eventually consistent and can omit the new container for seconds;
- // without this the row only appears on the next poll or manual refresh.
+ // Optimistic insert plus "Creating" pill. The list is eventually consistent and can omit the
+ // new container for seconds; without this the row only appears on the next poll.
setContainers((prev) =>
prev.some((c) => c.id === created.id) ? prev : [created, ...prev],
);
@@ -354,8 +321,8 @@ export function OpenAICodeExecSection({
next.add(created.id);
return next;
});
- // Follow-up refresh ~5s later to reconcile the optimistic row once
- // /v1/containers catches up. One shot; the poll covers a longer tail.
+ // Follow-up refresh ~5s later to reconcile the optimistic row. One shot; the poll covers a
+ // longer tail.
if (pendingRetryRef.current != null) {
window.clearTimeout(pendingRetryRef.current);
}
@@ -363,8 +330,8 @@ export function OpenAICodeExecSection({
pendingRetryRef.current = null;
void refresh();
}, 5000);
- // Auto-bind the new container to the active thread. ensureThreadRecord
- // first so the bind lands even if no message has been sent yet.
+ // Auto-bind the new container to the active thread. ensureThreadRecord first so the bind
+ // lands even if no message has been sent.
if (activeThreadId) {
try {
await ensureThreadRecord({
@@ -384,8 +351,8 @@ export function OpenAICodeExecSection({
);
} finally {
setCreating(false);
- // Refresh even on failure: the request may have partially succeeded
- // (container created, response lost); a re-fetch keeps the picker in sync.
+ // Refresh even on failure: the request may have partially succeeded (container created,
+ // response lost); a re-fetch keeps the picker in sync.
await refresh();
}
};
@@ -399,8 +366,7 @@ export function OpenAICodeExecSection({
{ providerId: provider.id, apiKey, baseUrl: provider.baseUrl || null },
id,
);
- // Tombstone the id so the picker hides it at once even if OpenAI's list
- // keeps returning it for a while.
+ // Tombstone the id so the picker hides it at once even if OpenAI's list keeps returning it for a while.
setTombstones((prev) => {
if (prev.has(id)) return prev;
const next = new Set(prev);
@@ -424,8 +390,8 @@ export function OpenAICodeExecSection({
} finally {
setDeleting(false);
setPendingDelete(null);
- // Always refresh so a stale list entry (deleted elsewhere or expired) is
- // purged even when the delete call errored.
+ // Always refresh so a stale list entry, deleted elsewhere or expired, is purged even when the
+ // delete call errored.
await refresh();
}
};
@@ -480,9 +446,8 @@ export function OpenAICodeExecSection({
</Button>
</div>
{sortedContainers.length === 0 ? (
- // Quiet placeholder with the same muted border as row cards so an
- // empty section doesn't look like an active control. The first
- // container is lazy-created on first send and appears after refresh.
+ // Quiet placeholder with the same muted border as row cards so an empty section does not look
+ // like an active control. The first container is lazy-created on first send.
<div className="flex h-9 w-full items-center rounded-md border border-dashed border-border/60 bg-muted/20 px-2 text-xs text-muted-foreground">
None yet - one will be created on first send.
</div>
diff --git a/studio/frontend/src/features/chat/components/research-activity-panel.tsx b/studio/frontend/src/features/chat/components/research-activity-panel.tsx
index 82e21edda6c..dfeabb22697 100644
--- a/studio/frontend/src/features/chat/components/research-activity-panel.tsx
+++ b/studio/frontend/src/features/chat/components/research-activity-panel.tsx
@@ -80,7 +80,7 @@ const terminalStatuses = new Set<ResearchRunStatus>([
const ACTIVITY_FOLLOW_SETTLE_MS = 450;
const ACTIVITY_BOTTOM_THRESHOLD_PX = 24;
// 2px, not 1, matching use-intent-aware-autoscroll: HiDPI subpixel rounding leaves a fractional
-// gap that a 1px threshold reads as unpinned, which would keep the follow loop running forever.
+// gap that a 1px threshold reads as unpinned, keeping the follow loop running forever.
const ACTIVITY_PINNED_THRESHOLD_PX = 2;
function useResearchActivityScroll(runId: string) {
@@ -121,8 +121,8 @@ function useResearchActivityScroll(runId: string) {
if (remaining <= 0) return;
settleTimer = window.setTimeout(() => {
settleTimer = null;
- // The timer lands on the deadline and its tick a frame later, so the window has closed by
- // then; this grants that tick one last follow pass rather than dropping it to the reconcile.
+ // The timer lands on the deadline and its tick a frame later, so the window has closed by then;
+ // this grants that tick one last follow pass rather than dropping it to the reconcile.
settleCheckDue = true;
requestTick();
}, remaining);
@@ -135,8 +135,8 @@ function useResearchActivityScroll(runId: string) {
const pinned = distanceFromBottom() <= ACTIVITY_PINNED_THRESHOLD_PX;
if (!pinned) element.scrollTop = element.scrollHeight;
updateAtBottom(true);
- // Chaining on the window alone forced a layout every frame for the whole run; growth that
- // leaves the view unpinned is the signal, and a quiet frame defers to the settle check.
+ // Chaining on the window alone forced a layout every frame for the whole run; growth that leaves
+ // the view unpinned is the signal, and a quiet frame defers to the settle check.
if (layoutChanged || !pinned) {
layoutChanged = false;
requestTick();
@@ -156,10 +156,10 @@ function useResearchActivityScroll(runId: string) {
const detach = () => {
detached = true;
followUntil = 0;
- // Cancel every pending follow step, not just the settle check. A frame queued before the
- // detach still runs, falls through to the reconcile below, and reads a flick shorter than
- // the bottom threshold as still-at-bottom: "Latest" never appears and nothing corrects it,
- // since followLayout returns early from here on.
+ // Cancel every pending follow step, not just the settle check. A frame queued before the detach
+ // still runs, falls through to the reconcile below, and reads a flick shorter than the bottom
+ // threshold as still-at-bottom: "Latest" never appears and nothing corrects it.
+ // followLayout returns early from here on, so nothing corrects it.
if (animationFrame !== null) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
@@ -889,12 +889,10 @@ export function ResearchActivityPanel({
style={
variant === "panel"
? {
- // The chat-model notice is an opaque absolute bar spanning the whole
- // chat content area, the panel column included, directly under the
- // header. Clear it the same way the header itself is cleared, or its
- // first 2.25rem -- the telescope, the title, the status pill and the
- // close button -- is painted over and unclickable. 0px whenever no
- // notice is on screen, so this is the geometry it always had.
+ // The chat-model notice is an opaque absolute bar spanning the whole chat content area, the
+ // panel column included, directly under the header. Clear it the same way the header itself is
+ // cleared, or its first 2.25rem is painted over and unclickable. 0px whenever no notice is on
+ // screen, so this is the geometry it always had.
height:
"calc(100% - var(--studio-content-top-inset, 0px) - var(--studio-chat-header-height, 48px) - var(--studio-chat-notice-height, 0px))",
marginTop:
diff --git a/studio/frontend/src/features/chat/components/stop-running-chats-dialog.tsx b/studio/frontend/src/features/chat/components/stop-running-chats-dialog.tsx
index dd9d6c13a1f..47083179e66 100644
--- a/studio/frontend/src/features/chat/components/stop-running-chats-dialog.tsx
+++ b/studio/frontend/src/features/chat/components/stop-running-chats-dialog.tsx
@@ -13,11 +13,9 @@ import {
} from "@/components/ui/alert-dialog";
import { useStopRunningChatsDialogStore } from "../stores/stop-running-chats-dialog-store";
-/**
- * Confirmation for applying a model or reload-required setting while chats are generating.
- * They share one llama-server, so the swap ends all of them: name them and make the user
- * opt in rather than truncating silently.
- */
+/** Confirmation for applying a model or reload-required setting while chats are generating. They
+ * share one llama-server, so the swap ends all of them: name them and make the user opt in
+ * rather than truncating silently. */
export function StopRunningChatsDialog() {
const open = useStopRunningChatsDialogStore((s) => s.open);
const count = useStopRunningChatsDialogStore((s) => s.count);
@@ -27,8 +25,8 @@ export function StopRunningChatsDialog() {
const effect = useStopRunningChatsDialogStore((s) => s.effect);
const resolve = useStopRunningChatsDialogStore((s) => s.resolve);
- // Embeddings, raw completions and audio share the model but are not conversations,
- // so name them generically rather than offering to stop chats that do not exist.
+ // Embeddings, raw completions and audio share the model but are not conversations, so name them
+ // generically rather than offering to stop chats that do not exist.
const noun = hasNonChat
? count === 1
? "request"
@@ -37,8 +35,8 @@ export function StopRunningChatsDialog() {
? "chat"
: "chats";
const sharer = hasNonChat ? "request" : "conversation";
- // Ejecting leaves no model loaded. Saying it "reloads the model" and offering "Stop and
- // reload" promised the opposite of what confirming does, for the destructive one.
+ // Ejecting leaves no model loaded. Saying it "reloads the model" and offering "Stop and reload"
+ // promised the opposite of what confirming does, for the destructive one.
const unloads = effect === "unload";
const lead = unloads
? `${action || "Unloading the model"} leaves no model loaded, and every open ${sharer} shares it, `
diff --git a/studio/frontend/src/features/chat/components/youtube-transcript-prompt.tsx b/studio/frontend/src/features/chat/components/youtube-transcript-prompt.tsx
index eb1cbf3fb97..c35bacaabbd 100644
--- a/studio/frontend/src/features/chat/components/youtube-transcript-prompt.tsx
+++ b/studio/frontend/src/features/chat/components/youtube-transcript-prompt.tsx
@@ -68,10 +68,8 @@ async function detachFile(composer: Composer, file: File): Promise<void> {
if (added) await composer.attachment({ id: added.id }).remove();
}
-/**
- * Offer to turn a pasted YouTube link into a transcript attachment. Sits above the
- * composer surface, matching the prompt-queue stack, and closes once the user picks.
- */
+/** Offer to turn a pasted YouTube link into a transcript attachment. Sits above the composer
+ * surface, matching the prompt-queue stack, and closes once the user picks. */
export function YoutubeTranscriptPrompt({
url,
onClose,
@@ -83,8 +81,8 @@ export function YoutubeTranscriptPrompt({
const [fetching, setFetching] = useState(false);
const abortRef = useRef<AbortController | null>(null);
- // A link removed from the draft unmounts this mid-fetch; drop the in-flight
- // request so the attachment does not land in a composer that moved on.
+ // A link removed from the draft unmounts this mid-fetch; drop the in-flight request so the
+ // attachment does not land in a composer that moved on.
useEffect(() => () => abortRef.current?.abort(), []);
const attach = useCallback(async () => {
@@ -106,8 +104,8 @@ export function YoutubeTranscriptPrompt({
);
if (controller.signal.aborted) return;
await composer.addAttachment(file);
- // A send lands while addAttachment is still pending, and abort cannot recall it,
- // so drop the file rather than let it ride along on the next message.
+ // A send lands while addAttachment is still pending, and abort cannot recall it, so drop the file
+ // rather than let it ride along on the next message.
if (controller.signal.aborted) {
await detachFile(composer, file);
return;
diff --git a/studio/frontend/src/features/chat/db.ts b/studio/frontend/src/features/chat/db.ts
index 928d1722e00..b0dde46429d 100644
--- a/studio/frontend/src/features/chat/db.ts
+++ b/studio/frontend/src/features/chat/db.ts
@@ -5,8 +5,8 @@ import Dexie, { type EntityTable, liveQuery } from "dexie";
import { useEffect, useRef, useState } from "react";
import type { MessageRecord, ThreadRecord } from "./types";
-// Legacy browser-only chat storage. Replaced by studio.db (see
-// chat-history-storage.ts), kept read-only for the one-shot import path.
+// Legacy browser-only chat storage. Replaced by studio.db (see chat-history-storage.ts), kept
+// read-only for the one-shot import path.
export const DEXIE_DB_NAME = "unsloth-chat";
const db = new Dexie(DEXIE_DB_NAME) as Dexie & {
@@ -42,12 +42,9 @@ db.version(3)
export { db };
-/**
- * Wraps Dexie liveQuery for React state updates.
- *
- * Include every semantic query input in `deps` (filters, sort keys, IDs). `querier`
- * identity is ignored to avoid re-subscribing every render on inline functions.
- */
+/** Wraps Dexie liveQuery for React state updates. Include every semantic query input in `deps`
+ * (filters, sort keys, IDs). `querier` identity is ignored to avoid re-subscribing every render
+ * on inline functions. */
export function useLiveQuery<T>(
querier: () => Promise<T>,
deps: unknown[] = [],
diff --git a/studio/frontend/src/features/chat/external-providers.ts b/studio/frontend/src/features/chat/external-providers.ts
index 32b1a4039c5..d56915bd3dd 100644
--- a/studio/frontend/src/features/chat/external-providers.ts
+++ b/studio/frontend/src/features/chat/external-providers.ts
@@ -18,11 +18,9 @@ export interface ExternalProviderConfig {
models: string[];
/** Cached available model ids from the provider's /models response. */
availableModels?: string[];
- /**
- * The provider type as the BACKEND stores it, which is not always `providerType`
- * above: `resolveUiProviderTypeFromConfig` shows a row saved as `openai` with a
- * custom name or base URL as "custom". Absent means unknown, not custom.
- */
+ /** The provider type as the BACKEND stores it, which is not always `providerType` above:
+ * `resolveUiProviderTypeFromConfig` shows a row saved as `openai` with a custom name or base
+ * URL as "custom". Absent means unknown, not custom. */
backendProviderType?: string;
/** Optional Max Tokens cap for this connection, replacing the undocumented-model fallback. */
maxOutputTokens?: number;
@@ -35,30 +33,24 @@ export interface ExternalProviderConfig {
authStatus?: ProviderAuthStatus;
/** Whether to ask supported hosted providers to use prompt caching. */
enablePromptCaching?: boolean;
- /**
- * Anthropic prompt-cache TTL bucket. Only meaningful when `enablePromptCaching`
- * is true and the provider supports the choice (Anthropic today). Maps to
- * backend `prompt_cache_ttl`, which sets `cache_control.ttl` on the cache
- * marker. Omitted = inherit Anthropic's default 5-minute pool.
- */
+ /** Anthropic prompt-cache TTL bucket. Only meaningful when `enablePromptCaching` is true and
+ * the provider supports the choice. Maps to backend `prompt_cache_ttl`, which sets
+ * `cache_control.ttl`. Omitted = inherit Anthropic's default 5-minute pool. */
promptCacheTtl?: "5m" | "1h";
/** User-pinned: the loaded vLLM model supports `enable_thinking`. */
isReasoningModel?: boolean;
- /**
- * Default idle-timeout (minutes) for new OpenAI shell containers. Pre-fills the
- * "Create container" dialog and is the TTL the auto-create-per-thread path POSTs
- * to /v1/containers. OpenAI's hard default is 20. Only for OpenAI cloud.
- */
+ /** Default idle-timeout (minutes) for new OpenAI shell containers. Pre-fills the create dialog
+ * and is the TTL the auto-create-per-thread path POSTs. OpenAI's hard default is 20. */
openaiContainerTtlMinutes?: number;
createdAt: number;
updatedAt: number;
}
// Gemini supports prompt caching, but the wire flow needs a separate POST to
-// /v1beta/cachedContents before generateContent can reference the cache; the
-// enable_prompt_caching boolean alone isn't enough. Until that two-step flow
-// ships, keep the picker off so the toggle doesn't silently no-op for Gemini.
-// See https://ai.google.dev/gemini-api/docs/caching.
+// /v1beta/cachedContents before generateContent can reference the cache. Until that two-step
+// flow ships, keep the picker off so the toggle does not silently no-op for Gemini. See
+// https://ai.google.dev/gemini-api/docs/caching.
+// The enable_prompt_caching boolean alone is not enough.
const PROMPT_CACHING_PROVIDER_TYPES = new Set(["openai", "anthropic"]);
export function supportsProviderPromptCaching(
@@ -67,11 +59,9 @@ export function supportsProviderPromptCaching(
return providerType != null && PROMPT_CACHING_PROVIDER_TYPES.has(providerType);
}
-/**
- * Whether the provider lets the user choose between a short and long prompt-cache
- * pool. Anthropic exposes 5m and 1h ephemeral pools via `cache_control.ttl`;
- * OpenAI's automatic cache has no equivalent knob, so it stays off the picker.
- */
+/** Whether the provider lets the user choose between a short and long prompt-cache pool.
+ * Anthropic exposes 5m and 1h ephemeral pools via `cache_control.ttl`; OpenAI's automatic
+ * cache has no equivalent knob. */
const PROMPT_CACHE_TTL_PROVIDER_TYPES = new Set(["anthropic"]);
export function supportsProviderPromptCacheTtl(
@@ -88,8 +78,8 @@ export function isPromptCacheTtl(value: unknown): value is "5m" | "1h" {
return typeof value === "string" && PROMPT_CACHE_TTL_VALUES.has(value as "5m" | "1h");
}
-// Provider types exposing the connection-level "reasoning model" toggle.
-// vLLM's OpenAI-compat endpoint doesn't advertise this per model.
+// Provider types exposing the connection-level "reasoning model" toggle. vLLM's OpenAI-compat
+// endpoint does not advertise this per model.
const REASONING_TOGGLE_PROVIDER_TYPES = new Set(["vllm"]);
export function supportsProviderReasoningToggle(
@@ -184,20 +174,12 @@ export function setProviderModelCapabilities(
persistProviderModelCapabilities();
}
-/** Drop persisted capabilities for provider types the registry no longer lists.
- *
- * This map outlives the backend that wrote it: it is localStorage, so a browser
- * carries it across a downgrade, and a per-entry write can only ever correct the
- * entries the registry still returns. A provider that has been hidden or that a
- * rolled-back backend does not know about is simply absent from the response, so
- * without this its last-known `studio_tools: true` latches forever and the
- * composer keeps offering a loop that backend cannot run.
- *
- * Convergence is the point: the registry response is the whole truth about which
- * provider types exist, so an empty one legitimately means "none", and clearing
- * is the safe direction anyway (an unknown capability reads as null, which every
- * caller treats as "not capable").
- */
+/** Drop persisted capabilities for provider types the registry no longer lists. This
+ * localStorage map outlives the backend that wrote it, and a per-entry write can only
+ * correct entries the registry still returns. A hidden or rolled-back provider is simply
+ * absent from the response, so without this its last-known `studio_tools: true` latches
+ * forever. The registry response is the whole truth, so an empty one legitimately means
+ * "none", and clearing is the safe direction: an unknown capability reads as null. */
export function pruneProviderModelCapabilities(knownProviderTypes: Iterable<string>): void {
hydrateProviderModelCapabilities();
const known = new Set(knownProviderTypes);
@@ -226,9 +208,8 @@ export function providerModelSupportsVision(
}
-/** Provider-level capability key. Self-hosted model ids are user-supplied, so
- * there is no per-model entry to look up: the registry declares the capability
- * once for the whole provider type and it applies to every model on it. */
+/** Provider-level capability key. Self-hosted model ids are user-supplied, so there is no
+ * per-model entry: the registry declares the capability once for the whole provider type. */
export const PROVIDER_CAPABILITY_WILDCARD = "*";
export function providerModelSupportsStudioTools(
@@ -246,12 +227,9 @@ export function providerModelSupportsStudioTools(
return typeof providerDefault === "boolean" ? providerDefault : null;
}
-/** Whether the connection behind an ``external::`` model id runs Unsloth tools.
- *
- * Resolves the provider type from the saved connection, so callers that only
- * have a checkpoint id (the runtime store) can ask the capability question
- * without reaching for the providers store and risking an import cycle.
- */
+/** Whether the connection behind an `external::` model id runs Unsloth tools. Resolves the
+ * provider type from the saved connection, so callers holding only a checkpoint id can ask
+ * the capability question without risking an import cycle. */
export function externalModelSupportsStudioTools(
checkpoint: string | null | undefined,
): boolean {
@@ -285,14 +263,10 @@ export function normalizeProviderMaxOutputTokens(
return value;
}
-/**
- * Whether a connection may carry a per-connection Max Tokens limit. Every type may,
- * except ChatGPT subscriptions, whose routing, model list and output cap are all fixed,
- * so an override there would be stored and never read.
- *
- * Both types are checked: the stored one is what the server validates against, the UI
- * one is all the dialog has for a connection with no server row yet.
- */
+/** Whether a connection may carry a per-connection Max Tokens limit. Every type may, except
+ * ChatGPT subscriptions, whose routing, model list and output cap are fixed. Both types are
+ * checked: the stored one is what the server validates against, the UI one is all the dialog
+ * has for a connection with no server row yet. */
export function supportsProviderMaxOutputTokens(
uiProviderType: string | null | undefined,
backendProviderType: string | null | undefined,
@@ -379,8 +353,8 @@ export function supportsRemoteModelCatalog(
);
}
-/** Presets that hide the API-key field. Ollama is not skipped: Ollama cloud
- * requires a key; local servers leave the optional field empty. */
+/** Presets that hide the API-key field. Ollama is not skipped: Ollama cloud requires a key;
+ * local servers leave the optional field empty. */
export function customPresetSkipsApiKeyField(
providerType: string | null | undefined,
): boolean {
@@ -439,13 +413,13 @@ export function toExternalBackendProviderType(
): string | undefined {
if (!providerType) return undefined;
// vLLM's /v1/responses applies the loaded model's chat template, which 400s on
- // strict-alternation templates (e.g. Gemma 3). Pass the type through so the
- // backend routes vLLM to /v1/chat/completions instead of the Responses path.
+ // strict-alternation templates. Pass the type through so the backend routes vLLM to
+ // /v1/chat/completions instead.
if (providerType === "vllm") return "vllm";
if (providerType === "ollama") return "ollama";
if (providerType === "llama_cpp") return "llama_cpp";
- // Generic custom servers are OpenAI-compatible, but should still use the
- // chat-completions backend path instead of OpenAI's Responses API route.
+ // Generic custom servers are OpenAI-compatible, but should still use the chat-completions
+ // backend path instead of OpenAI's Responses API route.
if (providerType === LEGACY_CUSTOM_PROVIDER_TYPE) {
return LEGACY_CUSTOM_PROVIDER_TYPE;
}
@@ -660,8 +634,7 @@ export function saveExternalProviders(
if (!canUseStorage()) return;
try {
localStorage.setItem(EXTERNAL_PROVIDERS_KEY, JSON.stringify(providers));
- // Legacy keys are migration input. Preserve unmatched entries until the
- // backend confirms the exact key was stored.
+ // Legacy keys are migration input. Preserve unmatched entries until the backend confirms the exact key was stored.
} catch {
// ignore
}
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
index 4f0258394ef..184ff32df28 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
@@ -148,8 +148,8 @@ export type SelectedModelInput = {
loadId?: string | null;
isLora?: boolean;
ggufVariant?: string;
- /** Where the pick came from (e.g. "hub", "local", "external"). Used to decide
- * whether an uncached repo should download via the Hub manager. */
+ /** Where the pick came from ("hub", "local", "external"). Decides whether an uncached repo
+ * should download via the Hub manager. */
source?: string;
/** Uncached non-GGUF HF repo staged for a snapshot download (variant null). */
isHubRepo?: boolean;
@@ -159,21 +159,21 @@ export type SelectedModelInput = {
forceReload?: boolean;
nativePathToken?: string;
nativePathExpiresAtMs?: number | null;
- /** Direct local .gguf file (no HF variant / native token) — still a GGUF
- * source, so the staging flow treats it as one. */
+ /** Direct local .gguf file (no HF variant / native token), still a GGUF source, so the staging
+ * flow treats it as one. */
isGguf?: boolean;
/** Staged metadata confirmed the separate DiffusionGemma runner. */
isDiffusion?: boolean;
throwOnError?: boolean;
- /** Keep the current speculative-decoding choice across the model switch
- * instead of resetting it to the standing preference. */
+ /** Keep the current speculative-decoding choice across the model switch instead of resetting it
+ * to the standing preference. */
keepSpeculative?: boolean;
config?: PerModelConfig;
previousConfig?: PerModelConfig;
};
-// Approved fingerprints by checkpoint, so a rollback after a failed switch can resend
-// the pinned approval the worker requires instead of being blocked.
+// Approved fingerprints by checkpoint, so a rollback after a failed switch can resend the pinned
+// approval the worker requires.
/** An absent route is not a failed read: only the second one withholds an answer. */
async function readReloadHint(
read: () => Promise<{ reloadRequired: boolean } | null>,
@@ -188,12 +188,10 @@ async function readReloadHint(
/** The two settings reads `serverWideReloadRequired` judges, fetched together. */
async function readServerWideReloadHints(): Promise<boolean> {
const [modelMemory, vramBudget] = await Promise.all([
- // Forced, like the budget below: a read that started before a save answers about
- // the policy it replaced, and a false from that would suppress the very reload the
- // save was made for.
+ // Forced, like the budget below: a read that started before a save answers about the policy it
+ // replaced, and a false from that would suppress the reload the save was made for.
readReloadHint(() => loadModelMemorySettings({ force: true })),
- // rethrow, or a failed read is indistinguishable from an absent route: this reader
- // answers null for both, which every other caller is right to treat alike.
+ // Rethrow, or a failed read is indistinguishable from an absent route: this reader answers null for both.
readReloadHint(() =>
loadVramBudgetSettings({ force: true, rethrow: true }),
),
@@ -353,11 +351,9 @@ function toChatModelRow(model: {
};
}
-// Merge capability flags from a load/status response into the matching
-// models[] entry. /api/models/list omits audio capability for default and
-// active-GGUF entries, so the attach gates (`activeModel?.hasAudioInput`)
-// would otherwise stay false. Mirrors the compare composer's sync.
-// Exported for tests.
+// Merge capability flags from a load/status response into the matching models[] entry:
+// /api/models/list omits audio capability for default and active-GGUF entries, so the attach
+// gates would stay false. Mirrors the compare composer's sync.
export function syncModelCapabilities(
modelId: string,
resp: {
@@ -377,14 +373,14 @@ export function syncModelCapabilities(
const synced = {
isVision: Boolean(resp.is_vision),
isGguf: Boolean(resp.is_gguf),
- // A locally scanned model is in no /api/models/list row, so this is the only place
- // its summary learns it is served by MLX, and the seed gate reads that.
+ // A locally scanned model is in no /api/models/list row, so this is the only place its summary
+ // learns it is served by MLX.
isMlx: Boolean(resp.is_mlx),
isAudio: Boolean(resp.is_audio),
audioType: resp.audio_type ?? null,
hasAudioInput: Boolean(resp.has_audio_input),
- // /api/models/list omits this for the active GGUF row, so without it the
- // video adapter reads false after every load and status hydration.
+ // /api/models/list omits this for the active GGUF row, so without it the video adapter reads
+ // false after every load and status hydration.
hasVideoInput: Boolean(resp.has_video_input),
};
const idx = models.findIndex((m) => m.id === modelId);
@@ -393,8 +389,8 @@ export function syncModelCapabilities(
...models,
{
id: modelId,
- // Label like the catalog entry that replaces this on the next /api/models/list;
- // the fallback keeps a cached GGUF's snapshot path out of the bar.
+ // Label like the catalog entry that replaces this on the next /api/models/list; the fallback
+ // keeps a cached GGUF's snapshot path out of the bar.
name: modelDisplayName(resp.display_name || modelId),
isLora: Boolean(resp.is_lora),
...synced,
@@ -536,8 +532,8 @@ async function syncInferenceStatusToStore(options?: {
if (checkpointId) {
const previousGgufVariant =
useChatRuntimeStore.getState().activeGgufVariant;
- // A model loaded outside this tab replaces the resident one, and the pin belonged to the
- // old one: keeping it reloads that one from another's settings.
+ // A model loaded outside this tab replaces the resident one, and the pin belonged to the old
+ // one: keeping it reloads that one from another's settings.
if (
checkpointId !== selectedCheckpoint ||
(statusRes.gguf_variant ?? null) !== (previousGgufVariant ?? null)
@@ -553,8 +549,8 @@ async function syncInferenceStatusToStore(options?: {
// checkpoint and is safe to migrate.
adoptingExistingServerModel: selectedCheckpoint === "",
});
- // setModels(listRes...) above used catalog data, which omits audio
- // capability. Re-apply live status so attach gates survive a refresh.
+ // setModels(listRes...) above used catalog data, which omits audio capability. Re-apply live
+ // status so attach gates survive a refresh.
syncModelCapabilities(checkpointId, statusRes);
// Against an already-resident model, history can load before this first status
@@ -577,15 +573,13 @@ async function syncInferenceStatusToStore(options?: {
!selectionChanged
) {
if (isIdleUnloadedStatus(statusRes, idleUnloadArmed)) return;
- // Loading an image, video or audio model evicts the chat one (the GPU arbiter
- // allows a single owner), and nothing else here would say so: the picker
- // keeps the selection, so the header would go on claiming it is loaded
+ // Loading an image, video or audio model evicts the chat one (one GPU owner), and nothing else
+ // here would say so: the picker keeps the selection, so the header would claim it is loaded
// and the next prompt would come back a bare 400.
const { residentCheckpoint: wasResident, modelLoading } =
useChatRuntimeStore.getState();
- // specFallbackReason survives here, so clearing activeModelIsLocal
- // alone would flip a local model's warning to "download failed". Every
- // load path and clearCheckpoint set it, so leave it consistent.
+ // specFallbackReason survives here, so clearing activeModelIsLocal alone would flip a local
+ // model's warning to "download failed".
useChatRuntimeStore.setState({
residentCheckpoint: null,
modelRequiresTrustRemoteCode: false,
@@ -601,24 +595,21 @@ async function syncInferenceStatusToStore(options?: {
selectedCheckpoint &&
(!modelLoading || options?.externalChatSlotLoad)
) {
- // Already the clean id the header shows: resolveInferenceCheckpointId
- // put it there, not a load path.
+ // Already the clean id the header shows: resolveInferenceCheckpointId put it there, not a load path.
if (wasResident) {
toast.info(`${wasResident} is no longer loaded`, {
description:
"The server released it, which loading an image, video or audio model does. Pick it again to keep chatting.",
});
}
- // Drop the pick too, which is what a server-side unload already does.
- // Dimming the tick was not enough: the name alone reads as "this is my
- // model" whatever the tick does, and sending to it returns a bare 400.
+ // Drop the pick too, as a server-side unload does: the name alone reads as "this is my model"
+ // whatever the tick does, and sending to it returns a bare 400.
useChatRuntimeStore.getState().clearCheckpoint();
}
}
} catch (error) {
- // A superseded refresh reports nothing, or a stale failure would raise a
- // toast about a read whose answer would have been discarded anyway. The LoRA
- // inventory settles from its own request above, never from a sibling's failure.
+ // A superseded refresh reports nothing, or a stale failure would raise a toast about a read
+ // whose answer would have been discarded. The LoRA inventory settles from its own request.
if (signal?.aborted || superseded()) return;
const message =
error instanceof Error ? error.message : "Failed to load models";
@@ -664,8 +655,7 @@ async function refreshAndWaitForServerModel(options?: {
* the refresh below leaves it intact.
*/
export async function resyncInferenceStatusAfterServerModelChange(): Promise<void> {
- // Both llama.cpp update paths land here, and an update replaces the binary whose
- // --help the flag catalogue describes.
+ // Both llama.cpp update paths land here, and an update replaces the binary whose --help the flag catalogue describes.
invalidateLlamaFlagCatalog();
if (!isExternalModelId(useChatRuntimeStore.getState().params.checkpoint)) {
useChatRuntimeStore.getState().clearCheckpoint();
@@ -775,17 +765,14 @@ export function useChatModelRuntime() {
[],
);
- // Nothing here polls /status: refresh runs on mount and when the model lists
- // change, never on a timer. So an image or video load evicting the chat model
- // (the GPU arbiter allows one owner) went unseen, and the header went on
- // showing the evicted model as loaded until something else happened to
- // refresh. Re-read whenever another runtime finishes a load.
+ // Nothing here polls /status: refresh runs on mount and when the model lists change. So an image
+ // or video load evicting the chat model went unseen and the header showed it as loaded.
+ // Re-read whenever another runtime finishes a load.
useEffect(
() =>
subscribeModelLifecycle(({ runtime }) => {
- // Dictation is a sidecar and evicts nothing; chat's own loads reconcile
- // themselves. A "tts" load does neither: it is the Audio page taking this very
- // slot, so read it back like an image or video load.
+ // Dictation is a sidecar and evicts nothing; chat's own loads reconcile themselves. A "tts" load
+ // does neither: it is the Audio page taking this very slot.
if (runtime === "chat" || runtime === "stt") return;
// Both edges, not only the settle. The arbiter evicts chat inside the
// image or video load POST, before the download starts, so waiting for
@@ -831,12 +818,10 @@ export function useChatModelRuntime() {
void (async () => {
try {
// Unforced on purpose: a chat may stream on the PREVIOUS model and must not be killed by
- // cancelling this load. Nothing to report, since the route runs its stop-loading fast
- // path ahead of the active-chat refusal.
+ // cancelling this load. Nothing to report, since the route runs its stop-loading path first.
await unloadModel({ model_path: model.id }).catch(() => {});
- // clearCheckpoint above assumed nothing was left loaded, but a forced switch keeps the
- // previous model resident until /load's teardown, and the stop-loading fast path leaves
- // it there. Take the answer from the backend, which reports none once it was evicted.
+ // clearCheckpoint above assumed nothing was left loaded, but a forced switch keeps the previous
+ // model resident until /load's teardown. Take the answer from the backend.
await syncInferenceStatusToStore().catch(() => {});
} finally {
cancelUnloadPendingRef.current = false;
@@ -900,8 +885,8 @@ export function useChatModelRuntime() {
loadingModelRef.current ??
useChatRuntimeStore.getState().loadingModelPick;
if (!inFlightLoad) return false;
- // The helper form, not an inline apply: it also carries the loaded
- // diffusion flag, which the restored config needs to stay correct.
+ // The helper form, not an inline apply: it also carries the loaded diffusion flag, which the
+ // restored config needs.
restorePreviousConfig();
const loadingSamePick =
inFlightLoad.id === modelId &&
@@ -919,30 +904,28 @@ export function useChatModelRuntime() {
};
if (bailIfLoadInFlight()) return;
- // Ask the backend, not params.checkpoint: an external pick leaves the local model
- // resident, and a pinned cached row loads under a name its picker row never shows.
+ // Ask the backend, not params.checkpoint: an external pick leaves the local model resident, and
+ // a pinned cached row loads under a name its picker row never shows.
// A staged config always carries forceReload, so Apply still reloads and prompts.
const selectedCheckpoint =
useChatRuntimeStore.getState().params.checkpoint;
const pendingConfig =
typeof selection !== "string" ? selection.config : undefined;
- // nativePathToken is excluded: a leased file is named by a label two files can share,
- // and only a completed load writes the lease, so adopting would keep a stale token.
+ // nativePathToken is excluded: a leased file is named by a label two files can share, and only a
+ // completed load writes the lease, so adopting would keep a stale token.
if (!forceReload && !nativePathToken) {
const residentStatus = await getInferenceStatus().catch(() => null);
- // Warm before reconciling the remembered GPU pick below: load-on-selection can run
- // before any GPU hook mounted, and a cold cache passes the pick through unvalidated
- // while performLoad, which warms it first, would have dropped it.
+ // Warm before reconciling the remembered GPU pick below: load-on-selection can run before any
+ // GPU hook mounted, and a cold cache passes the pick through unvalidated.
if (residentStatus && pendingConfig?.selectedGpuIds !== undefined) {
await ensureGpuDeviceCache().catch(() => {});
}
- // What /load would carry for a pick with no saved config, which is not always the
- // live runtime: performLoad treats a different checkpoint or variant as a model
- // switch and clears the per-model fields first (resetsPerModelSettings), so on that
- // door the request is the defaults, and comparing the outgoing model's settings
- // would both prompt for a load that dedupes and adopt one that does not.
- // gpuMemoryMode is standing and kvCacheDtype and tensorParallel are not reset, so
- // those three still come from the store.
+ // What /load would carry for a pick with no saved config, which is not always the live runtime:
+ // performLoad treats a different checkpoint or variant as a switch and clears the per-model
+ // fields first, so comparing the outgoing model's settings would both prompt for a load that
+ // dedupes and adopt one that does not. gpuMemoryMode and the dtypes still come from the store.
+ // gpuMemoryMode is standing, and kvCacheDtype and tensorParallel are not reset, so those three
+ // still come from the store.
const live = useChatRuntimeStore.getState();
const resetsPerModelSettings = Boolean(
live.params.checkpoint &&
@@ -960,14 +943,13 @@ export function useChatModelRuntime() {
gpuMemoryMode: live.gpuMemoryMode,
}
: currentRuntimePerModelConfig());
- // The slot count an unset --parallel resolves to. Session-cached, and 0 is the
- // catalogue's own "unknown", which the comparison reads as a reload.
+ // The slot count an unset --parallel resolves to. Session-cached, and 0 is the catalogue's own
+ // "unknown", which the comparison reads as a reload.
const managedFlags = residentStatus
? await loadManagedLlamaFlags().catch(() => null)
: null;
- // Hoisted so it can run twice: everything above was awaited, and in that window
- // another tab can swap the resident model, which this one is never told about
- // (the lifecycle events are dispatched on its own window).
+ // Hoisted so it can run twice: everything above was awaited, and in that window another tab can
+ // swap the resident model, which this one is never told about.
const adoptable = (status: InferenceStatusResponse) =>
(status.loading?.length ?? 0) === 0 &&
residentModelMatchesPick(status, {
@@ -975,35 +957,28 @@ export function useChatModelRuntime() {
loadPath,
ggufVariant,
}) &&
- // _reuse_loaded_gguf refuses its own already-loaded answer while the audio probe
- // is outstanding, so load_model reaches its fast path and re-probes there.
- // Nothing else does, so skipping /load would leave the model's audio
+ // _reuse_loaded_gguf refuses its own already-loaded answer while the audio probe is outstanding,
+ // so load_model reaches its fast path and re-probes there. Skipping /load would leave audio
// capabilities undetected for as long as the server runs.
status.audio_probe_pending !== true &&
- // A repairable speculative fallback is the one case where an identical request is
- // not a no-op: _runtime_matches_intent deliberately answers False for it so the
- // next load retries the drafter. Short-circuiting would suppress that and strand
- // the user with speculation off, so let /load through and let the backend decide.
+ // A repairable speculative fallback is the one case where an identical request is not a no-op:
+ // _runtime_matches_intent answers False so the next load retries the drafter, and
+ // short-circuiting would strand the user with speculation off.
!residentSpeculativeNeedsRepair(
status,
normalizeSpeculativeType(pendingConfig?.speculativeType) ??
readPersistedSpeculativeType(),
- // The route derives gguf_path from the identifier alone, and the
- // drafter_not_found retry is guarded on its absence.
+ // The route derives gguf_path from the identifier alone, and the drafter_not_found retry is
+ // guarded on its absence.
(loadPath ?? modelId).toLowerCase().endsWith(".gguf"),
) &&
- // The id names the weights, not how the server was invoked. A remembered context
- // length, drafter, placement or extra arg the resident load does not run is a real
- // reload, and skipping it would drop the setting with nothing on screen to say so.
- // Not `pendingConfig`: with no saved record the caller has already run
- // applyModelLoadConfigToRuntime(null), which resets the store to
- // DEFAULT_PER_MODEL_CONFIG, and performLoad reads the store for every field the
- // config does not carry. The live store is therefore what /load would send, on
- // both doors: reset to defaults after a hub or handoff pick, and still the
- // resident values where nothing reset it.
+ // The id names the weights, not how the server was invoked: a remembered context length,
+ // drafter, placement or extra arg the resident load does not run is a real reload. Not
+ // `pendingConfig`: with no saved record the caller has already reset the store to defaults,
+ // and performLoad reads the store for every field the config does not carry.
residentRuntimeMatchesConfig(status, comparedConfig, {
- // What the applier fills an unset field with, so the comparison is against
- // what /load would send rather than against silence.
+ // What the applier fills an unset field with, so the comparison is against what /load would send
+ // rather than against silence.
speculativeType: readPersistedSpeculativeType(),
gpuMemoryMode: readPersistedGpuMemoryMode(),
gpuLayers: GPU_LAYERS_AUTO,
@@ -1039,13 +1014,13 @@ export function useChatModelRuntime() {
});
},
parallelSlots: managedFlags?.defaultParallelSlots || null,
- // Never a config field, so the store is the only place it can come from,
- // and the reset clears it before the load reads it.
+ // Never a config field, so the store is the only place it can come from, and the reset clears it
+ // before the load reads it.
splitRatio: resetsPerModelSettings
? null
: useChatRuntimeStore.getState().splitRatio,
- // What /load sends: a pick saved in another index namespace, or naming GPUs
- // that are gone, is reconciled to Automatic before it leaves.
+ // What /load sends: a pick saved in another index namespace, or naming GPUs that are gone, is
+ // reconciled to Automatic before it leaves.
reconcileGpuIds: (ids, savedIndexKind) =>
reconcilePersistedGpuIds(
ids,
@@ -1058,30 +1033,26 @@ export function useChatModelRuntime() {
residentStatus &&
adoptable(residentStatus) &&
// Both are server-wide, so a save leaves the pick and its config identical while
- // adopt_load_intent_if_matched forces a reload anyway. Without them a policy or
- // budget change made between two picks of one model would never reach the child.
+ // adopt_load_intent_if_matched forces a reload anyway; without them a policy or budget
+ // change between two picks would never reach the child.
!(await readServerWideReloadHints())
) {
- // Read again, and judge again. A status fetched before those awaits describes the
- // model that was resident then, and adopting it would leave the picker naming
- // this model while prompts went to the one now loaded. A read that fails, or a
- // verdict that no longer holds, falls through to /load, where a real
- // disagreement belongs.
+ // Read again, and judge again: a status fetched before those awaits describes the model that was
+ // resident then, and adopting it would leave the picker naming this model while prompts went
+ // to another. A failed read falls through to /load.
const confirmedStatus = await getInferenceStatus().catch(() => null);
if (confirmedStatus && adoptable(confirmedStatus)) {
- // Same window as the confirm below: a rival load may have started during that GET,
- // and it owns the resident model now.
+ // Same window as the confirm below: a rival load may have started during that GET, and it owns
+ // the resident model now.
if (bailIfLoadInFlight()) return;
- // Roll back the config pre-applied for the load that is not happening, before
- // hydrating, so the resident status wins over the staged snapshot. The helper form
- // carries the loaded diffusion flag a resident image model needs.
+ // Roll back the config pre-applied for the load that is not happening, before hydrating, so the
+ // resident status wins over the staged snapshot. The helper carries the diffusion flag.
restorePreviousConfig();
- // ...except for maxSeqLength, which the rollback has the last word on: it is a
- // client-side generation cap, no status field echoes it, and applyActiveModelStatus
- // below therefore cannot correct it. Leaving it rolled back would hand this model
- // the OUTGOING one's cap, which is visible in truncation but nowhere on screen.
- // An absent cap is not silence either: applyPerModelConfigToRuntime resolves it to
- // defaultInferenceParams.maxSeqLength, so leaving it unset kept the outgoing cap.
+ // ...except for maxSeqLength, which the rollback has the last word on: it is a client-side cap
+ // no status field echoes, so leaving it rolled back would hand this model the OUTGOING one's
+ // cap, and applyActiveModelStatus below cannot correct it. An absent cap is not silence
+ // either: applyPerModelConfigToRuntime resolves it to defaultInferenceParams.maxSeqLength,
+ // so leaving it unset kept the outgoing cap.
const pickedMaxSeqLength =
normalizeMaxSeqLength(pendingConfig?.maxSeqLength) ??
defaultInferenceParams.maxSeqLength;
@@ -1094,9 +1065,9 @@ export function useChatModelRuntime() {
}
const previousGgufVariant =
useChatRuntimeStore.getState().activeGgufVariant;
- // Adopt this pick's own pin, by the rule a completed load writes it: the poll skips
- // its clearing while an external pick is active, so a pin taken for an earlier
- // resident would survive and Apply would reload that old model.
+ // Adopt this pick's own pin, by the rule a completed load writes it: the poll skips its clearing
+ // while an external pick is active, so a pin taken for an earlier resident would survive and
+ // Apply would reload that old model.
useChatRuntimeStore.setState({
activeLoadId: loadPath === modelId ? null : loadPath,
});
@@ -1108,10 +1079,9 @@ export function useChatModelRuntime() {
previousGgufVariant,
});
syncModelCapabilities(modelId, confirmedStatus);
- // The pick's own GPU selection, which the hydration above would otherwise
- // widen back. adopt_load_intent_if_matched records the incoming pool when it
- // adopts on the fitted subset (_record_matching_gpu_request), and skipping
- // /load skips that, so the status still names the GPUs the user removed.
+ // The pick's own GPU selection, which the hydration above would widen back:
+ // adopt_load_intent_if_matched records the incoming pool when it adopts on the fitted
+ // subset, and skipping /load skips that.
if (pendingConfig?.selectedGpuIds !== undefined) {
const picked = reconcilePersistedGpuIds(
pendingConfig.selectedGpuIds,
@@ -1126,17 +1096,16 @@ export function useChatModelRuntime() {
});
}
}
- // setCheckpoint above blanked the bar, this path returns before the post-load recount,
- // and a mounted thread does not rerun its history loader, so the bar would stay empty.
+ // setCheckpoint above blanked the bar, this path returns before the post-load recount, and a
+ // mounted thread does not rerun its history loader, so the bar would stay empty.
void refreshContextUsage({ afterModelLoad: true });
return;
}
}
}
- // Block queue materialization before taking the cancellation snapshot.
- // A queue that appears while the dialog is open must not be stopped
- // without having been included in the user's confirmation.
+ // Block queue materialization before taking the cancellation snapshot: a queue that appears while
+ // the dialog is open must not be stopped without having been included in the confirmation.
const lifecycleLease = useChatRuntimeStore
.getState()
.beginModelLoading();
@@ -1176,8 +1145,7 @@ export function useChatModelRuntime() {
}
return;
}
- // Re-check the tracked picker for a load that was already starting when
- // this lifecycle lease was acquired.
+ // Re-check the tracked picker for a load that was already starting when this lifecycle lease was acquired.
try {
if (bailIfLoadInFlight()) {
releasePreflightLifecycleLease();
@@ -1197,10 +1165,9 @@ export function useChatModelRuntime() {
typeof selection === "string" ? false : selection.isDownloaded ?? false;
const model = models.find((entry) => entry.id === modelId);
const lora = loras.find((entry) => entry.id === modelId);
- // A native path-token selection is a local GGUF by construction (the
- // native model intents only grant .gguf files), but its id is a display
- // label that need not end in ".gguf" -- without this, Manual + Auto
- // layers would pin the UI context instead of letting --fit size it.
+ // A native path-token selection is a local GGUF by construction, but its id is a display label
+ // that need not end in ".gguf": without this, Manual + Auto layers would pin the UI context
+ // instead of letting --fit size it.
const isGguf =
explicitIsGguf ??
(ggufVariant != null ||
@@ -1286,12 +1253,13 @@ export function useChatModelRuntime() {
let previousWasUnloaded = false;
const pendingLoadConfig =
typeof selection !== "string" ? selection.config : undefined;
- // The outgoing model's slot INTENT (blank = follow the server
- // default), which the resolved baseline cannot express. previousConfig
- // is the snapshot the picker took before pre-applying the target's
- // config, so the live control is only the outgoing one without it.
- // Read before the staged-metadata await below so an in-flight change
- // cannot perturb the outgoing snapshot.
+ // The outgoing model's slot INTENT (blank = follow the server default), which the resolved
+ // baseline cannot express. previousConfig is the snapshot taken before pre-applying the
+ // target's config. Read before the staged-metadata await so a change cannot perturb it.
+ // Unlike tensorParallel, vision does NOT survive a model switch: it is per-model config defaulting
+ // to vision on, so a target that saved none gets that default, not the outgoing model's setting.
+ // The dedupe above builds its own view of an unconfigured switch from DEFAULT_PER_MODEL_CONFIG,
+ // and resetsPerModelSettings cannot repair it, since this constant is captured before that block.
const previousNParallel =
typeof selection !== "string" && selection.previousConfig
? (selection.previousConfig.nParallel ?? null)
@@ -1304,8 +1272,8 @@ export function useChatModelRuntime() {
typeof selection !== "string" && selection.previousConfig
? (selection.previousConfig.nUbatch ?? null)
: useChatRuntimeStore.getState().nUbatch;
- // The outgoing tuning intent, read the same way and for the same
- // reason: a rollback restores the control, not the echo.
+ // The outgoing tuning intent, read the same way and for the same reason: a rollback restores the
+ // control, not the echo.
const previousServerTuning: ServerTuningValues =
typeof selection !== "string" && selection.previousConfig
? selection.previousConfig
@@ -1359,10 +1327,8 @@ export function useChatModelRuntime() {
|| previousVariant != null
|| previousActiveNativePathToken != null
|| (previousCheckpoint?.toLowerCase().endsWith(".gguf") ?? false);
- // Roll back to the previous model's own context. previousConfig was
- // snapshotted before this load pre-applied the next model's config, so
- // params.maxSeqLength may already be the next model's; use it only when
- // no snapshot exists.
+ // Roll back to the previous model's own context: previousConfig was snapshotted before this load
+ // pre-applied the next model's config, so params.maxSeqLength may already be the next model's.
const previousMaxSeqLength =
(typeof selection !== "string"
? selection.previousConfig?.maxSeqLength
@@ -1400,11 +1366,9 @@ export function useChatModelRuntime() {
stateBeforeUnload.modelRequiresTrustRemoteCode;
const previousActiveNativePathExpiresAtMs =
stateBeforeUnload.activeNativePathExpiresAtMs;
- // Snapshot the load settings at click time, before the awaits below
- // (validation, the trust dialog, unload). When the picker staged a
- // config payload, prefer it over the store: React may not have
- // flushed NumericValueInput's blur commit into state yet.
- // Per-model: a template is written against one model's tokens.
+ // Snapshot the load settings at click time, before the awaits below. When the picker staged a
+ // config payload, prefer it over the store: React may not have flushed NumericValueInput's
+ // blur commit yet. Per-model, since a template is written against one model's tokens.
let loadChatTemplateOverride =
pendingLoadConfig?.chatTemplateOverride?.trim()
? pendingLoadConfig.chatTemplateOverride
@@ -1414,9 +1378,11 @@ export function useChatModelRuntime() {
// Per-model, not a standing preference: eligibility is decided per model.
let loadMlxKvBits =
pendingLoadConfig?.mlxKvBits ?? stateBeforeUnload.mlxKvBits;
- // gpuMemoryMode is a standing preference (kept across a model switch);
- // the rest are per-model knobs the reset below clears, so they are
- // re-baselined there in lock-step with the store.
+ // gpuMemoryMode is a standing preference; the rest are per-model knobs the reset below clears, so
+ // they are re-baselined there in lock-step with the store.
+ // A GGUF native context can exceed maxSeqLength, so sizing on raw maxSeqLength could pass, unload,
+ // then have /load refuse it. Uses the click-time snapshot, so the two agree. A same-repo quant
+ // switch (different gguf_variant) is a different model for per-model knobs, so re-baseline them.
let loadCustomContextLength =
pendingLoadConfig?.customContextLength ??
stateBeforeUnload.customContextLength;
@@ -1425,16 +1391,11 @@ export function useChatModelRuntime() {
? false
: (pendingLoadConfig?.tensorParallel ??
stateBeforeUnload.tensorParallel);
- // The diffusion runner has no projector to skip, so the toggle is inert
- // there for the same reason tensorParallel is.
- //
- // Unlike tensorParallel, this does NOT survive a model switch: it is
- // per-model config defaulting to vision on, so a target that saved none gets
- // that default, not the outgoing model's setting. Carrying it over loaded the
- // new model text-only and silently, because the dedupe comparison above
- // already builds its own view of an unconfigured switch from
- // DEFAULT_PER_MODEL_CONFIG. resetsPerModelSettings cannot repair it: this
- // constant is captured before that block runs.
+ // The diffusion runner has no projector to skip, so the toggle is inert there. Unlike
+ // tensorParallel this does NOT survive a model switch: it is per-model config defaulting to
+ // vision on, and carrying it over loaded the new model text-only and silently, because the
+ // dedupe comparison builds its own view of an unconfigured switch. resetsPerModelSettings
+ // cannot repair it, since this constant is captured before that block runs.
const loadSwitchesModelOrVariant = Boolean(
currentCheckpoint &&
(currentCheckpoint !== modelId ||
@@ -1457,11 +1418,9 @@ export function useChatModelRuntime() {
let loadNCpuMoe =
pendingLoadConfig?.nCpuMoe ?? stateBeforeUnload.nCpuMoe;
let loadSplitRatio = stateBeforeUnload.splitRatio;
- // Reconcile the persisted pick against the GPUs present now, so a stale
- // cross-host / now-hidden pick is dropped before /load rather than
- // rejected there. Warm the device cache first: load-on-selection can
- // run before any GPU hook mounted, and a cold cache would pass the
- // pick through unvalidated. validateGpuIds derives from this too.
+ // Reconcile the persisted pick against the GPUs present now, so a stale cross-host pick is
+ // dropped before /load rather than rejected there. Warm the device cache first: a cold cache
+ // would pass the pick through unvalidated.
if (
pendingLoadConfig?.selectedGpuIds !== undefined ||
stateBeforeUnload.selectedGpuIds != null
@@ -1488,10 +1447,9 @@ export function useChatModelRuntime() {
pendingLoadConfig?.specDraftNMax ?? stateBeforeUnload.specDraftNMax;
let loadNParallel =
pendingLoadConfig?.nParallel ?? stateBeforeUnload.nParallel;
- // No store fallback and no reset with the rest below: undefined means
- // "this config never read them", and the route preserves the stored
- // flags when the field is omitted. Falling back to another model's
- // value, or to null, would clear flags the user set elsewhere.
+ // No store fallback and no reset with the rest below: undefined means "this config never read
+ // them", and the route preserves the stored flags when the field is omitted, so a fallback
+ // would clear flags the user set elsewhere.
const loadLlamaExtraArgs = pendingLoadConfig?.llamaExtraArgs;
let loadNBatch =
pendingLoadConfig?.nBatch ?? stateBeforeUnload.nBatch;
@@ -1508,20 +1466,15 @@ export function useChatModelRuntime() {
cacheRam: pendingLoadConfig?.cacheRam ?? stateBeforeUnload.cacheRam,
};
try {
- // Lightweight pre-flight validation: avoid unloading a working model
- // if the new identifier is clearly invalid (e.g. bad HF id / path).
+ // Lightweight pre-flight validation: avoid unloading a working model if the new identifier is
+ // clearly invalid.
const validateNativePathLease = nativePathToken
? (await consumeNativePathToken(nativePathToken, "validate-model")).nativePathLease
: undefined;
- // Validate with the same effective context /load uses: a GGUF native
- // context can exceed maxSeqLength, so sizing on raw maxSeqLength could
- // pass, unload, then have /load refuse it. Uses the click-time
- // snapshot (same values loadModel uses below), so the two agree.
- // Mirror /load on a cross-model switch: the reset below clears the
- // per-model Auto-layers context pin + GPU pick; gpuMemoryMode is a
- // standing preference kept across the switch. A same-repo quant switch
- // (different gguf_variant) is a different model for per-model knobs
- // (context/gpuLayers/pick/MoE are per variant), so re-baseline them too.
+ // Validate with the same effective context /load uses: a GGUF native context can exceed
+ // maxSeqLength, so sizing on raw maxSeqLength could pass, unload, then have /load refuse it.
+ // Mirror /load on a cross-model switch, and treat a same-repo quant switch as a different
+ // model for the per-model knobs.
const switchingModelOrVariant =
currentCheckpoint !== modelId ||
(loadActiveGgufVariant ?? null) !== (ggufVariant ?? null);
@@ -1596,8 +1549,8 @@ export function useChatModelRuntime() {
...(isGguf
? {
gpu_memory_mode: loadGpuMemoryMode,
- // Sized like the follow-up /load: else a manual DiffusionGemma
- // split 409s during training even when it fits.
+ // Sized like the follow-up /load: else a manual DiffusionGemma split 409s during training even
+ // when it fits.
gpu_layers: validateGpuLayers,
n_parallel: validateNParallel,
// omitted when blank, like the load payload below
@@ -1607,14 +1560,12 @@ export function useChatModelRuntime() {
...(validateNUbatch != null
? { n_ubatch: validateNUbatch }
: {}),
- // Same omit-when-blank rule, and the same values: the preflight
- // has to approve the command the follow-up /load sends.
+ // Same omit-when-blank rule, and the same values: the preflight has to approve the command the
+ // follow-up /load sends.
...serverTuningLoadPayload(validateServerTuning),
- // The same list the load below sends. A --ctx-size or cache
- // override in here changes the memory this preflight estimates,
- // so omitting it approves a different command: during training
- // that means approving the switch, unloading the resident model,
- // and having /load refuse the target with the real arguments.
+ // The same list the load below sends: a --ctx-size or cache override changes the memory this
+ // preflight estimates, so omitting it approves a different command and /load then refuses
+ // the target with the real arguments.
...(!targetIsDiffusion && loadLlamaExtraArgs !== undefined
? { llama_extra_args: loadLlamaExtraArgs ?? [] }
: {}),
@@ -1628,14 +1579,12 @@ export function useChatModelRuntime() {
upgrade: validation.transformers_upgrade,
// No installable release: custom-code models may fall back to the trust_remote_code gate below.
trustRemoteCodeFallback: validation.requires_trust_remote_code,
- // The install refuses while chats generate and takes no force flag of its own, so
- // without this the "Stop and reload" the user just confirmed dies here: Retry hits
- // the same 409, and this path leaves chats running.
+ // The install refuses while chats generate and takes no force flag of its own, so without this
+ // the "Stop and reload" the user just confirmed dies here and Retry hits the same 409.
forceCancelActive,
});
- // The install unloads the previous model before the swap (even when
- // the swap then fails), so any exit after this point must roll back.
- // False for the custom-code fallback, which resolves without installing.
+ // The install unloads the previous model before the swap even when the swap fails, so any exit
+ // after this point must roll back. False for the custom-code fallback, which installs nothing.
if (
useTransformersUpgradeDialogStore
.getState()
@@ -1649,9 +1598,9 @@ export function useChatModelRuntime() {
}
}
if (abortCtrl.signal.aborted) throw new Error("Cancelled");
- // Open the consent dialog when the model needs custom-code consent or has a
- // flagged unsafe file. Fires even when trustRemoteCode is preset on, since the
- // worker requires a matching fingerprint that only the dialog produces.
+ // Open the consent dialog when the model needs custom-code consent or has a flagged unsafe file.
+ // Fires even when trustRemoteCode is preset on, since the worker requires a matching
+ // fingerprint only the dialog produces.
if (
validation.requires_trust_remote_code
|| validation.requires_security_review
@@ -1678,25 +1627,21 @@ export function useChatModelRuntime() {
requestLocalPromptQueueStop(stopDecision.promptQueueThreadIds);
if (currentCheckpoint) {
// With chats generating, skip this preliminary unload: it cancels them ahead of /load's
- // preflight, so a rejected target truncates replies for a model that never loads
- // (/load evicts past those checks itself). Idle, unload first and free VRAM early.
+ // preflight, so a rejected target truncates replies for a model that never loads. Idle,
+ // unload first and free VRAM early.
if (!forceCancelActive) {
await unloadModel({ model_path: currentCheckpoint });
}
- // Set either way: /load can still leave no model resident, and an unneeded rollback
- // hits already_loaded before the gate.
+ // Set either way: /load can still leave no model resident, and an unneeded rollback hits
+ // already_loaded before the gate.
previousWasUnloaded = true;
}
if (abortCtrl.signal.aborted) throw new Error("Cancelled");
- // On a model switch, fall back to the persisted standing
- // preference rather than null so a per-session forced MTP mode
- // can't follow the user onto a model without an MTP head.
- // spec_draft_n_max is MTP-only and always resets. The loaded
- // shadow is seeded too, preventing a transient dirty Apply state.
- // keepSpeculative skips this for a staged Load: the user picked the
- // mode for this model on the sidebar, so honor it (the backend still
- // falls back at runtime if the model has no MTP head).
+ // On a model switch, fall back to the persisted standing preference rather than null so a
+ // per-session forced MTP mode cannot follow the user onto a model without an MTP head.
+ // spec_draft_n_max is MTP-only and always resets, and the loaded shadow is seeded to prevent
+ // a transient dirty Apply. keepSpeculative skips this for a staged Load.
if (resetsPerModelSettings) {
const persistedSpeculativeType = readPersistedSpeculativeType();
useChatRuntimeStore.setState({
@@ -1704,26 +1649,23 @@ export function useChatModelRuntime() {
loadedSpeculativeType: persistedSpeculativeType,
specDraftNMax: null,
loadedSpecDraftNMax: null,
- // Per-model too: a different model follows the server default
- // unless its staged config overrides it.
+ // Per-model too: a different model follows the server default unless its staged config overrides it.
nParallel: null,
loadedNParallel: null,
nBatch: null,
loadedNBatch: null,
nUbatch: null,
loadedNUbatch: null,
- // Per-model too, and cleared in both halves: a baseline left from
- // the model that just went would be re-sent by a later rollback.
+ // Per-model too, and cleared in both halves: a baseline left from the model that just went would
+ // be re-sent by a later rollback.
...clearedServerTuningState(),
- // Per-model GPU knobs must not follow onto a different model
- // (gpuMemoryMode is a standing preference and is kept).
+ // Per-model GPU knobs must not follow onto a different model (gpuMemoryMode is standing and is kept).
selectedGpuIds: null,
selectedGpuIndexKind: null,
gpuLayers: GPU_LAYERS_AUTO,
nCpuMoe: 0,
splitRatio: null,
- // A Manual+Auto context pin is per-model; clear it so a different
- // model loads at Auto/native, not the previous model's pin.
+ // A Manual+Auto context pin is per-model; clear it so a different model loads at Auto/native.
customContextLength: null,
});
loadSpeculativeType =
@@ -1741,17 +1683,15 @@ export function useChatModelRuntime() {
ctxCheckpoints: pendingLoadConfig?.ctxCheckpoints ?? null,
cacheRam: pendingLoadConfig?.cacheRam ?? null,
};
- // Both payload-only. The store keeps its values: a width is dormant
- // preset state off MLX, and a completed load rewrites both anyway.
+ // Both payload-only. The store keeps its values: a width is dormant preset state off MLX, and a
+ // completed load rewrites both anyway.
loadMlxKvBits = pendingLoadConfig?.mlxKvBits ?? null;
loadChatTemplateOverride =
pendingLoadConfig?.chatTemplateOverride?.trim()
? pendingLoadConfig.chatTemplateOverride
: null;
- // Keep the click-time snapshot in lock-step with the store reset so
- // the load below sizes against the cleared per-model knobs, not the
- // previous model's (gpuMemoryMode is standing, so left as captured).
- // An explicit staged config from run-settings still wins.
+ // Keep the click-time snapshot in lock-step with the store reset so the load sizes against the
+ // cleared per-model knobs. An explicit staged config from run-settings still wins.
loadCustomContextLength =
pendingLoadConfig?.customContextLength ?? null;
loadSelectedGpuIds =
@@ -1825,9 +1765,8 @@ export function useChatModelRuntime() {
);
const effectiveChatTemplateOverride =
loadChatTemplateOverride?.trim() ? loadChatTemplateOverride : null;
- // A queue can be created while the preliminary unload is pending.
- // Stop a second time at the final boundary so no prompt captured
- // against the outgoing checkpoint survives into the new backend.
+ // A queue can be created while the preliminary unload is pending, so stop a second time at the
+ // final boundary.
requestLocalPromptQueueStop();
const loadResponse = await loadModel({
model_path: loadPath,
@@ -1846,11 +1785,8 @@ export function useChatModelRuntime() {
spec_draft_n_max: loadSpecDraftNMax,
// GGUF-only: slots mean nothing for a transformers load.
n_parallel: isGguf ? loadNParallel : null,
- // Sent only once known, and [] is the explicit "launch with none":
- // the flags are llama-server's, so a transformers load never carries
- // them, and neither does a diffusion GGUF: that one is GGUF-shaped but
- // runs through the visual runner, which builds its command without
- // these, so sending them would record arguments the process never got.
+ // Sent only once known, and [] is the explicit "launch with none": the flags are llama-server's,
+ // so neither a transformers load nor a diffusion GGUF carries them.
...(isGguf && !targetIsDiffusion && loadLlamaExtraArgs !== undefined
? { llama_extra_args: loadLlamaExtraArgs ?? [] }
: {}),
@@ -1859,8 +1795,7 @@ export function useChatModelRuntime() {
...(isGguf && loadNUbatch != null
? { n_ubatch: loadNUbatch }
: {}),
- // llama-server's own, so gated like the extras above: GGUF, and not
- // the diffusion runner, which builds its command without them.
+ // llama-server's own, so gated like the extras above: GGUF, and not the diffusion runner.
...(isGguf && !targetIsDiffusion
? serverTuningLoadPayload(loadServerTuning)
: {}),
@@ -1878,20 +1813,18 @@ export function useChatModelRuntime() {
cpuFallbackReason = loadResponse.cpu_fallback_reason ?? null;
mmprojFallbackReason = loadResponse.mmproj_fallback_reason ?? null;
- // If cancelled while loading, don't update UI to show
- // the model as active -- it's being unloaded.
+ // If cancelled while loading, do not show the model as active: it is being unloaded.
if (abortCtrl.signal.aborted) throw new Error("Cancelled");
- // The load applied this spec mode, so persist the user's standing
- // preference now (the requested intent, not the resolved echo;
- // saveSpeculativeType keeps only the universal auto/ngram/off).
- // Skip for a per-model config (keepSpeculative): that choice is
- // model-specific and must not overwrite the global default.
+ // The load applied this spec mode, so persist the user's standing preference now (the requested
+ // intent, not the resolved echo). Skipped for a per-model config, whose choice must not
+ // overwrite the global default.
+ // The requested intent, not the resolved echo: saveSpeculativeType keeps only the universal
+ // auto/ngram/off. Skip for a per-model config (keepSpeculative).
if (!keepSpeculative) {
saveSpeculativeType(loadSpeculativeType);
}
- // Persist the GPU Memory mode only on a successful load (not on
- // dropdown change), so an abandoned selection doesn't stick.
+ // Persist the GPU Memory mode only on a successful load, so an abandoned selection does not stick.
persistGpuMemoryModeOnLoad(loadResponse, loadGpuMemoryMode);
const currentParams = useChatRuntimeStore.getState().params;
@@ -1933,17 +1866,16 @@ export function useChatModelRuntime() {
maxTokensCap: loadedContextCap,
},
);
- // Qwen3.5/3.6 small models (0.8B, 2B, 4B, 9B) disable thinking by default.
- // Anchored regex: first "Xb" / "X.Xb" after start-of-string or
- // [-_/.] so the version literal in "qwen3.5" / "qwen3.6" doesn't
- // match first, and for "Qwen3.5-35B-A3B" the result is 35 (total
- // params), not 3 (MoE active params).
+ // Qwen3.5/3.6 small models disable thinking by default. Anchored regex: the first "Xb" after
+ // start-of-string or [-_/.], so "Qwen3.5-35B-A3B" gives 35 rather than 3.
+ // Qwen3.5/3.6 small models are 0.8B, 2B, 4B and 9B. The regex matches the first "Xb" or "X.Xb"
+ // after start or [-_/.], so "Qwen3.5-35B-A3B" yields 35 (total), not 3 (MoE active).
let reasoningDefault = loadResponse.supports_reasoning ?? false;
if (reasoningDefault) {
const mid = modelId.toLowerCase();
if (mid.includes("qwen3.5") || mid.includes("qwen3.6")) {
- // Scan path segments right to left so the size nearest the leaf
- // wins over a size-like parent dir; trailing boundary stops "8bit".
+ // Scan path segments right to left so the size nearest the leaf wins; the trailing boundary
+ // stops "8bit".
const sizeRe = /(?:^|[-_.])(\d+\.?\d*)\s*([bm])(?:$|[-_.])/;
const sizeMatch = mid
.replace(/\\/g, "/")
@@ -1964,9 +1896,8 @@ export function useChatModelRuntime() {
const loadedSpec = normalizeSpeculativeType(
loadResponse.speculative_type,
);
- // Slots the load actually committed. Non-GGUF never sends them and
- // diffusion ignores --parallel, so a click-time count on either
- // would mint a phantom override a saved preset carries onto a GGUF.
+ // Slots the load actually committed: non-GGUF never sends them and diffusion ignores --parallel,
+ // so a click-time count would mint a phantom override.
const committedSlots =
(loadResponse.is_gguf ?? false) &&
!(loadResponse.is_diffusion ?? false)
@@ -2000,8 +1931,8 @@ export function useChatModelRuntime() {
const supportsPreserveThinking =
loadResponse.supports_preserve_thinking ?? false;
const supportsTools = loadResponse.supports_tools ?? false;
- // GLM-5.2-style models report their own effort levels (e.g.
- // high|max); everything else keeps the default low/medium/high.
+ // GLM-5.2-style models report their own effort levels; everything else keeps the default low/medium/high.
+ // They report high|max.
const reasoningEffortLevels =
loadResponse.reasoning_effort_levels &&
loadResponse.reasoning_effort_levels.length > 0
@@ -2053,13 +1984,11 @@ export function useChatModelRuntime() {
tensorParallel: loadedTp,
loadedTensorParallel: loadedTp,
loadedDisableVision: loadResponse.disable_vision ?? false,
- // Repaired from the echo like the knob above: loadDisableVision
- // forces the flag off for a diffusion target without writing the
- // store, so a Vision-off GGUF followed by a diffusion load would
- // otherwise leave the switch off over a load that never sent it.
+ // Repaired from the echo like the knob above: loadDisableVision forces the flag off for a
+ // diffusion target without writing the store, so a Vision-off GGUF followed by a diffusion
+ // load would leave the switch off over a load that never sent it.
disableVision: loadResponse.disable_vision ?? false,
- // Set alongside loadedIsMultimodal so the composer can say WHY
- // images are unavailable.
+ // Set alongside loadedIsMultimodal so the composer can say WHY images are unavailable.
loadedVisionDisabledByUser:
loadResponse.vision_disabled_by_user ?? false,
...loadedGpuMemoryFields(loadResponse),
@@ -2067,29 +1996,20 @@ export function useChatModelRuntime() {
loadedSpeculativeType: loadedSpec,
specDraftNMax: loadResponse.spec_draft_n_max ?? null,
loadedSpecDraftNMax: loadResponse.spec_draft_n_max ?? null,
- // Keep the click-time value: the echo is the resolved count, and
- // adopting it would pin a blank "server default" control.
+ // Keep the click-time value: the echo is the resolved count, and adopting it would pin a blank
+ // "server default" control.
nParallel: committedSlots,
loadedNParallel: committedSlots,
nBatch: committedNBatch,
loadedNBatch: committedNBatch,
...committedServerTuning,
- // What this model is running, for the rollback below. An omitted field
- // inherited the resident process's list, so the last thing we knew
- // still holds unless this was a different model.
- //
- // An explicit empty list is recorded as empty, not as null: the
- // rollback sends this field only when it has one, and omitting it is
- // what makes /load inherit, so a model that was launched with no
- // extras would come back carrying the arguments of the load that just
- // failed. null stays for "we were never told".
- //
- // The server's own echo first, since it is the only account of what
- // the launch actually carried: a reload that omits the field but sets
- // max_seq_length has its inherited --ctx-size stripped before launch,
- // and the status refresh that would notice runs while modelLoading is
- // still true and reseeds nothing. Without this the next rollback
- // resent a flag the successful reload had removed.
+ // What this model is running, for the rollback below: an omitted field inherited the resident
+ // process's list, so the last thing we knew still holds unless this was a different model.
+ // An explicit empty list is recorded as empty, not null, since omitting the field is what
+ // makes /load inherit. The server's echo comes first, as the only account of what the launch
+ // carried: a reload that omits the field but sets max_seq_length is stripped of its
+ // inherited --ctx-size, and the status refresh that would notice runs while
+ // modelLoading is still true and reseeds nothing.
loadedLlamaExtraArgs:
loadResponse.requested_llama_extra_args !== undefined
? (loadResponse.requested_llama_extra_args ?? [])
@@ -2131,8 +2051,7 @@ export function useChatModelRuntime() {
) {
const store = useChatRuntimeStore.getState();
if (store.activePresetSource === "builtin-default") {
- // Same rule as the load response: defaults first, this model's
- // remembered settings over them.
+ // Same rule as the load response: defaults first, this model's remembered settings over them.
store.setParams({ ...store.params, ...p }, {
fromModelDefaults: true,
maxTokensCap: loadedContextCap,
@@ -2144,9 +2063,8 @@ export function useChatModelRuntime() {
(loadResponse.is_gguf || isGguf || ggufVariant) &&
!isExternalModelId(modelId),
);
- // Remembered so auto-load re-picks what the user ran, not the
- // smallest. Native file-picker paths need a signed, expiring lease,
- // so they stay out.
+ // Remembered so auto-load re-picks what the user ran, not the smallest. Native file-picker paths
+ // need a signed, expiring lease, so they stay out.
const indexedLocalPick =
typeof selection !== "string" && selection.source === "local";
if (
@@ -2166,7 +2084,7 @@ export function useChatModelRuntime() {
});
}
} catch (error) {
- // Skip rollback if user cancelled -- model is already being unloaded.
+ // Skip rollback if the user cancelled: the model is already being unloaded.
if (abortCtrl.signal.aborted) throw error;
// If we unloaded a previous model and the new load failed, attempt a rollback.
if (previousWasUnloaded && previousCheckpoint) {
@@ -2220,29 +2138,23 @@ export function useChatModelRuntime() {
ctxCheckpoints: stateBeforeUnload.loadedCtxCheckpoints,
cacheRam: stateBeforeUnload.loadedCacheRam,
}),
- // Explicit, unlike the batch pair above: the failed switch left the
- // TARGET resident, so an omitted field here inherits across models,
- // which the route refuses, and the previous model would come back
- // without the arguments it was running.
+ // Explicit, unlike the batch pair above: the failed switch left the TARGET resident, so an
+ // omitted field here inherits across models, which the route refuses.
...(stateBeforeUnload.loadedLlamaExtraArgs != null
? { llama_extra_args: stateBeforeUnload.loadedLlamaExtraArgs }
: {}),
- // Restore the previous model in the split mode it was running,
- // not the default layer split.
+ // Restore the previous model in the split mode it was running, not the default layer split.
tensor_parallel: stateBeforeUnload.loadedTensorParallel ?? false,
- // What the PREVIOUS server was loaded with. Not the control field:
- // applyPerModelConfigToRuntime writes disableVision before this
- // baseline is captured, so it holds the TARGET's setting. Not
- // loadedVisionDisabledByUser either: that is narrowed to models that
- // can do images, so a non-vision GGUF would come back with vision on
- // while the control restored to off. Same separately tracked baseline
- // tensor_parallel uses above.
+ // What the PREVIOUS server was loaded with. Not the control field, which
+ // applyPerModelConfigToRuntime has already overwritten with the TARGET's setting, and not
+ // loadedVisionDisabledByUser, which is narrowed to models that can do images. Same
+ // separately tracked baseline tensor_parallel uses.
disable_vision: stateBeforeUnload.loadedDisableVision ?? false,
// Restore the previous model's GPU Memory placement, not backend defaults.
gpu_memory_mode: stateBeforeUnload.loadedGpuMemoryMode ?? "auto",
gpu_layers: stateBeforeUnload.loadedGpuLayers ?? GPU_LAYERS_AUTO,
- // A recovered Vulkan model needs its staged CPU-only runtime back
- // after the failed target load unloaded the live server.
+ // A recovered Vulkan model needs its staged CPU-only runtime back after the failed target load
+ // unloaded the live server.
cpu_fallback: stateBeforeUnload.loadedCpuFallback,
n_cpu_moe: stateBeforeUnload.loadedNCpuMoe ?? 0,
tensor_split: stateBeforeUnload.loadedSplitRatio ?? undefined,
@@ -2257,13 +2169,13 @@ export function useChatModelRuntime() {
activeModelIsLocal: rollbackResponse.is_local_model ?? false,
activeLoadId: previousActiveLoadId ?? null,
activeNativePathToken: previousActiveNativePathToken ?? null,
- // Restore the previous token's lease together with the token so a
- // rollback never pairs restored token A with failed load B's expiry.
+ // Restore the previous token's lease together with the token so a rollback never pairs restored
+ // token A with failed load B's expiry.
activeNativePathExpiresAtMs: previousActiveNativePathToken
? (previousActiveNativePathExpiresAtMs ?? null)
: null,
- // Restore the editable speculative knobs to the rolled-back
- // model's; the loaded baselines below come from its reload echo.
+ // Restore the editable speculative knobs to the rolled-back model's; the loaded baselines come
+ // from its reload echo.
speculativeType: stateBeforeUnload.loadedSpeculativeType ?? null,
specDraftNMax: stateBeforeUnload.loadedSpecDraftNMax ?? null,
// Control keeps its intent; only the baseline takes the echo.
@@ -2273,8 +2185,8 @@ export function useChatModelRuntime() {
loadedNBatch: stateBeforeUnload.loadedNBatch ?? null,
nUbatch: previousNUbatch,
loadedNUbatch: stateBeforeUnload.loadedNUbatch ?? null,
- // Same split: the controls keep the outgoing model's intent and
- // the baselines come from what that model was launched with.
+ // Same split: the controls keep the outgoing model's intent and the baselines come from what
+ // that model was launched with.
loadMode: previousServerTuning.loadMode ?? null,
loadedLoadMode: stateBeforeUnload.loadedLoadMode ?? null,
specDraftCacheDtype:
@@ -2291,8 +2203,8 @@ export function useChatModelRuntime() {
rollbackResponse.spec_draft_n_max ?? null,
loadedKvCacheDtype: rollbackResponse.cache_type_kv ?? null,
...mlxRuntimeStateFrom(rollbackResponse),
- // After the spread, which seeds the control from the echo; the
- // control keeps its intent, like nParallel above.
+ // After the spread, which seeds the control from the echo; the control keeps its intent, like
+ // nParallel above.
mlxKvBits: previousMlxKvBits,
loadedChatTemplateOverride:
stateBeforeUnload.loadedChatTemplateOverride,
@@ -2302,13 +2214,9 @@ export function useChatModelRuntime() {
rollbackResponse.tensor_parallel ?? false,
loadedDisableVision:
rollbackResponse.disable_vision ?? false,
- // The rolled-back model's own loaded value, matching the request
- // above field for field. Not stateBeforeUnload.disableVision, which
- // holds the TARGET's value by now and would show Vision off over a
- // restored projector, arming the next Apply to switch it off for real.
- // Not the echo either: the replayed request is gated on the model
- // having a projector, so a text-only GGUF switched off echoes false
- // and would flip Vision back on.
+ // The rolled-back model's own loaded value, matching the request above field for field. Not
+ // stateBeforeUnload.disableVision, which holds the TARGET's value by now, and not the echo
+ // either, since the replayed request is gated on the model having a projector.
disableVision: stateBeforeUnload.loadedDisableVision ?? false,
loadedVisionDisabledByUser:
rollbackResponse.vision_disabled_by_user ?? false,
@@ -2357,16 +2265,14 @@ export function useChatModelRuntime() {
);
loadToastIdRef.current = toastId;
- // Poll download progress for non-cached models, then (after download
- // or for cached models) poll the llama-server mmap phase so "Starting
- // model..." doesn't look frozen for minutes on large MoE models.
+ // Poll download progress for non-cached models, then poll the llama-server mmap phase so
+ // "Starting model..." does not look frozen on large MoE models.
let progressInterval: ReturnType<typeof setInterval> | null = null;
const expectedBytes =
typeof selection !== "string" ? selection.expectedBytes ?? 0 : 0;
- // One buffer per phase, so a flip cannot price the new phase against
- // the old one's clock. Was a private copy of the shared estimator, so
- // fixes never reached this toast. The helper takes SECONDS, not ms.
+ // One buffer per phase, so a flip cannot price the new phase against the old one's clock. Was a
+ // private copy of the shared estimator, so fixes never reached this toast. Takes SECONDS.
const dlSamples: TransferSample[] = [];
const mmapSamples: TransferSample[] = [];
@@ -2376,9 +2282,8 @@ export function useChatModelRuntime() {
total: number,
): { rate: number; eta: number; stable: boolean } {
if (typeof document !== "undefined" && document.hidden) {
- // This 2s interval is clamped to about once a minute while hidden,
- // and the estimator reads gaps as the burst cadence. The hub poll
- // loop and the voice poller drop them the same way.
+ // This 2s interval is clamped to about once a minute while hidden, and the estimator reads gaps
+ // as the burst cadence.
samples.length = 0;
return { rate: 0, eta: 0, stable: false };
}
@@ -2442,11 +2347,9 @@ export function useChatModelRuntime() {
prog.expected_bytes,
dlSamples,
);
- // loadProgress state is only read by the dismissed-toast inline
- // status. Writing it while the toast is visible re-renders the
- // whole chat page every poll — cheap in Chrome, janky in the
- // desktop WebView2 (laggy typing). Feed the toast directly and
- // only touch state when the inline view is actually live.
+ // loadProgress state is only read by the dismissed-toast inline status, and writing it while the
+ // toast is visible re-renders the whole chat page every poll (janky in WebView2). Feed the
+ // toast directly instead.
if (loadToastDismissedRef.current) {
setLoadProgress({
percent: pct,
@@ -2476,7 +2379,7 @@ export function useChatModelRuntime() {
const est = estimate(dlSamples, prog.downloaded_bytes, 0);
const rateSuffix =
est.stable ? ` • ${formatRate(est.rate)}` : "";
- // Inline-status-only state; skip the chat-page re-render unless it's shown.
+ // Inline-status-only state; skip the chat-page re-render unless it is shown.
if (loadToastDismissedRef.current) {
setLoadProgress({
percent: null,
@@ -2522,14 +2425,12 @@ export function useChatModelRuntime() {
if (!loadingModelRef.current) return;
if (!prog || prog.phase == null) return;
if (prog.phase === "ready") {
- // Loaded. The chat flow will flip loadingModelRef shortly;
- // just stop polling.
+ // Loaded. The chat flow will flip loadingModelRef shortly; just stop polling.
if (progressInterval) clearInterval(progressInterval);
return;
}
if (prog.bytes_total <= 0) return; // nothing useful to render
- // Decimal GB (1e9) so the total matches the file size Hugging Face
- // reports and the model-picker shows, not the smaller base-1024 GiB.
+ // Decimal GB (1e9) so the total matches the file size Hugging Face reports, not the smaller base-1024 GiB.
const loadedGb = prog.bytes_loaded / 1e9;
const totalGb = prog.bytes_total / 1e9;
const pct = Math.min(99, Math.round(prog.fraction * 100));
@@ -2540,9 +2441,8 @@ export function useChatModelRuntime() {
formatEta(est.eta) !== "--" ? ` • ${formatEta(est.eta)} left` : ""
}`
: base;
- // Inline-status-only state (see pollDownload): while the toast is
- // up, skip the state write so the chat page doesn't re-render every
- // poll during "Starting model" — the desktop WebView2 typing-lag fix.
+ // Inline-status-only state (see pollDownload): while the toast is up, skip the state write so
+ // the chat page does not re-render every poll.
if (loadToastDismissedRef.current) {
setLoadProgress({
percent: pct,
@@ -2583,8 +2483,8 @@ export function useChatModelRuntime() {
await performLoad();
// User cancelled mid-refresh; cancelLoading handles teardown.
if (abortCtrl.signal.aborted) return;
- // Same composition as the auto-load path, through the same helper, so the
- // two cannot describe an identical failure differently again.
+ // Same composition as the auto-load path, through the same helper, so the two cannot describe an
+ // identical failure differently.
const notice = loadFallbackNotice(
`${toastDisplayName} loaded`,
cpuFallbackReason,
@@ -2684,15 +2584,14 @@ export function useChatModelRuntime() {
}
let lifecycleLease: ModelLifecycleLease | null = null;
try {
- // Block queue materialization before taking the confirmation snapshot.
- // Otherwise a queue can appear while the dialog is open and be stopped
- // by the unload even though the user never confirmed stopping it.
+ // Block queue materialization before taking the confirmation snapshot, or a queue can appear
+ // while the dialog is open and be stopped without the user confirming it.
lifecycleLease = useChatRuntimeStore.getState().beginModelLoading();
if (lifecycleLease === null) {
return false;
}
- // Ejecting tears down llama-server, so every chat stops. Same prompt, but it
- // leaves no model loaded, so it must not be worded as a reload.
+ // Ejecting tears down llama-server, so every chat stops. Same prompt, but it leaves no model
+ // loaded, so it must not be worded as a reload.
const stopDecision = await confirmStopRunningChatsIfNeeded(
"Unloading the model",
"unload",
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-projects.ts b/studio/frontend/src/features/chat/hooks/use-chat-projects.ts
index fe93ec4249c..cfac0276248 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-projects.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-projects.ts
@@ -106,8 +106,8 @@ export function useChatProjects(): {
lastProjectsUpdateEvent = event;
void refresh(true, followUpIfPending);
};
- // Cached rows render immediately, then one shared request reconciles
- // changes made by another browser tab or API client.
+ // Cached rows render immediately, then one shared request reconciles changes made by another
+ // browser tab or API client.
void refresh(projectsLoaded);
window.addEventListener(CHAT_PROJECTS_UPDATED_EVENT, onProjectsUpdated);
return () => {
@@ -144,9 +144,9 @@ export async function deleteChatProject(
args: { deleteFiles?: boolean } = {},
): Promise<void> {
const kept = await deleteStoredChatProject(projectId, args);
- // The member chats went with the project, so their own sandboxes are
- // reachable from nothing: the same offer an ordinary chat delete makes, and
- // a sandbox the backend could not remove is kept even when asked to go.
+ // The member chats went with the project, so their own sandboxes are reachable from nothing: the
+ // same offer an ordinary chat delete makes, and a sandbox the backend could not remove is kept
+ // even when asked to go.
offerToDeleteKeptSandboxes(kept);
}
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-search-index.ts b/studio/frontend/src/features/chat/hooks/use-chat-search-index.ts
index fedace70fd3..8b1e4e008aa 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-search-index.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-search-index.ts
@@ -36,8 +36,8 @@ export interface ChatSearchItem {
title: string;
// Lowercased title + user messages only (short); searched first.
userSearchText: string;
- // Lowercased title + every message (incl. tool calls); fallback when user
- // text matches nothing. Prebuilt so filtering never re-lowercases per keystroke.
+ // Lowercased title plus every message; the fallback when user text matches nothing.
+ // Prebuilt so filtering never re-lowercases per keystroke.
searchText: string;
createdAt: number;
projectId?: string | null;
@@ -51,9 +51,8 @@ const ROW_RELEASE_DELAY_MS = 300;
// Keys whose values are base64 image/audio payloads, not searchable text.
const BINARY_KEY = /b64|base64|^(images?|audio|video)$/i;
-// Drop a trailing __MCP_IMAGES__ envelope only when it is the valid JSON image
-// array appended by the backend, so legit tool text that merely mentions the
-// marker stays searchable. (base64 runs below are scrubbed regardless.)
+// Drop a trailing __MCP_IMAGES__ envelope only when it is the valid JSON image array the
+// backend appended, so tool text merely mentioning the marker stays searchable.
function stripMcpImageSuffix(value: string): string {
const marker = "\n__MCP_IMAGES__:";
const idx = value.lastIndexOf(marker);
@@ -79,9 +78,8 @@ function stripMcpImageSuffix(value: string): string {
return value;
}
-// Readable text from tool args/results, dropping base64 image/audio blobs so
-// they never bloat the index (object fields by key, plus data URLs / long
-// base64 runs and the "__IMAGES__" suffix inside strings).
+// Readable text from tool args/results, dropping base64 image/audio blobs so they never
+// bloat the index.
function searchableText(value: unknown, depth = 0): string {
if (typeof value === "string") {
let text = stripMcpImageSuffix(value);
@@ -105,8 +103,8 @@ function searchableText(value: unknown, depth = 0): string {
return "";
}
-// Pull searchable text from a message: plain text, reasoning/thinking, tool
-// calls (name + args + result), cited sources (title + url) and pasted bodies.
+// Pull searchable text from a message: text, reasoning, tool calls, cited sources and
+// pasted bodies.
function extractText(message: MessageRecord): string {
const content = message.content;
const pasted = attachmentsPastedText(message.attachments);
@@ -155,7 +153,7 @@ interface ChatSearchIndexBuild {
complete: boolean;
}
-// Exported for the bare-node cache harness: it needs to prove that a failed read is not
+// Exported for the bare-node cache harness: it must prove a failed read is not
// indistinguishable from a completed empty history.
export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
const active = (
@@ -211,9 +209,8 @@ export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
);
let complete = true;
- // Legacy-only chats can exist before server-side history import finishes.
- // Fill only the missing ids via the legacy path instead of one request per
- // thread up front.
+ // Legacy-only chats can exist before server-side history import finishes. Fill only the
+ // missing ids via the legacy path instead of one request per thread up front.
const missingThreadIds = allThreadIds.filter(
(threadId) => !messagesByThread.has(threadId),
);
@@ -248,8 +245,8 @@ export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
}
merged.sort((a, b) => b.createdAt - a.createdAt);
- // Two tiers: user messages (short, searched first) and the full
- // conversation incl. tool calls (fallback when user text matches nothing).
+ // Two tiers: user messages (short, searched first) and the full conversation incl. tool
+ // calls, used when user text matches nothing.
const userParts: string[] = [item.title];
const allParts: string[] = [item.title];
for (const m of merged) {
@@ -271,8 +268,8 @@ export async function buildChatSearchIndex(): Promise<ChatSearchIndexBuild> {
// megabytes behind a closed dialog. Past this the index is rebuilt on each open.
const MAX_CACHED_SEARCH_TEXT_CHARS = 4_000_000;
-// Last built index, kept across opens so reopening paints the previous rows at
-// once and revalidates in place instead of collapsing back to the empty state.
+// Last built index, kept across opens so reopening paints the previous rows at once and
+// revalidates in place instead of collapsing to the empty state.
let cachedIndex: ChatSearchItem[] | null = null;
let cachedIndexEpoch = -1;
@@ -280,8 +277,8 @@ let cachedIndexEpoch = -1;
// open onto the previous user's chats.
function readCachedIndex(): ChatSearchItem[] | null {
if (cachedIndexEpoch !== getAuthSessionEpoch()) {
- // The next account's history is unknown, so the previous one's hint cannot size it
- // either. -1 is "nothing cached yet", every page load, and leaves this account's alone.
+ // The next account's history is unknown, so the previous one's hint cannot size it. -1 is
+ // "nothing cached yet" and leaves this account's alone.
if (cachedIndexEpoch !== -1) forgetChatSearchHasRows();
cachedIndex = null;
cachedIndexEpoch = getAuthSessionEpoch();
@@ -302,15 +299,15 @@ export function writeCachedIndex(next: ChatSearchItem[] | null): void {
next !== null && cachedSearchTextChars(next) > MAX_CACHED_SEARCH_TEXT_CHARS
? null
: next;
- // A build answers outright. An invalidation only says the history changed: a remembered
- // ROWS answer still holds, an EMPTY one may be about to gain its first chat.
+ // A build answers outright. An invalidation only says the history changed: a remembered ROWS
+ // answer still holds, an EMPTY one may be about to gain its first chat.
if (next !== null) rememberChatSearchHasRows(next.length > 0);
else if (chatSearchHadRows() === false) forgetChatSearchHasRows();
}
-// A partial build is useful for this open, but it is not an answer about whether the history
-// has rows. In particular, recording [] after every message read failed would make the next
-// page load open compact and then grow mid-animation once connectivity recovered.
+// A partial build is useful for this open but is not an answer about whether the history has
+// rows. Recording [] after every message read failed would open the next page load
+// compact and then grow mid-animation once connectivity recovered.
export function publishChatSearchBuild(
build: ChatSearchIndexBuild,
): ChatSearchItem[] {
@@ -319,18 +316,17 @@ export function publishChatSearchBuild(
} else {
cachedIndexEpoch = getAuthSessionEpoch();
cachedIndex = null;
- // An incomplete build cannot preserve a previous empty answer: partial rows already
- // disprove it, while no rows only mean the history could not be read. A positive partial
- // answer is safe and keeps the next open from starting compact again.
+ // An incomplete build cannot preserve a previous empty answer: partial rows already disprove
+ // it, while no rows only mean the history could not be read. A positive partial answer is
+ // safe and keeps the next open from starting compact.
if (build.items.length > 0) rememberChatSearchHasRows(true);
else forgetChatSearchHasRows();
}
return build.items;
}
-// Once a structural refresh has a deadline, stream chunks may join that refresh but must not
-// keep moving it. If the deadline already fired (no timer remains), a later chunk schedules
-// the usual follow-up so the in-flight snapshot is eventually refreshed.
+// Once a structural refresh has a deadline, stream chunks may join it but must not keep
+// moving it. If the deadline already fired, a later chunk schedules the usual follow-up.
export function shouldPostponeSearchRebuild(
structuralRebuildPending: boolean,
event: Event,
@@ -343,12 +339,12 @@ export function shouldPostponeSearchRebuild(
const SEARCH_SESSION_CHANGED_EVENT = "unsloth-chat-search-session-changed";
// A history change in another tab or from an API client never reaches this document, so the
-// cache would otherwise open onto rows that no longer exist and navigate to a dead thread.
+// cache would otherwise open onto rows that no longer exist.
if (typeof window !== "undefined") {
window.addEventListener("storage", (event) => {
- // An account switch elsewhere arrives as a storage write alone: the epoch and its events
- // are both this document's. The mark moves on a session boundary and not on an hourly
- // refresh, which must not cost a warm cache.
+ // An account switch elsewhere arrives as a storage write alone: the epoch and its events are
+ // both this document's. The mark moves on a session boundary, not on an hourly refresh,
+ // which must not cost a warm cache.
if (
event.key === AUTH_SESSION_MARK_KEY ||
(event.key === AUTH_TOKEN_KEY && event.newValue === null)
@@ -356,16 +352,16 @@ if (typeof window !== "undefined") {
writeCachedIndex(null);
// Shared by every account on the origin, so it cannot answer for the new one.
forgetChatSearchHasRows();
- // History first, for the sidebar. Then the account change, which the epoch check
- // cannot see: an open dialog restarts at once, superseding the rebuild just queued.
+ // History first, for the sidebar. Then the account change, which the epoch check cannot see:
+ // an open dialog restarts at once, superseding the rebuild just queued.
window.dispatchEvent(new Event(CHAT_HISTORY_UPDATED_EVENT));
window.dispatchEvent(new Event(SEARCH_SESSION_CHANGED_EVENT));
return;
}
if (event.key !== CHAT_HISTORY_REVISION_KEY) return;
writeCachedIndex(null);
- // Dropping the cache alone leaves an open dialog on pre-change rows with nothing
- // scheduled. Re-raised locally so every in-tab listener treats it as a local change.
+ // Dropping the cache alone leaves an open dialog on pre-change rows with nothing scheduled.
+ // Re-raised locally so every in-tab listener treats it as a local change.
window.dispatchEvent(new Event(CHAT_HISTORY_UPDATED_EVENT));
});
// The hint outlives the page, so a logout takes it: otherwise the next account to sign in
@@ -376,8 +372,8 @@ if (typeof window !== "undefined") {
}
// Whether to size for rows, readable during render so the dialog picks a height before its
-// opening paint. A built cache answers exactly; otherwise the last build's hint does, which
-// is all the first open of a page load has. null means genuinely unknown.
+// opening paint. A built cache answers exactly; otherwise the last build's hint does.
+// null means genuinely unknown.
export function chatSearchIndexHasRows(): boolean | null {
const cached = readCachedIndex();
if (cached !== null) return cached.length > 0;
@@ -408,13 +404,13 @@ export function useChatSearchIndex(enabled: boolean): {
useEffect(() => {
if (!enabled) {
setLoading(false);
- // With nothing cached these rows are the last thing holding the conversation text.
- // Released after the exit, not during the closing render: the portal stays mounted for
- // the animation, and emptying it there is the teardown this dialog exists to avoid.
+ // With nothing cached these rows are the last thing holding the conversation text. Released
+ // after the exit, not during the closing render: the portal stays mounted for the
+ // animation, and emptying it there is the teardown this dialog exists to avoid.
let release: ReturnType<typeof setTimeout> | null = null;
const scheduleRelease = () => {
- // Never postponed: a stream invalidates per chunk, and restarting would hold the
- // index for the whole generation.
+ // Never postponed: a stream invalidates per chunk, and restarting would hold the index for
+ // the whole generation.
if (release !== null) return;
release = setTimeout(() => {
release = null;
@@ -424,8 +420,8 @@ export function useChatSearchIndex(enabled: boolean): {
}, ROW_RELEASE_DELAY_MS);
};
scheduleRelease();
- // History can change while closed, so drop the cache rather than reopening onto chats
- // that no longer exist. Only the cache: clearing state re-renders per streaming chunk.
+ // History can change while closed, so drop the cache rather than reopening onto chats that
+ // no longer exist. Only the cache: clearing state re-renders per streaming chunk.
const invalidate = () => {
writeCachedIndex(null);
// The release above may already have run while the cache was still there.
@@ -439,8 +435,7 @@ export function useChatSearchIndex(enabled: boolean): {
}
let cancelled = false;
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
- // Set by a history event, cleared once its rebuild lands, so a close in between knows
- // the cached snapshot is stale.
+ // Set by a history event, cleared once its rebuild lands, so a close in between knows the cached snapshot is stale.
let rebuildPending = false;
let structuralRebuildPending = false;
@@ -456,8 +451,7 @@ export function useChatSearchIndex(enabled: boolean): {
if (cancelled || seq !== requestSeqRef.current) return;
if (epoch !== getAuthSessionEpoch()) return;
const result = publishChatSearchBuild(build);
- // A build older than the history event does not satisfy it, so the flag only
- // clears once nothing is queued.
+ // A build older than the history event does not satisfy it, so the flag only clears once nothing is queued.
if (debounceTimer === null) {
rebuildPending = false;
structuralRebuildPending = false;
@@ -466,9 +460,8 @@ export function useChatSearchIndex(enabled: boolean): {
})
.catch(() => {
if (cancelled || seq !== requestSeqRef.current) return;
- // A rebuild that failed leaves nothing fresher, and what is cached is the snapshot
- // it was called to replace: keeping it would offer a deleted chat as a live result
- // for as long as the dialog stays open.
+ // A rebuild that failed leaves nothing fresher, and what is cached is the snapshot it was
+ // called to replace: keeping it would offer a deleted chat as a live result.
if (rebuildPending) writeCachedIndex(null);
setItems(readCachedIndex() ?? []);
})
@@ -495,16 +488,15 @@ export function useChatSearchIndex(enabled: boolean): {
if (debounceTimer !== null) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
debounceTimer = null;
- // The structural deadline has now been honored. Stream events arriving while this
- // rebuild runs should return to quiet-window coalescing instead of forcing another
- // rebuild every debounce interval for the rest of the generation.
+ // The structural deadline has been honored. Stream events arriving while this rebuild runs
+ // return to quiet-window coalescing instead of forcing a rebuild every debounce interval.
structuralRebuildPending = false;
if (!cancelled) run();
}, SEARCH_REBUILD_DEBOUNCE_MS);
};
- // Not a history change: the rows on screen belong to whoever was signed in a moment ago,
- // so they go now. Rebuilding at once also advances the request sequence, retiring a build
+ // Not a history change: the rows on screen belong to whoever was signed in a moment ago, so
+ // they go now. Rebuilding at once also advances the request sequence, retiring a build
// still in flight for that account.
const onSessionChanged = () => {
if (cancelled) return;
@@ -522,9 +514,8 @@ export function useChatSearchIndex(enabled: boolean): {
return () => {
cancelled = true;
if (debounceTimer !== null) clearTimeout(debounceTimer);
- // Closing cancels a queued rebuild, so the snapshot left behind is stale and must not
- // survive the next open. The rendered rows stay: clearing them here tears the list down
- // inside the exit animation.
+ // Closing cancels a queued rebuild, so the snapshot left behind is stale and must not survive
+ // the next open. The rendered rows stay: clearing them tears the list down inside the exit.
if (rebuildPending) writeCachedIndex(null);
window.removeEventListener(CHAT_HISTORY_UPDATED_EVENT, scheduleRebuild);
window.removeEventListener(
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts b/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
index c7a0a3d3875..b4da8732b7f 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
@@ -50,11 +50,9 @@ export function groupThreads(
const pairItems = new Map<string, SidebarItem>();
for (const t of threads) {
- // Coerce archived to a boolean before comparing. Legacy threads (from the
- // older browser-only Unsloth, or any record predating the archived field)
- // can have archived === undefined or null; a raw `!== archived` comparison
- // would drop those from BOTH the Recents (archived=false) and Archived
- // (archived=true) lists, hiding existing chats. Treat missing as false.
+ // Coerce archived to a boolean before comparing. Legacy threads can have archived undefined or
+ // null, and a raw `!== archived` comparison would drop those from BOTH Recents and Archived,
+ // hiding existing chats. Treat missing as false.
if (Boolean(t.archived) !== archived) {
continue;
}
@@ -94,8 +92,8 @@ export function groupThreads(
return items.sort((a, b) => b.updatedAt - a.updatedAt);
}
-// Streaming fires CHAT_HISTORY_UPDATED_EVENT per chunk. Debounce so each quiet
-// window produces at most one O(N) fetch; requestSeq discards stale responses.
+// Streaming fires CHAT_HISTORY_UPDATED_EVENT per chunk. Debounce so each quiet window produces
+// at most one O(N) fetch; requestSeq discards stale responses.
const SIDEBAR_REFRESH_DEBOUNCE_MS = 300;
export function useChatSidebarItems(options?: {
@@ -122,19 +120,19 @@ export function useChatSidebarItems(options?: {
const listThreads = requireMessages
? listStoredChatThreadsWithMessages
: listStoredChatThreads;
- // includeArchived: archived threads are filtered out of Recents by
- // groupThreads, but the hook still needs them for archivedItems.
+ // includeArchived: archived threads are filtered out of Recents by groupThreads, but the hook
+ // still needs them for archivedItems.
const threads = await listThreads({
includeArchived: true,
projectId: options?.projectId,
});
- // Discard the response if a newer request was scheduled while we
- // were in flight, or if the effect was torn down.
+ // Discard the response if a newer request was scheduled while we were in flight, or if the
+ // effect was torn down.
if (cancelled || seq !== requestSeq) return;
setAllThreads(threads);
setLoaded(true);
- // Pre-cut legacy titles cannot grow with the sidebar. The repair reads
- // its own messages, as late as it can, not this list's.
+ // Pre-cut legacy titles cannot grow with the sidebar. The repair reads its own messages, as
+ // late as it can, not this list's.
void repairLegacyChatTitles(threads).catch(() => undefined);
} catch (error) {
if (isExpectedBackgroundChatStorageError(error)) {
@@ -153,8 +151,7 @@ export function useChatSidebarItems(options?: {
}, SIDEBAR_REFRESH_DEBOUNCE_MS);
}
- // Initial load fires immediately (no debounce) so the sidebar isn't
- // blank for 300ms on mount.
+ // Initial load fires immediately (no debounce) so the sidebar isn't blank for 300ms on mount.
requestSeq += 1;
void doLoad(requestSeq);
window.addEventListener(CHAT_HISTORY_UPDATED_EVENT, load);
@@ -165,11 +162,10 @@ export function useChatSidebarItems(options?: {
};
}, [enabled, options?.projectId, requireMessages]);
- // Memoised for identity as much as for the work. These arrays are the root
- // of every derived sidebar list, so rebuilding them on each render leaves
- // each of those with a new identity too, and an effect that depends on one
- // re-runs every render. Where such an effect also sets state, React
- // re-renders once to find the bail-out, rebuilds these, and never settles.
+ // Memoised for identity as much as for the work. These arrays are the root of every derived
+ // sidebar list, so rebuilding them per render gives each a new identity and an effect
+ // depending on one re-runs every render. Where such an effect sets state, React re-renders
+ // to find the bail-out, rebuilds these, and never settles.
const items = useMemo(() => groupThreads(allThreads ?? []), [allThreads]);
const archivedItems = useMemo(
() => groupThreads(allThreads ?? [], true),
@@ -181,8 +177,8 @@ export function useChatSidebarItems(options?: {
}
function cancelIfRunning(threadId: string): void {
- // Reaches a background thread, which cancelByThreadId cannot: a deleted chat must stop,
- // or the run keeps writing to a conversation that is gone.
+ // Reaches a background thread, which cancelByThreadId cannot: a deleted chat must stop, or the
+ // run keeps writing to a conversation that is gone.
stopChatThread(threadId);
}
@@ -264,8 +260,8 @@ export async function archiveAllChatItems(
onSelect?: (view: { mode: "single"; newThreadNonce: string }) => void,
): Promise<number> {
const threads = await listStoredChatThreads({ includeArchived: true });
- // Boolean() mirrors groupThreads: legacy records may have archived
- // undefined/null, which must count as "not archived".
+ // Boolean() mirrors groupThreads: legacy records may have archived undefined or null, which
+ // must count as "not archived".
const toArchive = threads.filter((t) => !t.archived);
if (toArchive.length === 0) return 0;
@@ -282,15 +278,14 @@ export async function archiveAllChatItems(
(write): write is PromiseRejectedResult => write.status === "rejected",
);
if (failure) {
- // Silent updates mean a partial batch announces itself nowhere, so whatever did
- // archive would stay listed here and in every other tab until some later change.
+ // Silent updates mean a partial batch announces itself nowhere, so whatever did archive would
+ // stay listed here and in every other tab until some later change.
notifyChatHistoryUpdated();
throw failure.reason;
}
- // Reset only when this action archived the active single thread or compare
- // pair. An already-archived chat opened from the archive is not in
- // toArchive and must stay open.
+ // Reset only when this action archived the active single thread or compare pair. An
+ // already-archived chat opened from the archive is not in toArchive and must stay open.
const archivedActive =
activeId !== undefined &&
toArchive.some(
@@ -357,9 +352,9 @@ export async function deleteChatItems(
try {
const kept = await deleteStoredChatThreads(threadIds, args);
- // Whether or not deletion was asked for: a sandbox that could not be
- // removed leaves files with no card left to reach them from, and the chat
- // is already gone, so this offer is the only notice and the only retry.
+ // Whether or not deletion was asked for: a sandbox that could not be removed leaves files with
+ // no card to reach them from, and the chat is already gone, so this offer is the only notice
+ // and the only retry.
offerToDeleteKeptSandboxes(kept);
} catch (error) {
removeChatThreadTombstones(threadIds);
diff --git a/studio/frontend/src/features/chat/hooks/use-pill-activation-order.ts b/studio/frontend/src/features/chat/hooks/use-pill-activation-order.ts
index 76987a86d34..c8c59a7896d 100644
--- a/studio/frontend/src/features/chat/hooks/use-pill-activation-order.ts
+++ b/studio/frontend/src/features/chat/hooks/use-pill-activation-order.ts
@@ -3,8 +3,8 @@
import { useEffect, useState } from "react";
-// Tracks which keys are active and their activation order, so opt-in
-// composer pills (Canvas, MCP) render in toggle-on order, not a fixed one.
+// Tracks which keys are active and their activation order, so opt-in composer pills (Canvas, MCP)
+// render in toggle-on order, not a fixed one.
export function usePillActivationOrder(states: Record<string, boolean>): string[] {
const [order, setOrder] = useState<string[]>(() =>
Object.keys(states).filter((key) => states[key]),
diff --git a/studio/frontend/src/features/chat/hooks/use-rag-tool-disabled.ts b/studio/frontend/src/features/chat/hooks/use-rag-tool-disabled.ts
index 5aacd99412e..6efae232d2b 100644
--- a/studio/frontend/src/features/chat/hooks/use-rag-tool-disabled.ts
+++ b/studio/frontend/src/features/chat/hooks/use-rag-tool-disabled.ts
@@ -8,9 +8,9 @@ import {
import { useExternalProvidersStore } from "../stores/external-providers-store";
import { useChatRuntimeStore } from "../stores/chat-runtime-store";
-// Pre-select gate for the RAG toggle, mirroring Web search/Code/MCP: armable
-// with no model; disabled only when a loaded model can't run
-// search_knowledge_base. The send path checks supportsTools independently.
+// Pre-select gate for the RAG toggle, mirroring Web search / Code / MCP: armable with no model,
+// disabled only when a loaded model cannot run search_knowledge_base. The send path checks
+// supportsTools independently.
export function useRagToolDisabled(): boolean {
const modelLoaded = useChatRuntimeStore(
(s) => !!s.params.checkpoint && !s.modelLoading,
diff --git a/studio/frontend/src/features/chat/hooks/use-transfer-stats.ts b/studio/frontend/src/features/chat/hooks/use-transfer-stats.ts
index 2a0e9f0a856..25a7066d63f 100644
--- a/studio/frontend/src/features/chat/hooks/use-transfer-stats.ts
+++ b/studio/frontend/src/features/chat/hooks/use-transfer-stats.ts
@@ -1,19 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Compute rate (bytes/sec) and ETA (seconds) from a time-series of cumulative
- * ``bytes`` values, using a rolling window of recent samples.
- *
- * Shared by the chat-flow download toast, training-start overlay, and
- * model-load phase UI: all three are a counter rising monotonically from 0
- * toward ``totalBytes``, polled on an interval, regardless of HTTP download or
- * mmap page-in.
- *
- * Stability: ``stable`` stays ``false`` until at least 3 samples spanning >=3s,
- * so the UI doesn't flash wild rates during the first tick or two when the
- * denominator is ~0.
- */
+/** Compute rate (bytes/sec) and ETA (seconds) from a time-series of cumulative `bytes` values,
+ * using a rolling window of recent samples. Shared by the chat-flow download toast,
+ * training-start overlay and model-load phase UI: all three are a counter rising monotonically
+ * toward `totalBytes`, polled on an interval. `stable` stays false until at least 3 samples
+ * spanning >=3s, so the UI does not flash wild rates while the denominator is ~0. */
import { useEffect, useRef, useState } from "react";
@@ -46,11 +38,10 @@ export function useTransferStats(
: 0;
if (typeof document !== "undefined" && document.hidden) {
- // Callers poll on an interval, and a hidden tab's is clamped to about
- // once a minute, so these gaps time the poller and the estimator would
- // read them as the burst cadence. Publishing nothing also stops a stale
- // reading outliving the transfer: this effect is keyed on ``bytes``, so a
- // counter that stops moving never runs it again to correct itself.
+ // Callers poll on an interval, and a hidden tab's is clamped to about once a minute, so these gaps
+ // time the poller and the estimator would read them as the burst cadence. Publishing nothing also
+ // stops a stale reading outliving the transfer: this effect is keyed on `bytes`, so a counter
+ // that stops moving never runs it again to correct itself.
samplesRef.current.length = 0;
setState({ rateBytesPerSecond: 0, etaSeconds: 0, stable: false });
return;
diff --git a/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts b/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts
index d2093d9aaa4..441812d4184 100644
--- a/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts
+++ b/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts
@@ -47,9 +47,8 @@ function sameArray<T>(a: T[] | null, b: T[] | null): boolean {
return JSON.stringify(a) === JSON.stringify(b);
}
-// Canonicalises backend / persisted speculative mode values onto the UI
-// modes. Re-exported from the store, which owns the vocabulary: a second
-// copy meant every new mode had to be added twice or the two would disagree.
+// Canonicalises backend / persisted speculative mode values onto the UI modes. Re-exported
+// from the store, which owns the vocabulary: a second copy would drift.
export { normalizeSpeculativeType } from "../stores/chat-runtime-store";
export function clampLocalReasoningEffort(
@@ -61,15 +60,9 @@ export function clampLocalReasoningEffort(
return "low";
}
-/**
- * Reasoning capability fields derived from a model load/status response.
- *
- * Centralises the effort-levels + can-disable derivation so every load path
- * (main load, status sync, shared/Compare composer, first-chat auto-load) agrees:
- * a hybrid GLM-style `enable_thinking_effort` model keeps its high|max|Off
- * controls no matter which path loaded it, instead of falling back to the
- * default low|medium|high and losing Max/Off.
- */
+/** Reasoning capability fields derived from a load/status response. Centralised so every
+ * load path agrees: a hybrid `enable_thinking_effort` model keeps its high|max|Off
+ * controls instead of falling back to low|medium|high and losing Max/Off. */
export function reasoningCapsFromLoad(resp: {
reasoning_style?: ReasoningStyle | null;
reasoning_effort_levels?: string[] | null;
@@ -84,8 +77,8 @@ export function reasoningCapsFromLoad(resp: {
resp.reasoning_effort_levels && resp.reasoning_effort_levels.length > 0
? (resp.reasoning_effort_levels as ReasoningEffort[])
: (["low", "medium", "high"] as const);
- // enable_thinking and enable_thinking_effort can both be turned off; only the
- // pure gpt-oss-style reasoning_effort is always-on.
+ // enable_thinking and enable_thinking_effort can both be turned off; only the pure
+ // gpt-oss-style reasoning_effort is always-on.
return {
reasoningStyle,
reasoningEffortLevels,
@@ -126,8 +119,8 @@ function ensureActiveModelInStoreList(
}
const summary: ChatModelRow = {
id: checkpointId,
- // active_model is already the clean public id; its leaf matches the catalog rows,
- // and the fallback keeps a snapshot path out of the trigger.
+ // active_model is already the clean public id; its leaf matches the catalog rows, and the
+ // fallback keeps a snapshot path out of the trigger.
name: modelDisplayName(status.active_model ?? checkpointId),
isVision: status.is_vision ?? false,
isLora: false,
@@ -139,9 +132,8 @@ function ensureActiveModelInStoreList(
export type ApplyInferenceStatusOptions = {
previousCheckpoint?: string;
- /** activeGgufVariant BEFORE the caller's setCheckpoint synced it to the
- * status -- without it a variant-only switch underneath the tab reads as
- * steady state and the hydration reseed keeps the old quant's baselines. */
+ /** activeGgufVariant BEFORE the caller's setCheckpoint synced it: without it a variant-only
+ * switch reads as steady state and the hydration reseed keeps the old quant's baselines. */
previousGgufVariant?: string | null;
/** Seed settings while the caller holds the model-loading lease. */
seedLoadParams?: boolean;
@@ -158,9 +150,8 @@ export function applyActiveModelStatusToStore(
const checkpointId = resolveInferenceCheckpointId(status);
if (!checkpointId) return;
- // Only reached with a model actually active, so this is the one place both
- // the status poll and the readopt path can publish residency from. Without
- // it a load would look unloaded until the next poll, up to 10s later.
+ // Only reached with a model active, so this is the one place both the status poll and the
+ // readopt path can publish residency from. Without it a load looks unloaded for up to 10s.
useChatRuntimeStore.setState({ residentCheckpoint: checkpointId });
const store = useChatRuntimeStore.getState();
@@ -176,10 +167,9 @@ export function applyActiveModelStatusToStore(
presetSource: store.activePresetSource,
loadedContextLength: loadedContextFields(status).loadedContextLength,
}),
- // The model's own remembered settings outrank the recommendation, or
- // every poll would undo them, but not past the context it loaded with.
- // context_length is reported for a safetensors load too, so this is not
- // narrowed to GGUF; absent, there is nothing to cap against.
+ // The model's remembered settings outrank the recommendation, or every poll would undo them,
+ // but not past the context it loaded with. context_length is reported for safetensors too,
+ // so this is not narrowed to GGUF; absent, there is nothing to cap against.
{
fromModelDefaults: true,
maxTokensCap: replayMaxTokensCap(status.context_length),
@@ -199,8 +189,9 @@ export function applyActiveModelStatusToStore(
const supportsReasoning = status.supports_reasoning ?? false;
const reasoningAlwaysOn = status.reasoning_always_on ?? false;
const reasoningStyle = status.reasoning_style ?? "enable_thinking";
- // GLM-5.2-style models report their own effort levels (e.g. high|max);
- // everything else keeps the default low/medium/high.
+ // GLM-5.2-style models report their own effort levels; everything else keeps the default
+ // low/medium/high.
+ // They report high|max.
const reasoningEffortLevels =
status.reasoning_effort_levels && status.reasoning_effort_levels.length > 0
? (status.reasoning_effort_levels as ReasoningEffort[])
@@ -278,10 +269,8 @@ export function applyActiveModelStatusToStore(
seedLoadParams,
modelChanged: slotsModelChanged,
});
- // The llama-server tuning group follows the same rule, and resolveBatchSizeSeed
- // is generic in the value type for exactly this: each one is an echo of what the
- // load REQUESTED, which is what makes the steady-poll and dirty-control cases
- // identical to the batch pair's.
+ // The llama-server tuning group follows the same rule, and resolveBatchSizeSeed is generic
+ // in the value type for exactly this: each is an echo of what the load REQUESTED.
const loadModeSeed = resolveBatchSizeSeed<string>({
incoming: status.requested_load_mode,
isGguf: status.is_gguf ?? true,
@@ -316,14 +305,10 @@ export function applyActiveModelStatusToStore(
seedLoadParams,
modelChanged: slotsModelChanged,
});
- // A load sends its context pin as max_seq_length and status only exposes the
- // resolved context plus the requested n_ctx, and a positive requested n_ctx
- // does NOT mean a human asked for it: an Auto same-model reload under a custom
- // preset reports one too. So the pin is re-seeded here from what this tab (or
- // the model's saved config) actually recorded, never inferred from the echo
- // alone, and the echo is trusted only where it is unambiguous. See
- // resolveCtxPinSeed for the full rule, including the mid-load window where
- // status still answers for the OUTGOING model.
+ // A load sends its context pin as max_seq_length while status exposes the resolved context
+ // plus the requested n_ctx, and a positive requested n_ctx does NOT mean a human asked
+ // for it. So the pin is re-seeded from what this tab or the saved config recorded, never
+ // inferred from the echo alone. See resolveCtxPinSeed for the full rule.
const ctxPinFields = resolveCtxPinSeed({
incoming: status.requested_context_length,
// MLX reports a requested context as well, so the rule below is about any
@@ -366,8 +351,8 @@ export function applyActiveModelStatusToStore(
},
{ ids: incomingGpuIds, indexKind: incomingGpuIndexKind },
) ||
- // Only a pin this status will actually move counts: a difference it declines
- // to apply (mid-load, or an echo it cannot read intent out of) is not one.
+ // Only a pin this status will actually move counts: a difference it declines to apply
+ // (mid-load, or an unreadable echo) is not one.
(ctxPinFields.loadedCustomContextLength !== undefined &&
prevState.loadedCustomContextLength !==
ctxPinFields.loadedCustomContextLength);
@@ -440,13 +425,10 @@ export function applyActiveModelStatusToStore(
specFallbackReason: status.spec_fallback_reason ?? null,
mmprojFallbackReason: status.mmproj_fallback_reason ?? null,
specDrafterKind: status.spec_drafter_kind ?? null,
- // The spec / KV seeds share the GPU-fields reseed mechanism below: a
- // non-GGUF status leaves their loaded baselines null, so the "unseeded"
- // guard re-fires every refresh -- hold them too while a staged pick's
- // settings are being edited, or the refresh resets the staged edit.
- // hydratingExistingModel reopens every load-param seed: when the active
- // model changed underneath this tab (auto-switch, another client), the
- // old model's baselines are stale and must adopt the new status.
+ // The spec / KV seeds share the GPU-fields reseed below: a non-GGUF status leaves their
+ // baselines null so the "unseeded" guard re-fires every refresh -- hold them too while a
+ // staged pick is being edited. hydratingExistingModel reopens every seed, since after an
+ // auto-switch the old model's baselines are stale.
...(seedLoadParams &&
(prevState.loadedSpeculativeType === null || hydratingExistingModel) && {
speculativeType: currentSpecType,
@@ -472,16 +454,10 @@ export function applyActiveModelStatusToStore(
tensorParallel: status.tensor_parallel,
loadedTensorParallel: status.tensor_parallel,
}),
- // A load knob like tensorParallel above. Without a reseed a tab that never
- // performed the load shows Vision ON over a model running with its projector
- // off, and the next Reload silently puts the projector back. Seeded from
- // disable_vision -- the request the load ran with -- not
- // vision_disabled_by_user, which is also gated on the model HAVING a projector
- // and so cannot round-trip a text-only GGUF.
- //
- // Unlike tensorParallel the guard is not just "unseeded": the baseline below is
- // unguarded, so an external reload of the same model would move it and the image
- // gate while leaving this control behind. See shouldSeedVisionSwitch.
+ // A load knob like tensorParallel above. Without a reseed a tab that never performed the
+ // load shows Vision ON over a projector-off server and the next Reload puts it back.
+ // Seeded from disable_vision, the request the load ran with, not vision_disabled_by_user,
+ // which cannot round-trip a text-only GGUF. See shouldSeedVisionSwitch.
...(seedLoadParams &&
status.disable_vision !== undefined &&
shouldSeedVisionSwitch({
@@ -491,21 +467,20 @@ export function applyActiveModelStatusToStore(
}) && {
disableVision: status.disable_vision,
}),
- // The rollback baseline, and unguarded like the mirror below rather than
- // seeded once: it has to track the RUNNING server, or a switch that fails
- // after a poll restores whatever the last seed happened to see.
+ // The rollback baseline, unguarded like the mirror below rather than seeded once: it must
+ // track the RUNNING server, or a switch failing after a poll restores a stale seed.
...(seedLoadParams &&
status.disable_vision !== undefined && {
loadedDisableVision: status.disable_vision,
}),
- // Unguarded, unlike the seed above: this mirrors the live load for the
- // composer's image gate, not a user setting, so every poll must land.
+ // Unguarded, unlike the seed above: this mirrors the live load for the composer's image
+ // gate, not a user setting, so every poll must land.
...(seedLoadParams &&
status.vision_disabled_by_user !== undefined && {
loadedVisionDisabledByUser: status.vision_disabled_by_user,
}),
- // Hydration only, so a steady poll never rewrites settings the store owns.
- // Width, verdict and request move together; a late reply can overwrite a newer one.
+ // Hydration only, so a steady poll never rewrites settings the store owns. Width, verdict
+ // and request move together; a late reply can overwrite a newer one.
...(seedLoadParams &&
hydratingExistingModel &&
status.mlx_kv_bits !== undefined &&
@@ -525,8 +500,8 @@ export function applyActiveModelStatusToStore(
chatTemplateOverrideReason: null,
mlxKvQuantNote: null,
})),
- // Recovery for a hydration this tab never saw, and only when nothing is
- // staged: re-seeding over an earlier edit would discard it.
+ // Recovery for a hydration this tab never saw, and only when nothing is staged: re-seeding
+ // over an earlier edit would discard it.
...(seedLoadParams &&
!hydratingExistingModel &&
status.is_mlx === true &&
@@ -541,49 +516,39 @@ export function applyActiveModelStatusToStore(
chatTemplateOverrideReason: status.chat_template_override_reason ?? null,
mlxKvQuantNote: status.mlx_kv_quant_note ?? null,
}),
- // Baseline only, never the control: the echo is the RESOLVED count and would
- // pin a blank "server default" control. The rollback re-sends the baseline,
- // so without this a rollback after a tab reload loses the override.
+ // Baseline only, never the control: the echo is the RESOLVED count and would pin a blank
+ // "server default" control. The rollback re-sends the baseline, so without this a rollback
+ // after a tab reload loses the override.
...(seedLoadParams &&
status.requested_parallel_slots != null &&
(prevState.loadedNParallel === null || hydratingExistingModel) && {
loadedNParallel: status.requested_parallel_slots,
}),
- // A slotless model must not keep the previous GGUF's baseline: the rollback
- // re-sends it. /status omits the echo for non-GGUF and sends an explicit
- // null for diffusion, so an absent field on a GGUF is an older backend.
+ // A slotless model must not keep the previous GGUF's baseline, since the rollback re-sends
+ // it. /status omits the echo for non-GGUF and nulls it for diffusion, so an absent field
+ // on a GGUF means an older backend.
...(seedLoadParams &&
(status.is_gguf === false || status.requested_parallel_slots === null) && {
loadedNParallel: null,
}),
- // Per-model: a change underneath this tab blanks the control like
- // performLoad's cross-model reset, or the old count follows onto the new
- // model. The baseline above still carries the rollback.
+ // Per-model: a change underneath this tab blanks the control like performLoad's cross-model
+ // reset, or the old count follows onto the new model. The baseline still has the rollback.
...(seedLoadParams && slotsModelChanged && { nParallel: null }),
- // AFTER that clear, which both a first hydration and a model change trip:
- // either would leave the control blank while the model runs on a remembered
- // override, so the next Apply would save the blank over it. Adopted only
- // when the running count matches, proving it is this model's own.
+ // AFTER that clear, which both a first hydration and a model change trip: either would leave
+ // the control blank while the model runs on a remembered override, so the next Apply would
+ // save the blank over it. Adopted only when the running count matches.
...(seedLoadParams &&
(slotsUnseeded || slotsModelChanged) &&
rememberedNParallel != null &&
rememberedNParallel === status.requested_parallel_slots && {
nParallel: rememberedNParallel,
}),
- // What the running server was actually invoked with. Without this a tab opened
- // while a model was already loaded knows nothing about its pass-through
- // arguments (the switch path is where they were recorded), and a rollback after
- // a failed switch restores that model without them: the failed target is left
- // resident, so an omitted field cannot inherit across models either.
- //
- // Adopted from every settled status, not only a first read or a model change:
- // another tab or an API client can reload the SAME model and variant with
- // different arguments, or with none, and a baseline pinned at the first read
- // would resend the old list from the rollback path and resurrect arguments that
- // are not running. seedLoadParams is the guard that matters here, and it is the
- // same one the rest of this block uses: while a load is in flight performLoad
- // owns these values, so a poll that answers mid-switch cannot overwrite them.
- // A backend that does not publish the field changes nothing.
+ // What the running server was actually invoked with. Without this a tab opened onto an
+ // already-loaded model knows nothing about its pass-through arguments, and a rollback
+ // after a failed switch restores that model without them. Adopted from every settled
+ // status, not just the first: another client can reload the SAME model with different
+ // arguments, and a pinned baseline would resurrect arguments that are not running.
+ // seedLoadParams still guards it, so a mid-switch poll cannot overwrite performLoad.
...(status.requested_llama_extra_args !== undefined &&
(status.is_gguf ?? true) &&
seedLoadParams && {
@@ -617,11 +582,9 @@ export function applyActiveModelStatusToStore(
}),
...("value" in cacheRamSeed && { cacheRam: cacheRamSeed.value ?? null }),
// A swap under this tab resets the controls too, but that clear belongs INSIDE
- // resolveBatchSizeSeed (modelChanged), not after it: unlike the slot count above,
- // the batch echo is the REQUESTED size, so a blanket null here would also discard
- // the value the seed just adopted from the new model's own echo. The control would
- // then read "default" while the server runs an explicit -b / -ub, and the next
- // Reload or Apply, which omits a blank field, would silently revert it.
+ // resolveBatchSizeSeed (modelChanged), not after it: the batch echo is the REQUESTED size,
+ // so a blanket null here would discard what the seed just adopted from the new model,
+ // leaving "default" over an explicit -b / -ub that the next Apply would revert.
...(seedLoadParams &&
(batchesUnseeded || slotsModelChanged) &&
rememberedNBatch != null &&
@@ -643,10 +606,9 @@ export function applyActiveModelStatusToStore(
placementOrContextChanged) &&
placementAndContextFields),
// The one load param that only ever seeded from null, so a switch left the previous model's
- // template in the store, which the Hub settings page reads as the new model's loaded config:
- // Apply then saves A's template under B. A same-model reload from another client moves it
- // too, so the seed also follows a changed status the way the GPU group above does: baseline
- // always, control only while it still sits on that baseline. See resolveChatTemplateSeed.
+ // template in the store and Apply saved A's template under B. A same-model reload moves it
+ // too, so the seed follows a changed status like the GPU group: baseline always, control
+ // only while it still sits on that baseline. See resolveChatTemplateSeed.
...resolveChatTemplateSeed({
incoming: status.chat_template_override,
previous: {
@@ -665,16 +627,14 @@ export function applyActiveModelStatusToStore(
hydratingExistingModel &&
storedReasoningEnabled === null
) {
- // Anchored regex: first "Xb" / "X.Xb" after start-of-string or
- // [-_/.] so the version literal in "qwen3.5" / "qwen3.6" doesn't match
- // first, and for "Qwen3.5-35B-A3B" the result is 35 (total params),
- // not 3 (MoE active params). Mirrors the regex in
- // use-chat-model-runtime.ts and the inline one in llama_cpp.py.
+ // Anchored regex: first "Xb"/"X.Xb" after start or [-_/.] so the version literal in
+ // "qwen3.5" does not match first, and "Qwen3.5-35B-A3B" yields 35 (total), not 3 (active).
+ // Mirrors use-chat-model-runtime.ts and the inline regex in llama_cpp.py.
let reasoningDefault = true;
const mid = checkpointId.toLowerCase();
if (mid.includes("qwen3.5") || mid.includes("qwen3.6")) {
- // Scan path segments right to left so the size nearest the leaf wins over
- // a size-like parent dir; trailing boundary prevents matching "8bit".
+ // Scan path segments right to left so the size nearest the leaf wins over a size-like parent
+ // dir; the trailing boundary prevents matching "8bit".
const sizeRe = /(?:^|[-_.])(\d+\.?\d*)\s*([bm])(?:$|[-_.])/;
const sizeMatch = mid
.replace(/\\/g, "/")
diff --git a/studio/frontend/src/features/chat/lib/chat-model-loaded.ts b/studio/frontend/src/features/chat/lib/chat-model-loaded.ts
index 5737d312e26..daa49d105db 100644
--- a/studio/frontend/src/features/chat/lib/chat-model-loaded.ts
+++ b/studio/frontend/src/features/chat/lib/chat-model-loaded.ts
@@ -1,9 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Whether the header may call the picked model loaded. Its own plain module so
-// the node suite can drive it: runtime-provider.tsx pulls in the whole chat
-// runtime.
+// Whether the header may call the picked model loaded. Its own plain module so the node suite can
+// drive it: runtime-provider.tsx pulls in the whole chat runtime.
export type ChatModelLoadedInput = {
/** The picker selection, "" when nothing is picked. */
@@ -16,15 +15,11 @@ export type ChatModelLoadedInput = {
residentCheckpoint: string | null | undefined;
};
-/**
- * Resident, not merely picked. Loading an image or video model evicts the chat
- * model (the GPU arbiter allows a single GPU owner) and leaves the selection
- * alone, so a check on the selection reported a model the backend had already
- * released and the next prompt failed with a bare 400.
- *
- * `undefined` means the first status read has not landed: assume loaded, or the
- * header flashes "not loaded" on every startup.
- */
+/** Resident, not merely picked. Loading an image or video model evicts the chat model (the GPU
+ * arbiter allows a single owner) and leaves the selection alone, so a check on the selection
+ * reported a model the backend had released and the next prompt failed with a bare 400.
+ * `undefined` means the first status read has not landed: assume loaded, or the header flashes
+ * "not loaded" on every startup. */
export function chatModelLoaded({
checkpoint,
modelLoading = false,
diff --git a/studio/frontend/src/features/chat/lib/external-model-label.ts b/studio/frontend/src/features/chat/lib/external-model-label.ts
index e58c75c66e3..486b2691d9f 100644
--- a/studio/frontend/src/features/chat/lib/external-model-label.ts
+++ b/studio/frontend/src/features/chat/lib/external-model-label.ts
@@ -3,30 +3,21 @@
import { parseExternalModelId } from "../external-providers";
-/**
- * The model id a connection was asked for, e.g. `kimi-k2.5`, or null when *id* is not an
- * `external::<connectionId>::<modelId>` selection.
- *
- * A connected model has no row in `store.models`, so every label chain that looks a
- * checkpoint up there falls through to the checkpoint itself. `buildExternalModelId`
- * percent-encodes the model id, so the raw string carries no separator any generic shortener
- * can use and the user is shown `external::6235be0905af4221::kimi-k2.5` (#8405). Put this
- * ahead of the raw id in those chains.
- */
+/** The model id a connection was asked for, e.g. `kimi-k2.5`, or null when *id* is not an
+ * `external::<connectionId>::<modelId>` selection. A connected model has no row in
+ * `store.models`, so every label chain that looks a checkpoint up there falls through to the
+ * checkpoint itself, and `buildExternalModelId` percent-encodes the model id, so the raw string
+ * carries no separator a generic shortener can use (#8405). Put this ahead of the raw id. */
export function externalModelLabel(
id: string | null | undefined,
): string | null {
return parseExternalModelId(id)?.modelId ?? null;
}
-/**
- * The short label for a compare pane's model id: the trailing segment, as the compare
- * toasts have always shown for a local model.
- *
- * A compare pane takes its id straight from the picker, and the compare header offers
- * connected models, so `external::abc::openai%2Fgpt-5` reached the plain
- * `id.split("/").pop()` this replaced and came back whole. Parsing first yields `gpt-5`.
- */
+/** The short label for a compare pane's model id: the trailing segment, as the compare toasts have
+ * always shown for a local model. A compare pane takes its id straight from the picker and the
+ * compare header offers connected models, so `external::abc::openai%2Fgpt-5` reached the plain
+ * `id.split("/").pop()` this replaced and came back whole. Parsing first yields `gpt-5`. */
export function compareModelDisplayName(id: string): string {
const value = externalModelLabel(id) ?? id;
const parts = value.split("/");
diff --git a/studio/frontend/src/features/chat/lib/friendly-names.ts b/studio/frontend/src/features/chat/lib/friendly-names.ts
index bc9c77b12d3..b4c9cd4b852 100644
--- a/studio/frontend/src/features/chat/lib/friendly-names.ts
+++ b/studio/frontend/src/features/chat/lib/friendly-names.ts
@@ -1,19 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Friendly default names for auto-created OpenAI shell containers, used by the
- * chat-adapter's lazy-create path (Code pill on, no thread container, non-default
- * TTL). Goal: a memorable label like "otter" instead of "chat-abc12345"; users
- * can still rename via the Unsloth alias map.
- *
- * The list is curated to be unambiguous, non-offensive nouns from natural
- * categories (animals, plants, geography, materials, weather), avoid
- * technical/political/brand words, and stay small (~1.5 KB).
- *
- * Collisions are tolerated: the real unique key is the ``cntr_*`` id, not the
- * name. A short random hex suffix keeps same-name picks visually distinct.
- */
+/** Friendly default names for auto-created OpenAI shell containers, used by the chat-adapter's
+ * lazy-create path. Goal: a memorable label like "otter" instead of "chat-abc12345"; users can
+ * still rename via the Unsloth alias map. Curated to be unambiguous, non-offensive nouns from
+ * natural categories, avoiding technical, political and brand words, and staying small (~1.5 KB).
+ * Collisions are tolerated: the real unique key is the `cntr_*` id. */
const WORDS = [
// animals
@@ -221,10 +213,8 @@ function randomHexSuffix(): string {
.padStart(4, "0");
}
-/**
- * Returns an English-word name with a short random hex suffix (e.g.
- * "kestrel-3f9c"), so repeated words stay distinguishable in the picker.
- */
+/** Returns an English-word name with a short random hex suffix ("kestrel-3f9c"), so repeated words
+ * stay distinguishable in the picker. */
export function pickFriendlyContainerName(): string {
const word = WORDS[Math.floor(Math.random() * WORDS.length)] ?? "container";
return `${word}-${randomHexSuffix()}`;
diff --git a/studio/frontend/src/features/chat/lib/gpu-placement.ts b/studio/frontend/src/features/chat/lib/gpu-placement.ts
index fdcbf1f2596..a852687e331 100644
--- a/studio/frontend/src/features/chat/lib/gpu-placement.ts
+++ b/studio/frontend/src/features/chat/lib/gpu-placement.ts
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Placement sentinels and resolution rules shared by the load paths. Kept free
-// of store / React imports so the contract is unit-testable.
+// Placement sentinels and resolution rules shared by the load paths. Kept free of store and
+// React imports so the contract is unit-testable.
-// Manual-mode gpu_layers sentinel: -1 = Auto (hand layer + context sizing to
-// llama.cpp's --fit). The Manual default; "all on GPU" is the slider's max.
+// Manual-mode gpu_layers sentinel: -1 = Auto, handing layer and context sizing to llama.cpp's
+// --fit. The Manual default; "all on GPU" is the slider's max.
export const GPU_LAYERS_AUTO = -1;
export function shouldHydrateGpuPlacementControls(
@@ -14,16 +14,11 @@ export function shouldHydrateGpuPlacementControls(
return cpuFallbackReason !== "vulkan_startup_crash";
}
-/** GPU placement a compare pane loads with.
- *
- * `own` is the pane's per-model config; `shared` is the live-store snapshot taken at
- * Send, so a sequential load's response echo cannot rewrite it mid-run.
- *
- * A pane's OWN split is sent rather than forced to Auto, since the diffusion runner
- * honours it (#7574). The shared snapshot is NOT inherited by a diffusion pane: its
- * layer count is bounded by another GGUF, no diffusion UI can show or clear it, and
- * a leaked 0 masks the devices entirely.
- */
+/** GPU placement a compare pane loads with. `own` is the pane's per-model config; `shared` is the
+ * live-store snapshot taken at Send, so a sequential load's echo cannot rewrite it mid-run. A
+ * pane's OWN split is sent rather than forced to Auto, since the diffusion runner honours it
+ * (#7574). The shared snapshot is NOT inherited by a diffusion pane: its layer count is
+ * bounded by another GGUF, no diffusion UI can clear it, and a leaked 0 masks the devices. */
export function resolveComparePlacement(
own: { gpuMemoryMode?: "auto" | "manual"; gpuLayers?: number },
shared: { gpuMemoryMode: "auto" | "manual"; gpuLayers: number },
@@ -37,13 +32,10 @@ export function resolveComparePlacement(
};
}
-/** Whether a pane must get the diffusion-safe placement above.
- *
- * An UNCLASSIFIED GGUF counts: the preflight only reads a header already on disk, so
- * an undownloaded GGUF with no family in its name comes back `diffusion_unknown`, and
- * `/load` may then read a diffusion header and apply the inherited count anyway.
- * Non-GGUF panes are excluded: definitively not diffusion, and they send no placement.
- */
+/** Whether a pane must get the diffusion-safe placement above. An UNCLASSIFIED GGUF counts: the
+ * preflight only reads a header already on disk, so an undownloaded GGUF with no family in its
+ * name comes back `diffusion_unknown`, and `/load` may then read a diffusion header and apply
+ * the inherited count anyway. Non-GGUF panes send no placement and are excluded. */
export function shouldPinDiffusionPlacement(
targetIsGguf: boolean,
isDiffusion: boolean | undefined,
@@ -53,18 +45,12 @@ export function shouldPinDiffusionPlacement(
return isDiffusion === true || diffusionUnknown;
}
-/** The split an older shim DROPPED, recovered from a load/status response.
- *
- * A shim without `--ngl` cannot apply a manual split, so the runner reports Auto while
- * the backend keeps the ask in `diffusion_requested_ngl`. In-memory state carries that
- * across a reload but not a refresh, where the store starts at Auto and the response
- * alone looks like a genuine Auto run; the ask has to be restored or the next Apply
- * sends `manual/-1`, which the post-upgrade retry can never turn back into the count.
- *
- * Null when there is nothing to recover: not diffusion, a split that was actually
- * applied (mode "manual", so `gpu_layers` is authoritative), or no standing ask. Zero
- * is a real ask (CPU-only) and is preserved.
- */
+/** The split an older shim DROPPED, recovered from a load/status response. A shim without
+ * `--ngl` cannot apply a manual split, so the runner reports Auto while the backend keeps the
+ * ask in `diffusion_requested_ngl`. In-memory state carries that across a reload but not a
+ * refresh, so the ask has to be restored or the next Apply sends `manual/-1`, which the
+ * post-upgrade retry can never turn back into the count. Null when there is nothing to
+ * recover; zero is a real ask (CPU-only) and is preserved. */
export function recoverDroppedDiffusionSplit(
isDiffusion: boolean | undefined,
mode: "auto" | "manual",
@@ -74,13 +60,10 @@ export function recoverDroppedDiffusionSplit(
return requestedNgl ?? null;
}
-/** Tri-state diffusion classification for a staged (pre-load) GGUF selection.
- *
- * `undefined` means "not known" and must NOT be collapsed to false by a caller that
- * hands the answer on: a definite false tells the compare flow this is an ordinary
- * GGUF, skipping the re-probe above and letting an unconfigured pane inherit another
- * model's layer split (#7574).
- */
+/** Tri-state diffusion classification for a staged (pre-load) GGUF selection. `undefined` means
+ * "not known" and must NOT be collapsed to false by a caller passing the answer on: a definite
+ * false tells the compare flow this is an ordinary GGUF, skipping the re-probe and letting an
+ * unconfigured pane inherit another model's layer split (#7574). */
export function resolveStagedDiffusionClassification(
knownDiffusion: boolean | undefined,
staged:
diff --git a/studio/frontend/src/features/chat/lib/llama-extra-args-normalize.ts b/studio/frontend/src/features/chat/lib/llama-extra-args-normalize.ts
index 5bebbcbaae9..23bc3bb6e5a 100644
--- a/studio/frontend/src/features/chat/lib/llama-extra-args-normalize.ts
+++ b/studio/frontend/src/features/chat/lib/llama-extra-args-normalize.ts
@@ -1,19 +1,13 @@
// 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 pass-through arguments `/load` resolves before its already-loaded comparator runs.
- *
- * Two of them change what the request means rather than adding to it, so a comparison
- * against the resident runtime has to resolve them the same way or it judges a request the
- * server never received. Mirrors `parse_split_mode_override` / `resolve_tensor_parallel`
- * and `parse_gpu_layers_override` in `core/inference/llama_server_args.py`, and the manual
- * offload strip the route applies in `routes/inference.py` before comparing.
- *
- * A malformed list answers null rather than throwing, unlike the backend's parser: this
- * runs while deciding whether to skip a load, and the load itself is where a bad argument
- * belongs.
- */
+/** The pass-through arguments `/load` resolves before its already-loaded comparator runs. Two of
+ * them change what the request means rather than adding to it, so a comparison against the
+ * resident runtime has to resolve them the same way or it judges a request the server never
+ * received. Mirrors `parse_split_mode_override`, `resolve_tensor_parallel` and
+ * `parse_gpu_layers_override` in llama_server_args.py, plus the manual offload strip the route
+ * applies before comparing. A malformed list answers null rather than throwing, unlike the
+ * backend's parser: the load itself is where a bad argument belongs. */
const SPLIT_MODE_FLAGS = new Set(["-sm", "--split-mode"]);
const GPU_LAYER_FLAGS = new Set(["-ngl", "--gpu-layers", "--n-gpu-layers"]);
@@ -28,13 +22,10 @@ const OFFLOAD_SHADOWING_FLAGS = new Set([
"--cpu-moe",
]);
-/**
- * `_flag_name`: the flag a token names, or null when it is a value.
- *
- * Shorts always start with a letter, so `-1` and `-0.5` are values, which is what lets
- * `--n-gpu-layers -1` parse at all. Long options fold underscores the way llama.cpp does.
- * The backend's attached `-np8` folding is left out: nothing here reads `-np`.
- */
+/** `_flag_name`: the flag a token names, or null when it is a value. Shorts always start with a
+ * letter, so `-1` and `-0.5` are values, which is what lets `--n-gpu-layers -1` parse at all.
+ * Long options fold underscores the way llama.cpp does. The backend's attached `-np8` folding
+ * is left out: nothing here reads `-np`. */
function flagName(token: string): string | null {
const trimmed = token.trim();
if (!trimmed.startsWith("-") || trimmed === "-" || trimmed === "--") {
@@ -65,8 +56,7 @@ function lastFlagValue(
continue;
}
const next = args?.[i + 1];
- // A flag with no value is malformed; the load rejects it, and until then it says
- // nothing about what was requested.
+ // A flag with no value is malformed; the load rejects it, and until then it says nothing about what was requested.
if (next === undefined || flagName(String(next)) !== null) {
return null;
}
@@ -76,10 +66,8 @@ function lastFlagValue(
return value;
}
-/**
- * `resolve_tensor_parallel`: an explicit `--split-mode` in extras last-wins over the
- * toggle, and tensor parallelism is on only when that mode is `tensor`.
- */
+/** `resolve_tensor_parallel`: an explicit `--split-mode` in extras last-wins over the toggle, and
+ * tensor parallelism is on only when that mode is `tensor`. */
export function resolveTensorParallel(
extraArgs: readonly string[] | null | undefined,
tensorParallel: boolean,
@@ -90,13 +78,9 @@ export function resolveTensorParallel(
: override.trim().toLowerCase() === "tensor";
}
-/**
- * `parse_gpu_layers_override`, in the three states its caller has to tell apart.
- *
- * The backend raises on a malformed value, so the load fails and says so. A comparison
- * that folded that into "no override" would strip the offending token, find the rest
- * agreeable and adopt, and the user's saved setting would go missing in silence instead.
- */
+/** `parse_gpu_layers_override`, in the three states its caller has to tell apart. The backend
+ * raises on a malformed value, so the load fails and says so. Folding that into "no override"
+ * would strip the offending token, adopt the rest, and lose the user's saved setting silently. */
export type GpuLayersOverride =
| { kind: "absent" }
| { kind: "value"; layers: number }
@@ -115,10 +99,8 @@ export function parseGpuLayersOverride(
: { kind: "invalid" };
}
-/**
- * The offload flags manual mode owns, dropped as the route drops them before comparing.
- * Only under manual: in auto an inherited `-ngl` is respected and reaches the child.
- */
+/** The offload flags manual mode owns, dropped as the route drops them before comparing. Only
+ * under manual: in auto an inherited `-ngl` is respected and reaches the child. */
export function stripManagedOffloadFlags(
extraArgs: readonly string[] | null | undefined,
): string[] | null | undefined {
diff --git a/studio/frontend/src/features/chat/lib/mlx-runtime-state.ts b/studio/frontend/src/features/chat/lib/mlx-runtime-state.ts
index b9278f2bdab..d4706e24051 100644
--- a/studio/frontend/src/features/chat/lib/mlx-runtime-state.ts
+++ b/studio/frontend/src/features/chat/lib/mlx-runtime-state.ts
@@ -12,11 +12,9 @@ type MlxRuntimeResponse = Pick<
| "mlx_kv_quant_note"
>;
-/** MLX KV-quantization state a load response establishes.
- *
- * A non-MLX response retires the verdict but omits mlxKvBits: the width is dormant
- * there, not wrong, and a preset carrying it must survive the round-trip.
- */
+/** MLX KV-quantization state a load response establishes. A non-MLX response retires the verdict but
+ * omits mlxKvBits: the width is dormant there, not wrong, and a preset carrying it must survive
+ * the round-trip. */
export function mlxRuntimeStateFrom(resp: MlxRuntimeResponse): {
mlxKvBits?: number | null;
loadedMlxKvBitsRequested: number | null;
diff --git a/studio/frontend/src/features/chat/lib/per-model-params.ts b/studio/frontend/src/features/chat/lib/per-model-params.ts
index 46fa1497dff..034048127ce 100644
--- a/studio/frontend/src/features/chat/lib/per-model-params.ts
+++ b/studio/frontend/src/features/chat/lib/per-model-params.ts
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Per-model parameter memory: a per-checkpoint record alongside the global set,
-// so a switch no longer hands the next model the previous one's settings. No
-// store or network imports, so the rules stay unit-testable.
+// Per-model parameter memory: a per-checkpoint record alongside the global set, so a switch no
+// longer hands the next model the previous one's settings. No store or network imports, so
+// the rules stay unit-testable.
import type {
InferenceParams,
@@ -29,8 +29,8 @@ export const PERSISTED_INFERENCE_PARAM_KEYS = [
"seed",
] as const satisfies readonly PersistedInferenceParamKey[];
-/** What the memory records. `maxSeqLength` is left out: the context belongs to
- * the load config, and a second copy would advertise one never loaded. */
+/** What the memory records. `maxSeqLength` is left out: the context belongs to the load config,
+ * and a second copy would advertise one never loaded. */
export const REMEMBERED_INFERENCE_PARAM_KEYS =
PERSISTED_INFERENCE_PARAM_KEYS.filter(
(key): key is PersistedInferenceParamKey => key !== "maxSeqLength",
@@ -65,9 +65,9 @@ function movedRememberedParam(changed: PersistedInferenceParams): boolean {
);
}
-/** Just the keys an edit moved. What goes to the server for a model that already
- * has an entry: it merges per key, so a whole snapshot would put this browser's
- * copy of every other key over another tab's. */
+/** Just the keys an edit moved. What goes to the server for a model that already has an entry:
+ * it merges per key, so a whole snapshot would put this browser's copy of every other key
+ * over another tab's. */
export function pickRememberedChanges(
changed: PersistedInferenceParams,
): PersistedInferenceParams {
@@ -81,8 +81,8 @@ export function pickRememberedChanges(
return picked;
}
-/** The map after an edit, or null when there is nothing to record (off, no model,
- * no persisted param moved), which leaves the map and its version untouched. */
+/** The map after an edit, or null when there is nothing to record (off, no model, no persisted
+ * param moved), which leaves the map and its version untouched. */
export function getRememberedParamsPatch(
enabled: boolean,
paramsByModel: Record<string, PersistedInferenceParams>,
@@ -93,14 +93,14 @@ export function getRememberedParamsPatch(
if (!(enabled && modelId && movedRememberedParam(changedParams))) {
return null;
}
- // The whole snapshot: replay overlays the entry, so a partial one would leave
- // the gaps filled by whichever model was on screen last.
+ // The whole snapshot: replay overlays the entry, so a partial one would leave the gaps filled
+ // by whichever model was on screen last.
return { ...paramsByModel, [modelId]: snapshot };
}
-/** The params a switch should land on. A model with nothing remembered keeps what
- * is on screen, and `current` comes back by identity so callers can tell that
- * apart. `maxTokensCap` is the context the model just loaded with. */
+/** The params a switch should land on. A model with nothing remembered keeps what is on screen,
+ * and `current` comes back by identity so callers can tell that apart. `maxTokensCap` is the
+ * context the model just loaded with. */
export function getReplayedParams(
enabled: boolean,
paramsByModel: Record<string, PersistedInferenceParams>,
@@ -109,8 +109,8 @@ export function getReplayedParams(
checkpointChanged: boolean,
maxTokensCap?: number,
): InferenceParams {
- // The cap describes the load, not the memory, so it applies even when nothing
- // replays. Identity survives when the budget already fits.
+ // The cap describes the load, not the memory, so it applies even when nothing replays. Identity
+ // survives when the budget already fits.
const capped = (params: InferenceParams): InferenceParams =>
maxTokensCap !== undefined && params.maxTokens > maxTokensCap
? { ...params, maxTokens: maxTokensCap }
@@ -122,8 +122,8 @@ export function getReplayedParams(
if (!remembered) {
return capped(current);
}
- // Key by key, not a spread: the row accepts every persisted key from any
- // writer, and a spread would carry maxSeqLength and stale keys into params.
+ // Key by key, not a spread: the row accepts every persisted key from any writer, and a spread
+ // would carry maxSeqLength and stale keys into params.
const replayed = { ...current };
for (const key of REMEMBERED_INFERENCE_PARAM_KEYS) {
const value = remembered[key];
diff --git a/studio/frontend/src/features/chat/lib/resident-config-match.ts b/studio/frontend/src/features/chat/lib/resident-config-match.ts
index 8b45a48448d..f078ba59716 100644
--- a/studio/frontend/src/features/chat/lib/resident-config-match.ts
+++ b/studio/frontend/src/features/chat/lib/resident-config-match.ts
@@ -65,15 +65,10 @@ function sameGpuSet(
return sameList(sort(left ?? []), sort(right ?? []));
}
-/**
- * What a config field resolves to when left unset.
- *
- * These four are not per-model, so omitting them is not silence: the applier fills them
- * from a standing preference or a constant (`applyPerModelConfigToRuntime`), and the load
- * sends that. The caller supplies them rather than this module reading the store, so the
- * comparison stays a pure function of its inputs. Required, not optional: a resolver a
- * caller can forget is one that silently keeps a runtime the user did not ask for.
- */
+/** What a config field resolves to when left unset. These four are not per-model, so omitting them
+ * is not silence: `applyPerModelConfigToRuntime` fills them from a standing preference or a
+ * constant and the load sends that. Required, not optional: a resolver a caller can forget
+ * silently keeps a runtime the user did not ask for. */
export type StandingConfigDefaults = {
/** `readPersistedSpeculativeType()`, already normalized. */
speculativeType: string | null;
@@ -83,71 +78,48 @@ export type StandingConfigDefaults = {
gpuLayers: number;
/** The applier's own constant, 0. */
nCpuMoe: number;
- /**
- * `reconcilePersistedGpuIds`, with the device cache already warm and the resident model's
- * diffusion flag bound. `performLoad` sends the reconciled pick, not the saved one: a
- * selection saved in another index namespace, or naming GPUs that are gone, becomes
- * Automatic before `/load`. Comparing the raw ids would let a saved physical `[1]` adopt a
- * server pinned to Vulkan device 1.
- */
+ /** `reconcilePersistedGpuIds`, with the device cache warm and the resident model's diffusion flag
+ * bound. `performLoad` sends the reconciled pick, not the saved one: comparing raw ids would
+ * let a saved physical `[1]` adopt a server pinned to Vulkan device 1. */
reconcileGpuIds: (
ids: number[] | null,
savedIndexKind: GpuIndexKind | null | undefined,
) => number[] | null;
- /**
- * `resolveLoadMaxSeqLength` bound to the inputs `performLoad` gives it, so the comparison
- * is against the n_ctx the load would send. An unset length is not simply 0: for a GGUF
- * re-pick it resolves to the resident context, and only otherwise to 0, which is Auto.
- */
+ /** `resolveLoadMaxSeqLength` bound to the inputs `performLoad` gives it, so the comparison is
+ * against the n_ctx the load would send. An unset length is not simply 0: for a GGUF re-pick
+ * it resolves to the resident context. */
resolveContextLength: (customContextLength: number | null) => number;
- /**
- * The slot count the server resolves an unset `--parallel` to, or null when it could not
- * be read. `_resolve_parallel_slots` fills an omitted request from the server-wide
- * default and stores THAT as `requested_parallel_slots`, so an unset ask is never null on
- * the status side and comparing the two directly reloaded every default pick.
- */
+ /** The slot count the server resolves an unset `--parallel` to, or null when unreadable.
+ * `_resolve_parallel_slots` stores the server-wide default as `requested_parallel_slots`, so
+ * an unset ask is never null on the status side and comparing directly reloaded every pick. */
parallelSlots: number | null;
- /**
- * `splitRatio` as the store holds it now, which is what the load sends. Never a config
- * field: `applyPerModelConfigToRuntime` clears it, so applying any remembered config
- * asks for the default distribution rather than the resident custom one.
- */
+ /** `splitRatio` as the store holds it now, which is what the load sends. Never a config field:
+ * `applyPerModelConfigToRuntime` clears it, so any remembered config asks for the default. */
splitRatio: number[] | null;
- /**
- * `normalizeSpeculativeType`, passed rather than imported. It lives on the chat runtime
- * store, which reaches React, and this module is deliberately a leaf so the node suite
- * can drive it; copying its mapping here (comma-chained legacy echoes and all) would be
- * a second copy free to drift from the one the load actually uses.
- */
+ /** `normalizeSpeculativeType`, passed rather than imported: it lives on the chat runtime store,
+ * which reaches React, and this module is a leaf so the node suite can drive it. A copy here
+ * would be free to drift from the one the load uses. */
normalizeSpeculative: (value: string | null | undefined) => string | null;
};
-/**
- * One setting the resident load can disagree about. `pinned` is whether the config has an
- * opinion at all, `agrees` whether the running server satisfies it. The pair survives for
- * `llamaExtraArgs`, the only field the applier leaves unresolved.
- */
+/** One setting the resident load can disagree about. `pinned` is whether the config has an opinion
+ * at all, `agrees` whether the running server satisfies it. The pair survives for
+ * `llamaExtraArgs`, the only field the applier leaves unresolved. */
type SettingCheck = {
/** Placement, which the backend rewrites wholesale on a preserved CPU fallback. */
placement?: true;
- /**
- * One of the two fields `_mlx_runtime_settings_match` compares. The non-GGUF branch of
- * /load checks identity and those, then answers already_loaded, so nothing else here
- * may decide against a safetensors or MLX resident.
- */
+ /** One of the two fields `_mlx_runtime_settings_match` compares. The non-GGUF branch of /load
+ * checks identity and those, then answers already_loaded, so nothing else here may decide
+ * against a safetensors or MLX resident. */
mlxComparable?: true;
- /**
- * Placement the diffusion branch of `_runtime_matches_intent` replaces wholesale with
- * one `_diffusion_manual_ngl` comparison, rather than comparing field by field.
- */
+ /** Placement the diffusion branch of `_runtime_matches_intent` replaces wholesale with one
+ * `_diffusion_manual_ngl` comparison. */
ggufPlacement?: true;
/** The diffusion branch's own comparison, which has no meaning off it. */
diffusionOnly?: true;
- /**
- * A chat-only invocation setting. `_runtime_matches_intent` guards these on
- * `not self._is_diffusion`, and the status nulls the ones it publishes at all, so
- * comparing them against a diffusion runtime rejects a load that would deduplicate.
- */
+ /** Chat-only invocation settings. `_runtime_matches_intent` guards these on `not
+ * self._is_diffusion` and the status nulls them, so comparing them against a diffusion
+ * runtime rejects a load that would deduplicate. */
chatOnly?: true;
pinned: (config: PerModelConfig) => boolean;
agrees: (
@@ -157,15 +129,10 @@ type SettingCheck = {
) => boolean;
};
-/**
- * Fallback reasons the backend retries on an IDENTICAL next load, taken from the arms of
- * `LlamaCppBackend._runtime_matches_intent` that return False to force the repair: a drafter
- * fetch that can be attempted again, and the stand-down the UI asks the user to fix by
- * updating llama.cpp. The rest are not here on purpose. "drafter_no_vram" and
- * "mla_mtp_disabled" are Auto-mode policy, and "runtime_error" only reopens when the draft
- * count changes, which the settings comparison already sees; treating them as repairable
- * would prompt to stop running chats on every re-pick and repair nothing.
- */
+/** Fallback reasons the backend retries on an IDENTICAL next load, from the arms of
+ * `LlamaCppBackend._runtime_matches_intent` that return False to force a repair. The rest are
+ * excluded on purpose: "drafter_no_vram" and "mla_mtp_disabled" are Auto-mode policy, and
+ * "runtime_error" only reopens when the draft count changes, which the comparison sees. */
const RETRYABLE_SPEC_FALLBACKS = new Set([
"drafter_not_found",
"binary_no_mtp",
@@ -184,17 +151,11 @@ const SPECULATIVE_MODES = new Set([
"dflash",
]);
-/**
- * Whether the resident load's speculative decoding is degraded in a way the next identical
- * `/load` would repair.
- *
- * This is the one place where sending a request the runtime already satisfies is not a
- * no-op, so it is the one reason to decline the shortcut on settings that match. The
- * decision stays coarser than the backend's: the status echoes the fallback reason but not
- * `_dflash_retry_needed` or an inconclusive capability probe, so a load that would dedupe
- * on the far side can still be sent. That costs one round trip through `already_loaded`,
- * which returns before any teardown.
- */
+/** Whether the resident load's speculative decoding is degraded in a way the next identical `/load`
+ * would repair. This is the one place where sending a request the runtime already satisfies
+ * is not a no-op. The decision stays coarser than the backend's: the status echoes the
+ * fallback reason but not `_dflash_retry_needed`, so a load that would dedupe can still be
+ * sent, costing one round trip through `already_loaded`. */
export function residentSpeculativeNeedsRepair(
status: Pick<
InferenceStatusResponse,
@@ -206,15 +167,13 @@ export function residentSpeculativeNeedsRepair(
| "spec_drafter_kind"
>,
resolvedSpeculativeType: string | null,
- /**
- * Whether the load carries a `gguf_path`, which the route sets from the identifier
- * alone: `source.gguf_path if model_identifier.lower().endswith(".gguf") else None`.
- */
+ /** Whether the load carries a `gguf_path`, which the route sets from the identifier alone:
+ * `source.gguf_path if model_identifier.lower().endswith(".gguf") else None`. */
sendsGgufPath = false,
): boolean {
const mode = resolvedSpeculativeType ?? "auto";
- // Two arms that record no fallback reason, so the reason check below cannot see them.
- // The probe arm is not gated on a mode; the DFlash one is, as the backend gates it.
+ // Two arms that record no fallback reason, so the reason check below cannot see them. The probe
+ // arm is not gated on a mode; the DFlash one is, as the backend gates it.
if (status.spec_probe_retry_pending === true) {
return true;
}
@@ -236,13 +195,12 @@ export function residentSpeculativeNeedsRepair(
) {
return false;
}
- // The drafter_not_found arm reloads so the next Apply retries the fetch, but excludes
- // the two kinds whose absence is not transient: DFlash asks through its retry flag
- // above, and an absent DSpark sidecar is the permanent state of every repo but one, so
- // retrying either would relaunch an identical server forever.
+ // The drafter_not_found arm reloads so the next Apply retries the fetch, but excludes the two
+ // kinds whose absence is not transient: DFlash asks through its retry flag, and an absent
+ // DSpark sidecar is the permanent state of every repo but one.
if (status.spec_fallback_reason === "drafter_not_found") {
- // The arm is guarded on `intent.gguf_path is None`, so a standalone file never
- // reaches it and an identical load dedupes rather than retrying the fetch.
+ // The arm is guarded on `intent.gguf_path is None`, so a standalone file never reaches it and an
+ // identical load dedupes rather than retrying the fetch.
if (sendsGgufPath) {
return false;
}
@@ -256,11 +214,9 @@ export function residentSpeculativeNeedsRepair(
return false;
}
}
- // A binary stand-down repairs only once a different llama-server is installed, which is
- // the necessary condition in spec_binary_fallback_can_retry. Without that the reload
- // dedupes and the prompt was for nothing. Only an explicit false settles it: a backend
- // too old to report it keeps the coarser answer rather than suppress a real repair, and
- // the field says nothing about a stand-down that was never about the binary.
+ // A binary stand-down repairs only once a different llama-server is installed, the condition in
+ // spec_binary_fallback_can_retry. Only an explicit false settles it: a backend too old to
+ // report it keeps the coarser answer rather than suppressing a real repair.
return !(
BINARY_SPEC_FALLBACKS.has(status.spec_fallback_reason ?? "") &&
status.spec_fallback_binary_changed === false
@@ -276,22 +232,14 @@ const requestedGpuMemoryMode = (
const cleanTemplate = (value: string | null | undefined): string | null =>
value?.trim() ? value : null;
-/**
- * Mirrors the fields `_runtime_matches_intent` reloads for, plus the MLX pair
- * `_mlx_runtime_settings_match` compares.
- *
- * Nearly every check is unconditionally pinned. A config reaches here only after
- * `applyModelLoadConfigToRuntime` wrote it over the runtime store (`chat-page.tsx:3242`,
- * `hub-page.tsx:1329`), and that applier resolves each of these with `?? null`, so the
- * snapshot `performLoad` takes reads null rather than inheriting the resident value: an
- * unset field asks for the default, not for whatever is running. Only `llamaExtraArgs` is
- * genuinely optional, being the one field with no `?? null` fallback.
- */
+/** Mirrors the fields `_runtime_matches_intent` reloads for, plus the MLX pair
+ * `_mlx_runtime_settings_match` compares. Nearly every check is unconditionally pinned: a
+ * config reaches here only after `applyModelLoadConfigToRuntime` resolved each field with `??
+ * null`, so an unset field asks for the default. Only `llamaExtraArgs` is optional. */
const SETTING_CHECKS: SettingCheck[] = [
{
- // Resolved, not compared raw: an unset length is Auto, which the load sends as 0 for a
- // cross-model GGUF pick and as the resident context when re-picking the same one.
- // Reading null as "no opinion" against a status echoing either number was a reload.
+ // Resolved, not compared raw: an unset length is Auto, which the load sends as 0 for a cross-model
+ // GGUF pick and as the resident context when re-picking the same one.
pinned: () => true,
// A safetensors or MLX status reports this too, so the general non-GGUF rule below
// is what keeps this check off those models.
@@ -310,8 +258,8 @@ const SETTING_CHECKS: SettingCheck[] = [
(c.mlxKvBits ?? null) === (s.mlx_kv_bits_requested ?? null),
},
{
- // Always pinned: an unset mode resolves to the standing preference, and the load sends
- // it. Reading it as silence let a pick asking for "off" adopt a resident MTP runtime.
+ // Always pinned: an unset mode resolves to the standing preference and the load sends it. Reading
+ // it as silence let a pick asking for "off" adopt a resident MTP runtime.
pinned: () => true,
agrees: (c, s, standing) =>
(standing.normalizeSpeculative(c.speculativeType) ??
@@ -333,9 +281,8 @@ const SETTING_CHECKS: SettingCheck[] = [
},
{
chatOnly: true,
- // Unknown default: null against the status's resolved count is a reload, the safe
- // direction. defaultParallelSlots is the EFFECTIVE count, so a build that clamps to one
- // slot reloads rather than adopts, which is the same bias.
+ // Unknown default: null against the status's resolved count is a reload, the safe direction.
+ // defaultParallelSlots is the EFFECTIVE count, so a build that clamps to one slot reloads.
pinned: () => true,
agrees: (c, s, standing) =>
(c.nParallel ?? standing.parallelSlots) ===
@@ -353,9 +300,8 @@ const SETTING_CHECKS: SettingCheck[] = [
},
{
chatOnly: true,
- // Same shape as the batch pair above: a blank control means the llama.cpp
- // default, and the status echoes what the load asked for rather than what it
- // resolved to, so null on both sides is agreement and anything else reloads.
+ // Same shape as the batch pair above: a blank control means the llama.cpp default and the status
+ // echoes what the load asked for, so null on both sides is agreement.
pinned: () => true,
agrees: (c, s) => (c.loadMode ?? null) === (s.requested_load_mode ?? null),
},
@@ -392,17 +338,15 @@ const SETTING_CHECKS: SettingCheck[] = [
(s.tensor_parallel ?? false) ||
// Rewritten away with the rest of the placement on a virtualised Metal device.
s.gpu_placement_paravirtual === true ||
- // A split the architecture gate normalized away: that layer-mode runtime IS this
- // request as the gate rewrote it, and the backend accepts it back unchanged. The
- // resolved mode again, since a pass-through --split-mode is what was gated.
+ // A split the architecture gate normalized away: that layer-mode runtime IS this request as the
+ // gate rewrote it, and the backend accepts it back unchanged.
(resolveTensorParallel(c.llamaExtraArgs, c.tensorParallel) &&
s.tensor_parallel !== true &&
s.tensor_parallel_dropped_by_arch_gate === true),
},
{
- // Not nullable, so it always has an opinion; a status omitting it ran with the
- // projector. The backend reloads when this disagrees, so adopting a resident
- // server that disagrees would drop the setting and keep the VRAM spent.
+ // Not nullable, so it always has an opinion; a status omitting it ran with the projector. The
+ // backend reloads when this disagrees, so adopting would keep the VRAM spent.
chatOnly: true,
pinned: () => true,
agrees: (c, s) => c.disableVision === (s.disable_vision ?? false),
@@ -416,8 +360,8 @@ const SETTING_CHECKS: SettingCheck[] = [
cleanTemplate(s.chat_template_override),
},
{
- // undefined: never read the stored value. null: the user cleared the box, which agrees
- // only with a load invoked with no pass-through args.
+ // undefined: never read the stored value. null: the user cleared the box, which agrees only with a
+ // load invoked with no pass-through args.
chatOnly: true,
pinned: (c) => c.llamaExtraArgs !== undefined,
agrees: (c, s, standing) =>
@@ -438,9 +382,8 @@ const SETTING_CHECKS: SettingCheck[] = [
(s.gpu_memory_mode ?? standing.gpuMemoryMode),
},
{
- // Resolves to GPU_LAYERS_AUTO rather than to a preference, but the load still sends it.
- // Only under Manual, as _runtime_matches_intent compares it: under Auto the fitter
- // chooses the offload, so the layer count the load carries decides nothing.
+ // Resolves to GPU_LAYERS_AUTO rather than to a preference, but the load still sends it. Only under
+ // Manual: under Auto the fitter chooses the offload, so the layer count decides nothing.
placement: true,
ggufPlacement: true,
pinned: () => true,
@@ -449,9 +392,9 @@ const SETTING_CHECKS: SettingCheck[] = [
requestedGpuLayers(c, standing) === (s.gpu_layers ?? standing.gpuLayers),
},
{
- // Manual with a non-negative pin, the same guard the backend uses. A config keeps a
- // hidden nCpuMoe after the layer slider goes back to Auto, and llama.cpp records 0,
- // so comparing it there rejected an otherwise identical runtime.
+ // Manual with a non-negative pin, the same guard the backend uses. A config keeps a hidden
+ // nCpuMoe after the layer slider goes back to Auto, and llama.cpp records 0, so comparing it
+ // there rejected an identical runtime.
placement: true,
ggufPlacement: true,
pinned: () => true,
@@ -461,9 +404,8 @@ const SETTING_CHECKS: SettingCheck[] = [
(c.nCpuMoe ?? standing.nCpuMoe) === (s.n_cpu_moe ?? standing.nCpuMoe),
},
{
- // Absent resolves to a null selection in the applier, and a null selection is sent as
- // Automatic, so this is pinned like the rest: an unset pick does not adopt a server
- // that was placed on a chosen GPU. Reconciled first, since that is what /load sends.
+ // Absent resolves to a null selection, which is sent as Automatic, so this is pinned like the
+ // rest: an unset pick does not adopt a server placed on a chosen GPU. Reconciled first.
placement: true,
pinned: () => true,
agrees: (c, s, standing) => {
@@ -471,9 +413,8 @@ const SETTING_CHECKS: SettingCheck[] = [
c.selectedGpuIds ?? null,
c.selectedGpuIndexKind,
);
- // The diffusion runner drives one device, so matches_gpu_ids reduces the request to
- // its lowest id and the status reports only that. Comparing the configured set
- // rejected a runtime the backend would have called identical.
+ // The diffusion runner drives one device, so matches_gpu_ids reduces the request to its lowest id
+ // and the status reports only that.
const pick =
s.is_diffusion === true && reconciled?.length
? [Math.min(...reconciled)]
@@ -481,38 +422,33 @@ const SETTING_CHECKS: SettingCheck[] = [
if (sameGpuSet(pick, s.requested_gpu_ids)) {
return true;
}
- // Either pool, as matches_gpu_ids accepts either: fitting may narrow the request to
- // the smallest subset that holds the model, and asking for that subset does not
- // reload. Comparing only the raw request prompted for a load that dedupes. Guarded on
- // a non-empty echo, since an absent one is no placement rather than Automatic, and
- // reading it as Automatic would make an unpinned pick match every pinned server.
+ // Either pool, as matches_gpu_ids accepts either: fitting may narrow the request to the smallest
+ // subset that holds the model. Guarded on a non-empty echo, since an absent one is no
+ // placement rather than Automatic.
return Boolean(s.gpu_ids?.length) && sameGpuSet(pick, s.gpu_ids);
},
},
{
- // The split is placement the config cannot carry: the applier clears splitRatio, so a
- // remembered config asks for the default distribution while a resident manual load may
- // be running a custom one. Omitting it kept that custom split with nothing saying so.
+ // The split is placement the config cannot carry: the applier clears splitRatio, so a remembered
+ // config asks for the default distribution while a resident manual load may run a custom one.
placement: true,
ggufPlacement: true,
pinned: () => true,
agrees: (_c, s, standing) => sameList(standing.splitRatio, s.tensor_split),
},
{
- // A managed override the backend would reject outright. It raises there, so the load
- // fails and says so; folding it into "no override" here would strip the token, find
- // the rest agreeable and adopt, losing the saved setting in silence.
+ // A managed override the backend would reject outright. Folding it into "no override" here would
+ // strip the token, find the rest agreeable and adopt, losing the saved setting in silence.
pinned: (c) => c.llamaExtraArgs !== undefined,
agrees: (c, _s, standing) =>
requestedGpuMemoryMode(c, standing) !== "manual" ||
parseGpuLayersOverride(c.llamaExtraArgs).kind !== "invalid",
},
{
- // What the diffusion branch compares in place of the placement fields above:
- // _diffusion_manual_ngl, the layer count only under Manual with a non-negative pin and
- // the runner's own default otherwise. An older shim that dropped a manual NGL leaves
- // the status reporting Auto while keeping the request here, so comparing the mode raw
- // rejected a load that deduplicates.
+ // What the diffusion branch compares in place of the placement fields: _diffusion_manual_ngl, the
+ // layer count only under Manual with a non-negative pin. An older shim that dropped a manual
+ // NGL leaves the status reporting Auto while keeping the request here, so comparing the mode
+ // raw rejected a load that deduplicates.
diffusionOnly: true,
pinned: () => true,
agrees: (c, s, standing) => {
@@ -520,9 +456,8 @@ const SETTING_CHECKS: SettingCheck[] = [
if (ngl !== (s.diffusion_requested_ngl ?? null)) {
return false;
}
- // The request is retained even when an older shim dropped the split, so once the
- // installed shim gains --ngl support the same request has to go through and finally
- // apply it. The backend rejects it for exactly that window.
+ // The request is retained even when an older shim dropped the split, so once the installed shim
+ // gains --ngl support the same request finally applies it.
return !(
ngl !== null &&
s.gpu_layers !== ngl &&
@@ -532,11 +467,8 @@ const SETTING_CHECKS: SettingCheck[] = [
},
];
-/**
- * The layer count `/load` ends up with. Under manual a pass-through `-ngl` is copied into
- * the first-class field before the comparator runs, so the raw toggle is not what was
- * requested.
- */
+/** The layer count `/load` ends up with. Under manual a pass-through `-ngl` is copied into the
+ * first-class field before the comparator runs, so the raw toggle is not what was requested. */
function requestedGpuLayers(
config: PerModelConfig,
standing: StandingConfigDefaults,
@@ -561,17 +493,10 @@ function diffusionManualNgl(
: null;
}
-/**
- * Whether the backend would rewrite this request into the resident CPU fallback.
- *
- * `adopt_load_intent_if_matched` runs `_preserve_cpu_fallback_intent` first, so after a
- * Vulkan startup crash an Auto request becomes the resident manual/zero-layer intent and
- * dedupes. Comparing placement literally rejected it and raised the prompt this PR removes.
- *
- * Mirrors `_cpu_fallback_request_eligible`, minus its environment terms: the resident
- * server already fell back under this same env, and a request carrying its own placement
- * args is excluded here rather than guessed at.
- */
+/** Whether the backend would rewrite this request into the resident CPU fallback.
+ * `adopt_load_intent_if_matched` runs `_preserve_cpu_fallback_intent` first, so after a
+ * Vulkan startup crash an Auto request becomes the resident manual/zero-layer intent and
+ * dedupes. Mirrors `_cpu_fallback_request_eligible` minus its environment terms. */
function cpuFallbackPlacementPreserved(
config: PerModelConfig,
status: ResidentRuntime,
@@ -595,47 +520,35 @@ function cpuFallbackPlacementPreserved(
);
}
-/**
- * Whether the resident load already runs the settings this pick would ask for.
- *
- * Identity is not the whole of a load: `LlamaCppBackend` reuses a running server only when
- * the request also agrees on context, KV dtype, slots, batch sizes, placement, speculative
- * mode, chat template and pass-through args. An unset field is still an opinion, since the
- * applier resolves it before the load reads it: unset asks for the default, not for whatever
- * is running. What keeps the common case working is the other door: a model the user never
- * configured arrives with no config at all, and a model they did configure carries what
- * `currentRuntimePerModelConfig` wrote from the load that made it resident.
- *
- * The bias is one-sided on purpose. "Differs" costs one reload, which is what happened
- * before any of this existed; a wrong "matches" leaves the user on settings they did not
- * ask for, with the panel rolled back to the resident model so nothing says so.
- *
- * `maxSeqLength` is not compared: a client-side generation cap no status echoes, and it
- * never reaches llama-server's invocation.
- */
+/** Whether the resident load already runs the settings this pick would ask for. `LlamaCppBackend`
+ * reuses a running server only when the request also agrees on context, KV dtype, slots,
+ * batch sizes, placement, speculative mode, chat template and pass-through args. An unset
+ * field is still an opinion: the applier resolves it before the load reads it. The bias is
+ * one-sided on purpose: "differs" costs one reload, while a wrong "matches" leaves the user
+ * on settings they did not ask for. `maxSeqLength` is not compared: it is a client-side cap
+ * that never reaches llama-server's invocation. */
export function residentRuntimeMatchesConfig(
status: ResidentRuntime,
config: PerModelConfig | null | undefined,
standing: StandingConfigDefaults,
): boolean {
- // No config at all is not the same as a config that pins nothing: with none, the load
- // path reads the live runtime, which was hydrated from the resident model, so there is
- // nothing that could differ.
+ // No config at all is not the same as a config that pins nothing: with none, the load path reads
+ // the live runtime, which was hydrated from the resident model.
if (!config) {
return true;
}
const placementPreserved =
- // A virtualised Metal device pins every GGUF request to the CPU before either
- // comparator runs, so placement cannot tell two requests apart there at all.
+ // A virtualised Metal device pins every GGUF request to the CPU before either comparator runs, so
+ // placement cannot tell two requests apart there.
status.gpu_placement_paravirtual === true ||
cpuFallbackPlacementPreserved(config, status, standing);
- // The diffusion runner takes no --parallel, no batch sizes and no pass-through args, so
- // the backend does not compare them and the status nulls what it publishes at all.
+ // The diffusion runner takes no --parallel, no batch sizes and no pass-through args, so the
+ // backend does not compare them and the status nulls them.
const diffusion = status.is_diffusion === true;
return SETTING_CHECKS.every(
(check) =>
- // The non-GGUF branch of /load checks identity and the MLX pair, then answers
- // already_loaded, so no llama.cpp invocation field may decide against one.
+ // The non-GGUF branch of /load checks identity and the MLX pair, then answers already_loaded, so
+ // no llama.cpp invocation field may decide against one.
(status.is_gguf === false && !check.mlxComparable) ||
(diffusion && check.chatOnly) ||
(diffusion && check.ggufPlacement) ||
diff --git a/studio/frontend/src/features/chat/lib/resident-model-match.ts b/studio/frontend/src/features/chat/lib/resident-model-match.ts
index fade1864800..c3a77559e4f 100644
--- a/studio/frontend/src/features/chat/lib/resident-model-match.ts
+++ b/studio/frontend/src/features/chat/lib/resident-model-match.ts
@@ -10,8 +10,8 @@ import {
residentModelIdMatches,
} from "@/features/hub/lib/model-identity";
-/** The two names one pick answers to: its picker row's catalog id, and the `model_path`
- * its load sends. They differ whenever a cached row pins a snapshot dir. */
+/** The two names one pick answers to: its picker row's catalog id, and the `model_path` its load
+ * sends. They differ whenever a cached row pins a snapshot dir. */
export type ModelPickNames = {
id: string;
loadPath?: string | null;
@@ -25,14 +25,10 @@ export type ResidentModelStatus = {
gguf_variant?: string | null;
};
-/**
- * Whether the resident model is the one this pick names.
- *
- * Each side holds two strings for one model and they need not be equal: a pinned cached row
- * loads by snapshot path while its row keeps the repo id, and the status publishes the public
- * id beside the raw one. Recognising that here skips a reload `/load` would have answered
- * `already_loaded`, and the confirmation that goes with it.
- */
+/** Whether the resident model is the one this pick names. Each side holds two strings for one
+ * model and they need not be equal: a pinned cached row loads by snapshot path while its row
+ * keeps the repo id, and the status publishes the public id beside the raw one. Recognising
+ * that skips a reload `/load` would have answered `already_loaded`. */
export function residentModelMatchesPick(
status: ResidentModelStatus,
pick: ModelPickNames,
@@ -52,13 +48,13 @@ export function residentModelMatchesPick(
) {
return false;
}
- // Only the raw identifier says WHICH weights are resident, as matches_load_source does:
- // every snapshot of a repo publishes the same public id.
+ // Only the raw identifier says WHICH weights are resident, as matches_load_source does: every
+ // snapshot of a repo publishes the same public id.
if (status.model_identifier) {
return modelIdsMatch(status.model_identifier, pick.loadPath ?? pick.id);
}
- // No raw identifier. The name this pick loads by still names one revision, so try it
- // literally first: an older backend put the raw path in active_model.
+ // No raw identifier. The name this pick loads by still names one revision, so try it literally
+ // first: an older backend put the raw path in active_model.
const loadName = pick.loadPath ?? pick.id;
if (modelIdsMatch(status.active_model, loadName)) {
return true;
diff --git a/studio/frontend/src/features/chat/lib/resolve-batch-size-seed.ts b/studio/frontend/src/features/chat/lib/resolve-batch-size-seed.ts
index 57bc00e82f9..c649f7c49c9 100644
--- a/studio/frontend/src/features/chat/lib/resolve-batch-size-seed.ts
+++ b/studio/frontend/src/features/chat/lib/resolve-batch-size-seed.ts
@@ -3,12 +3,9 @@
// what a fresh /api/inference/status does to one load control/baseline pair
-/**
- * Generic in the value type because the rule is about the ECHO, not about batch
- * sizes: the string tuning controls beside them (load mode, draft cache dtype)
- * follow the same steady-echo, dirty-control and model-change logic. Defaults to
- * number, so existing call sites read unchanged.
- */
+/** Generic in the value type because the rule is about the ECHO, not about batch sizes: the string
+ * tuning controls beside them follow the same steady-echo, dirty-control and model-change logic.
+ * Defaults to number, so existing call sites read unchanged. */
export interface BatchSizeSeedState<T extends number | string = number> {
/** The editable control: what the next load or Apply would send. */
value: T | null;
@@ -29,10 +26,9 @@ export function resolveBatchSizeSeed<T extends number | string = number>(options
previous: BatchSizeSeedState<T>;
/** No load of this tab's own is in flight (``!modelLoading``). */
seedLoadParams: boolean;
- /** The model/variant changed underneath this tab. Nothing staged against the
- * previous model may survive it, so the new model's echo is the whole truth:
- * the control adopts it even when a pending edit would otherwise hold, and
- * even when the new model happens to report the count the old one ran. */
+ /** The model/variant changed underneath this tab. Nothing staged against the previous model may
+ * survive it, so the new model's echo is the whole truth: the control adopts it even when a
+ * pending edit would otherwise hold, and even when the new model reports the old count. */
modelChanged?: boolean;
}): BatchSizeSeed<T> {
const {
@@ -49,9 +45,9 @@ export function resolveBatchSizeSeed<T extends number | string = number>(options
// a non-gguf has no batch flags; an absent field is an older backend saying nothing
const effective = isGguf ? incoming : null;
if (effective === undefined) {
- // Nothing to adopt, and nothing staged or recorded against the model that just
- // left may survive it: the baseline goes with the control, or a later rollback
- // resends the departed model's batch as if the new server were running it.
+ // Nothing to adopt, and nothing staged or recorded against the model that just left may survive
+ // it: the baseline goes with the control, or a later rollback resends the departed model's batch
+ // as if the new server were running it.
return modelChanged ? { value: null, loaded: null } : {};
}
// steady echo: an ordinary poll must not touch anything
diff --git a/studio/frontend/src/features/chat/lib/resolve-chat-template-seed.ts b/studio/frontend/src/features/chat/lib/resolve-chat-template-seed.ts
index 8b13d7ea9ee..f438e8923f2 100644
--- a/studio/frontend/src/features/chat/lib/resolve-chat-template-seed.ts
+++ b/studio/frontend/src/features/chat/lib/resolve-chat-template-seed.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// What a fresh /api/inference/status does to the chat-template override pair. Its own
-// module, importing neither the store nor a barrel, so the rules are testable off a browser.
+// What a fresh /api/inference/status does to the chat-template override pair. Its own module,
+// importing neither the store nor a barrel, so the rules are testable off a browser.
export interface ChatTemplateSeedState {
/** The editable control: what the next load or Apply would send. */
@@ -30,8 +30,8 @@ export function resolveChatTemplateSeed(options: {
}): ChatTemplateSeed {
const { incoming, previous, hydratingExistingModel, seedLoadParams } =
options;
- // While a load is in flight performLoad owns the load params, and an older backend
- // omitting the field says nothing about the template.
+ // While a load is in flight performLoad owns the load params, and an older backend omitting the
+ // field says nothing about the template.
if (incoming === undefined || !seedLoadParams) {
return {};
}
@@ -44,16 +44,15 @@ export function resolveChatTemplateSeed(options: {
loadedChatTemplateOverride: incoming,
};
}
- // Same checkpoint and quant, steady template: an ordinary poll must not touch the
- // control, or it would overwrite a staged edit every refresh.
+ // Same checkpoint and quant, steady template: an ordinary poll must not touch the control, or it
+ // would overwrite a staged edit every refresh.
if (sameTemplate(incoming, previous.loadedChatTemplateOverride)) {
return {};
}
- // The resident template moved under this tab -- another tab or an API caller reloaded
- // the same checkpoint with a different override. The baseline is a fact about the
- // running server, so it always advances (the rollback resends it). The control follows
- // only while it still sits on the old baseline; a genuinely dirty one keeps its intent,
- // exactly as the GPU group above preserves an unapplied placement edit.
+ // The resident template moved under this tab: another tab or an API caller reloaded the same
+ // checkpoint with a different override. The baseline is a fact about the running server, so it
+ // always advances, since the rollback resends it. The control follows only while it still sits
+ // on the old baseline; a genuinely dirty one keeps its intent, as the GPU group does.
const controlIsDirty = !sameTemplate(
previous.chatTemplateOverride,
previous.loadedChatTemplateOverride,
diff --git a/studio/frontend/src/features/chat/lib/resolve-ctx-pin-seed.ts b/studio/frontend/src/features/chat/lib/resolve-ctx-pin-seed.ts
index 176ee696764..071ba822f4d 100644
--- a/studio/frontend/src/features/chat/lib/resolve-ctx-pin-seed.ts
+++ b/studio/frontend/src/features/chat/lib/resolve-ctx-pin-seed.ts
@@ -3,11 +3,9 @@
// what a fresh /api/inference/status does to the Context Length control and its baseline
-/**
- * The subset of the pair this rule owns -- the editable Context Length (null =
- * Auto) and the value the resident server was invoked with, as this tab last saw
- * it -- to write back. Empty means leave the store alone.
- */
+/** The subset of the pair this rule owns -- the editable Context Length (null = Auto) and the
+ * value the resident server was invoked with, as this tab last saw it -- to write back.
+ * Empty means leave the store alone. */
export interface CtxPinSeed {
customContextLength?: number | null;
loadedCustomContextLength?: number | null;
@@ -18,32 +16,17 @@ const CLEAR: CtxPinSeed = {
loadedCustomContextLength: null,
};
-/**
- * Status reports `requested_n_ctx` (the n_ctx the running server was launched
- * with) and nothing about whether a human chose it, and the two are not the same
- * question: `resolveLoadMaxSeqLength` sends the resolved context on a same-model
- * reload so the reload does not resize, so an Auto reload under a custom or
- * modified preset reports a positive `requested_n_ctx` exactly like an explicit
- * pin does. A positive value is therefore evidence of nothing on its own, and no
- * backend field could fix that: the frontend sends the same number in both
- * cases, so the backend has no more information than this does.
- *
- * Two exceptions, both where the echo says more than "positive". Under Manual
- * memory with Auto layers the load sends its pin through
- * `resolveFitMaxSeqLength`, which puts 0 on the wire for Auto without exception,
- * so a positive echo there IS an explicit pin and is adopted. And an echo that
- * contradicts the pin this tab recorded means another client reloaded the same
- * model at a different context, so the baseline is dropped rather than resent.
- *
- * Otherwise this never invents a pin out of a positive echo. It clears one only on the
- * unambiguous signal (0, the wire value for Auto, which only an Auto load
- * sends), it leaves the control alone entirely while the model has not changed
- * -- whatever this tab's own load recorded there is better evidence than the
- * echo -- and on a model change, where nothing in the store belongs to the model
- * that arrived, it adopts that model's SAVED Context Length only when the
- * running server corroborates it by matching. That last step is the rule the
- * batch sizes next door already use for their own remembered values.
- */
+/** Status reports `requested_n_ctx` and nothing about whether a human chose it, and the two are
+ * not the same question: `resolveLoadMaxSeqLength` sends the resolved context on a same-model
+ * reload so the reload does not resize, so an Auto reload under a custom preset reports a
+ * positive echo exactly like an explicit pin does. No backend field could fix that, since the
+ * frontend sends the same number in both cases. Two exceptions, where the echo says more than
+ * "positive": under Manual memory with Auto layers the load puts 0 on the wire for Auto
+ * without exception, so a positive echo IS a pin; and an echo contradicting the recorded pin
+ * means another client reloaded at a different context, so the baseline is dropped.
+ * Otherwise this never invents a pin, clears one only on the unambiguous 0, leaves the
+ * control alone while the model has not changed, and on a model change adopts the saved
+ * Context Length only when the running server matches it. */
export function resolveCtxPinSeed(options: {
/** ``status.requested_context_length``; undefined on a backend that omits the field. */
incoming: number | null | undefined;
@@ -77,19 +60,17 @@ export function resolveCtxPinSeed(options: {
gpuLayers,
loadedPin,
} = options;
- // while a load is in flight performLoad owns the load params. This is also the
- // mid-load window where status still answers for the OUTGOING model (refreshes
- // do run with modelLoading true), so nothing here can plant a stale pin.
+ // While a load is in flight performLoad owns the load params. This is also the mid-load window
+ // where status still answers for the OUTGOING model, so nothing here can plant a stale pin.
if (!seedLoadParams) return {};
// a non-GGUF has no n_ctx: its max_seq_length must not be left standing as one
if (!isGguf) return CLEAR;
if (incoming === undefined) {
- // An older backend saying nothing. Nothing recorded against the model that
- // just left may survive it, but a steady poll must not touch the control.
+ // An older backend saying nothing. Nothing recorded against the model that just left may
+ // survive it, but a steady poll must not touch the control.
return modelChanged ? CLEAR : {};
}
- // 0 is the wire value for Auto and only an Auto load sends it, so this is the
- // one echo that proves its own meaning.
+ // 0 is the wire value for Auto and only an Auto load sends it, so this is the one echo that proves its own meaning.
if (incoming === null || !(incoming > 0)) return CLEAR;
// Unambiguous on MLX, so it is adopted without needing a saved config to corroborate
// it: another tab or an API client leaving a pinned model resident would otherwise
@@ -108,27 +89,18 @@ export function resolveCtxPinSeed(options: {
return { customContextLength: incoming, loadedCustomContextLength: incoming };
}
if (!modelChanged) {
- // Same model, ambiguous echo. What is in the store came from watching this
- // tab's own load, which knew what the user actually asked for; an echo that
- // an Auto reload produces just as readily is not grounds to overwrite it,
- // in either direction.
- //
- // Unless it CONTRADICTS the record. Another tab or an API client can reload
- // the same model at a different context, and then the pin here describes an
- // invocation that is no longer running: keeping it means the next unrelated
- // Apply resends it and silently takes back the other client's choice. The
- // echo still cannot say whether a human chose the new value, so it is not
- // adopted; the stale baseline is dropped instead, which leaves the control
- // on Auto and the next Apply agreeing with the server rather than fighting
- // it. Clearing is safe against an Auto reload of our own pinned model too:
- // that reports the resolved context, which IS the pin, so it matches here
- // and nothing moves.
+ // Same model, ambiguous echo. What is in the store came from watching this tab's own load,
+ // which knew what the user asked for; an echo an Auto reload produces just as readily is not
+ // grounds to overwrite it. Unless it CONTRADICTS the record: another client can reload the
+ // same model at a different context, and keeping the pin means the next Apply silently takes
+ // back that choice. The echo still cannot say whether a human chose the new value, so the
+ // stale baseline is dropped instead, leaving the control on Auto. Safe against an Auto reload
+ // of our own pinned model, which reports the resolved context and so matches.
return loadedPin != null && loadedPin !== incoming ? CLEAR : {};
}
- // A model changed underneath this tab, so nothing in the store belongs to the
- // one that arrived. Its saved config is the only record of intent left;
- // require the running server to match it, or a stale pin would be re-pinned
- // onto a server that is not running it.
+ // A model changed underneath this tab, so nothing in the store belongs to the one that arrived.
+ // Its saved config is the only record of intent left; require the running server to match it,
+ // or a stale pin would be re-pinned onto a server that is not running it.
return remembered != null && remembered > 0 && remembered === incoming
? {
customContextLength: remembered,
diff --git a/studio/frontend/src/features/chat/lib/resolve-vision-switch-seed.ts b/studio/frontend/src/features/chat/lib/resolve-vision-switch-seed.ts
index d928e62cc96..56360c032e0 100644
--- a/studio/frontend/src/features/chat/lib/resolve-vision-switch-seed.ts
+++ b/studio/frontend/src/features/chat/lib/resolve-vision-switch-seed.ts
@@ -11,25 +11,14 @@ export interface VisionSwitchSeedState {
loadedVisionDisabledByUser: boolean | null;
}
-/**
- * Whether a status poll may write the incoming value into the editable control.
- *
- * Three cases say yes:
- *
- * 1. Never seeded, so the control holds a default nobody chose.
- * 2. The model or variant changed under this tab, so the control belongs to the
- * model that just left.
- * 3. The RUNNING server's value moved while this tab was idle -- another tab or
- * an API client reloaded the same model with the opposite setting. Without
- * this, `loadedDisableVision` and the image gate follow the new server while
- * the control keeps the old value, so Advanced Settings shows the opposite of
- * the running projector and the next Apply quietly undoes the external change.
- *
- * The exception to case 3 is a genuinely pending local edit: a control that has
- * been moved away from its loaded baseline is the user's unapplied intent, and a
- * poll must not overwrite it. That is why "no pending edit" is `disableVision ===
- * loadedDisableVision` against the OLD baseline, not against the incoming value.
- */
+/** Whether a status poll may write the incoming value into the editable control. Three cases say
+ * yes: never seeded, so the control holds a default nobody chose; the model or variant changed
+ * under this tab; or the RUNNING server's value moved while this tab was idle, another client
+ * having reloaded the same model with the opposite setting. Without the third,
+ * `loadedDisableVision` and the image gate follow the new server while the control keeps the old
+ * value, so Advanced Settings shows the opposite of the running projector. The exception is a
+ * genuinely pending local edit, which is why "no pending edit" is compared against the OLD
+ * baseline, not against the incoming value. */
export function shouldSeedVisionSwitch(args: {
incoming: boolean;
previous: VisionSwitchSeedState;
diff --git a/studio/frontend/src/features/chat/lib/server-tuning-fields.ts b/studio/frontend/src/features/chat/lib/server-tuning-fields.ts
index 74960be235b..be137b67771 100644
--- a/studio/frontend/src/features/chat/lib/server-tuning-fields.ts
+++ b/studio/frontend/src/features/chat/lib/server-tuning-fields.ts
@@ -1,14 +1,10 @@
// 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 four llama-server tuning knobs every GGUF load carries: --load-mode, the
- * draft context's KV cache dtype, --ctx-checkpoints and --cache-ram.
- *
- * One module because they are always sent, committed and cleared as a group, and
- * every load path would otherwise repeat the same four lines with three chances
- * to forget one. State still lives in the runtime store.
- */
+/** The four llama-server tuning knobs every GGUF load carries: --load-mode, the draft context's
+ * KV cache dtype, --ctx-checkpoints and --cache-ram. One module because they are always sent,
+ * committed and cleared as a group, and every load path would otherwise repeat the same four
+ * lines with three chances to forget one. State still lives in the runtime store. */
/** The subset of a config these read; satisfied by PerModelConfig and the store. */
export interface ServerTuningValues {
@@ -26,11 +22,9 @@ export interface ServerTuningPayload {
cache_ram?: number;
}
-/**
- * What to send on /load, blank knobs omitted rather than nulled: the route reads
- * ``model_fields_set`` to decide whether the control owns the flag, and a null
- * counts as set, stripping the flag out of any inherited extra arguments.
- */
+/** What to send on /load, blank knobs omitted rather than nulled: the route reads
+ * `model_fields_set` to decide whether the control owns the flag, and a null counts as set,
+ * stripping the flag out of any inherited extra arguments. */
export function serverTuningLoadPayload(
values: ServerTuningValues,
): ServerTuningPayload {
@@ -58,11 +52,9 @@ export interface ServerTuningState {
loadedCacheRam: number | null;
}
-/**
- * What a launch committed: click-time values, not a backend echo, like the batch
- * sizes. Diffusion commits nothing (it launches no llama-server, and a value
- * recorded here would ride a saved preset onto the next GGUF).
- */
+/** What a launch committed: click-time values, not a backend echo, like the batch sizes. Diffusion
+ * commits nothing, since it launches no llama-server and a value recorded here would ride a
+ * saved preset onto the next GGUF. */
export function committedServerTuningState(
values: ServerTuningValues,
isDiffusion = false,
@@ -86,10 +78,8 @@ export function committedServerTuningState(
};
}
-/**
- * The pairs a load that sent none of them leaves behind. Both halves, or a
- * rollback re-sends the departed model's baseline as if this server ran it.
- */
+/** The pairs a load that sent none of them leaves behind. Both halves, or a rollback re-sends the
+ * departed model's baseline as if this server ran it. */
export function clearedServerTuningState(): ServerTuningState {
return {
loadMode: null,
diff --git a/studio/frontend/src/features/chat/lib/server-wide-reload.ts b/studio/frontend/src/features/chat/lib/server-wide-reload.ts
index c69836c0b05..44d5297d5e4 100644
--- a/studio/frontend/src/features/chat/lib/server-wide-reload.ts
+++ b/studio/frontend/src/features/chat/lib/server-wide-reload.ts
@@ -13,20 +13,13 @@ function declines(hint: ReloadHint): boolean {
return hint === "unknown" || (hint !== "unsupported" && hint.reloadRequired);
}
-/**
- * Whether a server-wide setting changed since the resident child launched.
- *
- * `adopt_load_intent_if_matched` refuses to reuse a running server when the Model Memory
- * policy no longer describes it, or when the VRAM budget moved. Neither is part of the load
- * intent, so identity and per-model config both still match and the resident shortcut would
- * skip a `/load` the backend was going to honour.
- *
- * An unknown answer declines. The two failures are not symmetric: one extra reload costs
- * the prompt this PR removes, while adopting on a read that failed after the user changed
- * a policy leaves the child on the old one with nothing on screen to say so. An absent
- * route is not an unknown answer, and keeps the shortcut working on a backend too old to
- * report either.
- */
+/** Whether a server-wide setting changed since the resident child launched.
+ * `adopt_load_intent_if_matched` refuses to reuse a running server when the Model Memory policy
+ * no longer describes it, or when the VRAM budget moved. Neither is part of the load intent, so
+ * identity and per-model config both still match and the resident shortcut would skip a `/load`
+ * the backend was going to honour. An unknown answer declines: one extra reload costs the prompt
+ * this removes, while adopting on a failed read leaves the child on the old policy with nothing
+ * on screen to say so. An absent route is not an unknown answer. */
export function serverWideReloadRequired(signals: {
modelMemory: ReloadHint;
vramBudget: ReloadHint;
diff --git a/studio/frontend/src/features/chat/lib/speech-only-status.ts b/studio/frontend/src/features/chat/lib/speech-only-status.ts
index 6b6a3906f95..96e78b06863 100644
--- a/studio/frontend/src/features/chat/lib/speech-only-status.ts
+++ b/studio/frontend/src/features/chat/lib/speech-only-status.ts
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Its own module so the node suite can drive it: apply-inference-status-to-store pulls
-// in the whole chat runtime store.
+// Its own module so the node suite can drive it: apply-inference-status-to-store pulls in the whole chat runtime store.
export type SpeechOnlyStatusInput = {
active_model?: string | null;
@@ -10,14 +9,10 @@ export type SpeechOnlyStatusInput = {
audio_type?: string | null;
};
-/**
- * Whether the resident model emits speech. The Audio page loads these into the single
- * slot chat reads, and openai_chat_completions answers a turn on one by SYNTHESIZING
- * the prompt, so chat must never adopt one.
- *
- * The same condition that route branches on, plus audio_vlm: Gemma 3n should never set
- * `is_audio`, and misreading one would lock a real chat model out of chat.
- */
+/** Whether the resident model emits speech. The Audio page loads these into the single slot chat
+ * reads, and openai_chat_completions answers a turn on one by SYNTHESIZING the prompt, so chat
+ * must never adopt one. The same condition that route branches on, plus audio_vlm: Gemma 3n
+ * should never set `is_audio`, and misreading one would lock a real chat model out of chat. */
export function isSpeechOnlyStatus(status: SpeechOnlyStatusInput): boolean {
return (
Boolean(status.is_audio) &&
diff --git a/studio/frontend/src/features/chat/local-model-options.ts b/studio/frontend/src/features/chat/local-model-options.ts
index 01ad43d754f..edad6d574c6 100644
--- a/studio/frontend/src/features/chat/local-model-options.ts
+++ b/studio/frontend/src/features/chat/local-model-options.ts
@@ -4,13 +4,10 @@
import type { LocalModelInfo } from "@/features/hub";
import type { LoraModelOption } from "@/features/model-picker";
-/** The device-inventory sources Chat lists as local models.
- *
- * Deliberately the same set as the picker's `PICKER_LOCAL_SOURCES`. `/api/hub/local`
- * scans read-only, so an Ollama row's id is an opaque `ollama-manifest:` reference;
- * POST /load resolves it through `materialize_ollama_model_ref`, which creates the
- * `.gguf` link on demand, so the reference is loadable as-is.
- */
+/** The device-inventory sources Chat lists as local models, deliberately the same set as the
+ * picker's `PICKER_LOCAL_SOURCES`. `/api/hub/local` scans read-only, so an Ollama row's id is an
+ * opaque `ollama-manifest:` reference; POST /load resolves it through
+ * `materialize_ollama_model_ref`, which creates the `.gguf` link on demand. */
const CHAT_LOCAL_SOURCES: ReadonlySet<LocalModelInfo["source"]> = new Set([
"lmstudio",
"models_dir",
@@ -31,14 +28,11 @@ function baseModelLabel(source: LocalModelInfo["source"]): string {
}
}
-/** Chat's local model options, one per load id.
- *
- * The shared inventory keys a row on (format, path), so a directory holding both GGUF and
- * safetensors weights arrives as two rows with distinct `inventory_id` but the SAME `id`.
- * The selector keys on `id` (`key={adapter.id}`, `selected={value === adapter.id}`), so
- * both rows would collide on a React key and render as selected together. The compat
- * endpoint returned one row per directory, so collapsing on `id` keeps that behaviour.
- */
+/** Chat's local model options, one per load id. The shared inventory keys a row on (format, path),
+ * so a directory holding both GGUF and safetensors weights arrives as two rows with distinct
+ * `inventory_id` but the SAME `id`. The selector keys on `id`, so both rows would collide on a
+ * React key and render as selected together. The compat endpoint returned one row per directory,
+ * so collapsing on `id` keeps that behaviour. */
export function chatLocalModelOptions(
rows: readonly LocalModelInfo[],
): LoraModelOption[] {
diff --git a/studio/frontend/src/features/chat/mcp-composer-button.tsx b/studio/frontend/src/features/chat/mcp-composer-button.tsx
index 8dcf7c07744..f4c8fe3cccf 100644
--- a/studio/frontend/src/features/chat/mcp-composer-button.tsx
+++ b/studio/frontend/src/features/chat/mcp-composer-button.tsx
@@ -48,8 +48,8 @@ type McpPreset = {
disablesWebSearch?: boolean; // turn the built-in Search pill off when enabled
};
-// Keyless remote MCP presets (rate-limited free tiers, no API key).
-// Hugging Face runs anonymously; add a token via "Manage MCP servers".
+// Keyless remote MCP presets (rate-limited free tiers, no API key). Hugging Face runs
+// anonymously; add a token via "Manage MCP servers".
const MCP_PRESETS: readonly McpPreset[] = [
{
id: "unsloth-docs",
@@ -69,8 +69,7 @@ const MCP_PRESETS: readonly McpPreset[] = [
},
] as const;
-// mcp_servers has no UNIQUE(url); dedupe by normalized URL so a preset toggle
-// reuses its row instead of duplicating.
+// mcp_servers has no UNIQUE(url); dedupe by normalized URL so a preset toggle reuses its row instead of duplicating.
function normalizeMcpUrl(url: string): string {
return (url || "").trim().toLowerCase().replace(/\/+$/, "");
}
@@ -106,8 +105,8 @@ export function McpComposerButton({
const listRefreshGenerationRef = useRef(0);
const hasLoadedServerSnapshotRef = useRef(false);
- // Grey out only when a loaded model lacks tool support; with no model yet,
- // MCP can still be pre-selected, like the other composer tools.
+ // Grey out only when a loaded model lacks tool support; with no model yet, MCP can still be
+ // pre-selected, like the other composer tools.
const usable = !modelLoaded || supportsTools;
const refresh = useCallback(
@@ -158,8 +157,8 @@ export function McpComposerButton({
};
}, [refresh]);
- // Load the server list on mount, and again when the dialog closes: it can
- // be opened from the chord as well as from this menu.
+ // Load the server list on mount, and again when the dialog closes: it can be opened from the
+ // chord as well as from this menu.
useEffect(() => {
if (dialogOpen) return;
let cancelled = false;
diff --git a/studio/frontend/src/features/chat/permission-mode-select.tsx b/studio/frontend/src/features/chat/permission-mode-select.tsx
index 0d9566dbc6c..bdcb262dbe1 100644
--- a/studio/frontend/src/features/chat/permission-mode-select.tsx
+++ b/studio/frontend/src/features/chat/permission-mode-select.tsx
@@ -33,10 +33,8 @@ import {
useChatRuntimeStore,
} from "./stores/chat-runtime-store";
-/**
- * Permission levels for tool calls. Full access stays last because it disables
- * both approval prompts and the code sandbox.
- */
+/** Permission levels for tool calls. Full access stays last because it disables both approval
+ * prompts and the code sandbox. */
export const PERMISSION_MODE_OPTIONS: readonly {
value: PermissionMode;
label: string;
@@ -84,9 +82,8 @@ export function permissionModeOption(mode: PermissionMode) {
);
}
-/** The option rows shared by every permission dropdown/submenu. Non-full
- * levels apply directly; picking Full access must go through the caller's
- * danger confirmation, so it's a separate callback. */
+/** The option rows shared by every permission dropdown or submenu. Non-full levels apply
+ * directly; picking Full access must go through the caller's danger confirmation. */
export function PermissionModeMenuItems({
onRequestFullAccess,
}: {
@@ -138,8 +135,8 @@ export function PermissionModeMenuItems({
);
}
-/** Danger confirmation shown before Full access turns on. Self-contained so
- * the dropdown works outside the chat page (e.g. the Settings dialog). */
+/** Danger confirmation shown before Full access turns on. Self-contained so the dropdown works
+ * outside the chat page (e.g. the Settings dialog). */
export function FullAccessConfirmDialog({
open,
onOpenChange,
@@ -176,10 +173,8 @@ export function FullAccessConfirmDialog({
);
}
-/**
- * Select-style dropdown (like the MCP composer menu) for picking the
- * permission level. Used in General settings and the chat settings sheet.
- */
+/** Select-style dropdown (like the MCP composer menu) for picking the permission level. Used in
+ * General settings and the chat settings sheet. */
export function PermissionModeDropdown({
side = "bottom",
align = "end",
@@ -227,8 +222,8 @@ export function PermissionModeDropdown({
How should tool calls be approved?
</DropdownMenuLabel>
<PermissionModeMenuItems
- // Defer past the menu-close focus restoration so the dialog's
- // focus trap isn't broken by the dropdown grabbing focus back.
+ // Defer past the menu-close focus restoration so the dialog's focus trap is not broken by the
+ // dropdown grabbing focus back.
onRequestFullAccess={() =>
setTimeout(() => setConfirmOpen(true), 0)
}
@@ -243,13 +238,10 @@ export function PermissionModeDropdown({
);
}
-/**
- * Composer pill (mirrors the MCP pill) showing the current permission level
- * in the chat box; clicking opens the level dropdown. Danger-styled while
- * Full access is on. The Full access pick routes through the store-driven
- * BypassPermissionsConfirmDialog mounted at the chat-page root, so the
- * warning survives this menu unmounting.
- */
+/** Composer pill showing the current permission level in the chat box; clicking opens the level
+ * dropdown. Danger-styled while Full access is on. The Full access pick routes through the
+ * store-driven confirm dialog mounted at the chat-page root, so the warning survives this
+ * menu unmounting. */
export function PermissionModeComposerPill({
side = "bottom",
}: {
diff --git a/studio/frontend/src/features/chat/presets/preset-load-config.ts b/studio/frontend/src/features/chat/presets/preset-load-config.ts
index f912f822e3d..6e5b73e0563 100644
--- a/studio/frontend/src/features/chat/presets/preset-load-config.ts
+++ b/studio/frontend/src/features/chat/presets/preset-load-config.ts
@@ -176,8 +176,8 @@ export function normalizePresetLoadConfig(
typeof partial.nUbatch === "number" && Number.isFinite(partial.nUbatch)
? Math.max(N_BATCH_MIN, Math.min(N_BATCH_MAX, Math.round(partial.nUbatch)))
: null,
- // Through the same normalizers the per-model store uses, so a hand-edited or
- // older preset cannot smuggle in a mode or dtype the panel cannot show.
+ // Through the same normalizers the per-model store uses, so a hand-edited or older preset cannot
+ // smuggle in a mode or dtype the panel cannot show.
loadMode: canonicalizeLoadMode(partial.loadMode),
specDraftCacheDtype:
typeof partial.specDraftCacheDtype === "string" &&
diff --git a/studio/frontend/src/features/chat/presets/preset-policy.ts b/studio/frontend/src/features/chat/presets/preset-policy.ts
index cc1b7bfea65..01fe98f801e 100644
--- a/studio/frontend/src/features/chat/presets/preset-policy.ts
+++ b/studio/frontend/src/features/chat/presets/preset-policy.ts
@@ -497,12 +497,10 @@ export function unpinnedLoadContext(
return isGgufLoad || isMlx ? 0 : appDefault;
}
-/**
- * Adjust a resolved max-seq-length for the GPU Memory mode. Under Manual + Auto
- * layers (GGUF, gpuLayers < 0) llama.cpp's --fit owns context sizing, so send 0
- * (the backend omits -c) unless the user pinned a length; every other case keeps
- * the resolved fallback. Shared by every GGUF load path so they can't drift.
- */
+/** Adjust a resolved max-seq-length for the GPU Memory mode. Under Manual with Auto layers (GGUF,
+ * gpuLayers < 0) llama.cpp's --fit owns context sizing, so send 0 (the backend omits -c) unless
+ * the user pinned a length; every other case keeps the resolved fallback. Shared by every GGUF
+ * load path so they cannot drift. */
export function resolveFitMaxSeqLength(
isGguf: boolean | null | undefined,
gpuMemoryMode: "auto" | "manual",
@@ -514,26 +512,14 @@ export function resolveFitMaxSeqLength(
return customContextLength && customContextLength > 0 ? customContextLength : 0;
}
-/**
- * The context pin a completed load leaves behind: the Context Length the user
- * EXPLICITLY set for it, or null for Auto.
- *
- * Takes the user's setting, never the n_ctx that went on the wire. The wire
- * value cannot answer this: `resolveLoadMaxSeqLength`'s isReloadingCurrentGguf
- * branch sends the resolved context on a same-model reload so the reload does
- * not resize, so under a custom or modified preset an Auto load puts a positive
- * n_ctx on the wire too. Reading a pin back out of that turned Auto into a
- * numeric pin at the current context on any same-model reload, after which a GPU
- * memory change could no longer auto-resize.
- *
- * Takes no GPU Memory mode, deliberately. `resolveLoadMaxSeqLength` honors a pin
- * in every mode, but the predicate this replaces kept one only under Manual +
- * Auto layers: a survival rule narrower than the send rule, so a load on Default
- * sent the user's pin and then dropped it on completion. That was the bug.
- *
- * Callers keep their own isGguf/targetIsGguf guard inline: a non-GGUF load has
- * no n_ctx, and its max_seq_length must not be pinned as one.
- */
+/** The context pin a completed load leaves behind: the Context Length the user EXPLICITLY set, or
+ * null for Auto. Takes the user's setting, never the n_ctx that went on the wire, which cannot
+ * answer this: `resolveLoadMaxSeqLength` sends the resolved context on a same-model reload, so
+ * under a custom preset an Auto load puts a positive n_ctx on the wire too, and reading a pin
+ * back out of that turned Auto into a numeric pin. Takes no GPU Memory mode deliberately: the
+ * predicate this replaces kept a pin only under Manual with Auto layers, narrower than the send
+ * rule, so a load on Default sent the user's pin and then dropped it. Callers keep their own
+ * isGguf guard inline, since a non-GGUF load has no n_ctx. */
export function resolveExplicitCtxPin(
customContextLength: number | null | undefined,
): number | null {
diff --git a/studio/frontend/src/features/chat/projects-page.tsx b/studio/frontend/src/features/chat/projects-page.tsx
index f3e300ed28d..59cd60b4a2b 100644
--- a/studio/frontend/src/features/chat/projects-page.tsx
+++ b/studio/frontend/src/features/chat/projects-page.tsx
@@ -84,8 +84,8 @@ const PROJECTS_INITIAL_FALLBACK = 8;
// Approx list row height in px, used to estimate how many rows fit the page.
const PROJECTS_ROW_HEIGHT = 68;
-// Modified column, matching a file-list feel: Today / Yesterday / N days ago,
-// then a short date once it is over a week old.
+// Modified column, matching a file-list feel: Today / Yesterday / N days ago, then a short date
+// once it is over a week old.
function formatModified(ts: number): string {
if (!Number.isFinite(ts)) return "";
const now = new Date();
@@ -249,8 +249,8 @@ export function ProjectsPage() {
);
return filtered;
}, [projects, query, sortMode]);
- // Default view shows as many rows as fit the page, then loads more as the
- // user scrolls near the bottom. Search always spans every project.
+ // Default view shows as many rows as fit the page, then loads more as the user scrolls near the
+ // bottom. Search always spans every project.
const isSearching = query.trim() !== "";
const visibleCount = baseFit + extraCount;
const visibleProjects = isSearching
@@ -266,8 +266,8 @@ export function ProjectsPage() {
window.dispatchEvent(new Event("unsloth:app-shell-ready"));
}, [hasLoaded]);
- // Estimate how many rows fit below the list's top so the first page fills the
- // screen without loading everything up front.
+ // Estimate how many rows fit below the list's top so the first page fills the screen without
+ // loading everything up front.
useEffect(() => {
function measure() {
const el = listRef.current;
@@ -284,8 +284,7 @@ export function ProjectsPage() {
return () => window.removeEventListener("resize", measure);
}, [hasLoaded]);
- // Infinite scroll: reveal another page-step whenever the sentinel near the
- // list bottom scrolls into view.
+ // Infinite scroll: reveal another page-step whenever the sentinel near the list bottom scrolls into view.
useEffect(() => {
const el = sentinelRef.current;
if (!el || !hasMore) return;
@@ -299,8 +298,8 @@ export function ProjectsPage() {
);
io.observe(el);
return () => io.disconnect();
- // Re-observe after each load so it keeps filling while the sentinel stays
- // in view (IntersectionObserver does not re-fire on a steady intersection).
+ // Re-observe after each load so it keeps filling while the sentinel stays in view
+ // (IntersectionObserver does not re-fire on a steady intersection).
}, [hasMore, visibleCount]);
function openProject(projectId: string) {
diff --git a/studio/frontend/src/features/chat/prompt-storage/mutation-lock.ts b/studio/frontend/src/features/chat/prompt-storage/mutation-lock.ts
index 6226c0d4faa..f1e5ab7e7e8 100644
--- a/studio/frontend/src/features/chat/prompt-storage/mutation-lock.ts
+++ b/studio/frontend/src/features/chat/prompt-storage/mutation-lock.ts
@@ -1,13 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// One in-flight mutation per row, tracked by id.
-//
-// The detail panes are keyed by row id, so selecting another row unmounts them.
-// A lock held in the pane comes back false on return, and since the save is an
-// unconditional PUT it can then land after a DELETE and write the deleted row
-// straight back. The set therefore lives above the key, and these are its two
-// transitions, kept here so they can be tested without a renderer.
+// One in-flight mutation per row, tracked by id. The detail panes are keyed by row id, so
+// selecting another row unmounts them and a lock held in the pane comes back false; since the
+// save is an unconditional PUT it can then land after a DELETE and write the deleted row back.
+// The set therefore lives above the key, and these are its two transitions.
export type LockSet = ReadonlySet<string>;
@@ -17,9 +14,9 @@ export function acquire(held: LockSet, id: string): [LockSet, boolean] {
return [new Set(held).add(id), true];
}
-// Prompts and prompt lists are separate tables with independent ids, so the same
-// id can name one of each and an import can produce that. One set keyed on the
-// raw id would then let a prompt's save disable the unrelated list's controls.
+// Prompts and prompt lists are separate tables with independent ids, so the same id can name one
+// of each and an import can produce that. One set keyed on the raw id would let a prompt's save
+// disable the unrelated list's controls.
export function lockKey(kind: "prompt" | "list", id: string): string {
return `${kind}:${id}`;
}
@@ -32,10 +29,9 @@ export function release(held: LockSet, id: string): LockSet {
return next;
}
-// A save is async and the editor stays usable while it runs, so by the time the
-// PUT resolves the draft may have moved on. Clearing it unconditionally throws
-// away whatever was typed meanwhile, which is newer than the server's copy and
-// is the user's most recent intent. Only clear what was actually sent.
+// A save is async and the editor stays usable while it runs, so by the time the PUT resolves the
+// draft may have moved on. Clearing it unconditionally throws away whatever was typed meanwhile,
+// which is the user's most recent intent. Only clear what was actually sent.
export function samePromptDraft(
a: { name: string; text: string },
diff --git a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
index b123003ffe9..b2686b6e7a9 100644
--- a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
+++ b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
@@ -205,8 +205,8 @@ function contentBlocksToText(content: unknown): string {
parts.push("[thinking]\n" + thinkText + "\n[/thinking]");
}
} else if (p.type === "tool-call") {
- // Keep base64 image payloads and sandbox card metadata out of every
- // export format: use the model-visible text (matches chat replay).
+ // Keep base64 image payloads and sandbox card metadata out of every export format: use the
+ // model-visible text, matching chat replay.
const result = toolResultModelText(
p.result,
typeof p.toolName === "string" ? p.toolName : undefined,
@@ -243,8 +243,8 @@ async function loadConversationMessages(
toast.info(emptyMessage);
return null;
}
- // No parentId = legacy flat thread (already DB createdAt-sorted); walking the
- // chain would invert order, so keep raw order.
+ // No parentId = legacy flat thread (already DB createdAt-sorted); walking the chain would invert
+ // order, so keep raw order.
const hasParentIds = raw.some((m) => (m as { parentId?: unknown }).parentId != null);
if (!hasParentIds) return raw;
return orderByParentChain(raw, { includeSiblings }) as typeof raw;
@@ -254,8 +254,7 @@ function exportTs(): string {
return new Date().toISOString().slice(0, 19).replace(/:/g, "-");
}
-// Attachments live in msg.attachments[].content, not msg.content, so flatten
-// both here or they'd be dropped on export.
+// Attachments live in msg.attachments[].content, not msg.content, so flatten both here or they'd be dropped on export.
function messageToText(msg: { content: unknown; attachments?: unknown }): string {
const parts: string[] = [];
const main = contentBlocksToText(msg.content);
@@ -263,8 +262,8 @@ function messageToText(msg: { content: unknown; attachments?: unknown }): string
if (Array.isArray(msg.attachments)) {
for (const attachment of msg.attachments as Array<{ content?: unknown }>) {
if (!attachment?.content) continue;
- // A paste carries a wrapper the same text never had when it fitted
- // inline, so strip it rather than exporting the marker.
+ // A paste carries a wrapper the same text never had when it fitted inline, so strip it rather
+ // than exporting the marker.
const attText = unwrapPastedTextContent(
contentBlocksToText(attachment.content),
);
@@ -274,8 +273,8 @@ function messageToText(msg: { content: unknown; attachments?: unknown }): string
return parts.join("\n\n");
}
-// Markdown counterpart to messageToText: same content and attachments, but each
-// part keeps its shape so the renderer can fence tool calls and collapse thinking.
+// Markdown counterpart to messageToText: same content and attachments, but each part keeps its
+// shape so the renderer can fence tool calls and collapse thinking.
function messageToMarkdown(msg: { content: unknown; attachments?: unknown }): string {
const normalizeToolResult = toolResultModelText;
const blocks = contentBlocksToMarkdownBlocks(msg.content, normalizeToolResult);
@@ -297,9 +296,9 @@ function messageToMarkdown(msg: { content: unknown; attachments?: unknown }): st
return renderConversationBlocks(blocks);
}
-// OpenAI messages array (tool-calling + multimodal fine-tuning): tool calls →
-// "tool_calls" + separate "role":"tool" messages; images → "image_url" parts;
-// audio dropped; thinking kept as a text part.
+// OpenAI messages array (tool-calling + multimodal fine-tuning): tool calls to "tool_calls" plus
+// separate "role":"tool" messages; images to "image_url" parts; audio dropped; thinking kept
+// as a text part.
type OAIContentPart =
| { type: "text"; text: string }
@@ -326,8 +325,7 @@ function messageToOpenAI(msg: { role: unknown; content: unknown; attachments?: u
...attachments.flatMap((a) => {
const att = a as { content?: unknown };
if (!Array.isArray(att.content)) return [];
- // Attachment text only: a message body is verbatim, and the paste
- // wrapper is not something the user wrote.
+ // Attachment text only: a message body is verbatim, and the paste wrapper is not something the user wrote.
return (att.content as Record<string, unknown>[]).map((part) =>
part?.type === "text" && typeof part.text === "string"
? { ...part, text: unwrapPastedTextContent(part.text) }
@@ -358,9 +356,8 @@ function messageToOpenAI(msg: { role: unknown; content: unknown; attachments?: u
);
toolCalls.push({ id, type: "function", function: { name, arguments: argsStr } });
if (p.result !== undefined && p.result !== null) {
- // Keep base64 image payloads out of exports: MCP image results carry
- // their model-visible text alongside the data, so serialize the text
- // (matching chat replay) instead of the full object.
+ // Keep base64 image payloads out of exports: MCP image results carry their model-visible text
+ // alongside the data, so serialize the text instead of the full object.
const modelText = toolResultModelText(p.result, name);
const resultStr =
typeof modelText === "string" ? modelText : JSON.stringify(modelText);
@@ -419,8 +416,8 @@ export async function exportConversationShareGPT(threadId: string): Promise<void
);
}
-// OpenAI/ChatML JSONL: {"messages": [{"role","content"}, ...]} per conversation;
-// Unsloth reads this as a ChatML dataset.
+// OpenAI/ChatML JSONL: {"messages": [{"role","content"}, ...]} per conversation; Unsloth reads
+// this as a ChatML dataset.
export async function exportConversationRawJsonl(threadId: string): Promise<void> {
return exportConversationJsonl(threadId, "training");
}
@@ -483,9 +480,8 @@ export const exportConversationMarkdown = createConversationMarkdownExporter({
notifyNoContent: () => toast.info("No exportable content."),
});
-// "skipped" is an empty conversation, which has already said so and must not
-// stop the rest of a pair; "failed" has toasted a reason, so stop there rather
-// than stack a second one.
+// "skipped" is an empty conversation, which has already said so and must not stop the rest of a
+// pair; "failed" has toasted a reason, so stop there rather than stack a second one.
type SaveSourceOutcome = "saved" | "skipped" | "failed";
async function saveConversationAsProjectSource(
@@ -500,8 +496,8 @@ async function saveConversationAsProjectSource(
const markdown = buildConversationMarkdown(
messages.map((msg) => ({
role: String(msg.role ?? ""),
- // As the markdown exporter does: a project source is retrieved back into
- // context, so the renderer's tokens must not be saved as prose.
+ // As the markdown exporter does: a project source is retrieved back into context, so the
+ // renderer's tokens must not be saved as prose.
content: stripSearchImageTokens(messageToMarkdown(msg)),
})),
);
@@ -540,12 +536,9 @@ export async function saveChatItemAsProjectSource(
}
}
-/**
- * A sidebar row as one markdown document, for the "Copy as Markdown" shortcut.
- * The halves of a compare pair are named the way saving them to project sources
- * names them, after their models: the two arrive in whichever order they last
- * answered in, so position alone would label them wrong.
- */
+/** A sidebar row as one markdown document. The halves of a compare pair are named after their
+ * models, as saving them to project sources does: the two arrive in whichever order they
+ * last answered in, so position alone would label them wrong. */
export async function buildChatItemMarkdown(item: {
id: string;
title: string;
@@ -685,9 +678,8 @@ export async function exportBulkConversationsSeparate(
await downloadBlob(zipped, `${basename}.zip`, "application/zip");
}
-// Scope-level bulk export shared by the sidebar Recents menu and
-// Settings -> Chat -> Data. "recents" = chats outside projects; "all" adds
-// project chats.
+// Scope-level bulk export shared by the sidebar Recents menu and Settings > Chat > Data.
+// "recents" = chats outside projects; "all" adds project chats.
export async function bulkExportConversationsByScope(
scope: "recents" | "all",
format: ConvExportFormat,
@@ -730,12 +722,9 @@ export async function exportProjectConversations(
);
}
-// ── Fine-tuning export ─────────────────────────────────────────────────────
-// One JSONL line per conversation: {"messages": [{"role", "content"}]} with
-// string-only content in system/user/assistant turns. Unsloth's training tab
-// detects this as ChatML natively (no column mapping, no standardization) and
-// it works with train-on-completions masking, which only trains on assistant
-// turns. Reasoning, tool calls, and images are dropped: clean SFT targets.
+// One JSONL line per conversation, string-only content in system/user/assistant turns: Unsloth's
+// training tab detects this as ChatML natively and it works with train-on-completions
+// masking. Reasoning, tool calls and images are dropped for clean SFT targets.
export type FineTuneMessage = {
role: "system" | "user" | "assistant";
@@ -750,8 +739,8 @@ function messageToPlainText(msg: {
attachments?: unknown;
}): string {
const parts: string[] = [];
- // Only attachment text is unwrapped: a message body is verbatim, and may
- // legitimately quote the wrapper syntax in a code sample.
+ // Only attachment text is unwrapped: a message body is verbatim, and may legitimately quote the
+ // wrapper syntax in a code sample.
const collect = (blocks: unknown, fromAttachment = false) => {
const normalize = fromAttachment
? unwrapPastedTextContent
@@ -795,11 +784,9 @@ function mergeSameRoleTurns(turns: FineTuneMessage[]): FineTuneMessage[] {
return merged;
}
-/** Conversation turns for fine-tuning, or null when the thread has no
- * usable user + assistant exchange. Consecutive same-role turns merge,
- * assistant turns before the first user turn drop (an assistant target
- * with no prompt teaches nothing), and trailing non-assistant turns drop
- * so chat templates format cleanly. */
+/** Conversation turns for fine-tuning, or null when the thread has no usable user + assistant
+ * exchange. Consecutive same-role turns merge, assistant turns before the first user turn
+ * drop (a target with no prompt teaches nothing), and trailing non-assistant turns drop. */
function messagesToFineTuneTurns(
messages: Array<{ role: unknown; content: unknown; attachments?: unknown }>,
): FineTuneMessage[] | null {
@@ -839,9 +826,9 @@ const SHAREGPT_FROM: Record<FineTuneMessage["role"], string> = {
assistant: "gpt",
};
-/** JSONL lines for one conversation in the chosen format. Alpaca is
- * single-turn, so each user to assistant pair becomes its own record with
- * the system prompt and earlier exchange carried in the input field. */
+/** JSONL lines for one conversation in the chosen format. Alpaca is single-turn, so each user to
+ * assistant pair becomes its own record with the system prompt and earlier exchange in the
+ * input field. */
function turnsToFineTuneLines(
turns: FineTuneMessage[],
format: FineTuneFormat,
@@ -903,8 +890,8 @@ export async function buildFineTuneJsonl(
const hasParentIds = raw.some(
(m) => (m as { parentId?: unknown }).parentId != null,
);
- // Chain only: retries/regenerations leave sibling branches, and mixing
- // alternate replies into one conversation corrupts the training targets.
+ // Chain only: retries/regenerations leave sibling branches, and mixing alternate replies into one
+ // conversation corrupts the training targets.
const ordered = hasParentIds
? (orderByParentChain(raw, { includeSiblings: false }) as typeof raw)
: raw;
@@ -943,8 +930,8 @@ export async function exportFineTuneJsonl(
return conversations;
}
-// ShareGPT training exports: prompt → one record (human turn + empty gpt slot);
-// list → one multi-turn record, each item a human turn.
+// ShareGPT training exports: prompt to one record (human turn + empty gpt slot); list to one
+// multi-turn record, each item a human turn.
function exportPromptTrainingJsonl(entry: PromptEntry): Promise<void> {
const record = {
conversations: [
@@ -1347,24 +1334,21 @@ function ExportModal({
);
}
-// Unsaved edits live in the parent keyed by entry id, so switching rows in the
-// rail never silently throws away what you typed.
+// Unsaved edits live in the parent keyed by entry id, so switching rows in the rail never
+// silently throws away what you typed.
type PromptDraft = { name: string; text: string };
type ListDraft = { name: string; items: string[] };
-// Factories, not shared constants: every reset would otherwise hand back the
-// same `items` array, one in-place edit from corrupting it for good.
+// Factories, not shared constants: every reset would otherwise hand back the same `items` array,
+// one in-place edit from corrupting it for good.
const emptyPromptDraft = (): PromptDraft => ({ name: "", text: "" });
const emptyListDraft = (): ListDraft => ({ name: "", items: ["", ""] });
-// Selecting the Lists tab auto-selects its first row, so the detail pane mounts
-// the editor with no click, and one controlled textarea per item makes that cost
-// grow faster than the item count: measured in Chromium, 100 items settle in
-// 247ms, 200 in 568ms, 500 in 2.5s and 2000 in 36s, against a flat 80ms on the
-// collapsed card this replaced. The backend accepts 10000 items in one list
-// (studio/backend/routes/prompts.py), which an import can produce, so past this
-// many the editor waits to be asked for. Nothing else does: save, run and export
-// all read the full items.
+// Selecting the Lists tab auto-selects its first row, and one controlled textarea per item makes
+// that cost grow faster than the item count: in Chromium 100 items settle in 247ms, 200 in 568ms,
+// 500 in 2.5s and 2000 in 36s, against a flat 80ms on the collapsed card this replaced. The backend
+// accepts 10000 items in one list (routes/prompts.py), which an import can produce, so past this
+// many the editor waits to be asked for; save, run and export all still read the full items.
const EDITOR_ROW_LIMIT = 100;
function relativeTime(ts: number): string {
@@ -1379,9 +1363,8 @@ function relativeTime(ts: number): string {
return new Date(ts).toLocaleDateString();
}
-// `current` drives the roving tabindex and stays true while a New form covers
-// the pane: without it every row is tabbable, so reaching the editor means
-// tabbing through the whole library.
+// `current` drives the roving tabindex and stays true while a New form covers the pane: without
+// it every row is tabbable, so reaching the editor means tabbing through the library.
function RailRow({
title,
preview,
@@ -1460,8 +1443,8 @@ function EmptyDetail({
);
}
-// Deleting a dirty entry destroys the stored copy and the only copy of the
-// unsaved draft at once, so that case asks first.
+// Deleting a dirty entry destroys the stored copy and the only copy of the unsaved draft at once,
+// so that case asks first.
function UnsavedDeleteConfirm({
open,
onOpenChange,
@@ -1541,15 +1524,14 @@ function PromptDetail({
[name, text, entry.name, entry.text, onDraftChange],
);
- // One mutation at a time, and the lock lives in the parent: this pane is
- // keyed by row id, so selecting another row unmounts it and a local lock
- // would come back false. PUT is an unconditional upsert, so a Save still in
- // flight can land after a DELETE and write the deleted row straight back.
+ // One mutation at a time, and the lock lives in the parent: this pane is keyed by row id, so
+ // selecting another row unmounts it and a local lock would come back false. PUT is an
+ // unconditional upsert, so a Save still in flight can land after a DELETE.
const handleSave = useCallback(async () => {
const trimText = text.trim();
if (!trimText) return;
- // Snapshot what is going to the server. The editor stays usable while the
- // request is in flight, so the draft can move on before it resolves.
+ // Snapshot what is going to the server: the editor stays usable while the request is in flight,
+ // so the draft can move on before it resolves.
const submitted: PromptDraft = { name, text };
await runMutation(lockKey("prompt", entry.id), async () => {
try {
@@ -1559,9 +1541,8 @@ function PromptDetail({
text: trimText,
updatedAt: now(),
});
- // Refresh before dropping the draft. Dropping it first uncovers the
- // entry this pane still holds, which is the pre-save copy, so the
- // editor flashes the old text until the fetch lands.
+ // Refresh before dropping the draft. Dropping it first uncovers the pre-save copy this pane still
+ // holds, so the editor flashes the old text until the fetch lands.
await onRefresh();
onSaved(submitted);
} catch (err) {
@@ -1583,17 +1564,17 @@ function PromptDetail({
return;
}
onDraftChange(undefined);
- // Refresh before clearing, or the keep-a-row-selected pass runs against a
- // list that still holds this row and can select the deleted entry back.
+ // Refresh before clearing, or the keep-a-row-selected pass runs against a list that still holds
+ // this row and can select the deleted entry back.
await onRefresh();
- // Id goes up so the parent only clears if this row is still selected: the
- // delete is awaited, and a click elsewhere meanwhile would be undone.
+ // Id goes up so the parent only clears if this row is still selected: the delete is awaited, and
+ // a click elsewhere meanwhile would be undone.
onDeleted(entry.id);
});
}, [entry.id, onDraftChange, onDeleted, onRefresh, runMutation]);
- // Export what the pane shows, normalised as saving would, or a dirty entry
- // writes a file that does not match the screen.
+ // Export what the pane shows, normalised as saving would, or a dirty entry writes a file that does
+ // not match the screen.
const exportValue: PromptEntry = dirty
? { ...entry, name: name.trim() || "Untitled Prompt", text: text.trim() }
: entry;
@@ -1697,11 +1678,10 @@ function PromptDetail({
);
}
-// A create has no row id yet, so it cannot use the by-id mutation lock above.
-// The ref is the authority for the same reason it is there: React state is not
-// readable straight after scheduling it. Call this above the New forms, never
-// inside one: selecting a rail row unmounts the form, and a guard that dies with
-// it comes back false while its request is still out.
+// A create has no row id yet, so it cannot use the by-id mutation lock above. The ref is the
+// authority because React state is not readable straight after scheduling it. Call this above
+// the New forms, never inside one: selecting a rail row unmounts the form, and a guard that
+// dies with it comes back false while its request is still out.
function useCreateGuard(): {
creating: boolean;
create: (fn: () => Promise<void>) => Promise<void>;
@@ -1722,8 +1702,8 @@ function useCreateGuard(): {
return { creating, create };
}
-// Draft lives in the parent, like the edit drafts: selecting a row hides this
-// form, and local state would be discarded with it.
+// Draft lives in the parent, like the edit drafts: selecting a row hides this form, and local
+// state would be discarded with it.
function NewPromptForm({
draft,
onDraftChange,
@@ -1744,11 +1724,9 @@ function NewPromptForm({
const { name, text } = draft;
const setName = (value: string) => onDraftChange({ ...draft, name: value });
const setText = (value: string) => onDraftChange({ ...draft, text: value });
- // A create outlives the form that started it. Selecting a row, switching tabs
- // or reopening New all unmount this one, and reopening mounts a different one,
- // so this answers "is the form the user is looking at still mine". Set in
- // setup, not just at the ref: StrictMode replays setup, cleanup, setup, and
- // the flag would stay false from that first cleanup for the pane's whole life.
+ // A create outlives the form that started it, so this answers "is the form the user is looking at
+ // still mine". Set in setup, not just at the ref: StrictMode replays setup, cleanup, setup,
+ // and the flag would stay false from that first cleanup for the pane's whole life.
const mounted = useRef(true);
useEffect(() => {
mounted.current = true;
@@ -1764,8 +1742,8 @@ function NewPromptForm({
if (!trimText) return;
const ts = now();
const id = newId();
- // What the request carries. The fields stay editable while it is out,
- // so the draft can move on before it resolves.
+ // What the request carries: the fields stay editable while it is out, so the draft can move on
+ // before it resolves.
const submitted: PromptDraft = { name, text };
try {
await savePromptEntry({
@@ -1776,18 +1754,18 @@ function NewPromptForm({
updatedAt: ts,
});
} catch (err) {
- // Without this the rejection is unhandled and the form just sits
- // there, so the prompt looks saved until the dialog is reopened.
+ // Without this the rejection is unhandled and the form just sits there, so the prompt looks saved
+ // until the dialog is reopened.
toast.error("Could not create prompt", {
description: err instanceof Error ? err.message : "Please try again.",
});
return;
}
- // Await the refresh first, or the keep-a-row-selected effect runs
- // against a list without the new id and bounces the selection off it.
+ // Await the refresh first, or the keep-a-row-selected effect runs against a list without the new
+ // id and bounces the selection off it.
await onRefresh();
- // The parent resets the draft if it still holds what was sent, and moves
- // the view to the new row only while this form is still the one on screen.
+ // The parent resets the draft if it still holds what was sent, and moves the view to the new row
+ // only while this form is still on screen.
onCreated(id, submitted, mounted.current);
}),
[name, text, create, onRefresh, onCreated],
@@ -1849,9 +1827,8 @@ function PromptListDetail({
const name = draft?.name ?? entry.name;
const items = draft?.items ?? entry.items;
- // Decided once per list and latched: this pane is keyed by row id, so selecting
- // another list re-decides it, but Add prompt taking a 100-item list to 101 must
- // not unmount the editor out from under the caret.
+ // Decided once per list and latched: this pane is keyed by row id, so selecting another list
+ // re-decides it, but Add prompt taking a 100-item list to 101 must not unmount the editor.
const [editorMounted, setEditorMounted] = useState(
() => items.length <= EDITOR_ROW_LIMIT,
);
@@ -1871,9 +1848,8 @@ function PromptListDetail({
[name, items, entry.name, entry.items, onDraftChange],
);
- // See PromptDetail: a Save landing after a DELETE would upsert the row back.
- // See PromptDetail: the lock is the parent's because this pane is keyed by
- // row id and unmounts on a row switch.
+ // See PromptDetail: a Save landing after a DELETE would upsert the row back, and the lock is the
+ // parent's because this pane unmounts on a row switch.
const handleSave = useCallback(async () => {
const filtered = items.filter((t) => t.trim());
if (filtered.length === 0) return;
@@ -1916,8 +1892,8 @@ function PromptListDetail({
});
}, [entry.id, onDraftChange, onDeleted, onRefresh, runMutation]);
- // Run what the editor shows. Off entry.items, deleting every draft item left
- // the button enabled on the stored length and ran the old list.
+ // Run what the editor shows. Off entry.items, deleting every draft item left the button enabled
+ // on the stored length and ran the old list.
const runnableItems = items.filter((t) => t.trim());
// See PromptDetail: export the visible draft, not the last saved copy.
@@ -2018,8 +1994,7 @@ function PromptListDetail({
<RotateCcwIcon className="size-3.5 mr-1" />Revert
</Button>
)}
- {/* The deferred summary renders neither editor nor preview, so the toggle
- would only relabel itself. */}
+ {/* The deferred summary renders neither editor nor preview, so the toggle would only relabel itself. */}
{editorMounted && (
<Button size="sm" variant="outline" onClick={() => setPreview((v) => !v)}>
{preview ? (
@@ -2053,8 +2028,8 @@ function PromptListDetail({
);
}
-// See NewPromptForm: the in-progress list lives in the parent so clicking a row
-// in the rail cannot silently discard a partially authored list.
+// See NewPromptForm: the in-progress list lives in the parent so clicking a row in the rail
+// cannot silently discard a partially authored list.
function NewPromptListForm({
draft,
onDraftChange,
@@ -2182,8 +2157,8 @@ export function PromptStorageDialog({
const [promptLists, setPromptLists] = useState<PromptListEntry[]>([]);
const [selectedPromptId, setSelectedPromptId] = useState<string | null>(null);
const [selectedListId, setSelectedListId] = useState<string | null>(null);
- // Maps, not plain objects: ids are arbitrary strings, so one named
- // "constructor" would resolve to an inherited prototype member.
+ // Maps, not plain objects: ids are arbitrary strings, so one named "constructor" would resolve to
+ // an inherited prototype member.
const [promptDrafts, setPromptDrafts] = useState<Map<string, PromptDraft>>(
() => new Map(),
);
@@ -2191,8 +2166,7 @@ export function PromptStorageDialog({
() => new Map(),
);
- // In-progress new entries, held here rather than inside the forms so that
- // hiding a form (by selecting a row in the rail) does not destroy the work.
+ // In-progress new entries, held here rather than inside the forms so that hiding a form does not destroy the work.
const [newPromptDraft, setNewPromptDraft] = useState<PromptDraft>(emptyPromptDraft);
const [newListDraft, setNewListDraft] = useState<ListDraft>(emptyListDraft);
const newPromptStarted =
@@ -2224,24 +2198,21 @@ export function PromptStorageDialog({
});
}, []);
- // Mutation locks live here rather than in the detail panes, which are keyed by
- // row id and so unmount the moment another row is selected. Held by id until
- // the request settles, or a Save in flight during a row switch comes back
- // unlocked and its unconditional PUT can land after a DELETE.
+ // Mutation locks live here rather than in the detail panes, which are keyed by row id and unmount
+ // the moment another row is selected. Held by id until the request settles, or a Save in
+ // flight during a row switch comes back unlocked and its PUT can land after a DELETE.
const [mutatingIds, setMutatingIds] = useState<ReadonlySet<string>>(
() => new Set<string>(),
);
- // The ref is the authority, not the state. A functional updater is not
- // guaranteed to run during setState, so reading the outcome straight after
- // scheduling one can see a stale answer, skip the request, and still acquire
- // the id later during render with nothing left to release it. The state
- // exists only so the buttons re-render.
+ // The ref is the authority, not the state: a functional updater is not guaranteed to run during
+ // setState, so reading the outcome straight after scheduling one can see a stale answer, skip
+ // the request, and still acquire the id later during render with nothing to release it.
const mutatingRef = useRef<ReadonlySet<string>>(new Set<string>());
const runMutation = useCallback(
async (id: string, fn: () => Promise<void>) => {
const [held, started] = acquire(mutatingRef.current, id);
- // The buttons are disabled while locked, but a second caller reaching here
- // anyway must not run and must not clear the first one's lock.
+ // The buttons are disabled while locked, but a second caller reaching here anyway must not run and
+ // must not clear the first one's lock.
if (!started) return;
mutatingRef.current = held;
setMutatingIds(held);
@@ -2255,15 +2226,13 @@ export function PromptStorageDialog({
[],
);
- // Above the New forms, not inside them: selecting a rail row unmounts the
- // form while its create is still out, and a guard mounted with it would let
- // reopening New mint a second id for the same draft.
+ // Above the New forms, not inside them: selecting a rail row unmounts the form while its create is
+ // still out, and a guard mounted with it would let reopening New mint a second id.
const promptCreate = useCreateGuard();
const listCreate = useCreateGuard();
- // Only drop the draft if it still holds what the request carried. Anything
- // typed while the save was in flight is newer than the server's copy and is
- // the user's most recent intent, so it stays and the row stays dirty.
+ // Only drop the draft if it still holds what the request carried: anything typed while the save
+ // was in flight is the user's most recent intent.
const clearPromptDraftIfSaved = useCallback(
(id: string, submitted: PromptDraft) => {
setPromptDrafts((prev) => {
@@ -2303,9 +2272,9 @@ export function PromptStorageDialog({
}
}, [open, refreshEntries, refreshLists]);
- // In the handler, not an effect keyed to activeTab. An effect leaves one render
- // of the new tab still holding the old query, and the keep-a-row-selected pass
- // runs in it and drops the row that tab had selected.
+ // In the handler, not an effect keyed to activeTab: an effect leaves one render of the new tab
+ // still holding the old query, and the keep-a-row-selected pass runs in it and drops the row
+ // that tab had selected.
const selectTab = useCallback((tab: Tab) => {
setActiveTab(tab);
setSearchQuery("");
@@ -2314,19 +2283,16 @@ export function PromptStorageDialog({
setShowNewList(false);
}, []);
- // Clear the search too: an active one the new entry does not match keeps it
- // out of the filtered rail, and the effect below bounces the selection off it.
- //
- // The draft resets only when it still holds what was sent. The fields stay
- // editable while the create is out, and Cancel can start a fresh one, so an
- // unconditional reset discards text that never reached the server.
+ // Clear the search too: an active one the new entry does not match keeps it out of the filtered
+ // rail. The draft resets only when it still holds what was sent, since the fields stay
+ // editable while the create is out.
const selectCreatedPrompt = useCallback(
(id: string, submitted: PromptDraft, fromOpenForm: boolean) => {
setNewPromptDraft((prev) =>
samePromptDraft(prev, submitted) ? emptyPromptDraft() : prev,
);
- // The user left the form while the request was out, so they are looking at
- // something else on purpose. Take the refresh, leave the view alone.
+ // The user left the form while the request was out, so they are looking at something else on
+ // purpose. Take the refresh, leave the view alone.
if (!fromOpenForm) return;
setSearchQuery("");
setSelectedPromptId(id);
@@ -2364,12 +2330,9 @@ export function PromptStorageDialog({
return all.filter((e) => e.name.toLowerCase().includes(q));
}, [promptLists, searchQuery]);
- // Keep a row selected whenever the filtered rail is non-empty, so the detail
- // pane never sits blank next to a list full of prompts. During render, not in
- // an effect: an effect paints the blank pane once before correcting it.
- // The visible tab only. searchQuery is shared, so filtering one collection also
- // filters the hidden one, and correcting against that dropped the row selected
- // there: switching back cleared the query and landed on the first row instead.
+ // Keep a row selected whenever the filtered rail is non-empty. During render, not in an effect:
+ // an effect paints the blank pane once before correcting it. The visible tab only, since
+ // searchQuery is shared and correcting against the hidden one dropped the row selected there.
if (activeTab === "prompts") {
if (filteredPrompts.length === 0) {
if (selectedPromptId !== null) setSelectedPromptId(null);
@@ -2410,8 +2373,8 @@ export function PromptStorageDialog({
[onUse, onOpenChange],
);
- // The rail is one tab stop; arrows move within it. Clicking rather than
- // setting the id keeps the two tabs on one handler.
+ // The rail is one tab stop; arrows move within it. Clicking rather than setting the id keeps the
+ // two tabs on one handler.
const handleRailKeyDown = useCallback((e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
const rows = Array.from(
@@ -2611,11 +2574,9 @@ export function PromptStorageDialog({
</div>
{/* */}
- {/* min-h-0, not a floor: the grid rows below already carry the minimum
- each pane's chrome needs, and the body scrolls to them. A floor
- here has to guess the height of the header and search above it,
- which grows when their text wraps on a narrow dialog, and the
- excess lands in DialogContent's overflow-hidden. */}
+ {/* min-h-0, not a floor: the grid rows below already carry the minimum each pane's chrome needs,
+ and a floor here has to guess the height of the header and search above it, whose text
+ wraps on a narrow dialog. */}
<div className="flex-1 min-h-0 overflow-y-auto px-4 sm:px-6 pb-4 sm:pb-6 grid gap-2 sm:gap-4 grid-cols-1 grid-rows-[minmax(132px,30%)_minmax(272px,1fr)] sm:grid-cols-[200px_minmax(0,1fr)] sm:grid-rows-1 lg:grid-cols-[248px_minmax(0,1fr)]">
{/* */}
<div className="flex min-h-[132px] flex-col gap-2 rounded-xl border border-border/50 bg-muted/20 p-2">
diff --git a/studio/frontend/src/features/chat/prompt-storage/reorder.ts b/studio/frontend/src/features/chat/prompt-storage/reorder.ts
index 58743327182..1906e06c662 100644
--- a/studio/frontend/src/features/chat/prompt-storage/reorder.ts
+++ b/studio/frontend/src/features/chat/prompt-storage/reorder.ts
@@ -6,17 +6,11 @@ export interface RowBox {
height: number;
}
-/**
- * Where the dragged row belongs for a pointer at `localY`, as an index into the
- * current order.
- *
- * Counts the rows the pointer has passed rather than hit-testing the row under
- * it, and skips the dragged row itself. Testing "which row contains the
- * pointer" oscillates across uneven heights: dropping a short row onto a tall
- * one leaves the tall row still under the pointer, which sends it straight
- * back. Midpoints of the others are stable, because after the move the dragged
- * row occupies the space that shifted them.
- */
+/** Where the dragged row belongs for a pointer at `localY`, as an index into the current order.
+ * Counts the rows the pointer has passed rather than hit-testing the row under it, and skips the
+ * dragged row. Testing "which row contains the pointer" oscillates across uneven heights:
+ * dropping a short row onto a tall one leaves the tall row still under the pointer, sending it
+ * straight back. Midpoints of the others are stable. */
export function insertionIndex(
rows: readonly (RowBox | undefined)[],
from: number,
@@ -32,16 +26,11 @@ export function insertionIndex(
return to;
}
-/**
- * Whether a window-level pointer event belongs to the drag `activePointerId`
- * started.
- *
- * The grip deliberately does not capture the pointer, so the listeners are on
- * `window` and see every pointer on the page. Ending a drag clears the id
- * before React can unsubscribe them, so treating "no active pointer" as a match
- * lets a still-held button reorder the list after the drag was already ended by
- * a window blur.
- */
+/** Whether a window-level pointer event belongs to the drag `activePointerId` started. The grip
+ * deliberately does not capture the pointer, so the listeners are on `window` and see every
+ * pointer on the page. Ending a drag clears the id before React can unsubscribe them, so
+ * treating "no active pointer" as a match lets a still-held button reorder the list after a
+ * window blur already ended the drag. */
export function ownsDrag(
activePointerId: number | null,
eventPointerId: number,
@@ -49,16 +38,11 @@ export function ownsDrag(
return activePointerId !== null && activePointerId === eventPointerId;
}
-/**
- * The translateY, in pixels, each row needs so that a reorder appears to start
- * from where the rows were before it.
- *
- * `first` must be read at the moment the reorder is requested. Rows change
- * height without the order changing, from the preview toggle and from a
- * textarea regrowing on resize, so a baseline recorded at some earlier commit
- * describes a layout that no longer exists and shifts every row by the
- * difference. Sub-pixel moves are dropped rather than animated.
- */
+/** The translateY each row needs so that a reorder appears to start from where the rows were
+ * before it. `first` must be read at the moment the reorder is requested: rows change height
+ * without the order changing, from the preview toggle and a textarea regrowing, so a baseline
+ * recorded at an earlier commit describes a layout that no longer exists. Sub-pixel moves are
+ * dropped rather than animated. */
export function flipShifts(
first: ReadonlyMap<string, number>,
last: ReadonlyMap<string, number>,
diff --git a/studio/frontend/src/features/chat/prompt-storage/sortable-prompt-items.tsx b/studio/frontend/src/features/chat/prompt-storage/sortable-prompt-items.tsx
index d617cfca20a..29225efc787 100644
--- a/studio/frontend/src/features/chat/prompt-storage/sortable-prompt-items.tsx
+++ b/studio/frontend/src/features/chat/prompt-storage/sortable-prompt-items.tsx
@@ -44,8 +44,8 @@ export function AutoTextarea({
// Layout effect: measuring after paint flashes the wrong height while typing.
useLayoutEffect(measure, [value, measure]);
- // A width change rewraps the text without touching `value`, and overflowY may
- // be "hidden", so the new lines would be clipped until the next keystroke.
+ // A width change rewraps the text without touching `value`, and overflowY may be "hidden", so
+ // the new lines would be clipped until the next keystroke.
useEffect(() => {
const el = ref.current;
if (!el) return;
@@ -100,8 +100,8 @@ function nextUid(): string {
return `i${uidSeq}`;
}
-// Rows are keyed by a synthetic uid, not array index: index keys would swap the
-// values under the caret on reorder and break the animation.
+// Rows are keyed by a synthetic uid, not array index: index keys would swap the values under
+// the caret on reorder and break the animation.
export function SortablePromptItems({
items,
onChange,
@@ -122,31 +122,28 @@ export function SortablePromptItems({
const rowRefs = useRef(new Map<string, HTMLDivElement>());
const prevOffsets = useRef(new Map<string, number>());
const uidsRef = useRef(uids);
- // Only a reorder animates, or an auto-growing textarea would make the rows
- // below it wobble as you type.
+ // Only a reorder animates, or an auto-growing textarea would make the rows below it wobble as you type.
const reorderTick = useRef(0);
const animatedTick = useRef(0);
- // Resync when the count changes from outside (revert, switching lists, import).
- // During render rather than in an effect, which would paint the new row once
- // under a stale key first.
+ // Resync when the count changes from outside (revert, switching lists, import). During render
+ // rather than in an effect, which would paint the new row once under a stale key first.
let rowUids = uids;
if (uids.length !== items.length) {
rowUids = items.map((_, i) => uids[i] ?? nextUid());
setUids(rowUids);
}
- // Layout offsets, not client rects: a rect moves with the scroll position, so
- // scrolling the pane between reorders would bake that distance into every
- // transform and jump the whole list.
+ // Layout offsets, not client rects: a rect moves with the scroll position, so scrolling the
+ // pane between reorders would bake that distance into every transform.
const measureOffsets = useCallback(() => {
const offsets = new Map<string, number>();
rowRefs.current.forEach((el, uid) => offsets.set(uid, el.offsetTop));
return offsets;
}, []);
- // FLIP: snap each moved row back to where it was, then release, so the browser
- // animates one transform per row rather than animating layout.
+ // FLIP: snap each moved row back to where it was, then release, so the browser animates one
+ // transform per row rather than animating layout.
useLayoutEffect(() => {
if (reorderTick.current === animatedTick.current) return;
animatedTick.current = reorderTick.current;
@@ -162,9 +159,8 @@ export function SortablePromptItems({
});
}, [uids, measureOffsets]);
- // Mirrors, so the drag listener does not resubscribe on every keystroke. A
- // layout effect, not an assignment during render: they only have to be
- // current before the next pointer event, and this runs well before that.
+ // Mirrors, so the drag listener does not resubscribe on every keystroke. A layout effect, not
+ // an assignment during render: they only have to be current before the next pointer event.
const itemsRef = useRef(items);
const onChangeRef = useRef(onChange);
useLayoutEffect(() => {
@@ -175,15 +171,15 @@ export function SortablePromptItems({
const applyOrder = useCallback((from: number, to: number) => {
if (from === to) return;
- // FLIP's "first", read now rather than at whichever commit last recorded it:
- // the preview toggle and a resize change row heights on their own, and
- // animating from those stale offsets shifted the whole list.
+ // FLIP's "first", read now rather than at whichever commit last recorded it: the preview
+ // toggle and a resize change row heights on their own, and animating from those stale
+ // offsets shifted the whole list.
prevOffsets.current = measureOffsets();
reorderTick.current += 1;
const nextItems = move(itemsRef.current, from, to);
const nextUids = move(uidsRef.current, from, to);
- // Advance the mirrors here too. A drag reorders faster than a commit, and
- // the next hit-test must not run against the pre-move order.
+ // Advance the mirrors here too. A drag reorders faster than a commit, and the next hit-test
+ // must not run against the pre-move order.
itemsRef.current = nextItems;
uidsRef.current = nextUids;
onChangeRef.current(nextItems);
@@ -192,15 +188,14 @@ export function SortablePromptItems({
const handlePointerDown = useCallback(
(uid: string, e: React.PointerEvent<HTMLButtonElement>) => {
- // Primary press only: a right/middle drag reports buttons 2 or 4, which
- // the zero-buttons release check below cannot end.
+ // Primary press only: a right or middle drag reports buttons 2 or 4, which the zero-buttons
+ // release check below cannot end.
if (e.button !== 0 || !e.isPrimary) return;
// isPrimary is per pointer type, so a mouse press can pass it mid-touch-drag.
if (pointerIdRef.current !== null) return;
pointerIdRef.current = e.pointerId;
- // No setPointerCapture: reordering moves the row's DOM node, and detaching
- // releases capture, killing the drag one row in. The window listener below
- // outlives the move instead.
+ // No setPointerCapture: reordering moves the row's DOM node, and detaching releases capture,
+ // killing the drag one row in. The window listener below outlives the move instead.
e.preventDefault();
pointerYRef.current = e.clientY;
setDraggingUid(uid);
@@ -218,8 +213,8 @@ export function SortablePromptItems({
const from = order.indexOf(draggingUid);
if (from < 0) return;
- // Layout offsets, not client rects: mid-FLIP a client rect still reports
- // the pre-animation position. offsetTop/offsetHeight ignore transforms.
+ // Layout offsets, not client rects: mid-FLIP a client rect still reports the pre-animation
+ // position. offsetTop/offsetHeight ignore transforms.
const localY = pointerYRef.current - container.getBoundingClientRect().top;
const boxes = order.map((uid) => {
const el = rowRefs.current.get(uid);
@@ -234,17 +229,15 @@ export function SortablePromptItems({
setDraggingUid(null);
};
- // Window listeners see every pointer; a second finger would otherwise
- // reorder with its own clientY and its release would end this drag. An
- // already-ended drag owns no pointer, so nothing matches until the cleanup
- // below unsubscribes.
+ // Window listeners see every pointer; a second finger would otherwise reorder with its own
+ // clientY and its release would end this drag. An already-ended drag owns no pointer.
const isDragPointer = (e: PointerEvent) =>
ownsDrag(pointerIdRef.current, e.pointerId);
const onMove = (e: PointerEvent) => {
if (!isDragPointer(e)) return;
- // Releasing outside the window delivers no pointerup, so treat the first
- // move with no buttons held as the release we missed.
+ // Releasing outside the window delivers no pointerup, so treat the first move with no buttons
+ // held as the release we missed.
if (e.buttons === 0) {
endDrag();
return;
@@ -287,8 +280,7 @@ export function SortablePromptItems({
ref={containerRef}
className={cn(
"relative flex flex-col gap-2",
- // The drag tracks the pointer window-wide; without this it paints a
- // text selection behind itself.
+ // The drag tracks the pointer window-wide; without this it paints a text selection behind itself.
draggingUid && "select-none",
)}
>
diff --git a/studio/frontend/src/features/chat/provider-capabilities.ts b/studio/frontend/src/features/chat/provider-capabilities.ts
index b49c310857d..3efdf24612e 100644
--- a/studio/frontend/src/features/chat/provider-capabilities.ts
+++ b/studio/frontend/src/features/chat/provider-capabilities.ts
@@ -6,20 +6,11 @@ import {
providerModelSupportsStudioTools,
} from "./external-providers";
-/**
- * Per-provider sampling parameter capability matrix.
- *
- * Derived from each provider's published chat-completion docs (2026-05). The
- * panel hides params a provider does not accept so users cannot dial a value
- * that gets silently dropped or rejected. "Local" models (non-external) use a
- * null capability — every knob renders for them.
- */
+/** Per-provider sampling capability matrix from each provider's chat docs (2026-05).
+ * Params a provider rejects are hidden; local models use a null capability, so all render. */
export interface ProviderCapabilities {
- /**
- * Temperature sampling. Reasoning-class models (OpenAI's gpt-5.x / o3 via
- * /v1/responses) reject this with `Unsupported parameter`.
- */
+ /** Temperature. Reasoning-class models (gpt-5.x / o3 via /v1/responses) reject it. */
temperature: boolean;
/** Nucleus (top_p) sampling. Same restriction as `temperature` on OpenAI. */
topP: boolean;
@@ -35,9 +26,8 @@ export interface ProviderCapabilities {
export type ExternalReasoningCapabilities = {
supportsReasoning: boolean;
- // Mirrors the store's ReasoningStyle. External providers only ever use the
- // first two; "enable_thinking_effort" exists so a local model's caps can be
- // assigned here without narrowing.
+ // Mirrors the store's ReasoningStyle. "enable_thinking_effort" exists so a local model's
+ // caps can be assigned here without narrowing.
reasoningStyle: "enable_thinking" | "reasoning_effort" | "enable_thinking_effort";
reasoningAlwaysOn: boolean;
supportsReasoningOff: boolean;
@@ -52,10 +42,8 @@ export type ExternalReasoningCapabilities = {
)[];
};
-/**
- * Pick a stored reasoning effort level that exists in `effortLevels`, mapping
- * legacy "xhigh" to "max" when only the latter is exposed (Claude 4.6).
- */
+/** Pick a stored effort level present in `effortLevels`, mapping legacy "xhigh" to "max"
+ * when only the latter is exposed (Claude 4.6). */
export function clampReasoningEffortToLevels(
preferred: ExternalReasoningCapabilities["reasoningEffortLevels"][number],
effortLevels: ExternalReasoningCapabilities["reasoningEffortLevels"],
@@ -77,14 +65,7 @@ export function clampReasoningEffortToLevels(
/** Fallback cap for a model with no documented limit and no connection override. */
export const EXTERNAL_MAX_OUTPUT_TOKENS = 32768;
-/**
- * Per-model max-output caps from each provider's docs:
- * OpenAI: developers.openai.com/api/docs/models/gpt-5.5
- * Anthropic: platform.claude.com/docs/en/about-claude/models
- * Gemini: ai.google.dev/gemini-api/docs/models/gemini-3.1-pro-preview
- * DeepSeek: api-docs.deepseek.com/quick_start/pricing (V4 family)
- * Local-model path is unaffected.
- */
+/** Per-model max-output caps from provider docs. The local-model path is unaffected. */
const EXTERNAL_MAX_OUTPUT_TOKENS_BY_MODEL: Array<{
providerType: string;
prefixes: readonly string[];
@@ -167,15 +148,9 @@ const EXTERNAL_MAX_OUTPUT_TOKENS_BY_MODEL: Array<{
{ providerType: "deepseek", prefixes: ["deepseek"], cap: 384000 },
];
-/**
- * The connection's effective Max Tokens ceiling for one model.
- *
- * A documented per-model cap bounds the connection override rather than replacing it:
- * one connection fronts many models on a router, so a limit set for a 256k-output model
- * must not raise the slider past what a smaller model accepts. An undocumented model
- * takes the override outright, then `EXTERNAL_MAX_OUTPUT_TOKENS`. The provider's output
- * floor wins over both, so the slider is never handed a max below its min.
- */
+/** The connection's effective Max Tokens ceiling for one model. A documented per-model cap
+ * bounds the override rather than replacing it (one connection fronts many router models).
+ * Undocumented takes the override, then EXTERNAL_MAX_OUTPUT_TOKENS; the output floor wins. */
export function getExternalMaxOutputTokens(
providerType: string | null | undefined,
modelId: string | null | undefined,
@@ -190,11 +165,8 @@ export function getExternalMaxOutputTokens(
return Math.max(resolved, getExternalMinOutputTokens(providerType));
}
-/**
- * The published per-model cap, or null when nothing documents this id. No table entry
- * targets a generic Custom connection, so those always read as undocumented. OpenRouter
- * `provider/model` ids have the prefix stripped before matching.
- */
+/** The published per-model cap, or null when nothing documents this id. Generic Custom
+ * connections always read undocumented. OpenRouter `provider/model` prefixes are stripped. */
function _documentedMaxOutputTokens(
providerType: string | null | undefined,
modelId: string | null | undefined,
@@ -223,15 +195,9 @@ function _documentedMaxOutputTokens(
return null;
}
-/**
- * The lowered Max Tokens the settings panel should write back, or null to leave it be.
- *
- * The availability guards are load-bearing: the caller PERSISTS this and it only ever
- * lowers, while `maxTokensMax` collapses to the 32,768 fallback whenever the provider is
- * momentarily unresolved (connections toggled off, cold hydration, a deleted connection).
- * No provider means the cap is unknown, not 32,768. Returning the cap itself is what
- * makes it converge in one pass.
- */
+/** The lowered Max Tokens to write back, or null to leave it. The guards are load-bearing:
+ * the caller PERSISTS this and it only lowers, while maxTokensMax collapses to the 32,768
+ * fallback when the provider is unresolved. No provider means unknown, not 32,768. */
export function resolveExternalMaxTokensClamp(input: {
settingsHydrated: boolean;
hasActiveExternalProvider: boolean;
@@ -256,46 +222,18 @@ function _inferProviderFromOpenrouterId(
return null;
}
-/**
- * Whether the external provider offers a built-in web-search tool that the
- * model invokes server-side. When `true`, the chat composer's Search button
- * is available for that provider and the chat-adapter forwards
- * `enable_tools: true, enabled_tools: ["web_search"]` on the request — the
- * backend routes the call through the provider's tool schema:
- * - OpenAI: `tools: [{type: "web_search"}]` on /v1/responses
- * - Anthropic: `tools: [{type: "web_search_20250305", name: "web_search",
- * max_uses: 5}]` on /v1/messages
- * - OpenRouter: `plugins: [{id: "web"}]` on /v1/chat/completions (the
- * router's universal web-search shape; works for every
- * underlying model including the `openrouter/free` router).
- * - Kimi: `tools: [{type: "builtin_function", function: {name:
- * "$web_search"}}]` with `thinking: {type:
- * "disabled"}`. Requires a client round-trip:
- * the first call returns the search args; the backend
- * echoes them back as a role=tool message; the second
- * call streams the answer. Handled in
- * _stream_kimi_web_search on the backend.
- *
- * Mistral is intentionally excluded: their `web_search` connector lives on
- * the Agents API (`/v1/agents` + `/v1/conversations`), not chat completions,
- * and returns `"WebSearchTool connector is not supported"` if injected into
- * /v1/chat/completions. Wiring it would require a dedicated Agents streaming
- * path. Gemini's grounded-search can be added with the same pattern when
- * matching backend translation lands.
- */
+/** Whether the provider offers a server-side web-search tool. Enables the Search button and
+ * sends `enable_tools` + `enabled_tools: ["web_search"]` for the backend to translate.
+ * Mistral is excluded: its connector is Agents-API only and errors on /v1/chat/completions. */
export function providerSupportsBuiltinWebSearch(
providerType: string | null | undefined,
modelId?: string | null | undefined,
baseUrl?: string | null | undefined,
): boolean {
- // Gemini ships grounded search via `tools: [{googleSearch: {}}]` on every
- // chat-capable model. Most image-tier ids reject text-tool wiring (the
- // responseModalities path excludes text tools), but Google documents Search
- // grounding on the Gemini 3 image family (gemini-3-pro-image-preview,
- // gemini-3.1-flash-image-preview, nano-banana-pro), so allow it there and
- // hide on older
- // image ids. Custom Gemini OpenAI-compat proxies skip the backend's native
- // translator, so native tool envelopes never reach them -- hide the pill.
+ // Gemini ships grounded search via `tools: [{googleSearch: {}}]` on chat-capable models. Most
+ // image-tier ids reject text-tool wiring, but Google documents Search grounding on the Gemini 3
+ // image family, so allow it there and hide on older image ids. Custom Gemini OpenAI-compat
+ // proxies skip the native translator, so hide the pill.
if (providerType === "gemini") {
if (isGeminiCustomOpenAICompatBase(baseUrl)) return false;
const normalized = modelId?.trim().toLowerCase() ?? "";
@@ -316,26 +254,16 @@ export function providerSupportsBuiltinWebSearch(
);
}
-/**
- * Whether the external provider exposes a server-side web_fetch tool
- * (single URL, text or PDF) emitting a document block. Anthropic-only
- * today (`web_fetch_20250910` / `web_fetch_20260209`). Gates the
- * composer's standalone Fetch pill, independent of Search.
- */
+/** Whether the provider exposes a server-side web_fetch tool emitting a document block.
+ * Anthropic-only today; gates the Fetch pill independently of Search. */
export function providerSupportsBuiltinWebFetch(
providerType: string | null | undefined,
): boolean {
return providerType === "anthropic";
}
-/**
- * Whether the active provider + model supports Anthropic fast-mode
- * (`speed: "fast"` + `fast-mode-2026-02-01` header). Opus 5 / Opus 4.8 only
- * per https://platform.claude.com/docs/en/build-with-claude/fast-mode: 4.7
- * errors on `speed`, and 4.6 accepts it but runs at standard speed, so the
- * toggle there promises a speed-up that never arrives.
- * Backend silently drops on unsupported models as a second defence.
- */
+/** Whether provider + model support Anthropic fast-mode (`speed: "fast"`). Opus 5 / 4.8 only:
+ * 4.7 errors on `speed`, 4.6 accepts it but runs at standard speed. Backend re-checks. */
const ANTHROPIC_FAST_MODE_MODEL_PREFIXES = [
"claude-opus-5",
"claude-opus-4-8",
@@ -347,36 +275,15 @@ export function providerSupportsFastMode(
): boolean {
if (providerType !== "anthropic") return false;
if (!modelId) return false;
- // Family boundary ("" or "-") required so IDs like "claude-opus-4-70" or
- // "claude-opus-4-7b" do not match.
+ // Family boundary ("" or "-") required so IDs like "claude-opus-4-70" do not match.
return ANTHROPIC_FAST_MODE_MODEL_PREFIXES.some(
(prefix) => modelId === prefix || modelId.startsWith(`${prefix}-`),
);
}
-/**
- * Whether the selected external provider/model exposes a server-side
- * code-execution tool. Two providers ship one today:
- *
- * - **Anthropic** (`code_execution_20250825`): Python + bash +
- * str_replace-based file edits inside a 5 GB sandboxed container
- * per request. Documented at
- * https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool
- *
- * - **OpenAI cloud** (`shell` on /v1/responses): bash inside a
- * reusable container; we auto-create one on the first turn of a
- * chat thread and reference it on subsequent turns via the
- * thread's stored `openaiCodeExecContainerId`. Documented at
- * https://developers.openai.com/api/docs/guides/tools-shell
- *
- * Returns false for every other provider. The backend also gates the OpenAI
- * shell tool on `is_openai_cloud` so custom OpenAI-compat servers reporting
- * `provider_type="openai"` never receive it; in practice those catalogs don't
- * surface the `gpt-5.5` ids, so the frontend prefix match suffices.
- *
- * v1 wires the tools only; file uploads (Anthropic `container_upload` / OpenAI
- * `input_file`) are a deliberate follow-up.
- */
+/** Whether the provider/model exposes server-side code execution: Anthropic's
+ * `code_execution_20250825` sandbox, or OpenAI cloud's `shell` on /v1/responses.
+ * False elsewhere; the backend also gates the shell tool on is_openai_cloud. */
const ANTHROPIC_CODE_EXECUTION_MODEL_PREFIXES = [
"claude-opus-5",
"claude-sonnet-5",
@@ -389,30 +296,23 @@ const ANTHROPIC_CODE_EXECUTION_MODEL_PREFIXES = [
"claude-opus-4-5",
"claude-sonnet-4-5",
"claude-haiku-4-5",
- // Deprecated upstream but the registry still exposes these ids; keep the
- // pill working for users on those snapshots.
+ // Deprecated upstream but the registry still exposes these ids; keep the pill working for users on those snapshots.
"claude-opus-4-1",
"claude-opus-4",
"claude-sonnet-4",
] as const;
-// OpenAI cloud shell-tool gating. The gpt-5.6 family (sol/terra/luna) lists the
-// hosted shell under its supported tools; gpt-5.5-pro shares the same
-// /v1/responses contract as gpt-5.5. `gpt-5.5-pro` is checked first so the
-// prefix match doesn't collide with e.g. a hypothetical `gpt-5.5-turbo`.
+// OpenAI cloud shell gating: the gpt-5.6 family lists the hosted shell and gpt-5.5-pro
+// shares the /v1/responses contract. Check `gpt-5.5-pro` first so the prefix match
+// cannot collide with a hypothetical `gpt-5.5-turbo`.
const OPENAI_CODE_EXECUTION_MODEL_PREFIXES = [
"gpt-5.6",
"gpt-5.5-pro",
"gpt-5.5",
] as const;
-/**
- * Strict check that a provider config points at OpenAI managed cloud
- * (api.openai.com) or Azure OpenAI Foundry (*.openai.azure.com), not a custom
- * OpenAI-compat backend. The shell and image-generation tools exist only on
- * cloud backends; sending them elsewhere 400s. Mirrors the backend's
- * `_is_openai_family_cloud` host check.
- */
+/** Strict check for OpenAI managed cloud or Azure Foundry, not a custom OpenAI-compat
+ * backend: shell and image-generation tools 400 elsewhere. Mirrors _is_openai_family_cloud. */
function isOpenAICloudBaseUrl(baseUrl: string | null | undefined): boolean {
if (!baseUrl) return true; // No override → uses the default openai.com base.
try {
@@ -446,15 +346,10 @@ export function providerSupportsBuiltinCodeExecution(
);
}
if (providerType === "gemini") {
- // Gemini's `tools: [{codeExecution: {}}]` is supported on every
- // chat-capable model. Image-tier ids (`-image`, `nano-banana`)
- // reject text-tool wiring because the inline-image path is
- // mutually exclusive with codeExecution. Custom Gemini
- // OpenAI-compat proxies skip the native translator on the
- // backend, so native codeExecution envelopes do not reach them.
- // Wire-up lives in `_stream_gemini` on the backend; output comes
- // back inline as executableCode/codeExecutionResult parts. See
- // https://ai.google.dev/gemini-api/docs/code-execution.
+ // Gemini code execution works on chat-capable models, but image-tier ids reject text-tool
+ // wiring (mutually exclusive with the inline-image path), and custom OAI-compat proxies
+ // skip the native translator. Wire-up lives in `_stream_gemini`; output comes back inline
+ // as executableCode / codeExecutionResult parts.
if (isGeminiCustomOpenAICompatBase(baseUrl)) return false;
if (isGeminiImageModel(normalized)) return false;
return normalized.startsWith("gemini-");
@@ -462,17 +357,9 @@ export function providerSupportsBuiltinCodeExecution(
return false;
}
-/**
- * Whether the provider TYPE ships a code sandbox of its own, model aside.
- *
- * Mirrors the `hosted_tools` entries carrying `code_execution` in the backend's
- * PROVIDER_REGISTRY (core/inference/providers.py). Deliberately coarser than
- * `providerSupportsBuiltinCodeExecution`: the question it answers is whether
- * running the model's code on the USER's machine would be a relocation, and
- * that does not depend on which model is selected. openai_codex is absent for
- * the same reason the backend registry leaves it out -- its code tools are
- * Unsloth's own, run by the Codex loop, and always have been.
- */
+/** Whether the provider TYPE ships its own code sandbox, model aside. Mirrors backend
+ * PROVIDER_REGISTRY `hosted_tools` code_execution. Coarser than the per-model check:
+ * it asks whether running the code locally would be a relocation. */
const PROVIDER_TYPES_WITH_CODE_SANDBOX = new Set(["openai", "anthropic", "gemini"]);
export function providerHostsCodeExecution(
@@ -481,15 +368,8 @@ export function providerHostsCodeExecution(
return PROVIDER_TYPES_WITH_CODE_SANDBOX.has(providerType ?? "");
}
-/**
- * Whether the selected provider/model exposes OpenAI's Responses-API
- * image_generation tool. On for OpenAI cloud (`api.openai.com`) with a
- * Responses-API family id (gpt-5.x). Mirrors the backend's `is_openai_cloud`
- * gate so the pill hides on custom OpenAI-compat backends reporting
- * `provider_type="openai"` that would 400 on a `{type:"image_generation"}` tool.
- * See backend/core/inference/external_provider.py (~line 2770) for dispatch and
- * backend/tests/test_openai_image_generation.py for round-trip coverage.
- */
+/** Whether provider/model exposes OpenAI's Responses-API image_generation tool. On for
+ * OpenAI cloud Responses-family ids, mirroring the backend's is_openai_cloud gate. */
const OPENAI_IMAGE_GENERATION_MODEL_PREFIXES = [
"gpt-5.5-pro",
"gpt-5.5",
@@ -516,35 +396,24 @@ export function providerSupportsBuiltinImageGeneration(
);
}
if (providerType === "gemini") {
- // Gemini Nano Banana image-output ids carry `-image` or the `nano-banana`
- // alias. The backend flips responseModalities to ["TEXT", "IMAGE"] and maps
- // inlineData parts into the same image_b64 tool_end envelope as the OpenAI
- // path so the UI renders inline. Custom Gemini OpenAI-compat proxies skip
- // the native translator, so hide the image pill there.
- // See https://ai.google.dev/gemini-api/docs/image-generation.
+ // Gemini Nano Banana image ids carry `-image` or the `nano-banana` alias; the backend maps
+ // inlineData into the same image_b64 envelope. Custom OAI-compat proxies skip the
+ // native translator, so hide the pill there.
if (isGeminiCustomOpenAICompatBase(baseUrl)) return false;
return normalized.includes("-image") || normalized.includes("nano-banana");
}
return false;
}
-/**
- * Whether `modelId` is a Gemini image-output id (Nano Banana family).
- * Mirrors the backend's `is_image_picker_model` guard so the frontend
- * hides text-only tool pills (web_search, code_execution) for these.
- */
+/** Whether `modelId` is a Gemini image-output id. Mirrors the backend's is_image_picker_model
+ * so text-only tool pills stay hidden. */
function isGeminiImageModel(modelId: string): boolean {
const m = modelId.toLowerCase();
return m.includes("-image") || m.includes("nano-banana");
}
-/**
- * Whether the saved Gemini connection points at a custom OpenAI-compat gateway
- * (any non-Google host). The backend `_is_openai_compatible` routes these
- * through `/chat/completions` instead of the native translator, so native Gemini
- * tool envelopes never reach them. Hide the matching Unsloth pills here so the
- * request, builder, and UI agree.
- */
+/** Whether the Gemini connection points at a custom OpenAI-compat gateway. The backend routes
+ * those through /chat/completions, so native Gemini tool envelopes never reach them. */
export function isGeminiCustomOpenAICompatBase(
baseUrl: string | null | undefined,
): boolean {
@@ -557,13 +426,8 @@ export function isGeminiCustomOpenAICompatBase(
}
}
-/**
- * Whether the given Gemini image model supports `tools: [{googleSearch: {}}]`.
- * Google documents Search grounding on the Gemini 3 image family
- * (gemini-3-pro-image-preview, gemini-3.1-flash-image-preview,
- * "Nano Banana Pro"); older image ids (gemini-2.5-flash-image) reject
- * it with "Search as tool is not enabled for this model".
- */
+/** Whether this Gemini image model supports googleSearch. Documented on the Gemini 3 image
+ * family; older ids reject it with "Search as tool is not enabled for this model". */
function geminiImageModelAllowsGoogleSearch(modelId: string): boolean {
const m = modelId.toLowerCase();
return (
@@ -574,15 +438,9 @@ function geminiImageModelAllowsGoogleSearch(modelId: string): boolean {
);
}
-/**
- * Per-provider minimum on the outbound max_tokens. Kimi requires
- * `max_tokens >= 16000` for thinking models so reasoning_content and the answer
- * both fit; lower truncates mid-stream. Others fall through to the generic 64.
- *
- * The chat-adapter resolves the floor on send and bumps maxTokens up to it if
- * the stored value is below; the settings slider min reflects the same floor so
- * the displayed value never drifts from what's sent.
- */
+/** Per-provider minimum outbound max_tokens. Kimi needs >= 16000 on thinking models so
+ * reasoning_content and the answer both fit; others use the generic 64. Adapter and
+ * slider min resolve the same floor. */
const EXTERNAL_MIN_OUTPUT_TOKENS_BY_PROVIDER: Record<string, number> = {
kimi: 16000,
};
@@ -613,10 +471,9 @@ const ALL_SUPPORTED: ProviderCapabilities = {
};
const PROVIDER_CAPABILITIES: Record<string, ProviderCapabilities> = {
- // OpenAI's flagship models (gpt-5.x / o3 / gpt-4.5) are reasoning-class
- // models served via /v1/responses, which rejects temperature, top_p, and
- // presence/frequency penalty. See backend
- // external_provider._stream_openai_responses for the proxy.
+ // OpenAI's flagship ids are reasoning-class on /v1/responses, which rejects temperature,
+ // top_p and presence/frequency penalty.
+ // The concrete ids are gpt-5.x, o3 and gpt-4.5. See external_provider._stream_openai_responses.
openai_codex: {
temperature: false,
topP: false,
@@ -634,13 +491,10 @@ const PROVIDER_CAPABILITIES: Record<string, ProviderCapabilities> = {
repetitionPenalty: false,
presencePenalty: false,
},
- // Anthropic's Messages API accepts top_k on 3.x and 4.5/4.6, but Claude
- // 4.7 (Opus/Sonnet/Haiku) deprecated it and returns 400 if it is set.
- // We surface top_k in the panel for all Anthropic providers and let the
- // backend strip it per-model — see _stream_anthropic in
- // studio/backend/core/inference/external_provider.py.
- // Presence/frequency penalty is not part of the Messages API on any
- // Claude generation.
+ // Anthropic accepts top_k on 3.x and 4.5/4.6, but 4.7 400s on it, so the panel surfaces it
+ // and the backend strips per-model. Presence/frequency penalty is not in the Messages API.
+ // Claude 4.7 is Opus, Sonnet and Haiku alike. Stripping lives in _stream_anthropic in
+ // core/inference/external_provider.py.
anthropic: {
temperature: true,
topP: true,
@@ -650,12 +504,9 @@ const PROVIDER_CAPABILITIES: Record<string, ProviderCapabilities> = {
presencePenalty: false,
},
mistral: OPENAI_COMPAT_BASE,
- // Gemini's generationConfig accepts temperature, topP, topK and
- // presencePenalty (plus a frequencyPenalty we don't surface). minP and
- // repetitionPenalty aren't part of the contract --
- // see https://ai.google.dev/api/rest/v1beta/GenerationConfig. Backend shaping
- // lives in _stream_gemini in
- // studio/backend/core/inference/external_provider.py.
+ // Gemini's generationConfig accepts temperature, topP, topK, presencePenalty; minP and
+ // repetitionPenalty are not in the contract. Shaping lives in _stream_gemini.
+ // Gemini also accepts a frequencyPenalty this panel does not surface.
gemini: {
temperature: true,
topP: true,
@@ -664,10 +515,8 @@ const PROVIDER_CAPABILITIES: Record<string, ProviderCapabilities> = {
repetitionPenalty: false,
presencePenalty: true,
},
- // Kimi k2.5/k2.6 are reasoning-class — the API locks temperature and top_p to
- // fixed defaults and 400s on any other value ("invalid temperature: only 1 is
- // allowed for this model"). Hide both sliders. Backend also strips these via
- // PROVIDER_REGISTRY['kimi']['body_omit'].
+ // Kimi k2.5/k2.6 lock temperature and top_p and 400 on any other value, so hide both
+ // sliders. The backend also strips them via PROVIDER_REGISTRY['kimi']['body_omit'].
kimi: {
temperature: false,
topP: false,
@@ -687,12 +536,11 @@ const PROVIDER_CAPABILITIES: Record<string, ProviderCapabilities> = {
},
qwen: OPENAI_COMPAT_BASE,
huggingface: OPENAI_COMPAT_BASE,
- // OpenRouter silently drops params the target model does not support, so we
- // surface every knob and let the gateway handle the per-model fan-out.
+ // OpenRouter silently drops unsupported params, so surface every knob and let the gateway
+ // fan out per model.
openrouter: ALL_SUPPORTED,
- // Local OpenAI-compat connections go through the OpenAI backend path, but
- // vLLM/Ollama/llama.cpp users often want top_k/min_p/repetition controls, so
- // be permissive.
+ // Local OpenAI-compat connections use the OpenAI path, but vLLM/Ollama/llama.cpp users
+ // want top_k/min_p/repetition, so be permissive.
custom: ALL_SUPPORTED,
vllm: ALL_SUPPORTED,
ollama: ALL_SUPPORTED,
@@ -701,11 +549,8 @@ const PROVIDER_CAPABILITIES: Record<string, ProviderCapabilities> = {
const DEFAULT_EXTERNAL_CAPABILITIES = OPENAI_COMPAT_BASE;
-/**
- * Resolve the capability set for an external provider. Returns `null` for
- * a local model (i.e. when `providerType` is null/undefined), which callers
- * should treat as "every knob applies".
- */
+/** Resolve the capability set for an external provider. Null for a local model, which
+ * callers treat as "every knob applies". */
export function getProviderCapabilities(
providerType: string | null | undefined,
): ProviderCapabilities | null {
@@ -748,8 +593,7 @@ const NO_REASONING_CAPS: ReasoningCaps = {
const ANTHROPIC_REASONING_MODELS = [
{
- // Fable / Mythos 5 think in adaptive mode always: `thinking.type
- // "disabled"` 400s, so there is no off switch to offer.
+ // Fable / Mythos 5 always think: `thinking.type "disabled"` 400s, so there is no off switch.
prefixes: ["claude-fable-5", "claude-mythos-5"],
supportsOff: false,
levels: ["low", "medium", "high", "xhigh", "max"],
@@ -806,8 +650,7 @@ const OPENAI_REASONING_MODELS = [
levels: ["medium", "high", "xhigh"],
},
{
- // gpt-5.6 (sol/terra/luna) rejects "minimal"; the ladder is the same as
- // gpt-5.5 / gpt-5.4.
+ // gpt-5.6 (sol/terra/luna) rejects "minimal"; the ladder is the same as gpt-5.5 / gpt-5.4.
prefixes: ["gpt-5.6", "gpt-5.5", "gpt-5.4"],
supportsOff: true,
levels: ["none", "low", "medium", "high", "xhigh"],
@@ -904,11 +747,9 @@ function withReasoningEffortStyle(caps: ReasoningCaps): ExternalReasoningCapabil
}
function resolveKimiReasoningCapabilities(modelId: string): ExternalReasoningCapabilities {
- // Kimi exposes a boolean thinking toggle, not an effort scale.
- // - kimi-k2.6: on by default, toggleable via
- // extra_body: {thinking: {type: enabled|disabled}}
- // - kimi-k2-thinking: always on, no off switch
- // - kimi-k2.5 (and others): no thinking
+ // Kimi has a boolean thinking toggle, not an effort scale: k2.6 on by default and
+ // toggleable, k2-thinking always on, k2.5 and others none.
+ // k2.6 is toggled via extra_body: {thinking: {type: enabled|disabled}}.
if (modelId === "kimi-k2-thinking") {
return withEnableThinkingStyle({
supportsReasoning: true,
@@ -924,18 +765,10 @@ function resolveKimiReasoningCapabilities(modelId: string): ExternalReasoningCap
return withEnableThinkingStyle();
}
-// Gemini's thinking ladder.
-// - Gemini 3.x (Pro + Flash + Flash-Lite) and the gemini-pro-latest /
-// gemini-flash-latest aliases use the string `thinkingConfig.thinkingLevel`
-// (LOW/MEDIUM/HIGH/MINIMAL); Pro tier rejects MINIMAL.
-// - Gemini 2.5 Flash + 2.5 Pro use the integer `thinkingConfig.thinkingBudget`
-// (0=off on Flash, -1=dynamic, N>0=cap; Pro rejects 0).
-// - 2.5 Flash-Lite: no native thinking surfaced; leave off.
-// - Image-tier ids: image generation path -- no reasoning controls.
-// Match the 3.x minors by pattern rather than enumerating them: a new
-// `gemini-3.6-flash` otherwise falls through to the 2.5 branch and gets the
-// integer thinkingBudget ladder, which Gemini 3 rejects. Mirrors
-// `_GEMINI3_FAMILY` / `_GEMINI3_PRO` in the backend's external_provider.py.
+// Gemini's thinking ladder: 3.x uses the string thinkingLevel (Pro rejects MINIMAL);
+// 2.5 Flash/Pro use integer thinkingBudget (0=off on Flash, -1=dynamic, Pro rejects 0);
+// 2.5 Flash-Lite and image ids get none. 3.x minors match by pattern so a new
+// `gemini-3.6-flash` cannot fall into the 2.5 branch. Mirrors _GEMINI3_FAMILY/_GEMINI3_PRO.
const GEMINI3_PRO_PATTERN = /^gemini-3(\.\d+)?-pro/;
const GEMINI3_FLASH_PATTERN = /^gemini-3(\.\d+)?-flash/;
const GEMINI3_PRO_PREFIXES = ["gemini-pro-latest"];
@@ -961,10 +794,9 @@ function resolveGeminiReasoningCapabilities(
// Image generation; no thinking knob.
return withEnableThinkingStyle();
}
- // Gemini 2.5 Flash-Lite: `thinkingBudget` 0 = off, positive range from 512
- // (backend maps "minimal" to that floor in _stream_gemini). Check this branch
+ // Gemini 2.5 Flash-Lite: thinkingBudget 0 = off, positive from 512. Must be checked
// BEFORE the broader `gemini-2.5-flash` prefix.
- // https://ai.google.dev/gemini-api/docs/thinking
+ // The backend maps "minimal" to that 512 floor in _stream_gemini.
if (m.startsWith("gemini-2.5-flash-lite")) {
return withReasoningEffortStyle({
supportsReasoning: true,
@@ -980,9 +812,8 @@ function resolveGeminiReasoningCapabilities(
});
}
if (GEMINI3_PRO_PATTERN.test(m) || GEMINI3_PRO_PREFIXES.some((p) => m.startsWith(p))) {
- // Gemini 3.x Pro: thinkingLevel low/medium/high; cannot fully disable, and
- // "minimal" is rejected on Pro. Refs:
- // https://ai.google.dev/gemini-api/docs/thinking and
+ // Gemini 3.x Pro: thinkingLevel low/medium/high; cannot fully disable, and "minimal" is rejected on Pro.
+ // Refs: https://ai.google.dev/gemini-api/docs/thinking and
// https://docs.cloud.google.com/vertex-ai/generative-ai/docs/models/gemini/3-1-pro.
return withReasoningEffortStyle({
supportsReasoning: true,
@@ -991,8 +822,7 @@ function resolveGeminiReasoningCapabilities(
});
}
if (GEMINI3_FLASH_PATTERN.test(m) || GEMINI3_FLASH_PREFIXES.some((p) => m.startsWith(p))) {
- // Gemini 3 Flash: thinkingLevel minimal/low/medium/high. Minimal is the
- // closest to "off" Google offers on Gemini 3.
+ // Gemini 3 Flash: thinkingLevel minimal/low/medium/high. Minimal is the closest to "off" Google offers on Gemini 3.
return withReasoningEffortStyle({
supportsReasoning: true,
supportsReasoningOff: false,
@@ -1005,9 +835,8 @@ function resolveGeminiReasoningCapabilities(
});
}
if (GEMINI25_PRO_PREFIXES.some((p) => m.startsWith(p))) {
- // Gemini 2.5 Pro: thinkingBudget cannot be 0 (API rejects "only works in
- // thinking mode"); backend coerces to a small positive budget. Hide the off
- // switch in the picker.
+ // Gemini 2.5 Pro rejects thinkingBudget 0, so the backend coerces to a small positive
+ // budget and the off switch is hidden.
return withReasoningEffortStyle({
supportsReasoning: true,
supportsReasoningOff: false,
@@ -1057,7 +886,7 @@ export interface ExternalReasoningResolveOptions {
baseUrl?: string | null;
}
-// vLLM has no per-model reasoning signal on OpenAI-compat — pin via user toggle.
+// vLLM has no per-model reasoning signal on OpenAI-compat, so pin via user toggle.
function resolveConnectionLevelReasoning(
normalizedProvider: string,
options: ExternalReasoningResolveOptions | undefined,
@@ -1071,10 +900,8 @@ function resolveConnectionLevelReasoning(
return null;
}
-/**
- * Resolve external-model thinking capabilities. Provider-specific matching lives
- * in the per-provider resolvers; others default to no reasoning controls.
- */
+/** Resolve external-model thinking capabilities. Per-provider resolvers do the matching;
+ * anything else defaults to no reasoning controls. */
export function getExternalReasoningCapabilities(
providerType: string | null | undefined,
modelId: string | null | undefined,
@@ -1093,8 +920,8 @@ export function getExternalReasoningCapabilities(
return withEnableThinkingStyle();
}
- // Some OpenRouter-routed ids are mandatory-reasoning and must stay on even
- // if they arrive through aliased/custom provider routes.
+ // Some OpenRouter-routed ids are mandatory-reasoning and must stay on even when they
+ // arrive through aliased or custom provider routes.
if (isOpenRouterMandatoryReasoningModel(normalizedModel)) {
return withEnableThinkingStyle({
supportsReasoning: true,
@@ -1116,9 +943,8 @@ export function getExternalReasoningCapabilities(
const isMistralProvider = normalizedProvider === "mistral";
const isOpenRouterProvider = normalizedProvider === "openrouter";
if (isOpenRouterProvider) {
- // OpenRouter's unified `reasoning` param is accepted on every request; the
- // gateway no-ops for non-reasoning models. Mandatory-reasoning ids are
- // handled by the early guard; everything else gets a toggleable control.
+ // OpenRouter's unified `reasoning` param is accepted everywhere and no-ops for non-reasoning
+ // models, so past the mandatory guard everything gets a toggleable control.
return {
supportsReasoning: true,
reasoningStyle: "enable_thinking",
@@ -1130,9 +956,8 @@ export function getExternalReasoningCapabilities(
if (isKimiProvider) return resolveKimiReasoningCapabilities(modelForMatching);
if (isMistralProvider) return resolveMistralReasoningCapabilities(modelForMatching);
if (normalizedProvider === "gemini") {
- // Custom Gemini OAI-compat gateways route through /chat/completions, which
- // drops the native thinkingConfig payload. Hide the native thinking ladder
- // so the UI doesn't advertise a control the backend can't honor.
+ // Custom Gemini OAI-compat gateways route through /chat/completions, which drops the native
+ // thinkingConfig payload, so hide the ladder.
if (isGeminiCustomOpenAICompatBase(options?.baseUrl)) {
return withEnableThinkingStyle();
}
diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsx
index 21fcc0e8255..896c06cf1f2 100644
--- a/studio/frontend/src/features/chat/runtime-provider.tsx
+++ b/studio/frontend/src/features/chat/runtime-provider.tsx
@@ -364,10 +364,10 @@ class VisionImageAdapter implements AttachmentAdapter {
class PDFAttachmentAdapter implements AttachmentAdapter {
accept = "application/pdf";
- // Refused here, not at send: the composer empties itself before it awaits send(), so a
- // ceiling that only fires there discards the typed message too. The throw is invisible
- // (nothing subscribes to attachmentAddError, the picker never awaits addAttachment), so
- // the toast is the only thing telling the user why no file appeared.
+ // Refused here, not at send: the composer empties itself before it awaits send(), so a ceiling
+ // that only fires there discards the typed message too. The throw is invisible: nothing
+ // subscribes to attachmentAddError and the picker never awaits addAttachment, so the toast is
+ // the only thing telling the user why no file appeared.
add({ file }: { file: File }): Promise<PendingAttachment> {
const sizeError = getDocumentAttachmentSizeError(file, "PDF");
if (sizeError) {
@@ -402,9 +402,8 @@ class PDFAttachmentAdapter implements AttachmentAdapter {
}
class TextAttachmentAdapter implements AttachmentAdapter {
- // MIME is unreliable for source files, so also match by extension
- // (assistant-ui's fileMatchesAccept supports ".ext" entries). Covers svg, code,
- // config and other plain-text formats; html keeps its own adapter below.
+ // MIME is unreliable for source files, so also match by extension (assistant-ui's
+ // fileMatchesAccept supports ".ext"). Covers svg, code and plain text; html has its own.
accept = TEXT_ATTACHMENT_ACCEPT;
async add({ file }: { file: File }): Promise<PendingAttachment> {
@@ -477,8 +476,7 @@ class TextAttachmentAdapter implements AttachmentAdapter {
content: [
{
type: "text",
- // A pasted file gets its own tag and size, the markers that outlive
- // the File once the message is stored.
+ // A pasted file gets its own tag and size, the markers that outlive the File once the message is stored.
text: attachmentContentText(
attachment.name,
text,
@@ -531,9 +529,8 @@ class DocxAttachmentAdapter implements AttachmentAdapter {
accept =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
- // The archive's own parts are checked too, not just the upload ceiling: a small .docx
- // can declare a part that only mammoth's inflate grows past the cap, and refusing that
- // at send() would empty the composer.
+ // The archive's own parts are checked too: a small .docx can declare a part that only mammoth's
+ // inflate grows past the cap, and refusing that at send() would empty the composer.
async add({ file }: { file: File }): Promise<PendingAttachment> {
const error = await getDocxAttachmentError(file);
if (error) {
@@ -672,9 +669,8 @@ function extractTextParts(m: ThreadMessage | undefined): string {
.trim();
}
-// A paste leaves the message's text in an attachment, so a title built from
-// inline text alone is "New Chat" for a paste-only turn and the bare
-// instruction for "summarise this" plus a paste. The sample is bounded.
+// A paste leaves the message's text in an attachment, so a title built from inline text alone
+// is "New Chat" for a paste-only turn. The sample is bounded.
function titleTextOf(m: ThreadMessage | undefined): string {
const text = extractTextParts(m);
if (m?.role !== "user") return text;
@@ -730,9 +726,8 @@ async function generateTitleWithModel(payload: {
repetition_penalty: 1.0,
enable_thinking: false,
reasoning_effort: "none",
- // Titling is a one-shot summarisation: never let it enter the tool loop.
- // Omitting the field would inherit the server's tools-on default and put
- // python/terminal schemas in a 24-token prompt.
+ // Titling is a one-shot summarisation: never let it enter the tool loop. Omitting the field
+ // would inherit the server's tools-on default and put tool schemas in a 24-token prompt.
enable_tools: false,
messages: [
{
@@ -807,15 +802,11 @@ function toThreadMessage(m: MessageRecord): ThreadMessage {
hasRunId &&
generationStatus === "completed" &&
custom.generationSettled !== true;
- // Persisted metadata alone must never restore a running message: a run that never
- // terminalised still says "running" on every later load, and `{type:"running"}`
- // unmounts Send, so the reply the user cannot stop is the one they cannot reply past.
- // Ask whether the run is corroborated live, and otherwise show the partial as
- // interrupted, which keeps every character and offers the Continue bar.
- // The gate is for unfinished runs only. A completed-but-unsettled run has a terminal
- // status, so /chat-runs/active never lists it and it could never be corroborated; it
- // still owns an unreplayed tail, and the follow that replays it returns on the first
- // snapshot rather than holding the composer.
+ // Persisted metadata alone must never restore a running message: a run that never terminalised
+ // still says "running" on every later load, and `{type:"running"}` unmounts Send. Ask
+ // whether the run is corroborated live, else show the partial as interrupted, which keeps
+ // every character and offers Continue. Unfinished runs only: a completed-but-unsettled run
+ // has a terminal status and is replayed by the follow instead.
const needsGenerationRecovery =
generationUnsettled ||
(generationUnfinished && generationIsCorroboratedLive(custom, m.threadId));
@@ -861,10 +852,8 @@ function scheduleGenerationRecovery(
const metadata = (storedMessage.metadata ?? {}) as Record<string, unknown>;
const runId = metadata.generationRunId;
if (typeof runId !== "string" || !generationNeedsRecovery(metadata)) return;
- // This tab is streaming the run itself. Following it from storage as well gives the reply
- // two writers, and the follower is always behind, so it imports a lagging prefix over the
- // live text. It also costs a PUT and a full re-parse per chunk against the same backend the
- // model is saturating.
+ // This tab is streaming the run itself: following it from storage as well gives the reply two
+ // writers, and the follower is always behind, so it imports a lagging prefix.
if (isLiveGenerationRun(runId)) return;
const existingRecovery = generationRecoveries.get(runId);
if (existingRecovery) {
@@ -908,13 +897,9 @@ function scheduleGenerationRecovery(
owner: serverCancel,
});
- /**
- * Write one state of the reply to storage and to every view showing it.
- *
- * `running` is the caller's, not derived here: a follow that hit its no-progress
- * deadline settles the message while its persisted run status is still non-terminal,
- * which is exactly the pair `generationNeedsRecovery` reads as "still going".
- */
+ /** Write one state of the reply to storage and to every view showing it. `running` is the
+ * caller's, not derived here: a follow that hit its no-progress deadline settles the message
+ * while its persisted run status is still non-terminal. */
const commit = async (
nextMetadata: Record<string, unknown>,
running: boolean,
@@ -932,8 +917,8 @@ function scheduleGenerationRecovery(
metadata: nextMetadata,
createdAt: storedMessage.createdAt,
}).catch(() => {
- // The producer may have committed a newer status between the event and
- // this write. Keep following; the terminal publish carries all content.
+ // The producer may have committed a newer status between the event and this write. Keep
+ // following; the terminal publish carries all content.
});
for (const view of views) {
@@ -946,10 +931,8 @@ function scheduleGenerationRecovery(
...item,
message: {
...item.message,
- // The status and the metadata are always this publish's:
- // they are what the recovery is following the run FOR. The
- // body is not, because a run this tab is also streaming is
- // replayed hundreds of characters behind the live reply.
+ // The status and the metadata are always this publish's, since they are what the recovery is
+ // following the run FOR. The body is not: a run this tab also streams replays far behind.
content: recoveredContentToImport(
item.message.content,
content,
@@ -1012,9 +995,8 @@ function scheduleGenerationRecovery(
try {
let lastPublishedStatus = "";
let identityValidated = false;
- // The follower reports its no-progress deadline by throwing, and the settlement
- // below is exactly what must happen when it does. Without this catch the throw
- // reaches the outer no-op handler and leaves a message marked running forever.
+ // The follower reports its no-progress deadline by throwing, and the settlement below is
+ // exactly what must happen then; without this catch the message stays running forever.
let followStalled = false;
try {
for await (const update of followChatGenerationRun(runId, {
@@ -1036,9 +1018,9 @@ function scheduleGenerationRecovery(
lastRequestMessage?.role === "assistant" &&
typeof lastRequestMessage.content === "string"
) {
- // Continue sends the old partial as an assistant prefill. The
- // server-owned placeholder is empty until the first client save,
- // so a reload before that save must seed replay from the request.
+ // Continue sends the old partial as an assistant prefill. The server-owned placeholder is
+ // empty until the first client save, so a reload before that save seeds replay from the
+ // request instead.
raw = lastRequestMessage.content;
}
}
@@ -1123,10 +1105,9 @@ function scheduleGenerationRecovery(
await publish(update.run);
}
if (isTerminalChatGenerationRun(update.run)) {
- // Only another successful active-list sync would otherwise drop it, so the
- // thread would keep reading as durable and a later subscriber-owned stream
- // on it would be capped, losing the checkpoints that are its only
- // persistence.
+ // Only another successful active-list sync would otherwise drop it, so the thread would keep
+ // reading as durable and a later subscriber-owned stream would be capped, losing the
+ // checkpoints that are its only persistence.
forgetServerActiveGenerationRun(runId);
}
}
@@ -1135,27 +1116,20 @@ function scheduleGenerationRecovery(
followStalled = true;
}
if (followStalled || generationNeedsRecovery(currentMetadata)) {
- // Fence the run before presenting the reply as resumable. Settling only in this
- // tab leaves the row queued/running/cancelling, and create_run refuses a thread
- // that already has an active generation, so Continue and the next message would
- // both 409 against a reply the UI had just declared finished. Best effort, and
- // not sufficient on its own: a producer wedged inside the engine stays in
- // `cancelling`, which is still active. That case is what the server-side sweeper
- // settles, and this path is reachable mainly when the sweeper is disabled.
+ // Fence the run before presenting the reply as resumable: settling only in this tab leaves the
+ // row active, and create_run refuses a thread that already has one, so Continue would 409.
+ // Best effort: a producer wedged inside the engine stays in `cancelling` for the sweeper.
serverCancel();
- // The follow returned with the run still non-terminal, so its no-progress
- // deadline expired. Settle the reply here rather than leaving a message that
- // says "running" until the next reload, which is what keeps Send unmounted.
- // Everything replayed so far is kept.
+ // The follow returned with the run still non-terminal, so its no-progress deadline expired.
+ // Settle the reply here rather than leaving a message that says "running" and keeps Send
+ // unmounted. Everything replayed so far is kept.
await commit(
{
...currentMetadata,
incomplete: { reason: "interrupted" as const },
- // The run row may still be non-terminal, so without this marker
- // generationNeedsRecovery stays true and the next online, pageshow or
- // visibility trigger starts another follower that republishes the message
- // as running. history.load clears it if /chat-runs/active still names the
- // run, so the server, not this tab, gets the last word.
+ // The run row may still be non-terminal, so without this marker generationNeedsRecovery stays
+ // true and the next trigger starts another follower. history.load clears it if
+ // /chat-runs/active still names the run, so the server gets the last word.
generationLocallyInterrupted: true,
},
false,
@@ -1200,7 +1174,7 @@ export async function ensureThreadRecord({
if (isChatThreadDeleted(threadId)) {
return;
}
- // Snapshot mutable creation inputs synchronously, before the await below. This runs in the same
+ // Snapshot mutable creation inputs synchronously, before the await below: this runs in the same
// tick as the user's send, so a toggle or checkpoint change during the point lookup cannot
// change the identity of the thread a retry persists.
const runtimeStateAtInit = useChatRuntimeStore.getState();
@@ -1213,8 +1187,7 @@ export async function ensureThreadRecord({
const createdAtInit = createdAt ?? Date.now();
// A temporary chat skips the history list so a storage outage cannot block its first send.
// Gated on the caller knowing the thread is new, not on its id: a `__LOCALID_` id is the
- // permanent key of every chat the app creates, so keying on the prefix tagged SAVED chats
- // incognito whenever a caller passed the open chat's id with the toggle on.
+ // permanent key of every chat the app creates, so keying on the prefix tagged SAVED chats.
if (incognitoAtInit && neverSent) {
markThreadIncognito(threadId);
return;
@@ -1224,8 +1197,8 @@ export async function ensureThreadRecord({
if (existing) {
return;
}
- // After the row check, so an already-persisted thread is never tagged: that is what keeps
- // a real chat saving normally when the toggle flips on mid-stream.
+ // After the row check, so an already-persisted thread is never tagged: that is what keeps a
+ // real chat saving normally when the toggle flips on mid-stream.
if (incognitoAtInit) {
markThreadIncognito(threadId);
return;
@@ -1246,9 +1219,8 @@ export async function ensureThreadRecord({
try {
await saveStoredChatThread(record);
} catch (error) {
- // assistant-ui can issue overlapping first-message persistence calls. If
- // another call created the same thread while this one waited, treat init as
- // successful and let the message write continue.
+ // assistant-ui can issue overlapping first-message persistence calls, so if another call
+ // created the same thread while this one waited, treat init as successful.
const existingAfterRace = await getStoredChatThread(threadId).catch(
() => undefined,
);
@@ -1273,9 +1245,8 @@ function createStudioDbAdapter(
}
return {
remoteId: thread.id,
- // Always regular: archive state is owned by the app's own controls.
- // Reporting archived here makes assistant-ui unarchive a chat the
- // moment it is opened.
+ // Always regular: archive state is owned by the app's own controls, and reporting archived here
+ // makes assistant-ui unarchive a chat the moment it is opened.
status: "regular",
title: thread.title,
};
@@ -1310,13 +1281,9 @@ function createStudioDbAdapter(
},
initialize(threadId: string) {
- // assistant-ui withholds the first message until this resolves, so the row write is tracked, not awaited.
- // Captured here, not inside the creator: a retry belongs to the send that initialized it,
- // not to a later incognito or checkpoint selection.
- // What the SEND was made under, not what is on screen now. Materialization is no longer
- // the same tick as the send, and all four move in between: the adapter is rebuilt with
- // the new project, and ChatPage's view effect clears `incognito`. A claim of null/false
- // wins over the store for the same reason.
+ // assistant-ui withholds the first message until this resolves, so the row write is tracked,
+ // not awaited. Captured here, not inside the creator: these describe what the SEND was made
+ // under, and all four move in between, since materialization is no longer the send's tick.
const claim = readThreadCreationClaim(threadId);
const runtimeStateAtInit = useChatRuntimeStore.getState();
const incognitoAtInit = claim ? claim.incognito : runtimeStateAtInit.incognito;
@@ -1335,17 +1302,16 @@ function createStudioDbAdapter(
pairId,
projectId: projectIdAtInit,
incognito: incognitoAtInit,
- // The one caller that can promise this: assistant-ui runs initialize() once, for
- // the id it just minted. Others hand in whatever chat is open.
+ // The one caller that can promise this: assistant-ui runs initialize() once, for the id it
+ // just minted. Others hand in whatever chat is open.
neverSent: true,
modelId: modelIdAtInit,
modelGgufVariant: modelGgufVariantAtInit,
createdAt: createdAtInit,
}),
);
- // A run already streaming on this thread filed its handles under "__default" because
- // the id did not exist yet. Re-key them now, or the sidebar row and Stop look up an
- // id nothing is registered against.
+ // A run already streaming on this thread filed its handles under "__default" because the id did
+ // not exist yet. Re-key them, or the sidebar row and Stop look up an unregistered id.
useChatRuntimeStore.getState().adoptDefaultThreadRun(threadId);
return Promise.resolve({ remoteId: threadId, externalId: undefined });
},
@@ -1361,9 +1327,8 @@ function createStudioDbAdapter(
},
async unarchive(remoteId: string) {
- // No-op on archive state: the app owns it via the sidebar menu and the
- // archived chats settings dialog. assistant-ui calls this when an
- // archived chat is opened, which must not unarchive it.
+ // No-op on archive state: the app owns it via the sidebar menu, and assistant-ui calls this
+ // when an archived chat is opened, which must not unarchive it.
await ensureStoredChatThread(remoteId);
},
@@ -1373,10 +1338,9 @@ function createStudioDbAdapter(
async generateTitle(remoteId: string, messages: readonly ThreadMessage[]) {
const autoTitle = useChatRuntimeStore.getState().autoTitle;
- // The run normally waits for its history append, but a bounded persistence wait can expire
- // while the creator is still queued. Use the same retry choke point as other mutations so a
- // temporarily missing row does not permanently skip first-turn title generation. A title is
- // cosmetic, so a row that never landed falls back to the default rather than rejecting here.
+ // The run normally waits for its history append, but a bounded persistence wait can expire while
+ // the creator is still queued, so use the same retry choke point as other mutations. A title
+ // is cosmetic, so a row that never landed falls back to the default.
const thread = await ensureStoredChatThread(remoteId).catch(
() => undefined,
);
@@ -1536,8 +1500,8 @@ function runStartThreadIdsForMessages(
async function waitForRunStartHistoryAppend(
messages: Parameters<ChatModelAdapter["run"]>[0]["messages"],
): Promise<string | undefined> {
- // Deep Research reserves an assistant placeholder before invoking the model
- // adapter, so the user message is not necessarily the final entry here.
+ // Deep Research reserves an assistant placeholder before invoking the model adapter, so the
+ // user message is not necessarily the final entry here.
const userMessage = [...messages]
.reverse()
.find((message) => message.role === "user");
@@ -1612,10 +1576,8 @@ function createPersistedRunAdapter(
if (reservationToken) {
releasePreStreamRunReservation(reservationToken);
}
- // Queued runs do not carry a direct-send reservation. Their persisted
- // preflight can still fail before the model adapter consumes the queued
- // settings. Stop matching pending/waiting work as well as an already
- // dispatched item so a rapid follow-up cannot run after persistence failed.
+ // Queued runs carry no direct-send reservation and their persisted preflight can still fail
+ // before the model adapter consumes the queued settings, so match pending/waiting work too.
requestPromptQueueStop(persistedRunThreadIds);
notifyPromptQueueRunFailed(
options.unstable_threadId ?? persistedRunThreadIds[0] ?? null,
@@ -1629,8 +1591,7 @@ function createPersistedRunAdapter(
]);
}
// The thread has an id by the time that resolves, but assistant-ui bound unstable_threadId
- // before the await. Hand the run its real id so a first turn never files its handles
- // under the unresolved key that concurrent runs share.
+ // before the await. Hand the run its real id so a first turn never uses the shared key.
const result = adapter.run(
!options.unstable_threadId && adoptedThreadId
? { ...options, unstable_threadId: adoptedThreadId }
@@ -1653,8 +1614,8 @@ function useStudioRuntimeAdapters(
pairId?: string,
reloadReadyThreadId?: string,
onInitialHistoryReady?: () => void,
- // A ref, so handing it down never changes the memoized runtime hook's identity: a new
- // hook identity would rebuild the runtime, which is the one thing this PR must not do.
+ // A ref, so handing it down never changes the memoized runtime hook's identity: a new identity
+ // would rebuild the runtime, which is the one thing this must not do.
backgroundedRef?: { current: boolean },
newThreadSwitchStateRef?: { current: NewThreadSwitchState },
): StudioRuntimeAdapters {
@@ -1685,9 +1646,8 @@ function useStudioRuntimeAdapters(
);
}, [aui]);
- // Mirror Data-tab attachment deletions into the loaded thread. The in-memory
- // repository otherwise keeps the attachment, and a later repo-to-storage sync
- // (e.g. deleting a message in the thread) would write it back.
+ // Mirror Data-tab attachment deletions into the loaded thread: the in-memory repository
+ // otherwise keeps the attachment, and a later repo-to-storage sync would write it back.
useEffect(() => {
let active = true;
let pendingDeletion = Promise.resolve();
@@ -1733,8 +1693,8 @@ function useStudioRuntimeAdapters(
return;
}
- // Preserve any messages added or streamed while WebCrypto ran.
- // Retry if the target's managed content itself changed.
+ // Preserve any messages added or streamed while WebCrypto ran; retry if the target's managed
+ // content itself changed.
const latest = thread.export();
const latestTarget = latest.messages.find(
(item) => item.message.id === messageId,
@@ -1848,8 +1808,8 @@ function useStudioRuntimeAdapters(
() => ({
async load() {
const completeLoad = <T,>(result: T, loadedThreadId?: string): T => {
- // A runtime bootstraps on an empty thread before switching to the
- // requested one, so an unrequested load is not readiness.
+ // A runtime bootstraps on an empty thread before switching to the requested one, so an
+ // unrequested load is not readiness.
const loadedTheRequestedThread =
!reloadReadyThreadId || loadedThreadId === reloadReadyThreadId;
if (onInitialHistoryReady) {
@@ -1892,22 +1852,17 @@ function useStudioRuntimeAdapters(
activeGenerationRuns = [];
activeGenerationRunsLoaded = false;
}
- // The endpoint is named for active runs but returns rows, so a run that
- // terminalised between the write and this read comes back with it, and
- // force-writing `generationSettled: false` over a finished reply revives it.
- // Take the still-live rows only and publish them as the corroboration
- // `toThreadMessage` restores a running status from. A failed read is silence,
- // not a report of "nothing is running".
+ // The endpoint is named for active runs but returns rows, so a run that terminalised between
+ // the write and this read comes back with it, and force-writing `generationSettled: false`
+ // over a finished reply revives it. Take the still-live rows only; a failed read is silence.
+ // Publish the still-live rows as the corroboration toThreadMessage restores a running status from.
activeGenerationRuns = activeGenerationRuns.filter(
(run) => !isTerminalChatGenerationRun(run),
);
- // And drop any run the message snapshot already shows as finished. The runs read
- // happens first, so a run that completed in between is still listed as running
- // here. Filtering the LIST rather than skipping at the overlay matters: the
- // registry sync below runs first, and a stale mapping there keeps the thread
- // reading as durable, so a later subscriber-owned stream on it would be capped
- // and lose everything it streamed past the cap. In another tab nothing else
- // would ever remove that mapping.
+ // And drop any run the message snapshot already shows as finished: the runs read happens first,
+ // so a run that completed in between is still listed. Filtering the LIST rather than
+ // skipping at the overlay matters, since a stale mapping keeps the thread reading as durable
+ // and a later subscriber-owned stream would be capped.
const terminalMessageRuns = new Set(
msgs
.filter((message) => {
@@ -1924,15 +1879,13 @@ function useStudioRuntimeAdapters(
activeGenerationRuns = activeGenerationRuns.filter(
(run) => !terminalMessageRuns.has(run.assistantMessageId),
);
- // The runs read happens BEFORE the messages read, so a run created between the
- // two would read as interrupted and re-enable the composer for a thread the
- // server is still generating on. Close that gap with one more lookup, and only
- // when a message actually names a run the first read missed.
+ // The runs read happens BEFORE the messages read, so a run created between the two would read
+ // as interrupted and re-enable the composer for a thread still generating. Close that gap
+ // with one more lookup, only when a message names a run the first read missed.
if (!activeGenerationRunsLoaded) {
- // The initial read failed, so this thread has no current answer. Retract any
- // answer from an earlier load: another tab may have started a run since, and
- // the stale answer would restore that live reply as interrupted and briefly
- // enable a conflicting send.
+ // The initial read failed, so this thread has no current answer. Retract any answer from an
+ // earlier load: another tab may have started a run since, and the stale answer would restore
+ // that live reply as interrupted.
markServerActiveGenerationRunsUnknown(remoteId);
}
if (activeGenerationRunsLoaded) {
@@ -1957,11 +1910,8 @@ function useStudioRuntimeAdapters(
(run) => !isTerminalChatGenerationRun(run),
);
} catch {
- // `missed` already proved the first list predates this run, so it is not
- // an answer either. Leave the thread unanswered rather than promoting a
- // list known to be stale, and retract any answer from an earlier load:
- // another tab may have started a run since, and the stale answer would
- // restore that live reply as interrupted.
+ // `missed` already proved the first list predates this run, so it is not an answer either.
+ // Leave the thread unanswered rather than promoting a list known to be stale.
answered = false;
markServerActiveGenerationRunsUnknown(remoteId);
}
@@ -1985,14 +1935,13 @@ function useStudioRuntimeAdapters(
generationStatus: run.status,
generationSettled: false,
serverManaged: true,
- // The server still has this run, which overrules a follower that gave up on
- // it locally. Clearing the marker here is what lets a genuinely slow run be
- // picked back up instead of staying interrupted for the life of the page.
+ // The server still has this run, which overrules a follower that gave up locally. Clearing the
+ // marker here is what lets a genuinely slow run be picked back up.
generationLocallyInterrupted: false,
};
}
- // Durable research can outlive this runtime. Reattach its server-owned
- // assistant message to the inline card after navigation or refresh.
+ // Durable research can outlive this runtime, so reattach its server-owned assistant message to
+ // the inline card after navigation or refresh.
const researchThreadState = await getResearchThreadState(
remoteId,
).catch(() => null);
@@ -2077,9 +2026,8 @@ function useStudioRuntimeAdapters(
const restoredUsage =
savedUsage && withinLocalLimit && modelMatches ? savedUsage : null;
if (restoredUsage) {
- // Key by the thread this loader read, not whichever is active when the await resolves:
- // a switch inside it would file this thread's usage under the incoming one. Same rule
- // the adapter's end-of-run write follows.
+ // Key by the thread this loader read, not whichever is active when the await resolves: a switch
+ // inside it would file this thread's usage under the incoming one.
store.setThreadContextUsage(remoteId, restoredUsage);
if (store.activeThreadId === remoteId) {
store.setContextUsage(restoredUsage);
@@ -2088,17 +2036,15 @@ function useStudioRuntimeAdapters(
// Only when nothing was restored: saved usage is the last completion's exact totals, and
// refreshContextUsage does NOT stand down for usage already there, so it would overwrite
// them with an estimate whose completionTokens is 0. A thread opened after a model switch
- // fails modelMatches and still gets priced (#7450).
- // Primary pane only: a compare pane never owns the global bar, so its count would be
- // rebuilt from storage, sent, then dropped at publish for not being activeThreadId.
+ // fails modelMatches and still gets priced (#7450). Primary pane only: a compare pane
+ // never owns the global bar.
if (!restoredUsage && modelType === "base" && !pairId) {
void refreshContextUsage({ threadId: remoteId });
}
- // If any message has a stored parentId, reconstruct the tree so
- // retries/regenerations load as branches rather than a flat list. For
- // mixed legacy/new threads, infer sequential parents for old messages to
- // preserve the chain. Fall back to fromArray for fully legacy threads.
+ // If any message has a stored parentId, reconstruct the tree so retries load as branches rather
+ // than a flat list, inferring sequential parents for old messages in mixed threads. Fall
+ // back to fromArray for fully legacy threads.
const hasParentIds = msgs.some((m) => m.parentId != null);
if (hasParentIds) {
const resolveParent = createParentResolver();
@@ -2143,25 +2089,21 @@ function useStudioRuntimeAdapters(
);
const write = (async () => {
const { remoteId } = await initializeThread;
- // The model run waits for the authoritative row. Clear-all does not: it tombstones this
- // known id directly, so a stalled request cannot hold the clear hostage.
+ // The model run waits for the authoritative row. Clear-all does not: it tombstones this known
+ // id directly, so a stalled request cannot hold the clear hostage.
await awaitStoredChatThreadWrites(remoteId);
if (isChatThreadDeleted(remoteId)) {
await deleteStoredChatThreads([remoteId]);
return;
}
- // published before the reads below: a temporary chat has no row to confirm, and a read
- // that fails must not leave the runtime pointing at the previously open chat
- //
- // ...but not while this pane is only mounted to keep its run attached: a hidden pane
- // naming itself active reaches Compare's exportThreadIds, so Export pulls the unrelated
- // base chat. Through a ref, read at publish time, since the write may have been queued
- // while the pane was still on screen.
- //
- // Nor mid-switch. switchToNewThread() is async, so mainThreadId is still the OUTGOING
- // thread for the whole gap and the guard below reads that as "this pane is on screen"; a
- // write landing there republishes the chat the user just left into the project they
- // navigated to. attempt !== landedAttempt is that gap.
+ // Published before the reads below: a temporary chat has no row to confirm, and a read that
+ // fails must not leave the runtime pointing at the previously open chat. Not while this pane
+ // is only mounted to keep its run attached, since a hidden pane naming itself active makes
+ // Export pull the unrelated base chat. Nor mid-switch: switchToNewThread() is async, so
+ // mainThreadId is still the OUTGOING thread for that gap, which the attempt check catches.
+ // Nor mid-switch: switchToNewThread() is async, so mainThreadId is still the OUTGOING thread for
+ // the whole gap, and a write landing there republishes the chat the user just left.
+ // attempt !== landedAttempt is that gap.
const switchState = newThreadSwitchStateRef?.current;
const switchInFlight = Boolean(
switchState &&
@@ -2228,14 +2170,12 @@ function useStudioRuntimeAdapters(
sameResearchRun ||
!incomingMetadata?.serverManaged ||
existingRevision > incomingRevision);
- // The backend owns this message and refuses client edits, and every field the save
- // would send was just read back from it, so the request is answered 409 every time.
- // One measured 43.6 s generation: 265 PUTs, 256 rejected, plus 353 whole-thread GETs
- // from the `ensureStoredChatThread` inside `saveStoredChatMessage`. Returning here
- // drops both.
- //
- // `parentId` is the one field not echoed back, so a reparent could differ. It is
- // dropped either way: the server rejects the whole request, so it never landed here.
+ // The backend owns this message and refuses client edits, and every field the save would send
+ // was just read back from it, so the request is answered 409 every time: one measured 43.6 s
+ // generation cost 265 PUTs, 256 rejected. `parentId` is the one field not echoed back, and
+ // it is dropped either way, since the server rejects the whole request.
+ // One measured 43.6 s generation: 265 PUTs, 256 rejected, plus 353 whole-thread GETs from the
+ // ensureStoredChatThread inside saveStoredChatMessage. Returning here drops both.
if (preserveServerManaged) {
await throwIfHistoryWasCleared(remoteId);
return;
@@ -2266,9 +2206,8 @@ function useStudioRuntimeAdapters(
],
);
- // Always register the adapter so the mic stays clickable for any engine. The
- // engine is resolved at listen() time and the composer shows guidance when it
- // cannot run, so engine switches also work on an already-mounted thread.
+ // Always register the adapter so the mic stays clickable for any engine: the engine is resolved
+ // at listen() time and the composer shows guidance when it cannot run.
const dictation = useMemo(() => new StudioDictationAdapter(), []);
const speech = useMemo(
() =>
@@ -2283,8 +2222,8 @@ function useStudioRuntimeAdapters(
new CompositeAttachmentAdapter([
new VisionImageAdapter(),
new AudioAttachmentAdapter(),
- // Before the document adapters: a composite takes the first match,
- // and .mkv/.mov must not fall through to them.
+ // Before the document adapters: a composite takes the first match, and .mkv/.mov must not fall
+ // through to them.
new VideoAttachmentAdapter(),
new TextAttachmentAdapter(),
new HtmlAttachmentAdapter(),
@@ -2366,26 +2305,21 @@ type PendingSavedThreadSwitch = { id: string; settled: boolean };
type NewThreadSwitchState = {
activeNonce: string | null;
hasSwitched: boolean;
- // Bumped by every switch this provider starts. The nonce alone does not identify an
- // attempt: leaving for a saved chat and coming back releases it, so two switches for
- // the SAME nonce can be in flight and the older must not speak for the newer.
+ // Bumped by every switch this provider starts. The nonce alone does not identify an attempt:
+ // leaving for a saved chat and coming back releases it, so two switches for the SAME nonce
+ // can be in flight.
attempt: number;
- // One entry per switch STARTED, so a nonce view recognises any of these ids landing after
- // the route moved on. By id rather than id SHAPE, which cannot tell a stale arrival from a
- // fresh switch's thread. Duplicates matter: two switches for one thread can overlap.
- // `settled` retires a claim with its own switch, so one that finished off-screen cannot be
- // spent by a later, legitimate open.
+ // One entry per switch STARTED, so a nonce view recognises any of these ids landing after the
+ // route moved on. By id rather than id SHAPE, which cannot tell a stale arrival from a fresh
+ // switch's thread. `settled` retires a claim with its own switch.
pendingSavedThreadIds: PendingSavedThreadSwitch[];
// The thread a nonce view is on, with the nonce it belongs to. Once the user has sent,
- // assistant-ui's newThreadId is gone and switchToNewThread() mints a SECOND blank thread
- // instead of restoring theirs. A ?new= URL survives materialization, so Back returns to the
- // same nonce; the pair is what tells a returning nonce from a new one.
+ // assistant-ui's newThreadId is gone and switchToNewThread() mints a SECOND blank thread; a
+ // ?new= URL survives materialization, so the pair is what tells a returning nonce from a new.
nonceThread: { nonce: string; threadId: string } | null;
- // The newest attempt whose own switch has LANDED. Ownership is only recorded from a thread
- // this nonce's switch actually opened: entering a nonce from a saved chat leaves that chat
- // as `mainThreadId` until switchToNewThread() resolves and its claim is already retired, so
- // "unclaimed and current" is not proof of ownership -- recording it there reopened the chat
- // the user had LEFT.
+ // The newest attempt whose own switch has LANDED. Ownership is only recorded from a thread this
+ // nonce's switch actually opened: entering a nonce from a saved chat leaves that chat as
+ // `mainThreadId` until switchToNewThread() resolves, so "unclaimed and current" is no proof.
landedAttempt: number;
};
@@ -2407,20 +2341,18 @@ function ThreadAutoSwitch({
const mainThreadId = useAuiState(({ threads }) => threads.mainThreadId);
useEffect(() => {
- // Paused as well as loading: requestTemporaryPromptQueueStop() names every temporary queue
- // on the page, not this provider's, so a backgrounded pane would stop a queue the on-screen
- // view owns. `paused` is a dependency, so both are paid on resume.
+ // Paused as well as loading: requestTemporaryPromptQueueStop() names every temporary queue on
+ // the page, not this provider's, so a backgrounded pane would stop the on-screen queue.
if (isLoading || paused) {
return;
}
newThreadSwitchStateRef.current.activeNonce = null;
if (mainThreadId !== threadId) {
- // Bumped, not read: this resolves asynchronously into a shared provider, so a newer
- // switch of EITHER kind must supersede it. Sharing one token across two saved
- // switches let a late rejection detach the chat already on screen.
+ // Bumped, not read: this resolves asynchronously into a shared provider, so a newer switch of
+ // EITHER kind must supersede it. One shared token let a late rejection detach the chat.
const attemptAtStart = (newThreadSwitchStateRef.current.attempt += 1);
- // One entry per switch started, duplicates included: A, B, A starts two A switches
- // and both can land, so each arrival must spend exactly one entry.
+ // One entry per switch started, duplicates included: A, B, A starts two A switches and both can
+ // land, so each arrival must spend exactly one entry.
const claims = newThreadSwitchStateRef.current.pendingSavedThreadIds;
const claim: PendingSavedThreadSwitch = { id: threadId, settled: false };
claims.push(claim);
@@ -2428,28 +2360,27 @@ function ThreadAutoSwitch({
if (claims.length > MAX_PENDING_SAVED_THREAD_SWITCHES) {
claims.splice(0, claims.length - MAX_PENDING_SAVED_THREAD_SWITCHES);
}
- // Saved chats keep running in the background, but a temporary chat is
- // unreachable after this switch and must not retain an active queue.
+ // Saved chats keep running in the background, but a temporary chat is unreachable after this
+ // switch and must not retain an active queue.
requestTemporaryPromptQueueStop();
const switchResult = aui.threads().switchToThread(threadId) as unknown;
if (
switchResult &&
typeof (switchResult as Promise<void>).then === "function"
) {
- // Both arms retire the claim, because both end the switch. A rejected switch never
- // assigns a main thread, so its claim could otherwise sit armed for ever.
+ // Both arms retire the claim, because both end the switch: a rejected switch never assigns a
+ // main thread, so its claim could otherwise sit armed for ever.
void (switchResult as Promise<void>).then(
() => {
claim.settled = true;
},
() => {
claim.settled = true;
- // Ahead of the staleness guard, deliberately (#9251): this releases the
- // retained reload shell, and a superseded attempt still ended.
+ // Ahead of the staleness guard, deliberately (#9251): this releases the retained reload shell,
+ // and a superseded attempt still ended.
onSwitchFailed?.();
- // Only if this switch is still the current one. Unguarded, a rejection landing
- // after the user moved to a project landing cleared the active id that view had
- // just set, detaching a chat this failure has nothing to do with.
+ // Only if this switch is still the current one: unguarded, a rejection landing after the user
+ // moved to a project landing cleared the active id that view had just set.
if (newThreadSwitchStateRef.current.attempt !== attemptAtStart) return;
if (syncActiveThreadId) {
useChatRuntimeStore.getState().setActiveThreadId(null);
@@ -2476,11 +2407,9 @@ function ThreadAutoSwitch({
if (isLoading || mainThreadId !== threadId) {
return;
}
- // The switch landed while this view is still mounted, so it was not stale and the
- // nonce view has nothing to correct. Released here rather than in the promise: this
- // effect only runs while the saved chat is on screen, which is exactly the condition.
- // Every SETTLED claim, not just this thread's: the view is stable here, so a finished
- // switch has had its chance to be corrected. In-flight ones can still land wrong.
+ // The switch landed while this view is still mounted, so it was not stale. Released here rather
+ // than in the promise, since this effect only runs while the saved chat is on screen. Every
+ // SETTLED claim, not just this thread's: in-flight ones can still land wrong.
const state = newThreadSwitchStateRef.current;
state.pendingSavedThreadIds = state.pendingSavedThreadIds.filter(
(claim) => !claim.settled,
@@ -2528,18 +2457,15 @@ function ThreadNewChatSwitch({
if (switchState.activeNonce === nonce) {
return;
}
- // Only this nonce's own thread, and only once sent to: a blank placeholder is still
- // replaced, as a new nonce and an untouched landing both expect.
+ // Only this nonce's own thread, and only once sent to: a blank placeholder is still replaced.
const recorded =
switchState.nonceThread?.nonce === nonce
? switchState.nonceThread.threadId
: null;
const runtimeThreads = aui.threads().__internal_getAssistantRuntime?.();
- // Guarded, unlike the reads elsewhere that pass an id the runtime just handed back: this
- // one is REMEMBERED, in a ref that outlives every view switch. getItemById() THROWS
- // "Entry not available in the store" for a dropped id rather than returning undefined, so
- // the optional chain would not catch it and the effect would take the app down. Unsloth
- // deletes through tombstones today; the point is not to depend on that.
+ // Guarded, unlike the reads elsewhere that pass an id the runtime just handed back: this one is
+ // REMEMBERED in a ref that outlives every view switch, and getItemById() THROWS for a
+ // dropped id rather than returning undefined, so the optional chain would not catch it.
let recordedRemoteId: string | undefined;
if (recorded) {
try {
@@ -2551,10 +2477,9 @@ function ThreadNewChatSwitch({
recordedRemoteId = undefined;
}
}
- // A tombstoned chat is not one to return to, for the same reason and by the same
- // mechanism as the resume restore above: deletion leaves the runtime item and its
- // remoteId intact, so "the store still knows it" is not "it still exists". Falling
- // through clears nonceThread below, which is what a nonce owning nothing looks like.
+ // A tombstoned chat is not one to return to: deletion leaves the runtime item and its remoteId
+ // intact, so "the store still knows it" is not "it still exists".
+ // Falling through clears nonceThread below, which is what a nonce owning nothing looks like.
const returningToOwnChat = Boolean(
recorded && recordedRemoteId && !isChatThreadDeleted(recordedRemoteId),
);
@@ -2571,8 +2496,8 @@ function ThreadNewChatSwitch({
switchState.hasSwitched = true;
const clearAttachments = () => {
try {
- // Chained, not just called: clearAttachments() removes each staged file through
- // the attachment adapter, so a rejecting remove() would go unhandled.
+ // Chained, not just called: clearAttachments() removes each staged file through the attachment
+ // adapter, so a rejecting remove() would go unhandled.
void Promise.resolve(aui.composer().clearAttachments()).catch(
() => undefined,
);
@@ -2583,13 +2508,11 @@ function ThreadNewChatSwitch({
if (shouldClearAttachments && !clearAfterSwitch) {
clearAttachments();
}
- // Saved chats keep running in the background. A temporary chat is never
- // persisted, so abandoning it must also discard its otherwise unreachable
- // queue. Queue provenance remains reliable even if incognito was cleared first.
+ // Saved chats keep running in the background, but a temporary chat is never persisted, so
+ // abandoning it must also discard its otherwise unreachable queue.
requestTemporaryPromptQueueStop();
- // A reopen is as cancellable as a saved switch: a project landing remounts with the
- // OLD nonce and rotates it in an effect, so the reopen is already in flight when the
- // rotation starts its own. Claimed so a reopen that resolves last is undone below.
+ // A reopen is as cancellable as a saved switch: a project landing remounts with the OLD nonce
+ // and rotates it in an effect, so the reopen is in flight when the rotation starts its own.
let reopenClaim: PendingSavedThreadSwitch | null = null;
if (returningToOwnChat && recorded) {
reopenClaim = { id: recorded, settled: false };
@@ -2605,8 +2528,8 @@ function ThreadNewChatSwitch({
);
}
}
- // Dropped when this nonce is still on screen: the reopen did its job and an armed
- // claim would have the correction undo it. Left armed only if the nonce moved on.
+ // Dropped when this nonce is still on screen: the reopen did its job and an armed claim would
+ // have the correction undo it.
const settleReopenClaim = () => {
if (!reopenClaim) return;
reopenClaim.settled = true;
@@ -2615,8 +2538,7 @@ function ThreadNewChatSwitch({
const at = switchStateNow.pendingSavedThreadIds.indexOf(reopenClaim);
if (at !== -1) switchStateNow.pendingSavedThreadIds.splice(at, 1);
};
- // Switch to a fresh local thread without persisting it yet; persistence
- // still happens on first message append.
+ // Switch to a fresh local thread without persisting it yet; persistence still happens on first message append.
void Promise.resolve(
returningToOwnChat && recorded
? aui.threads().switchToThread(recorded)
@@ -2624,9 +2546,8 @@ function ThreadNewChatSwitch({
).then(
() => {
settleReopenClaim();
- // This attempt's own switch has landed, so the main thread from here on is one it
- // opened. Only for the CURRENT attempt: a superseded switch landing late says
- // nothing about the thread a newer one is on.
+ // This attempt's own switch has landed, so the main thread from here on is one it opened. Only
+ // for the CURRENT attempt: a superseded switch landing late says nothing.
{
const switchStateNow = newThreadSwitchStateRef.current;
if (switchStateNow.attempt === attempt) {
@@ -2635,23 +2556,20 @@ function ThreadNewChatSwitch({
}
if (!clearAfterSwitch) return;
const switchStateNow = newThreadSwitchStateRef.current;
- // By attempt as well as nonce, matching the rejection arm. A saved-thread detour
- // releases the nonce, so returning starts a newer attempt; without the attempt check
- // this older completion still matches on nonce and wipes an attachment the newer one
- // staged. The nonce check stays: the detour nulls it without bumping the attempt.
+ // By attempt as well as nonce, matching the rejection arm: a saved-thread detour releases the
+ // nonce, so returning starts a newer attempt and this older completion would otherwise wipe
+ // an attachment the newer one staged.
if (switchStateNow.attempt !== attempt) return;
if (switchStateNow.activeNonce !== nonce) return;
clearAttachments();
},
() => {
settleReopenClaim();
- // The fresh thread never opened, so the view is still on the outgoing one.
- // Release the nonce, or the guard at the top of this effect reads it as already
- // served and the same New Chat can never be retried in place. Both arms are
- // handled, so a rejection is never unhandled.
+ // The fresh thread never opened, so the view is still on the outgoing one. Release the nonce, or
+ // the guard at the top reads it as served and the same New Chat can never be retried.
const switchStateNow = newThreadSwitchStateRef.current;
- // By attempt, not by nonce alone: a later switch for the same nonce owns the
- // thread it opened, and releasing it would switch away from a chat in use.
+ // By attempt, not by nonce alone: a later switch for the same nonce owns the thread it opened,
+ // and releasing it would switch away from a chat in use.
if (
switchStateNow.attempt === attempt &&
switchStateNow.activeNonce === nonce
@@ -2663,14 +2581,10 @@ function ThreadNewChatSwitch({
useChatRuntimeStore.getState().setActiveThreadId(null);
}, [aui, isLoading, newThreadSwitchStateRef, nonce, paused]);
- // Reassert this view's thread when a switch started before it lands anyway.
- //
- // Leaving a landing for a saved chat and coming back before switchToThread resolves used
- // to end with assistant-ui assigning that saved thread as the main one: the promise does
- // not know the route moved, and the shared provider has no remount to absorb it, so the
- // next message went to the wrong conversation. Recognised by the exact id that switch
- // asked for, which ThreadAutoSwitch records; the claim clears when honoured, so this fires
- // once per stale arrival.
+ // Reassert this view's thread when a switch started before it lands anyway. Leaving a landing
+ // for a saved chat and coming back before switchToThread resolves used to end with
+ // assistant-ui assigning that saved thread as the main one, since the promise does not know
+ // the route moved. Recognised by the exact id that switch asked for.
useEffect(() => {
if (isLoading || paused) {
return;
@@ -2679,11 +2593,9 @@ function ThreadNewChatSwitch({
if (switchState.activeNonce !== nonce) {
return;
}
- // Already on the thread this nonce owns, so there is nothing to correct -- and the claim
- // here is the sibling effect's OWN reopen. switchToThread() early-returns when its target
- // is already current (assistant-ui#2577), so the reopen resolves on a microtask and its
- // claim is still outstanding in the same commit. Reading that as a stale arrival replaced
- // the conversation with a blank chat, via Back from a materialized ?new= chat's own row.
+ // Already on the thread this nonce owns, and the claim here is the sibling effect's OWN reopen:
+ // switchToThread() early-returns when its target is already current (assistant-ui#2577), so
+ // the reopen resolves on a microtask and its claim is still outstanding in the same commit.
if (
mainThreadId &&
switchState.nonceThread?.nonce === nonce &&
@@ -2695,10 +2607,8 @@ function ThreadNewChatSwitch({
? switchState.pendingSavedThreadIds.findIndex((claim) => claim.id === mainThreadId)
: -1;
if (claimed === -1) {
- // Not a stale arrival, so this is the thread this view owns. Recorded here, not at
- // the switch: the id changes on materialization and a reattach needs the persisted one.
- // Only once this nonce's own switch has landed, though -- until then `mainThreadId` is
- // still the chat the user came FROM.
+ // Not a stale arrival, so this is the thread this view owns. Recorded here, not at the switch:
+ // the id changes on materialization and a reattach needs the persisted one.
if (mainThreadId && switchState.landedAttempt === switchState.attempt) {
switchState.nonceThread = { nonce, threadId: mainThreadId };
}
@@ -2716,9 +2626,8 @@ function ThreadNewChatSwitch({
).catch(() => undefined);
}, [aui, isLoading, mainThreadId, newThreadSwitchStateRef, nonce, paused]);
- // The effect above blanks the bar, and this view reaches no other recount trigger: no persisted
- // thread for the history loader, and ActiveThreadSync is off while a nonce is present. Keyed on
- // the model too: on a RELOAD of /chat?new=<uuid> nothing is known until status answers.
+ // The effect above blanks the bar, and this view reaches no other recount trigger. Keyed on the
+ // model too: on a RELOAD of /chat?new=<uuid> nothing is known until status answers.
useEffect(() => {
if (
isLoading ||
@@ -2733,10 +2642,9 @@ function ThreadNewChatSwitch({
const store = useChatRuntimeStore.getState();
if (store.activeThreadId != null || store.contextUsage != null) return;
void refreshContextUsage();
- // nonce: a fresh New Chat click re-runs the effect above, which blanks the bar again.
- // runActive is a DEPENDENCY, not just a guard: refreshContextUsage declines while anything
- // generates, and nothing else re-fires this when the run ends. ThreadContextUsageRecount
- // cannot cover for it -- an unpersisted New Chat has no activeThreadId.
+ // nonce: a fresh New Chat click re-runs the effect above, which blanks the bar again. runActive
+ // is a DEPENDENCY, not just a guard: refreshContextUsage declines while anything generates,
+ // and nothing else re-fires this when the run ends.
}, [
checkpoint,
loadedContextLength,
@@ -2785,28 +2693,23 @@ function NonceThreadResumeRestore({
if (useChatRuntimeStore.getState().activeThreadId != null) {
return;
}
- // Published raw, as ActiveThreadSync does on the paths this stands in for. A `__LOCALID_`
- // id is NOT a reason to skip: initialize() writes the row under whatever id assistant-ui
- // minted and hands it back, so skipping would make this a no-op for the ordinary case.
- // Consumers needing a persisted id filter for themselves.
+ // Published raw, as ActiveThreadSync does on the paths this stands in for. A `__LOCALID_` id is
+ // NOT a reason to skip: initialize() writes the row under whatever id assistant-ui minted.
if (!mainThreadId) {
return;
}
// ...but an UNTOUCHED landing must stay untouched: a compare round trip leaves a blank
- // placeholder here, and publishing it makes ProjectLanding swap the overview for an
- // empty Thread. On remoteId, not id shape (see above): a materialized chat keeps its
- // `__LOCALID_` id and has a remoteId; a placeholder has none.
+ // placeholder here, and publishing it makes ProjectLanding swap the overview for an empty
+ // Thread. On remoteId, not id shape.
const runtime = aui.threads().__internal_getAssistantRuntime?.();
const { remoteId } =
runtime?.threads.getItemById(mainThreadId).getState() ?? {};
if (!remoteId) {
return;
}
- // ...and neither must a chat the user deleted while they were away. Unsloth deletes by
- // tombstoning storage rather than calling runtime.threads.delete(), so the runtime item
- // and its remoteId both survive and every check above still passes. On remoteId, which
- // is the id storage and the sidebar delete agree on. Restoring here would undo
- // ProjectLanding's own tombstone check: this publishes the id that check refused to.
+ // ...and neither must a chat the user deleted while away. Unsloth deletes by tombstoning storage
+ // rather than calling runtime.threads.delete(), so every check above still passes. On
+ // remoteId, which is the id storage and the sidebar delete agree on.
if (isChatThreadDeleted(remoteId)) {
return;
}
@@ -2834,20 +2737,19 @@ function ThreadScopedSettingsSync({
useEffect(() => {
const { applyThreadScopedSettings } = useChatRuntimeStore.getState();
- // A chat not yet sent to has no row, so pairing it holds every edit behind a read certain
- // to 404 -- which is how an edit on a fresh /chat stopped reaching the installation
- // defaults. The `__LOCALID_` prefix stays on the id for good, so only the runtime's
+ // A chat not yet sent to has no row, so pairing it holds every edit behind a read certain to
+ // 404. The `__LOCALID_` prefix stays on the id for good, so only the runtime's
// pending-new-thread id tells the two apart.
if (activeThreadId !== null && activeThreadId === pendingNewThreadId) {
applyThreadScopedSettings(null, null);
return;
}
if (!enabled) {
- // Compare panes share one composer between two threads, so there is no single
- // chat whose snapshot could apply: compare runs on the installation defaults and
- // its edits move them, as every chat did before this change. Say so here rather
- // than leaving the module still pointing at the last single chat, whose stored
- // pills a model load would otherwise read back through threadScopedOverride.
+ // Compare panes share one composer between two threads, so no single chat's snapshot could
+ // apply: compare runs on the installation defaults and its edits move them. Said here rather
+ // than leaving the module pointing at the last single chat.
+ // Otherwise the module still points at the last single chat, whose stored pills a model load would
+ // read back through threadScopedOverride.
applyThreadScopedSettings(null, null);
return;
}
@@ -2855,15 +2757,15 @@ function ThreadScopedSettingsSync({
if (settingsHydrated) applyThreadScopedSettings(null, null);
return;
}
- // The composer is interactive while /api/chat/settings is still out, so start holding
- // this chat's edits as soon as its id is known. Waiting for hydration to begin the
- // pairing left that window writing edits into the installation defaults instead.
+ // The composer is interactive while /api/chat/settings is still out, so start holding this
+ // chat's edits as soon as its id is known; waiting for hydration left that window writing
+ // edits into the installation defaults.
beginThreadScopedPairing(activeThreadId);
if (!settingsHydrated) {
return () => {
- // Hydration finishing re-runs this effect for the same chat, and the held edits are
- // still waiting for that chat's read, so keep holding them. Any other reason to
- // leave means the chat is going away and they belong to it.
+ // Hydration finishing re-runs this effect for the same chat, and the held edits are still
+ // waiting for that chat's read, so keep holding them. Any other reason to leave means the
+ // chat is going away.
const now = useChatRuntimeStore.getState();
if (!now.settingsHydrated || now.activeThreadId !== activeThreadId) {
commitHeldThreadScopedEditsToTheirThread();
@@ -2876,9 +2778,8 @@ function ThreadScopedSettingsSync({
let defaulted = false;
let retryTimer: ReturnType<typeof setTimeout> | null = null;
let retriesLeft = THREAD_READ_RETRIES;
- // One per attempt, aborted when the attempt's deadline passes and when the chat is
- // left. Without it the losing side of the race below stayed open on the server for
- // the full write timeout while the next try opened another.
+ // One per attempt, aborted when the attempt's deadline passes and when the chat is left. Without
+ // it the losing side of the race stayed open on the server for the full write timeout.
const reads = new Set<AbortController>();
const abortReads = () => {
for (const read of reads) read.abort();
@@ -2887,42 +2788,34 @@ function ThreadScopedSettingsSync({
const sync = () => {
if (cancelled || paired) return;
- // the composer is live while this read is out, so hold any edit made in the meantime
- // rather than writing it to the installation defaults and then discarding it.
- // Drop to the installation defaults for the duration of the read. Until it lands the
- // store still holds the OUTGOING chat's values, and the composer is usable: a send in
- // that window is captured by snapshotQueuedChatRunSettings and carries the previous
- // chat's permission level and pills, so a chat stored as "ask" could run tools
- // without asking. The defaults are the only honest thing to show for a chat whose
- // own settings are not known yet, and a read that never resolves now leaves those
- // rather than another chat's.
- //
- // The pairing opened above survives this: a drop to the defaults for the chat that is
- // still open and still waiting on its read keeps holding rather than releasing.
+ // The composer is live while this read is out, so hold any edit made meanwhile rather than
+ // writing it to the installation defaults. Drop to those defaults for the duration: until
+ // the read lands the store still holds the OUTGOING chat's values, and a send in that window
+ // would carry the previous chat's permission level and pills.
+ // A send in that window is captured by snapshotQueuedChatRunSettings and carries the previous
+ // chat's permission level and pills, so a chat stored as "ask" could run tools without asking.
if (!defaulted) {
defaulted = true;
applyThreadScopedSettings(null, null);
}
beginThreadScopedPairing(activeThreadId);
- // Settle this chat's own PATCH first. Edit a chat, leave, come straight back and the
- // read can overtake the write and return the pre-edit snapshot, which then goes back
- // over the values the user set and is written out again by the next edit.
+ // Settle this chat's own PATCH first: edit a chat, leave, come straight back and the read can
+ // overtake the write and return the pre-edit snapshot.
const read = new AbortController();
reads.add(read);
- // The deadline covers the WAITS as well as the read: neither is bounded on its own (the
- // settings chain is a PATCH, and awaitStoredChatThreadWrites settles a row write opening
- // with an unbounded getStoredChatThread). In front of it their time went uncounted, so
- // the chain could outlast THREAD_PAIRING_WAIT_MS with the gate shut and the send refused.
- // Inside it, a stall is one failed attempt, which retryThreadRead handles.
+ // The deadline covers the WAITS as well as the read, neither of which is bounded on its own; in
+ // front of it their time went uncounted and the chain could outlast THREAD_PAIRING_WAIT_MS
+ // with the gate shut. Inside it, a stall is one failed attempt.
+ // The settings chain is a PATCH, and awaitStoredChatThreadWrites settles a row write opening with
+ // an unbounded getStoredChatThread. Inside the deadline a stall is one failed attempt, which
+ // retryThreadRead handles.
void Promise.race([
Promise.all([
- // This chat's own PATCH first: a read that overtakes it returns the pre-edit
- // snapshot, which then goes back over the values the user just set.
+ // This chat's own PATCH first: a read that overtakes it returns the pre-edit snapshot.
awaitThreadScopedSettingsWrite(activeThreadId),
- // And its row, which may not exist yet: initialize() resolves as soon as the id is
- // minted and leaves the POST tracked, so on a first send a read can overtake it, find
- // no row, and release this chat's held edits into the installation defaults. Settles
- // at once when nothing is tracked, so an existing chat waits for nothing.
+ // And its row, which may not exist yet: initialize() resolves as soon as the id is minted and
+ // leaves the POST tracked, so on a first send a read can overtake it and release this chat's
+ // held edits into the installation defaults.
awaitStoredChatThreadWrites(activeThreadId),
]).then(() =>
getStoredChatThreadReadResult(activeThreadId, {
@@ -2932,8 +2825,8 @@ function ThreadScopedSettingsSync({
),
new Promise<never>((_, reject) =>
setTimeout(() => {
- // The waits can expire before the fetch is issued; the controller is the only
- // thing stopping one issued a moment later from outliving them.
+ // The waits can expire before the fetch is issued; the controller is the only thing stopping one
+ // issued a moment later from outliving them.
read.abort();
reject(new Error("thread settings read timed out"));
}, THREAD_READ_TIMEOUT_MS),
@@ -2944,21 +2837,17 @@ function ThreadScopedSettingsSync({
})
.then(({ thread, cacheable }) => {
if (cancelled || paired) return;
- // A legacy fallback row means the backend GET FAILED and Dexie answered instead.
- // That is the failure case, not a confirmed missing row: the thread may well
- // have a snapshot on the server, so keep holding and retry rather than
- // releasing this chat's edits into the installation defaults.
+ // A legacy fallback row means the backend GET FAILED and Dexie answered instead: that is the
+ // failure case, not a confirmed missing row, so keep holding and retry.
if (thread && !cacheable) {
retryThreadRead();
return;
}
// a legacy fallback row carries no snapshot, and pinning would overwrite the real one.
if (!thread) {
- // a new chat's runtime-made id has no row yet, so it stays on the global
- // settings. The answer is in: there is no snapshot to wait for, so an edit
- // held for it is a plain default change and goes out now. Deferring that to a
- // second missing read meant an unsaved chat's click was written to a row that
- // does not exist, or attached to the chat once it was saved.
+ // A new chat's runtime-made id has no row yet, so it stays on the global settings. The answer is
+ // in, so an edit held for it is a plain default change and goes out now; deferring that
+ // wrote an unsaved chat's click to a row that does not exist.
releaseHeldThreadScopedEdits();
if (unpaired) return;
unpaired = true;
@@ -2974,25 +2863,21 @@ function ThreadScopedSettingsSync({
: null,
);
})
- // A failed read leaves the installation defaults up (dropped to above), not the
- // outgoing chat's settings, which would otherwise stay live indefinitely. The held
- // edit goes to the chat it was made in.
+ // A failed read leaves the installation defaults up, not the outgoing chat's settings, which
+ // would otherwise stay live indefinitely. The held edit goes to the chat it was made in.
.catch(() => retryThreadRead());
};
- // The read did not answer for this chat. Send what is held to the chat it was made
- // in, then keep the chat paired, or every later edit in it would fall through to the
- // installation defaults for as long as it stays open. A fresh browser with no legacy
- // cache has nothing else to fall back on, so retry a bounded few times.
+ // The read did not answer for this chat: send what is held to the chat it was made in, then
+ // keep the chat paired, or every later edit would fall through to the installation defaults.
+ // Retry a bounded few times.
const retryThreadRead = () => {
if (cancelled) return;
commitHeldThreadScopedEditsToTheirThread();
if (retryTimer !== null) return;
if (retriesLeft <= 0) {
- // Out of tries. Staying paired would hold every send behind "Loading this
- // chat's settings" with nothing left to resolve it, so give up openly: the
- // chat runs on the installation defaults, which is what it is already
- // showing, and say so once rather than failing silently.
+ // Out of tries. Staying paired would hold every send behind "Loading this chat's settings" with
+ // nothing left to resolve it, so give up openly and say so once.
applyThreadScopedSettings(null, null);
releaseHeldThreadScopedEdits();
toast.error("Could not load this chat's settings", {
@@ -3001,9 +2886,7 @@ function ThreadScopedSettingsSync({
});
return;
}
- // Keep the chat paired between tries, or an edit in it would fall through to
- // the installation defaults. A fresh browser with no legacy cache has nothing
- // else to fall back on, so it is worth a few goes.
+ // Keep the chat paired between tries, or an edit in it would fall through to the installation defaults.
beginThreadScopedPairing(activeThreadId);
retriesLeft -= 1;
retryTimer = setTimeout(() => {
@@ -3017,11 +2900,10 @@ function ThreadScopedSettingsSync({
return () => {
cancelled = true;
if (retryTimer !== null) clearTimeout(retryTimer);
- // Nothing is waiting on these once the chat is gone, and leaving them running is
- // how an outage turned every chat opened during it into three open requests.
+ // Nothing is waiting on these once the chat is gone, and leaving them running is how an outage
+ // turned every chat opened during it into three open requests.
abortReads();
- // switched away mid-read: the edit belongs to the chat it was made in, not to the
- // installation defaults that every other snapshot-less chat follows.
+ // switched away mid-read: the edit belongs to the chat it was made in, not to the installation defaults.
commitHeldThreadScopedEditsToTheirThread();
window.removeEventListener(CHAT_HISTORY_UPDATED_EVENT, sync);
};
@@ -3056,9 +2938,8 @@ function ActiveBranchRegistrar({
}
// Price whichever thread the bar points at whenever it has nothing to show. Only two paths reach
-// it: (1) a model change empties contextUsageByThreadId and a mounted thread does not rerun its
-// history loader; (2) on a deep link to /chat/:id the history loader and status can each land
-// before the other, so neither independently timed callback counts.
+// it: a model change empties contextUsageByThreadId while a mounted thread does not rerun
+// its history loader, and on a deep link the loader and status can each land before the other.
function ThreadContextUsageRecount({
enabled,
}: { enabled: boolean }): ReactElement | null {
@@ -3067,8 +2948,7 @@ function ThreadContextUsageRecount({
const loadedContextLength = useChatRuntimeStore((s) => s.loadedContextLength);
const modelLoading = useChatRuntimeStore((s) => s.modelLoading);
// A DEPENDENCY, not just a guard: nothing else here changes when a run ends, so a count skipped
- // for being busy would never be retried. Every run, not just local ones, since that is what the
- // endpoint refuses on.
+ // for being busy would never be retried. Every run, since that is what the endpoint refuses on.
const runActive = useChatRuntimeStore((s) =>
Object.values(s.runningByThreadId).some(Boolean),
);
@@ -3099,8 +2979,8 @@ function ThreadContextUsageRecount({
return null;
}
-// Exposes the current thread's cancelRun() via the shared store so external
-// surfaces can stop an in-flight stream before deleting the thread.
+// Exposes the current thread's cancelRun() via the shared store so external surfaces can stop an
+// in-flight stream before deleting the thread.
function CancelRegistrar(): ReactElement | null {
const aui = useAui();
const mainThreadId = useAuiState(({ threads }) => threads.mainThreadId);
@@ -3148,9 +3028,8 @@ function CancelRegistrar(): ReactElement | null {
}
return;
}
- // assistant-ui enters its running state before adapter preflight turns
- // on runningByThreadId. Keep the only cancel handle after navigation,
- // then release it when assistant-ui reports that the run actually ended.
+ // assistant-ui enters its running state before adapter preflight turns on runningByThreadId, so
+ // keep the only cancel handle after navigation and release it when the run ends.
let unsubscribe = () => {};
unsubscribe = thread.subscribe(() => {
if (thread.getState().isRunning) {
@@ -3181,8 +3060,8 @@ function ThreadBackendAutosave({
const aui = useAui();
const saveChainRef = useRef(Promise.resolve());
const pendingFirstSavesRef = useRef(new Map<string, Promise<void>>());
- // A ref, not a saveThread dependency: the save may be queued while visible and resolve
- // after Compare hides the pane, so this must read at PUBLISH time, not schedule time.
+ // A ref, not a saveThread dependency: the save may be queued while visible and resolve after
+ // Compare hides the pane, so this must read at PUBLISH time.
const backgroundedRef = useRef(backgrounded);
backgroundedRef.current = backgrounded;
@@ -3217,10 +3096,9 @@ function ThreadBackendAutosave({
return;
}
- // The save still runs while backgrounded; only the PUBLICATION is suppressed, or a hidden
- // pane reaches Compare's exportThreadIds and Export pulls the unrelated base chat. Same
- // stand-down mid-switch, for the same reason as the history adapter's publication:
- // mainThreadId is still the OUTGOING thread until switchToNewThread() resolves.
+ // The save still runs while backgrounded; only the PUBLICATION is suppressed, or a hidden pane
+ // reaches Compare's exportThreadIds. Same stand-down mid-switch: mainThreadId is still the
+ // OUTGOING thread until switchToNewThread() resolves.
const switchState = newThreadSwitchStateRef.current;
const switchInFlight =
switchState.activeNonce !== null &&
@@ -3274,9 +3152,8 @@ function ThreadBackendAutosave({
[reportAutosaveError, saveThread],
);
- // runEnd only reaches whichever thread is main, so a thread that stops being main
- // mid-run never gets its own and would checkpoint for the life of the page. Ask the
- // runtime instead of trusting the event, as CancelRegistrar above already does.
+ // runEnd only reaches whichever thread is main, so a thread that stops being main mid-run never
+ // gets its own and would checkpoint for the life of the page. Ask the runtime instead.
const isRunActive = useCallback(
(threadId: string): boolean => {
const runtime = aui.threads().__internal_getAssistantRuntime?.();
@@ -3293,8 +3170,8 @@ function ThreadBackendAutosave({
[aui],
);
- // Keep one scheduler for the component lifetime so its timers remain stoppable. The refs
- // give it the latest queueSave and liveness check when dependencies change.
+ // Keep one scheduler for the component lifetime so its timers remain stoppable; the refs give it
+ // the latest queueSave and liveness check.
const queueSaveRef = useRef(queueSave);
useEffect(() => {
queueSaveRef.current = queueSave;
@@ -3316,10 +3193,9 @@ function ThreadBackendAutosave({
}, []);
useEffect(() => {
- // A hidden renderer may never get its next interval: Chromium throttles chained timers
- // to a wake a minute and a WebView can be parked outright, so checkpoint on the way
- // out. No beforeunload, matching flushSettingsOnPageHidden: it does not fire on every
- // platform and a Tauri quit never fires it at all.
+ // A hidden renderer may never get its next interval: Chromium throttles chained timers to a wake
+ // a minute and a WebView can be parked outright, so checkpoint on the way out. No
+ // beforeunload, matching flushSettingsOnPageHidden.
const flush = () => {
checkpointsRef.current?.flushAll();
};
@@ -3357,18 +3233,16 @@ function ThreadBackendAutosave({
return null;
}
-// True when the chat tab is visible. While false, ChatPage stays mounted (runtime +
-// autosave alive, so the stream survives) but its views/composers unmount, so no
-// body-portaled surface bleeds over the active tab. Defaults true for use elsewhere.
+// True when the chat tab is visible. While false, ChatPage stays mounted (runtime and autosave
+// alive, so the stream survives) but its views unmount, so no portaled surface bleeds through.
export const ChatActiveContext = createContext(true);
export function useChatActive(): boolean {
return useContext(ChatActiveContext);
}
-// True inside a Compare pane. Both panes mount the same message controls, so a
-// window-level chord would otherwise be answered by whichever pane mounted
-// first, whatever the user was looking at.
+// True inside a Compare pane. Both panes mount the same message controls, so a window-level
+// chord would otherwise be answered by whichever pane mounted first.
const ComparePaneContext = createContext(false);
export function useInComparePane(): boolean {
@@ -3395,21 +3269,18 @@ export function ChatRuntimeProvider({
newThreadNonce?: string;
syncActiveThreadId?: boolean;
listThreads?: boolean;
- // Mounted only to keep an in-flight run attached while another view is on screen. The
- // runtime stays alive; everything driving the shared single-chat state (active thread,
- // context bar, thread-scoped settings) stands down so it can't fight the visible view.
+ // Mounted only to keep an in-flight run attached while another view is on screen: the runtime
+ // stays alive, and everything driving the shared single-chat state stands down.
backgrounded?: boolean;
onInitialHistoryReady?: () => void;
}): ReactElement {
- // Read by the history adapter's own active-thread publication, which is the sibling of
- // ThreadBackendAutosave's and needs the same stand-down. Kept in a ref so the memo below
- // never sees it change: rebuilding the runtime hook would rebuild the runtime, and the
- // whole point of the shared provider is that it does not.
+ // Read by the history adapter's own active-thread publication, the sibling of
+ // ThreadBackendAutosave's, which needs the same stand-down. Kept in a ref so the memo below
+ // never sees it change, since rebuilding the runtime hook would rebuild the runtime.
const backgroundedRef = useRef(backgrounded);
backgroundedRef.current = backgrounded;
- // Declared before the memo below because the memo reads it. Same ref the switch
- // components mutate, so the adapter can tell "this pane is on screen" from "this pane is
- // the thread we are switching AWAY from and mainThreadId has not caught up yet".
+ // Declared before the memo below because the memo reads it. Same ref the switch components
+ // mutate, so the adapter can tell "this pane is on screen" from "this pane is being left".
const newThreadSwitchStateRef = useRef<NewThreadSwitchState>({
activeNonce: null,
hasSwitched: false,
@@ -3451,9 +3322,8 @@ export function ChatRuntimeProvider({
return (
<AssistantRuntimeProvider runtime={runtime} aui={aui}>
- {/* Pane identity for the tool-output store maps: the adapter prefixes its
- keys with this scope so concurrent panes with colliding tool ids
- ("call_0") can't bleed live output into each other's cards. */}
+ {/* Pane identity for the tool-output store maps: the adapter prefixes its keys with this scope so
+ concurrent panes with colliding tool ids cannot bleed output into each other's cards. */}
<ChatProjectScopeContext.Provider value={projectId ?? null}>
<ToolPaneScopeContext.Provider value={toolPaneScope(modelType, pairId)}>
<ComparePaneContext.Provider value={Boolean(pairId)}>
@@ -3466,13 +3336,10 @@ export function ChatRuntimeProvider({
!backgrounded
}
/>
- {/* Compare clears activeThreadId on the way in, and this view is hidden rather
- than unmounted, so nothing puts it back: the nonce is unchanged so
- ThreadNewChatSwitch returns, and ActiveThreadSync is off while a nonce is
- present. ThreadScopedSettingsSync is NOT nonce-gated, so the chat came back
- detached -- on installation defaults, its edits moving those instead of its
- own snapshot, with no title, context usage or model notice. ProjectLanding
- restores on resume for the same reason; this is the single-chat half. */}
+ {/* Compare clears activeThreadId on the way in and this view is hidden rather than unmounted, so
+ nothing puts it back: the nonce is unchanged so ThreadNewChatSwitch returns, and
+ ActiveThreadSync is off while a nonce is present. ThreadScopedSettingsSync is NOT
+ nonce-gated, so the chat came back detached, with no title or context usage. */}
<NonceThreadResumeRestore
enabled={
modelType === "base" &&
@@ -3514,8 +3381,8 @@ export function ChatRuntimeProvider({
newThreadSwitchStateRef={newThreadSwitchStateRef}
/>
)}
- {/* The view stays mounted (only CSS-hidden) while off-route so the run
- stays attached and the stream alive; unmounting aborts generation. */}
+ {/* The view stays mounted (only CSS-hidden) while off-route so the run stays attached and the
+ stream alive; unmounting aborts generation. */}
{children}
</ComparePaneContext.Provider>
</ToolPaneScopeContext.Provider>
diff --git a/studio/frontend/src/features/chat/search-images/search-images.ts b/studio/frontend/src/features/chat/search-images/search-images.ts
index 9b4e226eb0a..40a10bfb303 100644
--- a/studio/frontend/src/features/chat/search-images/search-images.ts
+++ b/studio/frontend/src/features/chat/search-images/search-images.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// web_search image results: the backend keeps the URLs and hands out opaque
-// `[[img:<id>]]` tokens, which resolve here against that tool call's envelope.
+// web_search image results: the backend keeps the URLs and hands out opaque `[[img:<id>]]`
+// tokens, resolved here against that tool call's envelope.
import { findCodeBlockRegions, isInRegion } from "../../../lib/latex.ts";
@@ -97,10 +97,8 @@ export function searchImagePath(id: string): string {
return `/api/inference/search-images/${encodeURIComponent(id)}`;
}
-/**
- * `[[img:<id>]]` -> `<search-image token="…">`, skipping code; unknown ids are dropped.
- * `token`, not `id`: rehype-sanitize prefixes `id` values with `user-content-`.
- */
+/** `[[img:<id>]]` -> `<search-image token="...">`, skipping code; unknown ids are dropped.
+ * `token`, not `id`: rehype-sanitize prefixes `id` values with `user-content-`. */
export function rewriteSearchImageTokens(
text: string,
known: { has(id: string): boolean },
@@ -114,18 +112,15 @@ export function rewriteSearchImageTokens(
});
}
-/**
- * Drop every `[[img:<id>]]` the model wrote. The tokens are markup for the renderer,
- * so anything that hands the answer to the user as plain text — clipboard, markdown
- * export, read-aloud — has to strip them or they show up verbatim.
- */
+/** Drop every `[[img:<id>]]` the model wrote. The tokens are markup for the renderer, so
+ * anything handing the answer to the user as plain text -- clipboard, export, read-aloud --
+ * has to strip them or they show up verbatim. */
export function stripSearchImageTokens(text: string): string {
if (!text.includes("[[img:")) return text;
const codeRegions = findCodeBlockRegions(text);
- // One pass over the original, so the code-region offsets stay valid. A token sits
- // in its own block, so the first branch takes the blank line that introduces it
- // too; dropping the token alone would leave a widening gap. Code is left alone, as
- // in rewriteSearchImageTokens: there the token is prose about the feature.
+ // One pass over the original, so the code-region offsets stay valid. A token sits in its own
+ // block, so the first branch takes the blank line introducing it too; dropping the token
+ // alone would leave a widening gap. Code is left alone, where the token is prose.
return text.replace(
/\n\n[ \t]*\[\[img:[0-9a-f]{12}\]\][ \t]*(?=\n\n|\n?$)|\[\[img:[0-9a-f]{12}\]\]/g,
(match, offset: number) => (isInRegion(offset, codeRegions) ? match : ""),
@@ -170,10 +165,9 @@ function firstMatchOutsideCode(
const LIST_MARKER_RE = /^(\s*(?:[-*+•]|\d{1,2}[.)])\s+)/;
const HEADING_LINE_RE = /^\s*#{1,6}\s/;
const BLOCK_BREAK_RE = /^(?:\s*$|\s*(?:[-*+•]|\d{1,2}[.)])\s|\s*#{1,6}\s|\s*(?:```|~~~))/;
-// Display math, which BLOCK_BREAK_RE cannot see: a blank line inside `$$ ... $$`
-// read as the end of the block, and the card was spliced into the equation, so
-// KaTeX got markup instead of LaTeX. Bounded like latex.ts, so an unclosed `$$`
-// cannot run the scan over the whole answer.
+// Display math, which BLOCK_BREAK_RE cannot see: a blank line inside `$$ ... $$` read as the
+// end of the block and the card was spliced into the equation. Bounded like latex.ts, so an
+// unclosed `$$` cannot run the scan over the whole answer.
const DISPLAY_MATH_RE = /\$\$[\s\S]{0,4096}?\$\$|\\\[[\s\S]{0,4096}?\\\]/g;
function findDisplayMathRegions(text: string): Array<[number, number]> {
@@ -187,10 +181,8 @@ function findDisplayMathRegions(text: string): Array<[number, number]> {
return regions;
}
-/**
- * End of the block containing `index` — models wrap a list item across several
- * lines, and inserting at the end of the first one splits the sentence in half.
- */
+/** End of the block containing `index`. Models wrap a list item across several lines, and
+ * inserting at the end of the first one splits the sentence in half. */
function blockEndFrom(
text: string,
index: number,
@@ -216,10 +208,8 @@ function namesSameThing(a: string, b: string): boolean {
).test(longer);
}
-/**
- * Put each subject's image under the first line naming it. Subjects the answer never
- * names are left out, tokens the model placed win, and streaming text is untouched.
- */
+/** Put each subject's image under the first line naming it. Subjects the answer never names
+ * are left out, tokens the model placed win, and streaming text is untouched. */
export function placeSubjectImages(
text: string,
images: ReadonlyMap<string, SearchImageEntry>,
@@ -246,9 +236,9 @@ export function placeSubjectImages(
codeRegions: findCodeBlockRegions(part),
}));
for (const [key, entry] of bySubject) {
- // Outside code only, the same rule the subject match follows:
- // rewriteSearchImageTokens leaves a token inside a fence as literal text, so
- // counting that as "the model already placed it" showed no picture at all.
+ // Outside code only, the same rule the subject match follows: rewriteSearchImageTokens leaves
+ // a token inside a fence as literal text, so counting that as "already placed" showed no
+ // picture at all.
if (
messageParts.some(({ text: part, codeRegions: regions }) =>
tokenPlacedOutsideCode(part, entry.id, regions),
@@ -256,19 +246,18 @@ export function placeSubjectImages(
) {
continue;
}
- // On the original text: lowercasing can change length and shift every offset.
- // Global, so a mention inside code can be stepped over rather than ending the
- // search: `\`Go\`` in a snippet used to abandon the subject that a later prose
- // item names, and the picture was silently dropped.
+ // On the original text: lowercasing can change length and shift every offset. Global, so a
+ // mention inside code can be stepped over rather than ending the search: a name in a snippet
+ // used to abandon the subject a later prose item names.
const pattern = new RegExp(
`(^|[^\\p{L}\\p{N}])${escapeRegExp(key)}(?![\\p{L}\\p{N}])`,
"giu",
);
- // An earlier text part already carries this subject's card -- unless it only
- // said so in code, which shows no card and so must not suppress this one.
+ // An earlier text part already carries this subject's card -- unless it only said so in code,
+ // which shows no card and so must not suppress this one.
if (firstMatchOutsideCode(pattern, alreadyNamed, namedRegions) !== null) continue;
- // Where the NAME starts, not the boundary char before it: same line either way,
- // and the code-region test is about the name.
+ // Where the NAME starts, not the boundary char before it: same line either way, and the
+ // code-region test is about the name.
const at = firstMatchOutsideCode(pattern, text, codeRegions);
if (at === null) continue;
const lineStart = text.lastIndexOf("\n", at) + 1;
@@ -280,9 +269,8 @@ export function placeSubjectImages(
// A heading is a one-line block, so its own end is the insertion point.
insertions.push({ at: lineEnd, chunk: `\n\n[[img:${entry.id}]]` });
} else if (marker) {
- // Indented to the item's content column, so the card is a block inside the
- // item rather than a lazy continuation of its sentence, and the list keeps
- // its numbering.
+ // Indented to the item's content column, so the card is a block inside the item rather than a
+ // lazy continuation of its sentence, and the list keeps its numbering.
insertions.push({
at: blockEndFrom(text, at, mathRegions),
chunk: `\n\n${" ".repeat(marker[1].length)}[[img:${entry.id}]]`,
@@ -304,11 +292,9 @@ export function placeSubjectImages(
return out;
}
-// Split marker from subject, and never let two whitespace quantifiers span the same
-// run: the single-regex form nested `\s+`, `\s*` and a body class that also matched
-// spaces, which backtracked at ~O(n^3.5) -- 250 spaces in one bullet froze the thread
-// for 13s. The body is anchored on non-space edges so the surrounding runs cannot
-// overlap it; `.trim()` below made those edges dead weight anyway.
+// Split marker from subject, and never let two whitespace quantifiers span the same run: the
+// single-regex form backtracked at ~O(n^3.5), so 250 spaces in one bullet froze the thread
+// for 13s. The body is anchored on non-space edges so the surrounding runs cannot overlap.
const LIST_ITEM_MARKER_RE = /^[ \t]*(?:\d{1,2}[.)]|[-*+•])[ \t]+/;
const LIST_ITEM_RE =
/^(?:\*\*|__)?[ \t\r]*([^\s*_\n][^*_\n]{0,58}?[^\s*_\n])(?:[ \t\r]*(?:\*\*|__))?[ \t\r]*(?::|[-–—][ \t\r]|\(|$)/;
@@ -464,10 +450,8 @@ export function precedingTextForMessagePart(
return answerTextFromParts(parts.slice(0, partIndex));
}
-/**
- * The lead of each listed item or small heading (`1. **German Shepherd:** …`).
- * Short names only; a procedure, a code answer or a single item yields nothing.
- */
+/** The lead of each listed item or small heading. Short names only; a procedure, a code answer
+ * or a single item yields nothing. */
export function extractListSubjects(text: string): string[] {
if (text.includes("```") || text.includes("~~~")) return [];
const codeRegions = findCodeBlockRegions(text);
diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsx
index 6f5eff8b0b5..6f515ac2728 100644
--- a/studio/frontend/src/features/chat/shared-composer.tsx
+++ b/studio/frontend/src/features/chat/shared-composer.tsx
@@ -221,9 +221,8 @@ function isNativeComposing(event: Event) {
return "isComposing" in event && (event as InputEvent).isComposing === true;
}
-// Mirrors the threshold in thread.tsx. Chrome on Windows-over-WSL (#5546)
-// never fires `compositionend` after IME commit, so the compose flag would
-// otherwise stay true forever.
+// Mirrors the threshold in thread.tsx. Chrome on Windows-over-WSL (#5546) never fires
+// `compositionend` after IME commit, so the compose flag would otherwise stay true forever.
const IME_STUCK_TIMEOUT_MS = 2500;
function fileToBase64DataURL(file: File): Promise<string> {
@@ -259,8 +258,7 @@ function formatReasoningDisabledLabel(
modelId?: string,
): string {
const normalized = modelId?.trim().toLowerCase() ?? "";
- // Magistral keeps the "none" wire value, but UX presents this floor as
- // "Medium" rather than a disabled-state label.
+ // Magistral keeps the "none" wire value, but UX presents this floor as "Medium" rather than a disabled-state label.
if (normalized.includes("magistral-medium-latest")) return "Medium";
return supportsReasoningOff && isExternalOpenAIReasoning ? "None" : "Off";
}
@@ -271,8 +269,8 @@ function useDictation(
// Re-render support state when the user switches recognition engines.
const dictationEngine = useVoiceSettingsStore((s) => s.dictationEngine);
const [isDictating, setIsDictating] = useState(false);
- // True while a stopped recording's final audio is still transcribing; a
- // second click then cancels the pending transcription instead of re-stopping.
+ // True while a stopped recording's final audio is still transcribing; a second click then cancels
+ // the pending transcription instead of re-stopping.
const [isFinalizing, setIsFinalizing] = useState(false);
const sessionRef = useRef<StudioDictationSession | null>(null);
const startingRef = useRef(false);
@@ -289,9 +287,8 @@ function useDictation(
let session: StudioDictationSession;
try {
- // Routes to the engine chosen in Voice settings (browser or STT model),
- // honoring the selected microphone, language, and dictionary. Compare
- // feeds two panes, so recent dictations must not link the unrelated
+ // Routes to the engine chosen in Voice settings, honoring the selected microphone, language and
+ // dictionary. Compare feeds two panes, so recent dictations must not link the unrelated
// single-chat active thread.
session = new StudioDictationAdapter({ chatId: null }).listen();
} catch {
@@ -302,8 +299,7 @@ function useDictation(
sessionRef.current = session;
setIsDictating(true);
- // Append final transcripts; the adapter has already applied the dictionary
- // and records the session in Recent dictations.
+ // Append final transcripts; the adapter has already applied the dictionary and recorded the session.
session.onSpeech((result) => {
if (!result.isFinal) return;
const transcript = result.transcript?.trim() ?? "";
@@ -323,8 +319,8 @@ function useDictation(
const stop = useCallback(() => {
const session = sessionRef.current;
if (!session) return;
- // A second click while the final segment is transcribing discards the
- // pending transcription instead of leaving the pane stuck until timeout.
+ // A second click while the final segment is transcribing discards the pending transcription
+ // instead of leaving the pane stuck until timeout.
if (finalizingRef.current) {
session.cancel();
if (sessionRef.current === session) sessionRef.current = null;
@@ -335,8 +331,8 @@ function useDictation(
}
finalizingRef.current = true;
setIsFinalizing(true);
- // Keep the session and dictation state alive while its final audio segment
- // is transcribed. onEnd clears both after the transcript callbacks run.
+ // Keep the session and dictation state alive while its final audio segment is transcribed; onEnd
+ // clears both after the transcript callbacks run.
void session.stop().catch((error) => {
console.error("Could not stop dictation:", error);
session.cancel();
@@ -529,8 +525,8 @@ function PillGlyph({ children }: { children: ReactNode }) {
);
}
-/** True when a drop reached this composer from a portaled child, such as a
- * dialog and its overlay, which React routes through here but which owns it. */
+/** True when a drop reached this composer from a portaled child, such as a dialog and its
+ * overlay, which React routes through here but which owns it. */
function isPortaledDrop(event: ReactDragEvent): boolean {
const target = event.target as Element | null;
return !target?.closest?.(".chat-composer-surface");
@@ -717,10 +713,9 @@ export function SharedComposer({
mmprojFallbackReason,
});
const isCompareMode = Boolean(model1?.id || model2?.id);
- // Attach-time gate. Compare mode defers to send: the catalog can lag a
- // model's real capabilities (e.g. a GGUF whose mmproj arrives after the
- // snapshot), and models[] only syncs after ensureModelLoaded at send time.
- // Single mode uses the loaded model's runtime capability.
+ // Attach-time gate. Compare mode defers to send: the catalog can lag a model's real capabilities,
+ // and models[] only syncs after ensureModelLoaded at send time. Single mode uses the loaded
+ // model's runtime capability.
const attachUnavailableReason = isCompareMode ? null : imageUnavailableReason;
const effectiveExternalModelId =
selectedExternalProvider?.providerType === "openrouter" &&
@@ -756,10 +751,8 @@ export function SharedComposer({
const reasoningLockedOn =
effectiveSupportsReasoning &&
(effectiveReasoningAlwaysOn || !effectiveSupportsReasoningOff);
- // Kimi's $web_search builtin mandates thinking=disabled
- // (https://platform.kimi.ai/docs/guide/use-web-search). Both pills stay
- // clickable, but turning one on flips the other off; the click handlers
- // below enforce this so the visible state matches what the backend sends.
+ // Kimi's $web_search builtin mandates thinking=disabled. Both pills stay clickable, but turning
+ // one on flips the other off, so the visible state matches what the backend sends.
const isKimiExternal = selectedExternalProvider?.providerType === "kimi";
const effectiveReasoningEnabled = reasoningLockedOn ? true : reasoningEnabled;
const effectiveReasoningVisualEnabled =
@@ -767,27 +760,26 @@ export function SharedComposer({
const reasoningDisabled = !modelLoaded || !effectiveSupportsReasoning;
const showReasoningControl =
effectiveSupportsReasoning || effectiveReasoningAlwaysOn;
- // enable_thinking_effort (GLM-5.2: high|max + disable) reuses the effort
- // dropdown; it just also carries an Off row via supportsReasoningOff.
+ // enable_thinking_effort (GLM-5.2: high|max + disable) reuses the effort dropdown; it just also
+ // carries an Off row via supportsReasoningOff.
const isEffort =
effectiveReasoningStyle === "reasoning_effort" ||
effectiveReasoningStyle === "enable_thinking_effort";
- // GLM-5.2's effort menu (Off, high, max) has short rows, so it can sit a
- // touch skinnier. Skip the narrower floor when a Preserve thinking row is
- // present, since that longer label needs the wider width to stay one line.
+ // GLM-5.2's effort menu has short rows, so it can sit skinnier. Skip the narrower floor when a
+ // Preserve thinking row is present, since that label needs the wider width.
const narrowEffortMenu =
effectiveReasoningStyle === "enable_thinking_effort" &&
!supportsPreserveThinking;
const thinkingActiveLook = isEffort
? reasoningLockedOn || (effectiveReasoningVisualEnabled && !reasoningDisabled)
: reasoningLockedOn || (effectiveReasoningEnabled && !reasoningDisabled);
- // Two-pill gating: Search lights up on a local tool runtime (supportsTools:
- // Code/python + local web_search) OR a provider-run server-side web_search
- // (supportsBuiltinWebSearch: OpenAI/Anthropic/OpenRouter/Kimi). Code lights
- // up on the local runtime OR Anthropic with a model accepting the
- // server-side code_execution_20250825 tool (see
- // providerSupportsBuiltinCodeExecution). Anthropic is the only external
- // provider shipping a code-execution tool today.
+ // Two-pill gating: Search lights up on a local tool runtime OR a provider-run server-side
+ // web_search; Code on the local runtime OR Anthropic with a model accepting
+ // code_execution_20250825, the only external code-execution tool today.
+ // Search: supportsTools (Code/python plus local web_search) OR supportsBuiltinWebSearch
+ // (OpenAI/Anthropic/OpenRouter/Kimi). Code: the local runtime OR Anthropic with a model taking
+ // code_execution_20250825, per providerSupportsBuiltinCodeExecution. Anthropic is the only
+ // external provider shipping a code-execution tool today.
const supportsBuiltinCodeExecution = providerSupportsBuiltinCodeExecution(
selectedExternalProvider?.providerType,
effectiveExternalModelId,
@@ -801,23 +793,20 @@ export function SharedComposer({
const supportsBuiltinWebFetch = providerSupportsBuiltinWebFetch(
selectedExternalProvider?.providerType,
);
- // Gemini rejects codeExecution alongside image modalities. Search is
- // blocked on older Gemini image ids but allowed on Gemini 3 image models
- // (supportsBuiltinWebSearch encodes the per-model allowance), so we only
- // disable Code unconditionally in Gemini image mode.
+ // Gemini rejects codeExecution alongside image modalities. Search is blocked on older Gemini
+ // image ids but allowed on Gemini 3 image models, so only Code is disabled unconditionally
+ // in Gemini image mode.
const isExternalGemini = selectedExternalProvider?.providerType === "gemini";
const imageDisabled = !modelLoaded || !supportsBuiltinImageGeneration;
const imageModeDisablesCode =
isExternalGemini && imageToolsEnabled && !imageDisabled;
- // Image-tier Gemini models always reject codeExecution and reject
- // web_search on older ids (Gemini 3.x Pro/Flash allow it, encoded in
- // supportsBuiltinWebSearch). Don't let local `supportsTools` re-enable a
- // pill the Gemini backend silently drops: detect image-tier Gemini and
- // gate strictly on provider builtin support.
+ // Image-tier Gemini models always reject codeExecution and reject web_search on older ids, so do
+ // not let local `supportsTools` re-enable a pill the Gemini backend silently drops: gate
+ // strictly on provider builtin support.
const isGeminiImageTier =
isExternalGemini && supportsBuiltinImageGeneration;
- // Disable only when a loaded model lacks the capability; with no model the
- // tool can still be pre-selected, matching the + menu.
+ // Disable only when a loaded model lacks the capability; with no model the tool can still be
+ // pre-selected, matching the + menu.
const searchDisabled =
modelLoaded &&
(isGeminiImageTier
@@ -829,8 +818,8 @@ export function SharedComposer({
? true
: !(supportsTools || supportsBuiltinCodeExecution))) ||
imageModeDisablesCode;
- // Images pill lights only on OpenAI cloud Responses-API models and the
- // Gemini Nano Banana family. No local tool runtime fallback.
+ // Images pill lights only on OpenAI cloud Responses-API models and the Gemini Nano Banana
+ // family. No local tool runtime fallback.
const showImagePill = supportsBuiltinImageGeneration;
// Fetch pill: Anthropic-only (web_fetch_20250910 / web_fetch_20260209).
const webFetchDisabled = !modelLoaded || !supportsBuiltinWebFetch;
@@ -843,9 +832,8 @@ export function SharedComposer({
const ragDisabled =
modelLoaded && ((!externalUsesStudioTools && isExternalModel) || !supportsTools);
const showRagPill = !isExternalModel || externalUsesStudioTools;
- // Above 4 pills, collapse to icons only. Compare, Search, Code, and
- // permissions always show; the rest are conditional. Narrow viewports
- // collapse too: the labelled row is wider than a phone-width composer.
+ // Above 4 pills, collapse to icons only. Compare, Search, Code and permissions always show.
+ // Narrow viewports collapse too: the labelled row is wider than a phone-width composer.
const isMobile = useIsMobile();
const pillCount =
4 +
@@ -854,14 +842,12 @@ export function SharedComposer({
(showWebFetchPill ? 1 : 0) +
(artifactsEnabled ? 1 : 0) +
(mcpEnabledForChat ? 1 : 0);
- // Under the count threshold the row still overflows on long labels, wrapping
- // onto a second line inside the action bar. Measuring collapses just enough
- // to keep it beside the dictate/send controls.
+ // Under the count threshold the row still overflows on long labels, wrapping onto a second line
+ // inside the action bar, so measuring collapses just enough to keep it beside send.
const { pillRowRef, pillCompact } = useComposerPillFit(
isMobile || pillCount > 4,
);
- // Backwards-compatible alias for call sites still referencing
- // `toolsDisabled` (rare; both pills used it before).
+ // Backwards-compatible alias for call sites still referencing `toolsDisabled`.
const toolsDisabled = codeDisabled;
const setPendingAudioStore = useChatRuntimeStore((s) => s.setPendingAudio);
const clearPendingAudioStore = useChatRuntimeStore(
@@ -912,8 +898,8 @@ export function SharedComposer({
setTimeout(() => { sendRef.current?.(); }, 100);
}
- // Compare mode: advance the queue on cycle end, but stop on a failed step so we
- // don't burn prompts on incomplete results.
+ // Compare mode: advance the queue on cycle end, but stop on a failed step so prompts are not
+ // burned on incomplete results.
useEffect(() => {
const wasComparing = prevComparingRef.current;
prevComparingRef.current = comparing;
@@ -979,13 +965,12 @@ export function SharedComposer({
});
continue;
}
- // video_base64 targets the single loaded GGUF, so at most one side of
- // a compare could answer. Say that rather than drop the file.
+ // video_base64 targets the single loaded GGUF, so at most one side of a compare could answer. Say
+ // that rather than drop the file.
if (isVideoFile(file)) {
videoUnsupported = true;
continue;
}
- // Handle image files
if (!file.type.match(/^image\/(jpeg|png|webp|gif)$/i)) continue;
if (file.size > MAX_IMAGE_SIZE) continue;
if (attachUnavailableReason) {
@@ -1105,11 +1090,9 @@ export function SharedComposer({
? useChatRuntimeStore.getState().params.checkpoint
: undefined;
- // Generalized compare requires both panes to have a model. A half-
- // selected send either races to an empty bubble with bogus tok/s (#5569)
- // or leaves the empty pane with a dangling prompt. hasCompareHandles is
- // true only in GeneralCompareContent, so LoraCompare and single-pane
- // chats are unaffected.
+ // Generalized compare requires both panes to have a model: a half-selected send either races to
+ // an empty bubble with bogus tok/s (#5569) or leaves the empty pane with a dangling prompt.
+ // hasCompareHandles is true only in GeneralCompareContent.
if (hasCompareHandles && !isGeneralizedCompare) {
toast.error("Pick a model in each pane to compare", {
description:
@@ -1124,10 +1107,9 @@ export function SharedComposer({
!isGeneralizedCompare &&
imageUnavailableReason
) {
- // Single mode: the loaded model's runtime capability is known here.
- // Compare mode defers: each ensureModelLoaded sets loadedIsMultimodal
- // for its side, and the chat-adapter's pre-stream gate runs per-side
- // against that fresh state.
+ // Single mode: the loaded model's runtime capability is known here. Compare mode defers: each
+ // ensureModelLoaded sets loadedIsMultimodal for its side, and the adapter's pre-stream gate
+ // runs per side.
toast.error(imageUnavailableReason);
resetPromptQueue();
return;
@@ -1245,8 +1227,8 @@ export function SharedComposer({
});
let loadedFromConfig = false;
- // Warm the device cache before the snapshot below reconciles the GPU
- // pick: on a cold cache the reconcile passes a stale pick through.
+ // Warm the device cache before the snapshot below reconciles the GPU pick: on a cold cache the
+ // reconcile passes a stale pick through.
try {
if (store.selectedGpuIds != null) {
await ensureGpuDeviceCache();
@@ -1259,12 +1241,11 @@ export function SharedComposer({
});
return;
}
- // The GPU/offload knobs both compare loads must use, snapshotted at Send.
- // ensureModelLoaded runs sequentially and the first load's response echo
- // (loadedGpuMemoryFields) rewrites the live store -- a non-GGUF or Auto
- // first model resets gpuLayers/nCpuMoe/split/pick to defaults -- so
- // reading the store per load would hand model 2 the first model's echoed
- // defaults instead of the settings the user pressed Send with.
+ // The GPU/offload knobs both compare loads must use, snapshotted at Send: ensureModelLoaded runs
+ // sequentially and the first load's response echo rewrites the live store, so reading the
+ // store per load would hand model 2 the first model's echoed defaults.
+ // The first load's echo (loadedGpuMemoryFields) rewrites the live store, resetting
+ // gpuLayers/nCpuMoe/split/pick to defaults on a non-GGUF or Auto first model.
const compareLoadKnobs = {
gpuMemoryMode: store.gpuMemoryMode,
gpuLayers: store.gpuLayers,
@@ -1278,8 +1259,8 @@ export function SharedComposer({
return;
}
clearSubmittedDraft();
- // Set when an accepted transformers install unloaded the active model
- // server-side; a later failure must then clear the stale checkpoint.
+ // Set when an accepted transformers install unloaded the active model server-side; a later
+ // failure must then clear the stale checkpoint.
let upgradeUnloadedActive = false;
const compareSelectionNeedsLoad = (sel: CompareModelSelection) => {
const currentStore = useChatRuntimeStore.getState();
@@ -1295,16 +1276,13 @@ export function SharedComposer({
);
requestLocalPromptQueueStop(compareStopDecision?.promptQueueThreadIds);
};
- // Helper: load a model and update store checkpoint
async function ensureModelLoaded(
sel: CompareModelSelection,
): Promise<string> {
const currentStore = useChatRuntimeStore.getState();
const config = sel.config ?? null;
- // This pane's effective config: an explicit selection config, else the
- // remembered store config for this model/quant (never the other pane's).
- // No saved config resolves to all-null defaults, so settings below fall
- // through to their session default.
+ // This pane's effective config: an explicit selection config, else the remembered store config
+ // for this model/quant, never the other pane's. No saved config means all-null defaults.
const resolved = config
? { config, remembered: true }
: resolveInitialConfig(sel.id, sel.ggufVariant ?? null);
@@ -1323,8 +1301,8 @@ export function SharedComposer({
sel.id.toLowerCase().endsWith(".gguf");
const platform = usePlatformStore.getState();
let resolvedIsDiffusion = sel.isDiffusion;
- // Set when the preflight could not classify the GGUF, so a false
- // resolvedIsDiffusion below must not be read as "ordinary".
+ // Set when the preflight could not classify the GGUF, so a false resolvedIsDiffusion below must
+ // not be read as "ordinary".
let diffusionUnknown = false;
if (targetIsGguf && resolvedIsDiffusion === undefined) {
const preparedToken = await prepareHfTokenForUse(
@@ -1341,25 +1319,20 @@ export function SharedComposer({
resolvedIsDiffusion = staged.isDiffusion;
diffusionUnknown = staged.diffusionUnknown;
}
- // Pass-through arguments can live only in the server's override map (set
- // through the API, or from another browser), and this config comes from
- // local storage. /load's omission path inherits them from a RESIDENT
- // instance of the same model, which a compare pane starting cold or
- // switching away from the other model does not have, so without this the
- // experiment runs a different command from the one that was saved.
+ // Pass-through arguments can live only in the server's override map while this config comes from
+ // local storage, and /load's omission path inherits them from a RESIDENT instance, which a
+ // cold compare pane does not have, so the experiment would run a different command.
if (
targetIsGguf &&
// Not for the diffusion runner, which appends none of them.
resolvedIsDiffusion !== true
) {
try {
- // Sanitised for the same reason the panel sanitises what it hydrates:
- // either list becomes an EXPLICIT /load argument, which is validated
- // strictly rather than going through the carry-over paths that drop a
- // newly denied flag quietly. A pane on an install upgraded across a
- // denylist change would otherwise answer 400 on a comparison that ran
- // the day before, whether the list came from the server or from this
- // browser's own storage.
+ // Sanitised for the same reason the panel sanitises what it hydrates: either list becomes an
+ // EXPLICIT /load argument, validated strictly rather than going through the carry-over paths
+ // that drop a newly denied flag quietly.
+ // A pane on an install upgraded across a denylist change would otherwise answer 400 on a
+ // comparison that ran the day before.
const managed = await loadManagedLlamaFlags();
const clean = (tokens: readonly string[]) =>
sanitizeStoredExtraArgs(
@@ -1381,10 +1354,8 @@ export function SharedComposer({
if (cleaned.length > 0) {
ownConfig.llamaExtraArgs = cleaned;
} else if (resolvedArgs.explicit) {
- // An explicit empty row is a cleared box, and this pane has to send
- // it as one: left undefined the field is omitted and /load carries
- // the resident model's arguments into the comparison, so the panes
- // would not be running the command they are compared on.
+ // An explicit empty row is a cleared box, and this pane has to send it as one: left undefined the
+ // field is omitted and /load carries the resident model's arguments into the comparison.
ownConfig.llamaExtraArgs = [];
}
} else if (local !== null && local.length > 0) {
@@ -1426,13 +1397,9 @@ export function SharedComposer({
: ownRemembered
? ownConfig.tensorParallel
: fallbackTensorParallel;
- // The diffusion runner has no projector to skip, so the toggle is inert
- // there for the same reason tensorParallel is.
- //
- // A pane with no saved config gets the per-model DEFAULT, not the store's
- // current value, which belongs to whichever model is loaded. Where this parts
- // company with tensorParallel: that one deliberately stands across models,
- // while an unconfigured model is a model with vision on.
+ // The diffusion runner has no projector to skip, so the toggle is inert there. A pane with no
+ // saved config gets the per-model DEFAULT, not the store's current value: unlike
+ // tensorParallel, which stands across models, an unconfigured model is one with vision on.
const effectiveDisableVision = resolvedIsDiffusion
? false
: ownRemembered
@@ -1441,11 +1408,9 @@ export function SharedComposer({
if (ownConfig.selectedGpuIds != null) {
await ensureGpuDeviceCache();
}
- // A pane's OWN saved split is sent instead of being forced to Auto
- // (#7574); the shared Send-time snapshot is not, since its layer count
- // is bounded by another GGUF. Knobs the runner has no equivalent for
- // (MoE offload, tensor parallel) stay hard-forced. An UNCLASSIFIED GGUF
- // is pinned too: see lib/gpu-placement.ts.
+ // A pane's OWN saved split is sent instead of being forced to Auto (#7574); the shared Send-time
+ // snapshot is not, since its layer count is bounded by another GGUF. Knobs the runner has no
+ // equivalent for stay hard-forced, and an UNCLASSIFIED GGUF is pinned too.
const {
gpuMemoryMode: effectiveGpuMemoryMode,
gpuLayers: effectiveGpuLayers,
@@ -1474,23 +1439,22 @@ export function SharedComposer({
compareLoadKnobs.selectedGpuIndexKind,
resolvedIsDiffusion === true,
);
- // A pane's context comes from its own config only: a saved pin, or null
- // (Auto/native). It must not inherit the active model's shared snapshot --
- // resolveFitMaxSeqLength would treat that as a pin and load this pane at
- // the other model's context (changing VRAM/results or OOMing).
+ // A pane's context comes from its own config only: a saved pin, or null. It must not inherit the
+ // active model's shared snapshot, which resolveFitMaxSeqLength would treat as a pin and load
+ // this pane at the other model's context.
+ // A GGUF pane with no explicit context loads at native (0 -> n_ctx_train), not the session
+ // maxSeqLength, which would silently shrink the shown context.
const effectiveCustomContextLength = ownConfig.customContextLength;
let loadTrustRemoteCode = trustRemoteCode;
let approvedRemoteCodeFingerprint: string | null = null;
- // Size validation exactly as the load below, so the training-guard
- // preflight checks the footprint that actually loads (under Manual + Auto
- // layers the load sends 0 / the pinned context, not raw maxSeqLength).
+ // Size validation exactly as the load below, so the training-guard preflight checks the footprint
+ // that actually loads.
const compareMaxSeqLength = resolveFitMaxSeqLength(
targetIsGguf,
effectiveGpuMemoryMode,
effectiveGpuLayers,
- // Prefer this pane's own saved context pin over the shared snapshot,
- // falling back to its per-pane effective context (GGUF with no saved
- // context loads at native, not the session maxSeqLength).
+ // Prefer this pane's own saved context pin over the shared snapshot, falling back to its per-pane
+ // effective context.
effectiveCustomContextLength,
effectiveMaxSeqLength,
);
@@ -1506,19 +1470,19 @@ export function SharedComposer({
cache_type_kv: ownConfig.kvCacheDtype ?? null,
tensor_parallel: effectiveTensorParallel,
disable_vision: effectiveDisableVision,
- // Scope the validate to the picked GPUs. GGUF-only, like the load
- // below: a non-GGUF target must not inherit a hidden GGUF GPU pick.
+ // Scope the validate to the picked GPUs. GGUF-only, like the load below: a non-GGUF target must
+ // not inherit a hidden GGUF GPU pick.
...(targetIsGguf
? {
gpu_ids: effectiveSelectedGpuIds ?? undefined,
gpu_memory_mode: effectiveGpuMemoryMode,
- // Sized like the load below: a manual DiffusionGemma split
- // must not be validated as a full-GGUF occupant.
+ // Sized like the load below: a manual DiffusionGemma split must not be validated as a full-GGUF
+ // occupant.
gpu_layers: effectiveGpuLayers,
// Slots scale the KV estimate; keep validate sized like the load.
n_parallel: ownConfig.nParallel ?? null,
- // Only when this panel has read the stored value: omitted, the load
- // inherits it, which is what keeps CLI-set flags working.
+ // Only when this panel has read the stored value: omitted, the load inherits it, which is what
+ // keeps CLI-set flags working.
...(ownConfig.llamaExtraArgs !== undefined
? // biome-ignore lint/style/useNamingConvention: API schema
{ llama_extra_args: ownConfig.llamaExtraArgs ?? [] }
@@ -1544,9 +1508,8 @@ export function SharedComposer({
forceCancelActive:
compareStopDecision?.forceCancelActive ?? false,
});
- // The install unloads the active model before the swap (even when the
- // swap then fails); if a later gate cancels or the load fails, the UI
- // must stop pointing at that unloaded model.
+ // The install unloads the active model before the swap even when the swap fails, so if a later
+ // gate cancels the UI must stop pointing at that unloaded model.
if (
useTransformersUpgradeDialogStore
.getState()
@@ -1607,8 +1570,8 @@ export function SharedComposer({
tensor_split: compareLoadKnobs.splitRatio ?? undefined,
gpu_ids: effectiveSelectedGpuIds ?? undefined,
n_parallel: ownConfig.nParallel ?? null,
- // Only when this panel has read the stored value: omitted, the load
- // inherits it, which is what keeps CLI-set flags working.
+ // Only when this panel has read the stored value: omitted, the load inherits it, which keeps
+ // CLI-set flags working.
...(ownConfig.llamaExtraArgs !== undefined
? // biome-ignore lint/style/useNamingConvention: API schema
{ llama_extra_args: ownConfig.llamaExtraArgs ?? [] }
@@ -1623,22 +1586,21 @@ export function SharedComposer({
}
: {}),
});
- // Keep a compare pane's per-model speculative choice load-local: persist
- // the global preference only when it came from global settings.
+ // Keep a compare pane's per-model speculative choice load-local: persist the global preference
+ // only when it came from global settings.
if (ownConfig.speculativeType == null) {
saveSpeculativeType(effectiveSpeculativeType);
}
- // Persist the GPU Memory mode on a non-diffusion GGUF compare-load too,
- // so an applied manual choice survives a restart.
+ // Persist the GPU Memory mode on a non-diffusion GGUF compare-load too, so an applied manual
+ // choice survives a restart.
persistGpuMemoryModeOnLoad(resp, effectiveGpuMemoryMode);
upgradeUnloadedActive = false;
const store = useChatRuntimeStore.getState();
store.setCheckpoint(
resp.model,
resp.is_gguf ? (sel.ggufVariant ?? undefined) : null,
- // Same cap as the interactive load: this replays the model's
- // remembered settings, and a budget kept from a larger context does
- // not fit the one it just loaded with.
+ // Same cap as the interactive load: this replays the model's remembered settings, and a budget
+ // kept from a larger context does not fit the one it just loaded with.
{
// The reported window leads and the request stands in only for a backend
// that sizes nothing, as on the interactive load: an unpinned pane sends
@@ -1703,9 +1665,8 @@ export function SharedComposer({
...(targetIsGguf && !(resp.is_diffusion ?? false)
? committedServerTuningState(ownConfig)
: clearedServerTuningState()),
- // What this pane's launch is running, for a later rollback: the status
- // applier is held off for the whole load, so nothing else records it, and
- // a switch straight after would snapshot the other model's list.
+ // What this pane's launch is running, for a later rollback: the status applier is held off for
+ // the whole load, so a switch straight after would snapshot the other model's list.
loadedLlamaExtraArgs:
resp.requested_llama_extra_args !== undefined
? (resp.requested_llama_extra_args ?? [])
@@ -1713,9 +1674,8 @@ export function SharedComposer({
tensorParallel: resp.tensor_parallel ?? false,
loadedTensorParallel: resp.tensor_parallel ?? false,
loadedDisableVision: resp.disable_vision ?? false,
- // Adopted from the echo like the knob above: this pane loaded its own
- // model, so the editable value must follow it or Advanced Settings
- // shows the other pane's Vision state.
+ // Adopted from the echo like the knob above: this pane loaded its own model, so the editable
+ // value must follow it or Advanced Settings shows the other pane's Vision state.
disableVision: resp.disable_vision ?? false,
defaultChatTemplate: resp.chat_template ?? null,
chatTemplateOverride: effectiveChatTemplateOverride,
@@ -1727,18 +1687,16 @@ export function SharedComposer({
// plus loaded baselines) so the GPU controls round-trip. (The context group,
// customContextLength and native-path token/expiry clear in the tail below.)
...loadedGpuMemoryFields(resp),
- // Drives the GPU Memory controls' diffusion gate; set alongside the
- // GPU fields on every load path so the gate can't read stale.
+ // Drives the GPU Memory controls' diffusion gate; set alongside the GPU fields on every load path
+ // so the gate cannot read stale.
loadedIsDiffusion: resp.is_diffusion ?? false,
loadedIsMultimodal: isMultimodalResponse(resp),
- // Set alongside loadedIsMultimodal so the composer can say WHY images
- // are unavailable in compare mode too.
+ // Set alongside loadedIsMultimodal so the composer can say WHY images are unavailable in compare mode too.
loadedVisionDisabledByUser: resp.vision_disabled_by_user ?? false,
mmprojFallbackReason: resp.mmproj_fallback_reason ?? null,
activeModelIsLocal: resp.is_local_model ?? false,
- // Same value as the baseline above, so that when this pane becomes the
- // active model the UI and later reload/save use the context it actually
- // loaded with, not the previous/default one.
+ // Same value as the baseline above, so when this pane becomes the active model the UI and a later
+ // reload use the context it actually loaded with.
customContextLength: keepCustomCtx,
...loadedContextFields(resp),
// Compare selections load by repo/variant, never from the file picker,
@@ -1761,9 +1719,8 @@ export function SharedComposer({
});
}
loadedFromConfig = config != null;
- // Sync the models[] entry with the load response so attach/send gates
- // read fresh capabilities. /api/models/list can lag a model's actual
- // state (e.g. a GGUF whose mmproj arrived after the snapshot).
+ // Sync the models[] entry with the load response so attach/send gates read fresh capabilities:
+ // /api/models/list can lag a model's actual state.
const currentModels = useChatRuntimeStore.getState().models;
const idx = currentModels.findIndex((m) => m.id === sel.id);
const synced = {
@@ -1806,7 +1763,7 @@ export function SharedComposer({
setComparing(true);
try {
- // Side 1: load → generate → wait
+ // Side 1: load, generate, wait
if (handle1 && model1?.id) {
toast("Loading Model 1…", {
id: toastId,
@@ -1825,7 +1782,7 @@ export function SharedComposer({
await done;
}
- // Side 2: load → generate → wait
+ // Side 2: load, generate, wait
if (handle2 && model2?.id) {
acquireCompareModelLifecycle();
const needsLoad = compareSelectionNeedsLoad(model2);
@@ -1862,8 +1819,8 @@ export function SharedComposer({
} catch (err) {
compareStepSucceededRef.current = false;
resetPromptQueue();
- // The install already unloaded the previously active model; drop the
- // checkpoint so the UI does not keep pointing at an unloaded model.
+ // The install already unloaded the previously active model; drop the checkpoint so the UI does
+ // not keep pointing at it.
if (upgradeUnloadedActive) {
useChatRuntimeStore.getState().clearCheckpoint();
}
@@ -1932,20 +1889,17 @@ export function SharedComposer({
const busy = running || comparing;
function onKeyDown(e: KeyboardEvent) {
- // IME composition (JP/CN/KR): Enter commits the candidate, don't hijack it
- // (#5318). Re-pin composingRef in case the stuck watchdog (#5546) cleared
- // it during a long candidate-window pause, so a follow-up click-Send won't
- // submit preedit text. Re-arm the watchdog on the same path; without it the
- // WSL+Chrome no-compositionend case pins composingRef forever after an IME
- // keypress and re-locks Send.
+ // IME composition (JP/CN/KR): Enter commits the candidate, so do not hijack it (#5318). Re-pin
+ // composingRef in case the stuck watchdog (#5546) cleared it during a long candidate-window
+ // pause, and re-arm the watchdog on the same path, or the WSL+Chrome no-compositionend case
+ // pins composingRef forever.
if (e.nativeEvent.isComposing || e.keyCode === 229) {
composingRef.current = true;
refreshStuckImeTimer();
return;
}
- // Non-IME key while composingRef is stuck; mirrors the fix in thread.tsx.
- // On macOS, switching input methods without composing can leave composingRef
- // pinned; clear it immediately on the first non-IME keystroke.
+ // Non-IME key while composingRef is stuck; mirrors the fix in thread.tsx. On macOS, switching
+ // input methods without composing can leave composingRef pinned.
if (composingRef.current) {
// Candidate-confirming Enter can arrive as non-composing; keep it gated.
if (e.key === "Enter") {
@@ -1974,18 +1928,16 @@ export function SharedComposer({
!isDictating &&
!sendUnavailableReason;
- // Compare mode swaps this composer in for the single-chat one, and only one
- // of the two is ever on screen, so the chords register in both. Both gate on
- // the chat tab being visible: off-route the pane is hidden, not unmounted.
+ // Compare mode swaps this composer in for the single-chat one and only one is ever on screen, so
+ // the chords register in both. Both gate on the chat tab being visible: off-route the pane is
+ // hidden, not unmounted.
const chatActive = useChatActive();
useShortcut(
"startDictation",
() => {
- // As in the single-chat composer: a dialog over Chat leaves this
- // registered, and a microphone opened behind one is neither visible nor
- // stoppable from where the user is.
- // Stopping first and ungated, as in the single-chat composer: a recording
- // stays stoppable wherever the gate would say no.
+ // As in the single-chat composer: a dialog over Chat leaves this registered, and a microphone
+ // opened behind one is neither visible nor stoppable. Stopping first and ungated, so a
+ // recording stays stoppable wherever the gate would say no.
if (isDictating) {
stopDictation();
return;
@@ -1995,20 +1947,18 @@ export function SharedComposer({
},
{ enabled: chatActive },
);
- // Through the existing sendRef: `send` is render-scoped, which the React
- // compiler will not let a hook outlive.
+ // Through the existing sendRef: `send` is render-scoped, which the React compiler will not let a hook outlive.
useShortcut(
"sendMessage",
() => {
- // As in the single-chat composer: the draft behind a dialog is not what
- // the user is typing.
+ // As in the single-chat composer: the draft behind a dialog is not what the user is typing.
if (!isSurfaceInForeground(COMPOSER_INPUT_SELECTOR)) return;
sendRef.current?.();
},
{
enabled: chatActive && canSend,
- // As in the single-chat composer: a non-modal popover's search box is a
- // text field the composer is still the foreground behind.
+ // As in the single-chat composer: a non-modal popover's search box is a text field the composer
+ // is still the foreground behind.
skipInTextFields: true,
textFieldException: COMPOSER_INPUT_SELECTOR,
},
@@ -2016,17 +1966,15 @@ export function SharedComposer({
useShortcut(
"attachFiles",
() => {
- // As in the single-chat composer, which would otherwise raise the file
- // chooser from behind a dialog.
+ // As in the single-chat composer, which would otherwise raise the file chooser from behind a dialog.
if (!isSurfaceInForeground(COMPOSER_INPUT_SELECTOR)) return;
fileInputRef.current?.click();
},
{ enabled: chatActive },
);
- // Adjustable "+" menu items, keyed by id. Pinned ones render at the top
- // level; the rest fall into the "More" overflow submenu. Core items (photos,
- // web search, code) and "More" itself live outside this map.
+ // Adjustable "+" menu items, keyed by id. Pinned ones render at the top level; the rest fall into
+ // the "More" overflow submenu. Core items and "More" itself live outside this map.
const plusMenuNodes: Record<PlusMenuItemId, ReactNode> = {
chatWithFiles: (
<DropdownMenuItem
@@ -2199,8 +2147,8 @@ export function SharedComposer({
}}
onDragLeave={() => setDragging(false)}
onDrop={(e) => {
- // Phase 1 native model drops own Tauri local-path drops. Restore
- // browser attachment drops in Tauri once Phase 1d adds token bridging.
+ // Phase 1 native model drops own Tauri local-path drops. Restore browser attachment drops in
+ // Tauri once Phase 1d adds token bridging.
if (isTauri || isPortaledDrop(e)) return;
e.preventDefault();
setDragging(false);
@@ -2287,12 +2235,9 @@ export function SharedComposer({
ref={textareaRef}
value={text}
onChange={(e) => {
- // ALWAYS mirror the DOM value into React state, even during IME
- // composition: the controlled `value` must match the DOM at all
- // times, else an unrelated parent re-render reconciles the textarea
- // back to the stored value mid-composition, wiping the IME preedit
- // AND prior committed text (e.g. Tab-cycling candidates erases
- // earlier words). #5318.
+ // ALWAYS mirror the DOM value into React state, even during IME composition: the controlled
+ // `value` must match the DOM at all times, else an unrelated parent re-render reconciles the
+ // textarea back to the stored value mid-composition, wiping the preedit (#5318).
setCompositionState(isNativeComposing(e.nativeEvent));
setText(e.target.value);
}}
@@ -2309,20 +2254,17 @@ export function SharedComposer({
onKeyDown={onKeyDown}
onPaste={handleFilePaste}
onBlur={() => {
- // Mac: switching input methods can fire compositionstart without a
- // matching compositionend, leaving composingRef pinned. The OS always
- // commits or cancels composition before the element loses focus.
+ // Mac: switching input methods can fire compositionstart without a matching compositionend,
+ // leaving composingRef pinned. The OS always commits or cancels before focus is lost.
setCompositionState(false);
}}
placeholder="Send to both models..."
- // dir="auto" detects RTL (Arabic/Hebrew/Persian/Urdu) from the first
- // strong character; no effect on LTR scripts. Kept next to the
- // placeholder: the IME smoke reads this pair out of the source.
+ // dir="auto" detects RTL from the first strong character; no effect on LTR scripts. Kept next to
+ // the placeholder: the IME smoke reads this pair out of the source.
dir="auto"
- // aui-composer-input carries no styling anywhere; it is the name both
- // composers answer to, so one selector can mean "the composer" whichever
- // of the two is on screen. Escape's decline exception and the dictation
- // foreground check both rely on it.
+ // aui-composer-input carries no styling anywhere; it is the name both composers answer to, so one
+ // selector can mean "the composer" whichever is on screen. Escape's decline exception and the
+ // dictation foreground check rely on it.
className="composer-input aui-composer-input"
rows={1}
/>
@@ -2357,8 +2299,8 @@ export function SharedComposer({
open={newProjectOpen}
onOpenChange={setNewProjectOpen}
/>
- {/* Same + menu as single-chat (ComposerToolsMenu), wired to the
- compare composer's own file/audio inputs and tools. */}
+ {/* Same + menu as single-chat (ComposerToolsMenu), wired to the compare composer's own file/audio
+ inputs and tools. */}
<DropdownMenu
onOpenChange={(open) => {
if (open) void refreshRecentPrompts();
@@ -2429,8 +2371,7 @@ export function SharedComposer({
}
onSelect={() => setCodeToolsEnabled(!codeToolsEnabled)}
>
- {/* Scale, not width: an oversized box pushed the label out of
- line. */}
+ {/* Scale, not width: an oversized box pushed the label out of line. */}
<HugeiconsIcon
icon={CodeIcon}
strokeWidth={2}
@@ -2499,9 +2440,8 @@ export function SharedComposer({
</PillGlyph>
<span>Compare</span>
</button>
- {/* Permission-level pill sits immediately after Compare and ahead
- of every other tool pill (Search, Code, ...) so the Full access
- danger state reads first; only Compare outranks it. */}
+ {/* Permission-level pill sits immediately after Compare and ahead of every other tool pill so the
+ Full access danger state reads first. */}
<PermissionModeComposerPill side="top" />
<button
type="button"
@@ -2509,9 +2449,10 @@ export function SharedComposer({
onClick={() => {
const next = !toolsEnabled;
setToolsEnabled(next);
- // Kimi's $web_search builtin requires thinking=disabled
- // (https://platform.kimi.ai/docs/guide/use-web-search): toggle
- // the Think pill off when Search is on, mirroring the backend.
+ // Kimi's $web_search builtin requires thinking=disabled, so toggle the Think pill off when Search
+ // is on, mirroring the backend.
+ // Per https://platform.kimi.ai/docs/guide/use-web-search.
+ // Per https://platform.kimi.ai/docs/guide/use-web-search.
if (isKimiExternal) {
setReasoningEnabled(!next, { persist: false });
applyQwenThinkingParams(!next);
@@ -2619,8 +2560,7 @@ export function SharedComposer({
) : null}
{mcpEnabledForChat ? <McpComposerButton side="top" /> : null}
</div>
- {/* mr-0.5 matches the send button inset from the edge in normal chat;
- gap-1.5 matches its control spacing. */}
+ {/* mr-0.5 matches the send button inset from the edge in normal chat; gap-1.5 matches its control spacing. */}
<div className="ml-auto mr-0.5 flex items-center gap-1.5">
{showReasoningControl ? (
isEffort || supportsPreserveThinking ? (
@@ -2695,8 +2635,8 @@ export function SharedComposer({
setReasoningEffort(level);
setReasoningEnabled(true);
applyQwenThinkingParams(true);
- // Mutual exclusion: turning thinking on for a
- // Kimi model forces the web_search builtin off.
+ // Mutual exclusion: turning thinking on for a Kimi model forces its
+ // web_search builtin off.
if (isKimiExternal && toolsEnabled) {
setToolsEnabled(false, { persist: false });
}
@@ -2789,8 +2729,8 @@ export function SharedComposer({
const next = !reasoningEnabled;
setReasoningEnabled(next);
applyQwenThinkingParams(next);
- // Mutual exclusion: Kimi's $web_search builtin requires
- // thinking off, so turning thinking on flips Search off.
+ // Mutual exclusion: Kimi's $web_search builtin requires thinking off, so turning thinking on
+ // flips Search off.
if (isKimiExternal && next && toolsEnabled) {
setToolsEnabled(false, { persist: false });
}
diff --git a/studio/frontend/src/features/chat/stores/chat-navigation-store.ts b/studio/frontend/src/features/chat/stores/chat-navigation-store.ts
index c51a466593e..d702c5b61a9 100644
--- a/studio/frontend/src/features/chat/stores/chat-navigation-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-navigation-store.ts
@@ -4,12 +4,9 @@
import { create } from "zustand";
import type { SidebarItem } from "../hooks/use-chat-sidebar-items";
-/**
- * What the navigation chords need: the ordered rows the sidebar renders, the
- * open chat, the unread set and the rows wanting attention. The sidebar owns
- * the sorting and publishes its finished lists here, so nothing recomputes a
- * second copy that could disagree with the screen.
- */
+/** What the navigation chords need: the ordered rows the sidebar renders, the open chat, the
+ * unread set and the rows wanting attention. The sidebar owns the sorting and publishes its
+ * finished lists here, so nothing recomputes a copy that could disagree with the screen. */
/** How many rows back ⌃Tab can walk, bounded so the array cannot grow all day. */
const RECENTLY_VIEWED_LIMIT = 24;
@@ -17,8 +14,8 @@ const RECENTLY_VIEWED_LIMIT = 24;
export interface ChatNavigationState {
/** The pinned block, in the order the sidebar draws it. */
pinnedItems: SidebarItem[];
- /** Project chats on screen. Empty when the sidebar is organized as one list,
- * where those chats are in Recents instead. */
+ /** Project chats on screen. Empty when the sidebar is organized as one list, where those chats
+ * are in Recents instead. */
projectItems: SidebarItem[];
/** Recents, likewise. ⌥⌘1-6 indexes into this list alone. */
recentItems: SidebarItem[];
@@ -30,14 +27,12 @@ export interface ChatNavigationState {
unreadRowIds: Record<string, string>;
/** Row ids, most recently opened first. */
recentlyViewedIds: string[];
- /** A walk through the stack in progress: the order frozen at the first step,
- * and where in it the walk has got to. */
+ /** A walk through the stack in progress: the order frozen at the first step, and where in it the walk has got to. */
traversal: { order: string[]; index: number } | null;
/** Registered by the sidebar, the only code that can route to a row. */
openChatItem: ((item: SidebarItem) => void) | null;
- /** Rows or folders are selected, so Escape is already spoken for. Published
- * because bare Escape also declines a waiting tool call, and one press must
- * not do both. */
+ /** Rows or folders are selected, so Escape is already spoken for. Published because bare Escape
+ * also declines a waiting tool call, and one press must not do both. */
selectionActive: boolean;
publishLists: (next: {
@@ -51,11 +46,8 @@ export interface ChatNavigationState {
/** Drop one account's rows, unread set and walk. Called as the sidebar goes. */
resetAccountState: () => void;
setSelectionActive: (active: boolean) => void;
- /**
- * `rowIdByThreadId` groups threads by their row, so a Compare row still
- * counts once after a collapsed section takes it out of the published lists
- * while its threads stay unread.
- */
+ /** `rowIdByThreadId` groups threads by their row, so a Compare row still counts once after a
+ * collapsed section takes it out of the published lists while its threads stay unread. */
markThreadsUnread: (
threadIds: string[],
rowIdByThreadId?: Record<string, string>,
@@ -67,13 +59,9 @@ export interface ChatNavigationState {
endTraversal: () => void;
}
-/**
- * Everything the chords read off a row: which chat, where it routes, and the
- * threads behind it. Compared by value, not identity, since the sidebar
- * rebuilds its items; ids alone would keep a row whose project changed and
- * route the next jump to the project it left. Titles and timestamps churn and
- * nobody here reads them.
- */
+/** Everything the chords read off a row: which chat, where it routes, and the threads behind
+ * it. Compared by value, not identity, since the sidebar rebuilds its items; ids alone would
+ * keep a row whose project changed and route the next jump to the project it left. */
function sameRows(a: SidebarItem[], b: SidebarItem[]): boolean {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
@@ -100,11 +88,8 @@ function sameStrings(a: string[], b: string[]): boolean {
return true;
}
-/**
- * One account's chats. The unread set used to live in component state and died
- * with it; a module store does not, so signing out has to say so or the next
- * account inherits these rows and this walk.
- */
+/** One account's chats. The unread set used to live in component state and died with it; a
+ * module store does not, so signing out has to say so or the next account inherits these rows. */
const ACCOUNT_STATE = {
pinnedItems: [] as SidebarItem[],
projectItems: [] as SidebarItem[],
@@ -186,9 +171,8 @@ export const useChatNavigationStore = create<ChatNavigationState>(
noteViewed: (itemId) => {
const { recentlyViewedIds, traversal } = get();
- // Mid-walk the stack holds still: promoting each chat it lands on would
- // swap the top two, so the next step comes straight back and everything
- // below stays unreachable.
+ // Mid-walk the stack holds still: promoting each chat it lands on would swap the top two, so
+ // the next step comes straight back and everything below stays unreachable.
if (traversal && traversal.order[traversal.index] === itemId) return;
if (recentlyViewedIds[0] === itemId) {
if (traversal) set({ traversal: null });
@@ -218,8 +202,8 @@ export const useChatNavigationStore = create<ChatNavigationState>(
: state.activeItemId
? order.indexOf(state.activeItemId)
: -1;
- // Nothing open, or a chat outside the stack: the walk starts outside it,
- // so the first step lands on the end it walks from.
+ // Nothing open, or a chat outside the stack: the walk starts outside it, so the first step
+ // lands on the end it walks from.
const index =
from === -1
? delta > 0
@@ -233,8 +217,8 @@ export const useChatNavigationStore = create<ChatNavigationState>(
endTraversal: () => {
const { traversal, recentlyViewedIds } = get();
if (!traversal) return;
- // The walk is over, so the chat it finished on takes the top, the way it
- // would have if it had been opened by hand.
+ // The walk is over, so the chat it finished on takes the top, the way it would have if it had
+ // been opened by hand.
const landed = traversal.order[traversal.index];
set({
traversal: null,
@@ -247,11 +231,9 @@ export const useChatNavigationStore = create<ChatNavigationState>(
}),
);
-/**
- * Every chat row the sidebar shows, in the order it draws them: pinned, then
- * the project folders, then Recents. A pinned project chat is drawn twice, so
- * the first of the pair wins and the walk does not stop on it again.
- */
+/** Every chat row the sidebar shows, in draw order: pinned, then project folders, then Recents.
+ * A pinned project chat is drawn twice, so the first of the pair wins and the walk does not
+ * stop on it again. */
export function visibleChatItems(state: ChatNavigationState): SidebarItem[] {
const seen = new Set<string>();
const out: SidebarItem[] = [];
@@ -267,11 +249,9 @@ export function visibleChatItems(state: ChatNavigationState): SidebarItem[] {
return out;
}
-/**
- * How many chats hold an unread thread. Not the set's size: a Compare row is
- * backed by two threads and marked unread by both, so the set counted it twice.
- * Unreads no row accounts for still count one each, since the wipe clears them.
- */
+/** How many chats hold an unread thread. Not the set's size: a Compare row is backed by two
+ * threads and marked unread by both, so the set counted it twice. Unreads no row accounts
+ * for still count one each, since the wipe clears them. */
export function countUnreadRows(state: ChatNavigationState): number {
const listed = new Set<string>();
let rows = 0;
@@ -283,10 +263,9 @@ export function countUnreadRows(state: ChatNavigationState): number {
rows += 1;
for (const id of own) listed.add(id);
}
- // Plus the unreads no row accounts for: a chat archived while unread leaves
- // its thread in the set, and the wipe clears those too. Grouped by row, so a
- // hidden Compare row counts once rather than once per pane; an id with no
- // group recorded stands for itself.
+ // Plus the unreads no row accounts for: a chat archived while unread leaves its thread in the
+ // set, and the wipe clears those too. Grouped by row, so a hidden Compare row counts once
+ // rather than once per pane; an id with no group recorded stands for itself.
const hidden = new Set<string>();
for (const id of state.unreadThreadIds) {
if (!listed.has(id)) hidden.add(state.unreadRowIds[id] ?? id);
@@ -302,8 +281,8 @@ export function recentChatItemAtSlot(
return state.recentItems[slot - 1] ?? null;
}
-/** The row `delta` places from the open one, wrapping. With nothing open, the
- * first row or the last, so the chord still does something. */
+/** The row `delta` places from the open one, wrapping. With nothing open, the first row or the
+ * last, so the chord still does something. */
export function adjacentChatItem(
state: ChatNavigationState,
delta: number,
@@ -316,8 +295,8 @@ export function adjacentChatItem(
return items[next];
}
-/** The next row wanting attention after the open one, wrapping. The list
- * arrives already ordered, so this only picks the next entry. */
+/** The next row wanting attention after the open one, wrapping. The list arrives already
+ * ordered, so this only picks the next entry. */
export function nextAttentionChatItem(
state: ChatNavigationState,
): SidebarItem | null {
diff --git a/studio/frontend/src/features/chat/stores/chat-preferences-store.ts b/studio/frontend/src/features/chat/stores/chat-preferences-store.ts
index e1b44fbf10d..12078ba23f5 100644
--- a/studio/frontend/src/features/chat/stores/chat-preferences-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-preferences-store.ts
@@ -8,15 +8,11 @@ import {
PASTED_TEXT_THRESHOLD_CHOICES,
} from "../utils/pasted-text.ts";
-// Client-side chat UI prefs kept in localStorage, not the chat DB.
-// confirmDeleteChats: when off, deleting a chat skips the confirm dialog.
-// alwaysDeleteChatFiles: when on, deleting a chat also removes its sandbox
-// folder, without having to ask for it each time.
-// showModelDisclaimer: when off, hide the "LLMs can make mistakes" footer note.
-// showResponseModel: when on, assistant responses show the producing model.
-// collapseThinkingByDefault: when on, thinking stays collapsed instead of
-// streaming open.
-// pastedTextMinChars: paste length that becomes a .txt attachment. 0 is off.
+// Client-side chat UI prefs kept in localStorage, not the chat DB. confirmDeleteChats: off skips
+// the delete confirm dialog. alwaysDeleteChatFiles: on also removes the sandbox folder.
+// showModelDisclaimer: off hides the "LLMs can make mistakes" footer. showResponseModel: on shows
+// the producing model on responses. collapseThinkingByDefault: on keeps thinking collapsed.
+// pastedTextMinChars: paste length that becomes a .txt attachment; 0 is off.
export interface ChatPreferencesState {
confirmDeleteChats: boolean;
setConfirmDeleteChats: (value: boolean) => void;
diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
index 31975aa00c4..6a739452a6f 100644
--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
@@ -143,16 +143,8 @@ export const MODELS_FIT_ON_DEVICE_ONLY_KEY =
export const CHAT_BYPASS_PERMISSIONS_KEY = "unsloth_chat_bypass_permissions";
export const CHAT_PERMISSION_MODE_KEY = "unsloth_chat_permission_mode";
-/**
- * Permission level for local tool calls:
- * - "ask": always ask before every tool call runs.
- * - "auto" ("Approve for me", the default): only ask for calls the backend
- * detects as high risk; ordinary dev commands run immediately. Sandbox stays on.
- * - "off": never ask; tool calls run automatically inside the sandbox
- * (the original default before permission levels existed).
- * - "full" ("Full access"): no confirmations and the python/terminal sandbox
- * is disabled. Session-only; never restored from storage.
- */
+/** Local tool-call gate: "ask" every call, "auto" only high-risk ones, "off" never but keeps
+ * the sandbox, "full" drops both and is session-only. */
export type PermissionMode = "ask" | "auto" | "off" | "full";
export const CHAT_WEB_FETCH_TOOLS_ENABLED_KEY =
"unsloth_chat_web_fetch_tools_enabled";
@@ -164,21 +156,18 @@ export const CHAT_RAG_AUTOINJECT_MIN_SCORE_KEY =
"unsloth_chat_rag_autoinject_min_score";
export const CHAT_RAG_OCR_KEY = "unsloth_chat_rag_ocr_scanned";
export const CHAT_RAG_CAPTION_KEY = "unsloth_chat_rag_caption_figures";
-// Persist only the model-agnostic intents (auto/ngram/off). The model-specific
-// drafter modes (mtp/mtp+ngram/dspark/dflash) and spec_draft_n_max stay session-only:
-// a persisted choice would silently no-op on a model with no MTP head or no
-// DSpark sidecar. Unknown -> auto.
+// Only the model-agnostic intents (auto/ngram/off) persist: a saved drafter mode no-ops on a
+// model with no MTP head or DSpark sidecar.
+// The model-specific drafter modes and spec_draft_n_max stay session-only.
const PERSISTED_SPEC_MODES = new Set(["auto", "ngram", "off"]);
export type RagSource = { type: "thread" } | { type: "kb"; kbId: string };
-/** Where the composer files an attachment in a project chat. `project` indexes
-
-/** Key a choice made in a chat that has no id yet lives under until it gets one. */
+/** Where the composer files an attachment in a project chat; `project` indexes it.
+ * Key a choice made in a chat with no id yet lives under until it gets one. */
export const PENDING_CHAT_ATTACHMENT_KEY = "__pending__";
-/** Bumped whenever the pending entry changes hands, so a composer that read it
- * can tell whether the one sitting there afterwards is still its own. */
+/** Bumped when the pending entry changes hands, so a composer can tell it is still its own. */
let pendingAttachmentTargetClaim = 0;
export function readPendingAttachmentTargetClaim(): number {
@@ -194,11 +183,9 @@ export const DEFAULT_RAG_TOP_K = 5;
export type RagAutoInject = "auto" | "on" | "off";
export const DEFAULT_RAG_AUTOINJECT: RagAutoInject = "auto";
export const DEFAULT_RAG_AUTOINJECT_MIN_SCORE = 0.7;
-// OCR scanned/image-only PDF pages at ingest time. On by default; off skips the
-// extra vision pass (only matters when the loaded chat model has vision).
+// OCR scanned/image-only PDF pages at ingest; off skips the extra vision pass (only matters with a vision chat model).
export const DEFAULT_RAG_OCR = true;
-// Describe figures/charts in PDFs at ingest time so they become searchable. On by
-// default (no-op without a vision model); off skips the per-figure vision calls.
+// Describe figures/charts in PDFs at ingest so they become searchable; no-op without a vision model.
export const DEFAULT_RAG_CAPTION = true;
export const DEFAULT_RESEARCH_WEBSITE_POLICY: ResearchWebsitePolicy = {
allowedDomains: [],
@@ -206,8 +193,7 @@ export const DEFAULT_RESEARCH_WEBSITE_POLICY: ResearchWebsitePolicy = {
};
export const DEFAULT_RESEARCH_MODEL_TIMEOUT_SECONDS = 900;
-/** 0 (unlimited) or a finite budget the settings patch and the run route both accept.
- * Anything else would be dropped from the patch and 400 the run, so the default stands in. */
+/** 0 (unlimited) or a finite budget; the patch and run routes drop anything else and 400. */
function isSupportedResearchModelTimeout(value: number): boolean {
if (!Number.isSafeInteger(value) || value < 0) return false;
if (value > MAX_RESEARCH_MODEL_TIMEOUT_SECONDS) return false;
@@ -326,12 +312,8 @@ function loadRagNumber(
}
}
-// External provider selection is encoded into `params.checkpoint` as
-// `external::<providerId>::<modelId>`. PersistedChatSettings omits `checkpoint`
-// because the local-model side is mirrored by the backend's
-// /api/inference/status.active_model. External selections have no such mirror,
-// so without explicit localStorage persistence here the user's external pick
-// is reset to the default on every refresh.
+// External picks ride in `params.checkpoint` as `external::<providerId>::<modelId>`.
+// PersistedChatSettings omits `checkpoint`: only the local side is mirrored by /status.
const LAST_EXTERNAL_CHECKPOINT_KEY = "unsloth_chat_last_external_checkpoint";
function loadLastExternalCheckpoint(): string | null {
@@ -382,25 +364,21 @@ function saveLastExternalCheckpoint(value: string | null): void {
if (value && isExternalModelId(value)) {
window.localStorage.setItem(LAST_EXTERNAL_CHECKPOINT_KEY, value);
} else {
- // Clear on switch to a local/empty checkpoint so the next refresh
- // won't override the now-active local selection.
+ // Cleared on a switch to a local/empty checkpoint, or the next refresh overrides it.
window.localStorage.removeItem(LAST_EXTERNAL_CHECKPOINT_KEY);
}
} catch {
- // Storage quota / private-mode failures are non-fatal; selection just
- // won't survive the refresh.
+ // Storage quota / private-mode failures are non-fatal; the selection just will not survive a refresh.
}
}
-// "enable_thinking_effort" is a hybrid: an on/off gate (enable_thinking) plus an
-// effort level among a discrete set (e.g. GLM-5.2's high|max). It reuses the
-// reasoning_effort dropdown UI but, unlike gpt-oss, can be fully disabled.
+// "enable_thinking_effort" is a gate plus a level (GLM-5.2 high|max): it reuses the
+// reasoning_effort dropdown but, unlike gpt-oss, can be turned off entirely.
export type ReasoningStyle =
| "enable_thinking"
| "reasoning_effort"
| "enable_thinking_effort";
-/** One live DiffusionGemma denoising snapshot: the current canvas text at a
- * given step of a given block (block/step are 0-based; total = steps in block). */
+/** One live DiffusionGemma denoising snapshot: canvas text at a step of a block (0-based; total = steps in block). */
export type DiffusionCanvasFrame = {
block: number;
step: number;
@@ -443,9 +421,8 @@ function warnSettingsPersistenceFailure(): void {
});
}
-// Coalesce setting writes into one pendingPatch (deep merge for nested keys),
-// flush on a trailing-edge debounce and on beforeunload so a pending patch
-// survives tab close. Slider drags produce one HTTP write per quiet window.
+// Setting writes coalesce into one pendingPatch, flushed on a trailing debounce and on
+// beforeunload so a pending patch survives tab close.
type SettingsPatch = Parameters<typeof savePersistedChatSettingsPatch>[0];
const SETTINGS_DEBOUNCE_MS = 400;
@@ -457,12 +434,12 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
-// Discriminated unions, not partial patches: merging a `thread` pick into a
-// stored `kb` one keeps `kbId`, which the backend's thread variant forbids.
+// Discriminated unions, not partial patches: merging a `thread` pick into a stored `kb` one
+// keeps `kbId`, which the backend's thread variant forbids.
const ATOMIC_SETTING_KEYS = new Set<string>(["ragSource"]);
-// Maps of per-model objects, merged a level further in: two edits to different
-// fields inside one debounce window must not replace each other.
+// Maps of per-model objects, merged one level further in: two edits to different fields in
+// one debounce window must not replace each other.
const NESTED_MAP_SETTING_KEYS = new Set<string>(["inferenceParamsByModel"]);
function mergePatch(into: SettingsPatch, more: SettingsPatch): void {
@@ -500,12 +477,8 @@ async function flushSettingsPatch(keepalive = false): Promise<void> {
try {
await savePersistedChatSettingsPatch(patch, { keepalive });
} catch (error) {
- // A rejected patch is NOT requeued as it stands. The endpoint is
- // extra="forbid" and refuses the whole body on one bad field, so a patch the
- // server will never accept, requeued forever, makes every later save fail
- // too and the tab can no longer persist any chat setting. Keep the fields
- // the server did not name, drop the ones it did, and only reschedule when
- // the patch actually got smaller so the retry chain is bounded.
+ // extra="forbid" refuses the whole body on one bad field, so requeueing as-is would break
+ // every later save. Keep the fields the server did not name; reschedule only if it shrank.
const { patch: retryable, progressed } = retryablePatchAfterFailure(
patch,
error,
@@ -521,9 +494,7 @@ async function flushSettingsPatch(keepalive = false): Promise<void> {
}
}
-// Flushes handed to the network and not yet answered. pendingPatch and
-// pendingTimer are both empty across that window, so they cannot answer "is a
-// settings write still outstanding" on their own.
+// Flushes handed to the network and not yet answered: pendingPatch and pendingTimer are both empty across that window.
let unsettledFlushes = 0;
function settingsWritesAreDrained(): boolean {
@@ -558,25 +529,15 @@ function saveSettingsPatch(patch: SettingsPatch): void {
scheduleSettingsFlush();
}
-// A wedged PATCH must not hold a send open. Past this the run goes ahead on the
-// value the server already has, which is exactly where it stood before.
+// A wedged PATCH must not hold a send open; past this the run goes ahead on the value the server already has.
const SETTINGS_FLUSH_TIMEOUT_MS = 2000;
-/**
- * Send the debounced settings patch now and wait for it.
- *
- * Some settings are read by the backend out of SQLite at call time rather than
- * being carried in the request -- Search images picks the web_search schema that
- * way -- and the mirror above is a trailing-edge debounce, so a message sent
- * inside that window would run on the value before the toggle. Returns
- * immediately when nothing is queued, which is every send but one right after a
- * settings change.
- */
+/** Flush the debounced patch and wait. The backend reads some settings out of SQLite at call
+ * time, so a message sent inside the window would run on the pre-toggle value. */
export async function flushPendingChatSettings(): Promise<void> {
const queued = pendingTimer !== null || Object.keys(pendingPatch).length > 0;
- // Not just what is queued: the debounce may have fired already and handed its
- // patch to a request the server has not answered, which leaves both of those
- // empty while the value the backend reads is still the old one.
+ // Not just what is queued: the debounce may already have handed its patch to an unanswered
+ // request, leaving both empty while the backend still reads the old value.
if (!queued && unsettledFlushes === 0) return;
if (pendingTimer !== null) {
clearTimeout(pendingTimer);
@@ -596,36 +557,27 @@ export async function flushPendingChatSettings(): Promise<void> {
}
}
-// Best-effort flush of any pending patch when the page is going away. keepalive
-// lets the PUT outlive the unload; without it the browser cancels the fetch and
-// the user's last slider drag is dropped.
+// keepalive lets the PUT outlive the unload; without it the last slider drag is dropped.
function flushSettingsOnPageHidden(terminal: boolean): void {
if (pendingTimer !== null) clearTimeout(pendingTimer);
- // A captured edit lives only in the debounce, so send it before the tab goes.
- // Only on a terminal event, though: the beacon PATCHes the row directly and a
- // thread whose row has not been created yet answers 404, where the normal path
- // would have created it first. visibilitychange is not terminal (it fires on
- // every tab switch), and the page is still there to await the ensure.
- // Chats whose newest values have just gone out, so an older unsettled snapshot for
- // the same chat is not sent after them: each beacon takes a higher seq than the last,
- // which would make the stale one the winner.
+ // Terminal events only: the beacon PATCHes the row directly, and a row that does not exist
+ // yet answers 404. Chats just sent are excluded, since each beacon takes a higher seq.
const sentNewest = new Set<string>();
const flushedThreadId = threadSettingsWriteThreadId;
flushThreadScopedSettingsWrite(terminal);
if (terminal && flushedThreadId !== null) sentNewest.add(flushedThreadId);
- // An edit made while its chat's read was still out lives only in heldThreadScopedEdits,
- // so the flush above does not see it. Effect cleanup is not guaranteed during unload and
- // its ordinary fetch would not outlive the page anyway, so send it from here, keepalive.
+ // An edit made while its chat's read was out lives only in heldThreadScopedEdits, and effect
+ // cleanup is not guaranteed during unload, so send it from here.
if (terminal) {
const heldThreadId = pendingPairingThreadId;
void commitHeldThreadScopedEditsToTheirThread(true);
if (heldThreadId !== null) sentNewest.add(heldThreadId);
- // And anything an earlier visibilitychange already flushed the normal way, which
- // this event would otherwise leave to a fetch the page is about to cancel.
+ // And anything an earlier visibilitychange flushed the normal way, which this event would
+ // otherwise leave to a cancelled fetch.
beaconUnsettledThreadSettingsWrites(sentNewest);
}
- // An edit still waiting on hydration is a user edit like any other, and the
- // tab is going away, so send it rather than let the next session hydrate over it.
+ // An edit still waiting on hydration is a user edit, so send it rather than let the next
+ // session hydrate over it.
drainPreHydrationPatch();
if (Object.keys(pendingPatch).length === 0) return;
// Counted like any other flush: a keepalive PUT still on the wire would land
@@ -641,14 +593,8 @@ function flushSettingsOnPageHidden(terminal: boolean): void {
if (typeof window !== "undefined") {
window.addEventListener("beforeunload", () => flushSettingsOnPageHidden(true));
- // beforeunload is not the end of a page's life on every platform: mobile
- // Safari and backgrounded Android tabs are routinely discarded without it, and
- // a page restored from the back/forward cache never unloaded at all. pagehide
- // and the hidden transition of visibilitychange are the two the platform does
- // guarantee, so the debounced patch is normally already gone by the time an
- // unload would have run. Safe to add rather than replace: the pending patch is
- // swapped out before the request, so a second call with nothing queued returns
- // immediately and the two never send the same edit twice.
+ // beforeunload never fires on discarded mobile tabs, so pagehide and visibilitychange-hidden
+ // are added too. Safe: the pending patch is swapped out before the request.
window.addEventListener("pagehide", () => flushSettingsOnPageHidden(true));
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") flushSettingsOnPageHidden(false);
@@ -718,11 +664,8 @@ const RAG_AUTOINJECT_SETTING: MirroredSettingCodec = {
decode: normalizeStoredRagAutoInject,
};
-/**
- * Settings that describe the installation rather than the browser holding them.
- * Each entry pairs a localStorage slot with the /api/chat/settings field that
- * carries it to another browser or a remote session.
- */
+/** Settings describing the installation rather than the browser: each pairs a localStorage
+ * slot with the /api/chat/settings field that carries it to another browser. */
const MIRRORED_SETTINGS = {
reasoningEnabled: {
storageKey: CHAT_REASONING_ENABLED_KEY,
@@ -760,8 +703,7 @@ const MIRRORED_SETTINGS = {
showCanvasMenuItem: {
storageKey: CHAT_SHOW_CANVAS_MENU_ITEM_KEY,
...BOOLEAN_SETTING,
- // A profile predating the visibility flag keeps Canvas shown through an
- // explicit plus-menu pin, which loadShowCanvasMenuItem reads.
+ // A profile predating the visibility flag keeps Canvas shown through its plus-menu pin.
readForBackfill: () =>
readStorageValue(CHAT_SHOW_CANVAS_MENU_ITEM_KEY) !== null ||
readStorageValue(PLUS_MENU_PINS_STORAGE_KEY) !== null
@@ -785,8 +727,7 @@ const MIRRORED_SETTINGS = {
permissionMode: {
storageKey: CHAT_PERMISSION_MODE_KEY,
...STRING_SETTING,
- // A profile predating permission levels holds only the confirm toggle, and
- // loadPermissionMode derives the level from it.
+ // A profile predating permission levels holds only the confirm toggle; the level derives.
readForBackfill: () =>
readStorageValue(CHAT_PERMISSION_MODE_KEY) !== null ||
loadOptionalBool(CHAT_CONFIRM_TOOL_CALLS_KEY) !== null
@@ -839,13 +780,8 @@ const MIRRORED_SETTING_BY_STORAGE_KEY: ReadonlyMap<
let mirroredSettingsHydrated = false;
let preHydrationPatch: SettingsPatch | null = null;
-/**
- * Send a changed mirrored setting to the backend. An edit made before the
- * initial GET lands is held rather than dropped: the request would race the
- * hydrating value, so it is replayed once hydration has applied the server's.
- * The mutation version is bumped straight away either way, so hydration leaves
- * a field the user has just set alone.
- */
+/** An edit made before the initial GET lands is held and replayed after hydration, since the
+ * request would race it. The mutation version bumps either way, fencing the field. */
function mirrorSettingToBackend(key: string, raw: string): void {
const setting = MIRRORED_SETTING_BY_STORAGE_KEY.get(key);
if (!setting) return;
@@ -876,18 +812,13 @@ function flushPreHydrationSettings(): void {
saveSettingsPatch(patch);
}
-/**
- * Write a setting to its localStorage slot and, for the settings mirrored to
- * /api/chat/settings, to the backend as well. Storage is the synchronous boot
- * cache; the backend copy is what a second browser or a remote session reads.
- */
+/** Write a setting to localStorage and, when mirrored, to the backend. Storage is the
+ * synchronous boot cache; the backend copy is what another browser reads. */
function persistSetting(key: string, raw: string): void {
const mirrored = MIRRORED_SETTING_BY_STORAGE_KEY.get(key);
const writeGlobal = () => {
- // Before hydration the cache says nothing about the server's value, so an
- // explicit write is recorded even where it changes nothing locally. A
- // constraint write (deep research turning the tool pills off) has to reach
- // the backend, or hydration restores the toggle it was there to clear.
+ // Before hydration the cache says nothing about the server's value, so an explicit write is
+ // recorded even when it changes nothing locally.
if (!mirroredSettingsHydrated || readStorageValue(key) !== raw) {
mirrorSettingToBackend(key, raw);
}
@@ -903,11 +834,11 @@ const THREAD_SETTINGS_DEBOUNCE_MS = 400;
let threadScopedSettingsThreadId: string | null = null;
/** that thread's stored values, for the load paths that read a setting outside the store. */
let activeThreadScopedSettings: ThreadScopedSettings | null = null;
-// captured on the way into a thread: edits made with a chat open must not move the defaults.
+// Captured on the way into a thread: edits made with a chat open must not move the defaults.
let globalThreadScopedDefaults: ThreadScopedSettings | null = null;
let threadSettingsWriteTimer: ReturnType<typeof setTimeout> | null = null;
let threadSettingsWriteThreadId: string | null = null;
-// set only when pinning: the values applied on open, not whatever a model-capability effect
+// Only when pinning: the values applied on open, not whatever a model-capability effect
// leaves in the store by the time the debounce fires.
let threadSettingsWriteSnapshot: ThreadScopedSettings | null = null;
@@ -928,24 +859,16 @@ function readThreadScopedSettings(
for (const key of THREAD_SCOPED_SETTING_KEYS) {
source[key] = readThreadScopedValue(state, key);
}
- // drops "full" with it: a stored bypass would come back without the warning dialog.
+ // Drops "full" with it: a stored bypass would come back without the warning dialog.
return sanitizeThreadScopedSettings(source);
}
-// keeps a model load from re-applying the global default over the pills the chat is running with.
+// Keeps a model load from re-applying the global default over the pills the chat is running with.
export function threadScopedOverride<K extends ThreadScopedSettingKey>(
key: K,
): ThreadScopedSettings[K] | undefined {
- // activeThreadScopedSettings is only refreshed when the debounce fires, so for the 400ms
- // after an edit it still holds the pre-edit values, and the capture path deliberately
- // wrote nothing to localStorage either. A model load or a status poll landing in that
- // window would read the old value, revert the pill, and then be persisted by the write
- // the edit itself scheduled. The store already holds the edit, so prefer it.
- // A pending PIN carries its own snapshot, which is what the chat is about to be
- // stored as, so that answers instead of the live store. Falling through to
- // activeThreadScopedSettings here reads null, because a chat being pinned had no
- // snapshot to begin with, and the load then puts the global values back over the
- // edit the queued write is about to persist.
+ // activeThreadScopedSettings only refreshes on the debounce, so for 400ms it holds pre-edit
+ // values a load would revert and persist; prefer the store. A pending pin answers first.
if (
threadSettingsWriteThreadId !== null &&
threadSettingsWriteThreadId === threadScopedSettingsThreadId
@@ -962,21 +885,12 @@ export function threadScopedOverride<K extends ThreadScopedSettingKey>(
return activeThreadScopedSettings?.[key];
}
-/**
- * Fields the user has just set on the open chat, as opposed to ones a model's
- * capabilities moved in the store. The preservations below exist to stop a clamp erasing
- * a stored choice; they must not also undo a choice the user has only now made.
- */
+/** Fields the user just set on the open chat, as opposed to ones a model's capabilities
+ * moved. The preservations below must not undo a choice only now made. */
const explicitlyEditedThreadFields = new Set<string>();
-/**
- * Fields a provider constraint has moved in the store WITHOUT persisting them. Kimi's
- * builtin web search may not run with thinking, so the composer turns the other pill off
- * with `{ persist: false }`; that write bypasses persistSetting, and so the capture path,
- * on purpose. The value in the store is then the provider's, not the user's, and the
- * next full-snapshot write must not save it over what the chat has stored, or a chat
- * that asked for thinking comes back without it on a model that allows it.
- */
+/** Fields a provider constraint moved WITHOUT persisting (Kimi's search turns thinking off
+ * with `{persist:false}`). That value is the provider's, so no snapshot write may save it. */
const constraintSuppressedThreadFields = new Set<string>();
/** Both pills of Kimi's search/thinking exclusion; the only non-persisting setters. */
@@ -1014,14 +928,8 @@ const CLAMPED_PILL_KEYS = [
] as const;
type ClampedPillKey = (typeof CLAMPED_PILL_KEYS)[number];
-/**
- * The value of a sampling edit still waiting on its chat's read, if there is one.
- *
- * Pills can be read back off the store when the window closes, but the sampling keys
- * share `params` with the model's recommendation, so a load landing in the same window
- * would overwrite the edit first, pinning the model's value onto the chat as if the user
- * had chosen it. Captured at the edit instead; last entry wins, as the read-back did.
- */
+/** Sampling keys share `params` with the model's recommendation, so a load in this window
+ * would pin the model's value onto the chat. Captured at the edit; last entry wins. */
function heldThreadScopedParamValue(key: string): unknown {
for (let i = heldThreadScopedEdits.length - 1; i >= 0; i -= 1) {
if (heldThreadScopedEdits[i].field === key) {
@@ -1031,24 +939,18 @@ function heldThreadScopedParamValue(key: string): unknown {
return undefined;
}
-/**
- * The first value that is actually set. `??` cannot do this any more: `seed` is null when
- * the pin is cleared, and null there is the chat's own choice rather than a missing key.
- */
+/** First value actually set. `??` cannot do it: a cleared `seed` is null, and null there is
+ * the chat's own choice rather than a missing key. */
function firstSetThreadScopedValue(...values: unknown[]): unknown {
return values.find((value) => value !== undefined);
}
-/**
- * Put back the sampling keys the open chat owns, so a model load or status poll applying
- * that model's recommendation leaves the chat running on what it stored. Only an unpinned
- * chat falls through to the model's values.
- */
+/** Put back the sampling keys the open chat owns, so a load or status poll leaves it running
+ * what it stored. Only an unpinned chat falls through to the model's values. */
function restoreThreadScopedParams(params: InferenceParams): InferenceParams {
const kept: Record<string, unknown> = {};
for (const key of THREAD_SCOPED_PARAM_KEYS) {
- // Not ||, and not ?? either: 0, "", -1 and a cleared seed's null are all values a
- // user sets on purpose here.
+ // Not ||, and not ?? either: 0, "", -1 and a cleared seed's null are all values a user sets on purpose.
const held = firstSetThreadScopedValue(
heldThreadScopedParamValue(key),
threadScopedOverride(key),
@@ -1061,18 +963,9 @@ function restoreThreadScopedParams(params: InferenceParams): InferenceParams {
return hasKeys(kept) ? { ...params, ...kept } : params;
}
-/**
- * Take the open chat's own values back out of a snapshot about to be remembered against
- * a model: they belong to the chat, so leaving them in replays one chat's prompt and
- * sampling into the next chat opened on that model. What the model already remembered
- * wins over the installation values, so stepping off a model inside a chat does not
- * flatten a preference it was given outside one.
- *
- * A chat whose read is still out owns its keys just as much as an applied one (the edit
- * sits in heldThreadScopedEdits, not a snapshot); gating on the applied id alone let a
- * model switch in that window snapshot the chat's sampling into the outgoing model's
- * memory, which every other chat on that model then replays.
- */
+/** Take the open chat's own values out of a snapshot about to be remembered against a model,
+ * or its sampling replays into the next chat opened there. A chat whose read is still out
+ * owns its keys too, so gating on the applied id alone leaked them. */
function withoutActiveThreadParams(
state: ChatRuntimeStore,
params: InferenceParams,
@@ -1088,8 +981,8 @@ function withoutActiveThreadParams(
// Only a key this chat actually owns; the rest are already the model's.
const held = heldThreadScopedParamValue(key);
if (held === undefined && threadScopedOverride(key) === undefined) continue;
- // For a held key the installation copy can still be null and the store no longer
- // holds the pre-edit value; the sample taken when the window opened is that value.
+ // For a held key the installation copy can still be null and the store no longer holds the
+ // pre-edit value; the sample taken when the window opened is that value.
const own = firstSetThreadScopedValue(
remembered?.[key],
globalThreadScopedDefaults?.[key],
@@ -1103,11 +996,8 @@ function withoutActiveThreadParams(
return hasKeys(restored) ? { ...params, ...restored } : params;
}
-/**
- * Drop the sampling keys the open chat just took, so they reach neither the installation
- * defaults nor this model's memory: both are shared with every other chat. A model's own
- * values are never taken and pass straight through.
- */
+/** Drop the sampling keys the open chat just took: they must reach neither the installation
+ * defaults nor this model's memory, both shared with every other chat. */
function withoutCapturedThreadEdits(
changedParams: PersistedInferenceParams,
fromModelDefaults: boolean,
@@ -1117,8 +1007,7 @@ function withoutCapturedThreadEdits(
if (
isThreadScopedParamKey(key) &&
!fromModelDefaults &&
- // By value: this runs inside the updater, so the store still holds the pre-edit
- // value, and a load in the same pairing window is what a read-back would find.
+ // By value: this runs inside the updater, so a read-back would find the pre-edit value.
captureThreadScopedEdit(key, null, value)
) {
continue;
@@ -1128,18 +1017,14 @@ function withoutCapturedThreadEdits(
return shared;
}
-/**
- * Move the in-memory copy of the installation defaults to what was just written.
- * `applyThreadScopedSettings` falls back to it for a chat with no snapshot, so leaving it
- * stale means such a chat runs the sampling of whichever model was loaded before it.
- */
+/** Level the in-memory installation defaults with what was just written: a chat with no
+ * snapshot falls back to them, so a stale copy runs the previous model's sampling. */
function noteThreadScopedDefaults(shared: PersistedInferenceParams): void {
let next: Record<string, unknown> | null = null;
for (const [key, value] of Object.entries(shared)) {
if (!isThreadScopedParamKey(key)) continue;
- // Held field: the pairing capture restores it from the sample taken when the window
- // opened, so without this the pre-window value goes back and the in-memory defaults
- // stay behind the server's for the session, pinning onto the next snapshot-less chat.
+ // Held field: the pairing capture restores it from the pre-window sample, so without this
+ // the in-memory defaults stay behind the server's all session.
if (isHeldThreadScopedField(key)) {
hydratedDefaultsByHeldField.set(key, value);
}
@@ -1165,8 +1050,8 @@ function buildThreadScopedSnapshot(
): ThreadScopedSettings {
const settings =
snapshot ?? readThreadScopedSettings(useChatRuntimeStore.getState());
- // the write replaces the row, and the sanitizer drops a live "full", so without this the
- // level the chat had stored would be erased by any pill toggled under Full access.
+ // The write replaces the row and the sanitizer drops a live "full", so any pill toggled
+ // under Full access would otherwise erase the chat's stored level.
if (
settings.permissionMode === undefined &&
threadId === threadScopedSettingsThreadId &&
@@ -1174,13 +1059,8 @@ function buildThreadScopedSnapshot(
) {
settings.permissionMode = activeThreadScopedSettings.permissionMode;
}
- // Same for deep research, which apply() also holds back (external models and incognito
- // cannot run it). Without this, toggling any other pill in such a chat erases the true
- // it had stored, and it comes back off once the chat is on a local model again.
- //
- // Unless the user just changed it themselves: enabling Search, Code or Images clears
- // deep research deliberately, and restoring it here would ignore that and bring it
- // back, alongside Search, the moment the chat is on a local model again.
+ // Same for deep research, which apply() also holds back, or toggling any other pill erases
+ // the stored true. Unless the user just cleared it by enabling Search, Code or Images.
if (
threadId === threadScopedSettingsThreadId &&
!explicitlyEditedThreadFields.has("deepResearchEnabled") &&
@@ -1193,9 +1073,8 @@ function buildThreadScopedSnapshot(
) {
settings.deepResearchEnabled = true;
}
- // And for thinking, which a model that cannot stop thinking forces on in the store.
- // That true is the model's, so persisting it would erase a chat's stored false and
- // leave thinking on once the chat is back on a model where it is optional.
+ // And for thinking, which a model that cannot stop thinking forces on: that true is the
+ // model's, so persisting it would erase a chat's stored false.
if (
threadId === threadScopedSettingsThreadId &&
!explicitlyEditedThreadFields.has("reasoningEnabled") &&
@@ -1205,10 +1084,8 @@ function buildThreadScopedSnapshot(
) {
settings.reasoningEnabled = false;
}
- // Same again for every pill the model-selection pass in chat-page clamps off in the
- // store without touching the snapshot. The clamp is the model's, not the user's, so it
- // must not erase what the chat had stored; the condition is each pill's own capability
- // rule, which is exactly when the user could not have turned it off themselves.
+ // Same for every pill the model-selection pass clamps off without touching the snapshot:
+ // each pill's own capability rule is exactly when the user could not have done it.
if (threadId === threadScopedSettingsThreadId) {
const live = useChatRuntimeStore.getState();
const modelLoaded = !!live.params.checkpoint && !live.modelLoading;
@@ -1230,8 +1107,8 @@ function buildThreadScopedSnapshot(
}
}
}
- // And for the pills a provider constraint moved without persisting them: the store
- // holds the provider's value there, so the chat keeps the one it was stored with.
+ // And for pills a provider constraint moved without persisting: the store holds the
+ // provider's value, so the chat keeps the one it was stored with.
if (keepsStoredValueUnderConstraint("reasoningEnabled", threadId, settings)) {
settings.reasoningEnabled = activeThreadScopedSettings?.reasoningEnabled;
}
@@ -1241,8 +1118,7 @@ function buildThreadScopedSnapshot(
if (threadId === threadScopedSettingsThreadId) {
activeThreadScopedSettings = settings;
}
- // Spent: this snapshot has taken them into account, and the next one is about
- // whatever the user does next.
+ // Spent: this snapshot has taken them into account.
explicitlyEditedThreadFields.clear();
return settings;
}
@@ -1250,12 +1126,8 @@ function buildThreadScopedSnapshot(
const THREAD_SETTINGS_REPLAY_KEY = "unsloth_chat_thread_settings_replay";
const THREAD_SETTINGS_REPLAY_TIMEOUT_MS = 10_000;
-/**
- * Snapshots a terminal event sent but could not confirm, kept where the next session
- * will find them. The beacon cannot await anything: a chat whose row is still being
- * created answers 404 and the edit is gone, and the creation that follows knows nothing
- * about it. Writing the attempt down costs nothing and makes the loss recoverable.
- */
+/** Snapshots a terminal event sent but could not confirm, kept where the next session finds
+ * them: the beacon cannot await, and a row still being created answers 404. */
function rememberThreadSettingsForReplay(
threadId: string,
body: Record<string, unknown>,
@@ -1271,14 +1143,11 @@ function rememberThreadSettingsForReplay(
}
}
-/**
- * Re-send anything the last session could not confirm. Safe to run always: the seq the
- * body carries is that session's own, so a replay of a write that did land is refused
- * by the server rather than reverting anything newer.
- */
+/** Safe to run always: the body carries that session's own seq, so a replay of a write that
+ * did land is refused rather than reverting anything newer. */
export function replayUnconfirmedThreadSettings(): void {
- // Once per session: it is called from both hydration outcomes, and sending each body
- // twice would race two writes carrying the same seq for the same row.
+ // Once per session: both hydration outcomes call it, and sending each body twice would race
+ // two writes carrying the same seq.
if (threadSettingsReplayStarted) return;
threadSettingsReplayStarted = true;
if (!canUseStorage()) return;
@@ -1293,8 +1162,8 @@ export function replayUnconfirmedThreadSettings(): void {
}
const sent: Promise<unknown>[] = [];
for (const [threadId, body] of Object.entries(pending)) {
- // Bounded: every settings write in the session waits for these, so a socket that
- // never settles would leave the whole session unable to persist anything.
+ // Bounded: every settings write waits on these, so a socket that never settles would block
+ // persistence for the whole session.
const timeout = new AbortController();
const timer = setTimeout(() => timeout.abort(), THREAD_SETTINGS_REPLAY_TIMEOUT_MS);
const request = authFetch(`/api/chat/threads/${encodeURIComponent(threadId)}`, {
@@ -1304,23 +1173,18 @@ export function replayUnconfirmedThreadSettings(): void {
signal: timeout.signal,
})
.finally(() => clearTimeout(timer))
- // Only an ok response means it landed. authFetch resolves for 404 and 5xx too,
- // and the missing-row case this exists for is exactly the one that 404s, so
- // dropping the entry on any settled promise would throw the edit away for good.
+ // Only an ok response means it landed: authFetch resolves for 404 and 5xx too, and the
+ // missing-row case this exists for is exactly the one that 404s.
.then((res) => {
- // Only the body this request carried. A terminal event in THIS session can
- // store a newer body for the same thread while the replay is still out, and
- // that one is unconfirmed by definition: dropping it because an older replay
- // succeeded would leave the newest edit with nothing to recover it from.
+ // Only the body this request carried: a terminal event can store a newer one meanwhile, and
+ // dropping that because an older replay succeeded would lose the newest edit.
if (res.ok) forgetReplayedThreadSettings(threadId, body);
})
.catch(() => undefined);
sent.push(request);
}
- // Every snapshot write waits for this. The replay carries the PREVIOUS session's
- // writer id, so the server treats it as unrelated to anything this session sends and
- // will apply it whenever it arrives: on a slow connection a full snapshot from last
- // time could land after an edit made just now and revert it.
+ // Every snapshot write waits for this: the replay carries the PREVIOUS session's writer id,
+ // so the server applies it whenever it arrives and could revert a fresh edit.
threadSettingsReplaySettled = Promise.all(sent).then(() => undefined);
}
@@ -1328,12 +1192,8 @@ export function replayUnconfirmedThreadSettings(): void {
let threadSettingsReplaySettled: Promise<void> = Promise.resolve();
let threadSettingsReplayStarted = false;
-/**
- * Drop one entry, leaving the rest for the next attempt. `expected` narrows that to the
- * exact body the caller sent, for callers whose success says nothing about a newer entry
- * stored since; a caller that has itself just written the row passes nothing, because its
- * values supersede whatever the entry holds.
- */
+/** `expected` narrows the drop to the exact body the caller sent; a caller that has itself
+ * written the row passes nothing, since its values supersede the entry. */
function forgetReplayedThreadSettings(
threadId: string,
expected?: unknown,
@@ -1360,20 +1220,15 @@ function forgetReplayedThreadSettings(
}
}
-/**
- * Send the snapshot on tab close. The ensure-then-update chain cannot finish during unload, so
- * the row write goes straight out with keepalive, and what it could not confirm is left for
- * the next session to replay: a thread whose row is still being created answers 404, and the
- * creation that follows would otherwise land without the user's last edit.
- */
+/** The ensure-then-update chain cannot finish during unload, so the row write goes out with
+ * keepalive and whatever it cannot confirm is left for the next session. */
function sendThreadScopedSettingsBeacon(
threadId: string,
snapshot: ThreadScopedSettings | null,
merge = false,
): void {
- // A merge carries only what the user touched, for the chat whose own snapshot was
- // never read; sending a replacement built from the defaults on screen would erase
- // the rest of its row. Everything else replaces, as the debounced write does.
+ // A merge carries only what the user touched, for a chat whose snapshot was never read; a
+ // replacement built from the defaults on screen would erase the rest of its row.
const body = merge
? {
settingsPatch: snapshot,
@@ -1385,9 +1240,8 @@ function sendThreadScopedSettingsBeacon(
settingsSeq: nextThreadSettingsSeq(),
settingsWriter: threadSettingsWriter,
};
- // The beacon carries the newest values but skips the chain, so an older write would
- // otherwise land after it and put the stale snapshot back. The ticket stands down the
- // ones still queued; the abort ends the one already out, which no ticket can reach.
+ // The beacon carries the newest values but skips the chain, so an older write would land
+ // after it. The ticket stands down queued writes; the abort ends the one out.
takeThreadSettingsWriteTicket(threadId);
threadSettingsWriteAborts.get(threadId)?.abort();
rememberThreadSettingsForReplay(threadId, body);
@@ -1399,27 +1253,18 @@ function sendThreadScopedSettingsBeacon(
}).catch(() => undefined);
}
-// One chain per thread. The write REPLACES settings_json rather than merging it, so two
-// unordered writes do not merge into the newer one, they pick a winner: a slow first
-// request landing after a fast second restores the settings the user just moved away from.
+// One chain per thread: the write REPLACES settings_json, so two unordered writes pick a
+// winner and a slow first landing after a fast second reverts the user.
const threadSettingsWriteChains = new Map<string, Promise<unknown>>();
-// Newest snapshot issued per thread, so a write that is still waiting its turn can tell it
-// has been superseded and skip its PATCH rather than reinstate what it captured.
+// Newest snapshot per thread, so a queued write can tell it was superseded and skip its PATCH.
const threadSettingsWriteTickets = new Map<string, number>();
// The request each thread currently has out, so a newer snapshot can stand it down.
const threadSettingsWriteAborts = new Map<string, AbortController>();
-/**
- * Stamps each snapshot write so the server can refuse this tab's own older ones, which
- * is the case that needs it: a keepalive sent on unload can be undone by a PATCH the
- * server already had in hand, and no client-side abort reaches that.
- *
- * The id makes the ordering per-tab. A plain counter, and never a clock: comparing one
- * machine's numbers with another's means the browser that happens to be behind has
- * every edit refused while still being told it saved. Across tabs the last write wins,
- * as it did before any of this.
- */
+/** Stamps each snapshot write so the server refuses this tab's older ones: a keepalive sent
+ * on unload can be undone by a PATCH the server already had. A counter, never a clock:
+ * comparing machines' clocks silently loses the slower browser's edits. */
const threadSettingsWriter = crypto.randomUUID();
let lastThreadSettingsSeq = 0;
@@ -1439,24 +1284,20 @@ function writeThreadScopedSettings(
threadId: string,
snapshot: ThreadScopedSettings | null,
): Promise<boolean> {
- // Built now, not inside the chain: the snapshot must describe the store as it is at the
- // moment of the edit, not as it will be once the previous write has finished.
+ // Built now, not inside the chain: the snapshot must describe the store at the moment of the edit.
const settings = buildThreadScopedSnapshot(threadId, snapshot);
- // Stamped with the snapshot, not with the request: the seq has to say when the edit
- // happened, not when its turn in the chain came up.
+ // Stamped with the snapshot, not the request: the seq says when the edit happened, not when its turn came up.
const settingsSeq = nextThreadSettingsSeq();
const ticket = takeThreadSettingsWriteTicket(threadId);
const previous = threadSettingsWriteChains.get(threadId) ?? Promise.resolve();
const next = previous
.catch(() => undefined)
- // Last session's unconfirmed writes go first, or one of them can land on top of
- // this edit: it carries a different writer id, so nothing on the server orders it.
+ // Last session's unconfirmed writes go first: one carries a different writer id, so nothing
+ // on the server orders it against this edit.
.then(() => threadSettingsReplaySettled)
.catch(() => undefined)
.then(async () => {
- // superseded while queued: sending this would undo the newer snapshot, and the
- // newer one is what the replay entry should be measured against, so this counts
- // as landed for the purposes of the caller.
+ // Superseded while queued: sending this would undo the newer snapshot, so it counts as landed for the caller.
if ((threadSettingsWriteTickets.get(threadId) ?? ticket) !== ticket) {
return true;
}
@@ -1471,16 +1312,14 @@ function writeThreadScopedSettings(
{ settings, settingsSeq, settingsWriter: threadSettingsWriter },
{ signal: controller.signal },
);
- // This session's values are now the row's, so a leftover replay entry from the
- // last one would revert them if it were ever retried.
+ // This session's values are now the row's, so a leftover replay entry from the last one would revert them.
forgetReplayedThreadSettings(threadId);
return true;
} catch {
- // the chat still behaves as edited; only the snapshot for the next visit is lost.
- // An abort lands here too, and that one is deliberate: a newer snapshot won.
+ // The chat still behaves as edited; only the snapshot for the next visit is lost. An abort
+ // lands here too, deliberately: a newer snapshot won.
if (!controller.signal.aborted) warnSettingsPersistenceFailure();
- // An abort means a newer write won and is tracked in its place; a real failure
- // means nothing reached the row, and the caller needs to know.
+ // An abort means a newer write won and is tracked in its place; a real failure means nothing reached the row.
return controller.signal.aborted;
} finally {
if (threadSettingsWriteAborts.get(threadId) === controller) {
@@ -1498,26 +1337,18 @@ function writeThreadScopedSettings(
return next;
}
-/**
- * Settle this chat's snapshot before anything reads it back. A chat edited, left and
- * re-entered has its PATCH in flight, and a GET overtaking it returns the pre-edit
- * snapshot, which then gets applied over the values the user set and written back on the
- * next edit. Pending debounce included, or the read races the timer instead.
- */
+/** A chat edited, left and re-entered has its PATCH in flight, and a GET overtaking it reads
+ * the pre-edit snapshot. Pending debounce included, or the read races the timer. */
export async function settleThreadScopedSettingsForCopy(
threadId: string,
): Promise<void> {
- // An edit made while this chat's read was still out lives in neither the debounce nor
- // the chain, so settling those alone leaves it behind and the copy takes the old row.
- // Only for a caller that is about to read the row server side: the pairing itself
- // waits on this function's sibling, which must not close the window it just opened.
+ // An edit made while this chat's read was out lives in neither the debounce nor the chain.
+ // Only for a caller about to read the row server side.
if (pendingPairingThreadId === threadId) {
await commitHeldThreadScopedEditsToTheirThread();
}
- // A flushed replacement write that failed resolves false rather than throwing, so
- // awaiting the chain alone cannot tell a saved edit from a lost one. The copy is made
- // server side from the row: going ahead on a failed write hands the new chat the
- // pre-edit snapshot and tells the user nothing.
+ // A flushed replacement write that failed resolves false rather than throwing, so the chain
+ // alone cannot tell a saved edit from a lost one.
if (!(await awaitThreadScopedSettingsWrite(threadId))) {
throw new Error("This chat's settings could not be saved before copying it");
}
@@ -1537,25 +1368,12 @@ export async function awaitThreadScopedSettingsWrite(
return landed !== false;
}
-/**
- * Every row write that has already been started, landed.
- *
- * Not a flush: a debounce that has not fired yet is left where it is, so a caller cannot
- * use this to make a write happen earlier than the store would have. It is for a caller
- * that has just let the debounce fire and now needs the rows before it reads them back,
- * and does not know which chats the store decided to write.
- *
- * The chain a write runs on ends in `await import("../utils/chat-history-storage")`. How
- * many event-loop turns that costs is a property of the machine -- the specifier has no
- * extension, so it resolves through a hook, which means filesystem work under a test
- * runner and a chunk fetch in a browser. A caller that instead spins a fixed number of
- * turns, or races the loader with an import of its own, is asserting something about the
- * machine rather than about the store. Awaiting the chains asserts the thing itself.
- */
+/** Every row write already started has landed. Not a flush: an unfired debounce is left
+ * alone. Loops rather than spinning a fixed count, since the chain ends in a dynamic
+ * import whose cost is a property of the machine. */
export async function awaitStartedThreadScopedSettingsWrites(): Promise<void> {
- // A chain that settles can leave a newer one behind it for the same chat, so this
- // repeats until the map is empty rather than awaiting one snapshot of it. Bounded, so a
- // write that keeps rescheduling itself surfaces as a failed assertion, not as a hang.
+ // A settled chain can leave a newer one behind for the same chat, so this repeats until the
+ // map is empty. Bounded, so a self-rescheduling write fails an assertion, not hangs.
for (let pass = 0; pass < 20 && threadSettingsWriteChains.size > 0; pass += 1) {
await Promise.allSettled([...threadSettingsWriteChains.values()]);
}
@@ -1578,27 +1396,19 @@ function flushThreadScopedSettingsWrite(keepalive = false): void {
trackUnsettledThreadSettingsWrite(threadId, snapshot);
}
-/**
- * Start a normal write and keep its snapshot until it settles.
- *
- * Some browsers fire visibilitychange(hidden) and then pagehide. The first flushes
- * normally and clears the pending snapshot, so the terminal event would find nothing to
- * beacon and a discarded page takes the ordinary fetch with it. Holding the snapshot
- * lets the terminal path send it again, keepalive.
- */
+/** Some browsers fire visibilitychange(hidden) then pagehide: the first flush clears the
+ * pending snapshot, so holding it lets the terminal path resend with keepalive. */
function trackUnsettledThreadSettingsWrite(
threadId: string,
snapshot: ThreadScopedSettings | null,
): void {
- // Identity, not value: two ordinary edits both carry a null snapshot, so comparing
- // the value let the first request's settle delete the second one's tracking, and a
- // terminal event then found nothing to resend for a write still in flight.
+ // Identity, not value: two ordinary edits both carry a null snapshot, so comparing values
+ // let one request's settle delete another's tracking.
const entry: UnsettledThreadSettingsWrite = { snapshot };
unsettledThreadSettingsWrites.set(threadId, entry);
void writeThreadScopedSettings(threadId, snapshot).then((landed) => {
- // Only a write that reached the row stops being unsettled. Dropping it on failure
- // left nothing for a terminal event to beacon, so the edit was lost on close and
- // came back reverted, which is the opposite of what this tracking is for.
+ // Only a write that reached the row stops being unsettled; dropping it on failure left
+ // nothing for a terminal event to beacon.
if (landed && unsettledThreadSettingsWrites.get(threadId) === entry) {
unsettledThreadSettingsWrites.delete(threadId);
}
@@ -1613,13 +1423,8 @@ const unsettledThreadSettingsWrites = new Map<
UnsettledThreadSettingsWrite
>();
-/**
- * Re-send anything a normal flush has not landed yet, with keepalive.
- *
- * `alreadySent` names the threads the terminal flush has just beaconed. Those carry the
- * newest values, and every beacon takes a higher seq than the last, so re-sending an
- * older unsettled snapshot for the same chat afterwards would make the stale one win.
- */
+/** `alreadySent` names the threads just beaconed: they carry the newest values and every
+ * beacon takes a higher seq, so resending an older snapshot would let the stale one win. */
function beaconUnsettledThreadSettingsWrites(
alreadySent: ReadonlySet<string>,
): void {
@@ -1651,22 +1456,20 @@ function scheduleThreadScopedSettingsWrite(
threadSettingsWriteThreadId = null;
threadSettingsWriteSnapshot = null;
if (pendingThreadId !== null) {
- // Tracked the same way a manual flush is: once the timer has fired there is no
- // pending debounce left for a terminal event to find, so without this the write
- // in flight is the only copy and the page teardown cancels it.
+ // Tracked like a manual flush: once the timer fires there is no pending debounce for a
+ // terminal event to find, so the write in flight is the only copy and teardown kills it.
trackUnsettledThreadSettingsWrite(pendingThreadId, pendingSnapshot);
}
}, THREAD_SETTINGS_DEBOUNCE_MS);
}
-// the chat whose snapshot is in flight, and the edits made before it landed. The store already
-// shows them; only the read can say whether they belong to this chat or to the defaults, so they
-// wait here. Writing them globally in the meantime moved every other chat's default and was then
-// overwritten by the arriving snapshot, so the click both leaked and appeared to do nothing.
+// The chat whose snapshot is in flight, plus edits made before it landed: only the read can
+// say whether they are the chat's or the defaults'. Writing them globally moved every
+// other chat's default and was then overwritten by the arriving snapshot.
let pendingPairingThreadId: string | null = null;
/** The store's thread-scoped values as they stood when the current pairing began. */
let pairingWindowDefaults: ThreadScopedSettings | null = null;
-/** and the chat it was sampled for, so a retry does not resample over its own edit. */
+/** And the chat it was sampled for, so a retry does not resample over its own edit. */
let pairingWindowDefaultsThreadId: string | null = null;
let heldThreadScopedEdits: {
field: string;
@@ -1680,19 +1483,12 @@ export function beginThreadScopedPairing(threadId: string): void {
if (pendingPairingThreadId === threadId) return;
releaseHeldThreadScopedEdits();
pendingPairingThreadId = threadId;
- // What the installation defaults were before this chat could edit anything. Recorded
- // here because there is nowhere later to recover it from: an edit made during the
- // window overwrites the store, and on the first pairing of a session there is no
- // earlier capture to fall back on.
- //
- // Once per chat, not once per attempt: a retry after a failed read runs with the held
- // edit already in the store, and re-sampling would take that edit for a default.
+ // What the installation defaults were before this chat could edit anything; nothing later
+ // can recover it. Once per chat, not per attempt: a retry runs with the edit in the store.
if (pairingWindowDefaultsThreadId !== threadId) {
pairingWindowDefaultsThreadId = threadId;
- // Switching straight from one saved chat to another, the store still holds the
- // OUTGOING chat's values at this point, so it is not a source of defaults. The
- // capture made when that chat was paired is, and it exists whenever a chat is
- // applied. Only with no chat applied does the store itself hold the defaults.
+ // Switching straight between saved chats the store still holds the OUTGOING chat's values,
+ // so the capture made when it was paired is the source of defaults.
pairingWindowDefaults =
threadScopedSettingsThreadId === null
? readThreadScopedSettings(useChatRuntimeStore.getState())
@@ -1715,11 +1511,8 @@ function openThreadScopedPairingGate(threadId: string): void {
}
}
-/**
- * Release ONE chat's gate. Releasing all of them let a run started for A be freed by B's
- * pairing ending, which is the whole reason these are held per chat; the composer flag
- * is separate, and only tracks whether the chat now on screen is still waiting.
- */
+/** Release ONE chat's gate: releasing all let a run started for A be freed by B's pairing
+ * ending. The composer flag is separate and tracks only the chat on screen. */
function closeThreadScopedPairingGate(threadId: string | null): void {
if (threadId !== null) {
pairingSettledByThreadId.get(threadId)?.resolve();
@@ -1733,25 +1526,15 @@ function closeThreadScopedPairingGate(threadId: string | null): void {
}
}
-// Per chat, not one for all of them: a run started for A must not be released by B's
-// pairing ending, or it resumes and reads B's settings for A's run.
+// Per chat, not one for all: a run started for A must not be released by B's pairing ending.
const pairingSettledByThreadId = new Map<
string,
{ promise: Promise<void>; resolve: () => void }
>();
-/**
- * Resolves once `threadId`'s own settings are known, or immediately if they already
- * are. Every run goes through the adapter, so awaiting this there is what stops a
- * Reload, a Continue or an edit-and-send from starting on the installation defaults
- * that stand in while the read is out: a chat stored as "ask" would run as "off".
- * Bounded, so it cannot wait for the life of the tab.
- *
- * Resolves false when the wait ran out. The caller must NOT go ahead on that: the only
- * way to reach it is a chat left mid-read, whose gate is held shut deliberately and
- * whose settings never arrived, so the store now describes some other chat. Running
- * anyway is exactly the mix-up the gate exists to prevent, only 10 seconds later.
- */
+/** Resolves once `threadId`'s own settings are known; the adapter awaits it so a run cannot
+ * start on the installation defaults (a chat stored "ask" would run "off"). False means
+ * the wait ran out and the store now describes some other chat, so callers must stop. */
export function awaitThreadScopedPairing(
threadId: string | null | undefined,
): Promise<boolean> {
@@ -1766,10 +1549,10 @@ export function awaitThreadScopedPairing(
]);
}
-// Longer than the read can take (THREAD_READ_RETRIES retries, each bounded by
-// THREAD_READ_TIMEOUT_MS and spaced THREAD_READ_RETRY_MS apart, then a give-up that
-// opens the gate itself), so a slow read never reaches this and a run is only refused
-// for a chat whose pairing has genuinely been abandoned.
+// Longer than the read can take (retries, timeouts, and a give-up that opens the gate), so
+// a run is only refused for a chat whose pairing was genuinely abandoned.
+// THREAD_READ_RETRIES retries, each bounded by THREAD_READ_TIMEOUT_MS and spaced
+// THREAD_READ_RETRY_MS apart.
const THREAD_PAIRING_WAIT_MS = 30_000;
/** The chat turned out to own no snapshot: send the held edits to the defaults, as before. */
@@ -1779,8 +1562,7 @@ export function releaseHeldThreadScopedEdits(): void {
heldThreadScopedEdits = [];
pendingPairingThreadId = null;
pairingWindowDefaultsThreadId = null;
- // The answer is in: this chat owns no snapshot, so the defaults ARE its settings and
- // a run waiting on it can go.
+ // The answer is in: this chat owns no snapshot, so the defaults ARE its settings and a run waiting on it can go.
closeThreadScopedPairingGate(threadId);
for (const edit of held) {
// Written to the defaults now, so the value hydration held back is history.
@@ -1789,16 +1571,8 @@ export function releaseHeldThreadScopedEdits(): void {
}
}
-/**
- * The user left before the read finished. The edit belongs to the chat it was made in, so
- * write it there; replaying it into the installation defaults would move every
- * snapshot-less chat, which is the leak this path exists to prevent.
- *
- * The store still holds the edited values here: the incoming chat's own read has not
- * resolved yet, so nothing has overwritten them. A chat with no row of its own writes
- * nothing (`ensureStoredChatThread` only adopts a record that already exists), which
- * correctly leaves an unsaved chat following the defaults.
- */
+/** The user left before the read finished. The edit belongs to its chat, so write it there:
+ * replaying it into the installation defaults would move every snapshot-less chat. */
export function commitHeldThreadScopedEditsToTheirThread(
keepalive = false,
): Promise<void> {
@@ -1806,14 +1580,9 @@ export function commitHeldThreadScopedEditsToTheirThread(
const held = heldThreadScopedEdits;
heldThreadScopedEdits = [];
pendingPairingThreadId = null;
- // pairingWindowDefaultsThreadId is deliberately NOT cleared: a failed read commits
- // and then re-pairs the same chat, and the store holds the edit by then, so letting
- // the retry resample would take that edit for the installation default. Leaving for a
- // different chat resamples anyway, because the id no longer matches.
- //
- // The gate is NOT opened here. This runs when the user leaves mid-read, and this
- // chat's snapshot was never loaded, so a run still waiting on it must not be told the
- // settings are known: the store now shows the chat the user moved to.
+ // pairingWindowDefaultsThreadId is deliberately NOT cleared: a failed read re-pairs the same
+ // chat with the edit in the store, and resampling would take it for a default. The gate
+ // stays shut too, since the store now shows the chat the user moved to.
closeThreadScopedPairingGate(null);
if (threadId === null || held.length === 0) return Promise.resolve();
const changes = heldThreadScopedChanges(held);
@@ -1823,21 +1592,14 @@ export function commitHeldThreadScopedEditsToTheirThread(
sendThreadScopedSettingsBeacon(threadId, changes, true);
return Promise.resolve();
}
- // Returned rather than fired and forgotten: forking copies settings_json server side,
- // so it has to be able to wait for a held edit to reach the row first.
+ // Returned rather than fired and forgotten: forking copies settings_json server side, so it
+ // must wait for a held edit to reach the row.
return mergeThreadScopedSettingsIntoRow(threadId, changes);
}
-/**
- * Put the installation values back over the edits just written to the chat the user left.
- * Without this the store keeps showing that chat's temperature and system prompt, and the
- * next chat opened takes them: a snapshot-less one captures the store as the installation
- * defaults and is pinned with them, a brand new one simply runs on them.
- *
- * Only when the chat is actually being left. A failed read about to be retried commits the
- * same way while the chat stays open, as does a fork; putting the defaults back there
- * would undo an edit the user is looking at.
- */
+/** Put the installation values back over the edits just written to the chat being left, or
+ * the next chat takes its temperature and prompt. Only on a real leave: a retried read
+ * or a fork commits the same way while the chat stays open. */
function restoreDefaultsOverCommittedEdits(
threadId: string,
held: { field: string }[],
@@ -1850,8 +1612,8 @@ function restoreDefaultsOverCommittedEdits(
const fields: Record<string, unknown> = {};
const params: Record<string, unknown> = {};
for (const edit of held) {
- // The server answered for this field while the window was open, so that value is
- // the installation's; the pre-window sample is only what this browser had cached.
+ // The server answered for this field while the window was open, so that value is the
+ // installation's; the pre-window sample is only this browser's cache.
const value = hydratedDefaultsByHeldField.has(edit.field)
? hydratedDefaultsByHeldField.get(edit.field)
: before?.[edit.field];
@@ -1865,8 +1627,8 @@ function restoreDefaultsOverCommittedEdits(
}
}
if (!hasKeys(fields) && !hasKeys(params)) return;
- // setState and not setParams: these values are already the installation's, and going
- // through the setter would persist them back to it and to the loaded model's memory.
+ // setState, not setParams: these values are already the installation's, and the setter would
+ // persist them back to it and to the loaded model's memory.
useChatRuntimeStore.setState((state) =>
hasKeys(params)
? ({ ...fields, params: { ...state.params, ...params } } as Partial<ChatRuntimeStore>)
@@ -1880,8 +1642,8 @@ function heldThreadScopedChanges(
): ThreadScopedSettings {
const edited: Record<string, unknown> = {};
const live = useChatRuntimeStore.getState();
- // Same reader the snapshot path uses: sampling keys sit under `params`, so a direct
- // field read returns undefined and the sanitizer drops them.
+ // Same reader the snapshot path uses: sampling keys sit under `params`, so a direct field
+ // read returns undefined and the sanitizer drops them.
for (const edit of held) {
edited[edit.field] = readThreadScopedValue(
live,
@@ -1891,12 +1653,8 @@ function heldThreadScopedChanges(
return sanitizeThreadScopedSettings(edited);
}
-/**
- * PATCH just the fields in `changes`, leaving the rest of the row alone. Used when the
- * store cannot be trusted to describe the chat, which is any time its read has not
- * landed: the store is showing the installation defaults, and a replacement built from
- * those would erase everything the chat had stored that the user did not touch.
- */
+/** PATCH only `changes`. For when the store cannot be trusted to describe the chat (its read
+ * has not landed), since a replacement built from the defaults would erase the rest. */
async function mergeThreadScopedSettingsIntoRow(
threadId: string,
changes: ThreadScopedSettings,
@@ -1906,8 +1664,8 @@ async function mergeThreadScopedSettingsIntoRow(
const previous = threadSettingsWriteChains.get(threadId) ?? Promise.resolve();
const next = previous
.catch(() => undefined)
- // Last session's unconfirmed writes go first, or one of them can land on top of
- // this edit: it carries a different writer id, so nothing on the server orders it.
+ // Last session's unconfirmed writes go first: one carries a different writer id, so nothing
+ // on the server orders it against this edit.
.then(() => threadSettingsReplaySettled)
.catch(() => undefined)
.then(async () => {
@@ -1926,9 +1684,8 @@ async function mergeThreadScopedSettingsIntoRow(
forgetReplayedThreadSettings(threadId);
} catch (error) {
warnSettingsPersistenceFailure();
- // Rethrown as well as toasted: a fork waits on this to know the row holds the
- // edit before the backend copies it, and a resolved promise would let it make
- // a fork carrying the pre-edit snapshot.
+ // Rethrown as well as toasted: a fork waits on this to know the row holds the edit, and a
+ // resolved promise would let it fork the pre-edit snapshot.
throw error;
}
})
@@ -1942,17 +1699,9 @@ async function mergeThreadScopedSettingsIntoRow(
return next;
}
-/**
- * What the server said an installation default is, for fields hydration had to skip
- * because the user had just set them inside a chat whose read was still out. Those edits
- * belong to the chat, so when its pairing window closes the default goes back to the
- * value from before the window, which is this browser's pre-hydration copy. The server's
- * is the authoritative one and lands nowhere else.
- *
- * A default published by a model that loaded inside the window is recorded here for the
- * same reason: setParams already sent it to the installation, so restoring the pre-window
- * sample over it would leave this session's copy behind the server's.
- */
+/** What the server called an installation default, for fields hydration skipped because the
+ * user had just set them inside a chat whose read was out. When the pairing window closes
+ * the default returns to this rather than the browser's pre-hydration copy. */
const hydratedDefaultsByHeldField = new Map<string, unknown>();
/** Is this field an edit waiting on its chat's read, and so not the installation's to set? */
@@ -1960,7 +1709,7 @@ function isHeldThreadScopedField(field: string): boolean {
return heldThreadScopedEdits.some((edit) => edit.field === field);
}
-// reports whether the edit was taken; with no chat open the caller persists globally as before.
+// Reports whether the edit was taken; with no chat open the caller persists globally as before.
function captureThreadScopedEdit(
field: string,
writeGlobal: (() => void) | null = null,
@@ -1969,11 +1718,10 @@ function captureThreadScopedEdit(
if (!isThreadOwnedSettingKey(field)) return false;
const threadId = useChatRuntimeStore.getState().activeThreadId;
if (threadId === null) return false;
- // both ids: between a switch and its snapshot arriving the store still holds the old values.
+ // Both ids: between a switch and its snapshot arriving the store still holds the old values.
if (threadId === threadScopedSettingsThreadId) {
explicitlyEditedThreadFields.add(field);
- // Set by the user now, so the chat stores this rather than what it had before a
- // constraint moved the same field.
+ // Set by the user now, so the chat stores this rather than what it had before a constraint moved the same field.
constraintSuppressedThreadFields.delete(field);
scheduleThreadScopedSettingsWrite(threadId);
return true;
@@ -1985,17 +1733,9 @@ function captureThreadScopedEdit(
return false;
}
-/**
- * Persist a value a model load applied rather than one the user picked. Before
- * hydration such a write only reflects this browser's cache, so treating it as
- * an edit would both block the stored preference from hydrating and replay the
- * load's default over it. It reaches the cache and waits for the server's.
- *
- * `stillCurrent` says the value matches the live store. A load captures its
- * settings up front and persists them on success, so one that started before
- * hydration and finished after it would otherwise write the stale capture over
- * the preference that just arrived.
- */
+/** Persist a value a model load applied rather than one the user picked: before hydration
+ * such a write only reflects this browser's cache. `stillCurrent` says it matches the live
+ * store, so a load spanning hydration cannot write its stale capture over the answer. */
function persistLoadDerivedSetting(
key: string,
raw: string,
@@ -2021,14 +1761,8 @@ export function loadOptionalBool(key: string): boolean | null {
return raw === "true";
}
-/**
- * Resolve the web-search / code-execution pill state to apply when a model
- * loads. Honors the user's persisted preference so a tool-capable model never
- * re-enables a pill the user turned off, and never re-disables one they turned
- * on, and the open chat's own state wins over the installation default. When no
- * preference has been expressed the pills stay off: tool execution is opt-in, so
- * the person enables it with a click rather than a model turning it on for them.
- */
+/** Pill state to apply on a model load: the persisted preference wins in both directions, the
+ * open chat beats the installation default, and with no preference the pills stay off. */
export function resolveToolsEnabledOnLoad(supportsTools: boolean): {
toolsEnabled: boolean;
codeToolsEnabled: boolean;
@@ -2050,9 +1784,8 @@ function saveBool(key: string, value: boolean): void {
persistSetting(key, value ? "true" : "false");
}
-// The installation's own answer to the preserve-thinking switch, or null while it has
-// never given one. Hydration records the stored preference and the composer toggle
-// records the click; nothing else writes it.
+// The installation's own answer to the preserve-thinking switch, or null while it has never
+// given one. Only hydration and the composer toggle write it.
let storedPreserveThinking: boolean | null = null;
/** Record the preference a stored value or a toggle just expressed. */
@@ -2060,15 +1793,9 @@ function notePreserveThinkingPreference(value: boolean): void {
storedPreserveThinking = value;
}
-/**
- * The preserve-thinking value a model load or a status adoption should publish. The
- * family default the backend resolves (on for Qwen3.8, off everywhere else) is a
- * DEFAULT: it seeds the switch where the installation has never answered, and never
- * replaces an answer it gave, the same rule resolveToolsEnabledOnLoad applies to the
- * tool pills. That is also what makes a cold boot deterministic -- the settings GET and
- * the inference status race each other, and a load write that cannot overwrite a stored
- * preference leaves the same result whichever lands first.
- */
+/** The backend's family default is a DEFAULT: it seeds the switch where the installation
+ * never answered and never replaces an answer, as resolveToolsEnabledOnLoad does. That is
+ * what makes a cold boot deterministic when the settings GET and the status race. */
export function resolvePreserveThinkingOnLoad(resp: {
supports_preserve_thinking?: boolean | null;
preserve_thinking_default?: boolean | null;
@@ -2076,8 +1803,7 @@ export function resolvePreserveThinkingOnLoad(resp: {
return storedPreserveThinking ?? preserveThinkingDefaultFromLoad(resp);
}
-// The visibility flag shipped after the menu pins, so when it is absent,
-// profiles that had explicitly pinned Canvas keep it visible.
+// The visibility flag shipped after the menu pins, so when absent an explicit Canvas pin wins.
function loadShowCanvasMenuItem(): boolean {
const stored = loadOptionalBool(CHAT_SHOW_CANVAS_MENU_ITEM_KEY);
if (stored !== null) return stored;
@@ -2094,13 +1820,8 @@ function loadShowCanvasMenuItem(): boolean {
}
}
-/**
- * "full" is intentionally not restorable: it disables the sandbox and every
- * confirmation gate, so it must be re-enabled (through the warning dialog)
- * each session. First run falls back to the legacy "Confirm tool calls"
- * toggle so existing users keep their behavior (on -> ask, explicitly
- * off -> "off", i.e. no prompts); fresh installs default to "auto".
- */
+/** "full" is never restored: it disables the sandbox and every confirmation gate, so it needs
+ * the warning dialog each session. First run derives from the legacy confirm toggle. */
function loadPermissionMode(): PermissionMode {
return normalizeStoredPermissionMode(
readStorageValue(CHAT_PERMISSION_MODE_KEY),
@@ -2123,9 +1844,8 @@ function saveString(key: string, value: string): void {
persistSetting(key, value);
}
-// Canonicalises any backend value onto the Speculative Decoding dropdown's
-// modes ("auto"/"mtp"/"dspark"/"dflash"/"ngram"/"mtp+ngram"/"off"/null). Backend-only
-// legacy aliases map to their closest UI mode.
+// Canonicalises any backend value onto the Speculative Decoding modes; legacy backend-only
+// aliases map to their closest UI mode.
export function normalizeSpeculativeType(
v: string | null | undefined,
): string | null {
@@ -2190,9 +1910,8 @@ export function readPersistedSpeculativeType(): string {
return PERSISTED_SPEC_MODES.has(raw) ? raw : "auto";
}
-// MTP / null / unknown values are left unwritten so they stay session-only.
-// Called from the load path so only an applied preference is persisted, not an
-// unapplied dropdown edit the user might Reset or abandon before Apply.
+// MTP / null / unknown values stay unwritten and so session-only. Called from the load path,
+// so only an applied preference persists.
export function saveSpeculativeType(value: string | null): void {
if (value && PERSISTED_SPEC_MODES.has(value)) {
persistLoadDerivedSetting(
@@ -2203,8 +1922,7 @@ export function saveSpeculativeType(value: string | null): void {
}
}
-// GPU Memory strategy is a standing preference (like speculative type), not a
-// per-model setting: a "manual" choice persists across model switches and reloads.
+// A standing preference, not per-model: a "manual" choice survives model switches and reloads.
export function readPersistedGpuMemoryMode(): "auto" | "manual" {
return loadString(CHAT_GPU_MEMORY_MODE_KEY, "auto") === "manual" ? "manual" : "auto";
}
@@ -2217,9 +1935,8 @@ export function saveGpuMemoryMode(value: "auto" | "manual"): void {
);
}
-/** Persist the GPU Memory mode after a load, but only for a non-diffusion GGUF:
- * non-GGUF has no such mode, and diffusion runs mode-agnostic (reports "auto"),
- * so neither must clobber the standing manual preference. */
+/** Persist the GPU Memory mode after a load, but only for a non-diffusion GGUF: non-GGUF has
+ * no such mode and diffusion reports "auto", so neither may clobber the preference. */
export function persistGpuMemoryModeOnLoad(
resp: { is_gguf?: boolean; is_diffusion?: boolean },
mode: "auto" | "manual",
@@ -2230,8 +1947,8 @@ export function persistGpuMemoryModeOnLoad(
// Re-exported from its dependency-free home so existing imports keep working.
export { GPU_LAYERS_AUTO } from "../lib/gpu-placement";
-// Round real-valued shares to integers summing exactly to `total`, giving the
-// leftover units to the largest fractional parts (largest-remainder method).
+// Round real-valued shares to integers summing to `total`, leftovers to the largest
+// fractional parts (largest-remainder method).
function largestRemainder(shares: number[], total: number): number[] {
const out = shares.map((x) => Math.floor(x));
let rem = total - out.reduce((a, b) => a + b, 0);
@@ -2242,10 +1959,8 @@ function largestRemainder(shares: number[], total: number): number[] {
return out;
}
-// Spread `total` layers across GPUs in proportion to `weights` (e.g. per-GPU
-// VRAM), as integers summing exactly to `total`; even split for all-zero/empty
-// weights. Default per-GPU layer split before the user edits it (mirrors
-// llama.cpp's free-VRAM default).
+// Spread `total` layers over GPUs in proportion to `weights`, summing to `total`; even split
+// for all-zero weights. Mirrors llama.cpp's free-VRAM default.
export function distributeByWeight(total: number, weights: number[]): number[] {
if (weights.length === 0) return [];
const t = Math.max(0, Math.floor(total));
@@ -2258,10 +1973,8 @@ export function distributeByWeight(total: number, weights: number[]): number[] {
);
}
-// Set GPU `index` to `value` and rebalance the rest so per-GPU counts still sum
-// to `total`; others absorb the remainder in proportion to their counts (evenly
-// if all zero). The --tensor-split editor: counts are sent verbatim, and
-// llama.cpp gives each GPU exactly its count when gpu_layers == sum(counts).
+// Set GPU `index` to `value` and rebalance so the counts still sum to `total`. Counts are
+// sent verbatim, and llama.cpp honors each exactly when gpu_layers == sum(counts).
export function rebalanceSplit(
total: number,
counts: number[],
@@ -2285,14 +1998,9 @@ export function rebalanceSplit(
return out;
}
-// Validate a persisted gpu_ids pick against the GPUs present right now, before
-// restoring it from remembered settings. Returns null (= automatic) when the
-// pick is stale (none of the saved ids exist, or the host can't pin a multi-GPU
-// set), so a saved [1] on a now-1-GPU host doesn't get sent and rejected with no
-// way to clear it. A null pick (= automatic) passes through unchanged, and an
-// unpopulated device cache leaves the pick alone (the backend still guards).
-// An explicit null namespace means discovery had not completed when the live
-// state was captured, while an absent namespace is a legacy physical-ID pick.
+// Validate a persisted gpu_ids pick against the GPUs present now, returning null (automatic)
+// when stale, so a saved [1] on a 1-GPU host is not sent and rejected unclearably. A cold
+// device cache leaves it alone; an absent namespace is a legacy physical-ID pick.
export function reconcilePersistedGpuIds(
ids: number[] | null,
savedIndexKind?: GpuIndexKind | null,
@@ -2322,8 +2030,8 @@ export function requestedGpuIdsFromResponse(resp: {
: (resp.gpu_ids ?? null);
}
-// Store fields derived from a load/status response's GPU-memory settings.
-// Shared by every load path so the manual-knob round-trip can't drift.
+// Store fields derived from a load/status response's GPU-memory settings, shared by every
+// load path so the manual-knob round-trip cannot drift.
export function loadedGpuMemoryFields(resp: {
is_gguf?: boolean;
is_diffusion?: boolean;
@@ -2338,16 +2046,13 @@ export function loadedGpuMemoryFields(resp: {
requested_gpu_ids?: number[] | null;
diffusion_requested_ngl?: number | null;
}) {
- // GPU-memory state is meaningful only for a GGUF chat load. A non-GGUF response
- // still carries gpu_memory_mode (its default "auto" is serialized), so gate on
- // the authoritative is_gguf flag, not the field's presence -- otherwise loading
- // a transformers model would reset the standing manual preference.
+ // Meaningful only for a GGUF chat load, and a non-GGUF response still serializes
+ // gpu_memory_mode "auto", so gate on is_gguf or a transformers load resets the preference.
if (!resp.is_gguf) {
- // Clear the GPU pick / offload baseline a prior GGUF load may have left, so it
- // reflects the non-GGUF model (no pin) -- else a stale loadedGpuIds reads as
- // dirty (gpuIdsDirty is ungated) and Reset restores it while the picker is
- // hidden. gpuMemoryMode (the standing preference) is kept, but its loaded
- // baseline clears to null so Reset preserves the preference, not a stale mode.
+ // Clear the GPU pick / offload baseline a prior GGUF load left, else a stale loadedGpuIds
+ // reads as dirty. gpuMemoryMode stays but its baseline clears, so Reset keeps the mode.
+ // gpuIdsDirty is ungated, so Reset would restore it while the picker is hidden. gpuMemoryMode is
+ // kept as the standing preference, but its loaded baseline clears to null.
return {
selectedGpuIds: null,
selectedGpuIndexKind: null,
@@ -2369,33 +2074,26 @@ export function loadedGpuMemoryFields(resp: {
const hydratePlacementControls = shouldHydrateGpuPlacementControls(
resp.cpu_fallback_reason,
);
- // Keep the user's placement pool editable across status/load hydration.
- // gpu_ids remains the effective fitted subset for diagnostics.
+ // Keep the user's placement pool editable across hydration; gpu_ids stays the fitted subset.
const reportedGpuIds = requestedGpuIdsFromResponse(resp);
const gpuIndexKind =
reportedGpuIds == null
? null
: cachedPinnableGpuIndexKind(resp.is_diffusion === true);
- // A numeric ID is unsafe to adopt or persist once discovery says it is NOT a
- // physical CUDA/ROCm ID or a Vulkan ordinal (gpuIndexKind === null, cache
- // warm). But while discovery is still cold (gpuIndexKind === undefined) the
- // namespace is merely deferred, not rejected -- keep the just-applied pin so
- // a reload/rollback in that window doesn't omit gpu_ids and let llama.cpp
- // fall back to every device. A later status refresh resolves the namespace
- // once the shared system cache warms.
+ // A numeric id is unsafe to adopt once discovery says it is not a physical CUDA/ROCm id or
+ // Vulkan ordinal. While discovery is cold the namespace is merely deferred, so keep the
+ // pin, or a reload in that window lets llama.cpp fall back to every device.
const gpuIds =
reportedGpuIds != null && gpuIndexKind !== null ? reportedGpuIds : null;
- // A shim without --ngl reports Auto while the backend still holds the ask, so recover
- // it: in-memory state survives a reload but not a refresh.
+ // A shim without --ngl reports Auto while the backend still holds the ask, so recover it:
+ // in-memory state survives a reload but not a refresh.
const droppedSplit = recoverDroppedDiffusionSplit(
resp.is_diffusion,
mode,
resp.diffusion_requested_ngl,
);
- // Layer/MoE/split knobs apply (and are reported) only in manual mode; in auto
- // the server ignores them, so don't seed the loaded baseline or the editable
- // knobs with values it never applied. In manual, the server reports gpu_layers
- // = -1 under Auto, which round-trips the slider back to its Auto position.
+ // The layer/MoE/split knobs apply only in manual mode, so auto must not seed their baselines.
+ // In manual the server reports gpu_layers = -1 for Auto, round-tripping the slider.
const manualKnobs =
mode === "manual"
? {
@@ -2414,15 +2112,10 @@ export function loadedGpuMemoryFields(resp: {
loadedGpuLayers: null,
loadedNCpuMoe: null,
loadedSplitRatio: null,
- // Auto ignores these, so reset the editable knobs too (not just the
- // loaded baseline) -- else a later switch back to Manual would snapshot
- // and send a previous model's stale gpuLayers/nCpuMoe/split that this
- // load never applied. Mirrors the non-GGUF branch above.
- // Diffusion excepted: an "auto" diffusion response may be an older shim
- // DROPPING a manual split. Restore the ask when the response carries it (a
- // refresh has none left in memory), else keep what is standing. Resetting the
- // slider would turn the ask into manual/-1, unapplyable even after the
- // unsloth_zoo upgrade that adds --ngl.
+ // Auto ignores these, so reset the editable knobs or a switch back to Manual sends a previous
+ // model's values. Diffusion excepted: an "auto" response may be an older shim that dropped
+ // a manual split, so restore the ask when the response carries it. Resetting the slider
+ // would turn it into manual/-1, unapplyable even after the unsloth_zoo upgrade adds --ngl.
...(resp.is_diffusion
? droppedSplit != null
? { gpuLayers: droppedSplit }
@@ -2432,9 +2125,8 @@ export function loadedGpuMemoryFields(resp: {
splitRatio: null,
};
return {
- // A diffusion GGUF reporting "auto" ran on the runner's defaults, so an inert standing
- // manual preference must survive it. But "manual" means a split was actually applied
- // (#7574): adopt it, or a refresh hydrates back to "auto" while the runner serves one.
+ // A diffusion GGUF reporting "auto" ran on the runner's defaults, so an inert manual
+ // preference must survive it; "manual" means a split was applied (#7574), so adopt it.
...(hydratePlacementControls
? resp.is_diffusion && mode !== "manual"
? droppedSplit != null
@@ -2449,9 +2141,8 @@ export function loadedGpuMemoryFields(resp: {
moeLayerCount: resp.n_moe_layers ?? null,
// The picker reflects the requested placement pool, not a fitted subset.
selectedGpuIds: gpuIds,
- // gpuIndexKind is `undefined` only in the deferred (cache-cold) case here,
- // since a `null` kind already forced gpuIds to null above. Normalize to the
- // explicit-null-namespace convention (discovery not complete yet).
+ // gpuIndexKind is `undefined` only in the deferred cache-cold case, since a `null` kind
+ // already forced gpuIds to null; normalize to the explicit-null-namespace convention.
selectedGpuIndexKind: gpuIds == null ? null : (gpuIndexKind ?? null),
loadedGpuIds: gpuIds,
loadedGpuIndexKind: gpuIds == null ? null : (gpuIndexKind ?? null),
@@ -2459,8 +2150,7 @@ export function loadedGpuMemoryFields(resp: {
};
}
-// re-exported here so every existing importer keeps its path; defined in a leaf module
-// that a test can load without pulling the whole store in.
+// Re-exported so every importer keeps its path; defined in a leaf module a test can load.
export {
hasGgufSource,
isDownloadableHubRepo,
@@ -2477,10 +2167,8 @@ type ContextUsageSnapshot = {
cacheWriteTokens?: number;
};
-/**
- * One live run behind `runningByThreadId[id]`, with the `local` flag it started with so the
- * model-swap gate can tell llama-server runs from external ones when runs share a key.
- */
+/** One live run behind `runningByThreadId[id]`, with the `local` flag it started with so the
+ * model-swap gate can tell llama-server runs from external ones on a shared key. */
type ThreadRunOwner = {
owner: () => void;
local: boolean;
@@ -2494,11 +2182,8 @@ type ToolStatusEntry = {
type ChatRuntimeStore = {
settingsHydrated: boolean;
- /**
- * The open chat's own settings have been asked for but have not arrived. The store is
- * showing the installation defaults meanwhile, so a run started now would be captured
- * with them: a chat stored as "ask" could run tools without asking. Sending waits.
- */
+ /** The open chat's settings were asked for but have not arrived, so the store shows the
+ * installation defaults and a run started now would be captured with them. Sending waits. */
threadScopedSettingsPending: boolean;
params: InferenceParams;
/** Last-used sampling params per checkpoint id, replayed on model switch. */
@@ -2511,39 +2196,25 @@ type ChatRuntimeStore = {
loras: ChatLoraSummary[];
loraInventorySettled: boolean;
runningByThreadId: Record<string, boolean>;
- /**
- * The subset of `runningByThreadId` decoding on the local llama-server. Swapping the local
- * model neither interrupts an external-provider chat nor needs its consent, which is why
- * the backend keeps those out of `active_generations` too.
- */
+ /** Runs decoding on the local llama-server. Swapping the local model neither interrupts an
+ * external chat nor needs its consent, which is why the backend excludes them too. */
localRunByThreadId: Record<string, boolean>;
- /**
- * Which runs set `runningByThreadId[id]`; see `setThreadRunning`'s `owner`. A list, not one
- * entry: runs without a resolved thread id share the "__default" key, so one entry would let
- * a newer run's clear delete an older run's flag while it still generates.
- */
+ /** Which runs set `runningByThreadId[id]`; see `setThreadRunning`'s `owner`. A list, since
+ * runs with no resolved thread id share the "__default" key. */
runOwnerByThreadId: Record<string, ThreadRunOwner[]>;
cancelByThreadId: Record<string, () => void>;
- /**
- * Backend cancels for the threads generating in the background. `cancelByThreadId` only holds
- * the visible thread's `cancelRun()`, so the adapter parks a closure here that POSTs that
- * run's own cancel_id. A list for the same reason as `runOwnerByThreadId`: "__default" is shared.
- */
+ /** Backend cancels for background threads: `cancelByThreadId` holds only the visible thread's
+ * `cancelRun()`. A list, since unresolved ids share "__default". */
serverCancelByThreadId: Record<string, (() => void)[]>;
autoTitle: boolean;
hfToken: string;
modelsError: string | null;
- // Set only when a LOAD fails (not refresh/list/unload, which use modelsError);
- // lets the attach gates flag a failed load vs "no model picked".
+ // Set only when a LOAD fails (not refresh/list/unload, which use modelsError), so the attach
+ // gates can tell a failed load from "no model picked".
lastModelLoadError: string | null;
activeGgufVariant: string | null;
- /**
- * What /api/inference/status says is resident, as opposed to what the picker
- * has selected. undefined until the first status read, so the header does not
- * flash "not loaded" before anything is known. Loading an image or video
- * model evicts the chat model (one GPU owner at a time), which is otherwise
- * invisible here: the selection survives it.
- */
+ /** What /api/inference/status says is resident, as opposed to what the picker selected.
+ * undefined until the first read, so the header does not flash "not loaded". */
residentCheckpoint: string | null | undefined;
/** Whether the backend loaded the active model from a filesystem path. */
activeModelIsLocal: boolean;
@@ -2563,12 +2234,8 @@ type ChatRuntimeStore = {
supportsReasoning: boolean;
reasoningAlwaysOn: boolean;
reasoningEnabled: boolean;
- /**
- * The model id the OpenRouter router picked for the most recent stream when
- * the active checkpoint is the openrouter/free meta-model. Updated when a
- * chunk's `model` field differs from the requested id; cleared on a
- * non-OpenRouter model. UI display only (appended after `openrouter/free:`).
- */
+ /** The model the OpenRouter router picked for the latest stream under the openrouter/free
+ * meta-model. Cleared on a non-OpenRouter model; display only. */
lastOpenRouterChosenModel: string | null;
reasoningStyle: ReasoningStyle;
reasoningEffort: ReasoningEffort;
@@ -2577,31 +2244,17 @@ type ChatRuntimeStore = {
supportsPreserveThinking: boolean;
preserveThinking: boolean;
supportsTools: boolean;
- /**
- * Whether the active external provider exposes a server-side web_search tool
- * (OpenAI's /v1/responses today). Distinct from `supportsTools` (the local
- * tool runtime): this only enables the composer's Search pill for external
- * models. Local models keep `supportsTools` only.
- */
+ /** Whether the active external provider exposes a server-side web_search (OpenAI /v1/responses).
+ * Distinct from `supportsTools`: this only lights the Search pill for external models. */
supportsBuiltinWebSearch: boolean;
- /**
- * Whether the active external provider exposes a server-side code-execution
- * tool (Anthropic's `code_execution_20250825` on Claude 4.x). Distinct from
- * `supportsTools` like supportsBuiltinWebSearch: Anthropic dispatches it
- * server-side. Read by both composers' Code pill gate.
- */
+ /** Whether the provider exposes server-side code execution (Anthropic `code_execution_20250825`).
+ * Distinct from `supportsTools`, since Anthropic dispatches it itself. */
supportsBuiltinCodeExecution: boolean;
- /**
- * Whether the active external provider exposes a server-side image-generation
- * tool (OpenAI's Responses-API `image_generation`). Gates the composer's
- * Images pill. Local models never receive it (their runtime can't dispatch it).
- */
+ /** Whether the provider exposes server-side image generation (OpenAI Responses API).
+ * Local models never receive it. */
supportsBuiltinImageGeneration: boolean;
- /**
- * Whether the active external provider exposes a server-side web_fetch tool
- * (Anthropic's `web_fetch_20250910` / `web_fetch_20260209`). Gates the
- * composer's Fetch pill, independent of Search.
- */
+ /** Whether the provider exposes server-side web_fetch (Anthropic `web_fetch_*`). Gates the
+ * composer's Fetch pill, independent of Search. */
supportsBuiltinWebFetch: boolean;
toolsEnabled: boolean;
codeToolsEnabled: boolean;
@@ -2621,8 +2274,7 @@ type ChatRuntimeStore = {
ragSource: RagSource;
/** Default composer attachment scope for chats inside a project. */
projectAttachmentTarget: ProjectAttachmentTarget;
- /** Per-chat override of that default, so a pick in one chat does not redirect
- * the rest. Session-only: a reload falls back to the saved default. */
+ /** Per-chat override of that default, so a pick in one chat does not redirect the rest. Session-only. */
projectAttachmentTargetByThread: Record<string, ProjectAttachmentTarget>;
ragMode: RagMode;
ragTopK: number;
@@ -2633,66 +2285,39 @@ type ChatRuntimeStore = {
ragOcrScanned: boolean;
// Describe figures/charts at ingest time (vision model required).
ragCaptionFigures: boolean;
- /**
- * When on, local Unsloth tool calls pause for an explicit allow/deny in the
- * chat before they run.
- */
+ /** When on, local Unsloth tool calls pause for an explicit allow/deny before they run. */
confirmToolCalls: boolean;
- /**
- * Bypass Permissions: when on, tool calls run with no confirmation gate
- * AND the python/terminal execution sandbox is disabled on the backend
- * (secrets are still stripped). Takes precedence over confirmToolCalls.
- * Kept in sync with permissionMode ("full" <=> true).
- */
+ /** Tool calls run with no confirmation gate AND no python/terminal sandbox (secrets still
+ * stripped). Outranks confirmToolCalls; kept in sync with permissionMode "full". */
bypassPermissions: boolean;
- /**
- * Permission level. Single source of truth for the bypass dropdowns;
- * bypassPermissions and confirmToolCalls mirror it so legacy call sites
- * keep working. "full" is session-only (never persisted).
- */
+ /** Permission level. Single source of truth for the bypass dropdowns; bypassPermissions and
+ * confirmToolCalls mirror it. "full" is session-only. */
permissionMode: PermissionMode;
- /** Whether the "Enable Bypass Permissions?" warning dialog is open. Lifted out
- * of the composer menu so confirming/cancelling it doesn't leave the menu frozen. */
+ /** Whether the bypass warning dialog is open. Lifted out of the composer menu so confirming
+ * it does not leave the menu frozen. */
bypassConfirmOpen: boolean;
- /**
- * Per-chat set of tool names the user chose to auto-approve via "Always
- * allow". Keyed by UI confirmation scope, not necessarily the backend
- * sandbox session id. Not persisted across reloads.
- */
+ /** Per-chat tool names auto-approved via "Always allow", keyed by UI confirmation scope
+ * rather than the backend sandbox session id. Not persisted. */
alwaysAllowToolsBySession: Map<string, Set<string>>;
- /**
- * Tool calls currently paused awaiting the user's allow/deny decision,
- * keyed by the scoped frontend tool-call id. Each entry carries the backend
- * ``approvalId`` to echo back and the ``sessionId`` the generation runs
- * under, so the confirmation always resolves the exact pending call. The
- * ``autoAllowKey`` scopes the UI-only "Always allow" bucket per chat.
- * Only backend-gated local tool calls are added here.
- */
+ /** Calls paused for allow/deny, keyed by scoped frontend tool-call id. Each carries the
+ * backend `approvalId` and the run's `sessionId` so the exact call resolves;
+ * `autoAllowKey` scopes "Always allow" per chat. Backend-gated local calls only. */
toolConfirmations: Record<
string,
{ approvalId: string; sessionId: string; autoAllowKey: string }
>;
- /**
- * Fetch pill state, independent of `toolsEnabled` (Search). Only
- * consulted when `providerSupportsBuiltinWebFetch` is true.
- */
+ /** Fetch pill state, independent of `toolsEnabled` (Search). Read only when the provider
+ * supports builtin web_fetch. */
webFetchToolsEnabled: boolean;
- /**
- * Live tool status per conversation ("Running Python: ...") with its start time. Keyed by
- * thread, or one chat's tool call shows above every other composer; the timestamp keeps the
- * counter running across a thread switch.
- */
- /**
- * Per-run entries, newest last. Unresolved threads share "__default", so one scalar per key
- * meant a finishing run's clear removed a sibling's status while its tool was still running.
- */
+ /** Live tool status per conversation with its start time. Keyed by thread, or one chat's
+ * tool call shows above every composer; the timestamp keeps the counter running. */
+ /** Per-run entries, newest last. Unresolved threads share "__default", so one scalar per key
+ * let a finishing run's clear remove a sibling's status. */
toolStatusByThreadId: Record<string, ToolStatusEntry[]>;
- /** Live stdout/stderr from running tools, keyed by toolCallId. Transient:
- * appended by tool_output, cleared on tool_end or run end. */
+ /** Live stdout/stderr from running tools by toolCallId; cleared on tool_end or run end. */
toolLiveOutput: Record<string, string>;
- /** Full live output of finished tools whose result was truncated for the
- * model, keyed by toolCallId. Set from tool_end; finished cards prefer it
- * over the truncated result. Session-transient. */
+ /** Full live output of finished tools whose result was truncated for the model. Finished
+ * cards prefer it over the truncated result. */
toolFullOutput: Record<string, string>;
generatingStatus: string | null;
autoHealToolCalls: boolean;
@@ -2712,49 +2337,36 @@ type ChatRuntimeStore = {
loadedKvCacheDtype: string | null;
speculativeType: string | null;
loadedSpeculativeType: string | null;
- /**
- * Why speculative decoding was disabled despite being requested, or null.
- * Mirrors InferenceStatusResponse.spec_fallback_reason.
- */
+ /** Why speculative decoding was disabled despite being requested, or null. Mirrors
+ * InferenceStatusResponse.spec_fallback_reason. */
specFallbackReason: string | null;
/** Projector recovery outcome for the active GGUF, or null. */
mmprojFallbackReason: MmprojFallbackReason | null;
- /**
- * Which drafter the loaded model's speculative resolution was about: "mtp",
- * "dspark" or "dflash". Paired with specFallbackReason: the reason alone cannot name the
- * file to fix, since Auto resolves the kind server-side and the requested mode
- * still reads "auto".
- */
+ /** Which drafter the speculative resolution was about ("mtp", "dspark", "dflash").
+ * specFallbackReason alone cannot name the file to fix, since Auto resolves it server-side. */
specDrafterKind: string | null;
/** User --spec-draft-n-max override (null = platform default). */
specDraftNMax: number | null;
loadedSpecDraftNMax: number | null;
- /** User --parallel slots override for GGUF loads (null = server default).
- * Never re-seeded from an echo: the resolved count would pin a blank control. */
+ /** --parallel slots override for GGUF loads (null = server default). Never re-seeded from an
+ * echo: the resolved count would pin a blank control. */
nParallel: number | null;
- /** Slots the last successful load sent (null = default); the rollback
- * re-sends it so a failed switch can't lose the override. */
+ /** Slots the last successful load sent (null = default); a rollback re-sends them so a failed
+ * switch cannot lose the override. */
loadedNParallel: number | null;
/** user --batch-size override for gguf loads (null = llama.cpp default 2048) */
nBatch: number | null;
/** batch size the last successful load sent (null = default) */
loadedNBatch: number | null;
- /**
- * Pass-through llama-server arguments the resident model is running, as far as
- * this client knows (null = none, or never told). Kept so a rollback after a
- * failed switch can put the previous model back with them: by then the target
- * load has already replaced the backend's inheritance source, so omitting the
- * field on that rollback reads as a cross-model pickup and restores the model
- * without the arguments it was running.
- */
+ /** Pass-through args the resident model is running, as far as this client knows. A rollback
+ * resends them: by then the target load has replaced the backend's inheritance source. */
loadedLlamaExtraArgs: string[] | null;
/** user --ubatch-size override for gguf loads (null = llama.cpp default 512) */
nUbatch: number | null;
/** micro-batch size the last successful load sent (null = default) */
loadedNUbatch: number | null;
- /** user --spec-draft-type-k/-v override, the DRAFT context's KV cache dtype
- * (null = llama.cpp default f16). Separate from kvCacheDtype, which is the
- * target model's. */
+ /** --spec-draft-type-k/-v override, the DRAFT context's KV dtype (null = f16). Separate from
+ * kvCacheDtype, which is the target model's. */
specDraftCacheDtype: string | null;
/** draft cache dtype the last successful load sent (null = default) */
loadedSpecDraftCacheDtype: string | null;
@@ -2774,37 +2386,31 @@ type ChatRuntimeStore = {
tensorParallel: boolean;
/** Backend-reported tensor-parallel state; null until first hydrated. */
loadedTensorParallel: boolean | null;
- /** What the RUNNING server was loaded with, as opposed to what the control now
- * shows: a pending per-model config is applied to disableVision before a switch
- * captures its rollback baseline, so only this survives to restore. */
+ /** What the RUNNING server was loaded with, as opposed to what the control shows: a pending
+ * per-model config reaches disableVision before a switch captures its baseline. */
loadedDisableVision: boolean | null;
/** Load a vision GGUF without its mmproj, freeing the projector's VRAM. */
disableVision: boolean;
- /** Backend-reported: image input is off by request, not by absence of a
- * projector. Null until first hydrated. */
+ /** Backend-reported: image input is off by request, not by absence of a projector. Null until first hydrated. */
loadedVisionDisabledByUser: boolean | null;
- /** GPU memory strategy for GGUF loads. "auto" = Unsloth picks GPUs and context
- * to fit; "manual" = you own the offload (gpuLayers < 0 = Auto/--fit, >= 0
- * pins layers + nCpuMoe). */
+ /** GPU memory strategy for GGUF loads. "auto" fits GPUs and context for you; "manual" owns
+ * the offload (gpuLayers < 0 = Auto/--fit, >= 0 pins layers + nCpuMoe). */
gpuMemoryMode: "auto" | "manual";
/** Backend-reported gpu memory mode; null until first hydrated. */
loadedGpuMemoryMode: "auto" | "manual" | null;
/** The active model must use the staged CPU-only runtime when it is reloaded. */
loadedCpuFallback: boolean;
- /** Manual mode: layers to offload to GPU. -1 = Auto (--fit); >= model layer
- * count = all. */
+ /** Manual mode: layers to offload to GPU. -1 = Auto (--fit); >= model layer count = all. */
gpuLayers: number;
loadedGpuLayers: number | null;
/** Manual mode: MoE expert layers to keep on CPU (--n-cpu-moe); 0 = none. */
nCpuMoe: number;
loadedNCpuMoe: number | null;
- /** Manual mode: per-GPU layer counts (--tensor-split), in GPU-in-use order;
- * null = unset (llama.cpp splits by free VRAM). */
+ /** Manual mode: per-GPU layer counts (--tensor-split), in GPU-in-use order; null = unset. */
splitRatio: number[] | null;
/** Backend-reported per-GPU split ratio (--tensor-split); null = unset. */
loadedSplitRatio: number[] | null;
- /** Model layer count (GGUF block_count); the manual gpu-layers ceiling is
- * this + 1 (the output layer is offloadable too). */
+ /** Model layer count (GGUF block_count); the manual ceiling is this + 1, the output layer too. */
ggufLayerCount: number | null;
/** MoE expert-layer count: the nCpuMoe slider max; 0/null hides the slider. */
moeLayerCount: number | null;
@@ -2815,31 +2421,23 @@ type ChatRuntimeStore = {
loadedGpuIds: number[] | null;
/** Backend-reported namespace paired with loadedGpuIds. */
loadedGpuIndexKind: GpuIndexKind | null;
- /** Persisted: expand every On Device GGUF repo's quantizations by default
- * instead of waiting for a click. */
+ /** Persisted: expand every On Device GGUF repo's quantizations by default. */
expandQuantizations: boolean;
/** Persisted: show non-downloaded quantizations too, not just downloaded. */
showAllQuantizations: boolean;
- /** Persisted, off by default: chart each downloaded model's VRAM footprint
- * under its row. Opt-in because the figures are estimates, and a row that
- * cannot be sized is better left plain than annotated with a guess. */
+ /** Persisted, off by default: chart each model's VRAM footprint. Opt-in, being estimates. */
showMemoryBar: boolean;
- /** Persisted, shared by the chat model selector and the Hub page: list only
- * models whose size fits this device's memory budget. */
+ /** Persisted, shared with the Hub page: list only models that fit this device's budget. */
fitOnDeviceOnly: boolean;
loadedIsMultimodal: boolean;
- /** Active model is a block-diffusion model (DiffusionGemma): drives the
- * denoising-canvas artifact auto-render. */
+ /** Active model is a block-diffusion model (DiffusionGemma): drives the denoising-canvas artifact auto-render. */
loadedIsDiffusion: boolean;
- /**
- * Live denoising frame per conversation ("__default" until the id exists). Transient: set per
- * step, cleared when the run ends, never persisted. Keyed, not global: two denoising chats
- * overwrote each other's frame, so the visible preview flickered or vanished.
- */
+ /** Live denoising frame per conversation ("__default" until the id exists). Transient and
+ * keyed, since two denoising chats overwrote each other's frame. */
activeDiffusionCanvasByThreadId: Record<string, DiffusionCanvasFrame>;
customContextLength: number | null;
- /** The pinned context the loaded model used (null = Auto), so dirty-tracking
- * and a later fit Apply can tell an explicit pin apart from Auto. */
+ /** The pinned context the loaded model used (null = Auto), so dirty-tracking and a later fit
+ * Apply can tell an explicit pin from Auto. */
loadedCustomContextLength: number | null;
defaultChatTemplate: string | null;
chatTemplateOverride: string | null;
@@ -2848,13 +2446,8 @@ type ChatRuntimeStore = {
activeThreadEpoch: number;
queuedSettingsEpoch: number;
activeProjectId: string | null;
- /**
- * Temporary / incognito chat toggle. When on, the active conversation
- * lives only in assistant-ui's in-memory repository and is never
- * persisted to studio.db -- so it stays out of history and vanishes on
- * reload. Deliberately ephemeral: NOT mirrored to localStorage or the
- * backend settings, so a refresh always exits incognito.
- */
+ /** Incognito toggle: the conversation lives only in assistant-ui's in-memory repository and
+ * never reaches studio.db, so a refresh always exits incognito. */
incognito: boolean;
settingsPanelOpen: boolean;
editingMessageId: string | null;
@@ -2862,21 +2455,17 @@ type ChatRuntimeStore = {
pendingAudioName: string | null;
pendingImageEditReference: PendingImageEditReference | null;
contextUsage: ContextUsageSnapshot | null;
- /**
- * Per-thread copy of the above, so the bar survives a switch away and back. `contextUsage` is
- * the VISIBLE conversation's usage and a background run may not write it, so without this a
- * run finishing off-screen leaves nothing to restore.
- */
+ /** Per-thread copy of the above, so the bar survives a switch: `contextUsage` is the VISIBLE
+ * conversation's, and a background run may not write it. */
contextUsageByThreadId: Record<string, ContextUsageSnapshot>;
modelLoading: boolean;
loadingModelPick: LoadingModelPick | null;
- // What the resident model loaded from, when that is not its id. A reload rebuilds its target
+ // What the resident model loaded from, when that is not its id: a reload rebuilds its target
// from the checkpoint, so without this it goes back down the ref the pin avoided.
activeLoadId: string | null;
activeNativePathToken: string | null;
- // Wall-clock expiry (ms) of the active native path token. The desktop host
- // prunes file leases after a TTL, so a reload checks this to prompt
- // re-selection instead of reusing a dead token.
+ // Expiry (ms) of the active native path token: the desktop host prunes file leases on a TTL,
+ // so a reload prompts re-selection instead of reusing a dead token.
activeNativePathExpiresAtMs: number | null;
hydratePersistedSettings: () => Promise<void>;
beginModelLoading: () => ModelLifecycleLease | null;
@@ -2889,9 +2478,8 @@ type ChatRuntimeStore = {
options?: {
persist?: boolean;
trackQueuedSettings?: boolean;
- /** These params are the model's defaults, so its remembered settings are laid back
- * over them even though the checkpoint did not change. Being the model's and not
- * the user's, they also move the installation defaults, never the chat's. */
+ /** These params are the model's defaults, so its remembered settings are laid back over them
+ * even with no checkpoint change; being the model's, they move the installation defaults. */
fromModelDefaults?: boolean;
/** The context the model just loaded with. */
maxTokensCap?: number;
@@ -2905,30 +2493,18 @@ type ChatRuntimeStore = {
setActivePresetSource: (source: ChatPresetSource) => void;
setModels: (models: ChatModelRow[]) => void;
setLoras: (loras: ChatLoraSummary[]) => void;
- /**
- * `local` defaults to true, so an unqualified caller still counts for the model-swap gate.
- * `owner` narrows the clear to the run that set the flag: unresolved thread ids share the
- * "__default" key, so a blind delete would drop a sibling's live entry. Owners accumulate,
- * so the flag survives until the last one clears.
- */
+ /** `local` defaults true so an unqualified caller still counts for the model-swap gate.
+ * `owner` narrows the clear, since unresolved ids share "__default" and owners accumulate. */
setThreadRunning: (
threadId: string,
running: boolean,
options?: { local?: boolean; owner?: () => void },
) => void;
- /**
- * Re-key a first turn's run handles once its thread is persisted.
- *
- * A run that starts before its id exists files everything under "__default". Nothing moved it
- * afterwards, so once the user navigated away the sidebar found no run and showed no spinner;
- * stopChatThread had no handle either and the generation carried on holding a slot.
- */
+ /** Re-key a first turn's run handles once its thread is persisted. Everything files under
+ * "__default" before the id exists, so the sidebar found no run and Stop had no handle. */
adoptDefaultThreadRun: (threadId: string) => void;
- /**
- * Which key this run's handles live under now. `adoptDefaultThreadRun` re-keys them mid-run,
- * so a run that started under "__default" must look its owner up instead of reusing the key
- * it captured, or its writes and its final clear miss the entries.
- */
+ /** Which key this run's handles live under now: `adoptDefaultThreadRun` re-keys them mid-run,
+ * so a run started under "__default" must look its owner up. */
runKeyForOwner: (fallbackKey: string, owner: () => void) => string;
registerThreadCancel: (threadId: string, cancel: () => void) => void;
clearThreadCancel: (threadId: string, cancel?: () => void) => void;
@@ -2943,8 +2519,8 @@ type ChatRuntimeStore = {
ggufVariant?: string | null,
options?: {
trackQueuedSettings?: boolean;
- /** False when the switch only puts back what was on screen before a
- * hidden load, so the model it steps off was never the user's. */
+ /** False when the switch only puts back what was on screen before a hidden load, so the model
+ * it steps off was never the user's. */
persist?: boolean;
/** The context the model just loaded with, when the caller knows it. */
maxTokensCap?: number;
@@ -3005,9 +2581,8 @@ type ChatRuntimeStore = {
threadId: string | null,
target: ProjectAttachmentTarget,
) => void;
- /** Carry a choice made before the chat existed onto its new id. `claim` is
- * the value readPendingAttachmentTargetClaim gave when the choice was made;
- * a newer one means the entry now belongs to a different composer. */
+ /** Carry a choice made before the chat existed onto its new id. `claim` is what
+ * readPendingAttachmentTargetClaim gave; a newer one means another composer owns it. */
adoptPendingProjectAttachmentTarget: (threadId: string, claim?: number) =>
void;
/** Drop a choice made in a composer that never became a chat. */
@@ -3018,10 +2593,8 @@ type ChatRuntimeStore = {
setRagAutoInjectMinScore: (score: number) => void;
setRagOcrScanned: (enabled: boolean) => void;
setRagCaptionFigures: (enabled: boolean) => void;
- /**
- * `owner` is the run's identity token, as for `setThreadRunning`: unresolved threads share
- * "__default", so without it one run's cleanup clears a concurrent run's status.
- */
+ /** `owner` is the run's identity token, as for `setThreadRunning`: unresolved threads share
+ * "__default", so without it one run's cleanup clears a concurrent run's status. */
setToolStatus: (
threadId: string,
status: string | null,
@@ -3039,8 +2612,7 @@ type ChatRuntimeStore = {
threadId: string | null,
canvas: DiffusionCanvasFrame,
) => void;
- /** Drop only `threadId`'s canvas: a run ending in a background chat must not wipe the
- * frame another chat is still painting. */
+ /** Drop only `threadId`'s canvas: a run ending in a background chat must not wipe another's. */
clearActiveDiffusionCanvasForThread: (threadId: string | null) => void;
setAutoHealToolCalls: (enabled: boolean) => void;
setNudgeToolCalls: (enabled: boolean) => void;
@@ -3176,8 +2748,8 @@ const SCALAR_SETTING_KEYS = [
"gpuMemoryMode",
] as const satisfies readonly ScalarSettingKey[];
-// Ids this browser holds a local answer for. Hydration keeps these and merges
-// the rest, so a pre-hydration edit cannot drop other models.
+// Ids this browser holds a local answer for. Hydration keeps these and merges the rest, so a
+// pre-hydration edit cannot drop other models.
const locallyRememberedModels = new Set<string>();
const inferenceParamMutationVersions = Object.fromEntries(
PERSISTED_INFERENCE_PARAM_KEYS.map((key) => [key, 0]),
@@ -3266,11 +2838,8 @@ function cacheHydratedSettings(
}
}
-/**
- * Seed the backend from this browser for mirrored settings it has never stored.
- * Without it an existing install would keep its preferences local until each
- * one is next changed.
- */
+/** Seed the backend from this browser for mirrored settings it never stored; without it an
+ * existing install keeps its preferences local until each is next changed. */
function readStoredSettingValue(
setting: { storageKey: string } & MirroredSettingCodec,
): unknown {
@@ -3292,9 +2861,8 @@ function backfillMirroredSettings(settings: PersistedChatSettings): void {
if (hasKeys(patch)) saveSettingsPatch(patch);
}
-/** `bumpVersions` fences the moved keys against a hydration response in flight.
- * A model's own defaults do not get that: they must not outrank the settings the
- * user saved for it. */
+/** `bumpVersions` fences the moved keys against a hydration response in flight. A model's own
+ * defaults do not get that: they must not outrank the settings saved for it. */
function getChangedInferenceParams(
nextParams: InferenceParams,
currentParams: InferenceParams,
@@ -3316,8 +2884,8 @@ function getChangedInferenceParams(
return changedParams;
}
-/** Bump the hydration versions for the replayed keys and mirror them into the
- * global set, so a reload lands on this model's settings. */
+/** Bump the hydration versions for the replayed keys and mirror them globally, so a reload
+ * lands on this model's settings. */
function persistReplayedParams(
state: ChatRuntimeStore,
nextParams: InferenceParams,
@@ -3329,15 +2897,13 @@ function persistReplayedParams(
const changed = getChangedInferenceParams(nextParams, state.params);
if (state.settingsHydrated && hasKeys(changed)) {
saveSettingsPatch({ inferenceParams: changed });
- // Same reason as the setParams write: the in-memory copy is what a chat with
- // no snapshot falls back to, so it has to move with what was just stored.
+ // Same reason as the setParams write: the in-memory copy is what a chat with no snapshot falls back to.
noteThreadScopedDefaults(changed);
}
}
-/** Same fence as the inference-param versions, recorded per model so only the
- * edited one is protected. Before hydration the params are placeholders, so
- * nothing is recorded. */
+/** Same fence as the inference-param versions, per model so only the edited one is protected.
+ * Before hydration the params are placeholders, so nothing is recorded. */
function trackParamsByModel(
state: ChatRuntimeStore,
paramsByModel: Record<string, PersistedInferenceParams> | null,
@@ -3363,12 +2929,8 @@ function getReplayStatePatch(
return outgoing ? { paramsByModel: outgoing } : {};
}
-/** The map after a setParams call, falling back to the outgoing snapshot. A
- * non-persisting update (a background load) must not rewrite durable memory.
- *
- * Only an edit is recorded: defaults are not settings the model was used with,
- * and staged load params describe the model about to load. Both are picked up
- * by the snapshot taken when the model is left. */
+/** Falls back to the outgoing snapshot: a non-persisting update (a background load) must not
+ * rewrite durable memory. Only an edit is recorded; defaults ride the leaving snapshot. */
function getParamsByModelAfterEdit(
state: ChatRuntimeStore,
outgoing: Record<string, PersistedInferenceParams> | null,
@@ -3386,9 +2948,8 @@ function getParamsByModelAfterEdit(
outgoing ?? state.paramsByModel,
nextParams.checkpoint,
changedParams,
- // Same filter as the outgoing snapshot: an edit to a key the memory keeps but the
- // chat does not (maxTokens, fastMode) still records the WHOLE snapshot, so the open
- // chat's sampling would ride into the model's entry and replay into the next chat.
+ // Same filter as the outgoing snapshot: an edit to a key the memory keeps but the chat does
+ // not still records the WHOLE snapshot, riding the chat's sampling into the entry.
pickRememberedParams(withoutActiveThreadParams(state, nextParams)),
),
nextParams.checkpoint,
@@ -3396,8 +2957,7 @@ function getParamsByModelAfterEdit(
return recorded ?? outgoing;
}
-/** Snapshot the model being switched away from, so a model that was never edited
- * still keeps what it ran with. */
+/** Snapshot the model being switched away from, so a model that was never edited still keeps what it ran with. */
function rememberOutgoingModel(
state: ChatRuntimeStore,
outgoing: InferenceParams,
@@ -3408,9 +2968,8 @@ function rememberOutgoingModel(
const snapshot = pickRememberedParams(
withoutActiveThreadParams(state, outgoing),
);
- // Only to seed a model with no entry: later changes are written key by key by
- // the edit that made them, and a full snapshot would put this browser's copy
- // of untouched keys over another tab's.
+ // Only to seed a model with no entry: later changes are written key by key, and a full
+ // snapshot would put this browser's copy of untouched keys over another tab's.
const seeding = state.paramsByModel[outgoing.checkpoint] === undefined;
const next = trackParamsByModel(
state,
@@ -3423,8 +2982,8 @@ function rememberOutgoingModel(
),
outgoing.checkpoint,
);
- // A full snapshot rewrites every field, so send one only when this browser has
- // something to say: an edit made here, or an entry that does not exist yet.
+ // A full snapshot rewrites every field, so send one only when this browser has something to
+ // say: an edit made here, or an entry that does not exist yet.
if (next && state.settingsHydrated && outgoing.checkpoint && seeding) {
saveSettingsPatch({
inferenceParamsByModel: { [outgoing.checkpoint]: snapshot },
@@ -3442,8 +3001,8 @@ function persistParamEdit(
if (!hasKeys(changedParams)) {
return;
}
- // Only what moved: the server merges per key, so sending the rest would put
- // this browser's copy of every other key over another tab's.
+ // Only what moved: the server merges per key, so sending the rest would overwrite another
+ // tab's copy of every other key.
const rememberedChanges = pickRememberedChanges(changedParams);
saveSettingsPatch({
inferenceParams: changedParams,
@@ -3502,8 +3061,7 @@ function getHydratedPresetState(
return nextState;
}
-/** Keys the user moved while the request was in flight: the same fence, read the
- * other way round. */
+/** Keys the user moved while the request was in flight: the same fence, read the other way round. */
function pickLocallyEditedParams(
params: InferenceParams,
versions: SettingsHydrationVersions,
@@ -3550,9 +3108,8 @@ function capParamsToLoadedContext(
* own entry but outrank the global set, which belongs to the last model used. */
let modelLoadedBeforeHydration: string | null = null;
-/** A model stepped off before hydration. Nothing can be filed for it yet, but the
- * global set the response will deliver is what it ran with, so this says which
- * model to file that under. */
+/** A model stepped off before hydration: nothing can be filed for it yet, but the global set
+ * the response delivers is what it ran with, so this says who to file it under. */
let modelLeftBeforeHydration: string | null = null;
function noteModelDefaultsBeforeHydration(
@@ -3578,12 +3135,8 @@ function noteModelDefaultsBeforeHydration(
}
}
-/**
- * Whether the active checkpoint is an external model that cannot run deep
- * research, matching the composer's own rule. An unresolved provider is left
- * alone: the connection list may not have loaded, and refusing on that would
- * drop a Codex user's stored preference.
- */
+/** Whether the checkpoint is an external model that cannot run deep research, as the composer
+ * rules it. An unresolved provider is left alone: refusing would drop a Codex preference. */
function externalCheckpointRefusesDeepResearch(
checkpoint: string | null | undefined,
): boolean {
@@ -3595,11 +3148,8 @@ function externalCheckpointRefusesDeepResearch(
return provider != null && provider.providerType !== "openai_codex";
}
-/**
- * Kimi's $web_search builtin requires thinking disabled, so the composer keeps the two
- * pills mutually exclusive. A restore has to do the same, or a chat stored under another
- * provider sends a combination Kimi rejects.
- */
+/** Kimi's $web_search requires thinking disabled and the composer keeps the pills exclusive,
+ * so a restore must too, or a chat stored elsewhere sends a combination Kimi rejects. */
function isKimiCheckpoint(checkpoint: string | null | undefined): boolean {
const parsed = parseExternalModelId(checkpoint);
if (!parsed) return false;
@@ -3623,17 +3173,15 @@ function getHydratedSettingsState(
checkpoint,
);
modelLoadedBeforeHydration = null;
- // The toggle as it will read once this response lands, under the same fence
- // the scalar loop below applies.
+ // The toggle as it will read once this response lands, under the same fence the scalar loop below applies.
const remembersPerModel =
settings.rememberParamsPerModel !== undefined &&
scalarSettingMutationVersions.rememberParamsPerModel ===
versions.scalarSettings.rememberParamsPerModel
? settings.rememberParamsPerModel
: state.rememberParamsPerModel;
- // A model loaded mid-flight has no entry to restore its defaults from, so the
- // global set would overwrite them with the last model's. Only while the memory
- // is on: with it off that global set IS this model's settings.
+ // A model loaded mid-flight has no entry to restore defaults from, so the global set would
+ // overwrite them with the last model's. Only while the memory is on.
const keepModelDefaults =
remembersPerModel &&
loadedBeforeHydration &&
@@ -3641,11 +3189,8 @@ function getHydratedSettingsState(
const params = { ...state.params };
for (const key of PERSISTED_INFERENCE_PARAM_KEYS) {
const value = settings.inferenceParams?.[key];
- // A slider moved before this response landed is held for the open chat, like a pill
- // clicked in the same window. The edit wins in the store, but it is the chat's, so
- // keep the server's value for the restore that runs when the pairing window closes:
- // without it the default falls back to this browser's pre-hydration copy and is
- // pinned onto the next snapshot-less chat. The edit already moved the version below.
+ // A slider moved before this response landed is held for the open chat. The edit wins in the
+ // store but belongs to the chat, so keep the server's value for the restore.
if (value !== undefined && isHeldThreadScopedField(key)) {
hydratedDefaultsByHeldField.set(key, value);
continue;
@@ -3653,8 +3198,8 @@ function getHydratedSettingsState(
if (
value !== undefined &&
!keepModelDefaults &&
- // The context belongs to the load, not to the previous model's global set,
- // and no entry carries one for the replay below to put back.
+ // The context belongs to the load, not the previous model's global set, and no entry carries
+ // one for the replay below.
!(loadedBeforeHydration && key === "maxSeqLength") &&
inferenceParamMutationVersions[key] === versions.inferenceParams[key]
) {
@@ -3667,9 +3212,7 @@ function getHydratedSettingsState(
for (const [modelId, entry] of Object.entries(
settings.inferenceParamsByModel,
)) {
- // Stored as written: a gap is a key this model never pinned, and there is
- // no honest value to invent. The replay lays the entry over what a load
- // just published, which is where a gap belongs.
+ // Stored as written: a gap is a key this model never pinned, and there is no honest value to invent.
hydrated[modelId] = entry;
}
for (const modelId of locallyRememberedModels) {
@@ -3678,8 +3221,8 @@ function getHydratedSettingsState(
hydrated[modelId] = local;
}
}
- // The entry arriving for this model predates the fenced edit, so lay the
- // edit over it or the next defaults update replays the stale one.
+ // The entry arriving for this model predates the fenced edit, so lay the edit over it or the
+ // next defaults update replays the stale one.
if (checkpoint) {
const edited = pickLocallyEditedParams(params, versions);
if (hasKeys(edited)) {
@@ -3688,9 +3231,8 @@ function getHydratedSettingsState(
}
nextState.paramsByModel = hydrated;
} else if (checkpoint) {
- // No map in the response: an install upgraded from before this feature. With
- // no entry the next defaults update has nothing to replay and puts the
- // recommendation back over the fenced edit.
+ // No map in the response: an install upgraded from before this feature. With no entry the
+ // next defaults update puts the recommendation back over the fenced edit.
const edited = pickLocallyEditedParams(params, versions);
if (hasKeys(edited)) {
nextState.paramsByModel = {
@@ -3699,9 +3241,8 @@ function getHydratedSettingsState(
};
}
}
- // A model stepped off before this response landed could not be filed then, and
- // the global set it ran with is only now known. Without this it has no entry,
- // so switching back inherits whatever replaced it.
+ // A model stepped off before this response landed could not be filed then, and only now is
+ // the global set it ran with known; without this, switching back inherits a stranger.
const left = modelLeftBeforeHydration;
modelLeftBeforeHydration = null;
const byModel = nextState.paramsByModel ?? state.paramsByModel;
@@ -3722,10 +3263,8 @@ function getHydratedSettingsState(
nextState.paramsByModel = { ...byModel, [left]: inherited };
}
}
- // Under the same fence as the scalar loop below, and for the same reason: a click
- // made while this response was out is the newer answer. Recording the stored value
- // over it would leave the switch visibly on -- the fence keeps the store field --
- // while the next model load quietly resolved to the value the user just replaced.
+ // Same fence as the scalar loop: a click made while this response was out is the newer
+ // answer, and recording the stored value over it leaves the switch visibly wrong.
if (
settings.preserveThinking !== undefined &&
scalarSettingMutationVersions.preserveThinking ===
@@ -3735,25 +3274,21 @@ function getHydratedSettingsState(
}
for (const key of SCALAR_SETTING_KEYS) {
const value = settings[key];
- // Full access is session-only, so a stored level must not silently drop the
- // sandbox bypass the user accepted a warning for, and Full access never
- // confirms a tool call whatever the saved toggle says.
+ // Full access is session-only, so a stored level must not silently drop the sandbox bypass
+ // the user accepted a warning for.
if (
state.permissionMode === "full" &&
(key === "permissionMode" || key === "confirmToolCalls")
) {
continue;
}
- // Both describe the running model through a loaded* shadow this loop cannot
- // set, so skip them while a shadow owns them. With none resident the store
- // field is what the next load reads and then persists back, so the server's
- // preference has to land here or the load overwrites it with a default.
+ // Both describe the running model through a loaded* shadow this loop cannot set, so skip
+ // them while a shadow owns them. With none resident the store field is what a load reads.
if (loadShadowOwnsMirroredSetting(key, state)) {
continue;
}
- // A load sets this from the model's own capability, and only a load sets
- // reasoningAlwaysOn, so a stored false would ask a model that cannot stop
- // thinking to stop thinking.
+ // A load sets this from the model's own capability, and only a load sets reasoningAlwaysOn,
+ // so a stored false would ask a model that cannot stop thinking to stop.
if (
key === "reasoningEnabled" &&
value === false &&
@@ -3781,14 +3316,11 @@ function getHydratedSettingsState(
) {
continue;
}
- // A click made before this response landed is held for the open chat rather than
- // written globally, so it advances no mutation version and the server's value would
- // silently replace it. The user is looking at their click; it wins.
+ // A click made before this response landed is held for the open chat rather than written
+ // globally, so it advances no mutation version and the server's value would replace it.
if (isHeldThreadScopedField(key)) {
- // The click wins in the STORE, but it is the chat's, not the installation's. When
- // the window closes the default has to go back to a value, and the one captured
- // before the window is this browser's pre-hydration copy. Keep the server's here
- // so the restore has the authoritative value to go back to.
+ // The click wins in the STORE but belongs to the chat: when the window closes the default
+ // has to go back to something, so keep the server's authoritative value here.
if (
value !== undefined &&
scalarSettingMutationVersions[key] === versions.scalarSettings[key]
@@ -3804,9 +3336,8 @@ function getHydratedSettingsState(
(nextState as Record<ScalarSettingKey, unknown>)[key] = value;
}
}
- // The model already selected when this lands never crossed a checkpoint
- // transition, so nothing replayed its memory and it would run on the global
- // set, which belongs to whichever model was used last.
+ // The model already selected when this lands never crossed a checkpoint transition, so
+ // nothing replayed its memory and it would run on the last model's global set.
const remembered = (nextState.paramsByModel ?? state.paramsByModel)[
params.checkpoint
];
@@ -3814,8 +3345,7 @@ function getHydratedSettingsState(
(nextState.rememberParamsPerModel ?? state.rememberParamsPerModel) &&
remembered
) {
- // Same fence as the global set above: a key the user moved mid-flight is
- // their edit. REMEMBERED, not PERSISTED, as in getReplayedParams: a
+ // Same fence as the global set. REMEMBERED, not PERSISTED, as in getReplayedParams: a
// maxSeqLength the row carries must not replace the loaded context.
const replayed = { ...params };
for (const key of REMEMBERED_INFERENCE_PARAM_KEYS) {
@@ -3830,8 +3360,8 @@ function getHydratedSettingsState(
// The same cap the load and status replays apply.
nextState.params = replayed;
}
- // Outside the replay: an install with only a global set has no entry, and the
- // budget restored from it does not fit the load either.
+ // Outside the replay: an install with only a global set has no entry, and the budget
+ // restored from it does not fit the load either.
const capped = nextState.params ?? params;
// loadedContextLength describes whatever is resident, which an external pick
// leaves loaded, so it is not this checkpoint's context to clamp against.
@@ -4380,9 +3910,8 @@ function scheduleLegacyQwenDefaultsRetry(
export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
settingsHydrated: false,
threadScopedSettingsPending: false,
- // Hydrate the last external checkpoint so the external picker survives a
- // refresh. Local checkpoints are re-derived from the backend in
- // useChatModelRuntime and intentionally NOT persisted here.
+ // Hydrate the last external checkpoint so the picker survives a refresh; local ids are
+ // re-derived from the backend and deliberately not persisted.
params: (() => {
const persistedExternal = loadLastExternalCheckpoint();
unownedCheckpointBeforeHydration = persistedExternal;
@@ -4448,13 +3977,11 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
),
searchImages: loadBool(CHAT_SEARCH_IMAGES_KEY, false),
mcpEnabledForChat: loadBool(CHAT_MCP_ENABLED_KEY, false),
- // Mirrors permissionMode (gate requested for ask/auto) so both controls
- // agree on load.
+ // Mirrors permissionMode (gate requested for ask/auto) so both controls agree on load.
confirmToolCalls:
INITIAL_PERMISSION_MODE === "ask" || INITIAL_PERMISSION_MODE === "auto",
- // Never restore Bypass Permissions from storage: it disables the sandbox and
- // the confirmation gate, so it must be re-enabled (through the warning
- // dialog) each session rather than silently reactivating on reload.
+ // Never restore Bypass Permissions from storage: it disables the sandbox and the
+ // confirmation gate, so it needs the warning dialog each session.
bypassPermissions: false,
permissionMode: INITIAL_PERMISSION_MODE,
bypassConfirmOpen: false,
@@ -4744,14 +4271,13 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
replayUnconfirmedThreadSettings();
}
} catch {
- // Hydrate failed: treat as hydrated-with-defaults so future setParams
- // calls reach saveSettingsPatch (which toasts on real network failure).
+ // Hydrate failed: treat as hydrated-with-defaults so later setParams calls reach
+ // saveSettingsPatch, which toasts on real network failure.
warnSettingsPersistenceFailure();
mirroredSettingsHydrated = true;
flushPreHydrationSettings();
- // Independent of this endpoint: the tab-close snapshots waiting in storage are
- // rows' own settings, and leaving them unsent because /api/chat/settings was
- // briefly unavailable strands the last session's edit for the whole of this one.
+ // Independent of this endpoint: the tab-close snapshots are rows' own settings, and leaving
+ // them unsent because /api/chat/settings blipped strands the last session's edit.
replayUnconfirmedThreadSettings();
set({ settingsHydrated: true });
} finally {
@@ -4790,21 +4316,17 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
set({ modelRequiresTrustRemoteCode }),
setParams: (params, options) => {
set((state) => {
- // Mirror setCheckpoint: the local load path can mutate params.checkpoint
- // via setParams() before setCheckpoint runs, leaving stale per-turn
- // counters under the new checkpoint.
+ // Mirror setCheckpoint: the local load path can move params.checkpoint via setParams()
+ // first, leaving stale per-turn counters under the new checkpoint.
const checkpointChanged = state.params.checkpoint !== params.checkpoint;
const fromModelDefaults = options?.fromModelDefaults === true;
// Remember what the outgoing model was running with before replacing it.
const outgoing = checkpointChanged
? rememberOutgoingModel(state, state.params)
: null;
- // An interactive local load lands here with the destination checkpoint and
- // the backend's recommended params, and only reaches setCheckpoint later,
- // once params.checkpoint already matches. Replay here or that switch, the
- // common one, never restores the model's own settings. fromModelDefaults
- // marks the updates that re-apply model defaults after a load or a status
- // poll: they overwrite remembered values, so memory goes back over them.
+ // An interactive local load arrives with the destination checkpoint and recommended params,
+ // reaching setCheckpoint only later, so replay here or the switch never restores the
+ // model's own settings. fromModelDefaults marks the updates memory goes back over.
noteLoadedContext(params.checkpoint, options?.maxTokensCap);
const replayed = checkpointChanged || fromModelDefaults;
const nextParams = getReplayedParams(
@@ -4815,14 +4337,13 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
replayed,
options?.maxTokensCap,
);
- // A chat outranks both the model's memory and its defaults, so the sampling it
- // pinned goes back on top of the replay. Live store only: persistence is decided
- // from nextParams below, so a pinning chat does not withhold the model's value.
+ // A chat outranks both the model's memory and its defaults, so its pinned sampling goes back
+ // on top of the replay. Live store only; persistence is decided from nextParams.
const effective = replayed
? restoreThreadScopedParams(nextParams)
: nextParams;
- // A user edit fences the keys it moved against a hydration response still
- // in flight; only the HTTP write is gated on settingsHydrated.
+ // A user edit fences the keys it moved against a hydration response in flight; only the HTTP
+ // write is gated on settingsHydrated.
const changedParams = getChangedInferenceParams(
nextParams,
state.params,
@@ -4835,21 +4356,15 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
);
const persistingGlobally =
options?.persist !== false && state.settingsHydrated;
- // A sampling key moved with a chat open belongs to that chat, so it reaches neither
- // the installation defaults nor this model's memory, both shared with every other
- // chat. What is left over is still the installation's.
- //
- // Capture is NOT gated on hydration, unlike the global write: the composer and the
- // settings sheet are live while the initial /api/chat/settings request is still out,
- // and this chat's pairing has already begun. Leaving such an edit uncaptured let the
- // arriving snapshot apply over it and let the pairing capture take it for an
- // installation default, pinning it onto the next snapshot-less chat.
+ // A sampling key moved with a chat open belongs to that chat, so it reaches neither the
+ // defaults nor the model's memory. NOT gated on hydration, unlike the global write: the
+ // composer is live while the initial request is out, and an uncaptured edit leaked.
const sharedParams =
options?.persist !== false
? withoutCapturedThreadEdits(changedParams, fromModelDefaults)
: changedParams;
- // An edit belongs to the model the params now describe, so a call that moves
- // checkpoint and sliders at once files them under the destination.
+ // An edit belongs to the model the params now describe, so a call moving checkpoint and
+ // sliders at once files them under the destination.
const paramsByModel = getParamsByModelAfterEdit(
state,
outgoing,
@@ -4858,16 +4373,15 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
options?.persist !== false && !fromModelDefaults,
);
if (persistingGlobally) {
- // A switch replays the destination's entry over the params, so writing
- // it back says nothing new and, merged per key on the server, would put
- // this browser's copy over one another tab has since changed.
+ // A switch replays the destination's entry over the params, so writing it back says nothing
+ // new and, merged per key, would overwrite another tab's copy.
persistParamEdit(
sharedParams,
checkpointChanged ? null : paramsByModel,
nextParams.checkpoint,
);
- // Level with what was just written, or a chat opened later in the same
- // session falls back to the sampling from before this model loaded.
+ // Level with what was just written, or a chat opened later this session falls back to the
+ // sampling from before this model loaded.
noteThreadScopedDefaults(sharedParams);
} else if (fromModelDefaults && !state.settingsHydrated) {
noteModelDefaultsBeforeHydration(
@@ -4944,8 +4458,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
if (options?.owner) {
nextOwner[threadId] = [...owners, { owner: options.owner, local }];
}
- // Any local owner keeps the key counted by the model-swap gate, so an external run
- // joining a shared key must not clear a sibling's flag.
+ // Any local owner keeps the key counted by the model-swap gate, so an external run on a
+ // shared key must not clear a sibling's flag.
if (local) {
nextLocal[threadId] = true;
} else if (!owners.some((o) => o.local)) {
@@ -4955,11 +4469,9 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
const remaining = options?.owner
? owners.filter((o) => o.owner !== options.owner)
: [];
- // An owner missing from the list was already cleared, or the key belongs to siblings
- // only: either way this run must change nothing.
+ // An owner missing from the list was already cleared, or the key belongs to siblings only.
if (options?.owner && remaining.length === owners.length) return state;
- // An ownerless clear predates per-run tracking, so it must not speak for runs that
- // own the key: leave them to clear themselves.
+ // An ownerless clear predates per-run tracking, so it must not speak for runs that own the key.
if (!options?.owner && owners.length > 0) return state;
if (remaining.length > 0) {
nextOwner[threadId] = remaining;
@@ -4984,12 +4496,11 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
set((state) => {
const key = "__default";
if (!threadId || threadId === key) return state;
- // Two first turns can share "__default", and nothing links a run there to the thread being
- // persisted. Moving the arrays wholesale handed this thread the sibling's owner and stop
- // handle too, so stopping one aborted both. Adopt only when the key holds a single run.
+ // Two first turns can share "__default" with nothing linking a run to the thread being
+ // persisted, so moving the arrays wholesale handed over a sibling's stop handle. Adopt
+ // only when the key holds a single run.
if ((state.runOwnerByThreadId[key]?.length ?? 0) > 1) return state;
- // Only the transient run maps move. Anything already filed under the real id wins,
- // since that is a later, better-identified run.
+ // Only the transient run maps move; anything filed under the real id is better identified.
const moved: Partial<ChatRuntimeStore> = {};
const move = <T,>(
map: Record<string, T>,
@@ -5040,8 +4551,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
next[threadId] = [...(state.serverCancelByThreadId[threadId] ?? []), cancel];
return { serverCancelByThreadId: next };
}),
- // `cancel` narrows removal to the run that registered it: unresolved thread ids share the
- // "__default" key, so a blind delete would drop a live sibling.
+ // `cancel` narrows removal to the run that registered it: unresolved ids share "__default",
+ // so a blind delete would drop a live sibling.
clearThreadServerCancel: (threadId, cancel) =>
set((state) => {
const current = state.serverCancelByThreadId[threadId];
@@ -5068,22 +4579,18 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
setCheckpoint: (modelId, ggufVariant, options) => {
let scheduleQwenMigration = false;
set((state) => {
- // Persist external selections so they survive a refresh. Local ids are
- // NOT persisted -- they're re-derived from the backend on mount, and a
- // stale persisted local id would race the freshly-loaded model. See
+ // Persist external selections so they survive a refresh. Local ids are re-derived on mount,
+ // and a stale persisted one would race the freshly-loaded model. See
// LAST_EXTERNAL_CHECKPOINT_KEY notes.
saveLastExternalCheckpoint(isExternalModelId(modelId) ? modelId : null);
- // Only disarm research for a connection that cannot drive it. Gating on the id
- // prefix alone silently switched it off for capable providers too, and saveBool
- // now reaches the backend, so that would write the preference off for every
- // browser on the install. Hoisted because all three writes below share it.
+ // Only disarm research for a connection that cannot drive it: the id prefix alone switched
+ // it off for capable providers, and saveBool would write that off for every browser.
const clampsDeepResearch =
isExternalModelId(modelId) && !externalModelSupportsStudioTools(modelId);
if (clampsDeepResearch) {
saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, false);
}
- // Clear stale per-turn usage on model change; the relaxed external-provider
- // render gate would otherwise show old counters until the next completion.
+ // Clear stale per-turn usage on model change, or the relaxed external render gate shows it.
const checkpointChanged = state.params.checkpoint !== modelId;
// An interactive pick during hydration has no adoption signal either, so
// the previous session's global does not become this model's.
@@ -5106,9 +4613,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
checkpointChanged,
options?.maxTokensCap,
);
- // Clamp maxTokens to the new model's cap when switching into an external
- // model so a value carried over from a local session doesn't exceed the
- // slider's max.
+ // Clamp maxTokens to the new model's cap when switching into an external model, so a value
+ // carried over from a local session cannot exceed the slider's max.
let nextMaxTokens = baseParams.maxTokens;
if (checkpointChanged && isExternalModelId(modelId)) {
const parsed = parseExternalModelId(modelId);
@@ -5117,9 +4623,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
.getState()
.providers.find((p) => p.id === parsed.providerId)
: null;
- // Only when the connection is known. A checkpoint restored before the
- // provider store hydrates would otherwise read the 32,768 fallback and lower
- // a value nothing puts back. No provider means unknown, not 32,768.
+ // Only when the connection is known: a checkpoint restored before the provider store hydrates
+ // reads the 32,768 fallback and lowers a value nothing puts back.
if (provider) {
const cap = getExternalMaxOutputTokens(
provider.providerType,
@@ -5155,10 +4660,10 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
checkpoint: modelId,
maxTokens: nextMaxTokens,
};
- // The chat outranks the model it switches to, so its pinned sampling and prompt go
- // back over the replay; an external switch has no load after it to do that. Live
- // store only: getReplayStatePatch still persists from the unrestored object, so the
- // model's own values reach the installation defaults.
+ // The chat outranks the model it switches to, so its pinned sampling and prompt go back over
+ // the replay; an external switch has no load to do it. Live store only.
+ // Live store only: getReplayStatePatch still persists from the unrestored object, so the model's
+ // own values reach the installation defaults.
const restoredParams = checkpointChanged
? restoreThreadScopedParams(nextParams)
: nextParams;
@@ -5169,10 +4674,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
...(queuedSettingsChanged
? { queuedSettingsEpoch: state.queuedSettingsEpoch + 1 }
: {}),
- // Provenance and the spec-fallback reason both describe the model
- // being replaced, so they go together on a real change. Dropping only
- // one leaves the settings sheet pairing a stale reason with the wrong
- // recovery text. The load or status response reseeds both.
+ // Provenance and the spec-fallback reason both describe the model being replaced, so they go
+ // together; dropping one pairs a stale reason with the wrong recovery text.
...(checkpointChanged
? {
contextUsage: null,
@@ -5183,8 +4686,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
specDrafterKind: null,
}
: {}),
- // Switching to a connection whose provider cannot run Unsloth's tool
- // loop disables Deep Research; a capable one keeps the user's choice.
+ // Switching to a provider that cannot run Unsloth's tool loop disables Deep Research; a
+ // capable one keeps the user's choice.
...(clampsDeepResearch ? { deepResearchEnabled: false } : {}),
};
});
@@ -5211,45 +4714,41 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
})),
applyThreadScopedSettings: (threadId, settings) =>
set((state) => {
- // the pending write belongs to the outgoing thread, so it goes out before the swap.
+ // The pending write belongs to the outgoing thread, so it goes out before the swap.
flushThreadScopedSettingsWrite();
- // edits made while this chat's snapshot was in flight: keep them and store them on the
- // chat. Anything else and the read would silently undo a click the user already saw.
+ // Edits made while this chat's snapshot was in flight: keep them and store them on the chat,
+ // or the read would silently undo a click the user already saw.
const heldFields = new Set<string>();
if (threadId !== null && threadId === pendingPairingThreadId) {
for (const edit of heldThreadScopedEdits) heldFields.add(edit.field);
heldThreadScopedEdits = [];
pendingPairingThreadId = null;
- // The window is over, so the next visit to this chat samples afresh rather than
- // reusing what the installation defaults were the last time round.
+ // The window is over, so the next visit samples afresh rather than reusing this round's.
pairingWindowDefaultsThreadId = null;
// Its snapshot is now the one in the store, so anything waiting on it can go.
closeThreadScopedPairingGate(threadId);
} else if (
- // A drop to the defaults for the chat that is still open and still waiting on its
- // read keeps holding: those edits are that chat's, and releasing them here is the
- // leak this whole path exists to prevent.
+ // A drop to the defaults for a chat still open and still waiting on its read keeps holding:
+ // those edits are that chat's, and releasing them here is the leak this path prevents.
threadId !== null ||
pendingPairingThreadId === null ||
pendingPairingThreadId !== state.activeThreadId
) {
releaseHeldThreadScopedEdits();
}
- // Set from here rather than trusting the calls above: this updater's own return
- // value is merged last, so a `return state` would put the old flag back.
+ // Set from here rather than trusting the calls above: this updater's return value merges
+ // last, so a `return state` would put the old flag back.
const pending = pendingPairingThreadId !== null;
- // nothing was overridden while unpaired, so there is nothing to restore.
+ // Nothing was overridden while unpaired, so there is nothing to restore.
if (threadScopedSettingsThreadId === null && threadId === null) {
return state.threadScopedSettingsPending === pending
? state
: { ...state, threadScopedSettingsPending: pending };
}
if (threadScopedSettingsThreadId === null) {
- // A held edit is already in the store but belongs to its chat, not to the
- // installation. Capturing it here would promote it to the default that every
- // snapshot-less chat follows, so take the value from before the window opened.
- // Deleting the key instead leaves it with no fallback at all, and the edited
- // value then stays live into the next chat, which is the same leak.
+ // A held edit is in the store but belongs to its chat, so capturing it here would promote it
+ // to the default every snapshot-less chat follows; take the pre-window value. Deleting the
+ // key leaves no fallback and leaks the edited value into the next chat.
const captured = readThreadScopedSettings(state) as Record<
string,
unknown
@@ -5257,9 +4756,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
const beforeWindow = (pairingWindowDefaults ??
globalThreadScopedDefaults) as Record<string, unknown> | null;
for (const field of heldFields) {
- // The server answered for this field while the window was open, and hydration
- // had to skip it. That value is the installation's; the pre-window copy is
- // only what this browser had cached before the answer arrived.
+ // The server answered for this field while the window was open and hydration had to skip it:
+ // that value is the installation's, the pre-window copy only this browser's cache.
if (hydratedDefaultsByHeldField.has(field)) {
captured[field] = hydratedDefaultsByHeldField.get(field);
} else if (beforeWindow && field in beforeWindow) {
@@ -5273,8 +4771,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
}
threadScopedSettingsThreadId = threadId;
explicitlyEditedThreadFields.clear();
- // The constraint belongs to the chat it was applied in, and this chat's own
- // provider effects will say so again if it still holds here.
+ // The constraint belongs to the chat it was applied in, and this chat's own provider effects
+ // will say so again if it still holds.
constraintSuppressedThreadFields.clear();
const stored = hasThreadScopedSettings(settings)
? (settings as ThreadScopedSettings)
@@ -5285,15 +4783,13 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
const applied: Record<string, unknown> = {};
const paramsPatch: Record<string, unknown> = {};
for (const key of THREAD_SCOPED_SETTING_KEYS) {
- // the user set this one while the read was in flight, so it wins over what came back.
+ // The user set this one while the read was in flight, so it wins over what came back.
if (heldFields.has(key)) {
applied[key] = readThreadScopedValue(state, key);
continue;
}
- // full access was accepted through a warning dialog: a switch must not drop it.
- // The chat is still pinned with the level underneath it, or a chat first opened
- // under full access would store no level at all and follow the installation one
- // forever after, which is the opposite of what pinning on open is for.
+ // Full access was accepted through a warning dialog, so a switch must not drop it. The chat
+ // is still pinned with the level underneath, or it would store no level at all.
if (key === "permissionMode" && state.permissionMode === "full") {
const underneath =
stored?.permissionMode ??
@@ -5302,10 +4798,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
if (underneath !== "full") applied[key] = underneath;
continue;
}
- // setCheckpoint clears deep research for external models in the store only, so a
- // stored true would come back and fail every send in that chat. openai_codex is the
- // exception the composer already makes, so use the same predicate rather than
- // refusing every external checkpoint.
+ // setCheckpoint clears deep research for external models in the store only, so a stored true
+ // comes back and fails every send. openai_codex is the composer's own exception.
if (
key === "deepResearchEnabled" &&
(externalCheckpointRefusesDeepResearch(state.params.checkpoint) ||
@@ -5313,7 +4807,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
) {
continue;
}
- // a key the snapshot omits falls back to the defaults, not to the outgoing chat's value.
+ // A key the snapshot omits falls back to the defaults, not to the outgoing chat's value.
const value = firstSetThreadScopedValue(
stored?.[key],
globalThreadScopedDefaults?.[key],
@@ -5333,10 +4827,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
if (hasKeys(paramsPatch)) {
nextState.params = { ...state.params, ...paramsPatch };
}
- // Search and Thinking are mutually exclusive on Kimi, and the model-selection effect
- // that enforces it does not rerun on a thread switch. Restoring both, which a chat
- // stored under another provider can hold, would send an unsupported combination, so
- // the restore drops thinking exactly as clicking the Search pill does.
+ // Search and Thinking are exclusive on Kimi and the enforcing effect does not rerun on a
+ // thread switch, so the restore drops thinking as clicking the Search pill does.
if (
isKimiCheckpoint(state.params.checkpoint) &&
(applied.toolsEnabled ?? state.toolsEnabled) === true &&
@@ -5345,8 +4837,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
applied.reasoningEnabled = false;
if (state.reasoningEnabled !== false) target.reasoningEnabled = false;
}
- // pin what the chat shows now, or changing the defaults later would rewrite its modes.
- // A chat that already had a snapshot only needs a write if it is carrying a held edit.
+ // Pin what the chat shows now, or changing the defaults later would rewrite its modes. A
+ // chat that already had a snapshot only needs a write if it carries a held edit.
if (threadId !== null && (stored === null || heldFields.size > 0)) {
scheduleThreadScopedSettingsWrite(
threadId,
@@ -5381,15 +4873,12 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
setSettingsPanelOpen: (settingsPanelOpen) => set({ settingsPanelOpen }),
setEditingMessageId: (id) => set({ editingMessageId: id }),
clearCheckpoint: () => {
- // Mirror setCheckpoint's persistence: dropping the checkpoint must also
- // clear any stored external selection so the next refresh doesn't snap
- // back to a model the user intentionally cleared.
+ // Mirror setCheckpoint's persistence: dropping the checkpoint must also clear any stored external selection.
saveLastExternalCheckpoint(null);
saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, false);
return set((state) => ({
queuedSettingsEpoch: state.queuedSettingsEpoch + 1,
- // An unload leaves the model the same way a switch does, so record what it
- // was running with.
+ // An unload leaves the model the same way a switch does, so record what it was running with.
...(() => {
const outgoing = rememberOutgoingModel(state, state.params);
return outgoing ? { paramsByModel: outgoing } : {};
@@ -5399,8 +4888,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
checkpoint: "",
},
activeGgufVariant: null,
- // Nothing is picked, so there is nothing for residency to describe. Back
- // to unknown rather than null: null would be read as "was evicted".
+ // Nothing is picked, so residency has nothing to describe. Unknown, not null: null reads as
+ // "was evicted".
residentCheckpoint: undefined,
activeModelIsLocal: false,
activeLoadId: null,
@@ -5579,7 +5068,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
setDeepResearchEnabled: (deepResearchEnabled) =>
set((state) => {
saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, deepResearchEnabled);
- // the level this chat carries, or the global one when it carries none.
+ // The level this chat carries, or the global one when it carries none.
const permissionMode =
threadScopedOverride("permissionMode") ?? loadPermissionMode();
if (deepResearchEnabled) {
@@ -5681,9 +5170,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
setConfirmToolCalls: (confirmToolCalls) =>
set((state) => {
saveBool(CHAT_CONFIRM_TOOL_CALLS_KEY, confirmToolCalls);
- // The legacy toggle is a view over the permission level: on -> "ask",
- // off -> "off" (no prompts). While "full" is active the level is left
- // alone (the toggle is disabled in the UI anyway).
+ // The legacy toggle is a view over the level: on -> "ask", off -> "off". "full" is untouched.
if (state.permissionMode === "full") {
return {
confirmToolCalls,
@@ -5700,13 +5187,11 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
}),
setPermissionMode: (permissionMode) =>
set((state) => {
- // "full" is session-only (never persisted, see init); ask/auto/off
- // persist and keep the legacy confirm toggle in sync (the gate is
- // requested for both ask and auto).
+ // "full" is session-only (see init); ask/auto/off persist and keep the legacy confirm toggle in sync.
savePermissionMode(permissionMode);
if (permissionMode === "full") {
- // Full access sends confirm_tool_calls=false; keep the store flag in
- // sync so response metadata does not report confirmations as enabled.
+ // Full access sends confirm_tool_calls=false; keep the store flag in sync so metadata does
+ // not report confirmations as enabled.
saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, false);
return {
permissionMode,
@@ -5727,13 +5212,11 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
};
}),
setBypassPermissions: (bypassPermissions) =>
- // Deliberately not persisted (see init): a reload must not silently keep
- // the sandbox/confirmation bypass active without re-accepting the warning.
- // Turning bypass off returns to the last persisted ask/auto level.
+ // Deliberately not persisted (see init): a reload must not keep the sandbox/confirmation
+ // bypass unaccepted. Turning it off returns to the last persisted level.
set((state) => {
if (bypassPermissions) {
- // Full access never prompts; mirror confirm_tool_calls=false in the
- // store so metadata does not report confirmations as enabled.
+ // Full access never prompts; mirror confirm_tool_calls=false so metadata agrees.
saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, false);
return {
bypassPermissions,
@@ -5743,7 +5226,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
queuedSettingsEpoch: state.queuedSettingsEpoch + 1,
};
}
- // back to the level this chat carries, or the global one when it carries none.
+ // Back to the level this chat carries, or the global one when it carries none.
const permissionMode =
threadScopedOverride("permissionMode") ?? loadPermissionMode();
return {
@@ -5794,7 +5277,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
}),
setRagEnabled: (ragEnabled) =>
set((state) => {
- // the only thread-scoped setting with no global slot, so no persist helper reaches it.
+ // The only thread-scoped setting with no global slot, so no persist helper reaches it.
if (ragEnabled !== state.ragEnabled) captureThreadScopedEdit("ragEnabled");
return {
ragEnabled,
@@ -5820,16 +5303,14 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
}),
adoptPendingProjectAttachmentTarget: (threadId, claim) =>
set((state) => {
- // The entry under the shared key need not be the caller's: an abandoned
- // composer has had its own dropped and the next one's is sitting there,
- // which handing it over would consume.
+ // The entry under the shared key need not be the caller's: an abandoned composer has had its
+ // own dropped and the next one's is sitting there.
if (claim !== undefined && claim !== pendingAttachmentTargetClaim) {
return state;
}
const pending =
state.projectAttachmentTargetByThread[PENDING_CHAT_ATTACHMENT_KEY];
- // A chat that already made its own choice keeps it: the pending entry
- // belongs to a chat that does not exist yet.
+ // A chat that already made its own choice keeps it: the pending entry belongs to a chat that does not exist yet.
if (
pending === undefined ||
threadId in state.projectAttachmentTargetByThread
@@ -5859,9 +5340,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
rememberParamsPerModel,
state.rememberParamsPerModel,
);
- // Turning it on adopts the settings on screen for the active model.
- // Inside a chat the sampling on screen is that chat's and both writes below are
- // shared, so the outgoing snapshot's filter takes those keys back out first.
+ // Turning it on adopts the settings on screen for the active model. Inside a chat those are
+ // the chat's, so the outgoing snapshot's filter takes those keys out first.
const snapshot = pickRememberedParams(
withoutActiveThreadParams(state, state.params),
);
@@ -5876,8 +5356,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
),
state.params.checkpoint,
);
- // Turning it on is an explicit statement about the model on screen, so the
- // whole snapshot is what it means, not a key-by-key patch.
+ // Turning it on is an explicit statement about the model on screen, so the whole snapshot is
+ // what it means, not a key-by-key patch.
if (paramsByModel && state.settingsHydrated && state.params.checkpoint) {
saveSettingsPatch({
inferenceParamsByModel: {
@@ -5885,9 +5365,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
},
});
}
- // Turning it off makes the settings on screen the one shared set. The
- // global set can still be the last model's, so write it or the next launch
- // restores that instead.
+ // Turning it off makes the settings on screen the one shared set; the global set can still
+ // be the last model's, so write it or the next launch restores that.
if (!rememberParamsPerModel && state.settingsHydrated) {
saveSettingsPatch({ inferenceParams: snapshot });
// Third write of the installation-wide sampling; level the copy as the others do.
@@ -5957,8 +5436,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
const entries = state.toolStatusByThreadId[threadId] ?? [];
const mine = entries.find((e) => e.owner === owner);
if (!status) {
- // Drop only this run's entry: a sibling behind the same key may still be running a tool,
- // and its status has to survive this clear.
+ // Drop only this run's entry: a sibling behind the same key may still be running a tool.
if (mine === undefined) return state;
const rest = entries.filter((e) => e !== mine);
if (rest.length > 0) {
@@ -5967,7 +5445,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
delete next[threadId];
}
} else {
- // Same text from the same run means the same call, so keep startedAt: only a new tool restarts it.
+ // Same text from the same run means the same call, so keep startedAt.
if (mine?.status === status) return state;
const entry = { status, startedAt: Date.now(), owner };
next[threadId] = mine
@@ -6113,9 +5591,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
queuedSettingsEpoch: state.queuedSettingsEpoch + 1,
};
}),
- // Standing preference, but persisted only on a successful load (see
- // use-chat-model-runtime), not on selection -- so an unapplied pick the user
- // resets/abandons doesn't stick to the next session.
+ // A standing preference, persisted only on a successful load (see use-chat-model-runtime),
+ // so an unapplied pick does not stick to the next session.
setGpuMemoryMode: (gpuMemoryMode) => set({ gpuMemoryMode }),
setGpuLayers: (gpuLayers) => set({ gpuLayers }),
setNCpuMoe: (nCpuMoe) => set({ nCpuMoe }),
@@ -6150,9 +5627,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
set({ pendingImageEditReference }),
clearPendingImageEditReference: () =>
set({ pendingImageEditReference: null }),
- // Write through to the visible thread's own entry, so a value restored by the history loader
- // survives a switch away and back: that loader runs once per mount and setActiveThreadId
- // reads the map, so without this the bar goes blank on return.
+ // Write through to the visible thread's own entry so a value restored by the history loader
+ // survives a switch: that loader runs once per mount and setActiveThreadId reads the map.
setContextUsage: (contextUsage) =>
set((state) => {
if (!state.activeThreadId) return { contextUsage };
diff --git a/studio/frontend/src/features/chat/stores/mcp-servers-dialog-store.ts b/studio/frontend/src/features/chat/stores/mcp-servers-dialog-store.ts
index 0e64318238a..b385231bf6a 100644
--- a/studio/frontend/src/features/chat/stores/mcp-servers-dialog-store.ts
+++ b/studio/frontend/src/features/chat/stores/mcp-servers-dialog-store.ts
@@ -3,12 +3,9 @@
import { create } from "zustand";
-/**
- * One MCP servers dialog for the chat, opened from either side: the composer
- * pill's menu and the keyboard shortcut. The pill only exists once MCP is on
- * for the chat, and it ships off, so the dialog cannot live there or the
- * shortcut would do nothing until the user found the pill by hand.
- */
+/** One MCP servers dialog for the chat, opened from either side: the composer pill's menu and the
+ * keyboard shortcut. The pill only exists once MCP is on for the chat, and it ships off, so the
+ * dialog cannot live there or the shortcut would do nothing until the user found the pill. */
interface McpServersDialogState {
open: boolean;
setOpen: (open: boolean) => void;
diff --git a/studio/frontend/src/features/chat/stores/pinned-chats-store.ts b/studio/frontend/src/features/chat/stores/pinned-chats-store.ts
index 53c198be421..d8eba0a9d50 100644
--- a/studio/frontend/src/features/chat/stores/pinned-chats-store.ts
+++ b/studio/frontend/src/features/chat/stores/pinned-chats-store.ts
@@ -4,9 +4,8 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
-// Client-side pin state for chats, keyed by stable chat id. Kept in
-// localStorage, not the chat DB. New pins are prepended so the most
-// recently pinned chat sorts first in the Pinned section.
+// Client-side pin state for chats, keyed by stable chat id. Kept in localStorage, not the chat DB.
+// New pins are prepended so the most recently pinned chat sorts first in the Pinned section.
export interface PinnedChatsState {
pinnedIds: string[];
togglePin: (id: string) => void;
diff --git a/studio/frontend/src/features/chat/stores/pinned-projects-store.ts b/studio/frontend/src/features/chat/stores/pinned-projects-store.ts
index c6dbad5c79d..518380bd52d 100644
--- a/studio/frontend/src/features/chat/stores/pinned-projects-store.ts
+++ b/studio/frontend/src/features/chat/stores/pinned-projects-store.ts
@@ -4,9 +4,9 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
-// Client-side pin state for projects, keyed by project id. Kept in
-// localStorage. Pinned projects drive the sidebar "Projects" section; new pins
-// are prepended so the most recently pinned project sorts first.
+// Client-side pin state for projects, keyed by project id, kept in localStorage. Pinned projects
+// drive the sidebar "Projects" section; new pins are prepended so the most recently pinned sorts
+// first.
export interface PinnedProjectsState {
pinnedIds: string[];
togglePin: (id: string) => void;
diff --git a/studio/frontend/src/features/chat/stores/plus-menu-prefs-store.ts b/studio/frontend/src/features/chat/stores/plus-menu-prefs-store.ts
index 960649a901c..7c679c3bbf7 100644
--- a/studio/frontend/src/features/chat/stores/plus-menu-prefs-store.ts
+++ b/studio/frontend/src/features/chat/stores/plus-menu-prefs-store.ts
@@ -4,9 +4,8 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
-// Adjustable items in the composer "+" menu. The core items (Add photos &
-// files, Web search, Code) and the "More" overflow itself are always shown and
-// are intentionally NOT represented here.
+// Adjustable items in the composer "+" menu. The core items and the "More" overflow itself are
+// always shown and are intentionally NOT represented here.
export type PlusMenuItemId =
| "chatWithFiles"
| "mcp"
@@ -17,8 +16,8 @@ export type PlusMenuItemId =
| "projects"
| "bypassPermissions";
-// Canonical order used both for the pinned items at the top level and for the
-// items that fall into the "More" overflow submenu.
+// Canonical order used both for the pinned items at the top level and for the items that fall
+// into the "More" overflow submenu.
export const PLUS_MENU_ORDER: PlusMenuItemId[] = [
"chatWithFiles",
"mcp",
@@ -30,8 +29,8 @@ export const PLUS_MENU_ORDER: PlusMenuItemId[] = [
"bypassPermissions",
];
-// Defaults reproduce the historical layout: Chat with Files, MCP and Projects
-// pinned to the top level; everything else living under "More".
+// Defaults reproduce the historical layout: Chat with Files, MCP and Projects pinned to the top
+// level; everything else under "More".
const DEFAULT_PINS: Record<PlusMenuItemId, boolean> = {
chatWithFiles: true,
mcp: true,
@@ -50,9 +49,8 @@ export interface PlusMenuPrefsState {
pins: Record<PlusMenuItemId, boolean>;
setPin: (id: PlusMenuItemId, value: boolean) => void;
togglePin: (id: PlusMenuItemId) => void;
- // Ids of saved prompts the user pinned into the "Saved prompts" submenu.
- // Kept client-side (like the menu pins above) since prompts are addressed by
- // their stable server id.
+ // Ids of saved prompts the user pinned into the "Saved prompts" submenu. Kept client-side like
+ // the menu pins above, since prompts are addressed by their stable server id.
pinnedPromptIds: string[];
togglePinnedPrompt: (id: string) => void;
}
@@ -75,8 +73,8 @@ export const usePlusMenuPrefsStore = create<PlusMenuPrefsState>()(
}),
{
name: PLUS_MENU_PINS_STORAGE_KEY,
- // Backfill any ids added in a later release so persisted state from an
- // older version still resolves every menu item.
+ // Backfill any ids added in a later release so persisted state from an older version still
+ // resolves every menu item.
merge: (persisted, current) => {
const saved = persisted as Partial<PlusMenuPrefsState> | undefined;
return {
diff --git a/studio/frontend/src/features/chat/stores/research-run-store.ts b/studio/frontend/src/features/chat/stores/research-run-store.ts
index 440f37941b9..0b5907d3e45 100644
--- a/studio/frontend/src/features/chat/stores/research-run-store.ts
+++ b/studio/frontend/src/features/chat/stores/research-run-store.ts
@@ -182,13 +182,13 @@ export function researchPhaseTitle(phase: ResearchPhase | undefined): string {
}
/** Rows for one model call are keyed by its callId so the phase bracket and any streamed
- * reasoning for that call land on the same activity. */
+ * reasoning for that call land on the same activity. */
function phaseActivityId(attempt: number, callId: string): string {
return `reasoning-${attempt}-${callId}`;
}
-/** What the run is doing right now, for the collapsed card: the live activity, qualified by
- * the latest thing it has produced so a long call still shows movement. */
+/** What the run is doing right now, for the collapsed card: the live activity, qualified by the
+ * latest thing it has produced so a long call still shows movement. */
export function runningResearchActivityTitle(
activities: ResearchActivity[] | undefined,
): string | null {
@@ -207,13 +207,13 @@ export function stepResultDetail(sourceCount: number, action?: string): string {
if (sourceCount > 0) {
return `${sourceCount} ${sourceCount === 1 ? "source" : "sources"} found`;
}
- // A fetch records an excerpt of one page and never collects sources, so a count would read
- // as a failure. A search that returns nothing is a real outcome, not a styled success.
+ // A fetch records an excerpt of one page and never collects sources, so a count would read as a
+ // failure. A search that returns nothing is a real outcome, not a styled success.
return action === "fetch" ? "Page read" : "No usable results";
}
-/** Header summary. Counts are omitted until they exist, so a run that has not searched yet
- * reads as the work it is doing rather than "0 sources · 0 actions". */
+/** Header summary. Counts are omitted until they exist, so a run that has not searched yet reads
+ * as the work it is doing rather than "0 sources, 0 actions". */
export function researchProgressSummary(
run: ResearchRun,
elapsed: string,
@@ -267,9 +267,8 @@ function reduceActivity(
): ResearchActivity[] {
const next = [...activities];
const attempt = event.data.attempt ?? 0;
- // A retry deletes the old attempt's step rows while its events survive, and
- // the stream attaches the live snapshot to replayed history, so run.steps
- // only describes its own attempt.
+ // A retry deletes the old attempt's step rows while its events survive, and the stream attaches
+ // the live snapshot to replayed history, so run.steps only describes its own attempt.
const snapshotIsSameAttempt = attempt === (event.run.retryCount ?? 0);
// runs recorded before phase events carry no phase.ended, so close their rows as before.
@@ -318,8 +317,8 @@ function reduceActivity(
return next;
}
if (existingIndex >= 0) return next;
- // phase.ended is best-effort (_note_phase swallows append failures), so a new phase also
- // closes the previous one; otherwise a dropped end leaves that row spinning all run.
+ // phase.ended is best-effort (_note_phase swallows append failures), so a new phase also closes
+ // the previous one; otherwise a dropped end leaves that row spinning all run.
const stale = findLastActivityIndex(
next,
(activity) =>
@@ -641,9 +640,8 @@ export const useResearchRunStore = create<ResearchRunState>((set) => ({
state.planReviewByRunId[run.id],
run,
);
- // Claimed means spent: a finished run is the chat's one research. A run still going
- // keeps the toggle lit (it is what is happening) and a stopped one can be re-pointed,
- // so neither takes the toggle away.
+ // Claimed means spent: a finished run is the chat's one research. A run still going keeps the
+ // toggle lit and a stopped one can be re-pointed, so neither takes the toggle away.
const claimed = shouldBecomeLatest
? run.status === "completed" || run.status === "failed"
: Boolean(state.claimedThreadIds[run.threadId]);
@@ -961,7 +959,7 @@ export function beginExternalResearchFollow(
}
/** Yield the run each time the store applies something to it, until it settles or *signal*
- * aborts. Independent of the event stream, so a slow consumer cannot stall ingestion. */
+ * aborts. Independent of the event stream, so a slow consumer cannot stall ingestion. */
export async function* watchResearchRun(
runId: string,
options: { signal?: AbortSignal } = {},
diff --git a/studio/frontend/src/features/chat/stores/sidebar-organization-keys.ts b/studio/frontend/src/features/chat/stores/sidebar-organization-keys.ts
index eb2c0b692be..8a5b4c1cfcd 100644
--- a/studio/frontend/src/features/chat/stores/sidebar-organization-keys.ts
+++ b/studio/frontend/src/features/chat/stores/sidebar-organization-keys.ts
@@ -1,13 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Storage keys for the sidebar organization store, in a module that imports
- * nothing. The store sits in an import cycle through the chat barrel, so the key
- * defined there was still in its temporal dead zone when `general-tab.tsx` read
- * it at module scope, throwing and leaving a white screen on launch. A module
- * with no imports is always evaluated first, so do not add an import here.
- */
+/** Storage keys for the sidebar organization store, in a module that imports nothing. The store sits
+ * in an import cycle through the chat barrel, so the key defined there was still in its temporal
+ * dead zone when `general-tab.tsx` read it at module scope, throwing and leaving a white screen
+ * on launch. A module with no imports is always evaluated first, so do not add an import here. */
/** Exported so the preference reset clears the same key the store writes. */
export const SIDEBAR_ORGANIZATION_STORAGE_KEY = "unsloth_sidebar_organization";
diff --git a/studio/frontend/src/features/chat/stores/sidebar-organization-store.ts b/studio/frontend/src/features/chat/stores/sidebar-organization-store.ts
index 3fe47cb81e0..48f55e8e613 100644
--- a/studio/frontend/src/features/chat/stores/sidebar-organization-store.ts
+++ b/studio/frontend/src/features/chat/stores/sidebar-organization-store.ts
@@ -9,14 +9,14 @@ export type SidebarOrganizeBy = "project" | "list";
/** How chat rows are ordered inside whichever list they land in. */
export type SidebarChatSort = "priority" | "updated" | "manual";
-// Defined in a leaf module and re-exported here so existing importers are
-// unchanged: this store is in an import cycle, so a binding defined here would
-// be readable too late. See sidebar-organization-keys.ts.
+// Defined in a leaf module and re-exported here so existing importers are unchanged: this
+// store is in an import cycle, so a binding defined here would be readable too late. See
+// sidebar-organization-keys.ts.
export { SIDEBAR_ORGANIZATION_STORAGE_KEY } from "./sidebar-organization-keys.ts";
import { SIDEBAR_ORGANIZATION_STORAGE_KEY } from "./sidebar-organization-keys.ts";
-// Manual order is per list: dragging a chat in one project must not move it in
-// another list showing the same chat. Each list gets its own key.
+// Manual order is per list: dragging a chat in one project must not move it in another list
+// showing the same chat. Each list gets its own key.
export const RECENTS_ORDER_SCOPE = "recents";
export const PINNED_ORDER_SCOPE = "pinned";
// The project folders themselves, which drag regardless of the chat sort.
@@ -29,8 +29,8 @@ export function projectOrderScope(projectId: string): string {
export interface SidebarOrganizationState {
organizeBy: SidebarOrganizeBy;
chatSort: SidebarChatSort;
- // Pinned sorts on its own. Pin order already is a manual order, so it
- // defaults to "manual" and stays put while the lists below re-sort.
+ // Pinned sorts on its own. Pin order already is a manual order, so it defaults to "manual" and
+ // stays put while the lists below re-sort.
pinnedSort: SidebarChatSort;
/** Scope key -> row ids, in the order the user dragged them into. */
manualOrder: Record<string, string[]>;
@@ -40,10 +40,8 @@ export interface SidebarOrganizationState {
setManualOrder: (scope: string, ids: string[]) => void;
}
-/**
- * Moves `draggedId` into `targetId`'s slot, keeping the rest in order. Returns
- * `ids` itself when either row is missing, so a stale drop is a no-op.
- */
+/** Moves `draggedId` into `targetId`'s slot, keeping the rest in order. Returns `ids` itself
+ * when either row is missing, so a stale drop is a no-op. */
export function reorderIds(
ids: string[],
draggedId: string,
@@ -59,11 +57,8 @@ export function reorderIds(
return next;
}
-/**
- * Whether a chat belongs in Recents. With the Projects section on, a project
- * chat lives in its folder and listing it twice is noise; with it off there are
- * no folders, so Recents is the only place it can appear.
- */
+/** Whether a chat belongs in Recents. With the Projects section on, a project chat lives in its
+ * folder and listing it twice is noise; with it off there are no folders. */
export function showsInRecents(
projectId: string | null | undefined,
organizeBy: SidebarOrganizeBy,
@@ -71,11 +66,8 @@ export function showsInRecents(
return organizeBy === "list" || !projectId;
}
-/**
- * Which edge of the target row the drop indicator belongs on, matching where
- * `reorderIds` actually lands the row: after the target when dragging down,
- * before it when dragging up.
- */
+/** Which edge of the target row the drop indicator belongs on, matching where `reorderIds`
+ * actually lands the row: after the target when dragging down, before it when dragging up. */
export function dropEdgeFor(
ids: string[],
draggedId: string,
@@ -86,10 +78,8 @@ export function dropEdgeFor(
return from !== -1 && to !== -1 && from < to ? "bottom" : "top";
}
-/**
- * Moves a row one slot up or down. The menu path to the same reorder that
- * dragging does, for touch and keyboard, which never see a `dragstart`.
- */
+/** Moves a row one slot up or down. The menu path to the same reorder that dragging does, for
+ * touch and keyboard, which never see a `dragstart`. */
export function moveIdBy(
ids: string[],
id: string,
@@ -105,11 +95,9 @@ export function moveIdBy(
return next;
}
-/**
- * Applies a saved order to `items`, leaving rows it does not mention in their
- * incoming order and on top. A row the user never dragged is new to the list,
- * so it stays where the list's own rule put it rather than sinking.
- */
+/** Applies a saved order to `items`, leaving rows it does not mention in their incoming order
+ * and on top. A row the user never dragged is new to the list, so it stays where the list's
+ * own rule put it rather than sinking. */
export function applyManualOrder<T>(
items: T[],
order: string[] | undefined,
diff --git a/studio/frontend/src/features/chat/sync-external-providers.ts b/studio/frontend/src/features/chat/sync-external-providers.ts
index 28d17c81ee3..8b25f304348 100644
--- a/studio/frontend/src/features/chat/sync-external-providers.ts
+++ b/studio/frontend/src/features/chat/sync-external-providers.ts
@@ -92,9 +92,9 @@ export function mergeLearnedModelCapabilities(
supportsStudioTools: boolean | undefined,
): Record<string, { vision?: boolean; studio_tools?: boolean }> {
const fromRegistry = registryCapabilities ?? {};
- // A plan-listed slug is learned at runtime and the registry cannot describe it, so
- // rewriting this map from the registry alone would drop it and leave the composer
- // reading "unknown" as allowed again on the next start.
+ // A plan-listed slug is learned at runtime and the registry cannot describe it, so rewriting this
+ // map from the registry alone would drop it and leave the composer reading "unknown" as allowed
+ // again on the next start.
const capabilities: Record<string, { vision?: boolean; studio_tools?: boolean }> = {};
for (const [modelId, capability] of Object.entries(stored ?? {})) {
if (modelId !== PROVIDER_CAPABILITY_WILDCARD && !(modelId in fromRegistry)) {
@@ -132,12 +132,10 @@ export function pruneProviderModelIds(
return modelIds;
}
-/** Which model ids a synced connection ends up with: server, else browser-saved, else seed.
- *
- * The saved list is pruned BEFORE the emptiness test, not after it. A browser selection
- * made up entirely of retired slugs is no selection at all: resolving to it empties the
- * picker, and the caller's backfill would send that empty list to a backend that rejects
- * it. `serverModels` and `defaultModels` arrive pruned already. */
+/** Which model ids a synced connection ends up with: server, else browser-saved, else seed. The
+ * saved list is pruned BEFORE the emptiness test: a browser selection made up entirely of
+ * retired slugs is no selection at all, and resolving to it empties the picker and sends that
+ * empty list to a backend that rejects it. `serverModels` and `defaultModels` arrive pruned. */
export function resolveSyncedModelIds(
providerType: string,
serverModels: string[],
@@ -194,9 +192,9 @@ export async function syncExternalProvidersFromBackend(
]);
for (const entry of registryRows) {
- // Self-hosted model ids are user-supplied, so there is no per-model entry to
- // key off. The registry declares studio_tools once per provider type; park
- // it under the wildcard so the per-model lookup can fall back to it.
+ // Self-hosted model ids are user-supplied, so there is no per-model entry to key off. The
+ // registry declares studio_tools once per provider type; park it under the wildcard so the
+ // per-model lookup can fall back to it.
const capabilities = mergeLearnedModelCapabilities(
getProviderModelCapabilities(entry.provider_type),
entry.model_capabilities,
@@ -204,10 +202,9 @@ export async function syncExternalProvidersFromBackend(
);
setProviderModelCapabilities(entry.provider_type, capabilities);
}
- // Writing per returned entry can only correct what came back. Capabilities are
- // persisted in localStorage and outlive the backend that wrote them, so a
- // provider the registry has stopped listing (hidden, or unknown to a rolled
- // back backend) would otherwise keep its last `studio_tools: true` forever.
+ // Writing per returned entry can only correct what came back. Capabilities are persisted in
+ // localStorage and outlive the backend that wrote them, so a provider the registry has stopped
+ // listing would otherwise keep its last `studio_tools: true` forever.
pruneProviderModelCapabilities(registryRows.map((entry) => entry.provider_type));
const configRows = await reconcileLegacyProviderKeys(loadedConfigRows, {
getLegacyKey: getExternalProviderApiKey,
@@ -287,8 +284,8 @@ export async function syncExternalProvidersFromBackend(
const synced: ExternalProviderConfig = {
id: config.id,
providerType: uiProviderType,
- // Beside the UI type, which disagrees for a legacy row saved as `openai`:
- // only the stored type decides what the backend accepts.
+ // Beside the UI type, which disagrees for a legacy row saved as `openai`: only the stored type
+ // decides what the backend accepts.
backendProviderType: config.provider_type,
name: config.display_name,
baseUrl: config.base_url ?? "",
diff --git a/studio/frontend/src/features/chat/tool-approval.ts b/studio/frontend/src/features/chat/tool-approval.ts
index b1908dacc0e..4171b16278b 100644
--- a/studio/frontend/src/features/chat/tool-approval.ts
+++ b/studio/frontend/src/features/chat/tool-approval.ts
@@ -5,10 +5,8 @@
import { useChatRuntimeStore } from "./stores/chat-runtime-store";
-/**
- * True while this card's call is parked on the Allow / Deny prompt, so it can say it is
- * waiting rather than counting up "Running". Set when the backend gates the call.
- */
+/** True while this card's call is parked on the Allow / Deny prompt, so it can say it is waiting
+ * rather than counting up "Running". Set when the backend gates the call. */
export function useToolAwaitingApproval(toolCallId?: string): boolean {
return useChatRuntimeStore(
(s) =>
diff --git a/studio/frontend/src/features/chat/tool-call-arguments.ts b/studio/frontend/src/features/chat/tool-call-arguments.ts
index e41b755a0f5..d791818c1fb 100644
--- a/studio/frontend/src/features/chat/tool-call-arguments.ts
+++ b/studio/frontend/src/features/chat/tool-call-arguments.ts
@@ -11,15 +11,11 @@ function isSingleJsonObject(text: string): boolean {
}
}
-/**
- * One slot's accumulated argument text, cut into the top-level JSON objects it
- * holds: `complete` closed, `tail` is still being written.
- *
- * A second top-level `{` means the stream reused the slot for another parallel
- * call, which is what turns two calls into `{"url":"a"}{"url":"b"}`. Text that
- * is not a run of whole objects comes back whole in `tail`, leaving streams
- * this was never meant for alone.
- */
+/** One slot's accumulated argument text, cut into the top-level JSON objects it holds:
+ * `complete` closed, `tail` still being written. A second top-level `{` means the stream
+ * reused the slot for another parallel call, which is what turns two calls into
+ * `{"url":"a"}{"url":"b"}`. Text that is not a run of whole objects comes back whole in
+ * `tail`, leaving streams this was never meant for alone. */
export function splitTopLevelJsonObjects(text: string): {
complete: string[];
tail: string;
@@ -77,13 +73,10 @@ export function splitTopLevelJsonObjects(text: string): {
};
}
-/**
- * `splitTopLevelJsonObjects` over a string that only ever grows. Rescanning per
- * fragment is O(N^2): a 20 KB argument sent a character at a time took about a
- * second and a half on the thread that paints the stream. `feed` takes the same
- * string extended, never a rewritten one, so a caller that splits a slot drops
- * its scan.
- */
+/** `splitTopLevelJsonObjects` over a string that only ever grows. Rescanning per fragment is
+ * O(N^2): a 20 KB argument sent a character at a time took about a second and a half on the
+ * thread that paints the stream. `feed` takes the same string extended, never a rewritten
+ * one, so a caller that splits a slot drops its scan. */
export function createBoundaryScan(): {
feed: (text: string) => { complete: string[]; tail: string };
} {
@@ -146,17 +139,11 @@ export function createBoundaryScan(): {
};
}
-/**
- * The `function.arguments` string to replay for a stored tool call.
- *
- * `argsText` is the text the provider streamed and is preferred so replay is
- * byte-exact. Text that does not parse would be replayed on every later request
- * in the thread, and strict chat templates reject the whole request rather than
- * one call, so it falls back to the structured args the part already carries.
- *
- * `{ _raw }` is the adapter's marker for text it could not parse, so a thread
- * carrying one replays as `{}` rather than as the blob.
- */
+/** The `function.arguments` string to replay for a stored tool call. `argsText` is what the
+ * provider streamed and is preferred so replay is byte-exact. Text that does not parse would
+ * be replayed on every later request in the thread, and strict chat templates reject the
+ * whole request rather than one call, so it falls back to the structured args. `{ _raw }` is
+ * the adapter's marker for unparsable text, so a thread carrying one replays as `{}`. */
export function toolCallReplayArguments(
argsText: string | undefined,
args: unknown,
@@ -164,9 +151,8 @@ export function toolCallReplayArguments(
if (
typeof argsText === "string" &&
argsText.length > 0 &&
- // One object, because that is what `function.arguments` is. A run of them,
- // an array, a scalar or a half-written object gets the whole request
- // rejected, not just the one call.
+ // One object, because that is what `function.arguments` is. A run of them, an array, a scalar or
+ // a half-written object gets the whole request rejected, not just the one call.
isSingleJsonObject(argsText)
) {
return argsText;
@@ -178,20 +164,17 @@ export function toolCallReplayArguments(
}
const parsed = JSON.parse(serialized) as Record<string, unknown>;
const keys = Object.keys(parsed);
- // The adapter writes `{ _raw }` holding the exact text it could not parse,
- // so a lone `_raw` whose value IS that text is the marker, and replaying it
- // would send a parameter no tool declares. `_raw` is not reserved and an
- // MCP server's schema is its own, so recognise it only when the surviving
- // text proves it: a thread stored before `argsText` was kept has nothing to
- // compare against, and guessing from the value's shape would discard the
- // argument of a tool that really takes one. Guessing buys little anyway,
- // since the wrapped form is one JSON object and raises no `Extra data`.
+ // The adapter writes `{ _raw }` holding the exact text it could not parse, so a lone `_raw`
+ // whose value IS that text is the marker, and replaying it would send a parameter no tool
+ // declares. `_raw` is not reserved and an MCP server's schema is its own, so recognise it only
+ // when the surviving text proves it: a thread stored before `argsText` was kept has nothing to
+ // compare against, and guessing from the value's shape would discard a real argument.
if (
keys.length === 1 &&
keys[0] === "_raw" &&
typeof parsed._raw === "string" &&
- // Non-empty, or the equality proves nothing: the adapter only writes the
- // marker for parsed text, so `{ _raw: "" }` is a real argument.
+ // Non-empty, or the equality proves nothing: the adapter only writes the marker for parsed text,
+ // so `{ _raw: "" }` is a real argument.
parsed._raw.length > 0 &&
parsed._raw === argsText
) {
diff --git a/studio/frontend/src/features/chat/tool-call-id.ts b/studio/frontend/src/features/chat/tool-call-id.ts
index 7028bfebfa4..ae895074f22 100644
--- a/studio/frontend/src/features/chat/tool-call-id.ts
+++ b/studio/frontend/src/features/chat/tool-call-id.ts
@@ -23,14 +23,11 @@ export interface StreamedToolCallPart {
_has_stable_id?: boolean;
}
-/**
- * The id for a card drawn from a delta the provider gave no id to: the slot's
- * own `tool_call_<index>` when free, else the lowest `tool_call_<n>` that is.
- * The backend's `_mint_streamed_card_id` walks the same deltas under the same
- * rule, so its `tool_start` reaches this card instead of opening a second one.
- * `reserved` holds ids the provider sent, so one spelled `tool_call_0` keeps
- * it. No colon: a replayed id must satisfy `^[a-zA-Z0-9_-]+$`.
- */
+/** The id for a card drawn from a delta the provider gave no id to: the slot's own
+ * `tool_call_<index>` when free, else the lowest `tool_call_<n>` that is. The backend's
+ * `_mint_streamed_card_id` walks the same deltas under the same rule, so its `tool_start`
+ * reaches this card instead of opening a second one. `reserved` holds ids the provider sent.
+ * No colon: a replayed id must satisfy `^[a-zA-Z0-9_-]+$`. */
export function mintStreamedToolCallId(
parts: StreamedToolCallPart[],
deltaIndex: number | undefined,
@@ -45,12 +42,9 @@ export function mintStreamedToolCallId(
return `tool_call_${position}`;
}
-/**
- * Let a card drawn by the deltas answer to its own id. Without it,
- * `resolveToolCallPartId` mints `<backend id>:<uuid>` when the backend first
- * names an id-less call, `tool_start` finds no card and pushes one, and the
- * turn persists two parts per call.
- */
+/** Let a card drawn by the deltas answer to its own id. Without it, `resolveToolCallPartId` mints
+ * `<backend id>:<uuid>` when the backend first names an id-less call, `tool_start` finds no card
+ * and pushes one, and the turn persists two parts per call. */
export function bindStreamedToolCallCard(
ids: Map<string, string>,
partId: string,
@@ -58,10 +52,8 @@ export function bindStreamedToolCallCard(
if (!ids.has(partId)) ids.set(partId, partId);
}
-/**
- * Newest part holding `deltaIndex`, or -1. `unownedOnly` restricts the match to
- * a slot no provider id has claimed yet.
- */
+/** Newest part holding `deltaIndex`, or -1. `unownedOnly` restricts the match to a slot no
+ * provider id has claimed yet. */
function findDeltaIndexSlot(
parts: readonly StreamedToolCallPart[],
deltaIndex: number | undefined,
@@ -80,17 +72,11 @@ function findDeltaIndexSlot(
return -1;
}
-/**
- * Index of the tool-call part a `delta.tool_calls[]` fragment continues, or -1
- * when the fragment starts a new call.
- *
- * Providers restart `tool_calls[].index` at 0 for every tool round inside one
- * assistant response, so the index slot alone cannot separate a continuation
- * from the next round's opening fragment. A fragment carrying a stable id
- * therefore matches on that id, and falls back to the index slot only while no
- * other id owns it, which covers servers that stamp the real id on a later
- * fragment. Id-less fragments continue the newest part in their slot.
- */
+/** Index of the tool-call part a `delta.tool_calls[]` fragment continues, or -1 when the fragment
+ * starts a new call. Providers restart `tool_calls[].index` at 0 for every tool round inside one
+ * response, so the index slot alone cannot separate a continuation from the next round's
+ * opening fragment. A fragment carrying a stable id matches on that id, and falls back to the
+ * index slot only while no other id owns it. Id-less fragments continue the newest part. */
export function findStreamedToolCallPartIndex(
parts: readonly StreamedToolCallPart[],
partId: string | undefined,
diff --git a/studio/frontend/src/features/chat/tool-output-scope.ts b/studio/frontend/src/features/chat/tool-output-scope.ts
index 2490b56e1eb..419ee9fca1a 100644
--- a/studio/frontend/src/features/chat/tool-output-scope.ts
+++ b/studio/frontend/src/features/chat/tool-output-scope.ts
@@ -9,52 +9,37 @@ import { createContext, useContext } from "react";
import { stripAnsi } from "../../lib/strip-ansi";
import type { ModelType } from "./types";
-/**
- * Pane scope prefix for the transient tool-output store keys.
- *
- * Local GGUF tool ids are only unique within one response ("call_0", "call_1",
- * ...), and panes stream concurrently (compare mode mounts two runtimes; the
- * main view stays CSS-hidden off-route), so a bare id would let one pane's
- * stdout bleed into another's same-id card. Derived from static props
- * (`modelType` + `pairId`) shared by writer (adapter) and reader (components)
- * via one `ChatRuntimeProvider`, so they can never disagree.
- */
+/** Pane scope prefix for the transient tool-output store keys. Local GGUF tool ids are only
+ * unique within one response ("call_0", "call_1", ...), and panes stream concurrently, so a
+ * bare id would let one pane's stdout bleed into another's same-id card. Derived from static
+ * props shared by writer and reader via one `ChatRuntimeProvider`, so they cannot disagree. */
export function toolPaneScope(modelType?: ModelType, pairId?: string): string {
return `${modelType ?? "base"}\u0000${pairId ?? ""}`;
}
-/**
- * Narrow a pane scope to one conversation: two threads in a pane can both be mid "call_0",
- * so without the thread in the key they share a store entry and swap outputs.
- */
+/** Narrow a pane scope to one conversation: two threads in a pane can both be mid "call_0", so
+ * without the thread in the key they share a store entry and swap outputs. */
export function toolThreadScope(paneScope: string, threadId?: string): string {
return `${paneScope}\u0000${threadId ?? ""}`;
}
export const ToolPaneScopeContext = createContext<string>(toolPaneScope());
-/**
- * Store-key scope for the conversation this component renders in, taken from the surrounding
- * runtime so reader and writer agree without a prop.
- *
- * `remoteId`, not `id`: the adapter gets `unstable_threadId`, which assistant-ui sources from
- * `remoteId`, and an uninitialized thread has `id` but no `remoteId`. Reading `id` split the
- * keys apart for the first turn of every New Chat, so live tool output never reached the card.
- */
+/** Store-key scope for the conversation this component renders in, taken from the surrounding
+ * runtime so reader and writer agree without a prop. `remoteId`, not `id`: the adapter gets
+ * `unstable_threadId`, which assistant-ui sources from `remoteId`, and an uninitialized
+ * thread has `id` but no `remoteId`. Reading `id` split the keys apart on every New Chat. */
export function useToolPaneScope(): string {
const paneScope = useContext(ToolPaneScopeContext);
const threadId = useAuiState(({ threadListItem }) => threadListItem.remoteId);
return toolThreadScope(paneScope, threadId);
}
-/**
- * Read a tool-output map for one call, tolerating a run that started before its thread had an id.
- *
- * The adapter captures its scope once at run start, so a first turn writes under the unresolved
- * scope for its whole life. The autosave can assign `remoteId` mid-run, which moves this
- * component's key but not the writer's, and the card went blank. Falling back to the pane-wide
- * scope keeps those entries reachable; only an unpersisted first turn can be filed there.
- */
+/** Read a tool-output map for one call, tolerating a run that started before its thread had an
+ * id. The adapter captures its scope once at run start, so a first turn writes under the
+ * unresolved scope for its whole life, and an autosave assigning `remoteId` mid-run moves
+ * this component's key but not the writer's. Falling back to the pane-wide scope keeps those
+ * entries reachable; only an unpersisted first turn can be filed there. */
/** The scope a run that started before its thread had an id writes under. */
export function useUnresolvedToolPaneScope(): string {
return toolThreadScope(useContext(ToolPaneScopeContext), undefined);
@@ -67,9 +52,8 @@ export function useToolOutputFor(
): string {
// Unconditional: hooks cannot sit behind the early return below.
const unresolvedScope = useUnresolvedToolPaneScope();
- // Only a thread mid-run can be the one that just gained its id. Local ids repeat
- // ("call_0"), so an unconditional fallback showed a live first turn's stdout in every
- // older conversation whose own entry had been cleared.
+ // Only a thread mid-run can be the one that just gained its id. Local ids repeat ("call_0"), so
+ // an unconditional fallback showed a live first turn's stdout in every older conversation.
const isRunning = useAuiState(({ thread }) => thread.isRunning);
const own = map[toolOutputKey(paneScope, toolCallId)];
if (own !== undefined) return own;
@@ -82,24 +66,15 @@ export function toolOutputKey(paneScope: string, toolCallId: string): string {
return `${paneScope}\u0000${toolCallId}`;
}
-// Footer the backend appends when it truncates a result to protect the context
-// window (see backend tools._truncate). Marks where the result stops being a
-// copy of the stream, so it distinguishes "just truncated" from "carries
-// failure/exit status the stream never produced".
+// Footer the backend appends when it truncates a result to protect the context window (see
+// backend tools._truncate). Marks where the result stops being a copy of the stream.
const TRUNCATION_FOOTER_MARKER = "\n\n... (truncated";
-/**
- * Whether the live stdout holds more real output than the model-visible
- * `result` and should be preserved for the finished card. Shared by writer
- * (retain?) and reader (display?) so they agree.
- *
- * True when the result is truncated, OR the stream is longer. Truncation can't
- * fall back to length: a truncated result may be longer by byte count once its
- * footer / `Exit code N:` / `__IMAGES__` blob is appended, yet the stream still
- * holds more stdout. Also true when a short stream is absent from the result: a
- * timed-out/cancelled tool returns only a status line, so length alone would
- * drop the partial stdout the stream captured.
- */
+/** Whether the live stdout holds more real output than the model-visible `result` and should be
+ * preserved for the finished card. Shared by writer and reader so they agree. True when the
+ * result is truncated, OR the stream is longer. Truncation cannot fall back to length: a
+ * truncated result may be longer once its footer or exit line is appended. Also true when a
+ * short stream is absent from the result, since a cancelled tool returns only a status line. */
export function shouldPreserveFullOutput(full: string, result: string): boolean {
if (!full) {
return false;
@@ -110,20 +85,17 @@ export function shouldPreserveFullOutput(full: string, result: string): boolean
if (full.length > result.length) {
return true;
}
- // Stream no longer than the result, but a timed-out/cancelled tool's status
- // line never echoes the captured stdout: preserve the stream whenever its
- // content is absent from the result (trimmed to ignore trailing-newline drift).
+ // Stream no longer than the result, but a timed-out or cancelled tool's status line never
+ // echoes the captured stdout: preserve the stream whenever its content is absent from the
+ // result, trimmed to ignore trailing-newline drift.
const core = full.trim();
return core.length > 0 && !result.includes(core);
}
-/**
- * Pick what a finished python/terminal card shows. Prefer the fuller live
- * stream over the truncated `result`, but the result can carry failure/exit
- * text that never reached stdout ("Exit code N: ...", timeouts), so show the
- * stream when the result is just a truncated prefix of it, else append the
- * result so its status survives (and the copy button copies both).
- */
+/** Pick what a finished python/terminal card shows. Prefer the fuller live stream over the
+ * truncated `result`, but the result can carry failure or exit text that never reached
+ * stdout, so show the stream when the result is just a truncated prefix of it, else append
+ * the result so its status survives. */
export function preferFullToolOutput(full: string, result: string): string {
if (!shouldPreserveFullOutput(full, result)) {
return result;
@@ -133,10 +105,9 @@ export function preferFullToolOutput(full: string, result: string): string {
if (!core || full === result || full.startsWith(core)) {
return full;
}
- // Failed executions prefix the result (not the stream) with "Exit code N:\n",
- // so `full.startsWith(core)` above misses and a plain append would duplicate
- // the stdout. Re-attach just the exit prefix (and any missing-path hint) to
- // the fuller stream so the status survives without duplicating stdout.
+ // Failed executions prefix the result, not the stream, with "Exit code N:", so
+ // `full.startsWith(core)` misses and a plain append would duplicate the stdout. Re-attach
+ // just the exit prefix to the fuller stream so the status survives.
const exitMatch = core.match(/^(Exit code -?\d+:\n)([\s\S]*)$/);
if (exitMatch && full.startsWith(exitMatch[2])) {
const hint = result.match(/\nHint:[\s\S]*$/)?.[0] ?? "";
diff --git a/studio/frontend/src/features/chat/tour/steps.tsx b/studio/frontend/src/features/chat/tour/steps.tsx
index 84e3fe59505..ad5de4d5e7a 100644
--- a/studio/frontend/src/features/chat/tour/steps.tsx
+++ b/studio/frontend/src/features/chat/tour/steps.tsx
@@ -75,8 +75,8 @@ export function buildChatTourSteps({
];
if (canCompare) {
- // Compare lives in the + menu (no sidebar button to anchor to); this step
- // enters compare on its own and explains it.
+ // Compare lives in the + menu, with no sidebar button to anchor to; this step enters compare on
+ // its own and explains it.
steps.push({
id: "compare-view",
target: "chat-compare-view",
diff --git a/studio/frontend/src/features/chat/types.ts b/studio/frontend/src/features/chat/types.ts
index 63b3aec3ccb..57824402ec9 100644
--- a/studio/frontend/src/features/chat/types.ts
+++ b/studio/frontend/src/features/chat/types.ts
@@ -38,33 +38,19 @@ export interface ThreadRecord {
archived: boolean;
createdAt: number;
updatedAt?: number;
- /**
- * OpenAI shell tool container id from a prior response. When set, the
- * next turn reuses it via `environment.type="container_reference"` so
- * the model can read files it wrote earlier; else auto-creates one.
- *
- * Containers expire after ~20 min idle; on a stale id the backend
- * emits `_toolEvent.type="container_invalidated"` and the chat-adapter
- * clears this field so the next turn falls back to auto-create.
- */
+ /** OpenAI shell tool container id from a prior response. When set, the next turn reuses it via
+ * `environment.type="container_reference"` so the model can read files it wrote earlier; else
+ * auto-creates one. Containers expire after ~20 min idle; on a stale id the backend emits
+ * `container_invalidated` and the chat-adapter clears this field. */
openaiCodeExecContainerId?: string | null;
- /**
- * Anthropic code_execution container id from a prior response. When
- * set, the next turn sends a top-level `container` on /v1/messages so
- * filesystem state (files, packages, variables) persists; else auto-
- * creates one.
- *
- * Containers expire after ~1 hour; on a stale id the backend emits
- * `_toolEvent.type="container_invalidated"` and the chat-adapter
- * clears this field so the next turn falls back to auto-create.
- */
+ /** Anthropic code_execution container id from a prior response. When set, the next turn sends a
+ * top-level `container` on /v1/messages so filesystem state persists; else auto-creates one.
+ * Containers expire after ~1 hour; on a stale id the backend emits `container_invalidated` and
+ * the chat-adapter clears this field. */
anthropicCodeExecContainerId?: string | null;
- /**
- * If this thread was created via fork-from-message, points back at
- * the source thread + branch-point msg. Null/undefined for non-fork
- * threads. Used by the sidebar "fork" badge and the parent thread's
- * "N forks" indicator on the branch-point msg.
- */
+ /** If this thread was created via fork-from-message, points back at the source thread and
+ * branch-point message. Null or undefined for non-fork threads. Used by the sidebar "fork" badge
+ * and the parent thread's "N forks" indicator. */
forkedFromThreadId?: string | null;
forkedFromMessageId?: string | null;
/** this chat's own settings, applied when it is opened; absent means the global ones. */
diff --git a/studio/frontend/src/features/chat/types/api.ts b/studio/frontend/src/features/chat/types/api.ts
index deb34e9bbc8..a1ac05fea47 100644
--- a/studio/frontend/src/features/chat/types/api.ts
+++ b/studio/frontend/src/features/chat/types/api.ts
@@ -50,10 +50,8 @@ export interface LoadModelRequest {
/** Start a fresh runtime even when the active settings already match. */
force_reload?: boolean;
- /**
- * Stop any chats still generating instead of getting a 409: a load replaces the single
- * llama-server they all decode on. Set only after the user confirms.
- */
+ /** Stop any chats still generating instead of getting a 409: a load replaces the single
+ * llama-server they all decode on. Set only after the user confirms. */
force_cancel_active?: boolean;
nativePathLease?: string | null;
hf_token: string | null;
@@ -68,63 +66,44 @@ export interface LoadModelRequest {
chat_template_override?: string | null;
cache_type_kv?: string | null;
mlx_kv_bits?: number | null;
- /**
- * Speculative decoding mode for GGUF models. Canonical values: "auto"
- * (platform-aware: DSpark or DFlash when the model ships that sidecar, else
- * MTP on MTP GGUFs, ngram-mod fallback for sub-3B), "mtp" (force draft-mtp),
- * "dspark" (force draft-dspark with a sidecar), "dflash" (force draft-dflash
- * with a sidecar), "ngram" (force ngram-mod), "mtp+ngram" (ngram-mod +
- * draft-mtp chain), "off". Legacy "default"/"draft-mtp"/"draft-dspark"/
- * "draft-dflash"/"ngram-mod"/"ngram-simple" are still accepted by the backend.
- */
+ /** Speculative decoding mode for GGUF models: "auto" (platform-aware DSpark/DFlash when the model
+ * ships that sidecar, else MTP on MTP GGUFs, ngram-mod for sub-3B), "mtp", "dspark",
+ * "dflash", "ngram", "mtp+ngram", "off". The legacy spellings are still accepted. */
speculative_type?: string | null;
- /**
- * Override --spec-draft-n-max for drafter speculative decoding. Applied only
- * when speculative_type resolves to "mtp", "mtp+ngram", "dspark" or "dflash".
- */
+ /** Override --spec-draft-n-max for drafter speculative decoding. Applied only when speculative_type
+ * resolves to "mtp", "mtp+ngram", "dspark" or "dflash". */
spec_draft_n_max?: number | null;
- /**
- * Parallel decode slots for llama-server (--parallel), 1..64. Omit/null =
- * the launch default. The VRAM fitter may launch fewer to stay on GPU.
- */
+ /** Parallel decode slots for llama-server (--parallel), 1..64. Omit/null = the launch default. The
+ * VRAM fitter may launch fewer to stay on GPU. */
n_parallel?: number | null;
/** prompt batch size (--batch-size), 1..65536; omit/null = llama.cpp default 2048, gguf only */
n_batch?: number | null;
/** prompt micro-batch size (--ubatch-size), 1..65536; omit/null = llama.cpp default 512, capped at the batch size */
n_ubatch?: number | null;
- /** weight loading mode (--load-mode): auto/none/mmap/mlock/mmap+mlock/dio.
- * Omit/null = llama.cpp's own `auto`. Settings -> Model Memory overrides it. */
+ /** Weight loading mode (--load-mode): auto/none/mmap/mlock/mmap+mlock/dio. Omit/null = llama.cpp's
+ * own `auto`. Settings -> Model Memory overrides it. */
load_mode?: string | null;
- /** KV cache dtype for the DRAFT model's context (--spec-draft-type-k/-v);
- * omit/null = llama.cpp default f16. Only reaches the command line when the
- * load attaches a separate draft model. */
+ /** KV cache dtype for the DRAFT model's context (--spec-draft-type-k/-v); omit/null = f16. Only
+ * reaches the command line when the load attaches a separate draft model. */
spec_draft_cache_type?: string | null;
/** context checkpoints per slot (--ctx-checkpoints); omit/null = default 32, 0 disables */
ctx_checkpoints?: number | null;
/** host prompt cache size in MiB (--cache-ram); omit/null = default 8192, 0 disables, -1 unlimited */
cache_ram?: number | null;
- /**
- * Pass-through llama-server args, one argv token per entry, appended after
- * Unsloth's own flags so llama.cpp's last-wins parser takes these. Flags Unsloth
- * manages are refused with a 4xx naming the flag. Omit/null inherits the stored
- * per-model value; [] launches with none. GGUF only.
- */
+ /** Pass-through llama-server args, one argv token per entry, appended after Unsloth's own flags so
+ * llama.cpp's last-wins parser takes these. Managed flags are refused with a 4xx naming the
+ * flag. Omit/null inherits the stored per-model value; [] launches with none. GGUF only. */
// biome-ignore lint/style/useNamingConvention: API schema
llama_extra_args?: string[] | null;
- /**
- * Split the model across GPUs by tensor (--split-mode tensor) instead
- * of by layer for GGUF models. Multi-GPU only; no effect on a single GPU.
- */
+ /** Split the model across GPUs by tensor (--split-mode tensor) instead of by layer for GGUF models.
+ * Multi-GPU only. */
tensor_parallel?: boolean | null;
- /**
- * Load a vision-capable GGUF without its mmproj, freeing the VRAM the
- * projector would occupy. Image input is unavailable for the session;
- * text generation is unaffected.
- */
+ /** Load a vision-capable GGUF without its mmproj, freeing the VRAM the projector would occupy.
+ * Image input is unavailable for the session; text generation is unaffected. */
disable_vision?: boolean | null;
- /** GPU memory strategy for GGUF models. "auto" (default): Unsloth selects GPUs
- * and caps context to fit VRAM. "manual": you own the offload -- gpu_layers
- * -1 (Auto) hands sizing to llama.cpp's --fit, >= 0 pins layers/n_cpu_moe. */
+ /** GPU memory strategy for GGUF models. "auto" (default): Unsloth selects GPUs and caps context to
+ * fit VRAM. "manual": you own the offload, with gpu_layers -1 handing sizing to llama.cpp's
+ * --fit and >= 0 pinning layers/n_cpu_moe. */
gpu_memory_mode?: "auto" | "manual";
/** Manual mode: layers to offload to GPU (--gpu-layers, --fit off); -1 = Auto (--fit). */
gpu_layers?: number;
@@ -148,8 +127,8 @@ export interface ValidateModelResponse {
display_name?: string | null;
is_gguf?: boolean;
is_diffusion?: boolean;
- /** The diffusion check was inconclusive, so `is_diffusion: false` above means
- * "not known to be diffusion", not "known to be ordinary". */
+ /** The diffusion check was inconclusive, so `is_diffusion: false` above means "not known to be
+ * diffusion", not "known to be ordinary". */
diffusion_unknown?: boolean;
is_lora?: boolean;
is_vision?: boolean;
@@ -158,15 +137,14 @@ export interface ValidateModelResponse {
requires_security_review?: boolean;
/** Native context length from the local GGUF header; null until downloaded. */
context_length?: number | null;
- /** Total layer count (GGUF block_count); the manual gpu-layers ceiling is
- * this + 1 (llama.cpp counts the output layer as offloadable too); null
- * until downloaded. */
+ /** Total layer count (GGUF block_count); the manual gpu-layers ceiling is this + 1, since llama.cpp
+ * counts the output layer as offloadable. Null until downloaded. */
layer_count?: number | null;
- /** MoE expert-layer count from the GGUF header (manual --n-cpu-moe ceiling);
- * 0 for dense models, null until downloaded. */
+ /** MoE expert-layer count from the GGUF header (manual --n-cpu-moe ceiling); 0 for dense models,
+ * null until downloaded. */
moe_layer_count?: number | null;
- /** Embedded GGUF chat template, returned when include_chat_template is set
- * (native lease-backed picks); null for non-GGUF, over-cap, or not read. */
+ /** Embedded GGUF chat template, returned when include_chat_template is set; null for non-GGUF,
+ * over-cap, or not read. */
chat_template?: string | null;
/** Architecture only shipped by a newer transformers; UI pauses on the upgrade dialog. */
requires_transformers_upgrade?: boolean;
@@ -187,10 +165,9 @@ export interface GgufVariantDetail {
update_available?: boolean;
/** An interrupted download: some shards are missing, so it cannot load yet. */
partial?: boolean;
- /** Variants sharing this key share one companion download footprint (text
- * encoder, VAE, tokenizer, configs). The set is not repo-wide: one repo can
- * hold GGUFs of different families, and FLUX.2-klein picks its text encoder
- * per checkpoint size. Null/absent means unknown, so the repo is one group. */
+ /** Variants sharing this key share one companion download footprint. The set is not repo-wide: one
+ * repo can hold GGUFs of different families, and FLUX.2-klein picks its text encoder per
+ * checkpoint size. Null/absent means unknown, so the repo is one group. */
dependency_key?: string | null;
}
@@ -232,9 +209,8 @@ export interface LoadModelResponse {
is_gguf?: boolean;
is_local_model?: boolean;
is_diffusion?: boolean;
- /** GPU-layer count the diffusion runner was ASKED for, when it differs from what
- * it applied: a shim without --ngl runs Auto, so gpu_layers reports -1 while
- * this carries the standing request. */
+ /** GPU-layer count the diffusion runner was ASKED for, when it differs from what it applied: a shim
+ * without --ngl runs Auto, so gpu_layers reports -1 while this carries the request. */
diffusion_requested_ngl?: number | null;
is_audio?: boolean;
audio_type?: string | null;
@@ -276,9 +252,9 @@ export interface LoadModelResponse {
spec_draft_n_max?: number | null;
/** Whether tensor-parallel split (--split-mode tensor) is active. */
tensor_parallel?: boolean;
- /** The load ran with the vision projector deliberately left unloaded. Echoes the
- * request, so it round-trips the Advanced Settings switch even on a GGUF that
- * never had a projector -- unlike vision_disabled_by_user below. */
+ /** The load ran with the vision projector deliberately left unloaded. Echoes the request, so it
+ * round-trips the Advanced Settings switch even on a GGUF that never had a projector, unlike
+ * vision_disabled_by_user below. */
disable_vision?: boolean;
/** Image input is off because the user asked, not because the mmproj is missing. */
vision_disabled_by_user?: boolean;
@@ -297,11 +273,9 @@ export interface LoadModelResponse {
gpu_ids?: number[] | null;
/** User-requested GPU placement pool before fit-time narrowing. */
requested_gpu_ids?: number[] | null;
- /** Slots the load was invoked with (else the --parallel default). Null for
- * non-GGUF loads. */
+ /** Slots the load was invoked with (else the --parallel default). Null for non-GGUF loads. */
requested_parallel_slots?: number | null;
- /** Slots llama-server actually runs, after any fit-time reduction. Null for
- * non-GGUF loads. */
+ /** Slots llama-server actually runs, after any fit-time reduction. Null for non-GGUF loads. */
parallel_slots?: number | null;
/** batch size (--batch-size) the load was invoked with; null = default */
requested_n_batch?: number | null;
@@ -323,8 +297,8 @@ export interface UnloadModelRequest {
model_path: string;
/** Cancel this exact in-flight load; never unload an already-resident model. */
cancel_load_request_id?: string | null;
- /** Stop any chats still generating instead of getting a 409: the unload takes down the
- * llama-server they all decode on. */
+ /** Stop any chats still generating instead of getting a 409: the unload takes down the llama-server
+ * they all decode on. */
force_cancel_active?: boolean;
}
@@ -336,9 +310,8 @@ export interface InferenceStatusResponse {
is_gguf?: boolean;
is_local_model?: boolean;
is_diffusion?: boolean;
- /** GPU-layer count the diffusion runner was ASKED for, when it differs from what
- * it applied: a shim without --ngl runs Auto, so gpu_layers reports -1 while
- * this carries the standing request. */
+ /** GPU-layer count the diffusion runner was ASKED for, when it differs from what it applied: a shim
+ * without --ngl runs Auto, so gpu_layers reports -1 while this carries the request. */
diffusion_requested_ngl?: number | null;
gguf_variant?: string | null;
is_audio?: boolean;
@@ -384,9 +357,8 @@ export interface InferenceStatusResponse {
spec_draft_n_max?: number | null;
/** Whether tensor-parallel split (--split-mode tensor) is active. */
tensor_parallel?: boolean;
- /** The load ran with the vision projector deliberately left unloaded. Echoes the
- * request, so it round-trips the Advanced Settings switch even on a GGUF that
- * never had a projector -- unlike vision_disabled_by_user below. */
+ /** The load ran with the vision projector deliberately left unloaded. Echoes the request, so it
+ * round-trips the Advanced Settings switch even on a GGUF that never had a projector. */
disable_vision?: boolean;
/** Image input is off because the user asked, not because the mmproj is missing. */
vision_disabled_by_user?: boolean;
@@ -405,11 +377,9 @@ export interface InferenceStatusResponse {
gpu_ids?: number[] | null;
/** User-requested GPU placement pool before fit-time narrowing. */
requested_gpu_ids?: number[] | null;
- /** Slots the active load was invoked with (else the --parallel default).
- * Null when no GGUF model is loaded. */
+ /** Slots the active load was invoked with (else the --parallel default). Null when no GGUF model is loaded. */
requested_parallel_slots?: number | null;
- /** Slots llama-server actually runs, after any fit-time reduction. Null when
- * no GGUF model is loaded. */
+ /** Slots llama-server actually runs, after any fit-time reduction. Null when no GGUF model is loaded. */
parallel_slots?: number | null;
/** batch size (--batch-size) the active load was invoked with; null = default */
requested_n_batch?: number | null;
@@ -428,27 +398,14 @@ export interface InferenceStatusResponse {
n_layers?: number | null;
/** Model's MoE expert-layer count (the n_cpu_moe ceiling); 0 if not MoE. */
n_moe_layers?: number;
- /**
- * Why a speculative drafter was disabled despite being requested.
- * "binary_no_mtp" / "binary_outdated" -> updating llama.cpp would re-enable
- * it; "runtime_error" -> the current build could not run it;
- * "drafter_not_found" -> its MTP, DSpark or DFlash sidecar was unavailable;
- * "drafter_no_vram" -> an Auto-mode fit downgrade: the model pins on GPU but
- * the drafter's reserve does not, and Auto keeps the context rather than
- * shrink it (choose the drafter in Settings to force it);
- * "mla_mtp_disabled" -> an Auto-mode policy downgrade for MLA models
- * (GLM-5.2 et al.) whose llama.cpp MTP path is slower than no speculation
- * (updating won't help; choose MTP in Settings to force it);
- * "mtp_partial_offload" -> an Auto-mode policy downgrade for an embedded
- * Hybrid Mamba MTP head on a partially offloaded placement, whose recurrent
- * rollback copies cost more layers than the drafting wins back (updating
- * won't help; choose MTP in Settings to force it). Null otherwise.
- */
- /**
- * Which drafter the resolution was about: "mtp", "dspark" or "dflash". Auto
- * resolves the kind itself, so speculative_type still reads "auto", and a fallback leaves
- * the engaged type at "default": neither names the file to fix.
- */
+ /** Why a speculative drafter was disabled despite being requested. "binary_no_mtp"/
+ * "binary_outdated": updating llama.cpp would re-enable it. "runtime_error": the build could
+ * not run it. "drafter_not_found": its sidecar was unavailable. "drafter_no_vram": an
+ * Auto-mode fit downgrade. "mla_mtp_disabled" and "mtp_partial_offload": Auto-mode policy
+ * downgrades where MTP costs more than it wins. Null otherwise. */
+ /** Which drafter the resolution was about: "mtp", "dspark" or "dflash". Auto resolves the kind
+ * itself, so speculative_type still reads "auto" and a fallback leaves the engaged type at
+ * "default": neither names the file to fix. */
spec_drafter_kind?: string | null;
spec_fallback_reason?: string | null;
/** Only for a binary stand-down: whether a different llama-server is installed now. */
@@ -487,8 +444,8 @@ export interface ApiMonitorEntry {
updated_at: number;
finished_at?: number | null;
duration_ms?: number | null;
- // duration_ms covers the whole request, queue wait and prefill included. decode_ms is
- // only the generating span, and is absent unless the engine reported it.
+ // duration_ms covers the whole request, queue wait and prefill included. decode_ms is only the
+ // generating span, and is absent unless the engine reported it.
decode_ms?: number | null;
context_length?: number | null;
context_usage?: number | null;
@@ -519,15 +476,15 @@ export interface ApiMonitorQueue {
export interface ApiMonitorResponse {
status: "idle" | "ready" | "generating";
- // Server wall clock (seconds) at snapshot, so started_at can be dated without trusting
- // the browser's clock. Absent on a backend older than the field.
+ // Server wall clock (seconds) at snapshot, so started_at can be dated without trusting the
+ // browser's clock. Absent on an older backend.
server_time?: number;
active_model?: string | null;
context_length?: number | null;
active_requests: number;
/** Live slot/queue occupancy; null when no llama model is loaded. */
queue?: ApiMonitorQueue | null;
- /** Absent on older backends -- treat only an explicit `false` as disabled. */
+ /** Absent on older backends: treat only an explicit `false` as disabled. */
logging_enabled?: boolean;
entries: ApiMonitorEntry[];
}
@@ -574,13 +531,9 @@ export type OpenAIMessageContentPart =
export type OpenAIMessageContent = string | OpenAIMessageContentPart[];
-/**
- * OpenAI Chat Completions tool_call shape. Assistant turns echo function calls
- * as `tool_calls`; the matching result rides on a separate `role="tool"`
- * message keyed by `tool_call_id`. `extra_content.google.thought_signature` is
- * the Gemini round-trip field the backend translator emits (on `delta.
- * tool_calls`) and consumes (when rebuilding the functionCall part next turn).
- */
+/** OpenAI Chat Completions tool_call shape. Assistant turns echo function calls as `tool_calls`;
+ * the matching result rides on a separate `role="tool"` message keyed by `tool_call_id`.
+ * `extra_content.google.thought_signature` is the Gemini round-trip field. */
export interface OpenAIToolCallPart {
id?: string;
type?: "function";
@@ -606,7 +559,7 @@ export interface OpenAIChatCompletionsRequest {
model: string;
messages: OpenAIChatMessage[];
stream: boolean;
- /** Reasoning-class OpenAI models reject these — caller may omit. */
+ /** Reasoning-class OpenAI models reject these; caller may omit. */
temperature?: number;
top_p?: number;
max_tokens: number;
@@ -631,11 +584,9 @@ export interface OpenAIChatCompletionsRequest {
| "xhigh"
| null;
preserve_thinking?: boolean | null;
- /**
- * Resume the trailing assistant turn rather than opening a new one: the rendered
- * prompt ends inside the partial answer, so the model emits its next token. Local
- * models only -- the external-provider proxy forwards an explicit field list.
- */
+ /** Resume the trailing assistant turn rather than opening a new one: the rendered prompt ends inside
+ * the partial answer, so the model emits its next token. Local models only, since the
+ * external-provider proxy forwards an explicit field list. */
continue_final_message?: boolean;
thinking?: { type: "disabled" | "enabled" } | null;
enable_tools?: boolean | null;
@@ -646,12 +597,9 @@ export interface OpenAIChatCompletionsRequest {
studio_tool_history?: boolean;
/** Local models + enable_tools only. */
confirm_tool_calls?: boolean;
- /**
- * Local models + enable_tools only. Gate level for local tool calls: "ask"
- * prompts on every call, "auto" prompts only on calls flagged unsafe, "off"
- * never prompts, "full" never prompts and drops the sandbox. Unset behaves
- * as "ask".
- */
+ /** Local models plus enable_tools only. Gate level for local tool calls: "ask" prompts on every
+ * call, "auto" only on calls flagged unsafe, "off" never, "full" never and drops the
+ * sandbox. Unset behaves as "ask". */
permission_mode?: "ask" | "auto" | "off" | "full";
/** Local models + enable_tools only. Full-access escape hatch. */
bypass_permissions?: boolean;
@@ -687,54 +635,34 @@ export interface OpenAIChatCompletionsRequest {
external_model?: string;
encrypted_api_key?: string;
provider_base_url?: string | null;
- /**
- * Boolean toggle for OpenAI/Anthropic ephemeral cache_control. For Gemini the
- * backend also accepts a cached-content resource name (`cachedContents/...`)
- * string, forwarded as `generationConfig.cachedContent`.
- */
+ /** Boolean toggle for OpenAI/Anthropic ephemeral cache_control. For Gemini the backend also accepts
+ * a cached-content resource name, forwarded as `generationConfig.cachedContent`. */
enable_prompt_caching?: boolean | string | null;
- /**
- * OpenAI shell-tool container id from the prior response in this thread. When
- * set and the Code pill is on, the backend routes the next /v1/responses with
- * `environment.type="container_reference"` so filesystem state persists; unset
- * → `container_auto` (fresh container). OpenAI cloud + gpt-5.5 family only.
- */
+ /** OpenAI shell-tool container id from the prior response in this thread. When set and the Code
+ * pill is on, the backend routes the next /v1/responses with
+ * `environment.type="container_reference"` so filesystem state persists; unset means a fresh
+ * container. OpenAI cloud and the gpt-5.5 family only. */
openai_code_exec_container_id?: string | null;
- /**
- * Anthropic code_execution container id from the prior response in this
- * thread. When set and the Code pill is on, the backend forwards a top-level
- * `container` on /v1/messages so filesystem state persists; unset →
- * auto-created. Anthropic provider with `code_execution` in `enabled_tools`.
- */
+ /** Anthropic code_execution container id from the prior response in this thread. When set and the
+ * Code pill is on, the backend forwards a top-level `container` on /v1/messages so filesystem
+ * state persists; unset is auto-created. */
anthropic_code_exec_container_id?: string | null;
- /**
- * Anthropic fast-mode toggle. Opus 4.6 / 4.7 only; dropped silently elsewhere.
- * See https://platform.claude.com/docs/en/build-with-claude/fast-mode
- */
+ /** Anthropic fast-mode toggle. Opus 4.6 / 4.7 only; dropped silently elsewhere. */
fast_mode?: boolean | null;
- /**
- * Opt into the OpenAI-standard trailing usage chunk on streams
- * (`choices: []` with `usage` + llama-server `timings` populated). The
- * backend only emits it when `include_usage` is set; the local chat UI
- * sends it so the context-usage bar and tok/s readout populate.
- */
+ /** Opt into the OpenAI-standard trailing usage chunk on streams. The backend only emits it when
+ * `include_usage` is set; the local chat UI sends it so the context-usage bar and tok/s
+ * readout populate. */
stream_options?: { include_usage?: boolean } | null;
}
export interface OpenAIChatDelta {
role?: string;
content?: string | null;
- /**
- * Streamed assistant tool calls. The Gemini and OpenAI Responses translators
- * emit incremental deltas (function name + arguments fragments) so the
- * chat-adapter can render tool cards as they arrive.
- */
+ /** Streamed assistant tool calls. The Gemini and OpenAI Responses translators emit incremental
+ * deltas so the chat-adapter can render tool cards as they arrive. */
tool_calls?: OpenAIToolCallPart[];
- /**
- * Provider-specific passthrough. Gemini ships `thoughtSignature`, citations,
- * `native_part`, etc., here so the round-trip can replay them on follow-up
- * turns without bleeding into other providers.
- */
+ /** Provider-specific passthrough. Gemini ships `thoughtSignature`, citations, `native_part` and the
+ * like here so the round-trip can replay them without bleeding into other providers. */
extra_content?: Record<string, unknown>;
}
@@ -757,43 +685,37 @@ export interface OpenAIChatChunk {
prompt_tokens_after?: number;
context_length?: number;
fits: boolean;
- // Present when the evicted turns were archived and searched. Counts only, never
- // message text: this rides an SSE chunk that reaches the client.
+ // Present when the evicted turns were archived and searched. Counts only, never message text: this
+ // rides an SSE chunk that reaches the client.
archived_messages?: number;
recalled_chunks?: number;
- // Present only when `fits` is false: the floor the conversation cannot go below, and
- // how much of it is the message just sent. Together they say whether the history or
- // that one message is the problem, i.e. whether "shorten the conversation" helps.
+ // Present only when `fits` is false: the floor the conversation cannot go below, and how much of
+ // it is the message just sent. Together they say whether the history or that one message is
+ // the problem.
irreducible_tokens?: number;
latest_turn_tokens?: number;
- // Whether `latest_turn_tokens` is a real token count or the four-characters-a-token
- // estimate the fit falls back to when nothing could price the turn at all. A turn the
- // template renders as nothing on its own is priced by difference and stays exact.
- // Only the counted one may be quoted as the turn's size.
+ // Whether `latest_turn_tokens` is a real count or the four-characters-a-token estimate the fit
+ // falls back to. Only the counted one may be quoted as the turn's size.
latest_turn_exact?: boolean;
- // The floor both counts above carry: what a rendered prompt costs with no messages in
- // it, which on a tool-enabled request is the whole tool catalogue. Subtract it before
- // comparing them, or the catalogue is blamed on the turn. Absent from an older server,
- // where zero reproduces the old behaviour.
+ // The floor both counts above carry: what a rendered prompt costs with no messages, which on a
+ // tool-enabled request is the whole tool catalogue. Subtract it before comparing them, or the
+ // catalogue is blamed on the turn.
shared_prompt_tokens?: number;
- // Where the compaction boundary sits in the messages THIS request was sent with.
- // Absolute, unlike dropped_messages, so re-sending it after a turn that refit several
- // times cannot advance the boundary past the turns actually evicted.
+ // Where the compaction boundary sits in the messages THIS request was sent with. Absolute, unlike
+ // dropped_messages, so re-sending it after a turn that refit several times cannot advance the
+ // boundary past the turns actually evicted.
boundary_messages?: number;
- // The text the boundary landed ON, so the count can be re-derived by position. A count
- // is only valid against the transcript it was counted on, and deleting an already
- // evicted prompt shortens that transcript; without the anchor the replayed count then
- // evicts live turns instead. Carried through untouched, like boundary_messages.
+ // The text the boundary landed ON, so the count can be re-derived by position: a count is only
+ // valid against the transcript it was counted on, and deleting an already evicted prompt
+ // shortens that transcript.
boundary_anchor?: string;
- // How much extra trim the fit that set the boundary above used. Replayed against the
- // request's own ratio: a boundary cut under more headroom than the caller now asks for
- // is discarded, so lowering the setting hands the history back.
+ // How much extra trim the fit that set the boundary used. Replayed against the request's own
+ // ratio, so a boundary cut under more headroom than the caller now asks for is discarded.
boundary_headroom_ratio?: number;
- // Whose message that is: in a tool loop the last one is often a tool result rather
- // than anything the user typed.
+ // Whose message that is: in a tool loop the last one is often a tool result rather than anything the user typed.
latest_turn_role?: string;
- // The prompt's share of the window (context_length minus the reply reserve), which is
- // what one turn must fit inside. Not re-derived here: the formula lives in the fit.
+ // The prompt's share of the window (context_length minus the reply reserve), which is what one turn
+ // must fit inside. Not re-derived here: the formula lives in the fit.
prompt_target?: number;
};
}
diff --git a/studio/frontend/src/features/chat/types/runtime.ts b/studio/frontend/src/features/chat/types/runtime.ts
index 79651f07e13..9e77125d407 100644
--- a/studio/frontend/src/features/chat/types/runtime.ts
+++ b/studio/frontend/src/features/chat/types/runtime.ts
@@ -15,21 +15,16 @@ export interface InferenceParams {
checkpoint: string;
/** Allow loading models with custom code (e.g. NVIDIA Nemotron). Only enable for repos you trust. */
trustRemoteCode?: boolean;
- /**
- * Anthropic fast-mode toggle. Opus 4.6 / 4.7 only; higher OTPS at 6x Opus
- * pricing. Default false.
- * https://platform.claude.com/docs/en/build-with-claude/fast-mode
- */
+ /** Anthropic fast-mode toggle. Opus 4.6 / 4.7 only; higher OTPS at 6x Opus pricing. Default
+ * false. https://platform.claude.com/docs/en/build-with-claude/fast-mode */
fastMode?: boolean;
- /**
- * Sampling seed forwarded to the backends that read one so a generation can be
- * reproduced. `null` is unset, which leaves the backend to draw its own per request.
- */
+ /** Sampling seed forwarded to the backends that read one so a generation can be reproduced.
+ * `null` is unset, which leaves the backend to draw its own per request. */
seed?: number | null;
}
-/** llama.cpp reads the seed as a uint32 and spends 0xFFFFFFFF on LLAMA_DEFAULT_SEED,
- * its "draw one" sentinel, so a pin can name every value below that and no other. */
+/** llama.cpp reads the seed as a uint32 and spends 0xFFFFFFFF on LLAMA_DEFAULT_SEED, its "draw
+ * one" sentinel, so a pin can name every value below that and no other. */
export const MAX_SAMPLING_SEED = 4_294_967_294;
/** An absent flag reads as false, so a row that omits one makes the gate answer wrong. */
@@ -40,9 +35,9 @@ export type SeedGateFlags = Required<
/** What the store holds, so every place that mints a row has to state those flags. */
export type ChatModelRow = ChatModelSummary & SeedGateFlags;
-/** Whether the loaded model's backend reads a sampling seed. Shared so the panel cannot
- * offer the field where the request would drop it, and takes the same `models[]` summary
- * the request body already reads `isGguf` from, so the two cannot answer differently. */
+/** Whether the loaded model's backend reads a sampling seed. Shared so the panel cannot offer
+ * the field where the request would drop it, and takes the same `models[]` summary the
+ * request body already reads `isGguf` from. */
export function modelReadsSamplingSeed(
activeModel: SeedGateFlags | null | undefined,
): boolean {
@@ -50,13 +45,13 @@ export function modelReadsSamplingSeed(
if (activeModel?.isAudio && !activeModel.hasAudioInput) {
return false;
}
- // llama-server takes a seed, and so does MLX. The transformers backend declares no
- // `seed` kwarg, so worker.py's `_backend_declares` gate drops it before generation.
+ // llama-server takes a seed, and so does MLX. The transformers backend declares no `seed`
+ // kwarg, so worker.py's `_backend_declares` gate drops it before generation.
return activeModel?.isGguf === true || activeModel?.isMlx === true;
}
-/** The params that survive a reload. `checkpoint` names the model rather than
- * being one of its settings, so it is not one of them. */
+/** The params that survive a reload. `checkpoint` names the model rather than being one of its
+ * settings, so it is not one of them. */
export type PersistedInferenceParams = Partial<
Omit<InferenceParams, "checkpoint">
>;
@@ -89,8 +84,8 @@ export interface ChatModelSummary {
isAudio?: boolean;
audioType?: string | null;
hasAudioInput?: boolean;
- /** llama-server takes video for this model: mmproj video support, a build
- * with video enabled, and ffmpeg installed. */
+ /** llama-server takes video for this model: mmproj video support, a build with video enabled,
+ * and ffmpeg installed. */
hasVideoInput?: boolean;
}
diff --git a/studio/frontend/src/features/chat/utils/archived-chat-export.ts b/studio/frontend/src/features/chat/utils/archived-chat-export.ts
index 834dfdcf5fa..04368b29336 100644
--- a/studio/frontend/src/features/chat/utils/archived-chat-export.ts
+++ b/studio/frontend/src/features/chat/utils/archived-chat-export.ts
@@ -10,8 +10,8 @@ type ExportThreadView = {
type ExportMessageView = { threadId?: string };
type ExportProjectView = { id?: string };
-// Full chat-export backup shape, kept structural so the pure filter below
-// stays decoupled from the storage layer that produces it.
+// Full chat-export backup shape, kept structural so the pure filter below stays decoupled from the
+// storage layer that produces it.
export interface ChatExportData {
exportedAt?: string;
version?: number;
@@ -21,9 +21,9 @@ export interface ChatExportData {
messages: unknown[];
}
-// Restrict a full chat export to archived threads, their messages and the
-// projects those threads belong to. Pure: never mutates the input, and keeps
-// the original thread/message objects so the backup re-imports unchanged.
+// Restrict a full chat export to archived threads, their messages and the projects those threads
+// belong to. Pure: never mutates the input, and keeps the original objects so the backup
+// re-imports unchanged.
export function filterArchivedChatExport<T extends ChatExportData>(
full: T,
): { data: T; archivedCount: number } {
diff --git a/studio/frontend/src/features/chat/utils/auto-compaction.ts b/studio/frontend/src/features/chat/utils/auto-compaction.ts
index f89279bfae9..a396d1a7085 100644
--- a/studio/frontend/src/features/chat/utils/auto-compaction.ts
+++ b/studio/frontend/src/features/chat/utils/auto-compaction.ts
@@ -97,8 +97,8 @@ export function ggufCompactionRequestFields(options: {
} {
if (!options.isGguf) return {};
if (!options.autoCompactEnabled) {
- // An omitted field falls back to UNSLOTH_CONTEXT_OVERFLOW, which may still
- // compact. "error" is an explicit refusal of that fallback.
+ // An omitted field falls back to UNSLOTH_CONTEXT_OVERFLOW, which may still compact. "error" is an
+ // explicit refusal of that fallback.
return { context_overflow: "error" };
}
if (options.contextPolicy === "rolling") {
diff --git a/studio/frontend/src/features/chat/utils/auto-continue-run-keeper.ts b/studio/frontend/src/features/chat/utils/auto-continue-run-keeper.ts
index 03413c9a440..b1cb76cc124 100644
--- a/studio/frontend/src/features/chat/utils/auto-continue-run-keeper.ts
+++ b/studio/frontend/src/features/chat/utils/auto-continue-run-keeper.ts
@@ -1,15 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Wires the automatic-continuation lease keeper to the runs actually in flight.
- *
- * The keeper itself is in `continuation.ts` and knows nothing about this app; all it asks
- * for is "is thread X generating right now", which `runningByThreadId` answers per thread
- * and regardless of what is on screen. The same store field is what the prompt queue uses
- * for completion detection, for the same reason: it "tracks the actual thread (not
- * aui.thread()), so detection survives navigation".
- */
+/** Wires the automatic-continuation lease keeper to the runs actually in flight. The keeper itself
+ * is in `continuation.ts` and knows nothing about this app; all it asks is "is thread X
+ * generating right now", which `runningByThreadId` answers per thread and regardless of what is
+ * on screen. The prompt queue uses the same field for completion detection, so detection
+ * survives navigation. */
import { useChatRuntimeStore } from "../stores/chat-runtime-store";
import {
@@ -24,23 +20,14 @@ import {
const keeper = createAutoContinueLeaseKeeper({
signal: {
- /**
- * Generating, and not the image gate settling its waiters.
- *
- * A continuation carrying an image that the loaded model cannot read -- a chat with a
- * picture in it, reopened on a truncated reply with a text-only model loaded, or with
- * Vision switched off since -- is refused before any request is made, and the gate
- * pulses this field true and then false so compare mode's `waitForRunEnd` resolves. To
- * a hold that is waiting for its own run to start, that pair reads as the run starting
- * and finishing: it arms on the first and is released on the second, with the `done`
- * marker that tells every other tab for a day that the message HAS been continued. The
- * failure that follows cannot undo it, because a released hold is gone. So the pulse is
- * not counted as a run, the hold never arms, and the failure discards it as it does any
- * other run that died before it started.
- *
- * Only when the gate is ALL that holds the flag. A real run sharing the key -- the
- * other compare pane, or a sibling under "__default" -- still answers yes.
- */
+ /** Generating, and not the image gate settling its waiters. A continuation carrying an image the
+ * loaded model cannot read is refused before any request is made, and the gate pulses this
+ * field true and then false so compare mode's `waitForRunEnd` resolves. To a hold waiting for
+ * its own run to start, that pair reads as the run starting and finishing: it arms on the
+ * first and is released on the second, with the `done` marker that tells every other tab for a
+ * day that the message HAS been continued, which the failure that follows cannot undo. So the
+ * pulse is not counted as a run. Only when the gate is ALL that holds the flag: a real run
+ * sharing the key still answers yes. */
isRunning: (threadId) => {
const state = useChatRuntimeStore.getState();
return (
@@ -62,15 +49,10 @@ function tick(): void {
}
}
-/**
- * A run that failed before it ever reached `runningByThreadId`.
- *
- * The adapter wrapper catches everything `adapter.run` throws -- this chat's settings
- * pairing running out, a model that will not load, a connection it refuses -- and announces
- * it per thread, which is the only signal that separates a preflight that FAILED from one
- * that is merely long. Nothing here reads a clock: a slow run emits no event and keeps its
- * hold and its renewals, which is what the absence of an arming deadline is for.
- */
+/** A run that failed before it ever reached `runningByThreadId`. The adapter wrapper catches
+ * everything `adapter.run` throws and announces it per thread, which is the only signal
+ * separating a preflight that FAILED from one that is merely long. Nothing here reads a clock:
+ * a slow run emits no event and keeps its hold and its renewals. */
function onRunFailed(event: Event): void {
const threadId = (event as CustomEvent<PromptQueueRunFailedEventDetail>)
.detail?.threadId;
@@ -86,20 +68,12 @@ function onRunFailed(event: Event): void {
let listening = false;
-/**
- * Hold `messageId`'s lease for as long as `threadId`'s own run is generating.
- *
- * Called by the continuation bar at the moment it starts a run, because that is the last
- * point at which anything knows both ids: the bar unmounts as soon as the continuation's
- * sibling becomes the selected branch, and the thread it ran on may not be the one on
- * screen a second later.
- *
- * `threadId` is the key the run files itself under, which is `threadListItem.remoteId` --
- * the same value assistant-ui passes the adapter as `unstable_threadId`. A thread with no
- * remote id yet (the first turn of a New Chat) files its run under a shared placeholder
- * that is not safe to watch, so nothing is held for it and its lease simply runs out its
- * TTL. That is the same outcome as a tab closing mid-run, and it is never an early release.
- */
+/** Hold `messageId`'s lease for as long as `threadId`'s own run is generating. Called by the
+ * continuation bar at the moment it starts a run, the last point at which anything knows both
+ * ids: the bar unmounts as soon as the continuation's sibling becomes selected. `threadId` is
+ * the key the run files itself under, `threadListItem.remoteId`. A thread with no remote id yet
+ * files under a shared placeholder that is not safe to watch, so nothing is held and its lease
+ * runs out its TTL, the same outcome as a tab closing mid-run. */
export function holdAutoContinueRun(
messageId: string,
threadId: string | undefined,
@@ -108,8 +82,8 @@ export function holdAutoContinueRun(
return;
}
keeper.hold(messageId, threadId);
- // Registered with the first hold rather than at import, so a module nobody continues in
- // adds no listener. Once, and never removed: it is one handler for the tab.
+ // Registered with the first hold rather than at import, so a module nobody continues in adds no
+ // listener. Once, and never removed: it is one handler for the tab.
if (!listening && typeof window !== "undefined") {
listening = true;
window.addEventListener(PROMPT_QUEUE_RUN_FAILED_EVENT, onRunFailed);
diff --git a/studio/frontend/src/features/chat/utils/chat-attachment-events.ts b/studio/frontend/src/features/chat/utils/chat-attachment-events.ts
index dbde1578901..41a2942c6f1 100644
--- a/studio/frontend/src/features/chat/utils/chat-attachment-events.ts
+++ b/studio/frontend/src/features/chat/utils/chat-attachment-events.ts
@@ -1,12 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Notifies loaded chat runtimes when the Data tab deletes a stored attachment.
- * Without this, the active thread's in-memory repository still holds the
- * attachment, and any later repo-to-storage sync (e.g. deleting a message in
- * that thread) writes it back, undoing the deletion.
- */
+/** Notifies loaded chat runtimes when the Data tab deletes a stored attachment. Without this, the
+ * active thread's in-memory repository still holds it, and any later repo-to-storage sync
+ * writes it back, undoing the deletion. */
import forge from "node-forge";
@@ -45,8 +42,7 @@ function stableJson(value: unknown): string {
return JSON.stringify(value) ?? "null";
}
-/** Canonical payload used to detect whether an async hash still describes the
- * current message content. */
+/** Canonical payload used to detect whether an async hash still describes the current message content. */
export function chatContentPartAttachmentSignature(
part: unknown,
): string | null {
@@ -74,8 +70,8 @@ export function chatContentPartAttachmentSignature(
return stableJson(payload);
}
-/** Mirrors the backend's stable content-part identity without adding private
- * metadata to the message payload sent to inference. */
+/** Mirrors the backend's stable content-part identity without adding private metadata to the
+ * message payload sent to inference. */
export async function chatContentPartAttachmentIdFromSignature(
signature: string,
): Promise<string> {
@@ -91,8 +87,8 @@ export async function chatContentPartAttachmentIdFromSignature(
byte.toString(16).padStart(2, "0"),
).join("");
} catch {
- // Fall through to the pure-JS implementation below. Some embedded
- // browsers expose crypto.subtle but reject it outside a secure context.
+ // Fall through to the pure-JS implementation below. Some embedded browsers expose crypto.subtle
+ // but reject it outside a secure context.
}
}
if (hex === null) {
diff --git a/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts b/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts
index bf42c354919..52a4982f885 100644
--- a/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts
+++ b/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts
@@ -95,14 +95,12 @@ export async function loadGenerationOverlaySnapshot<TMessage, TRun>(
): Promise<{
messages: TMessage[];
activeRuns: TRun[];
- /**
- * False when the active-run read failed, so its empty list is "unknown", not "none".
- * A caller that decides a reply is dead from an absent run has to tell the two apart.
- */
+ /** False when the active-run read failed, so its empty list is "unknown", not "none". A caller
+ * deciding a reply is dead from an absent run has to tell the two apart. */
activeRunsLoaded: boolean;
}> {
- // Runs first closes the create-between-snapshots gap. If a run commits after
- // this read, the later message snapshot already carries its durable metadata.
+ // Runs first closes the create-between-snapshots gap. If a run commits after this read, the
+ // later message snapshot already carries its durable metadata.
let activeRunsLoaded = true;
const activeRuns = await listActiveRuns(threadId).catch(() => {
activeRunsLoaded = false;
@@ -217,14 +215,9 @@ export function recoveredGenerationFinalMetadata(options: {
return next;
}
-/**
- * The reply as one string, with reasoning back inside `<think>` tags.
- *
- * The recovery follows a run by replaying its chunk events, so it keeps the
- * reply as the text those chunks carried rather than as parts. This is the
- * inverse of `parseAssistantContent`, and it is what lets a body that arrived
- * as parts be compared against one that arrived as deltas.
- */
+/** The reply as one string, with reasoning back inside `<think>` tags. The recovery replays a
+ * run's chunk events, so it keeps the reply as the text those chunks carried rather than as
+ * parts. Inverse of `parseAssistantContent`, so a parts body can be compared to a delta one. */
export function generationRawContent(content: unknown): {
raw: string;
reasoningOpen: boolean;
@@ -252,23 +245,12 @@ export function generationRawContent(content: unknown): {
return { raw, reasoningOpen };
}
-/**
- * Which body a recovery publish should put in front of the reader.
- *
- * A recovery replays the run's events from the cursor the last save recorded,
- * one publish per event, each publish paying a write to storage before the
- * next event is read. When the run being replayed is the one this tab is also
- * streaming, that walk is orders of magnitude behind the live stream, so its
- * body is a PREFIX of what the reader is already looking at. Importing it
- * rewinds the reply to its opening lines until the adapter's next yield
- * restores it, which is the reasoning pane collapsing and recovering about
- * twice a second.
- *
- * Prefix, not length: a body that genuinely disagrees with the view is the
- * server's and still wins, because storage is authoritative for everything a
- * recovery exists to repair. Only a body that carries nothing the view is not
- * already showing is refused.
- */
+/** Which body a recovery publish should show. A recovery replays the run's events from the
+ * last saved cursor, one publish per event, each paying a storage write. When the replayed
+ * run is also the one this tab is streaming, that walk is far behind the live stream and
+ * its body is a PREFIX of what the reader sees, so importing it rewinds the reply twice a
+ * second. Prefix, not length: a body that genuinely disagrees is the server's and wins,
+ * since storage is authoritative. Only a body carrying nothing new is refused. */
export function recoveredContentToImport<TContent>(
viewContent: TContent,
recoveredContent: TContent,
@@ -285,13 +267,11 @@ export function generationNeedsRecovery(
metadata: Record<string, unknown>,
): boolean {
const status = String(metadata.generationStatus) as StoredGenerationStatus;
- // Stamped by a follower that hit its no-progress deadline. Re-following on every
- // recovery trigger is what turned one stuck run into a permanently blocked composer;
- // history.load clears the marker if /chat-runs/active still names the run.
- //
- // Non-terminal only: a run the backend finished is stored completed and unsettled, and
- // /chat-runs/active excludes completed runs, so honouring the marker there would leave
- // that reply running forever with its event tail never imported.
+ // Stamped by a follower that hit its no-progress deadline. Re-following on every recovery
+ // trigger turned one stuck run into a permanently blocked composer; history.load clears the
+ // marker if /chat-runs/active still names the run. Non-terminal only: a run the backend
+ // finished is stored completed, and /chat-runs/active excludes completed runs, so honouring
+ // the marker there would leave that reply running forever.
if (metadata.generationLocallyInterrupted === true && !TERMINAL.has(status)) {
return false;
}
@@ -351,9 +331,9 @@ export function generationRecoveryMetadata(options: {
if (totalChunks !== undefined) {
next.generationChunkCount = totalChunks;
}
- // Carried with the cursor for the same reason the two above are: the usage chunk arrives
- // before the terminal event, so a cursor published past it and then reloaded would resume
- // after it and lose the token counts and server timings for good.
+ // Carried with the cursor for the same reason as the two above: the usage chunk arrives before
+ // the terminal event, so a cursor published past it and reloaded would resume after it and
+ // lose the token counts and server timings for good.
if (usage !== undefined) {
next.generationRecoveryUsage = usage;
}
@@ -413,38 +393,23 @@ export function subscribeGenerationRecoveryTriggers(
};
}
-/**
- * Runs this tab is streaming itself, so a recovery never follows one of them.
- *
- * A durable run otherwise gets TWO readers in the tab that started it. The adapter streams
- * it, and `scheduleGenerationRecovery` also replays it from storage, because its only gate
- * is `generationNeedsRecovery(metadata)` and `history.load()` force-writes
- * `generationSettled: false` onto any message matching an active run. Nothing asks whether
- * this tab is already the producer.
- *
- * That second reader is expensive, not merely redundant. It publishes on EVERY chunk event,
- * and each publish re-parses the whole reply and awaits a PUT of the entire message, so a
- * reply of N chunks costs N round trips and N full parses whose payload grows with the
- * reply: quadratic in the length of the answer, on the main thread, against the same backend
- * the model is saturating.
- *
- * Module state, deliberately. A reload is exactly the case where this tab has STOPPED being
- * the producer and a recovery should run, and a reload clears this by construction. Nothing
- * needs to expire it.
- */
+/** Runs this tab is streaming itself, so a recovery never follows one. A durable run otherwise
+ * gets TWO readers in the tab that started it: the adapter streams it and
+ * scheduleGenerationRecovery replays it from storage, since its only gate is
+ * generationNeedsRecovery and history.load force-writes generationSettled false. That
+ * second reader publishes on EVERY chunk, each publish re-parsing the whole reply and
+ * awaiting a PUT of the entire message: quadratic in the answer length, on the main thread.
+ * Module state deliberately: a reload is exactly when this tab has stopped being the
+ * producer, and a reload clears this by construction. */
const liveGenerationRuns = new Set<string>();
-// runId -> owning thread. The Set above answers "is this run live"; the checkpoint
-// scheduler needs the inverse, "does this thread have a durable run at all".
+// runId -> owning thread. The Set above answers "is this run live"; the checkpoint scheduler
+// needs the inverse, "does this thread have a durable run at all".
const liveGenerationThreads = new Map<string, string>();
-/**
- * Runs claimed before the server admitted them.
- *
- * The early claim stops a recovery trigger starting a second follower during that await.
- * Boundedness is separate: until the POST lands the thread's checkpoints are its only
- * persistence, and createChatGenerationRun retries until aborted, so the await can outlast
- * the cap.
- */
+/** Runs claimed before the server admitted them. The early claim stops a recovery trigger
+ * starting a second follower during that await. Boundedness is separate: until the POST
+ * lands the thread's checkpoints are its only persistence, and the create retries until
+ * aborted, so the await can outlast the cap. */
const provisionalGenerationRuns = new Set<string>();
/** Claim a run as streamed by this tab. Pair with `releaseLiveGenerationRun` in a finally. */
@@ -463,12 +428,8 @@ export function claimLiveGenerationRun(
}
}
-/**
- * Release a run this tab was streaming.
- *
- * Must be unconditional, in a finally: a run left claimed after its stream died is a run this
- * tab will never recover, which is the failure this registry could introduce if it leaked.
- */
+/** Release a run this tab was streaming. Must be unconditional, in a finally: a run left
+ * claimed after its stream died is one this tab will never recover. */
export function releaseLiveGenerationRun(runId: string): void {
liveGenerationRuns.delete(runId);
liveGenerationThreads.delete(runId);
@@ -480,10 +441,8 @@ export function isLiveGenerationRun(runId: string): boolean {
return liveGenerationRuns.has(runId);
}
-/**
- * Whether `threadId` has a durable run, either streaming here or named by the server. A
- * subscriber-owned stream has none, and its periodic checkpoint is its ONLY persistence.
- */
+/** Whether `threadId` has a durable run, streaming here or named by the server. A
+ * subscriber-owned stream has none, and its periodic checkpoint is its ONLY persistence. */
export function threadHasDurableGenerationRun(threadId: string): boolean {
for (const [runId, owner] of liveGenerationThreads.entries()) {
if (owner === threadId && !provisionalGenerationRuns.has(runId)) return true;
@@ -494,21 +453,15 @@ export function threadHasDurableGenerationRun(threadId: string): boolean {
return false;
}
-/**
- * Runs the server has named as still going, keyed by the thread whose load asked.
- *
- * Persisted metadata is not evidence of a live run: a run that never terminalises leaves
- * `generationStatus: "running"` in storage for good, and a reload rebuilds a running
- * message straight back out of it. Only `/api/inference/chat-runs/active` can say a run
- * is still going. Module state, so a reload drops the previous session's word for it.
- */
+/** Runs the server has named as still going, keyed by the thread whose load asked. Persisted
+ * metadata is not evidence of a live run: a run that never terminalises leaves
+ * `generationStatus: "running"` in storage for good. Only /chat-runs/active can say a run
+ * is still going. Module state, so a reload drops the previous session's word for it. */
const serverActiveGenerationRuns = new Map<string, string>();
-/**
- * Replace what the server last said about `threadId`. Call only after a SUCCESSFUL read
- * of the active-run list: a failed read is not a report of "nothing is running", and
- * treating it as one would mark a live reply interrupted.
- */
+/** Replace what the server last said about `threadId`. Call only after a SUCCESSFUL read of
+ * the active-run list: a failed read is not a report of "nothing is running", and treating
+ * it as one would mark a live reply interrupted. */
export function syncServerActiveGenerationRuns(
threadId: string,
runIds: Iterable<string>,
@@ -520,42 +473,31 @@ export function syncServerActiveGenerationRuns(
serverAnsweredThreads.add(threadId);
}
-/**
- * Threads the server has answered the active-run question for. Per thread, not per
- * process: one global flag let A's answer turn a FAILED read for B into an empty list.
- */
+/** Threads the server has answered the active-run question for. Per thread, not per process:
+ * one global flag let A's answer turn a FAILED read for B into an empty list. */
const serverAnsweredThreads = new Set<string>();
export function serverHasAnsweredActiveRuns(threadId: string): boolean {
return serverAnsweredThreads.has(threadId);
}
-/**
- * Forget what the server last said about `threadId`, because the newest read failed.
- *
- * The answer is a point in time, not a permanent property. Another tab can start a run
- * after a successful read, so keeping the old answer once a refresh has failed would
- * restore that genuinely running reply as interrupted.
- */
+/** Forget what the server last said about `threadId`, because the newest read failed. The
+ * answer is a point in time, not a permanent property: another tab can start a run after a
+ * successful read, so keeping the old answer would restore a running reply as interrupted. */
export function markServerActiveGenerationRunsUnknown(threadId: string): void {
serverAnsweredThreads.delete(threadId);
// The run mappings go with it. They are the same stale answer in another shape, and
// isServerActiveGenerationRun is consulted BEFORE the local-interruption marker, so a
- // leftover entry restores the message as running while generationNeedsRecovery still
- // refuses to start a follower: a blocked composer with neither recovery nor a local
- // Stop handle, which is the wedge this PR exists to remove.
+ // leftover entry restores the message as running while generationNeedsRecovery refuses to
+ // start a follower: a blocked composer with neither recovery nor a local Stop handle.
for (const [runId, owner] of [...serverActiveGenerationRuns]) {
if (owner === threadId) serverActiveGenerationRuns.delete(runId);
}
}
-/**
- * Drop one run from the server-active map now that it has reached a terminal status.
- *
- * Only another successful sync would otherwise remove it, so the thread would keep
- * reading as durable and a later subscriber-owned stream on it would be capped, losing
- * the periodic saves that are that stream's only persistence.
- */
+/** Drop one run from the server-active map now that it has reached a terminal status. Only
+ * another successful sync would otherwise remove it, so the thread would keep reading as
+ * durable and a later subscriber-owned stream on it would be capped, losing its only saves. */
export function forgetServerActiveGenerationRun(runId: string): void {
serverActiveGenerationRuns.delete(runId);
}
@@ -574,12 +516,8 @@ export function isServerActiveGenerationRun(runId: string): boolean {
return serverActiveGenerationRuns.has(runId);
}
-/**
- * Whether a restored assistant message may be shown as still generating.
- *
- * The corroboration gate: unfinished metadata says only that this reply once had a run,
- * never that the run is still alive.
- */
+/** Whether a restored assistant message may be shown as still generating. The corroboration
+ * gate: unfinished metadata says only that this reply once had a run. */
export function generationIsCorroboratedLive(
metadata: Record<string, unknown>,
threadId?: string,
@@ -587,18 +525,16 @@ export function generationIsCorroboratedLive(
const runId = metadata.generationRunId;
if (typeof runId !== "string") return false;
if (isLiveGenerationRun(runId) || isServerActiveGenerationRun(runId)) return true;
- // A follower already gave up on this run locally. Without this it keeps taking the
- // benefit of the doubt below, so every online/pageshow/visibility trigger starts
- // another follower that republishes the message as running and blocks the composer
- // again. Only the server naming the run live may revive it, and the two checks above
- // are exactly that.
+ // A follower already gave up on this run locally. Without this it keeps taking the benefit of
+ // the doubt below, so every online/pageshow/visibility trigger starts another follower that
+ // republishes the message as running. Only the server naming the run live may revive it.
if (
metadata.generationLocallyInterrupted === true &&
!TERMINAL.has(String(metadata.generationStatus) as StoredGenerationStatus)
) {
return false;
}
- // No answer for THIS thread is not a "no". Stay with the persisted status until this
- // thread's own read has landed; the recovery follower settles it from there.
+ // No answer for THIS thread is not a "no". Stay with the persisted status until this thread's
+ // own read has landed; the recovery follower settles it from there.
return threadId === undefined || !serverHasAnsweredActiveRuns(threadId);
}
diff --git a/studio/frontend/src/features/chat/utils/chat-history-revision.ts b/studio/frontend/src/features/chat/utils/chat-history-revision.ts
index 24480b9eb2f..6b88826ecfb 100644
--- a/studio/frontend/src/features/chat/utils/chat-history-revision.ts
+++ b/studio/frontend/src/features/chat/utils/chat-history-revision.ts
@@ -5,21 +5,17 @@
// same-document. Only that something changed is published, never a chat id or any text.
export const CHAT_HISTORY_REVISION_KEY = "unsloth_chat_history_revision";
-// Long enough to swallow a generation's per-chunk saves, short enough that a tab going
-// quiet publishes before anyone reads a stale row. Exported for the tests.
+// Long enough to swallow a generation's per-chunk saves, short enough that a tab going quiet
+// publishes before anyone reads a stale row. Exported for the tests.
export const CROSS_TAB_REVISION_DEBOUNCE_MS = 500;
let revisionWriteTimer: ReturnType<typeof setTimeout> | null = null;
-/**
- * Whether a same-document history event came from the coalesced streaming autosave rather than
- * a structural change.
- *
- * A listener that retires work on a history change needs the difference: chunk saves arrive
- * faster than any debounce, so treating one as structural starves that work for a whole
- * generation. Anything without the detail counts as structural, including the event the
- * cross-tab listener re-raises, since a revision write only lands once changes have settled.
- */
+/** Whether a same-document history event came from the coalesced streaming autosave rather than a
+ * structural change. A listener that retires work on a history change needs the difference:
+ * chunk saves arrive faster than any debounce, so treating one as structural starves that work
+ * for a whole generation. Anything without the detail counts as structural, including the event
+ * the cross-tab listener re-raises. */
export function isCoalescedHistoryEvent(event: Event): boolean {
return (event as CustomEvent<{ coalesce?: boolean }>).detail?.coalesce === true;
}
@@ -51,13 +47,10 @@ function clearPending(): boolean {
return true;
}
-/**
- * Publishes a history change to the other documents.
- *
- * `coalesce` is for the per-chunk streaming path alone, where a write per chunk would block
- * this tab and wake every other one. A structural change must not use it: sharing the
- * stream's quiet window leaves a deleted chat live in another tab for a whole generation.
- */
+/** Publishes a history change to the other documents. `coalesce` is for the per-chunk streaming
+ * path alone, where a write per chunk would block this tab and wake every other one. A
+ * structural change must not use it: sharing the stream's quiet window leaves a deleted chat
+ * live in another tab for a whole generation. */
export function publishChatHistoryRevision(coalesce: boolean): void {
if (!coalesce) {
// Anything still waiting says no more than this does.
@@ -65,8 +58,7 @@ export function publishChatHistoryRevision(coalesce: boolean): void {
writeRevision();
return;
}
- // Rescheduled rather than left to run, so a generation collapses into one write instead
- // of one per debounce period.
+ // Rescheduled rather than left to run, so a generation collapses into one write instead of one per debounce period.
clearPending();
revisionWriteTimer = setTimeout(() => {
revisionWriteTimer = null;
diff --git a/studio/frontend/src/features/chat/utils/chat-history-storage.ts b/studio/frontend/src/features/chat/utils/chat-history-storage.ts
index c2bef6812c3..077f1c68d60 100644
--- a/studio/frontend/src/features/chat/utils/chat-history-storage.ts
+++ b/studio/frontend/src/features/chat/utils/chat-history-storage.ts
@@ -40,15 +40,10 @@ import {
} from "./chat-thread-tombstones";
import { ThreadRecordWriteCoordinator } from "./thread-record-write-coordinator";
-// Thread ids that belong to a temporary/incognito session. A thread is
-// tagged once, at creation (ensureThreadRecord, when the toggle is on), and
-// stays tagged for its whole lifetime -- the readers and writers below
-// consult this set, never the live toggle. That decoupling is what makes
-// mid-stream toggling safe: flipping the toggle can neither leak an
-// in-flight incognito run into history nor drop a normal thread's writes.
-// Per-thread reads short-circuit too (nothing is stored to fetch); only the
-// thread list stays ungated, so real history still loads next to a
-// temporary chat.
+// Thread ids belonging to a temporary/incognito session. A thread is tagged once at creation
+// and stays tagged for life; readers and writers consult this set, never the live toggle.
+// That is what makes mid-stream toggling safe. Per-thread reads short-circuit; only the
+// thread list stays ungated, so real history loads next to a temporary chat.
const incognitoThreadIds = new Set<string>();
export function markThreadIncognito(threadId: string): void {
@@ -67,11 +62,10 @@ type ThreadListArgs = {
includeArchived?: boolean;
};
-// localStorage perf-hint that the Dexie -> studio.db import already
-// finished. NOT the import gate -- the server-side ledger
-// (chat_legacy_imports) is the source of truth so a studio.db wipe stays
-// recoverable. The hint only short-circuits the listing paths' "also
-// surface Dexie threads?" branches once the ledger has covered everything.
+// localStorage perf-hint that the Dexie -> studio.db import finished. NOT the import gate:
+// the server-side ledger is the source of truth so a studio.db wipe stays recoverable.
+// The hint only short-circuits the listing paths' "also surface Dexie threads?" branches.
+// The server-side ledger is chat_legacy_imports.
const LEGACY_CHAT_IMPORT_KEY = "unsloth_chat_legacy_imported_to_studio_db";
let legacyChatImportPromise: Promise<void> | null = null;
@@ -79,8 +73,8 @@ let legacyChatImportPromise: Promise<void> | null = null;
// Bumped whenever a backend thread row is created or backfilled, so a listing can tell whether its read raced one.
let legacyChatImportGeneration = 0;
-// no browser-side ordering: the delete transaction tombstones before removing the row, so a
-// confirmed delete always beats a save that reaches sqlite later
+// No browser-side ordering: the delete transaction tombstones before removing the row, so a
+// confirmed delete always beats a save that reaches sqlite later.
const threadRecordWrites = new ThreadRecordWriteCoordinator(
(threadId) =>
new Error(
@@ -102,9 +96,8 @@ export function awaitStoredChatThreadWrites(threadId: string): Promise<void> {
return threadRecordWrites.settleCurrent(threadId);
}
-/** Start one background initializer for an id so the first message can render at once.
- * Returns the tracked write so a retry can adopt its outcome; callers that only start one
- * ignore it, and the rejection is always handled below. */
+/** Start one background initializer for an id so the first message renders at once. Returns
+ * the tracked write so a retry can adopt its outcome; the rejection is handled below. */
export function trackStoredChatThreadRecord(
threadId: string,
createRecord: () => Promise<void>,
@@ -130,8 +123,8 @@ export function trackStoredChatThreadRecord(
if (initializingThreadRecords.get(threadId) === work) {
initializingThreadRecords.delete(threadId);
}
- // a clear bumps the epoch before closing admission, so it retires this creator without
- // tombstoning a thread the clear may yet fail to remove
+ // A clear bumps the epoch before closing admission, so it retires this creator without
+ // tombstoning a thread the clear may yet fail to remove.
if (epoch === threadRecordClearEpoch && !isChatThreadDeleted(threadId)) {
failedThreadRecordByThreadId.set(threadId, createRecord);
}
@@ -344,8 +337,8 @@ function mergeMessages(
return { messages: Array.from(byId.values()), shouldSync };
}
-// point imports commit their row before the bump lands, so a listing waits for the ones already
-// in flight instead of trusting the generation alone
+// Point imports commit their row before the bump lands, so a listing waits on the ones
+// already in flight instead of trusting the generation alone.
const pendingLegacyThreadImports = new Set<Promise<unknown>>();
function importLegacyThread(
@@ -443,9 +436,8 @@ async function applyLegacyThreadBackfill(
}
}
-// Fast-path: check whether the "unsloth-chat" DB exists without opening
-// it. Supported on modern Chromium/Firefox/Safari; older browsers return
-// undefined and we fall through to the next probe.
+// Fast-path: check whether the "unsloth-chat" DB exists without opening it. Older browsers
+// return undefined and fall through to the next probe.
async function dexieDbAbsent(): Promise<boolean> {
if (typeof indexedDB === "undefined") return true;
const dbs = (indexedDB as IDBFactory).databases;
@@ -462,8 +454,7 @@ async function dexieDbAbsent(): Promise<boolean> {
}
}
-// Fast-path: Dexie exists but is empty. count() reads the IndexedDB
-// store metadata, not the rows -- cheap regardless of record count.
+// Fast-path: Dexie exists but is empty. count() reads store metadata, not rows.
async function dexieIsEmpty(): Promise<boolean> {
try {
const counts = await readLegacyStore<[number, number] | null>(
@@ -474,18 +465,15 @@ async function dexieIsEmpty(): Promise<boolean> {
const [threadCount, messageCount] = counts;
return threadCount === 0 && messageCount === 0;
} catch {
- // Dexie threw (corrupt DB / version mismatch / quota). Returning
- // false forces the slow path (same Dexie underneath); it'll throw
- // too and reset the import promise so the next caller can retry.
+ // Dexie threw (corrupt DB / version mismatch / quota). Returning false forces the slow path,
+ // which throws too and resets the import promise so the next caller can retry.
return false;
}
}
async function importLegacyChatsIfNeeded(): Promise<void> {
- // Session-level cache: repeated sidebar mounts in the same tab share
- // one import. localStorage is NOT consulted -- the server-side ledger
- // is the source of truth, so a studio.db wipe re-triggers the import
- // even if the browser kept its old hint.
+ // Session-level cache: repeated sidebar mounts share one import. localStorage is NOT
+ // consulted, so a studio.db wipe re-triggers the import even with a stale hint.
if (legacyChatImportPromise) return legacyChatImportPromise;
legacyChatImportPromise = (async () => {
@@ -495,16 +483,13 @@ async function importLegacyChatsIfNeeded(): Promise<void> {
return;
}
- // Fast-path: Dexie exists but is empty (already migrated long
- // ago and Dexie just hasn't been GC'd, or the browser created an
- // empty DB for some reason).
+ // Fast-path: Dexie exists but is empty (migrated long ago, or the browser created an empty DB).
if (await dexieIsEmpty()) {
markLegacyChatImportDone();
return;
}
- // Slow path: diff Dexie against the server-side ledger and import
- // any threads not already recorded.
+ // Slow path: diff Dexie against the server-side ledger and import any threads not already recorded.
const legacyThreads = await readLegacyStore(
() => db.threads.toArray(),
null,
@@ -521,9 +506,8 @@ async function importLegacyChatsIfNeeded(): Promise<void> {
const unimportedIds: string[] = [];
const unimportedThreads: ThreadRecord[] = [];
- // "Unimported" = missing from the ledger. Include threads already in
- // the backend (without a ledger row) so the ledger gets backfilled
- // for old-FE-then-new-FE users; else the next launch re-diffs forever.
+ // "Unimported" = missing from the ledger. Include threads already in the backend without a
+ // ledger row so it gets backfilled, else the next launch re-diffs forever.
for (const thread of legacyThreads) {
if (isChatThreadDeleted(thread.id)) continue;
if (importedThreadIds.has(thread.id)) continue;
@@ -563,8 +547,8 @@ async function importLegacyChatsIfNeeded(): Promise<void> {
} else {
const saved = await saveLegacyChatThread(thread);
if (!saved) {
- // Record the authoritative deletion in the migration ledger too, so another browser's
- // stale Dexie copy does not keep attempting the same forbidden import.
+ // Record the authoritative deletion in the migration ledger too, so another browser's stale
+ // Dexie copy does not keep attempting the same forbidden import.
newlyImportedIds.push(thread.id);
continue;
}
@@ -596,13 +580,12 @@ async function importLegacyChatsIfNeeded(): Promise<void> {
try {
result = await recordChatImportLedger(newlyImportedIds);
} catch {
- // Network error: leave the perf hint alone so the next launch
- // retries. Import is idempotent via UPSERT, so no duplicates.
+ // Network error: leave the perf hint alone so the next launch retries. Import is idempotent
+ // via UPSERT, so no duplicates.
return;
}
- // Only flip the hint when the backend actually has the ledger. On
- // older deployments (404/405/501) it would lie ("import done" with an
- // empty ledger), defeating recovery after a studio.db wipe.
+ // Only flip the hint when the backend actually has the ledger. On older deployments
+ // (404/405/501) it would lie and defeat recovery after a studio.db wipe.
if (result.supported) {
markLegacyChatImportDone();
}
@@ -625,8 +608,8 @@ export async function getStoredChatThreadReadResult(
threadId: string,
options: { bounded?: boolean; timeoutMs?: number; signal?: AbortSignal } = {},
): Promise<StoredChatThreadReadResult> {
- // Incognito threads are never stored, so the lookup can only come back
- // empty -- short-circuit it instead of doing a Dexie read + backend GET.
+ // Incognito threads are never stored, so the lookup can only come back empty -- short-circuit
+ // instead of a Dexie read plus backend GET.
if (isThreadIncognito(threadId)) {
return { thread: undefined, cacheable: true };
}
@@ -639,9 +622,8 @@ export async function getStoredChatThreadReadResult(
);
let backendThread: ThreadRecord | null;
try {
- // Bounded for a caller that is gating the UI on this read: an unbounded GET that
- // never answers leaves the request open for the life of the page, and every retry
- // opens another.
+ // Bounded for a caller gating the UI on this read: an unbounded GET that never answers leaves
+ // the request open for the life of the page, and every retry opens another.
backendThread = await getChatThread(threadId, {
bounded: options.bounded,
timeoutMs: options.timeoutMs,
@@ -680,22 +662,20 @@ export async function ensureStoredChatThread(
fallback?: ThreadRecord,
options: { bounded?: boolean; signal?: AbortSignal } = {},
): Promise<ThreadRecord | undefined> {
- // An incognito thread is never persisted, so there's genuinely nothing
- // to ensure -- skip the backend round-trips this would otherwise make
- // on every autosave (runStart/runEnd) and message append.
+ // An incognito thread is never persisted, so there is nothing to ensure -- skip the backend
+ // round-trips this would make on every autosave and message append.
if (isThreadIncognito(threadId)) return undefined;
if (isChatThreadDeleted(threadId)) return undefined;
- // Outcome ignored on purpose: adopting the failure here would skip the retryFailedThreadRecord
- // branch below for exactly the callers already waiting when the write rejected.
+ // Outcome ignored on purpose: adopting the failure here would skip retryFailedThreadRecord
+ // for exactly the callers already waiting when the write rejected.
await awaitStoredChatThreadWrites(threadId);
const legacyThread =
fallback ??
(await readLegacyStore(() => db.threads.get(threadId), undefined));
let backendThread: ThreadRecord | null;
try {
- // Bounded for a caller whose own request carries a deadline: this read runs BEFORE
- // it, so an unbounded one here means neither the caller's signal nor the write
- // timeout ever applies and the write chain behind it never settles.
+ // Bounded for a caller whose own request carries a deadline: this read runs BEFORE it, so an
+ // unbounded one means neither signal applies and the write chain never settles.
backendThread = await getChatThread(threadId, {
bounded: options.bounded,
signal: options.signal,
@@ -725,9 +705,9 @@ async function retryFailedThreadRecord(
}
if (createRecord) {
failedThreadRecordByThreadId.delete(threadId);
- // Through the same initializer path, so a retry that fails again stays retryable, and
- // rethrowing on purpose: a caller handed undefined reads it as "no row to update" and drops
- // its patch, which is how the prompt queue loses its model correction.
+ // Through the same initializer path, so a retry that fails again stays retryable. Rethrows on
+ // purpose: a caller handed undefined reads it as "no row to update" and drops its patch,
+ // which is how the prompt queue loses its model correction.
await trackStoredChatThreadRecord(threadId, createRecord);
} else {
await awaitStoredChatThreadWrites(threadId);
@@ -829,8 +809,8 @@ export async function listStoredChatThreads(
"threads" in backendResult ? backendResult.threads : undefined;
if (backendThreads) {
await importLegacyChatsIfNeeded().catch(() => undefined);
- // a point import can commit its row before its generation bump lands, so wait on the ones
- // already in flight rather than trusting the generation alone
+ // A point import can commit its row before its generation bump lands, so wait on the ones
+ // already in flight rather than trusting the generation alone.
await Promise.allSettled([...pendingLegacyThreadImports]);
if (legacyChatImportGeneration !== importGenerationBeforeRead) {
backendThreads = await listChatThreads(args).catch(() => backendThreads);
@@ -859,8 +839,7 @@ export async function listStoredChatThreadsWithMessages(
): Promise<ThreadRecord[]> {
const threads = await listStoredChatThreads(args);
if (threads.length === 0) return [];
- // One batched HTTP call instead of N. Per-thread legacy Dexie fallback
- // only fires when the batch result is empty.
+ // One batched HTTP call instead of N. Per-thread legacy Dexie fallback only fires when the batch result is empty.
const threadIds = threads.map((t) => t.id);
let backendByThread: Map<string, MessageRecord[]>;
try {
@@ -949,14 +928,13 @@ export async function moveStoredChatItemToProject(
);
}
-// Payloads the server answered 409 for, so the ~300ms autosave stops resending them.
-// Keyed by payload, not by id: a protected message can be refused transiently, when its
-// generationSeq lost a race, and blocking the id would drop the terminal write
-// (_safe_generation_assistant_update).
+// Payloads the server answered 409 for, so the ~300ms autosave stops resending them. Keyed by
+// payload, not id: a protected message can be refused transiently when its generationSeq
+// lost a race, and blocking the id would drop the terminal write.
const rejectedChatMessagePayloads = new Map<string, Map<string, string>>();
-// Entries hold whole messages, and only the delete paths clear them. Exceeding the cap
-// costs one extra request for whichever message fell out.
+// Entries hold whole messages, and only the delete paths clear them. Exceeding the cap costs
+// one extra request for whichever message fell out.
const MAX_REJECTED_PAYLOADS = 32;
/** Least-recently-written first, both across threads and within one. */
@@ -1060,8 +1038,8 @@ export async function syncStoredChatMessages(
if (isChatThreadDeleted(threadId)) return [];
await ensureStoredChatThread(threadId);
const synced = await syncChatMessages(threadId, messages, options);
- // Deleting rows frees their ids while the thread survives, so no tombstone runs. Gated on
- // an actual deletion: an ordinary sync runs constantly and would undo the whole cache.
+ // Deleting rows frees their ids while the thread survives, so no tombstone runs. Gated on an
+ // actual deletion: an ordinary sync runs constantly and would undo the whole cache.
if (options.pruneMissing || (options.deletedMessageIds?.length ?? 0) > 0) {
clearServerOwnedChatMessages();
}
@@ -1091,9 +1069,8 @@ export async function updateStoredChatThread(
options: { notify?: boolean; signal?: AbortSignal } = {},
): Promise<ThreadRecord | undefined> {
if (isThreadIncognito(threadId)) return undefined;
- // Same bound and same signal as the write it precedes: a stall here left the settings
- // write chain pending for the life of the page, and reopening or forking that chat
- // waits on that chain.
+ // Same bound and signal as the write it precedes: a stall here left the settings write chain
+ // pending for the life of the page, and reopening that chat waits on it.
const thread = await ensureStoredChatThread(threadId, undefined, {
bounded: true,
signal: options.signal,
@@ -1108,23 +1085,22 @@ export async function deleteStoredChatThreads(
args: { deleteFiles?: boolean } = {},
): Promise<string[]> {
// Incognito chats have no history row, but their ids still name sandboxes. Send every id to
- // the backend for file cleanup while limiting Dexie and write-coordinator work to stored chats.
+ // the backend for file cleanup while limiting Dexie work to stored chats.
idsToDelete = Array.from(new Set(idsToDelete));
const ids = idsToDelete.filter((id) => !isThreadIncognito(id));
if (idsToDelete.length === 0) return [];
let kept: string[] = [];
- // the backend tombstones every requested id in the transaction that deletes its row, so a save
- // reaching sqlite later is rejected rather than resurrecting the thread
+ // The backend tombstones every requested id in the transaction that deletes its row, so a
+ // save reaching sqlite later is rejected rather than resurrecting the thread.
try {
kept = await deleteChatThreads(idsToDelete, args);
} catch (error) {
// With only incognito ids there is no row whose absence can reconcile an ambiguous response.
if (ids.length === 0) throw error;
- // an aborted or dropped response is not proof the delete failed. the caller rolls its
- // tombstone back on a throw, and doing that for a row the backend did remove leaves the
- // thread 410 on every later write, so confirm the rows really survived first.
- // Bounded: the DELETE only got here by aborting on a wedged socket, and an unbounded read
- // would hang the delete instead. A read that cannot answer counts the row as surviving.
+ // An aborted response is not proof the delete failed. The caller rolls its tombstone back on
+ // a throw, and doing that for a row the backend did remove leaves the thread 410 on every
+ // later write, so confirm the rows really survived first. Bounded, since an unbounded read
+ // would hang the delete; a read that cannot answer counts the row as surviving.
const survived = await Promise.all(
ids.map((id) =>
getChatThread(id, { bounded: true }).then(
@@ -1175,8 +1151,7 @@ let clearStoredChatsPromise: Promise<ClearStoredChatsResult> | null = null;
export function clearStoredChats(
options: { deleteFiles?: boolean } = {},
): Promise<ClearStoredChatsResult> {
- // A clear already in flight wins: the dedupe is what keeps two clears from
- // racing, and only one caller can start one.
+ // A clear already in flight wins: the dedupe is what keeps two clears from racing, and only one caller can start one.
if (clearStoredChatsPromise) return clearStoredChatsPromise;
threadRecordClearEpoch += 1;
@@ -1221,15 +1196,15 @@ async function clearStoredChatsWithAdmissionClosed(
notify: false,
operationId,
deleteFiles: options.deleteFiles,
- // the transaction finds existing rows itself; these ids additionally fence legacy rows and
- // writes that have not committed yet
+ // The transaction finds existing rows itself; these ids additionally fence legacy rows and
+ // writes that have not committed yet.
tombstoneThreadIds: idsToFence,
});
try {
- // Retried once under the same operationId, still with admission closed. A request that timed
- // out is not proof its transaction did not run, and the retry takes the writer lock behind
- // that transaction and replays its recorded result, so admission cannot reopen into a window
- // where a new chat is created and then deleted by a clear that lands late.
+ // Retried once under the same operationId with admission still closed. A timeout is not proof
+ // the transaction did not run, and the retry takes the writer lock behind it and replays
+ // the recorded result, so admission cannot reopen into a window where a new chat is
+ // created and then deleted by a late clear.
const backendResult = await runBackendClear().catch(() =>
runBackendClear(),
);
@@ -1258,8 +1233,8 @@ async function clearStoredChatsWithAdmissionClosed(
);
result.legacy = legacyCleared ? "cleared" : "failed";
- // reported from the rows the backend says it removed, never from the fence set: an id fenced for
- // a write that never committed had no chat to delete
+ // Reported from the rows the backend says it removed, never from the fence set: an id fenced
+ // for a write that never committed had no chat to delete.
const allThreadIds = Array.from(
new Set([...legacyThreadIds, ...backendDeletedThreadIds]),
);
diff --git a/studio/frontend/src/features/chat/utils/chat-import.ts b/studio/frontend/src/features/chat/utils/chat-import.ts
index 80dff64bf29..dd4b23f86b2 100644
--- a/studio/frontend/src/features/chat/utils/chat-import.ts
+++ b/studio/frontend/src/features/chat/utils/chat-import.ts
@@ -1,10 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Imports Open WebUI JSON arrays, OpenAI/ShareGPT JSONL, and role/content CSV.
- * JSON records stream individually so large exports never become one JS string.
- */
+/** Imports Open WebUI JSON arrays, OpenAI/ShareGPT JSONL, and role/content CSV. JSON records
+ * stream individually so large exports never become one JS string. */
import { notifyChatHistoryUpdated } from "../api/chat-api";
import type { MessageRecord, ParsedConversation, ThreadRecord } from "../types";
@@ -39,12 +37,9 @@ const NATIVE_CHUNK_BYTES = 8 * 1024 * 1024;
/** Limit concurrent writes for exports that may contain thousands of chats. */
const WRITE_CONCURRENCY = 6;
-/**
- * Report progress at least this often by bytes as well as by conversations. A
- * count-only cadence leaves the toast reading "0 so far (0%)" for the entire
- * read of an export made of a few very large chats, which is the case the
- * progress toast exists for.
- */
+/** Report progress at least this often by bytes as well as by conversations. A count-only
+ * cadence leaves the toast reading "0 so far (0%)" for the entire read of an export made of
+ * a few very large chats, which is the case the toast exists for. */
const PROGRESS_BYTES = 4 * 1024 * 1024;
export interface ImportProgress {
@@ -78,12 +73,12 @@ async function* nativeBytes(handle: {
const bytes = await readNativeChatImportChunk(
handle.token,
offset,
- // Never past the size the picker recorded: bytes appended to a file that
- // is still being written are not part of the export that was chosen.
+ // Never past the size the picker recorded: bytes appended to a file still being written are not
+ // part of the export that was chosen.
Math.min(NATIVE_CHUNK_BYTES, handle.size - offset),
);
- // The picker recorded the size, so a short read means the file shrank since
- // then. Stopping quietly would pass a partial export off as the whole one.
+ // The picker recorded the size, so a short read means the file shrank since then. Stopping
+ // quietly would pass a partial export off as the whole one.
if (bytes.byteLength === 0) {
throw new Error(
`${handle.name} ended after ${offset} of ${handle.size} bytes; it changed after it was picked.`,
@@ -103,16 +98,16 @@ export function nativeImportSource(handle: {
return {
name: handle.name,
size: handle.size,
- // Decoding is fatal here because the native reader it replaced rejected
- // invalid UTF-8 outright rather than saving a chat full of U+FFFD.
+ // Decoding is fatal here because the native reader it replaced rejected invalid UTF-8 outright
+ // rather than saving a chat full of U+FFFD.
chunks: () => decodeTextChunks(nativeBytes(handle), true),
};
}
// Record conversion
-// role:"tool" results are absorbed into the preceding assistant tool-call
-// part's `result` field rather than becoming separate records.
+// role:"tool" results are absorbed into the preceding assistant tool-call part's `result` field
+// rather than becoming separate records.
function oaiContentToParts(raw: unknown): unknown[] {
if (!Array.isArray(raw)) {
return typeof raw === "string" && raw.trim()
@@ -175,8 +170,8 @@ function oaiMessagesToRecords(
const name = typeof fn.name === "string" ? fn.name : "unknown";
const argsStr = typeof fn.arguments === "string" ? fn.arguments : "{}";
let args: unknown = {};
- // _raw matches what the stream adapter and the backend keep for
- // arguments the model did not emit as valid JSON.
+ // _raw matches what the stream adapter and the backend keep for arguments the model did not
+ // emit as valid JSON.
try { args = JSON.parse(argsStr); } catch { args = { _raw: argsStr }; }
const result = toolResults.get(tcId);
parts.push({
@@ -241,8 +236,7 @@ function sharegptToRecords(
}
function csvToRecords(csvText: string, threadId: string, baseTs: number): MessageRecord[] {
- // parseCsv handles quoted newlines, so multi-line message content
- // round-trips from the exporter.
+ // parseCsv handles quoted newlines, so multi-line message content round-trips from the exporter.
const rows = parseCsv(csvText).slice(1);
const records: MessageRecord[] = [];
let prevId: string | null = null;
@@ -280,8 +274,7 @@ export function recordToConversation(
if (typeof record !== "object" || record === null) return null;
const obj = record as Record<string, unknown>;
- // Fresh ID: reusing the exported thread_id would clobber an existing
- // thread on import.
+ // Fresh ID: reusing the exported thread_id would clobber an existing thread on import.
const threadId = crypto.randomUUID();
const title = typeof obj.title === "string" ? obj.title : fallbackTitle;
const baseTs = typeof obj.created_at === "number" ? obj.created_at : Date.now();
@@ -355,8 +348,8 @@ async function writeConversation(
try {
await syncStoredChatMessages(threadId, messages, { pruneMissing: false });
} catch (error) {
- // The thread row is already in the sidebar. Left behind it is a blank chat
- // the user has to delete by hand, and a retry adds another one.
+ // The thread row is already in the sidebar. Left behind it is a blank chat the user has to
+ // delete by hand, and a retry adds another one.
await deleteStoredChatThreads([threadId]).catch(() => {});
throw error;
}
@@ -451,8 +444,8 @@ export async function importConversationsFromSource(
}
await Promise.allSettled(inFlight);
- // Those chats have to reach the sidebar even when the read failed, or the UI
- // stays empty until a reload and a retry duplicates every one of them.
+ // Those chats have to reach the sidebar even when the read failed, or the UI stays empty until
+ // a reload and a retry duplicates every one of them.
if (progress.imported > 0) notifyChatHistoryUpdated();
report();
diff --git a/studio/frontend/src/features/chat/utils/chat-search-history-hint.ts b/studio/frontend/src/features/chat/utils/chat-search-history-hint.ts
index 5c0e506af5d..3fe29c32dc5 100644
--- a/studio/frontend/src/features/chat/utils/chat-search-history-hint.ts
+++ b/studio/frontend/src/features/chat/utils/chat-search-history-hint.ts
@@ -1,16 +1,16 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Whether the last build found any chats, remembered across page loads. The index is built
-// only while the dialog is open, so without this the first open of a page load cannot tell
-// "no chats" from "not looked yet". A bare flag: never a count, never any chat text.
+// Whether the last build found any chats, remembered across page loads. The index is built only
+// while the dialog is open, so without this the first open of a page load cannot tell "no
+// chats" from "not looked yet". A bare flag: never a count, never any chat text.
const CHAT_SEARCH_HAS_ROWS_KEY = "unsloth_chat_search_has_rows";
-// Only the empty answer is aged out, and it carries the time it was written. Chats created
-// on another device or through the API never reach this tab, so emptiness cannot stay
+// Only the empty answer is aged out, and it carries the time it was written. Chats created on
+// another device or through the API never reach this tab, so emptiness cannot stay
// authoritative: a stale "has rows" costs a full-height dialog over a short history, while a
-// stale "empty" sizes a populated one compact and then grows it mid-open, which is the
-// stutter the hint exists to prevent. Long enough to survive a restart within a working day.
+// stale "empty" sizes a populated one compact and then grows it mid-open. Long enough to
+// survive a restart within a working day.
const EMPTY_HINT_TTL_MS = 12 * 60 * 60 * 1000;
function storage(): Storage | null {
@@ -43,12 +43,9 @@ export function forgetChatSearchHasRows(): void {
}
}
-/**
- * The last completed build's answer, or null when unknown: never built here, forgotten on a
- * session change, an empty answer past its window, or no localStorage. Unknown must stay
- * distinct from known-empty, since only known-empty can size compact without risking a
- * mid-open resize.
- */
+/** The last completed build's answer, or null when unknown: never built here, forgotten on a
+ * session change, an empty answer past its window, or no localStorage. Unknown must stay
+ * distinct from known-empty, since only known-empty can size compact without a mid-open resize. */
export function chatSearchHadRows(): boolean | null {
const store = storage();
if (!store) return null;
diff --git a/studio/frontend/src/features/chat/utils/chat-search-list-height.ts b/studio/frontend/src/features/chat/utils/chat-search-list-height.ts
index 01716113ff3..725cb7a7efa 100644
--- a/studio/frontend/src/features/chat/utils/chat-search-list-height.ts
+++ b/studio/frontend/src/features/chat/utils/chat-search-list-height.ts
@@ -1,15 +1,15 @@
// 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 dialog holds one list height for a whole open: compact only when the history is known
-// to have no rows, given up as soon as rows arrive, so filtering never resizes it. `hasRows`
-// must therefore answer for a history not yet indexed (see chatSearchIndexHasRows): reading
-// that as "no rows" sizes a populated dialog compact and then grows it mid-open.
+// The dialog holds one list height for a whole open: compact only when the history is known to
+// have no rows, given up as soon as rows arrive, so filtering never resizes it. `hasRows` must
+// therefore answer for a history not yet indexed (see chatSearchIndexHasRows): reading that as
+// "no rows" sizes a populated dialog compact and then grows it mid-open.
export function isCompactChatSearchList(
wasCompact: boolean,
hasRows: boolean | null,
): boolean {
- // null is "unknown", which only a completed build settles. Compact is wrong for a history
- // that turns out to have rows, so unknown takes the fixed height.
+ // null is "unknown", which only a completed build settles. Compact is wrong for a history that
+ // turns out to have rows, so unknown takes the fixed height.
return wasCompact && hasRows === false;
}
diff --git a/studio/frontend/src/features/chat/utils/chat-settings-storage.ts b/studio/frontend/src/features/chat/utils/chat-settings-storage.ts
index 8a93891d2c2..f29695e6834 100644
--- a/studio/frontend/src/features/chat/utils/chat-settings-storage.ts
+++ b/studio/frontend/src/features/chat/utils/chat-settings-storage.ts
@@ -175,10 +175,9 @@ function sanitizeInferenceParams(
return hasKeys(params) ? params : undefined;
}
-// Not capped. The server merge never removes keys and keeps an existing key in
-// its original position, so a load-time trim would permanently hide the oldest
-// entries: editing one of those models would write an update that the next
-// reload silently drops again. Entries are a dozen numbers each.
+// Not capped. The server merge never removes keys and keeps an existing key in its original
+// position, so a load-time trim would permanently hide the oldest entries: editing one of those
+// models would write an update the next reload silently drops. Entries are a dozen numbers each.
function sanitizeInferenceParamsByModel(
value: unknown,
): Record<string, PersistedInferenceParams> | undefined {
@@ -459,12 +458,9 @@ export function loadLegacyChatSettings(): PersistedChatSettings {
export interface LoadedChatSettings {
settings: PersistedChatSettings;
- /**
- * The GET answered, so a mirrored field missing from `settings` is missing on
- * the server too. False when the read fell back to this browser's legacy
- * storage: nothing is then known about the server, and treating every field
- * as absent would back this browser's stale values over another's.
- */
+ /** The GET answered, so a mirrored field missing from `settings` is missing on the server too.
+ * False when the read fell back to this browser's legacy storage: nothing is then known about the
+ * server, and treating every field as absent would back this browser's stale values over another's. */
fromServer: boolean;
/**
* Whether these values are what the server holds. False when a legacy import
diff --git a/studio/frontend/src/features/chat/utils/chat-thread-creation-claim.ts b/studio/frontend/src/features/chat/utils/chat-thread-creation-claim.ts
index e9d07a4465b..83d0696fb26 100644
--- a/studio/frontend/src/features/chat/utils/chat-thread-creation-claim.ts
+++ b/studio/frontend/src/features/chat/utils/chat-thread-creation-claim.ts
@@ -1,22 +1,14 @@
// 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 creation inputs a first send was made under, captured when the user pressed Send.
- *
- * `initialize()` snapshots project, temporary status, model and creation time when the
- * thread MATERIALIZES, which used to be the same tick as the send. Two things separated
- * them: `BaseComposerRuntimeCore.send()` awaits every incomplete attachment before
- * `handleSend` and Unsloth's PDF/DOCX/text adapters extract there, and the shared provider
- * (#8908) now survives a view switch. Navigating meanwhile moves all four -- the adapter is
- * rebuilt with the new project, and ChatPage's view effect clears `incognito` -- so a
- * document sent from a Temporary Chat, or from one project into another, was created under
- * wherever the user ended up. `ensureThreadRecord` already takes these as snapshot
- * parameters; this is what lets the caller supply them from the send.
- *
- * Per SEND, not per thread creation: `switchToNewThread()` reuses an untouched blank thread,
- * so one local id can be the current new thread in two views in a row.
- */
+/** The creation inputs a first send was made under, captured when the user pressed Send.
+ * `initialize()` snapshots project, temporary status, model and creation time when the thread
+ * MATERIALIZES, which used to be the same tick as the send. Two things separated them:
+ * `BaseComposerRuntimeCore.send()` awaits every incomplete attachment before `handleSend`, and
+ * the shared provider (#8908) survives a view switch. Navigating meanwhile moves all four, so a
+ * document sent from a Temporary Chat, or from one project into another, was created under
+ * wherever the user ended up. Per SEND, not per thread creation: `switchToNewThread()` reuses an
+ * untouched blank thread, so one local id can be the current new thread in two views in a row. */
export type ThreadCreationClaim = {
projectId: string | null;
incognito: boolean;
@@ -27,12 +19,10 @@ export type ThreadCreationClaim = {
const claimsByThreadId = new Map<string, ThreadCreationClaim & { claimedAt: number }>();
-/**
- * Bounded rather than consumed on read: `initialize()` and the run adapter both read a claim
- * with no ordering guarantee between them, so releasing on the first starves the second.
- * Expiry is safe -- a persisted row makes `ensureThreadRecord` early-return, a fresh thread
- * always gets a fresh local id, and every send overwrites the ids it stamps.
- */
+/** Bounded rather than consumed on read: `initialize()` and the run adapter both read a claim with
+ * no ordering guarantee between them, so releasing on the first starves the second. Expiry is
+ * safe: a persisted row makes `ensureThreadRecord` early-return, a fresh thread always gets a
+ * fresh local id, and every send overwrites the ids it stamps. */
const CLAIM_TTL_MS = 10 * 60 * 1000;
const MAX_CLAIMS = 64;
@@ -62,10 +52,8 @@ export function claimThreadCreation(
gc(now);
}
-/**
- * The whole claim, so a value OF null or false (no project, not temporary) stays
- * distinguishable from no claim: the first wins over the store, the second defers to it.
- */
+/** The whole claim, so a value OF null or false (no project, not temporary) stays distinguishable
+ * from no claim: the first wins over the store, the second defers to it. */
export function readThreadCreationClaim(
threadId: string,
): ThreadCreationClaim | undefined {
diff --git a/studio/frontend/src/features/chat/utils/chat-thread-tombstones.ts b/studio/frontend/src/features/chat/utils/chat-thread-tombstones.ts
index 4d2a0da653b..0c15f094c39 100644
--- a/studio/frontend/src/features/chat/utils/chat-thread-tombstones.ts
+++ b/studio/frontend/src/features/chat/utils/chat-thread-tombstones.ts
@@ -1,11 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Tombstones mask deleted threads in the Dexie read fallback. Each carries a
- * `deletedAt` so old entries can be GC'd, keeping localStorage bounded. Reads
- * accept both the legacy plain-string format and the {id, deletedAt} tuple.
- */
+/** Tombstones mask deleted threads in the Dexie read fallback. Each carries a `deletedAt` so old
+ * entries can be GC'd, keeping localStorage bounded. Reads accept both the legacy plain-string
+ * format and the {id, deletedAt} tuple. */
interface Tombstone {
id: string;
diff --git a/studio/frontend/src/features/chat/utils/chat-title.ts b/studio/frontend/src/features/chat/utils/chat-title.ts
index e7f28f839c4..c2bd6d0cd25 100644
--- a/studio/frontend/src/features/chat/utils/chat-title.ts
+++ b/studio/frontend/src/features/chat/utils/chat-title.ts
@@ -3,19 +3,17 @@
import type { MessageRecord, ThreadRecord } from "../types";
-/** Store the whole first line and let the sidebar clip it with CSS, so a wider
- * one shows more. Matches the rename input's maxLength: UTF-16 units, ellipsis
- * included. */
+/** Store the whole first line and let the sidebar clip it with CSS, so a wider one shows more.
+ * Matches the rename input's maxLength: UTF-16 units, ellipsis included. */
export const FALLBACK_TITLE_MAX = 120;
-/** Older titles were stored pre-cut at 48 chars with a literal "...". Kept to
- * find and rewrite those rows. */
+/** Older titles were stored pre-cut at 48 chars with a literal "...". Kept to find and rewrite those rows. */
export const LEGACY_FALLBACK_TITLE_MAX = 48;
const LEGACY_FALLBACK_SUFFIX = "...";
-/** Drop unpaired surrogates: they render as nothing, and one reaching the
- * backend fails its SQLite bind and 500s the title write. Iteration yields a
- * valid pair whole, so a length-1 unit in the range is a lone surrogate. */
+/** Drop unpaired surrogates: they render as nothing, and one reaching the backend fails its
+ * SQLite bind and 500s the title write. Iteration yields a valid pair whole, so a length-1
+ * unit in the range is a lone surrogate. */
function dropLoneSurrogates(text: string): string {
let out = "";
for (const character of text) {
@@ -32,8 +30,8 @@ function firstLineOf(text: string): string {
return dropLoneSurrogates(firstLine).replace(/\s+/g, " ").trim();
}
-/** Cut to at most `maxUnits` UTF-16 units without splitting an astral character:
- * a lone surrogate parses fine and then fails the backend's SQLite bind. */
+/** Cut to at most `maxUnits` UTF-16 units without splitting an astral character: a lone surrogate
+ * parses fine and then fails the backend's SQLite bind. */
function cutToUnits(text: string, maxUnits: number): string {
let out = "";
for (const character of text) {
@@ -93,16 +91,14 @@ export interface LegacyTitleRepair {
threadId: string;
/** The clipped title the rewrite is based on, guarding the write. */
previousTitle: string;
- /** The message the title came from, guarded too: deleting it must not leave
- * its text expanded into the title. */
+ /** The message the title came from, guarded too: deleting it must not leave its text expanded into the title. */
openingMessageId: string;
title: string;
}
export interface LegacyRepairPage {
candidates: ThreadRecord[];
- /** What this page skipped. The next page reads it, so a row this page failed
- * on is not redrawn by the same drain. */
+ /** What this page skipped. The next page reads it, so a row this page failed on is not redrawn by the same drain. */
rest: ThreadRecord[];
hasMore: boolean;
}
@@ -134,10 +130,9 @@ export function threadsMissingMessages(
return ids.filter((id) => (messagesByThreadId.get(id) ?? []).length === 0);
}
-/** Of those, the ones still worth retrying: no record of their import
- * finishing, so their messages may yet land. One the ledger knows is simply
- * empty, and retrying it would re-read it on every refresh for the session,
- * since its title stays clipped and keeps matching the pre-filter. */
+/** Of those, the ones still worth retrying: no record of their import finishing, so their
+ * messages may yet land. One the ledger knows is simply empty, and retrying it would re-read it
+ * on every refresh for the session, since its title stays clipped. */
export function threadsAwaitingImport(
ids: readonly string[],
messagesByThreadId: ReadonlyMap<string, MessageRecord[]>,
@@ -148,8 +143,8 @@ export function threadsAwaitingImport(
);
}
-/** Rows to rewrite. A title must be the exact old cut of its own first
- * message, so a rename ending in "..." is left alone. */
+/** Rows to rewrite. A title must be the exact old cut of its own first message, so a rename
+ * ending in "..." is left alone. */
export function planLegacyTitleRepairs(
threads: ThreadRecord[],
messagesByThreadId: Map<string, MessageRecord[]>,
diff --git a/studio/frontend/src/features/chat/utils/clear-all-chats.ts b/studio/frontend/src/features/chat/utils/clear-all-chats.ts
index fa9ee4b9389..9a6a65d34c1 100644
--- a/studio/frontend/src/features/chat/utils/clear-all-chats.ts
+++ b/studio/frontend/src/features/chat/utils/clear-all-chats.ts
@@ -10,9 +10,8 @@ import { stopChatThread } from "./stop-chat-thread";
export const countAllChats = countStoredChats;
export async function clearAllChats(options: { deleteFiles?: boolean } = {}) {
- // Invalidate fresh-thread initialization before either queue cancellation or
- // storage enumeration can yield. A late initializer must not recreate a chat
- // after this clear finishes.
+ // Invalidate fresh-thread initialization before either queue cancellation or storage enumeration
+ // can yield. A late initializer must not recreate a chat after this clear finishes.
chatHistoryClearBoundary.advance();
const { runningByThreadId, cancelByThreadId, serverCancelByThreadId } =
useChatRuntimeStore.getState();
diff --git a/studio/frontend/src/features/chat/utils/clipboard-payload.ts b/studio/frontend/src/features/chat/utils/clipboard-payload.ts
index 3a839e3b338..166250e58ab 100644
--- a/studio/frontend/src/features/chat/utils/clipboard-payload.ts
+++ b/studio/frontend/src/features/chat/utils/clipboard-payload.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Shared clipboard inspection. The file paste path and the long text paste path
-// must agree on what counts as a file, or a paste falls through both or neither.
+// Shared clipboard inspection. The file paste path and the long text paste path must agree on what
+// counts as a file, or a paste falls through both or neither.
function clipboardTypes(clipboardData: DataTransfer): string[] {
return Array.from(clipboardData.types, (type) => type.toLowerCase());
@@ -43,8 +43,8 @@ export function clipboardHasFileEntries(clipboardData: DataTransfer): boolean {
return Array.from(clipboardData.items).some((item) => item.kind === "file");
}
-// Native (Tauri) images and copied files are advertised by type only, with no
-// entry in files or items, and only pasteClipboardFiles can read them.
+// Native (Tauri) images and copied files are advertised by type only, with no entry in files or
+// items, and only pasteClipboardFiles can read them.
export function clipboardAdvertisesFiles(clipboardData: DataTransfer): boolean {
const types = clipboardTypes(clipboardData);
return (
diff --git a/studio/frontend/src/features/chat/utils/compare-pane-threads.ts b/studio/frontend/src/features/chat/utils/compare-pane-threads.ts
index ef6f95933a1..9e18d207fb7 100644
--- a/studio/frontend/src/features/chat/utils/compare-pane-threads.ts
+++ b/studio/frontend/src/features/chat/utils/compare-pane-threads.ts
@@ -38,10 +38,8 @@ export function resolveComparePaneThreadIds(
return { shape: null, first: undefined, second: undefined };
}
-/**
- * A persisted shape owns the renderer. Reclassifying it from the loaded checkpoint
- * relabels existing histories and can write the other comparison mode into them.
- */
+/** A persisted shape owns the renderer. Reclassifying it from the loaded checkpoint relabels
+ * existing histories and can write the other comparison mode into them. */
export function compareVariantForPair(
threads: ThreadRecord[],
checkpointIsLora: boolean | null,
@@ -63,11 +61,9 @@ export type CheckpointCompareClassInput = {
inventorySettled: boolean;
};
-/**
- * Is the loaded checkpoint a LoRA, meaning a base-vs-fine-tuned compare on the fast
- * simultaneous adapter-toggle path? `null` only while it is genuinely unclassified,
- * because that blanks the compare view.
- */
+/** Is the loaded checkpoint a LoRA, meaning a base-vs-fine-tuned compare on the fast simultaneous
+ * adapter-toggle path? `null` only while it is genuinely unclassified, since that blanks the
+ * compare view. */
export function checkpointCompareClass(
input: CheckpointCompareClassInput,
): boolean | null {
@@ -84,9 +80,9 @@ export function checkpointCompareClass(
return true;
}
if (input.inventorySettled) return false;
- // An explicit catalog row answers on its own. The deferred inventory could only add
- // an adapter row, which this one rules out, and chat defers it by 1.2s, so waiting
- // rendered a new pair blank for at least that long.
+ // An explicit catalog row answers on its own. The deferred inventory could only add an adapter
+ // row, which this one rules out, and chat defers it by 1.2s, so waiting rendered a new pair
+ // blank for at least that long.
return row ? false : null;
}
@@ -100,11 +96,9 @@ export type ComparePairReadState =
| { status: "unreadable" }
| { status: "ready"; variant: CompareVariant };
-/**
- * Every outcome of the pair read reaches a rendered state. A failure retries once and
- * then asks for a visible surface: leaving it unsettled renders nothing at all, and
- * settling it as an empty pair picks a renderer for a pair whose shape is unknown.
- */
+/** Every outcome of the pair read reaches a rendered state. A failure retries once and then asks
+ * for a visible surface: leaving it unsettled renders nothing at all, and settling it as an
+ * empty pair picks a renderer for a pair whose shape is unknown. */
export function comparePairReadState(
outcome: ComparePairReadOutcome,
checkpointIsLora: boolean | null,
diff --git a/studio/frontend/src/features/chat/utils/composer-draft.ts b/studio/frontend/src/features/chat/utils/composer-draft.ts
index 01f65af198d..60bbb6554e8 100644
--- a/studio/frontend/src/features/chat/utils/composer-draft.ts
+++ b/studio/frontend/src/features/chat/utils/composer-draft.ts
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Per-thread composer drafts persisted in localStorage. New (unsaved) chats
-// share the NEW_CHAT_DRAFT_ID slot; callers clear it when a fresh chat starts
-// so one new chat's draft never bleeds into the next.
+// Per-thread composer drafts persisted in localStorage. New (unsaved) chats share the
+// NEW_CHAT_DRAFT_ID slot; callers clear it when a fresh chat starts so one new chat's draft
+// never bleeds into the next.
const DRAFT_PREFIX = "chat-draft:";
const PASTE_DRAFT_PREFIX = "chat-draft-pastes:";
const NEW_CHAT_DRAFT_ID = "__new__";
@@ -12,16 +12,15 @@ export function composerDraftKey(threadId: string | null | undefined): string {
return `${DRAFT_PREFIX}${threadId ?? NEW_CHAT_DRAFT_ID}`;
}
-// Pasted attachments live in their own slot rather than inside the text draft,
-// so typing never rewrites a paste that can run to megabytes.
+// Pasted attachments live in their own slot rather than inside the text draft, so typing never
+// rewrites a paste that can run to megabytes.
export function composerPasteDraftKey(
threadId: string | null | undefined,
): string {
return `${PASTE_DRAFT_PREFIX}${threadId ?? NEW_CHAT_DRAFT_ID}`;
}
-// The names are not stored: a pasted file is named from its own text, so
-// recreating it reproduces the name it had.
+// The names are not stored: a pasted file is named from its own text, so recreating it reproduces the name it had.
export function readPasteDraft(key: string): string[] {
let raw: string | null = null;
try {
@@ -39,8 +38,8 @@ export function readPasteDraft(key: string): string[] {
}
}
-// A paste large enough to blow the storage quota throws here, leaving the text
-// draft untouched, which is why the two slots are written separately.
+// A paste large enough to blow the storage quota throws here, leaving the text draft untouched,
+// which is why the two slots are written separately.
export function writePasteDraft(key: string, pastes: readonly string[]): void {
try {
if (pastes.length > 0) {
@@ -53,8 +52,8 @@ export function writePasteDraft(key: string, pastes: readonly string[]): void {
}
}
-// All storage access is best-effort: localStorage throws when unavailable
-// (private mode, blocked storage) or full (quota), so swallow failures.
+// All storage access is best-effort: localStorage throws when unavailable (private mode, blocked
+// storage) or full, so swallow failures.
export function readComposerDraft(key: string): string | null {
try {
return window.localStorage.getItem(key);
diff --git a/studio/frontend/src/features/chat/utils/composer-send-guard.ts b/studio/frontend/src/features/chat/utils/composer-send-guard.ts
index 8fc5b9c80a8..abd71832b6e 100644
--- a/studio/frontend/src/features/chat/utils/composer-send-guard.ts
+++ b/studio/frontend/src/features/chat/utils/composer-send-guard.ts
@@ -1,17 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Refuses writes that put a just-sent message back into the composer: an event
- * the engine queued against the pre-send value (autocorrect commit, IME
- * finalise) or a draft autosave that raced the send. The next autosave makes
- * one stick, so the prompt sends and the text stays in the box.
- *
- * Retired by user intent, never by a clock: queue latency has no upper bound, so
- * a backgrounded page can deliver a stale write long after any window expired.
- * Typing retires it since a typed value differs, and a paste retires it
- * explicitly, being the one legitimate write identical to what was sent.
- */
+/** Refuses writes that put a just-sent message back into the composer: an event the engine
+ * queued against the pre-send value (autocorrect commit, IME finalise) or a draft autosave
+ * that raced the send. Retired by user intent, never by a clock: queue latency has no upper
+ * bound, so a backgrounded page can deliver a stale write long after any window expired.
+ * Typing retires it since a typed value differs, and a paste retires it explicitly. */
export type SentTextGuard = {
/** Values to refuse. The overlay send arms the wrapper and what was on screen. */
readonly texts: readonly string[];
@@ -32,11 +26,9 @@ export function armSentTextGuard(
};
}
-/**
- * Whether a keydown is the user starting to type again rather than part of the
- * send. Enter is excluded because the send arms the guard from inside that very
- * keydown, and a chord is excluded because it is a command, not a character.
- */
+/** Whether a keydown is the user starting to type again rather than part of the send. Enter is
+ * excluded because the send arms the guard from inside that very keydown, and a chord is
+ * excluded because it is a command, not a character. */
export function isGuardRetiringKey(event: {
key: string;
metaKey: boolean;
@@ -44,9 +36,9 @@ export function isGuardRetiringKey(event: {
altKey?: boolean;
getModifierState?: (key: "AltGraph") => boolean;
}): boolean {
- // AltGr types characters but Windows reports it as Ctrl+Alt, so the chord
- // check alone would drop every character produced with it. Both forms are
- // tested: some builds set the flags even while AltGraph reads true.
+ // AltGr types characters but Windows reports it as Ctrl+Alt, so the chord check alone would
+ // drop every character produced with it. Both forms are tested: some builds set the flags
+ // even while AltGraph reads true.
const altGraph =
event.getModifierState?.("AltGraph") === true ||
(event.ctrlKey && event.altKey === true && event.key.length === 1);
@@ -58,12 +50,9 @@ export function isGuardRetiringKey(event: {
return !["Shift", "Control", "Alt", "Meta", "CapsLock"].includes(event.key);
}
-/**
- * A keydown, or a composition starting for dictation, handwriting or an IME.
- * The send's queued writes are delivered before either, so one proves they have
- * drained. Relaxes the equality check only, never the autocorrect rule or the
- * draft suppression, so it cannot widen into the original bug.
- */
+/** A keydown, or a composition starting for dictation, handwriting or an IME. The send's queued
+ * writes are delivered before either, so one proves they have drained. Relaxes the equality
+ * check only, never the autocorrect rule or the draft suppression. */
export function markSentTextGuardUserInput(
guard: SentTextGuard | null,
): SentTextGuard | null {
@@ -85,29 +74,22 @@ export function applySentTextGuard(
guard: SentTextGuard | null,
write: {
value: string;
- /**
- * An autocorrect commit, the engine rather than the user. It cannot start
- * from an empty composer, so on one it is stale, mutated value and all.
- */
+ /** An autocorrect commit, the engine rather than the user. It cannot start from an empty
+ * composer, so on one it is stale, mutated value and all. */
replacesText: boolean;
- /**
- * Undo, redo, or text brought in from elsewhere. A queued write never
- * reports one, so it applies even when it restores what was sent.
- */
+ /** Undo, redo, or text brought in from elsewhere. A queued write never reports one, so it
+ * applies even when it restores what was sent. */
isDeliberate: boolean;
- /**
- * An IME composition write. Stale only when the composition began before
- * the send; one begun after raises compositionstart, which records user
- * input, so userInputSince separates the two.
- */
+ /** An IME composition write. Stale only when the composition began before the send; one begun
+ * after raises compositionstart, which records user input. */
isComposition: boolean;
composerIsEmpty: boolean;
},
): { accept: boolean; guard: SentTextGuard | null } {
if (guard === null) return { accept: true, guard: null };
if (write.isDeliberate) return { accept: true, guard: null };
- // Re-typing the whole prompt is only one write when it is one character, so
- // equality alone would swallow every retry of a "?" or a single emoji.
+ // Re-typing the whole prompt is only one write when it is one character, so equality alone
+ // would swallow every retry of a "?" or a single emoji.
if (guard.texts.includes(write.value)) {
if (guard.userInputSince) return { accept: true, guard: null };
return { accept: false, guard };
diff --git a/studio/frontend/src/features/chat/utils/confirm-stop-running-chats.ts b/studio/frontend/src/features/chat/utils/confirm-stop-running-chats.ts
index 6524b9e2a08..78aede718ad 100644
--- a/studio/frontend/src/features/chat/utils/confirm-stop-running-chats.ts
+++ b/studio/frontend/src/features/chat/utils/confirm-stop-running-chats.ts
@@ -14,7 +14,8 @@ import { listLocalPreStreamRunReservations } from "./pre-stream-run-reservation"
export interface StopRunningChatsDecision {
/** False when the user chose to keep generating; the caller must not load. */
proceed: boolean;
- /** Pass as `force_cancel_active`. True only after an explicit confirmation, so the backend's 409 still guards every other caller. */
+ /** Pass as `force_cancel_active`. True only after an explicit confirmation, so the backend's 409
+ * still guards every other caller. */
forceCancelActive: boolean;
/** Local-model prompt queues to stop immediately before the model-changing request. */
promptQueueThreadIds: string[];
@@ -32,18 +33,16 @@ export function getLocalPromptQueueThreadIds(): string[] {
];
}
-/**
- * Gate a model load / reload on local chats still generating or queued: they share one
- * llama-server, so a reload ends all of them. Ask first, then let the backend cancel active
- * runs and return the pending queue targets for the caller to stop after preflight.
- * External-provider chats and queues are left out of both.
- */
+/** Gate a model load or reload on local chats still generating or queued: they share one
+ * llama-server, so a reload ends all of them. Ask first, then let the backend cancel active
+ * runs and return the pending queue targets for the caller to stop after preflight.
+ * External-provider chats and queues are left out of both. */
export async function confirmStopRunningChatsIfNeeded(
action = "Loading a different model",
effect: StopRunningChatsEffect = "reload",
): Promise<StopRunningChatsDecision> {
- // Local runs only: an external-provider chat is not stopped by the swap, so counting it
- // would block a safe load behind a dialog. The backend excludes them for the same reason.
+ // Local runs only: an external-provider chat is not stopped by the swap, so counting it would
+ // block a safe load behind a dialog. The backend excludes them for the same reason.
const { runningByThreadId, localRunByThreadId } =
useChatRuntimeStore.getState();
let running = Object.entries(runningByThreadId)
@@ -91,8 +90,8 @@ export async function confirmStopRunningChatsIfNeeded(
let hasNonChat = false;
// Always merge the backend snapshot: runningByThreadId is this tab's memory, empty after a
- // reload and blind to a second tab, while force_cancel_active cancels every backend run.
- // The union stays local-only, since external-provider runs are never in it.
+ // reload and blind to a second tab, while force_cancel_active cancels every backend run. The
+ // union stays local-only, since external-provider runs are never in it.
try {
const active = await getActiveGenerations();
const entries = active.active ?? [];
@@ -101,16 +100,16 @@ export async function confirmStopRunningChatsIfNeeded(
merged.add(threadId);
}
running = [...merged];
- // Count conversations, not handles: one chat holds several at once while a tool
- // continuation registers its next leg before the previous unwinds, and active.count
- // counts those separately. A first turn started before its id was persisted has no
- // id to merge, so add those back or the prompt names fewer chats than will stop.
+ // Count conversations, not handles: one chat holds several at once while a tool continuation
+ // registers its next leg before the previous unwinds, and active.count counts those
+ // separately. A first turn started before its id was persisted has no id to merge, so add
+ // those back or the prompt names fewer chats than will stop.
const unnamed = entries.filter((entry) => !entry.thread_id).length;
count = entries.length
? running.length + unnamed + unnamedPreStreamRuns
: Math.max(active.count ?? 0, running.length) + unnamedPreStreamRuns;
- // Embeddings / completions / audio share the model but are not conversations, so the
- // prompt must not offer to stop chats that do not exist.
+ // Embeddings, completions and audio share the model but are not conversations, so the prompt
+ // must not offer to stop chats that do not exist.
hasNonChat = entries.some((entry) => (entry.kind ?? "chat") !== "chat");
} catch {
// Backend unreachable / older build: fall back to the local map only.
@@ -129,9 +128,9 @@ export async function confirmStopRunningChatsIfNeeded(
try {
const threads = await listStoredChatThreads();
const byId = new Map(threads.map((t) => [t.id, t]));
- // A compare conversation runs two pane threads, and the sidebar and the route both treat
- // it as one chat. Counting the raw ids asked to stop two and listed its title twice. Fold
- // panes onto their pairId, keeping the backend's count when it is higher.
+ // A compare conversation runs two pane threads, and the sidebar and the route both treat it as
+ // one chat. Counting the raw ids asked to stop two and listed its title twice. Fold panes onto
+ // their pairId, keeping the backend's count when it is higher.
const seen = new Set<string>();
for (const id of running) {
const thread = byId.get(id);
@@ -159,8 +158,8 @@ export async function confirmStopRunningChatsIfNeeded(
};
}
- // Deliberately no local stop: the backend holds the cancel until the load clears preflight,
- // so stopping now would truncate every chat even for a rejected load.
+ // Deliberately no local stop: the backend holds the cancel until the load clears preflight, so
+ // stopping now would truncate every chat even for a rejected load.
return {
proceed: true,
forceCancelActive: true,
diff --git a/studio/frontend/src/features/chat/utils/context-truncation.ts b/studio/frontend/src/features/chat/utils/context-truncation.ts
index 2d215c63e7d..327d36f6372 100644
--- a/studio/frontend/src/features/chat/utils/context-truncation.ts
+++ b/studio/frontend/src/features/chat/utils/context-truncation.ts
@@ -16,13 +16,10 @@ function spreadSum(
return { [key]: (a ?? 0) + (b ?? 0) };
}
-/**
- * Whether the fit removed turns from the prompt that was actually sent.
- *
- * Not `fits`: a fit under the physical window that misses the reply reserve still sends
- * the shortened prompt with `fits: false`, and those turns are just as gone. Every path
- * that returned the ORIGINAL messages reports `dropped_messages` as zero.
- */
+/** Whether the fit removed turns from the prompt that was actually sent. Not `fits`: a fit under
+ * the physical window that misses the reply reserve still sends the shortened prompt with
+ * `fits: false`, and those turns are just as gone. Paths returning the ORIGINAL messages
+ * report `dropped_messages` as zero. */
export function promptWasShortened(
truncation: ContextTruncation | undefined,
): truncation is ContextTruncation {
@@ -33,11 +30,10 @@ export function compactionBoundary(
truncation: ContextTruncation | undefined,
): number {
if (!promptWasShortened(truncation)) return 0;
- // boundary_messages is where the boundary sits in the saved transcript, and every fit
- // that evicts records one, rescues included. So the fallback is only for turns saved
- // before it existed, hence gated on `fits`, the one shape those turns have: elsewhere
- // dropped_messages is a per-refit total, not a position, and reading it as one sets a
- // high-water mark `showsNotice` never sees exceeded again.
+ // boundary_messages is where the boundary sits in the saved transcript, and every fit that
+ // evicts records one. So the fallback is only for turns saved before it existed, hence gated
+ // on `fits`: elsewhere dropped_messages is a per-refit total, not a position, and reading it
+ // as one sets a high-water mark `showsNotice` never sees exceeded again.
return (
truncation.boundary_messages ??
(truncation.fits ? (truncation.dropped_messages ?? 0) : 0)
@@ -52,11 +48,10 @@ function nonNegativeInt(value: number | undefined): number {
export function latestTurnOwnTokens(
truncation: ContextTruncation | null | undefined,
): number {
- // `latest_turn_tokens` prices a whole rendered PROMPT: template wrapper plus, on a
- // tool-enabled request, the entire tool catalogue. That sits inside `irreducible_tokens`
- // too and does not cancel (built-in catalogue alone is over a thousand tokens, MCP tools
- // uncapped), so a 6-token "hi" was reported as thousands "on its own".
- // `shared_prompt_tokens` is that floor, measured by the server on an empty prompt.
+ // `latest_turn_tokens` prices a whole rendered PROMPT: template wrapper plus, on a tool-enabled
+ // request, the entire tool catalogue. That sits inside `irreducible_tokens` too and does not
+ // cancel (the built-in catalogue alone is over a thousand tokens), so a 6-token "hi" was
+ // reported as thousands. `shared_prompt_tokens` is that floor, measured on an empty prompt.
const latest = nonNegativeInt(truncation?.latest_turn_tokens);
// Never the whole turn: reporting it as zero tokens is a worse lie than the old one.
const shared = Math.min(
@@ -71,14 +66,13 @@ export function latestTurnIsTheProblem(
budget: number,
): boolean {
if (!truncation) return false;
- // `latest_turn_exact: false` means nothing could price the turn, so the number is the
- // message's JSON at four characters a token while every other number here is a
- // tokenizer count of a rendered prompt: 16,400 characters of newlines estimate 8,207
- // against 557 rendered. A turn the template renders as nothing is NOT this case (the
- // server prices that by difference, exact). Absent means a server predating the flag.
+ // `latest_turn_exact: false` means nothing could price the turn, so the number is the message's
+ // JSON at four characters a token while every other number here is a tokenizer count of a
+ // rendered prompt: 16,400 characters of newlines estimate 8,207 against 557 rendered. A turn
+ // the template renders as nothing is NOT this case. Absent means a server predating the flag.
if (!(truncation.latest_turn_exact ?? true)) return false;
- // Measured WITHOUT the shared floor, so a turn only over budget once a tool catalogue
- // stands beside it is not blamed. An older server sends no floor, which reads as zero.
+ // Measured WITHOUT the shared floor, so a turn only over budget once a tool catalogue stands
+ // beside it is not blamed. An older server sends no floor, which reads as zero.
return latestTurnOwnTokens(truncation) > budget;
}
@@ -86,11 +80,11 @@ export function historyCannotHelp(
truncation: ContextTruncation | null | undefined,
): boolean {
if (!truncation) return false;
- // `irreducible_tokens` prices what survives dropping every evictable group: template
- // wrapper, tool catalogue, system turns and the newest turn. At or over the WINDOW,
- // llama-server refuses on size alone however short the conversation gets, so "start a
- // new chat" opens a chat that fails identically. Below it, shortening really can work
- // (the fit refuses at `prompt_target` but passes the untrimmed messages on).
+ // `irreducible_tokens` prices what survives dropping every evictable group: template wrapper,
+ // tool catalogue, system turns and the newest turn. At or over the WINDOW, llama-server
+ // refuses on size alone however short the conversation gets, so "start a new chat" opens a
+ // chat that fails identically. Below it, shortening really can work.
+ // The fit refuses at `prompt_target` but passes the untrimmed messages on.
const irreducible = nonNegativeInt(truncation.irreducible_tokens);
const window = nonNegativeInt(truncation.context_length);
return irreducible > 0 && window > 0 && irreducible >= window;
@@ -110,30 +104,27 @@ export function mergeContextTruncation(
current.prompt_tokens_before ?? incoming.prompt_tokens_before,
prompt_tokens_after:
incoming.prompt_tokens_after ?? current.prompt_tokens_after,
- // A turn can compact more than once (the tool loop refits per iteration), so these
- // accumulate rather than taking the last chunk's value. Spread conditionally so a
- // plain rolling-window response keeps its old shape, with no archive keys set to
- // undefined.
+ // A turn can compact more than once (the tool loop refits per iteration), so these accumulate
+ // rather than taking the last chunk's value. Spread conditionally so a plain rolling-window
+ // response keeps its old shape.
...spreadSum("archived_messages", current.archived_messages, incoming.archived_messages),
...spreadSum("recalled_chunks", current.recalled_chunks, incoming.recalled_chunks),
};
- // boundary_messages needs no rule: it is absolute, so the spread above already keeps
- // the latest fit's value. Summing it is the bug it exists to fix. boundary_anchor rides
- // along with it for the same reason, and the two must come from the SAME fit.
+ // boundary_messages needs no rule: it is absolute, so the spread above already keeps the latest
+ // fit's value. Summing it is the bug it exists to fix. boundary_anchor rides along for the
+ // same reason, and the two must come from the SAME fit.
- // The irreducible diagnosis describes ONE fit that gave up, so an earlier failure
- // followed by a later success would otherwise leave those numbers on a result that fit.
- // Deleted rather than spread as undefined, which would put both keys on every ordinary
- // response; delete on an absent key is a no-op.
+ // The irreducible diagnosis describes ONE fit that gave up, so an earlier failure followed by a
+ // later success would otherwise leave those numbers on a result that fit. Deleted rather than
+ // spread as undefined, which would put both keys on every ordinary response.
if (incoming.fits) {
delete merged.irreducible_tokens;
delete merged.latest_turn_tokens;
- // Rides with the count it describes: alone it says nothing, and left behind it would
- // describe a number that is no longer there.
+ // Rides with the count it describes: alone it says nothing, and left behind it would describe a
+ // number that is no longer there.
delete merged.latest_turn_exact;
- // Likewise the floor: a stale one subtracted from a later fit's count moves the blame
- // rather than removing it.
+ // Likewise the floor: a stale one subtracted from a later fit's count moves the blame rather than removing it.
delete merged.shared_prompt_tokens;
}
return merged;
diff --git a/studio/frontend/src/features/chat/utils/continuation.ts b/studio/frontend/src/features/chat/utils/continuation.ts
index 0cf0c8acd1d..87dd2ecfff0 100644
--- a/studio/frontend/src/features/chat/utils/continuation.ts
+++ b/studio/frontend/src/features/chat/utils/continuation.ts
@@ -1,14 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Resuming a response that stopped early: Max Tokens ran out (`length`), Stop was
- * pressed (`cancelled`), or the stream was cut (`interrupted`).
- *
- * Continuing re-sends the conversation with the partial as the final assistant turn
- * plus `continue_final_message`, so the prompt ends mid-sentence and the model emits
- * the next token. The new text is appended to the partial.
- */
+/** Resuming a response that stopped early (`length`, `cancelled`, `interrupted`): the conversation
+ * is re-sent with the partial as the final assistant turn plus `continue_final_message`, so
+ * the prompt ends mid-sentence and the new text is appended to the partial. */
/** Why a turn ended before the model was done. */
export type IncompleteReason = "length" | "cancelled" | "interrupted";
@@ -48,13 +43,9 @@ export function readIncompleteInfo(metadata: unknown): IncompleteInfo | null {
return null;
}
-/**
- * assistant-ui's reason for each of ours. `length` is a truthful stop, not a failure:
- * mapping it to `error` paints MessagePrimitive.Error's red box and a Retry button over
- * a turn that already offers the Continue bar below it, on every reloaded max-tokens
- * answer including ones written before this feature existed. `interrupted` keeps `error`
- * on purpose -- a cut stream is the thing the user has to be told about.
- */
+/** assistant-ui's reason for each of ours. `length` is a truthful stop, not a failure: mapping it
+ * to `error` paints a red box and a Retry button over a turn that already offers the Continue
+ * bar. `interrupted` keeps `error` on purpose, since a cut stream must be told about. */
const STATUS_REASON: Record<
IncompleteReason,
"cancelled" | "length" | "error"
@@ -86,11 +77,9 @@ export function incompleteLabel(reason: IncompleteReason): string {
return INCOMPLETE_LABELS[reason];
}
-/**
- * Drop text the continuation repeated from the end of the partial. Local models
- * continue token-exactly, but a provider that ignores assistant prefill can restate
- * the last few words. Only a suffix the continuation opens with is removed.
- */
+/** Drop text the continuation repeated from the end of the partial: local models continue
+ * token-exactly, but a provider that ignores assistant prefill can restate the last few
+ * words. Only a suffix the continuation opens with is removed. */
export function stripContinuationOverlap(
partial: string,
continuation: string,
@@ -107,11 +96,8 @@ export function stripContinuationOverlap(
return continuation;
}
-/**
- * True when the "continuation" is really a fresh answer. Judged on the partial's
- * opening, which a genuine continuation never reproduces; appending a restart would
- * read as a stutter, so the caller keeps it alone.
- */
+/** True when the "continuation" is really a fresh answer, judged on the partial's opening, which a
+ * genuine continuation never reproduces. */
export function isRestart(partial: string, continuation: string): boolean {
const head = partial.trimStart().slice(0, RESTART_PROBE);
if (head.length < RESTART_PROBE) {
@@ -121,12 +107,8 @@ export function isRestart(partial: string, continuation: string): boolean {
return continuation.trimStart().startsWith(head);
}
-/**
- * Merge a partial answer with its continuation.
- *
- * `streaming` skips the restart check, which needs more text than early chunks carry;
- * it runs once at the end.
- */
+/** Merge a partial answer with its continuation. `streaming` skips the restart check, which needs
+ * more text than early chunks carry; it runs once at the end. */
export function joinContinuation(
partial: string,
continuation: string,
@@ -141,36 +123,12 @@ export function joinContinuation(
return `${partial}${stripContinuationOverlap(partial, continuation)}`;
}
-/**
- * The merge a resumed turn applies to its cumulative text, streaming and at the end.
- *
- * The bug this fixes is `isRestart` running per arrival. It cannot fire until the continuation
- * reaches RESTART_PROBE characters, and the moment it does it publishes the continuation ALONE.
- * Over a 1602-character partial that took the value from 1649 characters to 48 in one arrival,
- * measured character by character. That is not only a visible collapse: Stop persists the last
- * STREAMED yield, because assistant-ui drops what a run yields after an abort and the terminal
- * merge sits behind `!abortSignal.aborted`, so stopping in that window SAVED the 48 characters
- * and discarded the whole partial the user was reading.
- *
- * `streaming` exists for exactly this and production never passed it. It suppresses the restart
- * check, which needs more text than early chunks carry, and leaves the overlap trim, which only
- * ever removes text the continuation itself repeated. So a streamed merge keeps the partial and
- * the new text, always, and a genuine restart is collapsed once at the end where the evidence
- * for it is complete.
- *
- * Two things were tried first and are recorded because both were worse:
- *
- * * repairing ONLY at the end. That saves `partial + repeated tail` on Stop, so a Continue
- * built on it replays the duplicate. It swapped a display bug for a stored one.
- * * holding the partial until the trim could no longer change (at `min(partial.length,
- * MAX_OVERLAP)`). Monotonic, but Stop inside that window persisted the STALE PARTIAL and
- * discarded every newly generated character, which is worse than either.
- *
- * The overlap trim can still shift the join by up to MAX_OVERLAP characters as a longer overlap
- * starts matching, so a publish is not strictly monotonic. That is bounded, rare, confined to
- * external-provider continuations, and never loses text. It is deliberately not worth holding
- * generated output back to avoid.
- */
+/** The merge a resumed turn applies to its cumulative text, streaming and at the end. `isRestart`
+ * must not run per arrival: it cannot fire until the continuation reaches RESTART_PROBE
+ * characters and then publishes the continuation ALONE, which on a 1602-character partial
+ * collapsed the value to 48 characters, and Stop persists the last STREAMED yield. So a
+ * streamed merge keeps both texts and a genuine restart is collapsed once at the end. The
+ * overlap trim can still shift the join by up to MAX_OVERLAP, which never loses text. */
export function createContinuationMerger(
partial: string,
repair: boolean,
@@ -185,14 +143,8 @@ export function createContinuationMerger(
};
}
-/**
- * Whether a finished MLX turn used its whole budget, i.e. stopped for length.
- *
- * MLX alone needs the inference: it reports finish_reason "stop" at the cap. Everyone
- * else reports "length" themselves, transformers included (from the token count
- * `generate` returned, which distinguishes an exhausted budget from a stop token that
- * happened to land at the cap), so inferring for them would relabel a finished answer.
- */
+/** Whether a finished MLX turn used its whole budget. MLX alone needs the inference: it reports
+ * finish_reason "stop" at the cap, while everyone else reports "length" themselves. */
export function budgetImpliesTruncation({
isMlx,
maxTokens,
@@ -212,13 +164,9 @@ export function budgetImpliesTruncation({
);
}
-/**
- * Whether an assistant turn can be resumed at all.
- *
- * A turn that called a tool cannot: the continuation runs as a sibling, so the call and
- * its result are absent from the outbound history and the model would be asked to carry
- * on from an answer whose evidence is missing. Matches the backend guard.
- */
+/** Whether an assistant turn can be resumed at all. A turn that called a tool cannot: the
+ * continuation runs as a sibling, so the call and its result are absent from the outbound
+ * history. Matches the backend guard. */
export function isContinuableContent(
content: readonly unknown[] | undefined,
): boolean {
@@ -241,11 +189,8 @@ export function isContinuableContent(
return hasText;
}
-/**
- * The newest Gemini text-part thoughtSignature on an assistant turn. The streaming
- * adapter pins it onto the final text part; the continuation carries it so the resumed
- * turn is replayed signed rather than as bare text.
- */
+/** The newest Gemini text-part thoughtSignature on an assistant turn, carried so the resumed turn
+ * is replayed signed rather than as bare text. */
export function readTextThoughtSignature(
content: readonly unknown[] | undefined,
): string | undefined {
@@ -267,15 +212,10 @@ export function readTextThoughtSignature(
return undefined;
}
-/**
- * Providers that reject a trailing assistant turn.
- *
- * Anthropic removed assistant prefill in Claude 4.6 and never allowed it with extended
- * thinking. Gemini requires a multiturn request to end in a user turn or a function
- * response. Mistral takes a prefill only on an assistant message carrying `prefix: true`,
- * which neither the outbound message type nor the backend schema has. All get the partial
- * plus an instruction turn instead, keeping the last message a user turn.
- */
+/** Providers that reject a trailing assistant turn: Anthropic removed prefill in Claude 4.6,
+ * Gemini requires a multiturn request to end in a user turn or function response, and Mistral
+ * needs `prefix: true`, which neither the outbound type nor the backend schema has. All get
+ * the partial plus an instruction turn instead. */
const PREFILL_REJECTING_PROVIDERS = new Set(["anthropic", "gemini", "mistral"]);
export function rejectsAssistantPrefill(
@@ -284,28 +224,19 @@ export function rejectsAssistantPrefill(
return providerType != null && PREFILL_REJECTING_PROVIDERS.has(providerType);
}
-/**
- * Self-hosted servers the backend sends the continuation flags to.
- *
- * Mirrors `_CONTINUATION_FLAG_PROVIDERS` in `core/inference/external_provider.py`: vLLM
- * and llama-server document `continue_final_message` + `add_generation_prompt`, so they
- * resume at the exact token boundary and need no overlap repair. Every other connection
- * may ignore the trailing assistant turn and restart.
- */
+/** Self-hosted servers the backend sends the continuation flags to. Mirrors
+ * `_CONTINUATION_FLAG_PROVIDERS` in external_provider.py: vLLM and llama-server document
+ * `continue_final_message` + `add_generation_prompt`, so they resume at the exact token
+ * boundary and need no overlap repair. */
const EXACT_RESUME_PROVIDERS = new Set(["vllm", "llama_cpp"]);
export function resumesExactly(providerType: string | undefined): boolean {
return providerType != null && EXACT_RESUME_PROVIDERS.has(providerType);
}
-/**
- * Whether the run this thread would start can resume a partial at all.
- *
- * These two answer from scratch before the continuation request is read: audio input
- * re-listens and an audio-output model regenerates the clip. Continue would restart, so
- * it is hidden. Armed Deep Research is not here: the model decides whether an armed turn
- * becomes research, and a turn that did is hidden by its run, not by the toggle.
- */
+/** Whether the run this thread would start can resume a partial at all. Audio input re-listens and
+ * an audio-output model regenerates the clip, so Continue would restart and is hidden. Armed
+ * Deep Research is not here: such a turn is hidden by its run, not by the toggle. */
export function modeAllowsContinuation({
fromAudioInput,
audioOutputModel,
@@ -327,10 +258,8 @@ export const CONTINUATION_RUN_CONFIG_KEY = "unslothContinuation";
export type ContinuationRequest = {
/** The partial answer to resume, exactly as it was rendered. */
partial: string;
- /**
- * Gemini text-part thoughtSignature from the turn being resumed. The sibling run drops
- * the original assistant message, so replaying it here keeps the history signed.
- */
+ /** Gemini text-part thoughtSignature from the turn being resumed: the sibling run drops the
+ * original assistant message, so replaying it here keeps the history signed. */
thoughtSignature?: string;
};
@@ -353,45 +282,21 @@ export function readContinuationRequest(
return null;
}
-/**
- * Resuming a Max Tokens cut WITHOUT asking.
- *
- * Hitting the cap is not a decision the user made; it is the reply running out of room
- * mid-sentence, and the only sensible answer to "the answer is not finished" is to finish
- * it. Every other reason is left alone: `cancelled` is the user pressing Stop, so
- * resuming would restart the thing they just stopped, and `interrupted` means the
- * connection dropped, where a silent retry can hide a broken link.
- *
- * Bounded, because a model that will not stop would otherwise loop forever, and each
- * round grows the transcript and drives compaction harder. After the budget is spent the
- * bar comes back and the user decides.
- */
+/** Resuming a Max Tokens cut WITHOUT asking: hitting the cap is not a decision the user made.
+ * Every other reason is left alone, since `cancelled` would restart what the user just
+ * stopped and `interrupted` can hide a broken link. Bounded, because a model that will not
+ * stop would loop forever and each round drives compaction harder. */
export const AUTO_CONTINUE_LIMIT = 3;
-/**
- * Rounds already spent per logical turn, keyed by the parent the continuation hangs off.
- *
- * Keyed on the PARENT, not the message: a continuation runs as a sibling of the turn it
- * resumes, so each round produces a new message id and a per-message counter would reset
- * every time and never reach its limit. The parent is the one id every round of the same
- * turn shares. In memory only; a reload is a fresh decision by a present user.
- */
+/** Rounds already spent per logical turn, keyed by the parent the continuation hangs off: a
+ * continuation runs as a sibling, so a per-message counter would reset every round and never
+ * reach its limit. In memory only. */
const spent = new Map<string, number>();
-/**
- * Whether this cut should resume on its own.
- *
- * `fits` and `promptTarget` come from the resumed turn's own truncation metadata, and
- * both exist because resuming FIGHTS the context window. A continuation replays the
- * partial as the final assistant turn, and the fit protects the final group, so the
- * partial is the one thing compaction may not evict. Once it is large enough that the
- * system turn plus the carried-forward block will not fit beside it, the request is
- * irreducible and every further round produces the same refusal.
- *
- * Measured at a 4,864-token context: a 3,217-token partial plus system and X came to
- * 4,218 against a 3,648-token target, so the answer could not be resumed at all -- and
- * three automatic rounds each asked anyway and each failed identically.
- */
+/** Whether this cut should resume on its own. `fits` and `promptTarget` come from the resumed
+ * turn's truncation metadata because resuming FIGHTS the context window: the partial is
+ * replayed as the final assistant turn, which the fit protects, so once it is large enough
+ * that the system turn will not fit beside it every further round refuses identically. */
export function shouldAutoContinue(
reason: IncompleteReason | null | undefined,
key: string | null | undefined,
@@ -414,12 +319,10 @@ export function shouldAutoContinue(
return false;
}
if (fits === false) {
- // Neither shape of `fits: false` can be resumed. A refusal never fitted, and a rescue
- // is reached only once eviction ran out of eligible turns, so its prompt is already
- // the floor. Nothing is left to evict either way, while a continuation replays the
- // partial as the final assistant turn, which the fit protects, asking for MORE prompt
- // against the same window: on a 460-of-500-token rescue a 10-character partial
- // already refuses.
+ // Neither shape of `fits: false` can be resumed: a refusal never fitted, and a rescue is reached
+ // only once eviction ran out of eligible turns. Nothing is left to evict, while a
+ // continuation asks for MORE prompt against the same window.
+ // On a 460-of-500-token rescue a 10-character partial already refuses.
return false;
}
if (
@@ -428,16 +331,15 @@ export function shouldAutoContinue(
promptTarget > 0 &&
partialTokens >= promptTarget
) {
- // The partial alone meets the budget, so nothing can be sent beside it -- not the
- // system prompt, not the user's own question. Raising Context Length is the only
- // remedy and the bar says so.
+ // The partial alone meets the budget, so nothing can be sent beside it. Raising Context Length is
+ // the only remedy and the bar says so.
return false;
}
return (spent.get(key) ?? 0) < limit;
}
-/** Record a round against `key`. Called before the run starts, so a run that fails to
- * produce anything still consumes its budget rather than retrying forever. */
+/** Record a round against `key`. Called before the run starts, so a run that fails to produce
+ * anything still consumes its budget. */
export function recordAutoContinue(key: string): void {
spent.set(key, (spent.get(key) ?? 0) + 1);
}
@@ -447,45 +349,20 @@ export function autoContinueCount(key: string | null | undefined): number {
return key ? (spent.get(key) ?? 0) : 0;
}
-/**
- * How long a lease survives without being renewed.
- *
- * A lease, not a permanent flag: the tab that wins can be closed or crash mid-run, and a
- * flag it never gets to clear would leave the message unresumable for the life of the
- * profile. The winner renews for as long as its run is live (see
- * `renewAutoContinueLease`), so the TTL is not a guess at how long a continuation takes:
- * it is how long a tab that stopped renewing is still believed to be alive.
- *
- * Three minutes because renewals are timers, and a hidden tab is throttled to roughly one
- * timer callback a minute in Chrome. Three minutes leaves a backgrounded winner about
- * three chances to check in before anyone else may touch its message, while a tab that
- * really died gives the message back inside the pause a user notices anyway. The manual
- * Continue button is never gated on any of this, so the way out is always open.
- */
+/** How long a lease survives without being renewed. A lease, not a permanent flag: the winning tab
+ * can crash mid-run, and a flag it never clears would leave the message unresumable for the
+ * life of the profile. Three minutes because a hidden tab is throttled to roughly one timer
+ * callback a minute. The manual Continue button is never gated on any of this. */
export const AUTO_CONTINUE_LEASE_TTL_MS = 180_000;
-/**
- * How often the holder renews while its run is live. Well inside the TTL even at the
- * once-a-minute rate a hidden tab's timers are throttled to.
- */
+/** How often the holder renews while its run is live. Well inside the TTL even at the
+ * once-a-minute rate a hidden tab's timers are throttled to. */
export const AUTO_CONTINUE_LEASE_RENEW_MS = 30_000;
-/**
- * How long a record stays behind once the run it covered has finished.
- *
- * A lease answers "is somebody running this message"; this answers "has this message
- * already been continued", which is what the per-tab claim set answers inside the tab that
- * won and what no other tab has any way of learning. Nothing tells a second tab that the
- * sibling was written: `notifyChatHistoryUpdated` dispatches a same-window event, no chat
- * key has a `storage` listener, and persisted messages are re-read only by `history.load`
- * when a thread is opened. So a tab still holding the pre-continuation branch in memory
- * takes the message straight back the moment the record goes -- on the next remount of the
- * bar, which leaving the chat and returning is enough to produce -- and the user pays for a
- * second continuation from a history that is missing the first.
- *
- * Long enough to outlive the sitting the continuation happened in, not permanent: the ids
- * are pruned like any other record, so the key cannot grow without bound.
- */
+/** How long a record stays behind once the run it covered has finished. A lease answers "is
+ * somebody running this message"; this answers "has this message already been continued",
+ * which nothing else tells a second tab, so a tab still holding the pre-continuation branch
+ * takes the message straight back the moment the record goes. Not permanent. */
export const AUTO_CONTINUE_CONTINUED_TTL_MS = 86_400_000;
/** The `localStorage` key holding the leases, one record per claimed message id. */
@@ -494,16 +371,9 @@ export const AUTO_CONTINUE_LEASE_KEY = "unsloth_chat_auto_continue_leases";
/** The Web Locks name every read-modify-write of that key is taken under. */
export const AUTO_CONTINUE_LOCK_NAME = "unsloth_chat_auto_continue_claim";
-/**
- * What a claim attempt did.
- *
- * Three answers, not a boolean, because the two ways of not starting a run want opposite
- * things on screen. `held-elsewhere` is another tab running this message: this one has to
- * drop the "Continuing automatically" spinner and put its manual Continue button back, or
- * it shows a spinner for a run it will never own. `skipped` is this tab declining its own
- * duplicate call -- a `<StrictMode>` replay, or a claim already in flight -- where the run
- * IS coming and changing what is on screen would be wrong.
- */
+/** What a claim attempt did. Three answers, not a boolean, because the two ways of not starting a
+ * run want opposite things on screen: `held-elsewhere` must drop the spinner and restore the
+ * manual Continue button, while `skipped` is this tab declining its own duplicate call. */
export type AutoContinueClaim = "started" | "skipped" | "held-elsewhere";
/** The slice of `Storage` the lease needs. Narrow so a test can hand over a fake. */
@@ -521,22 +391,14 @@ export type AutoContinueLockManager = {
type Lease = {
token: string;
expires: number;
- /**
- * The run this record covered reached a terminal state, so the message HAS been
- * continued. A plain lease that lapses means only that its holder stopped answering and
- * the message is free again; this one means the work is done and nobody should repeat it.
- */
+ /** The run this record covered reached a terminal state, so the message HAS been continued. A
+ * lapsed lease means only that its holder stopped answering; this means the work is done. */
done?: boolean;
};
-/**
- * The browser's own storage, or nothing when there is not one to read.
- *
- * Resolved per call rather than once at import: `localStorage` is absent under the test
- * runner and in SSR, and Safari's private mode throws on the property itself, not just on
- * a write. Every caller treats null as "no cross-tab seam" and falls back to the module
- * claim below, which is exactly what shipped before the lease existed.
- */
+/** The browser's own storage, or nothing when there is not one. Resolved per call rather than at
+ * import: `localStorage` is absent under the test runner and in SSR, and Safari's private
+ * mode throws on the property itself. Null falls back to the module claim. */
function browserLeaseStorage(): AutoContinueLeaseStorage | null {
try {
if (typeof window === "undefined") {
@@ -550,15 +412,10 @@ function browserLeaseStorage(): AutoContinueLeaseStorage | null {
}
}
-/**
- * The browser's lock manager, or nothing where there is not one.
- *
- * `navigator.locks` is what makes the claim exclusive: read, decide and write happen
- * inside one held lock, so no other tab can be between them. localStorage alone cannot do
- * this -- its individual operations are atomic, but a read-modify-write across three
- * statements is not, and the storage mutex that used to serialize such sequences was
- * dropped from the spec, so nothing holds a lock across them.
- */
+/** The browser's lock manager, or nothing where there is not one. `navigator.locks` is what makes
+ * the claim exclusive: localStorage's individual operations are atomic, but a
+ * read-modify-write across three statements is not, and the storage mutex that used to
+ * serialize such sequences was dropped from the spec. */
function browserLockManager(): AutoContinueLockManager | null {
try {
const locks = (globalThis.navigator as { locks?: unknown } | undefined)
@@ -599,14 +456,9 @@ function newLeaseToken(): string {
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
}
-/**
- * One tab's view of which messages have been continued.
- *
- * Constructible because a second instance is precisely what a second browser tab is: its
- * own module scope, its own empty claim set, the same saved thread, and the same storage
- * and lock manager underneath. That is the case the lease exists for, and the only way to
- * write it down as a test.
- */
+/** One tab's view of which messages have been continued. Constructible because a second instance is
+ * precisely what a second browser tab is: its own module scope and claim set over the same
+ * storage and lock manager, which is the case the lease exists for. */
export function createAutoContinueTab({
storage,
locks,
@@ -637,30 +489,18 @@ export function createAutoContinueTab({
forget: (messageId: string | null | undefined) => void;
reset: () => void;
} {
- /**
- * Messages this tab has already continued automatically, by message id.
- *
- * Separate from `spent`, which counts rounds per logical turn and is what bounds a
- * runaway loop. This answers a different question: has THIS message already been
- * resumed once. A component-local ref cannot, because it dies with the component.
- * Leave the chat with a truncated branch selected and come back, and the ref is fresh
- * while the parent still has budget, so the effect fires again and creates another
- * sibling and another paid request. Module scope outlives the remount; the ids are
- * short and bounded by how many replies a session truncates.
- */
+ /** Messages this tab has already continued automatically, by message id. Separate from `spent`,
+ * which counts rounds per turn: this asks whether THIS message has been resumed once. A
+ * component-local ref dies with the component, so leaving a truncated branch and returning
+ * fired the effect again and paid for another sibling. */
const continued = new Set<string>();
/** Claims this tab has in flight. The lock is async, so a second call can arrive. */
const claiming = new Set<string>();
- /**
- * Lease tokens this tab holds, by message id, each tagged with the holder that took it.
- *
- * Tagged, because "this tab" is not one runtime. Compare mode mounts two of them side by
- * side, each with its own thread and its own run, and either can be resuming a truncated
- * reply while the other is idle. A holder renews and releases what IT took; the pane that
- * finishes first must not hand the other pane's still-generating message to anyone.
- */
+ /** Lease tokens this tab holds, by message id, each tagged with the holder that took it. Tagged
+ * because "this tab" is not one runtime: compare mode mounts two side by side, and the pane
+ * that finishes first must not hand the other pane's still-generating message to anyone. */
const ownTokens = new Map<string, { token: string; holder: string }>();
const leaseSeam = (): AutoContinueLeaseStorage | null =>
@@ -669,15 +509,9 @@ export function createAutoContinueTab({
const lockSeam = (): AutoContinueLockManager | null =>
locks === undefined ? browserLockManager() : locks;
- /**
- * Run `mutate` with nobody else inside it.
- *
- * With a lock manager this is genuinely exclusive across tabs. Without one -- an older
- * browser, an embedded webview, the test runner -- it degrades to running the body
- * straight through, which is the write-then-read-back below and no worse than what
- * shipped before. A lock manager that rejects the request is treated the same way,
- * and a body that has already run is never run twice.
- */
+ /** Run `mutate` with nobody else inside it. With a lock manager this is exclusive across tabs;
+ * without one it degrades to running the body straight through, which is the
+ * write-then-read-back below. A body that has already run is never run twice. */
async function exclusively<T>(mutate: () => T, fallback: T): Promise<T> {
const manager = lockSeam();
if (!manager) {
@@ -694,14 +528,9 @@ export function createAutoContinueTab({
}
}
- /**
- * Drop what has lapsed, so the key cannot grow with every truncated reply.
- *
- * The one mutation here that is deliberately not per message. It only ever removes
- * records that are already dead by their own expiry -- `liveLease` reads them as free
- * whether they are in the key or not -- so it cannot end anybody's hold, and it is the
- * only thing bounding the size of a key every tab writes to.
- */
+ /** Drop what has lapsed, so the key cannot grow with every truncated reply. The one mutation
+ * deliberately not per message: it only removes records already dead by their own expiry, so
+ * it cannot end anybody's hold. */
function prune(
leases: Record<string, Lease>,
now: number,
@@ -730,13 +559,9 @@ export function createAutoContinueTab({
}
}
- /**
- * Take the lease, or report who has it. Runs inside the lock.
- *
- * A storage that cannot be read or written is not an error: the tab keeps the
- * module-only claim, which is what single-tab use has always relied on, rather than
- * refusing a continuation the user is waiting for.
- */
+ /** Take the lease, or report who has it. Runs inside the lock. A storage that cannot be read or
+ * written is not an error: the tab keeps the module-only claim rather than refusing a
+ * continuation the user is waiting for. */
function takeLease(
messageId: string,
now: number,
@@ -765,8 +590,8 @@ export function createAutoContinueTab({
next[messageId] = { token, expires: now + AUTO_CONTINUE_LEASE_TTL_MS };
try {
store.setItem(AUTO_CONTINUE_LEASE_KEY, JSON.stringify(next));
- // Read back, which is the only defence left when there is no lock manager: whoever
- // wrote last is the token in storage, and the other tab does not find its own.
+ // Read back, which is the only defence left when there is no lock manager: whoever wrote last is
+ // the token in storage, and the other tab does not find its own.
if (readLeases(store)[messageId]?.token !== token) {
return "held-elsewhere";
}
@@ -782,17 +607,10 @@ export function createAutoContinueTab({
return ownTokens.get(messageId)?.holder === holder;
}
- /**
- * Rewrite ONE lease with a new expiry, if this holder is the one that took it. Runs
- * inside the lock, and reports whether the lease is still this tab's to stamp.
- *
- * One message, never a holder's whole set. A holder is a runtime, and a runtime outlives
- * the run inside it: rounds of the same turn are claimed one after another under the same
- * holder, and the next round is claimed BEFORE the finished one is given back, because
- * React runs the bar's effect before the thread's. Anything that operated on "every lease
- * this holder owns" would settle the round that just started along with the round that
- * ended.
- */
+ /** Rewrite ONE lease with a new expiry, if this holder is the one that took it, and report whether
+ * it is still this tab's to stamp. One message, never a holder's whole set: rounds of the
+ * same turn share a holder, and the next round is claimed BEFORE the finished one is given
+ * back, so a holder-wide operation would settle the round that just started. */
function restamp(
messageId: string,
holder: string,
@@ -832,8 +650,8 @@ export function createAutoContinueTab({
return "skipped";
}
if (liveLease(messageId, now)) {
- // Cheap answer before touching the lock. Not recorded locally: if the tab holding
- // it dies, this one takes the message back when the lease lapses.
+ // Cheap answer before touching the lock. Not recorded locally: if the tab holding it dies, this
+ // one takes the message back when the lease lapses.
return "held-elsewhere";
}
claiming.add(messageId);
@@ -854,10 +672,8 @@ export function createAutoContinueTab({
if (!messageId) {
return false;
}
- // A claim of this tab's own that is still in flight deliberately does NOT count: the
- // spinner it is rendering is the right thing to show, and treating it as taken would
- // flash the manual bar in the middle of winning. Its duplicate calls are refused by
- // `claim` itself.
+ // A claim of this tab's own that is still in flight deliberately does NOT count: the spinner it is
+ // rendering is the right thing to show, and `claim` itself refuses its duplicate calls.
return continued.has(messageId) || Boolean(liveLease(messageId, now));
},
async renew(messageId, holder, { now = Date.now() } = {}) {
@@ -890,26 +706,15 @@ export function createAutoContinueTab({
),
false,
);
- // Held no longer, and marked done rather than handed back: the message HAS been
- // continued now, and the only thing that could hand it to another tab is a lease that
- // lapses, which is reserved for a holder that stopped answering. Only this message, so
- // the next round of the same turn -- claimed under this same holder before this
- // release lands -- keeps its own lease and its own renewals.
+ // Held no longer, and marked done rather than handed back: the message HAS been continued, and
+ // only a lapsing lease should hand it on. Only this message, so the next round of the same
+ // turn keeps its own lease and renewals.
ownTokens.delete(messageId);
},
- /**
- * Take back a claim whose run was never issued.
- *
- * Only this tab's own record of having continued the message. The storage lease is left
- * exactly where it is, to run out its own TTL: that is what a tab closed mid-claim
- * already leaves behind, and dropping it here would hand the message to a second tab
- * while this one may still be deciding what to do with it.
- *
- * Without this the message stays in `continued` for the life of the tab, and every
- * later claim of it -- the branch picker back to the truncated sibling, or returning to
- * the chat -- answers "skipped", so the automatic continuation never runs again for a
- * request that was never made.
- */
+ /** Take back a claim whose run was never issued. Only this tab's own record: the storage lease is
+ * left to run out its TTL, since dropping it here would hand the message to a second tab
+ * while this one may still be deciding. Without this the message stays in `continued` for the
+ * life of the tab and every later claim answers "skipped". */
forget(messageId) {
if (!messageId) {
return;
@@ -924,8 +729,8 @@ export function createAutoContinueTab({
ownTokens.clear();
return;
}
- // Synchronous, and so outside the lock: a reset is a test seam and a new thread
- // starting from zero, neither of which races another tab for this key.
+ // Synchronous, and so outside the lock: a reset is a test seam and a new thread starting from
+ // zero, neither of which races another tab.
try {
const leases = readLeases(store);
for (const [id, held] of ownTokens) {
@@ -945,13 +750,9 @@ export function createAutoContinueTab({
/** This tab. */
const tab = createAutoContinueTab();
-/**
- * Claim `messageId` for an automatic continuation, and say what happened.
- *
- * Asynchronous because exclusivity is: the read-decide-write is taken under a Web Lock so
- * that two tabs cannot both come out of it believing they won. The caller starts its run
- * on `started` and only on `started`.
- */
+/** Claim `messageId` for an automatic continuation, and say what happened. Asynchronous because
+ * exclusivity is: the read-decide-write is taken under a Web Lock so two tabs cannot both
+ * believe they won. The caller starts its run only on `started`. */
export function claimAutoContinue(
messageId: string | null | undefined,
holder: string,
@@ -959,43 +760,23 @@ export function claimAutoContinue(
return tab.claim(messageId, { holder });
}
-/**
- * Whether `messageId` is already being continued automatically -- by this tab, or by
- * another one whose lease is still live.
- *
- * Asked at render time, so the answer has to match what `claimAutoContinue` will do a tick
- * later as closely as a synchronous read can. It cannot be exact, because the lock that
- * decides a genuine race resolves later; the caller therefore also re-renders off the
- * claim's own answer rather than trusting this alone.
- */
+/** Whether `messageId` is already being continued automatically, by this tab or by another whose
+ * lease is still live. Asked at render time, so it cannot be exact; the caller also
+ * re-renders off the claim's own answer. */
export function wasAutoContinued(messageId: string | null | undefined): boolean {
return tab.claimed(messageId);
}
-/**
- * Give a claim back when the run it was taken for turned out never to be issued.
- *
- * The claim is what stops this tab continuing the same message twice, so it is only ever
- * dropped where nothing was started: the caller checked, in the same tick and off the same
- * store it starts the run from, that the message it claimed is no longer there to resume.
- * The cross-tab lease is deliberately untouched and lapses on its own.
- */
+/** Give a claim back when the run it was taken for turned out never to be issued: only ever where
+ * nothing was started, checked in the same tick off the same store. The cross-tab lease is
+ * deliberately untouched and lapses on its own. */
export function forgetAutoContinue(messageId: string | null | undefined): void {
tab.forget(messageId);
}
-/**
- * Keep ONE message's lease alive while the run resuming it is still going.
- *
- * Called on a timer for as long as that runtime's thread is running. Without it the TTL
- * would have to guess the longest continuation anyone might generate, and a slow local
- * model on a big Max Tokens would outlive that guess and have its message taken by another
- * tab mid-stream.
- *
- * Named by message and by holder, not by holder alone. The holder keeps two panes of
- * compare mode apart; the message id is what keeps one round of a turn apart from the next,
- * which the same holder claims moments later.
- */
+/** Keep ONE message's lease alive while the run resuming it is still going. Without it the TTL
+ * would have to guess the longest continuation anyone might generate. Named by message and by
+ * holder: the holder keeps two compare panes apart, and the message id keeps rounds apart. */
export function renewAutoContinueLease(
messageId: string,
holder: string,
@@ -1003,19 +784,10 @@ export function renewAutoContinueLease(
return tab.renew(messageId, holder);
}
-/**
- * Give ONE message's lease back when the run resuming it reaches a terminal state,
- * cancelled included.
- *
- * It is cut to the settle window rather than deleted, so a tab still showing the
- * pre-continuation branch cannot immediately start the duplicate this file exists to
- * prevent, and the full TTL is left to mean one thing only: the holder stopped answering.
- *
- * By message, because a holder can own two at once and they are not in the same state. A
- * turn that hits Max Tokens again is claimed by the bar's effect before this thread's
- * effect observes the first run ending, so a holder-wide release settles a round that has
- * only just begun, and one settle window later another tab takes it.
- */
+/** Give ONE message's lease back when the run reaches a terminal state, cancelled included. Cut to
+ * the settle window rather than deleted, so a tab still showing the pre-continuation branch
+ * cannot start a duplicate, and the full TTL means only that the holder stopped answering. By
+ * message, because a turn that hits Max Tokens again is claimed before the first run ends. */
export function releaseAutoContinueLease(
messageId: string,
holder: string,
@@ -1023,44 +795,20 @@ export function releaseAutoContinueLease(
return tab.release(messageId, holder);
}
-/**
- * Whether the thread a lease was taken for is generating right now.
- *
- * Deliberately NOT "is the thread on screen running". A run keeps streaming when the user
- * opens another chat -- the chat view is keyed by project so the provider is not remounted,
- * and the runtime just points its selection somewhere else -- so a lease whose lifetime is
- * read off the selected thread is released the moment the user looks away, and one settle
- * window later another tab can claim a message that is still being written. The signal has
- * to be per thread and indifferent to selection.
- */
+/** Whether the thread a lease was taken for is generating right now. Deliberately NOT "is the
+ * thread on screen running": a run keeps streaming when the user opens another chat, so a
+ * lease read off the selected thread is released the moment they look away. */
export type AutoContinueRunSignal = {
isRunning(threadId: string): boolean;
subscribe(onChange: () => void): () => void;
};
-/**
- * Holds the lease of each continuation this tab is running, for as long as its own run runs.
- *
- * A hold is (message, thread) and its lifetime is the run's: it arms when THAT thread starts
- * generating and is given back when THAT thread stops, whatever is on screen meanwhile and
- * whichever component has mounted or unmounted. Nothing here reads React state.
- *
- * Arming waits for a run that starts after the hold is taken. A round that hits Max Tokens
- * again is claimed while the finished round is still winding down, so a hold that armed on
- * "the thread is running" would arm on its predecessor's run and be released when THAT one
- * ended, mid-stream. So a hold taken while the thread is busy waits to see it idle first.
- *
- * A hold whose run has not appeared YET is kept and renewed, never timed out. No deadline
- * separates "this run is never coming" from "this run is still in preflight", because the
- * preflight has no bound: `chat-adapter.ts` awaits the thread's own settings pairing (up to
- * 30 seconds by itself), then `waitForModelReady`, which polls every 500ms for as long as a
- * model is loading -- minutes for a large local GGUF, and exactly the state a tab just
- * opened on a truncated reply is in. A fixed arming timeout dropped the hold in the middle
- * of that, its renewals stopped, and the lease lapsed under a run that had since started
- * streaming, so another tab could claim the message and pay for the same continuation twice.
- * Renewing instead costs only this: a claim whose run genuinely never starts holds its lease
- * until the tab is closed, and the manual Continue button is never gated on any of it.
- */
+/** Holds the lease of each continuation this tab is running, for as long as its own run runs. A
+ * hold is (message, thread) and arms when THAT thread starts generating; a hold taken while
+ * the thread is busy waits to see it idle first, or it would arm on its predecessor's run. A
+ * hold whose run has not appeared yet is renewed, never timed out: the preflight has no bound
+ * (settings pairing, then waiting while a large local GGUF loads), and a fixed arming timeout
+ * dropped the hold under a run that had since started streaming. */
export function createAutoContinueLeaseKeeper({
signal,
renew = (messageId, holder, now) => tab.renew(messageId, holder, { now }),
@@ -1109,10 +857,9 @@ export function createAutoContinueLeaseKeeper({
release(hold.messageId, hold.threadId, at);
continue;
}
- // Not armed yet, so its run is still in preflight, which has no upper bound (see the
- // note above this function). Kept and renewed rather than timed out: dropping it here
- // stopped the renewals while the run was still on its way, and the lease then lapsed
- // under a live continuation.
+ // Not armed yet, so its run is still in preflight, which has no upper bound. Kept and renewed
+ // rather than timed out: dropping it stopped the renewals while the run was on its way, and
+ // the lease then lapsed under a live continuation.
}
if (holds.size === 0 && unsubscribe) {
unsubscribe();
@@ -1131,29 +878,17 @@ export function createAutoContinueLeaseKeeper({
holds.set(key(messageId, threadId), {
messageId,
threadId,
- // Claimed while the thread is between runs, which is the ordinary case: the bar
- // only fires on a reply that has finished.
+ // Claimed while the thread is between runs, the ordinary case: the bar only fires on a reply that has finished.
idle: !signal.isRunning(threadId),
armed: false,
});
unsubscribe ??= signal.subscribe(observe);
},
observe,
- /**
- * `threadId`'s run failed on its way out, before it ever reached the run signal.
- *
- * The one thing that can end a hold which never armed, and it is a fact rather than a
- * deadline: the adapter threw, so this run is not slow, it is over. Not a timeout by
- * another name -- a preflight that is merely long emits nothing here and keeps its hold
- * and its renewals, which is the whole reason arming has no deadline.
- *
- * Armed holds are left alone. Their run did reach the signal, so the thread going idle
- * is what settles them, with the `done` marker that says the message HAS been continued
- * -- which a run that never produced a token has no right to write. This one only
- * discards: the lease it stops renewing lapses on its own TTL and the message comes
- * back to whoever is still looking at it, rather than being renewed for the life of the
- * tab and refused to every other one.
- */
+ /** `threadId`'s run failed on its way out, before it ever reached the run signal. The one thing
+ * that can end a hold which never armed, and a fact rather than a deadline: the adapter
+ * threw, so the run is over. Armed holds are left alone, since the thread going idle settles
+ * them with the `done` marker. This one only discards, so the lease lapses on its own TTL. */
failed(threadId) {
if (!threadId) {
return;
@@ -1172,8 +907,7 @@ export function createAutoContinueLeaseKeeper({
observe();
const at = now();
for (const hold of holds.values()) {
- // Armed or still waiting for its run: either way this tab is alive and expects to
- // be running that message, which is the whole question the lease asks.
+ // Armed or still waiting for its run: either way this tab is alive and expects to be running that message.
renew(hold.messageId, hold.threadId, at);
}
},
@@ -1188,21 +922,10 @@ export function createAutoContinueLeaseKeeper({
};
}
-/**
- * Whether THIS message is the one to continue automatically.
- *
- * `shouldAutoContinue` answers about the turn: is the reason right, does the partial
- * still fit, is the round budget unspent. It keeps saying yes after a message has been
- * claimed, because the budget is per turn and one spent round out of three leaves the
- * turn resumable, while the claim is per message and is what actually decides whether a
- * run starts. Anything rendering off the turn's answer alone therefore reports an
- * already-claimed message as continuing while `claimAutoContinue` refuses to start
- * anything: a spinner that never resolves, over the manual Continue button it replaces.
- *
- * Reachable without a reload. The continuation runs as a sibling and the branch picker
- * leads straight back to the truncated partial, and leaving the chat and returning lands
- * on it too whenever it is still the selected branch.
- */
+/** Whether THIS message is the one to continue automatically. `shouldAutoContinue` answers about
+ * the turn and keeps saying yes after a message has been claimed, since the budget is per
+ * turn while the claim is per message, so rendering off the turn's answer alone showed a
+ * spinner that never resolves over the manual Continue button. */
export function shouldAutoContinueMessage(
messageId: string | null | undefined,
reason: IncompleteReason | null | undefined,
@@ -1215,18 +938,9 @@ export function shouldAutoContinueMessage(
return shouldAutoContinue(reason, key, options);
}
-/**
- * Test seam; also lets a new thread start from zero.
- *
- * A full reset gives back the leases this tab wrote as well, so "start from zero" means
- * the same thing it did before the lease existed. Leases other tabs hold are left alone:
- * they are not this tab's to release, and a run they are driving is still going.
- *
- * Deliberately wholesale, unlike renewal and release, which are per holder. A run ending
- * is one runtime saying it is finished, and says nothing about the others; a reset is the
- * whole tab being told it has no claims at all, which is what a test between cases and a
- * cleared history both mean. Nothing in the run lifecycle calls it.
- */
+/** Test seam; also lets a new thread start from zero. A full reset gives back the leases this tab
+ * wrote, while leases other tabs hold are left alone. Deliberately wholesale, unlike renewal
+ * and release: a reset is the whole tab being told it has no claims. */
export function resetAutoContinue(key?: string): void {
if (key === undefined) {
spent.clear();
diff --git a/studio/frontend/src/features/chat/utils/conversation-markdown-export.ts b/studio/frontend/src/features/chat/utils/conversation-markdown-export.ts
index 9f6ad53a240..2b5228a4b69 100644
--- a/studio/frontend/src/features/chat/utils/conversation-markdown-export.ts
+++ b/studio/frontend/src/features/chat/utils/conversation-markdown-export.ts
@@ -29,8 +29,8 @@ type ConversationMarkdownExportDependencies<
readonly notifyNoContent: () => void;
};
-/** The markdown for one thread: null when it could not be loaded, empty when
- * it holds nothing exportable. Shared by the download and the copy shortcut. */
+/** The markdown for one thread: null when it could not be loaded, empty when it holds nothing
+ * exportable. Shared by the download and the copy shortcut. */
export function createConversationMarkdownBuilder<
Message extends StoredConversationMessage,
>({
@@ -48,9 +48,8 @@ export function createConversationMarkdownBuilder<
const normalizedMessages: ConversationMarkdownMessage[] = messages.map(
(message) => ({
role: String(message.role ?? ""),
- // Renderer markup, never prose: an exported answer must not carry raw
- // tokens. Here rather than in the exporter, so the copy chord strips
- // them too.
+ // Renderer markup, never prose: an exported answer must not carry raw tokens. Here rather than in
+ // the exporter, so the copy chord strips them too.
content: stripSearchImageTokens(renderMessage(message)),
}),
);
@@ -63,12 +62,9 @@ function headingText(title: string): string {
return title.replace(/\s+/g, " ").trim();
}
-/**
- * One document from several threads, each under its own heading. A compare row
- * is two models answering the same prompt, and the transcripts carry only role
- * headings, so unnamed halves cannot be told apart. A lone thread is left
- * exactly as the download writes it.
- */
+/** One document from several threads, each under its own heading. A compare row is two models
+ * answering the same prompt, and the transcripts carry only role headings, so unnamed halves
+ * cannot be told apart. A lone thread is left exactly as the download writes it. */
export async function buildNamedConversationsMarkdown(
conversations: readonly { readonly id: string; readonly title: string }[],
build: (threadId: string) => Promise<string | null>,
diff --git a/studio/frontend/src/features/chat/utils/conversation-markdown.ts b/studio/frontend/src/features/chat/utils/conversation-markdown.ts
index 96932d3aa95..6bff0817310 100644
--- a/studio/frontend/src/features/chat/utils/conversation-markdown.ts
+++ b/studio/frontend/src/features/chat/utils/conversation-markdown.ts
@@ -24,18 +24,17 @@ function roleLabel(role: string): string {
if (knownLabel) {
return knownLabel;
}
- // Imported transcripts keep their own role strings verbatim, and this one is
- // interpolated into a ## heading closeOpenBlocks never sees: a line break would
- // end the heading, a tag would open an element nothing later closes.
+ // Imported transcripts keep their role strings verbatim, and this one is interpolated into a
+ // ## heading closeOpenBlocks never sees: a line break would end the heading, a tag would
+ // open an element nothing later closes.
const label = role.replace(/[\s]+/g, " ").replace(/[<>&\\[\]`*_#]/g, "").trim();
return label.length > 0
? `${label[0]?.toUpperCase()}${label.slice(1)}`
: "Message";
}
-// A message split into renderable pieces. The JSONL/CSV exports flatten to plain
-// text, leaving tool calls as one-line JSON and thinking as [thinking] markers;
-// markdown is a document format, so each piece renders on its own terms.
+// A message split into renderable pieces. JSONL/CSV exports flatten to plain text; markdown
+// is a document format, so each piece renders on its own terms.
export type ConversationMarkdownBlock =
| { readonly kind: "text"; readonly text: string }
| { readonly kind: "thinking"; readonly text: string }
@@ -55,49 +54,41 @@ const INLINE_DATA_PLACEHOLDER = "[inline data omitted]";
const LINE_BREAK_PATTERN = /[\r\n]/;
// Generated audio is a custom tag holding a base64 data URI: player bytes, not transcript.
const AUDIO_DATA_URI_PATTERN = /<audio-player\s+src="data:[^"]*"\s*\/>/g;
-// Both ends, not just the closer: reasoning quoting a whole details element would
-// keep its opener and spend this block's closer, hiding every later turn.
+// Both ends, not just the closer: reasoning quoting a whole details element would keep its
+// opener and spend this block's closer, hiding every later turn.
const DETAILS_TAG_PATTERN = /<(\/?)(details)((?:\s[^>]*)?)>/gi;
-// A blank line and a ## heading separate turns, which is enough for the markdown
-// parser but not for the browser: an element left open keeps reading, so a message
-// that forgets a closing fence, comment or tag silently takes every later turn
-// inside it. Each message therefore closes what it opened.
-//
-// This is a line scanner over a fixed tag set, not a parser, and is meant to stay
-// one: it covers what people actually leave open (an unclosed fence, an
-// unterminated comment, a quoted details, a pasted script). A tag left unfinished
-// is neutralised rather than closed, since nothing can close it. Shapes needing a
-// real html tokenizer (processing instructions, self-closing svg or mathml) are
-// repaired best effort or not at all: the export is a document, not a security
-// boundary.
+// A blank line and a ## heading separate turns for the markdown parser but not the browser:
+// an element left open keeps reading, so each message closes what it opened. This is a
+// line scanner over a fixed tag set and is meant to stay one: it covers what people
+// actually leave open. An unfinished tag is neutralised rather than closed, since nothing
+// can close it. Shapes needing a real tokenizer are repaired best effort or not at all:
+// the export is a document, not a security boundary.
const FENCE_LINE_PATTERN = /^( {0,3})(`{3,}|~{3,})([\s\S]*)$/;
const INDENTED_CODE_PATTERN = /^(?: {4}|\t)/;
-// Equal-length delimiters, so a wide span may hold a narrower run: `` `x` `` is
-// one span, not two. CommonMark 6.1 reads a whole contiguous run as one delimiter,
-// so neither end may sit inside a longer one: ```x`` is live text, and masking it
-// would hide the tags it holds. The leading capture pins the opener to the start
-// of its run in place of a lookbehind, which Safari only gained in 16.4 and would
-// take the whole bundle down if unparseable.
+// Equal-length delimiters, so a wide span may hold a narrower run. CommonMark 6.1 reads a
+// contiguous run as one delimiter, so neither end may sit inside a longer one. The leading
+// capture pins the opener to the start of its run in place of a lookbehind, which Safari
+// only gained in 16.4 and would take the whole bundle down if unparseable.
const CODE_SPAN_PATTERN = /(^|[^`])(`+)(?!`)[\s\S]*?\2(?!`)/g;
// The html tokenizer ends a tag name at whitespace, a solidus or >.
const TAG_OPEN_PATTERN = /^<(\/?)([A-Za-z][^\s/>]*)/;
-// Block quote markers are structure, not content: a > that opens a quoted line
-// is not the > that finishes a tag left open on the line above.
+// Block quote markers are structure, not content: a > opening a quoted line is not the >
+// that finishes a tag left open above.
const BLOCKQUOTE_PATTERN = /^(?: {0,3}>)+/;
-// CommonMark 6.3: an angle-bracket destination is a url, so [x](<details>) is a
-// link, not an element; reading it as one emits or spends a closer wrongly.
+// CommonMark 6.3: an angle-bracket destination is a url, so [x](<details>) is a link, not an
+// element; reading it as one emits or spends a closer wrongly.
const LINK_DESTINATION_PATTERN = /\]\(\s*<[^<>]*>/g;
-// An image description becomes the alt attribute, escaped, so a tag in there
-// is text. A link label is not: CommonMark parses raw html inside one.
+// An image description becomes an escaped alt attribute, so a tag there is text. A link label
+// is not: CommonMark parses raw html inside one.
const IMAGE_DESCRIPTION_PATTERN = /!\[[^\]]*\]/g;
// A run left over once a line's closed spans are masked opens a span into the next.
const OPEN_RUN_PATTERN = /(^|[^`])(`+)(?!`)/;
-// Block structure is read before inlines, so a line starting with a tag begins
-// an html block and ends any span that was still open above it.
+// Block structure is read before inlines, so a line starting with a tag begins an html block
+// and ends any span still open above it.
const HTML_BLOCK_START_PATTERN = /^ {0,3}<[A-Za-z!/?]/;
-// A run only opens a span if its match arrives: an unmatched run is live text.
-// The search stops where a span would, at a blank line or an html block.
+// A run only opens a span if its match arrives; an unmatched run is live text. The search
+// stops where a span would, at a blank line or an html block.
function runClosesLater(lines: readonly string[], from: number, run: string): boolean {
const closer = new RegExp("(^|[^`])(" + run + ")(?!`)");
for (let index = from; index < lines.length; index += 2) {
@@ -108,9 +99,9 @@ function runClosesLater(lines: readonly string[], from: number, run: string): bo
}
return false;
}
-// Comments, processing instructions and cdata sections run to their own
-// terminator, not to a blank line or a tag, so one left open swallows the file.
-// ownLine: a ?> appended to the last line would land inside the instruction.
+// Comments, processing instructions and cdata run to their own terminator, not to a blank
+// line or tag, so one left open swallows the file. ownLine: a ?> appended to the last
+// line would land inside the instruction.
const LITERAL_BLOCKS = [
{ opener: "<!--", terminator: "-->", ownLine: false },
{ opener: "<?", terminator: "?>", ownLine: true },
@@ -118,14 +109,14 @@ const LITERAL_BLOCKS = [
] as const;
// One that opens and closes on the same line is literal text, not an opener.
const CLOSED_LITERAL_PATTERN = /<!--[\s\S]*?-->|<\?[\s\S]*?\?>|<!\[CDATA\[[\s\S]*?\]\]>/g;
-// Nothing at all closes plaintext: the browser reads every byte after it as
-// that element's text. The opener itself is the only thing that can be undone.
+// Nothing closes plaintext: the browser reads every byte after it as that element's text.
+// The opener itself is the only thing that can be undone.
const UNCLOSABLE_ELEMENTS: ReadonlySet<string> = new Set(["plaintext"]);
-// Containers need a blank line before their closer or it reads as a lazy
-// continuation; in a raw text element that blank line is content instead.
+// Containers need a blank line before their closer or it reads as a lazy continuation; in a
+// raw text element that blank line is content instead.
const CONTAINER_ELEMENTS: ReadonlySet<string> = new Set(["details", "select"]);
-// Read as text, not markup, until their own end tag, so a </details> inside one
-// must not close a details open outside it. pre is absent: it holds real markup.
+// Read as text until their own end tag, so a </details> inside one must not close a details
+// open outside it. pre is absent: it holds real markup.
const RAW_TEXT_ELEMENTS: ReadonlySet<string> = new Set([
"iframe",
"noembed",
@@ -136,21 +127,21 @@ const RAW_TEXT_ELEMENTS: ReadonlySet<string> = new Set([
"title",
"xmp",
]);
-// CommonMark start condition 1: inside one of these the markdown block runs to
-// the end tag, so a fence or an indented code line in there is literal text.
+// CommonMark start condition 1: inside one of these the block runs to the end tag, so a fence
+// or indented code line in there is literal text.
const CONDITION_1_ELEMENTS: ReadonlySet<string> = new Set([
"pre",
"script",
"style",
"textarea",
]);
-// CommonMark 2.4: a backslash before ascii punctuation makes it literal, so
-// \<script> opens nothing. Only an odd run escapes: an even match length with the <.
+// CommonMark 2.4: a backslash before ascii punctuation makes it literal, so \<script> opens
+// nothing. Only an odd run escapes.
const ESCAPED_LT_PATTERN = /\\+</g;
-// Elements a blank line does not close: the browser either reads their content as
-// text or holds them open as a container, so whatever follows is swallowed rather
-// than rendered. GFM's tagfilter lists nearly the same set for the same reason.
+// Elements a blank line does not close: the browser reads their content as text or holds them
+// open as a container, so whatever follows is swallowed. GFM's tagfilter lists nearly the
+// same set for the same reason.
const PERSISTENT_ELEMENTS: ReadonlySet<string> = new Set([
"details",
"iframe",
@@ -177,8 +168,8 @@ function closeOpenBlocks(text: string): string {
let indented = false;
// A code span may cross a soft line break, so an unclosed run keeps masking.
let span = "";
- // Tag starts whose > has not arrived, and the quote a value is inside. Every <
- // after an unfinished tag is inside it, until that one is neutralised.
+ // Tag starts whose > has not arrived, and the quote a value is inside. Every < after an
+ // unfinished tag is inside it until that one is neutralised.
let unfinished: { readonly part: number; readonly column: number }[] = [];
let quote = "";
const open: string[] = [];
@@ -198,8 +189,8 @@ function closeOpenBlocks(text: string): string {
line = " ".repeat(consumed) + line.slice(consumed);
block = null;
}
- // Fences, indents and tags mean different things inside a block quote, so read
- // them against the quoted content. Blanking, not slicing, keeps columns intact.
+ // Fences, indents and tags mean different things inside a block quote, so read them against
+ // the quoted content. Blanking, not slicing, keeps columns intact.
const marker = BLOCKQUOTE_PATTERN.exec(line)?.[0] ?? "";
const content = line.slice(marker.length);
// The quote ending ends the fence, so an unclosed one never reaches the next turn.
@@ -218,18 +209,18 @@ function closeOpenBlocks(text: string): string {
fence = { run, indent, quoted: marker !== "" };
continue;
}
- // A backtick opener with a backtick in its info string is not a fence: the line
- // is prose and falls through, since its tags are live. Indented code, code spans
- // and closed comments are literal, so a < in one opens nothing; but indented code
- // only starts after a blank line, else the line is a lazy continuation and live.
+ // A backtick opener with a backtick in its info string is not a fence: the line is prose and
+ // falls through, since its tags are live. Indented code, code spans and closed comments are
+ // literal, but indented code only starts after a blank line, else the line is a lazy
+ // continuation and live.
if (!unfinished.length && !literal) {
indented = indented
? blankLine || INDENTED_CODE_PATTERN.test(content)
: afterBlank && INDENTED_CODE_PATTERN.test(content);
if (indented) continue;
}
- // A span in progress swallows this line up to its closing run. A blank line
- // ends it, and so does an html block, which the parser sees first.
+ // A span in progress swallows this line up to its closing run. A blank line ends it, and so
+ // does an html block, which the parser sees first.
if (span && (blankLine || HTML_BLOCK_START_PATTERN.test(content))) span = "";
if (span) {
const closer = new RegExp("(^|[^`])(" + span + ")(?!`)").exec(line);
@@ -318,16 +309,16 @@ function closeOpenBlocks(text: string): string {
open.push(tag);
continue;
}
- // Innermost matching opener, so </div></details> still closes the details. A
- // closer with no opener is inert, so it must not license a later opener.
+ // Innermost matching opener, so </div></details> still closes the details. A closer with no
+ // opener is inert, so it must not license a later opener.
const index = open.lastIndexOf(tag);
if (index !== -1) open.splice(index, 1);
}
}
- // An unfinished tag cannot be closed: the tokenizer is still reading its name or
- // an attribute value, so a later </script> is swallowed as more of the same tag.
- // Neutralising the < is the only repair. Last first, so columns do not shift.
+ // An unfinished tag cannot be closed: the tokenizer is still reading its name or attribute
+ // value, so a later </script> is swallowed. Neutralising the < is the only repair.
+ // Last first, so columns do not shift.
escapes.push(...unfinished);
escapes.sort((a, b) => b.part - a.part || b.column - a.column);
for (const { part, column } of escapes) {
@@ -335,15 +326,15 @@ function closeOpenBlocks(text: string): string {
parts[part] = `${line.slice(0, column)}&lt;${line.slice(column + 1)}`;
}
- // Close on the body's own line ending. A renderer that ignores bare carriage
- // returns would read a \n-prefixed closer as a fresh fence instead.
+ // Close on the body's own line ending. A renderer that ignores bare carriage returns would
+ // read a \n-prefixed closer as a fresh fence instead.
const eol = !text.includes("\n") && text.includes("\r") ? "\r" : "\n";
let out = parts.join("");
if (block !== null) out += block.ownLine ? `${eol}${block.terminator}` : block.terminator;
// Indented to the opener's column: at column zero the closer would end its list.
if (fence !== null && !fence.quoted) out += `${eol}${fence.indent}${fence.run}`;
- // Innermost first, so closers nest as the openers did; each on its own line after
- // a blank one so it is a block, not a lazy continuation of the paragraph above.
+ // Innermost first, so closers nest as the openers did; each on its own line after a blank one
+ // so it is a block, not a lazy continuation.
for (let index = open.length - 1; index >= 0; index -= 1) {
const name = open[index] as string;
out += CONTAINER_ELEMENTS.has(name)
@@ -371,27 +362,26 @@ function inlineCode(raw: string): string {
0,
);
const ticks = "`".repeat(longestRun + 1);
- // Padding keeps an edge backtick from closing the span and an empty value from
- // collapsing into one delimiter run. CommonMark 6.1 also strips one space from each
- // end of a span padded at both, unless all spaces, so such a value needs a spare pair.
+ // Padding keeps an edge backtick from closing the span and an empty value from collapsing
+ // into one delimiter run. CommonMark 6.1 also strips one space from each padded end
+ // unless all spaces, so such a value needs a spare pair.
const stripped = value.startsWith(" ") && value.endsWith(" ") && value.trim() !== "";
const pad = !value || value.startsWith("`") || value.endsWith("`") || stripped ? " " : "";
return `${ticks}${pad}${value}${pad}${ticks}`;
}
-// Labels sit inside ** ** and [ ]: a line break ends either and lets the rest out
-// as markdown, a backtick opens a code span, and < starts inline html.
+// Labels sit inside ** ** and [ ]: a line break ends either and lets the rest out as
+// markdown, a backtick opens a code span, and < starts inline html.
function escapeMarkdownLabel(value: string): string {
// Not _: it cannot emphasise inside a word, so snake_case keys stay readable.
return value.replace(/[\r\n]+/g, " ").replace(/([\\[\]*`<])/g, "\\$1");
}
-// A destination is parsed, not copied: CommonMark 6.2 resolves entity references
-// inside it and 2.4 consumes a backslash before ASCII punctuation. Search results
-// are attacker-controllable and both rules move where the link goes:
-// `https://docs.unsloth.ai&commat;evil.test/` decodes to credentials on evil.test.
-// Only an entity-opening ampersand is escaped, so ordinary query separators stay
-// readable, and &amp; not %26 because encoding one folds it into the value before.
+// A destination is parsed, not copied: CommonMark 6.2 resolves entity references inside it
+// and 2.4 consumes a backslash before ASCII punctuation, and both move where the link goes
+// (`https://docs.unsloth.ai&commat;evil.test/` decodes to credentials on evil.test). Only
+// an entity-opening ampersand is escaped, and &amp; not %26 because encoding one folds it
+// into the value before.
const ENTITY_REFERENCE_PATTERN =
/&(?=(?:[A-Za-z][A-Za-z0-9]{1,31}|#\d{1,7}|#[Xx][0-9A-Fa-f]{1,6});)/g;
@@ -427,8 +417,8 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
-// A multi-line string is almost always source, so fence it as-is rather than let it
-// read as a JSON string full of \n. No language tag: guessing wrong is worse than none.
+// A multi-line string is almost always source, so fence it as-is rather than let it read as
+// a JSON string full of \n. No language tag: guessing wrong is worse than none.
function renderValue(label: string, value: unknown): string[] {
if (value === undefined) return [];
const escapedLabel = escapeMarkdownLabel(label);
diff --git a/studio/frontend/src/features/chat/utils/deep-research-handoff.ts b/studio/frontend/src/features/chat/utils/deep-research-handoff.ts
index 24181ee6014..4bf2afee67f 100644
--- a/studio/frontend/src/features/chat/utils/deep-research-handoff.ts
+++ b/studio/frontend/src/features/chat/utils/deep-research-handoff.ts
@@ -1,12 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Reading the model's Deep Research handoff off the tool events every loop publishes.
- *
- * `deep_research` is an ordinary tool to the three loops that run it, so there is no bespoke
- * frame to key on: the question rides on `tool_start` and the result on `tool_end`.
- */
+/** Reading the model's Deep Research handoff off the tool events every loop publishes.
+ * `deep_research` is an ordinary tool to the three loops that run it, so there is no bespoke
+ * frame to key on: the question rides on `tool_start` and the result on `tool_end`. */
/** The opening of the backend's `DEEP_RESEARCH_STARTED`, which is what says the call ran. */
export const DEEP_RESEARCH_STARTED_MARKER = "Deep Research has started";
@@ -39,13 +36,9 @@ export function newDeepResearchHandoff(): DeepResearchHandoff {
};
}
-/**
- * Fold one `deep_research` tool event into the handoff.
- *
- * @returns whether the adapter should swallow the event instead of drawing a tool card. An
- * ungated call is hidden on both halves: the research card is the reply, and a tool pill would
- * just precede it saying the same thing.
- */
+/** Fold one `deep_research` tool event into the handoff. @returns whether the adapter should
+ * swallow the event instead of drawing a tool card. An ungated call is hidden on both halves:
+ * the research card is the reply, and a tool pill would just say the same thing. */
export function readDeepResearchToolEvent(
handoff: DeepResearchHandoff,
event: DeepResearchToolEvent,
@@ -53,9 +46,9 @@ export function readDeepResearchToolEvent(
const callId =
typeof event.tool_call_id === "string" ? event.tool_call_id : "";
if (event.type === "tool_start") {
- // The local loop paints a provisional tool_start with empty arguments before the real
- // one, so only a start carrying a question is remembered, and only the first: a second
- // call in the same turn is the model repeating itself.
+ // The local loop paints a provisional tool_start with empty arguments before the real one, so
+ // only a start carrying a question is remembered, and only the first: a second call in the
+ // same turn is the model repeating itself.
const args = event.arguments;
const question =
args && typeof args === "object"
@@ -63,14 +56,14 @@ export function readDeepResearchToolEvent(
: "";
if (handoff.question === null && question) {
handoff.pendingCallId = callId;
- // Clamped, not refused: the endpoint rejects a longer one and the whole handoff would
- // fail, where a small model padding the question still asked something researchable.
+ // Clamped, not refused: the endpoint rejects a longer one and the whole handoff would fail,
+ // where a small model padding the question still asked something researchable.
handoff.pendingQuestion = Array.from(question)
.slice(0, DEEP_RESEARCH_QUESTION_MAX_CHARS)
.join("");
}
- // A gated call has to keep its Allow / Deny card: the loop blocks on a verdict, so
- // hiding the card asks the user nothing and the turn hangs there.
+ // A gated call has to keep its Allow / Deny card: the loop blocks on a verdict, so hiding the
+ // card asks the user nothing and the turn hangs there.
if (event.awaiting_confirmation === true) {
return false;
}
@@ -82,18 +75,18 @@ export function readDeepResearchToolEvent(
}
// The result is what says the tool ran. tool_end on its own does not: a denied, skipped,
// truncated or budget-exhausted call is closed by the same event, and handing off on one
- // researches a question the loop refused to pass on -- spending the chat's one run.
+ // researches a question the loop refused to pass on, spending the chat's one run.
const result = typeof event.result === "string" ? event.result : "";
if (
result.startsWith(DEEP_RESEARCH_STARTED_MARKER) &&
handoff.question === null
) {
- // The run starts on the question the model passed; if that could not be read, on the
- // user's own message, which is what research did before the model had a say.
+ // The run starts on the question the model passed; if that could not be read, on the user's own
+ // message, which is what research did before the model had a say.
handoff.question =
callId === handoff.pendingCallId ? handoff.pendingQuestion : "";
}
- // Close whatever this pair opened: a hidden start gets a hidden end, and a card the user
- // was asked to approve gets closed by the event that ends it.
+ // Close whatever this pair opened: a hidden start gets a hidden end, and a card the user was
+ // asked to approve gets closed by the event that ends it.
return handoff.hiddenCallIds.has(callId);
}
diff --git a/studio/frontend/src/features/chat/utils/delete-thread-message.ts b/studio/frontend/src/features/chat/utils/delete-thread-message.ts
index 312cf543e95..10c2663030b 100644
--- a/studio/frontend/src/features/chat/utils/delete-thread-message.ts
+++ b/studio/frontend/src/features/chat/utils/delete-thread-message.ts
@@ -1,17 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * assistant-ui exposes no public `deleteMessage` in our version, but
- * `MessageRepository` already does branch-safe deletion. We import it from
- * `@assistant-ui/core/internal` (the exported internal surface); avoid the
- * deeper `runtime/utils/message-repository` path since newer releases no
- * longer export arbitrary deep paths.
- *
- * Keep this file the only importer of `MessageRepository`. When bumping
- * `@assistant-ui/react` / `core`, re-run chat delete + reload smoke tests;
- * the path or API may change without a semver signal.
- */
+/** assistant-ui exposes no public `deleteMessage` in our version, but `MessageRepository` already
+ * does branch-safe deletion. Imported from `@assistant-ui/core/internal`, the exported internal
+ * surface; avoid the deeper `runtime/utils/message-repository` path, which newer releases no
+ * longer export. Keep this file the only importer, and re-run chat delete plus reload smoke
+ * tests when bumping `@assistant-ui/react`: the API may change without a semver signal. */
import { MessageRepository } from "@assistant-ui/core/internal";
import type {
CompleteAttachment,
@@ -29,10 +23,9 @@ import {
reconcileServerManagedMessages,
} from "./research-message-sync";
-// A copy of the list, not of what is in it. assistant-ui replaces parts and attachments
-// rather than mutating them, and the records built from these are serialized straight
-// into the PUT body, so a deep clone of the whole thread bought nothing and cost more
-// than the request that follows it.
+// A copy of the list, not of what is in it. assistant-ui replaces parts and attachments rather
+// than mutating them, and the records built from these are serialized straight into the PUT
+// body, so a deep clone of the whole thread bought nothing.
function snapshotContent(
content: ThreadMessage["content"],
): ThreadMessage["content"] {
@@ -112,16 +105,14 @@ async function withStoredResearchMessages(
return reconcileServerManagedMessages(records, stored);
}
-/**
- * Persist exported messages, pruning only for explicit delete flows.
- */
+/** Persist exported messages, pruning only for explicit delete flows. */
export async function syncExportedRepositoryToBackend(
remoteId: string,
exp: ExportedMessageRepository,
options: { pruneMissing?: boolean; deletedMessageIds?: string[] } = {},
): Promise<void> {
- // No ensureStoredChatThread here: syncStoredChatMessages ensures the row itself, and
- // this used to make every save pay for the same GET /threads/{id} twice.
+ // No ensureStoredChatThread here: syncStoredChatMessages ensures the row itself, and this used
+ // to make every save pay for the same GET /threads/{id} twice.
const records = exp.messages.map(({ message, parentId }) =>
exportedItemToRecord(remoteId, parentId, message),
);
@@ -140,9 +131,7 @@ type ThreadImportExport = {
import: (data: ExportedMessageRepository) => void;
};
-/**
- * Remove a message from the thread and mirror the result to backend storage.
- */
+/** Remove a message from the thread and mirror the result to backend storage. */
export async function deleteThreadMessage(args: {
thread: ThreadImportExport;
messageId: string;
diff --git a/studio/frontend/src/features/chat/utils/dictation-send.ts b/studio/frontend/src/features/chat/utils/dictation-send.ts
index 984195c7665..445ffc634ae 100644
--- a/studio/frontend/src/features/chat/utils/dictation-send.ts
+++ b/studio/frontend/src/features/chat/utils/dictation-send.ts
@@ -1,29 +1,19 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Whether the recording bar's send should submit once dictation ends.
- *
- * Only when this recording actually added text: silence and failed
- * transcription leave a pre-recording draft in place rather than sending it
- * half-finished.
- *
- * @param before composer text captured when send was pressed
- * @param after composer text once the session ended
- */
+/** Whether the recording bar's send should submit once dictation ends. Only when this recording
+ * actually added text: silence and failed transcription leave a pre-recording draft in place
+ * rather than sending it half-finished. @param before composer text when send was pressed
+ * @param after composer text once the session ended */
export function dictationProducedText(before: string, after: string): boolean {
const trimmed = after.trim();
return trimmed.length > 0 && trimmed !== before.trim();
}
-/**
- * Every state the composer's submit rejects.
- *
- * One definition for both jobs: greying out the recording bar's send, and
- * deciding whether a pending send waits rather than being spent on a submit
- * that would bounce. Text presence is not here, since the transcript supplies
- * it after the button is pressed.
- */
+/** Every state the composer's submit rejects. One definition for both jobs: greying out the
+ * recording bar's send, and deciding whether a pending send waits rather than being spent on a
+ * submit that would bounce. Text presence is not here, since the transcript supplies it after
+ * the button is pressed. */
export function dictationSendBlocked(state: {
/** The composer itself is unavailable. */
composerDisabled: boolean;
@@ -53,15 +43,10 @@ export function dictationSendBlocked(state: {
);
}
-/**
- * Whether a pending dictation send may submit now.
- *
- * Three ways it must not. The composer is reused across thread switches, so a
- * send pressed in one thread can land after a move to another, where it would
- * submit that thread's draft. The plus menu stays open to the user while
- * recording and can insert a saved prompt, which changes the text without any
- * speech. And silence leaves whatever was already there.
- */
+/** Whether a pending dictation send may submit now. Three ways it must not. The composer is reused
+ * across thread switches, so a send pressed in one thread can land after a move to another,
+ * where it would submit that thread's draft. The plus menu stays open while recording and can
+ * insert a saved prompt, changing the text without any speech. And silence leaves what was there. */
export function shouldSubmitDictation(input: {
/** Composer identity when send was pressed. */
originComposer: string;
diff --git a/studio/frontend/src/features/chat/utils/download-json.ts b/studio/frontend/src/features/chat/utils/download-json.ts
index 0c5ce00ff94..42f2a039e0e 100644
--- a/studio/frontend/src/features/chat/utils/download-json.ts
+++ b/studio/frontend/src/features/chat/utils/download-json.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Save `data` as a pretty-printed JSON file via a temporary object URL. Uses
-// only the standard Blob/anchor download path so it works in every browser.
+// Save `data` as a pretty-printed JSON file via a temporary object URL. Uses only the standard
+// Blob/anchor download path so it works in every browser.
export function triggerJsonDownload(data: unknown, filename: string): void {
const blob = new Blob([JSON.stringify(data, null, 2)], {
type: "application/json",
diff --git a/studio/frontend/src/features/chat/utils/export-chat-history.ts b/studio/frontend/src/features/chat/utils/export-chat-history.ts
index 6f641a9fa99..c45e48df4a8 100644
--- a/studio/frontend/src/features/chat/utils/export-chat-history.ts
+++ b/studio/frontend/src/features/chat/utils/export-chat-history.ts
@@ -27,8 +27,7 @@ export async function buildArchivedChatExport() {
return filterArchivedChatExport(await buildChatExport());
}
-// Download only the archived chats. Returns how many were exported; skips the
-// download entirely when there are none.
+// Download only the archived chats. Returns how many were exported; skips the download entirely when there are none.
export async function downloadArchivedChatExport(): Promise<number> {
const { data, archivedCount } = await buildArchivedChatExport();
if (archivedCount === 0) {
diff --git a/studio/frontend/src/features/chat/utils/fork-count-store.ts b/studio/frontend/src/features/chat/utils/fork-count-store.ts
index 02a9167b19c..38dcccc1606 100644
--- a/studio/frontend/src/features/chat/utils/fork-count-store.ts
+++ b/studio/frontend/src/features/chat/utils/fork-count-store.ts
@@ -1,13 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * One fork-count subscription per rendered thread, shared by every message badge.
- *
- * The badge used to own a `CHAT_HISTORY_UPDATED_EVENT` listener and a request per
- * message, so a 200-message thread spent 200 requests on every history event, and
- * streaming raises one of those per chunk.
- */
+/** One fork-count subscription per rendered thread, shared by every message badge. The badge used
+ * to own a `CHAT_HISTORY_UPDATED_EVENT` listener and a request per message, so a 200-message
+ * thread spent 200 requests on every history event, and streaming raises one per chunk. */
import {
CHAT_HISTORY_UPDATED_EVENT,
getThreadForkCounts,
@@ -18,17 +14,12 @@ import { isThreadIncognito } from "./chat-history-storage";
export const FORK_COUNT_REFRESH_DEBOUNCE_MS = 300;
// The ceiling on how long a real fork change can sit unrendered. A trailing edge alone is a
-// starvation hazard, not merely a slow path: CHAT_HISTORY_UPDATED_EVENT fires once per streaming
-// chunk, so a reply running in a background thread resets the timer forever and a fork deleted
-// from the sidebar keeps its old badge for as long as that reply runs, which on a queued or long
-// generation is minutes. The event is a bare Event with no detail and six other consumers, so
-// telling fork changes apart from chunks would mean changing a contract well outside this store;
-// bounding the wait fixes the same problem inside it.
-//
-// 2000 rather than something tighter because the cost of the bound is one whole-thread fetch per
-// window for as long as a stream runs. At 300ms that is the per-chunk traffic this store exists
-// to remove; at 2000 it is under a sixth of it, and it is paid only while something is actually
-// streaming.
+// starvation hazard: CHAT_HISTORY_UPDATED_EVENT fires once per streaming chunk, so a reply
+// running in a background thread resets the timer forever and a deleted fork keeps its old
+// badge for minutes. The event is a bare Event with six other consumers, so telling fork
+// changes apart from chunks would mean changing a contract well outside this store. 2000
+// rather than tighter because the bound costs one whole-thread fetch per window while a
+// stream runs; at 300ms that is the per-chunk traffic this store exists to remove.
export const FORK_COUNT_REFRESH_MAX_WAIT_MS = 2000;
type Counts = ReadonlyMap<string, number>;
@@ -52,13 +43,11 @@ let listening = false;
async function refresh(threadId: string): Promise<void> {
const entry = entries.get(threadId);
if (!entry) return;
- // A temporary chat is the one thread whose forks cannot exist: ensureThreadRecord marks it
- // and returns without a row. The row decides that, not the id -- a `__LOCALID_` prefix
- // belongs to every chat the app creates, saved ones included.
- //
- // Skipped because the answer is already the empty map the entry holds, not to dodge a
- // failure: fork_counts_for_thread GROUPs chat_threads without looking the source up, so an
- // unknown thread gets 200 and an empty map. Only GET /threads/{id} 404s.
+ // A temporary chat is the one thread whose forks cannot exist: ensureThreadRecord marks it and
+ // returns without a row. The row decides that, not the id, since a `__LOCALID_` prefix belongs
+ // to every chat the app creates. Skipped because the answer is already the empty map the entry
+ // holds, not to dodge a failure: fork_counts_for_thread GROUPs without looking the source up,
+ // so an unknown thread gets 200 and an empty map.
if (isThreadIncognito(threadId)) return;
const seq = ++entry.seq;
let counts: Counts;
@@ -90,17 +79,16 @@ function runRefresh(): void {
}
function onHistoryUpdated(): void {
- // Clear and reschedule, as the sidebar refresh does. Returning while a timer exists would
- // make this a leading-edge throttle: streaming fires this event per chunk, so the timer
- // would expire mid-stream and the next chunk would start another window, costing one
- // whole-thread fetch every FORK_COUNT_REFRESH_DEBOUNCE_MS for as long as the reply runs.
- // Fork counts cannot change during generation, so every one of those is wasted.
+ // Clear and reschedule, as the sidebar refresh does. Returning while a timer exists would make
+ // this a leading-edge throttle: streaming fires this event per chunk, so the timer would
+ // expire mid-stream and the next chunk would start another window, costing one whole-thread
+ // fetch per debounce window for as long as the reply runs. Fork counts cannot change during
+ // generation, so every one of those is wasted.
if (pendingRefresh) clearTimeout(pendingRefresh);
pendingRefresh = setTimeout(runRefresh, FORK_COUNT_REFRESH_DEBOUNCE_MS);
- // The bound. Deliberately not restarted while it is already running, or a per-chunk event
- // stream would push it out exactly the way it pushes out the trailing edge, and the ceiling
- // would not be a ceiling. Implemented as a second timer rather than a Date.now() deadline so
- // it is driven by the same clock as the debounce and can be tested without a fake Date.
+ // The bound. Deliberately not restarted while it is already running, or a per-chunk event stream
+ // would push it out the way it pushes out the trailing edge. A second timer rather than a
+ // Date.now() deadline so it shares the debounce's clock and can be tested without a fake Date.
if (!maxWaitTimer) {
maxWaitTimer = setTimeout(runRefresh, FORK_COUNT_REFRESH_MAX_WAIT_MS);
}
diff --git a/studio/frontend/src/features/chat/utils/format-transfer.ts b/studio/frontend/src/features/chat/utils/format-transfer.ts
index 3ee6189db06..f0af99cc510 100644
--- a/studio/frontend/src/features/chat/utils/format-transfer.ts
+++ b/studio/frontend/src/features/chat/utils/format-transfer.ts
@@ -1,11 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Format a byte-per-second rate (e.g. 1_234_567 → "1.2 MB/s"). Returns `"--"`
- * for non-finite or non-positive inputs so the label renders safely before the
- * first stable sample.
- */
+/** Format a byte-per-second rate (1_234_567 -> "1.2 MB/s"). Returns `"--"` for non-finite or
+ * non-positive inputs so the label renders safely before the first stable sample. */
export function formatRate(bytesPerSecond: number): string {
if (!Number.isFinite(bytesPerSecond) || bytesPerSecond <= 0) return "--";
const bps = bytesPerSecond;
@@ -15,17 +12,12 @@ export function formatRate(bytesPerSecond: number): string {
return `${(bps / 1024 ** 3).toFixed(2)} GB/s`;
}
-/**
- * Beyond a day the number is noise. The hub formatter has clamped since #7679;
- * this one did not, so one estimate read "> 24h left" on a hub row and an
- * unbounded hour count on the model-load toast.
- */
+/** Beyond a day the number is noise. The hub formatter has clamped since #7679; this one did not,
+ * so one estimate read "> 24h left" on a hub row and an unbounded hour count on the toast. */
const MAX_DISPLAYABLE_ETA_SECONDS = 24 * 60 * 60;
-/**
- * Format an ETA in seconds as a short string (e.g. 125 → "2m 5s", 3725 →
- * "1h 2m"). Returns `"--"` for non-finite or non-positive inputs.
- */
+/** Format an ETA in seconds as a short string (125 -> "2m 5s", 3725 -> "1h 2m"). Returns `"--"`
+ * for non-finite or non-positive inputs. */
export function formatEta(seconds: number): string {
if (!Number.isFinite(seconds) || seconds <= 0) return "--";
const s = Math.round(seconds);
diff --git a/studio/frontend/src/features/chat/utils/image-input-support.ts b/studio/frontend/src/features/chat/utils/image-input-support.ts
index 607c3546ce3..c050a6a0bb0 100644
--- a/studio/frontend/src/features/chat/utils/image-input-support.ts
+++ b/studio/frontend/src/features/chat/utils/image-input-support.ts
@@ -30,9 +30,8 @@ export function getImageInputUnavailableReason({
}: {
activeModel?: ChatModelSummary;
isExternalModel: boolean;
- // true/false = caller knows; null/undefined = unknown (default-allow).
- // External selections aren't in runtime.models[], so callers resolve
- // provider-type capability and pass it here.
+ // true/false = caller knows; null/undefined = unknown (default-allow). External selections are
+ // not in runtime.models[], so callers resolve provider-type capability and pass it here.
externalSupportsVision?: boolean | null;
// Fallback toast label when activeModel is missing.
externalModelLabel?: string | null;
@@ -40,8 +39,8 @@ export function getImageInputUnavailableReason({
modelLoaded: boolean;
// Runtime lastModelLoadError; lets the no-model branch flag a failed load.
loadError?: string | null;
- // Backend-reported: image input is off because Vision was switched off for this
- // model, not because no projector could be found.
+ // Backend-reported: image input is off because Vision was switched off for this model, not
+ // because no projector could be found.
visionDisabledByUser?: boolean | null;
mmprojFallbackReason?: MmprojFallbackReason | null;
}): string | null {
@@ -77,10 +76,9 @@ export function getImageInputUnavailableReason({
return fallbackReason;
}
- // loadedIsMultimodal is true for vision OR audio; that one flag can't tell
- // them apart, so only block when activeModel confirms audio-only (audio
- // capability set AND isVision === false). Otherwise trust the load
- // response: the models-list entry may be stale or not yet injected.
+ // loadedIsMultimodal is true for vision OR audio and cannot tell them apart, so only block when
+ // activeModel confirms audio-only (audio capability set AND isVision === false). Otherwise
+ // trust the load response: the models-list entry may be stale or not yet injected.
if (loadedIsMultimodal) {
const isAudioOnly =
Boolean(activeModel?.isAudio || activeModel?.hasAudioInput) &&
@@ -90,9 +88,9 @@ export function getImageInputUnavailableReason({
}
}
const label = activeModel?.name || activeModel?.id || "Current model";
- // Before the generic message below, which would otherwise send someone who
- // switched Vision off hunting for a vision model with a valid mmproj. The
- // model is capable and the projector is fine; the setting is what is in the way.
+ // Before the generic message below, which would otherwise send someone who switched Vision off
+ // hunting for a vision model with a valid mmproj. The model is capable and the projector is
+ // fine; the setting is what is in the way.
if (visionDisabledByUser) {
return `Vision is turned off for ${label}. Turn it back on in the model's Advanced Settings to attach images.`;
}
@@ -105,20 +103,13 @@ export function getImageInputUnavailableReason({
);
}
-/**
- * The owners of the running-flag pulse the gate fires when it refuses a turn.
- *
- * `chat-adapter.ts` flips `runningByThreadId` on and straight back off before it throws,
- * so compare mode's `waitForRunEnd` resolves instead of hanging on a run that never
- * reached the streaming path. It is a settlement for waiters, not a run: nothing was
- * requested, nothing was generated, and anything reading that field as "this thread's run
- * started, and then it ended" is reading a request that was never issued as a finished one.
- *
- * A WeakSet rather than a flag on the function, because the store types an owner as a bare
- * `() => void` and the identity is all either side needs. Fresh per pulse: siblings share
- * the "__default" key and `setThreadRunning` clears by owner, so two gates firing on one
- * key must not clear each other's entry.
- */
+/** The owners of the running-flag pulse the gate fires when it refuses a turn. `chat-adapter.ts`
+ * flips `runningByThreadId` on and straight back off before it throws, so compare mode's
+ * `waitForRunEnd` resolves instead of hanging on a run that never reached the streaming path.
+ * It is a settlement for waiters, not a run. A WeakSet rather than a flag on the function,
+ * because the store types an owner as a bare `() => void`. Fresh per pulse: siblings share
+ * the "__default" key and `setThreadRunning` clears by owner, so two gates firing on one key
+ * must not clear each other's entry. */
const imageGateRunOwners = new WeakSet<() => void>();
/** A run-owner token that marks its `setThreadRunning` pair as the gate's own pulse. */
@@ -128,14 +119,9 @@ export function createImageGateRunOwner(): () => void {
return owner;
}
-/**
- * Whether everything holding this thread's running flag is a gate pulse.
- *
- * Asked by readers that mean "is this thread generating", so a real run sharing the key --
- * an unresolved sibling under "__default", or a gate firing on one pane while the other
- * streams -- still answers yes. An empty list is a run from before per-run tracking and is
- * taken at face value.
- */
+/** Whether everything holding this thread's running flag is a gate pulse. Asked by readers that
+ * mean "is this thread generating", so a real run sharing the key still answers yes. An empty
+ * list is a run from before per-run tracking and is taken at face value. */
export function isImageGateRunOnly(
owners: readonly { owner: () => void }[] | undefined,
): boolean {
diff --git a/studio/frontend/src/features/chat/utils/incremental-assistant-content.ts b/studio/frontend/src/features/chat/utils/incremental-assistant-content.ts
index ecc5437091b..a07017c4fb8 100644
--- a/studio/frontend/src/features/chat/utils/incremental-assistant-content.ts
+++ b/studio/frontend/src/features/chat/utils/incremental-assistant-content.ts
@@ -13,14 +13,10 @@ type ContentPart = NonNullable<ChatModelRunResult["content"]>[number];
const THINK_OPEN_TAG = "<think>";
const THINK_CLOSE_TAG = "</think>";
-/**
- * Length of the longest suffix of `text` that is a proper prefix of `tag`.
- *
- * Those characters cannot be classified yet: `<thi` at the end of what has
- * arrived is plain text if the reply stops there and the opening of a
- * reasoning block if `nk>` arrives next, so they are held back and reclassified
- * once the next arrival settles them.
- */
+/** Length of the longest suffix of `text` that is a proper prefix of `tag`. Those characters
+ * cannot be classified yet: `<thi` at the end of what has arrived is plain text if the reply
+ * stops there and the opening of a reasoning block if `nk>` arrives next, so they are held
+ * back and reclassified once the next arrival settles them. */
function heldBackLength(text: string, tag: string): number {
const most = Math.min(tag.length - 1, text.length);
for (let size = most; size > 0; size -= 1) {
@@ -31,13 +27,10 @@ function heldBackLength(text: string, tag: string): number {
return 0;
}
-/**
- * One run of text between two tool-call boundaries, parsed as it arrives.
- *
- * Reproduces `parseAssistantContent` over the run exactly, including that it
- * coalesces adjacent parts of the same kind and that a run ending inside a
- * `<think>` block yields a reasoning part with no closing tag.
- */
+/** One run of text between two tool-call boundaries, parsed as it arrives. Reproduces
+ * `parseAssistantContent` over the run exactly, including that it coalesces adjacent parts of
+ * the same kind and that a run ending inside a `<think>` block yields an unclosed reasoning
+ * part. */
class ParsedRun {
readonly parts: ContentPart[] = [];
private insideThink = false;
@@ -73,15 +66,12 @@ class ParsedRun {
}
}
- /**
- * The run's parts. The held-back characters are folded in as whatever
- * `parseAssistantContent` would call them if the reply stopped here, so the
- * result is that function's output for every state the run passes through.
- */
+ /** The run's parts. The held-back characters are folded in as whatever `parseAssistantContent`
+ * would call them if the reply stopped here, so the result is that function's output for
+ * every state the run passes through. */
view(): ContentPart[] {
- // A copy even when nothing is held back. The retained parts are the state
- // the next arrival extends, so handing the array itself out would let a
- // caller rewrite what has already been parsed.
+ // A copy even when nothing is held back. The retained parts are the state the next arrival
+ // extends, so handing the array itself out would let a caller rewrite what has been parsed.
const out = this.parts.slice();
if (!this.held) {
return out;
@@ -106,14 +96,10 @@ class ParsedRun {
export type SegmentedAssistantText = {
/** Take the characters an arrival added to the reply. */
appendText(delta: string): void;
- /**
- * Parsed parts for the run before each boundary, then the run after the last
- * one, so the result always has `boundaries.length + 1` entries.
- *
- * `rawText` is only read when the retained state cannot be trusted for it;
- * on the streaming path that is the boundaries changing, which happens once
- * per tool call, not once per arrival.
- */
+ /** Parsed parts for the run before each boundary, then the run after the last one, so the result
+ * always has `boundaries.length + 1` entries. `rawText` is only read when the retained state
+ * cannot be trusted for it; on the streaming path that is the boundaries changing, once per
+ * tool call rather than once per arrival. */
runs(rawText: string, boundaries: readonly number[]): ContentPart[][];
};
@@ -132,21 +118,12 @@ function sameBoundaries(
return true;
}
-/**
- * Parse the accumulated reply into content parts without rereading it.
- *
- * The adapter appends to one string per arrival and then parses the whole
- * thing, which is O(reply) per arrival for two reasons: the parse itself, and
- * that `text += delta` builds a cons string whose first read flattens it. This
- * keeps the parse of everything already seen and extends it with the delta, so
- * neither cost is paid again.
- *
- * The retained state describes an append-only reply. Anything else, a rewrite
- * of the prefix or a suffix removed or a tool call landing behind the end,
- * shows up as a length or boundary mismatch and rebuilds from `rawText`. That
- * costs one full parse, on the arrivals that do it, which is the behaviour
- * this replaces.
- */
+/** Parse the accumulated reply into content parts without rereading it. The adapter appends to
+ * one string per arrival and then parses the whole thing, which is O(reply) per arrival for
+ * two reasons: the parse itself, and that `text += delta` builds a cons string whose first
+ * read flattens it. This keeps the parse of everything already seen and extends it with the
+ * delta. The retained state describes an append-only reply; anything else shows up as a
+ * length or boundary mismatch and rebuilds from `rawText`, at one full parse. */
export function createSegmentedAssistantText({
trustAppends = true,
}: { trustAppends?: boolean } = {}): SegmentedAssistantText {
diff --git a/studio/frontend/src/features/chat/utils/json-record-stream.ts b/studio/frontend/src/features/chat/utils/json-record-stream.ts
index 1f4e645a8e1..ab9dfbceeb5 100644
--- a/studio/frontend/src/features/chat/utils/json-record-stream.ts
+++ b/studio/frontend/src/features/chat/utils/json-record-stream.ts
@@ -15,10 +15,8 @@ export interface ImportSource {
chunks(): AsyncIterable<TextChunk>;
}
-/**
- * Decode byte ranges into text. `fatal` rejects invalid UTF-8 instead of
- * substituting replacement characters, which the desktop path requires.
- */
+/** Decode byte ranges into text. `fatal` rejects invalid UTF-8 instead of substituting
+ * replacement characters, which the desktop path requires. */
export async function* decodeTextChunks(
byteChunks: AsyncIterable<Uint8Array>,
fatal = false,
@@ -55,10 +53,8 @@ export function fileImportSource(file: File): ImportSource {
};
}
-/**
- * Read a whole source. The bound is the bytes read, not the length of the
- * decoded string: multibyte text is longer in bytes than in UTF-16 units.
- */
+/** Read a whole source. The bound is the bytes read, not the length of the decoded string:
+ * multibyte text is longer in bytes than in UTF-16 units. */
export async function readAllText(
source: ImportSource,
maxBytes: number,
@@ -78,10 +74,7 @@ export async function readAllText(
return text;
}
-/**
- * Stream top-level records from a JSON array or JSONL/NDJSON, including
- * records formatted across multiple lines.
- */
+/** Stream top-level records from a JSON array or JSONL/NDJSON, including records formatted across multiple lines. */
export interface StreamJsonOptions {
onBytes?: (bytes: number) => void;
/** A framed record that does not parse. The stream continues past it. */
@@ -96,10 +89,8 @@ interface Salvage {
/** A line-leading `{`/`[` that follows a finished value: the next record. */
const NEW_RECORD_LINE = /[^\s:,[{]\s*\n[[{]/;
-/**
- * Recover records after an unclosed JSONL row. Until line framing is proven,
- * only unindented object or array lines qualify as record boundaries.
- */
+/** Recover records after an unclosed JSONL row. Until line framing is proven, only unindented
+ * object or array lines qualify as record boundaries. */
function salvageLines(region: string, proven: boolean): Salvage {
const records: unknown[] = [];
const damaged: string[] = [];
@@ -153,9 +144,9 @@ export async function* streamJsonRecords(
try {
buffer = consumed > 0 ? buffer.slice(consumed) + chunk.text : buffer + chunk.text;
} catch (error) {
- // Records are dropped from the buffer as they are emitted, so the only way
- // to reach the engine's maximum string length is ONE record that long.
- // Untranslated, that surfaces as a bare "Invalid string length".
+ // Records are dropped from the buffer as they are emitted, so the only way to reach the
+ // engine's maximum string length is ONE record that long. Untranslated, that surfaces as a
+ // bare "Invalid string length".
if (error instanceof RangeError) {
throw new RangeError(
"One chat in this export is too large to read in a single piece. " +
@@ -184,9 +175,8 @@ export async function* streamJsonRecords(
}
if (start < 0) {
- // Past the array's own close, only whitespace belongs to the file. A
- // record after it means this is not the single export it claims to
- // be, and importing it would take in data from who knows where.
+ // Past the array's own close, only whitespace belongs to the file. A record after it means this
+ // is not the single export it claims to be.
if (sawArrayEnd) {
if (code !== SPACE && code !== TAB && code !== NEWLINE && code !== RETURN) {
throw new SyntaxError(
@@ -237,21 +227,17 @@ export async function* streamJsonRecords(
}
}
- // A pending record is damaged once a later line starts a record of its
- // own. That is a line-leading `{` or `[` which JSON could not continue
- // the pending text with: after a finished value another value is a new
- // record, while after `:` `,` `[` `{` it is this record's own nesting.
- // Deciding it that way keeps the outcome independent of where the chunk
- // boundary fell, and recovering here rather than only at end of input
- // keeps a file whose first row is broken from buffering whole.
+ // A pending record is damaged once a later line starts a record of its own: a line-leading `{`
+ // or `[` which JSON could not continue the pending text with. Deciding it that way keeps the
+ // outcome independent of where the chunk boundary fell, and recovering here rather than only
+ // at end of input keeps a file whose first row is broken from buffering whole.
if (!sawArrayStart && start >= 0) {
const lastNewline = buffer.lastIndexOf("\n");
const boundary =
lastNewline > start ? NEW_RECORD_LINE.exec(buffer.slice(start, lastNewline)) : null;
if (boundary) {
- // Only the text before the boundary is damaged. The scanner resumes
- // from the boundary itself, so a pretty-printed record after a broken
- // row is framed by its nesting rather than shredded into lines.
+ // Only the text before the boundary is damaged. The scanner resumes from the boundary itself,
+ // so a pretty-printed record after a broken row is framed by its nesting rather than shredded.
const at = start + boundary.index + boundary[0].length - 1;
const damaged = buffer.slice(start, at).trim();
if (damaged) options.onMalformed?.(damaged);
@@ -270,10 +256,9 @@ export async function* streamJsonRecords(
const tail = start >= 0 ? buffer.slice(start).trim() : "";
if (tail) {
if (sawArrayStart) {
- // Arrays have no line boundary on which to recover a truncated record.
- // The failure is the same one the closing-bracket check below reports, so
- // it says so in the same words rather than surfacing the engine's own
- // "Unterminated string in JSON at position ..." to the user.
+ // Arrays have no line boundary on which to recover a truncated record. The failure is the same
+ // one the closing-bracket check below reports, so it says so in the same words rather than
+ // surfacing the engine's own "Unterminated string in JSON" to the user.
let record: unknown;
try {
record = JSON.parse(tail);
@@ -296,8 +281,8 @@ export async function* streamJsonRecords(
}
}
- // A download cut between two records leaves every record read so far intact,
- // so the missing bracket is the only sign that the rest never arrived.
+ // A download cut between two records leaves every record read so far intact, so the missing
+ // bracket is the only sign that the rest never arrived.
if (sawArrayStart && !sawArrayEnd) {
throw new SyntaxError(
"The JSON array ends before its closing bracket, so the export is incomplete.",
diff --git a/studio/frontend/src/features/chat/utils/last-local-model-load.ts b/studio/frontend/src/features/chat/utils/last-local-model-load.ts
index 29d90378d39..343965d629f 100644
--- a/studio/frontend/src/features/chat/utils/last-local-model-load.ts
+++ b/studio/frontend/src/features/chat/utils/last-local-model-load.ts
@@ -19,8 +19,7 @@ export type LastLocalModelLoad = {
};
const API_PATH = "/api/settings/last-local-model";
-// Pre-backend installs kept the record here; still read so an upgrade does not
-// forget the model.
+// Pre-backend installs kept the record here; still read so an upgrade does not forget the model.
const LEGACY_STORAGE_KEY = "unsloth.last-local-model-load.v1";
function isLastLocalModelKind(value: unknown): value is LastLocalModelKind {
@@ -140,11 +139,10 @@ export async function readLastLocalModelLoad(
? legacy.pendingSync
: legacy.loadedAt > backendLoadedAt)
) {
- // A local record the backend has not seen: a PUT dropped at teardown
- // (pendingSync) or a pre-upgrade bundle's write. Re-sync it with its
- // original stamp, even on an identity match, so the backend stamp
- // advances past older dropped writes elsewhere. An unstamped backend
- // record gives no order, so only a pending shadow may outrank it.
+ // A local record the backend has not seen: a PUT dropped at teardown (pendingSync) or a
+ // pre-upgrade bundle's write. Re-sync it with its original stamp, even on an identity match, so
+ // the backend stamp advances past older dropped writes elsewhere. An unstamped backend record
+ // gives no order, so only a pending shadow may outrank it.
recordLastLocalModelLoad({
...legacy.record,
loadedAt: legacy.loadedAt,
@@ -181,8 +179,8 @@ export function recordLastLocalModelLoad(input: {
}
const loadedAt =
typeof input.loadedAt === "number" ? input.loadedAt : Date.now();
- // Shadow write first, synchronously: a fetch pending at teardown is dropped
- // without running either callback. Also covers a pre-route backend's 404.
+ // Shadow write first, synchronously: a fetch pending at teardown is dropped without running
+ // either callback. Also covers a pre-route backend's 404.
writeLegacyRecord(record, true, loadedAt);
authFetch(API_PATH, {
method: "PUT",
@@ -203,8 +201,8 @@ export function recordLastLocalModelLoad(input: {
if (!res.ok) {
return;
}
- // Adopt the server's answer: it may have clamped or rejected this
- // write, and the shadow must mirror what is stored.
+ // Adopt the server's answer: it may have clamped or rejected this write, and the shadow must
+ // mirror what is stored.
let serverRecord: LastLocalModelLoad | null = null;
let serverLoadedAt: number | null = null;
try {
@@ -232,9 +230,8 @@ export function recordLastLocalModelLoad(input: {
} catch {
// Pre-loaded_at backend or opaque response: fall back to our stamp.
}
- // Clear only this write's marker: a newer load may have replaced the
- // shadow mid-flight, even a reload of the same model, so the stamp must
- // match too.
+ // Clear only this write's marker: a newer load may have replaced the shadow mid-flight, even a
+ // reload of the same model, so the stamp must match too.
const legacy = readLegacyEntry();
if (
!legacy?.pendingSync ||
diff --git a/studio/frontend/src/features/chat/utils/mirrored-chat-settings.ts b/studio/frontend/src/features/chat/utils/mirrored-chat-settings.ts
index dd39ea219a5..136f60ed01c 100644
--- a/studio/frontend/src/features/chat/utils/mirrored-chat-settings.ts
+++ b/studio/frontend/src/features/chat/utils/mirrored-chat-settings.ts
@@ -1,11 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Composer, RAG and model-load toggles the chat runtime store mirrors to
-// /api/chat/settings so they follow the installation rather than one browser's
-// localStorage. Every value has to survive a round trip through a browser's
-// storage, so each one is re-validated here before it reaches the backend,
-// which rejects the whole patch on a single out-of-contract field.
+// Composer, RAG and model-load toggles the chat runtime store mirrors to /api/chat/settings so
+// they follow the installation rather than one browser's localStorage. Every value has to
+// survive a round trip through a browser's storage, so each is re-validated here before it
+// reaches the backend, which rejects the whole patch on a single out-of-contract field.
import type { PersistedChatSettings } from "../api/chat-settings-api";
@@ -39,11 +38,11 @@ const MIRRORED_ENUM_VALUES = {
Record<keyof PersistedChatSettings, readonly string[]>
>;
-// One year, the ceiling ChatSettingsPayload and the run route both enforce. A larger value
-// would be dropped from the patch and then 400 the run, so it is bounded where it is set.
+// One year, the ceiling ChatSettingsPayload and the run route both enforce. A larger value would
+// be dropped from the patch and then 400 the run, so it is bounded where it is set.
export const MAX_RESEARCH_MODEL_TIMEOUT_SECONDS = 365 * 24 * 3600;
-// 0 is the unlimited sentinel, not a very short budget, so it sits below the run route's
-// finite floor. A stored 1..9 would 400 every run.
+// 0 is the unlimited sentinel, not a very short budget, so it sits below the run route's finite
+// floor. A stored 1..9 would 400 every run.
export const MIN_FINITE_RESEARCH_MODEL_TIMEOUT_SECONDS = 10;
// Bounds match the ge/le the backend payload enforces on the same fields.
@@ -170,11 +169,9 @@ export function assignSanitizedMirroredSettings(
if (ragSource) settings.ragSource = ragSource;
}
-/**
- * Map a stored RAG auto-inject value onto the three-way control. Storage
- * predating that control holds "true"/"false", which the backend rejects, so
- * the migration has to run before a backfill sends the stored value.
- */
+/** Map a stored RAG auto-inject value onto the three-way control. Storage predating that control
+ * holds "true"/"false", which the backend rejects, so the migration has to run before a
+ * backfill sends the stored value. */
export function normalizeStoredRagAutoInject(
raw: string,
): "auto" | "on" | "off" {
@@ -182,14 +179,11 @@ export function normalizeStoredRagAutoInject(
return raw === "false" ? "off" : "auto";
}
-/**
- * Whether a resident model's own baseline currently owns a mirrored setting.
- * speculativeType and gpuMemoryMode are each written with a loaded* shadow that
- * hydration cannot set, so moving the editable half alone while a shadow holds
- * the other splits the pair. With no shadow the stored preference is the one the
- * next load reads, and hydration has to apply it or the load sends a default and
- * persists that default back over the server's value.
- */
+/** Whether a resident model's own baseline currently owns a mirrored setting. speculativeType and
+ * gpuMemoryMode are each written with a loaded* shadow that hydration cannot set, so moving the
+ * editable half alone while a shadow holds the other splits the pair. With no shadow the stored
+ * preference is what the next load reads, and hydration has to apply it or the load sends a
+ * default and persists that default back over the server's value. */
export function loadShadowOwnsMirroredSetting(
key: string,
shadows: {
diff --git a/studio/frontend/src/features/chat/utils/mmproj-fallback.ts b/studio/frontend/src/features/chat/utils/mmproj-fallback.ts
index 7205aa8c931..e4281171331 100644
--- a/studio/frontend/src/features/chat/utils/mmproj-fallback.ts
+++ b/studio/frontend/src/features/chat/utils/mmproj-fallback.ts
@@ -3,24 +3,18 @@
import type { CpuFallbackReason, MmprojFallbackReason } from "../types/api";
-/**
- * Said when the whole model, not just the projector, ended up on the CPU.
- *
- * Lived as a duplicated string literal in chat-adapter.ts and
- * use-chat-model-runtime.ts. It is here so the two load paths cannot describe the
- * same condition differently, which is what let them drift apart below.
- */
+/** Said when the whole model, not just the projector, ended up on the CPU. Here rather than
+ * duplicated in chat-adapter.ts and use-chat-model-runtime.ts, so the two load paths cannot
+ * describe the same condition differently. */
export const CPU_FALLBACK_MESSAGE =
"The auto-selected Vulkan backend crashed during startup, so GPU acceleration is disabled for this model session.";
const MMPROJ_FALLBACK_MESSAGES: Record<MmprojFallbackReason, string> = {
- // Three routes reach this one placement: the fit estimate predicting the
- // projector will not fit in VRAM, a GPU allocation failure at startup, and a
- // bare signal crash with no non-projector diagnostic. So the message names the
- // outcome and no cause. "Could not start on the GPU" is untrue of the predicted
- // route, which never attempts a GPU load, and "does not fit in VRAM" is untrue
- // of the crash routes -- and that one is worse than vague, because it sends
- // someone whose GPU runtime is broken off to cut context and offload layers.
+ // Three routes reach this one placement: the fit estimate predicting the projector will not fit
+ // in VRAM, a GPU allocation failure at startup, and a bare signal crash with no non-projector
+ // diagnostic. So the message names the outcome and no cause. "Could not start on the GPU" is
+ // untrue of the predicted route, and "does not fit in VRAM" is worse than vague for the crash
+ // routes, since it sends someone with a broken GPU runtime off to cut context.
cpu_offload:
"Unsloth is running the vision projector in system memory rather than on the GPU. Image input remains available, but image processing may be slower.",
projector_incompatible:
@@ -55,26 +49,13 @@ export function mmprojLoadNotice(
};
}
-/**
- * The one description of how a load was degraded, covering BOTH fallbacks.
- *
- * The two load paths each wrote this inline as
- * `mmproj ? mmprojMessage : cpu ? cpuMessage : undefined`, which silently drops the
- * CPU message whenever both are set. That combination is not hypothetical: on a
- * CPU-fallback replay `llama_cpp.py` preserves `_cpu_fallback_reason` (it only clears
- * it when `not _replaying_cpu_fallback`) and clears `_mmproj_fallback_reason` so the
- * projector can fail again in that same launch. A low-VRAM machine whose Vulkan
- * backend crashed is exactly where the projector then falls back too -- the case the
- * feature was written for.
- *
- * The user was told "loaded without vision" and never told the model was on the CPU,
- * so a silently unaccelerated session read as a deliberate, explained one.
- *
- * `baseTitle` is suffixed rather than replaced because the two callers name models
- * differently: the auto-load passes a label that already reads "Loaded X (Q4_K_M)",
- * the explicit load passes "X loaded". Both take the same suffixes, which is why one
- * helper can serve them.
- */
+/** The one description of how a load was degraded, covering BOTH fallbacks. The two load paths
+ * each wrote this inline as `mmproj ? a : cpu ? b : undefined`, which silently drops the CPU
+ * message whenever both are set. That combination is not hypothetical: on a CPU-fallback replay
+ * llama_cpp.py preserves `_cpu_fallback_reason` and clears `_mmproj_fallback_reason`, so a
+ * low-VRAM machine whose Vulkan backend crashed hits both, and the user was told "loaded
+ * without vision" and never that the model was on the CPU. `baseTitle` is suffixed rather than
+ * replaced because the two callers name models differently. */
export function loadFallbackNotice(
baseTitle: string,
cpuFallbackReason: CpuFallbackReason | null | undefined,
@@ -82,9 +63,8 @@ export function loadFallbackNotice(
): { title: string; description: string | undefined; degraded: boolean } {
const textOnly = isTextOnlyMmprojFallback(mmprojFallbackReason);
- // When the whole model is on the CPU, saying the projector is too adds nothing --
- // "on CPU" already covers it. Losing vision entirely is a different fact and is
- // always said.
+ // When the whole model is on the CPU, saying the projector is too adds nothing. Losing vision
+ // entirely is a different fact and is always said.
let suffix = "";
if (cpuFallbackReason && textOnly) {
suffix = " on CPU, without vision";
@@ -96,8 +76,7 @@ export function loadFallbackNotice(
suffix = " without vision";
}
- // Both sentences, CPU first: it is the broader condition, and the projector
- // sentence reads as a detail under it.
+ // Both sentences, CPU first: it is the broader condition, and the projector sentence reads as a detail under it.
const parts: string[] = [];
if (cpuFallbackReason) {
parts.push(CPU_FALLBACK_MESSAGE);
diff --git a/studio/frontend/src/features/chat/utils/model-download-staging.ts b/studio/frontend/src/features/chat/utils/model-download-staging.ts
index 36f12ded1be..89e38c02436 100644
--- a/studio/frontend/src/features/chat/utils/model-download-staging.ts
+++ b/studio/frontend/src/features/chat/utils/model-download-staging.ts
@@ -12,16 +12,15 @@ export function hasGgufSource(x: {
);
}
-/** A local-disk model id: Unix absolute (/), relative (./ ../), tilde (~/),
- * Windows drive (C:\) or UNC (\\server). Shared so the loader and the
- * hub-repo predicate classify ids identically. */
+/** A local-disk model id: Unix absolute, relative, tilde, Windows drive or UNC. Shared so the
+ * loader and the hub-repo predicate classify ids identically. */
export function isLocalModelPath(id: string): boolean {
return /^(\/|\.{1,2}[\\/]|~[\\/]|[A-Za-z]:[\\/]|\\\\)/.test(id);
}
-/** An uncached HF hub repo we can download as a full snapshot (non-GGUF
- * safetensors / MLX). Excludes GGUF sources, local paths, native files, LoRA,
- * and external provider models so none are mis-routed into a snapshot. */
+/** An uncached HF hub repo we can download as a full snapshot (non-GGUF safetensors / MLX).
+ * Excludes GGUF sources, local paths, native files, LoRA and external provider models so none
+ * are mis-routed into a snapshot. */
export function isDownloadableHubRepo(x: {
id: string;
source?: string;
@@ -39,9 +38,9 @@ export function isDownloadableHubRepo(x: {
);
}
-/** A pick the Hub download manager must fetch first: an uncached snapshot repo, or a Hub
- * GGUF whose quant is not on disk. Everything else loads directly, so a caller that
- * cannot prove a Hub pick is missing must leave `source` unset. */
+/** A pick the Hub download manager must fetch first: an uncached snapshot repo, or a Hub GGUF
+ * whose quant is not on disk. Everything else loads directly, so a caller that cannot prove a
+ * Hub pick is missing must leave `source` unset. */
export function wantsDownloadManagerStaging(x: {
id: string;
source?: string;
diff --git a/studio/frontend/src/features/chat/utils/model-lifecycle-gate.ts b/studio/frontend/src/features/chat/utils/model-lifecycle-gate.ts
index b2e218bbe75..10a616eeb3b 100644
--- a/studio/frontend/src/features/chat/utils/model-lifecycle-gate.ts
+++ b/studio/frontend/src/features/chat/utils/model-lifecycle-gate.ts
@@ -1,11 +1,8 @@
export type ModelLifecycleLease = number;
-/**
- * Exclusive ownership for the singleton local-model lifecycle.
- *
- * The store mirrors this gate into `modelLoading` for UI consumers, while the
- * lease prevents one async caller from clearing another caller's loading state.
- */
+/** Exclusive ownership for the singleton local-model lifecycle. The store mirrors this gate into
+ * `modelLoading` for UI consumers, while the lease prevents one async caller from clearing
+ * another caller's loading state. */
export class ModelLifecycleGate {
private activeLease: ModelLifecycleLease | null = null;
private nextLease = 1;
diff --git a/studio/frontend/src/features/chat/utils/offer-kept-sandbox-files.ts b/studio/frontend/src/features/chat/utils/offer-kept-sandbox-files.ts
index 8528dcbe7a2..3a67c87062a 100644
--- a/studio/frontend/src/features/chat/utils/offer-kept-sandbox-files.ts
+++ b/studio/frontend/src/features/chat/utils/offer-kept-sandbox-files.ts
@@ -4,12 +4,8 @@
import { toast } from "sonner";
import { deleteStoredChatThreads } from "./chat-history-storage";
-/**
- * Offer to delete sandbox files a delete kept.
- *
- * Once the chats are gone there is no card left to reach those folders from,
- * so every delete surface that cannot ask up front makes the offer here.
- */
+/** Offer to delete sandbox files a delete kept. Once the chats are gone there is no card left to
+ * reach those folders from, so every delete surface that cannot ask up front offers here. */
export function offerToDeleteKeptSandboxes(keptThreadIds: string[]): void {
if (keptThreadIds.length === 0) return;
toast(
@@ -26,9 +22,8 @@ export function offerToDeleteKeptSandboxes(keptThreadIds: string[]): void {
onClick: () => {
void deleteStoredChatThreads(keptThreadIds, { deleteFiles: true })
.then((stillKept) => {
- // A tool still running in there, a surviving fork, or a folder
- // that would not go: the request succeeded and the files did not,
- // so this offer is the only way back to them.
+ // A tool still running in there, a surviving fork, or a folder that would not go: the request
+ // succeeded and the files did not, so this offer is the only way back to them.
if (stillKept.length > 0) offerToDeleteKeptSandboxes(stillKept);
})
.catch(() => {
diff --git a/studio/frontend/src/features/chat/utils/openapi-support.ts b/studio/frontend/src/features/chat/utils/openapi-support.ts
index 94db2a5a73b..3016485a829 100644
--- a/studio/frontend/src/features/chat/utils/openapi-support.ts
+++ b/studio/frontend/src/features/chat/utils/openapi-support.ts
@@ -3,9 +3,9 @@
const GUARD_FIELDS = ["expectedTitle", "expectedOpeningMessageId"] as const;
-/** Whether the served schema declares the guard fields. The desktop app ships
- * its own frontend against a separately installed backend, and an older one
- * drops these and writes unguarded. Anything unrecognised is unsupported. */
+/** Whether the served schema declares the guard fields. The desktop app ships its own frontend
+ * against a separately installed backend, and an older one drops these and writes unguarded.
+ * Anything unrecognised is unsupported. */
export function schemaDeclaresRepairGuards(document: unknown): boolean {
if (typeof document !== "object" || document === null) return false;
const components = (document as { components?: unknown }).components;
@@ -22,9 +22,8 @@ export function schemaDeclaresRepairGuards(document: unknown): boolean {
export interface GuardProbe {
supported: boolean;
- /** Only a schema that arrived and parsed settles it. A 401 while the token
- * warms up or a 503 at startup is a moment, not an answer, and caching one
- * would park the migration for the session. */
+ /** Only a schema that arrived and parsed settles it. A 401 while the token warms up or a 503 at
+ * startup is a moment, not an answer, and caching one would park the migration for the session. */
settled: boolean;
}
diff --git a/studio/frontend/src/features/chat/utils/openwebui-import.ts b/studio/frontend/src/features/chat/utils/openwebui-import.ts
index 0dd2dcaa358..a27925853a3 100644
--- a/studio/frontend/src/features/chat/utils/openwebui-import.ts
+++ b/studio/frontend/src/features/chat/utils/openwebui-import.ts
@@ -1,12 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Converts Open WebUI exports to Unsloth conversations. `history.messages` is
- * the authoritative DAG and `currentId` selects its active branch; flat
- * `messages` is a legacy fallback. Both modern output items and legacy details
- * blocks are converted to Unsloth message parts.
- */
+/** Converts Open WebUI exports to Unsloth conversations. `history.messages` is the
+ * authoritative DAG and `currentId` selects its active branch; flat `messages` is a legacy
+ * fallback. Modern output items and legacy details blocks both convert to message parts. */
import type { MessageRecord, ParsedConversation } from "../types";
@@ -38,10 +35,9 @@ function epochMs(value: unknown): number | null {
return null;
}
const ms = value < 1e12 ? Math.round(value * 1000) : Math.round(value);
- // An out-of-range stamp (nanoseconds mistaken for milliseconds, say) has to be
- // discarded rather than carried: past 2^53 `previousTs + 1` stops advancing, so
- // every later message collapses onto one createdAt and the depth-first order
- // below no longer survives the reload it exists to survive.
+ // An out-of-range stamp (nanoseconds read as milliseconds) is discarded rather than carried:
+ // past 2^53 `previousTs + 1` stops advancing, so every later message collapses onto one
+ // createdAt and the depth-first order no longer survives a reload.
return ms > MAX_EPOCH_MS ? null : ms;
}
@@ -63,8 +59,8 @@ function looksLikeOpenWebUIMessage(value: unknown): boolean {
"modelIdx" in value ||
"done" in value ||
"output" in value ||
- // A timestamp alone is generic enough for an OpenAI JSONL conversation to
- // carry one; alongside a message id it is Open WebUI's own shape.
+ // A timestamp alone is generic enough for an OpenAI JSONL conversation to carry one;
+ // alongside a message id it is Open WebUI's own shape.
("timestamp" in value && typeof value.id === "string")
);
}
@@ -88,20 +84,16 @@ export function isOpenWebUIRecord(value: unknown): boolean {
if (!blob) return false;
if (isDict(blob.history) && isDict(blob.history.messages)) return true;
if (!Array.isArray(blob.messages)) return false;
- // One tool turn settles it: Open WebUI keeps tool calls in the message body,
- // never as sibling Chat Completions turns.
+ // One tool turn settles it: Open WebUI keeps tool calls in the message body, never as sibling Chat Completions turns.
if (blob.messages.some(looksLikeOpenAIToolMessage)) return false;
return blob.messages.some(looksLikeOpenWebUIMessage);
}
// Content
-// A closed fence first, then an opener that never closed: an answer cut off
-// inside a code block still quotes code, not an Open WebUI construct. The
-// unclosed form is anchored to the start of a line, which is where markdown
-// requires a block fence to begin. Unanchored, a single stray ``` mid-sentence
-// swallowed the whole rest of the message, so every tool call after it was
-// rendered as literal markup and lost.
+// A closed fence first, then an opener that never closed: an answer cut off inside a code
+// block still quotes code. The unclosed form is anchored to line start, where markdown
+// requires a fence; unanchored, a stray ``` swallowed the rest of the message.
const FENCED_CODE =
/```[\s\S]*?```|~~~[\s\S]*?~~~|(?:^|\n)[ \t]{0,3}(?:```|~~~)[\s\S]*$/g;
const DETAILS_BLOCK = /<details\b([^>]*)>([\s\S]*?)<\/details>/gi;
@@ -118,8 +110,8 @@ function numericEntity(digits: string, radix: number): string | null {
function unescapeHtml(value: string): string {
return value
- // Numeric escapes first, and `&amp;` last, so a doubly-escaped `&amp;#39;`
- // survives one pass as the literal text `&#39;` rather than an apostrophe.
+ // Numeric escapes first and `&amp;` last, so a doubly-escaped `&amp;#39;` survives one pass
+ // as the literal text rather than an apostrophe.
.replace(/&#x([0-9a-f]+);/gi, (whole, hex: string) => numericEntity(hex, 16) ?? whole)
.replace(/&#(\d+);/g, (whole, dec: string) => numericEntity(dec, 10) ?? whole)
.replace(/&lt;/g, "<")
@@ -148,12 +140,9 @@ function parseJsonLoose(value: string | undefined): unknown {
}
}
-/**
- * `args` is typed as an object on a tool-call part, and the renderer indexes it.
- * A malformed `arguments` attribute parses loosely to a bare string, so the raw
- * text is kept where a reader can still see it instead of being passed off as
- * a structured argument object.
- */
+/** `args` is typed as an object on a tool-call part and the renderer indexes it. A malformed
+ * `arguments` attribute parses loosely to a bare string, so the raw text is kept visible
+ * instead of being passed off as a structured argument object. */
function toolArgs(value: unknown): Dict {
if (isDict(value)) return value;
if (typeof value === "string" && value) return { arguments: value };
@@ -196,10 +185,8 @@ function fencedRanges(content: string): Array<[number, number]> {
return ranges;
}
-/**
- * Convert details blocks while preserving blocks quoted inside code fences.
- * A real tool result may itself contain fenced code.
- */
+/** Convert details blocks while preserving blocks quoted inside code fences. A real tool
+ * result may itself contain fenced code. */
function contentWithDetailsToParts(content: string): unknown[] {
const parts: unknown[] = [];
const fences = fencedRanges(content);
@@ -223,8 +210,7 @@ function contentWithDetailsToParts(content: string): unknown[] {
const attributes = detailsAttributes(match[1]);
const body = detailsBody(match[2], attributes.type === "reasoning");
if (attributes.type === undefined) {
- // A plain <details> the model wrote is formatting, not an Open WebUI
- // construct, so it keeps its markup.
+ // A plain <details> the model wrote is formatting, not an Open WebUI construct, so it keeps its markup.
pushText(parts, match[0]);
} else if (attributes.type === "tool_calls") {
const result =
@@ -279,9 +265,8 @@ function outputItemsToParts(output: unknown[]): { parts: unknown[]; sawMessage:
if (!isDict(item)) continue;
if (item.type === "reasoning") {
- // `summary` is an empty array whenever summaries are off, so the source
- // is whichever of the two actually holds text rather than whichever the
- // item happens to carry.
+ // `summary` is an empty array whenever summaries are off, so the source is whichever of the
+ // two actually holds text.
const summary = reasoningText(item.summary);
const text = summary || reasoningText(item.content);
if (text) parts.push({ type: "reasoning", text });
@@ -303,14 +288,13 @@ function outputItemsToParts(output: unknown[]): { parts: unknown[]; sawMessage:
}
if (item.type === "image_generation_call") {
- // The item carries the generated image itself as base64. The call id is
- // an OpenAI session reference that means nothing outside the export, so
- // only the image comes over.
+ // The item carries the generated image itself as base64. The call id is an OpenAI session
+ // reference meaning nothing outside the export, so only the image comes over.
const encoded = str(item.result) ?? str(item.b64_json);
if (encoded) {
const format = str(item.output_format) ?? "png";
- // A url is already addressable. Wrapping one in a base64 data url makes
- // a permanently broken image out of something that might have rendered.
+ // A url is already addressable. Wrapping one in a base64 data url makes a permanently broken
+ // image out of something that might have rendered.
parts.push({
type: "image",
image: PORTABLE_IMAGE.test(encoded) ? encoded : `data:image/${format};base64,${encoded}`,
@@ -337,9 +321,9 @@ function outputItemsToParts(output: unknown[]): { parts: unknown[]; sawMessage:
continue;
}
- // Built-in Responses tools (`web_search_call`, `shell_call`, ...) follow the
- // same `<tool>_call` / `<tool>_call_output` pair as a function call, so they
- // become the same portable tool part rather than being dropped.
+ // Built-in Responses tools follow the same `<tool>_call` / `<tool>_call_output` pair as a
+ // function call, so they become the same portable tool part rather than being dropped.
+ // `web_search_call`, `shell_call` and the rest.
const builtin = typeof item.type === "string" ? /^(\w+)_call$/.exec(item.type) : null;
if (builtin) {
const callId = str(item.call_id) ?? str(item.id) ?? crypto.randomUUID();
@@ -356,8 +340,7 @@ function outputItemsToParts(output: unknown[]): { parts: unknown[]; sawMessage:
if (typeof item.type === "string" && item.type.endsWith("_call_output")) {
const callId = str(item.call_id);
- // `output` is a plain string for most tools and a content array only when
- // the tool returned images or files.
+ // `output` is a plain string for most tools and a content array only when the tool returned images or files.
const outputParts = Array.isArray(item.output) ? item.output : [];
let resultText = typeof item.output === "string" ? item.output : "";
const images: string[] = [];
@@ -373,17 +356,15 @@ function outputItemsToParts(output: unknown[]): { parts: unknown[]; sawMessage:
}
const target = callId ? toolCallIndex.get(callId) : undefined;
if (target) {
- // A tool that returned only images has no result text, and its images
- // are pushed as their own parts below. ToolFallbackResult renders
- // nothing at all for an undefined result but draws a "Result:" heading
- // over an empty block for "", which reads as "it returned nothing".
+ // A tool that returned only images has no result text; its images are pushed as their own
+ // parts below. ToolFallbackResult renders nothing for undefined but draws a "Result:"
+ // heading over an empty block for "", which reads as "it returned nothing".
if (resultText) target.result = resultText;
} else if (resultText.trim()) {
pushText(parts, resultText);
}
- // A tool that returned images: keep them as image parts so they still
- // render. A bare `/api/v1/files/<id>` is dead outside Open WebUI, but an
- // absolute url resolves anywhere the chat is opened.
+ // A tool that returned images: keep them as image parts so they still render. A bare
+ // `/api/v1/files/<id>` is dead outside Open WebUI, but an absolute url resolves anywhere.
for (const image of images) {
if (PORTABLE_IMAGE.test(image)) parts.push({ type: "image", image });
}
@@ -393,10 +374,8 @@ function outputItemsToParts(output: unknown[]): { parts: unknown[]; sawMessage:
return { parts, sawMessage };
}
-/**
- * Keep inline images. Keep document names without extracted text, which would
- * otherwise be resent to the model on the next turn.
- */
+/** Keep inline images. Keep document names without extracted text, which would otherwise be
+ * resent to the model on the next turn. */
function filesToParts(files: unknown): { parts: unknown[]; attachments: unknown[] } {
const parts: unknown[] = [];
const attachments: unknown[] = [];
@@ -424,12 +403,9 @@ function filesToParts(files: unknown): { parts: unknown[]; attachments: unknown[
return { parts, attachments };
}
-/**
- * Chat Completions multimodal content: `[{type:"text"}, {type:"image_url"}]`.
- * Detection cannot be perfect, so a record that lands here carrying the OpenAI
- * array shape has to keep its turns rather than lose them: reading only string
- * `content` dropped the whole message.
- */
+/** Chat Completions multimodal content. Detection cannot be perfect, so a record landing here
+ * with the OpenAI array shape must keep its turns: reading only string `content` dropped
+ * the whole message. */
function arrayContentToParts(raw: unknown[]): unknown[] {
return raw.flatMap((entry): unknown[] => {
if (!isDict(entry)) return [];
@@ -462,12 +438,11 @@ function messageParts(
}
const content = typeof message.content === "string" ? message.content : "";
- // `content` mirrors the answer an output message item already carries, so it
- // is used whenever no such item produced text. A tool result left over from
- // an earlier turn is not that answer.
+ // `content` mirrors the answer an output message item already carries, so it is used only
+ // when no such item produced text. A tool result from an earlier turn is not that answer.
if (content && !sawMessage) {
- // Open WebUI writes those details blocks into its own assistant output. The
- // same markup in a prompt is text the user typed, not reasoning or a call.
+ // Open WebUI writes those details blocks into its own assistant output. The same markup in a
+ // prompt is text the user typed, not reasoning or a call.
if (role === "assistant") parts.push(...contentWithDetailsToParts(content));
// Literal prompt text: leading whitespace can be a markdown code block.
else if (content.trim()) parts.push({ type: "text", text: content });
@@ -505,8 +480,8 @@ function collectNodes(chat: Dict): Node[] {
byId.set(id, { id, parentId: str(raw.parentId), raw });
}
- // An empty or unusable DAG is damage like a missing one: the flat branch is
- // then the only copy of the conversation left.
+ // An empty or unusable DAG is damage like a missing one: the flat branch is then the only
+ // copy of the conversation left.
if (byId.size === 0) {
// No DAG: the flat active branch is all that survived.
const flat = Array.isArray(chat.messages) ? chat.messages : [];
@@ -525,8 +500,8 @@ function collectNodes(chat: Dict): Node[] {
return nodes;
}
- // Re-derive children from parentId: `childrenIds` goes stale on edits, and an
- // orphan parent (deleted message) has to fall back to being a root.
+ // Re-derive children from parentId: `childrenIds` goes stale on edits, and an orphan parent
+ // has to fall back to being a root.
const children = new Map<string, Node[]>();
const roots: Node[] = [];
for (const node of byId.values()) {
@@ -545,10 +520,9 @@ function collectNodes(chat: Dict): Node[] {
const ordered: Node[] = [];
const visited = new Set<string>();
- // Depth-first, active branch last: studio restores the head from the final
- // message, so the branch the user had open is the one that opens here too.
- // The traversal is iterative because a long chat is a chain thousands of
- // messages deep, which overflows the call stack long before the cap above.
+ // Depth-first, active branch last: studio restores the head from the final message, so the
+ // branch the user had open opens here too. Iterative, since a chain thousands of messages
+ // deep overflows the call stack long before the cap above.
const walk = (root: Node): void => {
const stack: Node[] = [root];
while (stack.length > 0) {
@@ -566,9 +540,9 @@ function collectNodes(chat: Dict): Node[] {
}
};
- // What the roots can reach, before emitting any of it: the rest is trapped in
- // a cycle and has to be walked between the other branches and the selected
- // one, so the selected branch is still the last message studio reopens on.
+ // What the roots can reach, before emitting any of it: the rest is trapped in a cycle and is
+ // walked between the other branches and the selected one, so the selected branch is still
+ // the last message studio reopens on.
const reachable = new Set<string>();
const pending = [...roots];
while (pending.length > 0) {
@@ -610,10 +584,8 @@ function roleOf(raw: Dict): MessageRecord["role"] {
return "assistant";
}
-/**
- * Convert one exported Open WebUI chat. Returns null when it holds no message
- * we can render (an empty chat, or one whose turns were all blank).
- */
+/** Convert one exported Open WebUI chat. Null when it holds no renderable message (an empty
+ * chat, or one whose turns were all blank). */
export function openWebUIRecordToConversation(
record: unknown,
fallbackTitle: string,
@@ -624,8 +596,8 @@ export function openWebUIRecordToConversation(
const threadId = crypto.randomUUID();
const nodes = collectNodes(chat);
- // The first message is a better start than `updated_at`, which is the end of
- // the conversation and would drag every message in it up to that moment.
+ // The first message is a better start than `updated_at`, which is the end of the
+ // conversation and would drag every message in it up to that moment.
const createdAt =
epochMs(outer.created_at) ??
epochMs(chat.timestamp) ??
@@ -634,19 +606,18 @@ export function openWebUIRecordToConversation(
Date.now();
const messages: MessageRecord[] = [];
- // Unsloth sorts stored messages by createdAt, so the timeline has to be
- // strictly increasing or the depth-first order above would not survive a
- // reload. Real timestamps are kept whenever they already increase.
+ // Unsloth sorts stored messages by createdAt, so the timeline must be strictly increasing or
+ // the depth-first order would not survive a reload. Real timestamps are kept when they
+ // already increase.
let previousTs = createdAt - 1;
const keptIdByOriginal = new Map<string, string>();
for (const node of nodes) {
const role = roleOf(node.raw);
const { content, attachments } = messageParts(node.raw, role);
- // A message that renders to nothing (a failed turn holding only an error)
- // is dropped, and its children relink to the nearest ancestor that stayed.
- // A user turn that uploaded a file and typed nothing still renders, as
- // studio shows attachments on user messages.
+ // A message rendering to nothing is dropped and its children relink to the nearest surviving
+ // ancestor. A user turn that uploaded a file and typed nothing still renders, as studio
+ // shows attachments on user messages.
const renders = content.length > 0 || (role === "user" && attachments.length > 0);
const parentId = node.parentId ? (keptIdByOriginal.get(node.parentId) ?? null) : null;
if (!renders) {
diff --git a/studio/frontend/src/features/chat/utils/parse-assistant-content.ts b/studio/frontend/src/features/chat/utils/parse-assistant-content.ts
index 11277efeab5..0577c5fb518 100644
--- a/studio/frontend/src/features/chat/utils/parse-assistant-content.ts
+++ b/studio/frontend/src/features/chat/utils/parse-assistant-content.ts
@@ -8,11 +8,9 @@ type ContentPart = NonNullable<ChatModelRunResult["content"]>[number];
const THINK_OPEN_TAG = "<think>";
const THINK_CLOSE_TAG = "</think>";
-/**
- * Normalize streamed string or structured delta content to inline text.
- * Structured reasoning-only chunks remain distinguishable so their fallback
- * timer can span consecutive chunks even though each chunk carries closed tags.
- */
+/** Normalize streamed string or structured delta content to inline text. Structured
+ * reasoning-only chunks stay distinguishable so their fallback timer can span consecutive
+ * chunks even though each chunk carries closed tags. */
export function extractDeltaText(delta: unknown): {
text: string;
structuredReasoningContinues: boolean;
@@ -80,8 +78,8 @@ export function extractDeltaText(delta: unknown): {
return { text, structuredReasoningContinues };
}
-// ContentPart from @assistant-ui/react has readonly fields, so `last.text +=
-// text` fails (TS2540). Replace the last element with a merged object instead.
+// ContentPart from @assistant-ui/react has readonly fields, so `last.text += text` fails
+// (TS2540). Replace the last element with a merged object instead.
export function appendTextPart(parts: ContentPart[], text: string): void {
if (!text) return;
@@ -139,54 +137,37 @@ export function hasUnclosedThinkTag(raw: string): boolean {
return raw.lastIndexOf(THINK_OPEN_TAG) > raw.lastIndexOf(THINK_CLOSE_TAG);
}
-/**
- * The most characters of a `<think>` or `</think>` that can sit before the point
- * a previous call stopped at: one short of the longer tag, since a tag
- * straddling the boundary has at least one character on the new side.
- */
+/** The most characters of a `<think>` or `</think>` that can sit before the point a previous
+ * call stopped at: one short of the longer tag, since a tag straddling the boundary has at
+ * least one character on the new side. */
const THINK_TAG_OVERLAP =
Math.max(THINK_OPEN_TAG.length, THINK_CLOSE_TAG.length) - 1;
export type ThinkTagTracker = {
- /**
- * Take the characters an arrival added. Reads `delta` and the seven
- * characters in front of it, never the accumulated reply.
- */
+ /** Take the characters an arrival added. Reads `delta` and the seven characters in front of it,
+ * never the accumulated reply. */
append(delta: string): void;
- /**
- * Take back the suffix a rewrite removed. `text` is the buffer AFTER the
- * removal, and is read only when a tag left with the suffix or the retained
- * overlap has to be refilled, both of which the trailing template-literal
- * strip makes rare.
- */
+ /** Take back the suffix a rewrite removed. `text` is the buffer AFTER the removal, and is read
+ * only when a tag left with the suffix or the retained overlap has to be refilled. */
retract(text: string): void;
- /**
- * Whether the accumulated text ends inside a `<think>` block, that is, what
- * `hasUnclosedThinkTag` returns for the same text.
- */
+ /** Whether the accumulated text ends inside a `<think>` block, that is, what
+ * `hasUnclosedThinkTag` returns for the same text. */
endsInsideThink(): boolean;
};
-/**
- * Track `hasUnclosedThinkTag` across a stream without reading the buffer.
- *
- * `hasUnclosedThinkTag` walks the whole reply, so calling it once per SSE
- * arrival costs O(reply^2) over a reply. Reading the buffer at all costs that
- * much, even for one character: `text += delta` builds a cons string, and the
- * next read of it flattens the whole thing. So this takes the delta rather
- * than the buffer, and keeps the last seven characters itself, which is the
- * most a tag split across arrivals can hide in front of one. A `<think>`
- * delivered one character at a time over seven arrivals is still found on the
- * arrival that completes it.
- */
+/** Track `hasUnclosedThinkTag` across a stream without reading the buffer. That helper walks the
+ * whole reply, so calling it per SSE arrival costs O(reply^2). Reading the buffer at all
+ * costs that much even for one character, since `text += delta` builds a cons string the next
+ * read flattens. So this takes the delta and keeps the last seven characters itself, the most
+ * a tag split across arrivals can hide in front of one. */
export function createThinkTagTracker(): ThinkTagTracker {
- // Characters taken so far; the two indices are the last position of each tag
- // within them, or -1, exactly as `lastIndexOf` reports.
+ // Characters taken so far; the two indices are the last position of each tag within them, or
+ // -1, exactly as `lastIndexOf` reports.
let length = 0;
let lastOpen = -1;
let lastClose = -1;
- // The final `THINK_TAG_OVERLAP` characters, held flat so the next arrival can
- // be searched together with the tag prefix it may complete.
+ // The final `THINK_TAG_OVERLAP` characters, held flat so the next arrival can be searched
+ // together with the tag prefix it may complete.
let overlap = "";
const refindWithin = (text: string): void => {
@@ -205,8 +186,8 @@ export function createThinkTagTracker(): ThinkTagTracker {
}
const window = overlap + delta;
const from = length - overlap.length;
- // A tag re-found inside the overlap is the one already recorded, so it
- // can only reproduce the index it produced last time.
+ // A tag re-found inside the overlap is the one already recorded, so it can only reproduce the
+ // index it produced last time.
const openAt = window.lastIndexOf(THINK_OPEN_TAG);
if (openAt !== -1) {
lastOpen = Math.max(lastOpen, from + openAt);
diff --git a/studio/frontend/src/features/chat/utils/pasted-text.ts b/studio/frontend/src/features/chat/utils/pasted-text.ts
index 76be4c9d586..722b735ab66 100644
--- a/studio/frontend/src/features/chat/utils/pasted-text.ts
+++ b/studio/frontend/src/features/chat/utils/pasted-text.ts
@@ -51,12 +51,11 @@ type PlainPasteKeyEvent = {
readonly altKey: boolean;
};
-/** `KeyboardEvent.keyCode` for V, which follows the physical key, not the
- * character the layout and Option produce. */
+/** `KeyboardEvent.keyCode` for V, which follows the physical key, not the character the layout and Option produce. */
const V_KEY_CODE = 86;
-// Resolved once: the chord below is read on every keydown in the composer, and
-// the platform cannot change under a live document.
+// Resolved once: the chord below is read on every keydown in the composer, and the platform
+// cannot change under a live document.
let macPlatform: boolean | null = null;
function isMacPlatform(): boolean {
@@ -68,12 +67,9 @@ function isMacPlatform(): boolean {
return macPlatform;
}
-/**
- * The paste-without-formatting chord: ⌥⇧⌘V on macOS, which is what its Edit
- * menu carries and so the only one that actually pastes there, and Ctrl+Shift+V
- * elsewhere. It asks for the clipboard in the field, so a paste made with it
- * stays inline however long it is.
- */
+/** The paste-without-formatting chord: Opt-Shift-Cmd-V on macOS, which is what its Edit menu
+ * carries and so the only one that actually pastes there, and Ctrl+Shift+V elsewhere. It
+ * asks for the clipboard in the field, so a paste made with it stays inline however long. */
export function isPlainPasteChord(
event: PlainPasteKeyEvent,
mac: boolean = isMacPlatform(),
@@ -83,36 +79,27 @@ export function isPlainPasteChord(
if (mac ? !event.metaKey || event.ctrlKey : !event.ctrlKey || event.metaKey) {
return false;
}
- // Option belongs to the chord on macOS and to nothing off it. ⇧⌘V is taken
- // on macOS too: web apps bind it, so a host that maps it should reach the
- // field rather than the attachment path.
+ // Option belongs to the chord on macOS and to nothing off it. Shift-Cmd-V is taken on macOS
+ // too: web apps bind it, so a host that maps it should reach the field.
if (event.altKey && !mac) return false;
- // The layout decides where paste lives, not the board: Dvorak puts V on the
- // QWERTY period key and moves the chord with it. So a key that types a
- // letter answers for itself, and a letter that is not v is not this chord
- // however it is wired.
+ // The layout decides where paste lives, not the board: Dvorak puts V on the QWERTY period key
+ // and moves the chord with it. So a key that types a letter answers for itself, and a
+ // letter that is not v is not this chord however it is wired.
const typed = (event.key ?? "").toLowerCase();
if (!event.altKey && typed.length === 1 && typed >= "a" && typed <= "z") {
return typed === "v";
}
- // No letter to read: Option rewrote it, ⌥V being "√" and ⌥⇧V "◊", or the
- // layout types none at all. The two signals left disagree on a remapped
- // board -- `code` is the position the key sits at, `keyCode` the letter it
- // stands for -- and only one of them can be pointing at the chord, so either
- // saying V is enough. The wrong one costing us nothing is what makes that
- // safe: this only counts while the keys are still down, and the one paste
- // that can land by then is the chord's own.
+ // No letter to read: Option rewrote it, or the layout types none. The two signals left
+ // disagree on a remapped board -- `code` is the position, `keyCode` the letter -- so either
+ // saying V is enough. That is safe because this only counts while the keys are still down,
+ // and the one paste that can land by then is the chord's own.
return event.code === "KeyV" || event.keyCode === V_KEY_CODE;
}
-/**
- * How long a plain-paste chord stands for the paste it asks for. The browser
- * dispatches that paste while it is still handling the keydown, so the window
- * only has to survive one task. It has to expire, though: on macOS ⇧⌘V is a
- * web-app convention rather than a menu command, so it can be pressed and
- * paste nothing, and the Edit-menu paste the user reaches for next arrives
- * with no keydown of its own to clear the chord.
- */
+/** How long a plain-paste chord stands for the paste it asks for. The browser dispatches that
+ * paste while still handling the keydown, so the window need only survive one task. It must
+ * expire though: on macOS Shift-Cmd-V is a convention rather than a menu command, so it can
+ * paste nothing, and the Edit-menu paste reached for next has no keydown to clear it. */
export const PLAIN_PASTE_GESTURE_MS = 1000;
/** True when the paste being handled is the one that chord asked for. */
@@ -120,14 +107,13 @@ export function plainPasteStillCounts(chordAt: number, now: number): boolean {
return chordAt > 0 && now - chordAt < PLAIN_PASTE_GESTURE_MS;
}
-// Identity separates a pasted blob from a .txt the user attached. A sent
-// message keeps no File, so the wrapper below carries it over instead.
+// Identity separates a pasted blob from a .txt the user attached. A sent message keeps no
+// File, so the wrapper below carries it over instead.
const pastedTextFiles = new WeakSet<File>();
// The text as pasted, so draft autosave never re-reads the File on a keystroke.
const pastedTextByFile = new WeakMap<File, string>();
-// Length alone decides. Whitespace is not exempt: the bigger the paste, the
-// worse it is inline.
+// Length alone decides. Whitespace is not exempt: the bigger the paste, the worse it is inline.
export function shouldAttachPastedText(
text: string,
minChars: number = PASTED_TEXT_DEFAULT_MIN_CHARS,
@@ -137,13 +123,11 @@ export function shouldAttachPastedText(
return text.length >= minChars;
}
-// Splitting a multi-megabyte paste to read one line allocates every other
-// line for nothing, so walk to the first non-blank line instead, and never
-// copy more of it than a name can hold.
+// Splitting a multi-megabyte paste to read one line allocates every other line for nothing, so
+// walk to the first non-blank line instead, and never copy more than a name can hold.
function firstTextLine(text: string): string {
- // Bounded twice over: each slice, so one line cannot be copied whole, and
- // the walk itself, so a paste of nothing but blank lines cannot be stepped
- // through a line at a time.
+ // Bounded twice over: each slice, so one line cannot be copied whole, and the walk itself, so
+ // a paste of nothing but blank lines cannot be stepped through a line at a time.
const limit = Math.min(text.length, PASTED_TEXT_NAME_TOTAL_SCAN_CHARS);
let start = 0;
while (start < limit) {
@@ -199,10 +183,8 @@ export function pastedTextOf(file: File | undefined): string | undefined {
return file === undefined ? undefined : pastedTextByFile.get(file);
}
-/**
- * Wraps an attachment for the model. A paste gets its own tag and its size,
- * the only markers that survive a reload, since the File does not.
- */
+/** Wraps an attachment for the model. A paste gets its own tag and its size, the only markers
+ * that survive a reload, since the File does not. */
export function attachmentContentText(
name: string,
text: string,
@@ -218,8 +200,8 @@ export function isPastedTextContent(text: string | undefined): boolean {
return text?.startsWith(`<${PASTED_TEXT_TAG} name=`) === true;
}
-// Header only. Sizing a sent paste any other way means walking every byte of
-// it during render, which is what the chip must never do.
+// Header only. Sizing a sent paste any other way means walking every byte of it during render,
+// which is what the chip must never do.
const PASTED_TEXT_BYTES_RE = /^<pasted_text name=[^\n>]* bytes=(\d+)>/;
const PASTED_TEXT_HEADER_SCAN_CHARS = 1024;
@@ -233,8 +215,8 @@ export function pastedTextContentBytes(
return bytes === undefined ? undefined : Number(bytes);
}
-// Where the body sits inside the wrapper, so callers can slice out the piece
-// they need instead of unwrapping megabytes. Unwrapped content is all body.
+// Where the body sits inside the wrapper, so callers can slice out the piece they need instead
+// of unwrapping megabytes. Unwrapped content is all body.
function attachmentBodyRange(content: string): { start: number; end: number } {
const tag = content.startsWith("<attachment name=")
? "attachment"
@@ -253,10 +235,7 @@ function attachmentBodyRange(content: string): { start: number; end: number } {
};
}
-/**
- * Previews wrapped content without unwrapping it: the body can be megabytes,
- * and only the opening is ever rendered.
- */
+/** Previews wrapped content without unwrapping it: the body can be megabytes, and only the opening is ever rendered. */
export function pastedTextContentPreview(content: string): {
text: string;
remaining: number;
@@ -270,10 +249,8 @@ export function pastedTextContentPreview(content: string): {
};
}
-/**
- * The opening of an attachment's body, for naming a thread. A paste-only
- * message has no inline text, so this is all the title has to work from.
- */
+/** The opening of an attachment's body, for naming a thread. A paste-only message has no
+ * inline text, so this is all the title has to work from. */
export function attachmentContentSample(
content: string,
max: number = ATTACHMENT_SAMPLE_CHARS,
@@ -303,10 +280,8 @@ export function attachmentsSample(
return "";
}
-/**
- * A pasted body without its wrapper, for anything that shows the text as the
- * user pasted it: copy, exports, fine-tuning rows. Other content is untouched.
- */
+/** A pasted body without its wrapper, for anything showing the text as the user pasted it:
+ * copy, exports, fine-tuning rows. Other content is untouched. */
export function unwrapPastedTextContent(content: string): string {
if (!isPastedTextContent(content)) return content;
const { start, end } = attachmentBodyRange(content);
@@ -318,11 +293,9 @@ export function pastedTextContentBody(content: string): string {
return isPastedTextContent(content) ? unwrapPastedTextContent(content) : "";
}
-/**
- * Every pasted body on a message, for the paths that read `content` alone and
- * would otherwise lose the paste: Chat Search and copying a message. Other
- * attachment types are not included, since neither path ever had them.
- */
+/** Every pasted body on a message, for the paths that read `content` alone and would otherwise
+ * lose the paste: Chat Search and copying a message. Other attachment types are not
+ * included, since neither path ever had them. */
export function attachmentsPastedText(
attachments: readonly AttachmentLike[] | undefined,
): string {
diff --git a/studio/frontend/src/features/chat/utils/project-attachment-target.ts b/studio/frontend/src/features/chat/utils/project-attachment-target.ts
index 0399cc74362..817b0129833 100644
--- a/studio/frontend/src/features/chat/utils/project-attachment-target.ts
+++ b/studio/frontend/src/features/chat/utils/project-attachment-target.ts
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/** Where a file attached in a project chat is indexed: `project` into the shared
- * sources, `thread` into the one chat. Ignored outside a project. Its own module,
- * with no imports, so a stored value can be tested without the runtime store. */
+/** Where a file attached in a project chat is indexed: `project` into the shared sources, `thread`
+ * into the one chat. Ignored outside a project. Its own module, with no imports, so a stored
+ * value can be tested without the runtime store. */
export type ProjectAttachmentTarget = "project" | "thread";
export const CHAT_PROJECT_ATTACHMENT_TARGET_KEY =
@@ -12,10 +12,10 @@ export const CHAT_PROJECT_ATTACHMENT_TARGET_KEY =
// A project exists to share context, so its chats default to the whole project.
export const DEFAULT_PROJECT_ATTACHMENT_TARGET: ProjectAttachmentTarget = "project";
-/** The target a stored value means, for any string a profile might hold. Nothing
- * stored is an install that never chose, which gets the default; a value this
- * build does not know came from a later one, and the safe reading of that is the
- * narrow scope, since only the wider one can file a file where nobody meant. */
+/** The target a stored value means, for any string a profile might hold. Nothing stored is an
+ * install that never chose, which gets the default; a value this build does not know came from a
+ * later one, and the safe reading of that is the narrow scope, since only the wider one can file
+ * a file where nobody meant. */
export function normalizeProjectAttachmentTarget(
raw: string | null | undefined,
): ProjectAttachmentTarget {
diff --git a/studio/frontend/src/features/chat/utils/project-source-plan.ts b/studio/frontend/src/features/chat/utils/project-source-plan.ts
index 69369be069b..a4472e4ccc1 100644
--- a/studio/frontend/src/features/chat/utils/project-source-plan.ts
+++ b/studio/frontend/src/features/chat/utils/project-source-plan.ts
@@ -20,13 +20,10 @@ function modelLabel(thread: ProjectSourceThread): string | undefined {
return label || undefined;
}
-/**
- * Which half of a compare this is. listStoredChatThreads sorts by updatedAt, so
- * arrival order is whichever half answered last: naming by index alone would
- * swap the two names between saves of one pair. modelType carries the pane, so
- * only a thread missing it falls back to position. The LoRA compare gets the
- * words the compare header uses, since "base" beats "1" in a filename.
- */
+/** Which half of a compare this is. listStoredChatThreads sorts by updatedAt, so arrival order is
+ * whichever half answered last: naming by index alone would swap the two names between saves.
+ * modelType carries the pane, so only a thread missing it falls back to position. The LoRA
+ * compare gets the compare header's words, since "base" beats "1" in a filename. */
function paneLabel(thread: ProjectSourceThread, index: number): string {
if (thread.modelType === "base") return "base";
if (thread.modelType === "lora") return "fine-tuned";
@@ -35,12 +32,9 @@ function paneLabel(thread: ProjectSourceThread, index: number): string {
return String(index + 1);
}
-/**
- * What a "Save to project sources" click uploads. A compare pair is two models
- * answering the same prompt and both halves carry the row's single title, so
- * name each after its model: the sources panel lists a document by filename and
- * nothing else would tell the two apart.
- */
+/** What a "Save to project sources" click uploads. A compare pair is two models answering the same
+ * prompt and both halves carry the row's single title, so name each after its model: the sources
+ * panel lists a document by filename and nothing else would tell the two apart. */
export function planChatItemSources(
item: { id: string; title: string; type: string },
threads: readonly ProjectSourceThread[],
@@ -57,8 +51,7 @@ export function planChatItemSources(
const uses = new Map<string, number>();
for (const { label } of named) uses.set(label, (uses.get(label) ?? 0) + 1);
return named.map(({ thread, label, side }) => {
- // Only a colliding half carries a side, so two different models keep the
- // plain "<title> - <model>" name.
+ // Only a colliding half carries a side, so two different models keep the plain "<title> - <model>" name.
const suffix = (uses.get(label) ?? 0) > 1 ? ` - ${side}` : "";
return { id: thread.id, title: `${item.title} - ${label}${suffix}` };
});
diff --git a/studio/frontend/src/features/chat/utils/prompt-queue-boundary.ts b/studio/frontend/src/features/chat/utils/prompt-queue-boundary.ts
index c2845492eec..118260403da 100644
--- a/studio/frontend/src/features/chat/utils/prompt-queue-boundary.ts
+++ b/studio/frontend/src/features/chat/utils/prompt-queue-boundary.ts
@@ -5,9 +5,9 @@ import {
} from "./prompt-queue-events";
import { localPromptQueueModelBoundary } from "./prompt-queue-model-boundary";
-// Re-exported so existing importers and the barrel keep their paths. Module
-// scope readers should use ./prompt-queue-events directly: this module has
-// dependencies, so the cycle can catch it mid-initialization.
+// Re-exported so existing importers and the barrel keep their paths. Module scope readers should
+// use ./prompt-queue-events directly: this module has dependencies, so the cycle can catch it
+// mid-initialization.
export {
PROMPT_QUEUE_RUN_FAILED_EVENT,
PROMPT_QUEUE_STOP_EVENT,
@@ -36,10 +36,8 @@ export function requestPromptQueueStop(threadIds?: string[]) {
);
}
-/**
- * Stop every materialized local queue and invalidate local queue factories
- * that are still waiting for settings hydration.
- */
+/** Stop every materialized local queue and invalidate local queue factories that are still waiting
+ * for settings hydration. */
export function requestLocalPromptQueueStop(
additionalThreadIds: string[] = [],
) {
diff --git a/studio/frontend/src/features/chat/utils/prompt-queue-events.ts b/studio/frontend/src/features/chat/utils/prompt-queue-events.ts
index 70164481426..eff37996c29 100644
--- a/studio/frontend/src/features/chat/utils/prompt-queue-events.ts
+++ b/studio/frontend/src/features/chat/utils/prompt-queue-events.ts
@@ -1,17 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Prompt-queue event names and their detail shapes.
- *
- * Split out of prompt-queue-boundary so thread.tsx can read them from module
- * scope: features/chat is in an import cycle, so reaching these through the
- * barrel risks the temporal dead zone that crashed use-model-memory.ts.
- *
- * Imports nothing on purpose. ESM evaluates dependencies first, so a module
- * with none cannot be re-entered mid-initialization. One import reopens the
- * hazard.
- */
+/** Prompt-queue event names and their detail shapes. Split out of prompt-queue-boundary so
+ * thread.tsx can read them from module scope: features/chat is in an import cycle, so reaching
+ * these through the barrel risks the temporal dead zone that crashed use-model-memory.ts. Imports
+ * nothing on purpose: ESM evaluates dependencies first, so a module with none cannot be
+ * re-entered mid-initialization. One import reopens the hazard. */
export const PROMPT_QUEUE_STOP_EVENT = "unsloth:prompt-queue-stop";
export const PROMPT_QUEUE_RUN_FAILED_EVENT = "unsloth:prompt-queue-run-failed";
diff --git a/studio/frontend/src/features/chat/utils/prompt-queue-input.ts b/studio/frontend/src/features/chat/utils/prompt-queue-input.ts
index 70ebc0fc6db..d78cab9fc26 100644
--- a/studio/frontend/src/features/chat/utils/prompt-queue-input.ts
+++ b/studio/frontend/src/features/chat/utils/prompt-queue-input.ts
@@ -3,17 +3,13 @@
/** How keyboard and drag input map to prompt-queue actions. */
-/**
- * Private drag type for a queue row, so a reorder is distinguishable from a
- * file or a text selection dragged in from outside.
- */
+/** Private drag type for a queue row, so a reorder is distinguishable from a file or a text
+ * selection dragged in from outside. */
export const PROMPT_QUEUE_DRAG_TYPE = "application/x-unsloth-prompt-queue-item";
-/**
- * Whether a drag carries a queue row. Anything else must pass through
- * untouched: the page dropzone skips events the row already prevented, so
- * claiming a file drag here would silently drop the file.
- */
+/** Whether a drag carries a queue row. Anything else must pass through untouched: the page
+ * dropzone skips events the row already prevented, so claiming a file drag here would
+ * silently drop the file. */
export function isPromptQueueDragTypes(
types: ArrayLike<string> | undefined | null,
): boolean {
@@ -21,13 +17,9 @@ export function isPromptQueueDragTypes(
return Array.from(types).includes(PROMPT_QUEUE_DRAG_TYPE);
}
-/**
- * Cmd/Ctrl+Enter, the queue chord. Shift+Enter stays a newline whatever else is
- * held, and Alt disqualifies it too: Windows reports AltGr as Ctrl+Alt on any
- * layout the browser does not read as having a real AltGraph, and those layouts
- * hold AltGr for everyday characters. `key`, not `code`, so the keypad's Enter
- * is the same chord.
- */
+/** Cmd/Ctrl+Enter, the queue chord. Shift+Enter stays a newline whatever else is held, and Alt
+ * disqualifies it too: Windows reports AltGr as Ctrl+Alt on any layout the browser does not
+ * read as having a real AltGraph. `key`, not `code`, so the keypad's Enter is the same chord. */
export function isPromptQueueChord(event: {
key: string;
shiftKey: boolean;
@@ -39,12 +31,9 @@ export function isPromptQueueChord(event: {
return event.metaKey || event.ctrlKey;
}
-/**
- * Whether a queue start for this thread is registered but has not reached the
- * queue store yet. Starting one awaits settings hydration, and nothing else
- * marks the thread as queueing during that gap, so a plain Enter would send the
- * same text the pending queue is about to dispatch.
- */
+/** Whether a queue start for this thread is registered but has not reached the queue store yet.
+ * Starting one awaits settings hydration, and nothing else marks the thread as queueing during
+ * that gap, so a plain Enter would send the text the pending queue is about to dispatch. */
export function hasPendingPromptQueueStart(
reservations: Iterable<{ cancelled: boolean; threadId: string | null }>,
threadId: string | null,
@@ -56,14 +45,10 @@ export function hasPendingPromptQueueStart(
return false;
}
-/**
- * Identity of a pasted-text queue start, held for the length of the file read
- * before it, so a submit during that read joins it instead of starting a second
- * read of the same attachment.
- *
- * The wait mode is deliberately out: it is recomputed per submit and flips if a
- * run starts mid-read, which would split one prompt across two keys.
- */
+/** Identity of a pasted-text queue start, held for the length of the file read before it, so a
+ * submit during that read joins it instead of starting a second read. The wait mode is
+ * deliberately out: it is recomputed per submit and flips if a run starts mid-read, which
+ * would split one prompt across two keys. */
export function pastedTextQueueKey(
threadId: string | null,
text: string,
diff --git a/studio/frontend/src/features/chat/utils/prompt-queue-model-boundary.ts b/studio/frontend/src/features/chat/utils/prompt-queue-model-boundary.ts
index 391f78366e5..37c2374b9e9 100644
--- a/studio/frontend/src/features/chat/utils/prompt-queue-model-boundary.ts
+++ b/studio/frontend/src/features/chat/utils/prompt-queue-model-boundary.ts
@@ -25,13 +25,9 @@ export type LocalPromptQueueStopPlan = {
retainedItemIndexes: number[];
};
-/**
- * Preserve external-provider work when the singleton local model changes.
- *
- * Only local items depend on the outgoing model. A dispatched local item must
- * be cancelled, but external follow-ups remain valid and can resume once the
- * thread runtime becomes idle.
- */
+/** Preserve external-provider work when the singleton local model changes. Only local items depend
+ * on the outgoing model. A dispatched local item must be cancelled, but external follow-ups
+ * remain valid and can resume once the thread runtime becomes idle. */
export function planLocalPromptQueueStop(
items: readonly PromptQueueModelStopItem[],
runIndex: number,
diff --git a/studio/frontend/src/features/chat/utils/prompt-queue-reorder.ts b/studio/frontend/src/features/chat/utils/prompt-queue-reorder.ts
index 563a5e032a6..01f07904618 100644
--- a/studio/frontend/src/features/chat/utils/prompt-queue-reorder.ts
+++ b/studio/frontend/src/features/chat/utils/prompt-queue-reorder.ts
@@ -1,15 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Index math for dragging a queued prompt into another's slot, kept apart from
- * the queue engine so the ordering rules are testable on plain arrays.
- */
+/** Index math for dragging a queued prompt into another's slot, kept apart from the queue engine
+ * so the ordering rules are testable on plain arrays. */
-/**
- * Items already sent are fixed, so nothing may move to or from a slot before
- * the one about to dispatch.
- */
+/** Items already sent are fixed, so nothing may move to or from a slot before the one about to dispatch. */
export function canReorderPromptQueueRange(
fromIndex: number,
toIndex: number,
@@ -23,11 +18,9 @@ export function canReorderPromptQueueRange(
return fromIndex >= activeIndex && toIndex >= activeIndex;
}
-/**
- * The queue after the move, or null when the move is not allowed. `toIndex` is
- * read off the original array, so a downward drag lands after the target and an
- * upward drag lands before it, which is how a dragged row reads on screen.
- */
+/** The queue after the move, or null when the move is not allowed. `toIndex` is read off the
+ * original array, so a downward drag lands after the target and an upward drag lands before it,
+ * which is how a dragged row reads on screen. */
export function reorderPromptQueueItems<T>(
items: readonly T[],
fromIndex: number,
@@ -45,10 +38,7 @@ export function reorderPromptQueueItems<T>(
return next;
}
-/**
- * Whether the move changed which item dispatches next, so the caller knows to
- * retarget the pending send.
- */
+/** Whether the move changed which item dispatches next, so the caller knows to retarget the pending send. */
export function promptQueueActiveItemChanged<T>(
before: readonly T[],
after: readonly T[],
diff --git a/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts b/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts
index d61feac5f1a..4580d78b5c5 100644
--- a/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts
+++ b/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts
@@ -140,14 +140,13 @@ export function consumeQueuedChatRunSettings(
const index = threadId
? pendingSettings.findIndex((entry) => entry.threadIds.has(threadId))
: -1;
- // Never consume another chat's snapshot as a fallback. Multiple queued
- // chats can start concurrently, so a "sole pending entry" is not proof that
- // it belongs to this adapter run.
+ // Never consume another chat's snapshot as a fallback. Multiple queued chats can start
+ // concurrently, so a "sole pending entry" is not proof that it belongs to this adapter run.
if (index < 0) {
return null;
}
- // Tool calls can invoke the adapter multiple times for one assistant run.
- // Keep the snapshot available until the owning prompt queue observes that
- // the whole run is idle, then discard it through its registration id.
+ // Tool calls can invoke the adapter multiple times for one assistant run. Keep the snapshot
+ // available until the owning prompt queue observes that the whole run is idle, then discard it
+ // through its registration id.
return pendingSettings[index].settings;
}
diff --git a/studio/frontend/src/features/chat/utils/queued-model-capabilities.ts b/studio/frontend/src/features/chat/utils/queued-model-capabilities.ts
index fa5e17fe042..7a48e772534 100644
--- a/studio/frontend/src/features/chat/utils/queued-model-capabilities.ts
+++ b/studio/frontend/src/features/chat/utils/queued-model-capabilities.ts
@@ -11,11 +11,9 @@ export type QueuedModelCapabilities = Pick<
| "hasVideoInput"
>;
-/**
- * Give a queued run an accurate private model entry without changing the
- * visible chat's model catalog. Status-derived capabilities override stale
- * catalog values, and a model loaded outside Unsloth gets a minimal entry.
- */
+/** Give a queued run an accurate private model entry without changing the visible chat's model
+ * catalog. Status-derived capabilities override stale catalog values, and a model loaded outside
+ * Unsloth gets a minimal entry. */
export function mergeQueuedModelCapabilities(
models: ChatModelRow[],
checkpoint: string,
diff --git a/studio/frontend/src/features/chat/utils/qwen-params.ts b/studio/frontend/src/features/chat/utils/qwen-params.ts
index 1e3fffffe8e..82304a33769 100644
--- a/studio/frontend/src/features/chat/utils/qwen-params.ts
+++ b/studio/frontend/src/features/chat/utils/qwen-params.ts
@@ -7,14 +7,9 @@ import { useChatRuntimeStore } from "../stores/chat-runtime-store";
// cycle. Import it from there directly rather than through this module.
import { resolveQwenThinkingParams } from "./qwen-sampling-table";
-/**
- * Apply Qwen3-family recommended sampling parameters when the Think toggle
- * changes. Qwen3.5, Qwen3.6, and Qwen3.8 also need a presence_penalty bump on
- * top of the Qwen3 defaults.
- *
- * Used by both the thread assistant UI and the shared chat composer so the
- * two call sites stay in sync.
- */
+/** Apply Qwen3-family recommended sampling parameters when the Think toggle changes. Qwen3.5,
+ * Qwen3.6 and Qwen3.8 also need a presence_penalty bump on top of the Qwen3 defaults. Used by
+ * both the thread assistant UI and the shared chat composer. */
export function applyQwenThinkingParams(thinkingOn: boolean): void {
const store = useChatRuntimeStore.getState();
const checkpoint = store.params.checkpoint?.toLowerCase() ?? "";
@@ -22,7 +17,7 @@ export function applyQwenThinkingParams(thinkingOn: boolean): void {
if (params === null || store.activePresetSource !== "builtin-default") {
return;
}
- // Deliberately unmarked, unlike the post-load path applying the same table: the
- // user asked for this mode here, so it must land even on a chat pinning sampling.
+ // Deliberately unmarked, unlike the post-load path applying the same table: the user asked for
+ // this mode here, so it must land even on a chat pinning sampling.
store.setParams({ ...store.params, ...params });
}
diff --git a/studio/frontend/src/features/chat/utils/reasoning-duration.ts b/studio/frontend/src/features/chat/utils/reasoning-duration.ts
index 380b46adfef..009e89a9e48 100644
--- a/studio/frontend/src/features/chat/utils/reasoning-duration.ts
+++ b/studio/frontend/src/features/chat/utils/reasoning-duration.ts
@@ -42,13 +42,10 @@ export function countReasoningGroups(
return getReasoningGroupIndex(parts, parts.length - 1) + 1;
}
-/**
- * Total reasoning text in the LAST reasoning group (the group any new
- * reasoning would join). The adapter compares this across chunks to tell "the
- * model is still thinking" from "the model has moved on to the answer": a
- * provider that closes every reasoning block atomically would otherwise freeze
- * the group's timer at its first close.
- */
+/** Total reasoning text in the LAST reasoning group, the one any new reasoning would join. The
+ * adapter compares this across chunks to tell "still thinking" from "moved on to the answer":
+ * a provider that closes every reasoning block atomically would otherwise freeze the group's
+ * timer at its first close. */
export function lastReasoningGroupTextLength(
parts: readonly MessagePartLike[],
): number {
@@ -90,23 +87,19 @@ export function createReasoningDurationTracker(
now: () => number = Date.now,
) {
let durations: number[] = [];
- // First time each group index became visible. A group can be closed and
- // reopened -- a provider that emits several complete <think>...</think>
- // blocks in a row has them coalesced into one rendered group -- so the
+ // First time each group index became visible. A group can be closed and reopened -- several
+ // complete <think>...</think> blocks in a row are coalesced into one rendered group -- so the
// duration is always measured from the first sighting, not the last.
const startedAt: number[] = [];
let activeIndex: number | null = null;
let groupCount = 0;
- // Reasoning text seen so far per group, used to decide whether a closed
- // group is still growing and should reopen.
+ // Reasoning text seen so far per group, used to decide whether a closed group is still growing and should reopen.
const reasoningLength: number[] = [];
- // The group a server summary would land on. The backend emits one summary at
- // the end of each visible reasoning pass, before the next pass can begin, so
- // "the group that started most recently" is the correct target. (A FIFO queue
- // is tempting but wrong: it mis-assigns as soon as one group has no summary.)
+ // The group a server summary would land on. The backend emits one summary at the end of each
+ // visible reasoning pass, before the next can begin, so "the group that started most recently"
+ // is the correct target. A FIFO queue mis-assigns as soon as one group has no summary.
let serverSummaryTargetIndex: number | null = null;
- // Indices whose duration came from the server; local timing must not
- // overwrite an authoritative value.
+ // Indices whose duration came from the server; local timing must not overwrite an authoritative value.
const serverClaimed = new Set<number>();
const setDuration = (index: number, duration: number) => {
@@ -149,9 +142,8 @@ export function createReasoningDurationTracker(
}
const at = now();
finishGroupAt(at);
- // A single delta can reveal more than one group at once. Any index we
- // skipped became visible and closed within this same chunk, so give it a
- // measured zero rather than leaving a hole in the persisted array.
+ // A single delta can reveal more than one group at once. Any index we skipped became visible and
+ // closed within this same chunk, so give it a measured zero rather than leaving a hole.
for (let skipped = groupCount; skipped < index; skipped += 1) {
if (startedAt[skipped] === undefined) {
startedAt[skipped] = at;
@@ -165,13 +157,10 @@ export function createReasoningDurationTracker(
groupCount = Math.max(groupCount, index + 1);
serverSummaryTargetIndex = index;
},
- /**
- * Reopen a group that already closed, but only while its reasoning text is
- * still growing. Providers that emit each reasoning block as a complete
- * <think>...</think> chunk close the group on every chunk; without this the
- * group would freeze at the first close. Gating on growth is what keeps the
- * timer from running on into the answer.
- */
+ /** Reopen a group that already closed, but only while its reasoning text is still growing.
+ * Providers that emit each reasoning block as a complete chunk close the group every chunk;
+ * without this it would freeze at the first close. Gating on growth is what keeps the timer
+ * from running on into the answer. */
resumeGroup(index: number, currentReasoningLength: number) {
const seen = reasoningLength[index] ?? 0;
if (currentReasoningLength <= seen) {
diff --git a/studio/frontend/src/features/chat/utils/reasoning-visibility.ts b/studio/frontend/src/features/chat/utils/reasoning-visibility.ts
index 588fd228aa9..53d35e3e16b 100644
--- a/studio/frontend/src/features/chat/utils/reasoning-visibility.ts
+++ b/studio/frontend/src/features/chat/utils/reasoning-visibility.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Open/closed rules for a reasoning group, kept out of the component so the
-// streaming and preference interplay stays testable.
+// Open/closed rules for a reasoning group, kept out of the component so the streaming and
+// preference interplay stays testable.
export interface ReasoningOpenStateInput {
/** Group is receiving reasoning deltas. */
isStreaming: boolean;
@@ -14,10 +14,8 @@ export interface ReasoningOpenStateInput {
manualOpen: boolean;
}
-/**
- * Thinking auto opens while it streams and collapses when it finishes, unless
- * the preference is on: then only an explicit open shows it, in either phase.
- */
+/** Thinking auto opens while it streams and collapses when it finishes, unless the preference is
+ * on: then only an explicit open shows it, in either phase. */
export function resolveReasoningOpen({
isStreaming,
collapseByDefault,
@@ -41,11 +39,9 @@ export function reasoningAutoOpensWhileStreaming(
return isStreaming && !collapseByDefault;
}
-/**
- * A new round starts when streaming resumes. Regenerate reuses the component,
- * so last round's open state has to clear in that same render, not in an
- * effect, or the block paints open before collapsing.
- */
+/** A new round starts when streaming resumes. Regenerate reuses the component, so last round's
+ * open state has to clear in that same render, not in an effect, or the block paints open
+ * before collapsing. */
export function startsNewReasoningRound(
isStreaming: boolean,
wasStreaming: boolean,
diff --git a/studio/frontend/src/features/chat/utils/recorded-sandbox-session.ts b/studio/frontend/src/features/chat/utils/recorded-sandbox-session.ts
index e02d21e6799..4e4714df79a 100644
--- a/studio/frontend/src/features/chat/utils/recorded-sandbox-session.ts
+++ b/studio/frontend/src/features/chat/utils/recorded-sandbox-session.ts
@@ -8,19 +8,12 @@ import {
import type { MessageRecord } from "../types";
-/**
- * Every sandbox a chat's tool calls actually wrote to, read back from its
- * history, newest first.
- *
- * Moving a chat between projects rewrites only its `projectId` while the files
- * stay put, so a scope from current membership can name a folder this chat
- * never used; the id recorded on a tool result is the one its files are under.
- * A chat that ran tools on both sides of such a move names both, which is why
- * this returns a list: the newest alone would hide the folder it left behind.
- *
- * Gated on the tool name and the full wrapper shape, as the adapter is: a
- * custom or MCP tool with its own `sessionId` would name an unrelated folder.
- */
+/** Every sandbox a chat's tool calls actually wrote to, read back from its history, newest first.
+ * Moving a chat between projects rewrites only its `projectId` while the files stay put, so a
+ * scope from current membership can name a folder this chat never used; the id recorded on a tool
+ * result is the one its files are under. A chat that ran tools on both sides of such a move names
+ * both, which is why this returns a list. Gated on the tool name and the full wrapper shape, as
+ * the adapter is: a custom or MCP tool with its own `sessionId` would name an unrelated folder. */
export function allRecordedSandboxSessionIds(
messages: MessageRecord[],
): string[] {
diff --git a/studio/frontend/src/features/chat/utils/refresh-context-usage.ts b/studio/frontend/src/features/chat/utils/refresh-context-usage.ts
index 4f14a4596e7..486306da12b 100644
--- a/studio/frontend/src/features/chat/utils/refresh-context-usage.ts
+++ b/studio/frontend/src/features/chat/utils/refresh-context-usage.ts
@@ -31,14 +31,14 @@ function superseded(threadKey: string | null, generation: number): boolean {
return refreshGenerations.get(threadKey) !== generation;
}
-// Threads with a count on the wire. A model load fires two triggers (the post-load call and the
-// modelLoading effect), and both would otherwise render the template and tokenize.
+// Threads with a count on the wire. A model load fires two triggers, and both would otherwise
+// render the template and tokenize.
const countsInFlight = new Set<string | null>();
// A trigger deferred behind an in-flight count, replayed once that count settles WITHOUT
-// publishing. Dropping it loses the bar: a run stopped before it emits usage fires the retry this
-// depends on, and the in-flight count then rejects its now-stale branch. Replaying it
-// unconditionally would restore the doubled model-load count, where the first trigger publishes.
+// publishing. Dropping it loses the bar: a run stopped before it emits usage fires the
+// retry this depends on. Replaying it unconditionally would restore the doubled model-load
+// count, where the first trigger publishes.
const retryAfterInFlight = new Map<string | null, RefreshOptions>();
function storedMessageToRunMessage(record: MessageRecord): ThreadMessage {
@@ -101,11 +101,9 @@ function foldPart(part: unknown, seed: number): number {
return foldHash(serialized, seed);
}
-/**
- * Identity of the branch a count priced. Content is hashed, not measured: a run mutates a turn in
- * place (streaming grows its text; a tool result lands on a part with no `text`), so neither a
- * length nor a part tally sees it. Attachments are folded in because the counter prices them too.
- */
+/** Identity of the branch a count priced. Content is hashed, not measured: a run mutates a
+ * turn in place, so neither a length nor a part tally sees it. Attachments are folded in
+ * because the counter prices them too. */
function branchSignature(messages: readonly ThreadMessage[]): string {
let hash = 0;
let parts = 0;
@@ -129,10 +127,8 @@ type ActiveBranchReader = () => readonly ThreadMessage[] | null;
let readActiveBranch: ActiveBranchReader | null = null;
-/**
- * Publish the mounted runtime's visible branch so the recount prices it instead of the persisted
- * records. Only the single-chat pane registers one; compare panes never own the bar.
- */
+/** Publish the mounted runtime's visible branch so the recount prices it instead of the
+ * persisted records. Only the single-chat pane registers one. */
export function setActiveBranchReader(reader: ActiveBranchReader | null): void {
readActiveBranch = reader;
}
@@ -172,11 +168,10 @@ export async function refreshContextUsage(
if (options?.invalidate) store.setContextUsage(null);
- // Never count while anything is generating: the endpoint refuses, and the recount effect depends
- // on this, so the last run finishing re-fires it. runningByThreadId, not the narrower
- // localRunByThreadId: the endpoint refuses during an external-provider run too, so gating on less
- // than the server refuses on would spend a request to be told 503 and lose the retry -- nothing
- // re-fires this effect when an external run ends unless it is in the dependency array.
+ // Never count while anything is generating: the endpoint refuses, and the recount effect
+ // depends on this so the last run finishing re-fires it. runningByThreadId, not the
+ // narrower local one: the endpoint refuses during an external-provider run too, and
+ // nothing re-fires this effect when an external run ends unless it is in the deps.
if (Object.values(store.runningByThreadId ?? {}).some(Boolean)) return;
const capturedThreadId = threadId ?? null;
@@ -199,10 +194,9 @@ export async function refreshContextUsage(
countsInFlight.add(capturedThreadId);
let published = false;
try {
- // Prefer the mounted runtime: it is what the next request reads from (an incognito thread
- // persists nothing, and after a retry the newest stored leaf is a branch the user left). A
- // captured null is EXCLUDED, not matched: New Chat leaves the outgoing conversation mounted
- // until switchToNewThread() settles, so null === null would price it into the empty chat.
+ // Prefer the mounted runtime: it is what the next request reads from. A captured null is
+ // EXCLUDED, not matched: New Chat leaves the outgoing conversation mounted until
+ // switchToNewThread() settles, so null === null would price it into the empty chat.
const readOwnBranch = (): readonly ThreadMessage[] | null =>
capturedThreadId != null &&
useChatRuntimeStore.getState().activeThreadId === capturedThreadId
@@ -215,7 +209,7 @@ export async function refreshContextUsage(
// Re-read before publishing, so a turn sent while this count was in flight drops it.
let countedBranch: string | null = null;
// The stored fallback's witness: storage records and ThreadMessages hash differently, so only
- // ids survive both, and the last one moves as soon as a turn is sent or an edit mints one.
+ // ids survive both, and the last one moves as soon as a turn is sent.
let countedLastId: string | null = null;
const fromLiveBranch = Boolean(liveBranch && liveBranch.length > 0);
if (fromLiveBranch) {
@@ -228,22 +222,19 @@ export async function refreshContextUsage(
);
}
- // /chat/count_tokens always 503s on images: /apply-template swaps each for a marker. Declining
- // before the hash below keeps the base64 out of it and out of a request body that can reach
- // megabytes, both synchronous on the UI thread.
+ // /chat/count_tokens always 503s on images and /apply-template swaps each for a marker.
+ // Declining before the hash keeps the base64 out of it and out of a request body that can
+ // reach megabytes, both synchronous on the UI thread.
if (messagesContainImage(runMessages)) return;
- // The real request replays the newest user audio as audio_base64 but toOpenAIMessages has
- // no audio branch, so counting would price a text-only prompt. Decline as images do.
+ // The real request replays the newest user audio as audio_base64 but toOpenAIMessages has no
+ // audio branch, so counting would price a text-only prompt. Decline as images do.
if (findLatestUserAudioBase64(runMessages)) return;
- // Same for video, and more so: the real request replays the clip as
- // video_base64 and llama-server expands it into frames, while
- // toOpenAIMessages has no video branch -- so counting would price a
- // text-only prompt and the bar would show room the window does not have.
- // /chat/count_tokens 503s on video for the same reason. Declining here also
- // keeps up to 85 MB of base64 out of branchSignature's JSON.stringify,
- // which is the synchronous main-thread cost the image bail above exists for.
+ // Same for video: the request replays the clip as video_base64 and llama-server expands it
+ // into frames, while toOpenAIMessages has no video branch, so the bar would show room the
+ // window does not have. Declining also keeps up to 85 MB of base64 out of
+ // branchSignature's JSON.stringify, the same main-thread cost as the image bail.
if (findLatestUserVideoBase64(runMessages)) return;
if (fromLiveBranch) {
@@ -253,7 +244,7 @@ export async function refreshContextUsage(
}
// A completion finishing mid-count writes exact usage for a turn this count predates, so drop
- // the recount rather than roll the bar backwards. Sampled once runMessages is fixed.
+ // the recount rather than roll the bar backwards.
const usageBeforeCount = useChatRuntimeStore.getState().contextUsage;
// undefined, not null: a chat with no persisted thread has no project to resolve from.
@@ -277,8 +268,8 @@ export async function refreshContextUsage(
});
if (stale()) return;
- // The response type is a compile-time assertion only: anything else answering 200 here would
- // put undefined on the bar and throw from toLocaleString.
+ // The response type is a compile-time assertion only: anything else answering 200 would put
+ // undefined on the bar and throw from toLocaleString.
if (typeof inputTokens !== "number" || !Number.isFinite(inputTokens)) return;
// The endpoint counts with whatever is resident, never the model asked for: a load from
// another tab returns another tokenizer's total, which the checkpoint guards cannot see.
@@ -297,9 +288,9 @@ export async function refreshContextUsage(
if (useChatRuntimeStore.getState().runningByThreadId[capturedThreadId ?? "__default"]) {
return;
}
- // The usage snapshot above only sees a completion that WROTE usage, so a run stopped before
- // emitting any leaves it equal and the branch is the only witness. An empty current branch is
- // a mismatch: deleting the sole exchange mid-count would leave the old total on the thread.
+ // The usage snapshot only sees a completion that WROTE usage, so a run stopped before
+ // emitting any leaves it equal and the branch is the only witness. An empty current branch
+ // is a mismatch: deleting the sole exchange mid-count would leave the old total.
if (countedBranch != null) {
const current = readActiveBranch?.();
if (current != null && branchSignature(current) !== countedBranch) {
diff --git a/studio/frontend/src/features/chat/utils/repair-legacy-chat-titles.ts b/studio/frontend/src/features/chat/utils/repair-legacy-chat-titles.ts
index 9aed8d5e81a..00787987ff7 100644
--- a/studio/frontend/src/features/chat/utils/repair-legacy-chat-titles.ts
+++ b/studio/frontend/src/features/chat/utils/repair-legacy-chat-titles.ts
@@ -18,8 +18,7 @@ import { type GuardProbe, readGuardProbe } from "./openapi-support";
import { runWithConcurrency } from "./run-with-concurrency";
import { createSerialQueue } from "./serial-queue";
-/** Threads already tried, so ones that could not be rewritten stay off every
- * later refresh. */
+/** Threads already tried, so ones that could not be rewritten stay off every later refresh. */
const attempted = new Set<string>();
/** Rows per pass, so a long history drains in pages. */
@@ -31,17 +30,17 @@ const REPAIR_CONCURRENCY = 4;
/** Breather between pages. */
const REPAIR_PAGE_PAUSE_MS = 500;
-/** Several sidebars can be mounted at once, so REPAIR_CONCURRENCY only holds if
- * their passes queue rather than overlap. */
+/** Several sidebars can be mounted at once, so REPAIR_CONCURRENCY only holds if their passes
+ * queue rather than overlap. */
const serial = createSerialQueue();
-/** Cached once the served schema answers. A failed probe is not cached, so a
- * startup hiccup does not park the migration for the session. */
+/** Cached once the served schema answers. A failed probe is not cached, so a startup hiccup does
+ * not park the migration for the session. */
let guardSupport: Promise<boolean> | null = null;
-/** Whether this backend enforces the conditional title patch. Probing by
- * sending one would let an older backend apply the write, the very harm being
- * checked for, so the served schema answers; anything unreadable is a no. */
+/** Whether this backend enforces the conditional title patch. Probing by sending one would let
+ * an older backend apply the write, the very harm being checked for, so the served schema
+ * answers; anything unreadable is a no. */
function backendEnforcesTitleGuard(): Promise<boolean> {
guardSupport ??= (async () => {
let probe: GuardProbe = { supported: false, settled: false };
@@ -69,8 +68,7 @@ export function repairLegacyChatTitles(
}
async function runRepairPass(threads: ThreadRecord[]): Promise<number> {
- // Claim nothing until the guard is known: a rewrite that can silently beat a
- // rename is not worth a tidier title.
+ // Claim nothing until the guard is known: a rewrite that can silently beat a rename is not worth a tidier title.
if (!(await backendEnforcesTitleGuard())) return 0;
const { candidates, rest, hasMore } = selectLegacyRepairPage(
@@ -84,8 +82,8 @@ async function runRepairPass(threads: ThreadRecord[]): Promise<number> {
let messages: Map<string, MessageRecord[]>;
try {
- // Not the caller's earlier map: the backend's own view, taken as late as
- // possible, is what the rewrite has to be based on. One batched call.
+ // Not the caller's earlier map: the backend's own view, taken as late as possible, is what the
+ // rewrite has to be based on. One batched call.
messages = await batchListChatMessages(ids);
} catch {
// Nothing was decided, so let a later refresh try these again.
@@ -93,13 +91,12 @@ async function runRepairPass(threads: ThreadRecord[]): Promise<number> {
return 0;
}
- // Backend messages only: Dexie keeps rows the backend has pruned, so merging
- // the two could put a deleted prompt back into a title. A chat not imported
- // yet reads as unknown below and is retried once its messages land.
+ // Backend messages only: Dexie keeps rows the backend has pruned, so merging the two could put
+ // a deleted prompt back into a title. A chat not imported yet reads as unknown below.
const repairs = planLegacyTitleRepairs(candidates, messages);
- // Nothing stored means either mid-import or emptied by the user; the ledger
- // tells them apart, and is only fetched when there is something to decide.
+ // Nothing stored means either mid-import or emptied by the user; the ledger tells them apart,
+ // and is only fetched when there is something to decide.
const withoutMessages = threadsMissingMessages(ids, messages);
if (withoutMessages.length > 0) {
let imported = new Set<string>();
@@ -116,12 +113,10 @@ async function runRepairPass(threads: ThreadRecord[]): Promise<number> {
let repaired = 0;
await runWithConcurrency(repairs, REPAIR_CONCURRENCY, async (repair) => {
try {
- // The backend PATCH directly, not updateStoredChatThread: that ensures
- // the thread first, re-importing one deleted on another client from the
- // Dexie rows still here, and a migration must never create anything.
- // Both guards answer 409 if a rename or a delete of the opening prompt
- // lands first, and a title patch leaves updatedAt alone, so Recents keeps
- // its order.
+ // The backend PATCH directly, not updateStoredChatThread: that ensures the thread first,
+ // re-importing one deleted on another client, and a migration must never create anything.
+ // Both guards answer 409 if a rename or a delete of the opening prompt lands first, and a
+ // title patch leaves updatedAt alone, so Recents keeps its order.
await updateChatThread(
repair.threadId,
{ title: repair.title },
@@ -136,8 +131,8 @@ async function runRepairPass(threads: ThreadRecord[]): Promise<number> {
}
});
- // A page that wrote nothing fires no history update, so schedule the next one
- // here. It runs on `rest`, so rows this page unmarked are not drawn again.
+ // A page that wrote nothing fires no history update, so schedule the next one here. It runs on
+ // `rest`, so rows this page unmarked are not drawn again.
if (hasMore) {
setTimeout(() => {
void repairLegacyChatTitles(rest).catch(() => undefined);
diff --git a/studio/frontend/src/features/chat/utils/reply-source-markdown.ts b/studio/frontend/src/features/chat/utils/reply-source-markdown.ts
index 03d7e4c5407..7da5f478b81 100644
--- a/studio/frontend/src/features/chat/utils/reply-source-markdown.ts
+++ b/studio/frontend/src/features/chat/utils/reply-source-markdown.ts
@@ -6,13 +6,10 @@ import {
renderConversationBlocks,
} from "./conversation-markdown";
-/**
- * One assistant reply as the markdown its saved source holds. The reply's own
- * reasoning, tool calls and citations are part of what a "save this reply"
- * click asks to keep, and getCopyText() concatenates text parts alone: they
- * would be dropped, and a reply that is only a tool call would read as empty.
- * Same conversion the whole-chat save runs.
- */
+/** One assistant reply as the markdown its saved source holds. The reply's own reasoning, tool calls
+ * and citations are part of what a "save this reply" click asks to keep, and getCopyText()
+ * concatenates text parts alone, so they would be dropped and a reply that is only a tool call
+ * would read as empty. Same conversion the whole-chat save runs. */
export function replySourceMarkdown(
content: unknown,
normalizeToolResult?: (result: unknown, toolName?: string) => unknown,
diff --git a/studio/frontend/src/features/chat/utils/research-message-sync.ts b/studio/frontend/src/features/chat/utils/research-message-sync.ts
index a9d15da1e01..cd6ac58cc81 100644
--- a/studio/frontend/src/features/chat/utils/research-message-sync.ts
+++ b/studio/frontend/src/features/chat/utils/research-message-sync.ts
@@ -18,18 +18,12 @@ export function hasResearchMetadata(metadata: unknown): boolean {
return RESEARCH_METADATA_KEYS.some((key) => key in keys);
}
-/**
- * Replace the client's copy of every server-managed research message with the stored one.
- *
- * The runtime keeps client-only fields on a research turn (the live `researchRun` object, a
- * `serverRevision`, streamed content parts), so mirroring the repository verbatim asks the
- * backend to edit messages it owns. It answers 409 and drops the whole payload, which cost the
- * thread its autosave and made deleting any message in a research thread fail. A faithful copy
- * is the no-op the guard is designed to accept.
- *
- * The research prompt carries no metadata of its own, so it is identified as the parent of the
- * report, matching the user_message_id/assistant_message_id pair the backend protects.
- */
+/** Replace the client's copy of every server-managed research message with the stored one. The
+ * runtime keeps client-only fields on a research turn (the live `researchRun`, a
+ * `serverRevision`, streamed content parts), so mirroring the repository verbatim asks the
+ * backend to edit messages it owns. It answers 409 and drops the whole payload, which cost the
+ * thread its autosave. A faithful copy is the no-op the guard accepts. The research prompt
+ * carries no metadata of its own, so it is identified as the parent of the report. */
export function reconcileServerManagedMessages(
records: MessageRecord[],
stored: MessageRecord[],
@@ -45,8 +39,8 @@ export function reconcileServerManagedMessages(
return records.map((record) => {
if (!serverManaged.has(record.id)) return record;
const stored = storedById.get(record.id);
- // parentId stays the client's: deleting the message a research prompt hung off relinks it,
- // and echoing the stored parent would persist a link to a row the same sync then prunes.
+ // parentId stays the client's: deleting the message a research prompt hung off relinks it, and
+ // echoing the stored parent would persist a link to a row the same sync then prunes.
return stored ? { ...stored, parentId: record.parentId } : record;
});
}
diff --git a/studio/frontend/src/features/chat/utils/research-run-binding.ts b/studio/frontend/src/features/chat/utils/research-run-binding.ts
index c7d6e2b028a..c26fa55a2e1 100644
--- a/studio/frontend/src/features/chat/utils/research-run-binding.ts
+++ b/studio/frontend/src/features/chat/utils/research-run-binding.ts
@@ -1,18 +1,12 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Whether a reply still owns the research run its metadata names.
- *
- * A thread holds one run for its lifetime, so a stopped run is re-pointed at the next question
- * and keeps its id. The server moves the binding to the new reply and clears it from the old
- * row, but a message already in memory keeps the copy it was loaded with -- and the run store
- * is keyed by run id, so the stopped reply would render whatever that run is doing now: the new
- * question's live card, and then its report, in two places at once.
- *
- * The run's own `assistantMessageId` is the answer, since that is exactly what the server moves.
- * A run that never named one (created without a placeholder) belongs to whoever asks.
- */
+/** Whether a reply still owns the research run its metadata names. A thread holds one run for its
+ * lifetime, so a stopped run is re-pointed at the next question and keeps its id. The server
+ * moves the binding to the new reply, but a message already in memory keeps the copy it was
+ * loaded with, and the run store is keyed by run id, so the stopped reply would render whatever
+ * that run is doing now, in two places at once. The run's own `assistantMessageId` is the answer,
+ * since that is exactly what the server moves. A run that never named one belongs to whoever asks. */
export function researchReplyOwnsRun(
boundAssistantMessageId: unknown,
messageId: unknown,
diff --git a/studio/frontend/src/features/chat/utils/row-selection.ts b/studio/frontend/src/features/chat/utils/row-selection.ts
index 9b407492bf5..89b29a9932d 100644
--- a/studio/frontend/src/features/chat/utils/row-selection.ts
+++ b/studio/frontend/src/features/chat/utils/row-selection.ts
@@ -1,10 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Multi-select for sidebar rows: cmd or ctrl click toggles one row, shift click
- * takes the block between the anchor and the clicked row.
- */
+/** Multi-select for sidebar rows: cmd or ctrl click toggles one row, shift click takes the block
+ * between the anchor and the clicked row. */
/** Rows from `anchorId` to `targetId` inclusive, in list order, either way round. */
export function rangeBetween(
@@ -14,8 +12,8 @@ export function rangeBetween(
): string[] {
const from = ids.indexOf(anchorId);
const to = ids.indexOf(targetId);
- // A missing anchor means the list changed under the selection, so the click
- // stands on its own rather than selecting an arbitrary block.
+ // A missing anchor means the list changed under the selection, so the click stands on its own
+ // rather than selecting an arbitrary block.
if (from === -1 || to === -1) return to === -1 ? [] : [targetId];
return from <= to ? ids.slice(from, to + 1) : ids.slice(to, from + 1);
}
diff --git a/studio/frontend/src/features/chat/utils/run-checkpoint-scheduler.ts b/studio/frontend/src/features/chat/utils/run-checkpoint-scheduler.ts
index 11436daffb2..2ab45055b22 100644
--- a/studio/frontend/src/features/chat/utils/run-checkpoint-scheduler.ts
+++ b/studio/frontend/src/features/chat/utils/run-checkpoint-scheduler.ts
@@ -6,15 +6,11 @@
/** Quiet time after a checkpoint settles before the next one is taken. */
export const RUN_CHECKPOINT_INTERVAL_MS = 8_000;
-/**
- * How long one thread may be checkpointed before the schedule gives up on it. A run that
- * never terminalises would otherwise checkpoint for the life of the page.
- *
- * Never rearmed, unlike the follow deadline in chat-generation-api.ts. That is safe only
- * because the cap applies to durable runs alone (see `isBounded`), which the server
- * persists; a subscriber-owned stream, whose only persistence IS these saves, is never
- * capped.
- */
+/** How long one thread may be checkpointed before the schedule gives up on it. A run that never
+ * terminalises would otherwise checkpoint for the life of the page. Never rearmed, unlike the
+ * follow deadline in chat-generation-api.ts. That is safe only because the cap applies to
+ * durable runs alone (see `isBounded`), which the server persists; a subscriber-owned stream,
+ * whose only persistence IS these saves, is never capped. */
export const RUN_CHECKPOINT_MAX_DURATION_MS = 30 * 60_000;
/** Injectable so a test can drive the schedule without real time passing. */
@@ -32,10 +28,8 @@ export type RunCheckpointScheduler = {
stop: (threadId: string) => void;
/** Stop every thread, for unmount. */
stopAll: () => void;
- /**
- * Checkpoint every live thread right now, leaving the schedule untouched. For the page
- * transitions that precede a renderer going away or being throttled.
- */
+ /** Checkpoint every live thread right now, leaving the schedule untouched. For the page
+ * transitions that precede a renderer going away or being throttled. */
flushAll: () => void;
};
@@ -59,22 +53,16 @@ export function createRunCheckpointScheduler(
options: {
intervalMs?: number;
timers?: RunCheckpointTimers;
- /**
- * Whether the thread's run is still going. runEnd is the fast path out, but it only
- * reaches whichever thread is main, so one that stops being main mid-run would
- * checkpoint for the life of the page. Omit it to treat every thread as active.
- */
+ /** Whether the thread's run is still going. runEnd is the fast path out, but it only reaches
+ * whichever thread is main, so one that stops being main mid-run would checkpoint for the
+ * life of the page. Omit it to treat every thread as active. */
isActive?: (threadId: string) => boolean;
- /**
- * Wall-clock cap on one thread's schedule. `isActive` cannot serve as one: it reports
- * the runtime's own `isRunning`, which a run that never terminalises holds true.
- */
+ /** Wall-clock cap on one thread's schedule. `isActive` cannot serve as one: it reports the
+ * runtime's own `isRunning`, which a run that never terminalises holds true. */
maxDurationMs?: number;
- /**
- * Whether `maxDurationMs` applies to this thread. A subscriber-owned stream persists
- * ONLY through these checkpoints, so capping one would lose everything it streamed
- * after the cap if the page went away before `runEnd`. Omit to cap every thread.
- */
+ /** Whether `maxDurationMs` applies to this thread. A subscriber-owned stream persists ONLY
+ * through these checkpoints, so capping one would lose everything it streamed after the cap
+ * if the page went away before `runEnd`. Omit to cap every thread. */
isBounded?: (threadId: string) => boolean;
} = {},
): RunCheckpointScheduler {
@@ -125,8 +113,8 @@ export function createRunCheckpointScheduler(
schedule(threadId, state);
}
};
- // Both exits take a final save: a schedule that ends without one has lost
- // whatever the last interval produced, exactly as a missed runEnd would.
+ // Both exits take a final save: a schedule that ends without one has lost whatever the last
+ // interval produced, exactly as a missed runEnd would.
const capped =
now() - state.startedAt >= maxDurationMs && isBoundedRun(threadId);
if (!isRunning(threadId) || capped) {
diff --git a/studio/frontend/src/features/chat/utils/settings-retry.ts b/studio/frontend/src/features/chat/utils/settings-retry.ts
index 539434af53d..960c45d587d 100644
--- a/studio/frontend/src/features/chat/utils/settings-retry.ts
+++ b/studio/frontend/src/features/chat/utils/settings-retry.ts
@@ -1,19 +1,15 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Which failed settings writes are worth keeping, and which have to be let go.
-//
-// The settings queue coalesces every change into one patch and requeues it when the
-// PUT fails, which is right for a server that is down and wrong for a server that
-// refuses this patch. /api/chat/settings is extra="forbid" and rejects the WHOLE
-// body on one bad field, so a permanently-rejected value requeued forever rides
-// along with every later save and takes it down too: after one 400 no chat setting
-// can be persisted again for the life of the tab. Measured in Chromium, Firefox and
-// WebKit against a live Unsloth before this existed.
-//
-// Reachable without a bug on either side, whenever the two versions differ: a new
-// bundle against a rolled-back backend has every mirrored field rejected as
-// extra_forbidden, which is exactly the shape below.
+// Which failed settings writes are worth keeping, and which have to be let go. The settings
+// queue coalesces every change into one patch and requeues it when the PUT fails, which is
+// right for a server that is down and wrong for one that refuses this patch.
+// /api/chat/settings is extra="forbid" and rejects the WHOLE body on one bad field, so a
+// permanently-rejected value requeued forever takes every later save down with it: after one
+// 400 no chat setting can persist again for the life of the tab. Reachable without a bug on
+// either side whenever the two versions differ.
+// A new bundle against a rolled-back backend has every mirrored field rejected as extra_forbidden,
+// which is exactly the shape below.
/** A non-2xx answer from the settings endpoint, carrying what the server said. */
export class ChatSettingsRequestError extends Error {
@@ -28,13 +24,9 @@ export class ChatSettingsRequestError extends Error {
}
}
-/**
- * Whether retrying this failure could ever succeed.
- *
- * A network error, a 5xx or a timeout is transient, so the patch is kept. A 4xx is
- * the server saying this body is wrong; sending it again cannot help. 408 and 429
- * are the two 4xx that explicitly mean "later", so they stay retryable.
- */
+/** Whether retrying this failure could ever succeed. A network error, a 5xx or a timeout is
+ * transient, so the patch is kept. A 4xx is the server saying this body is wrong. 408 and 429
+ * are the two 4xx that explicitly mean "later", so they stay retryable. */
export function isTerminalSettingsRejection(error: unknown): boolean {
if (!(error instanceof ChatSettingsRequestError)) return false;
const { status } = error;
@@ -42,17 +34,11 @@ export function isTerminalSettingsRejection(error: unknown): boolean {
return status >= 400 && status < 500;
}
-/**
- * The top-level setting names a validation error blames, e.g. `["ragTopK"]`.
- *
- * FastAPI answers a pydantic failure with `detail: [{loc: ["ragTopK"], ...}]`. Only
- * the first element of `loc` is used: a nested failure (`["inferenceParams",
- * "temperature"]`) means that whole setting is unsendable as it stands, and the
- * queue works in whole settings.
- *
- * An empty result means the field could not be identified, so the caller drops the
- * patch outright rather than guessing.
- */
+/** The top-level setting names a validation error blames, e.g. `["ragTopK"]`. FastAPI answers a
+ * pydantic failure with `detail: [{loc: ["ragTopK"], ...}]`. Only the first element of `loc`
+ * is used: a nested failure means that whole setting is unsendable, and the queue works in
+ * whole settings. An empty result means the field could not be identified, so the caller
+ * drops the patch rather than guessing. */
export function rejectedSettingKeys(detail: unknown): string[] {
if (!Array.isArray(detail)) return [];
const keys = new Set<string>();
@@ -66,17 +52,11 @@ export function rejectedSettingKeys(detail: unknown): string[] {
return [...keys];
}
-/**
- * The part of `patch` still worth sending after `error`.
- *
- * Returns the patch unchanged when the failure is transient. On a terminal
- * rejection it drops the named fields and keeps the rest, so one bad value cannot
- * take the other settings in the same coalesced patch with it; when no field can be
- * named, everything is dropped.
- *
- * `progressed` is what makes the retry safe to schedule: it is only true when the
- * patch got strictly smaller, so a retry loop is bounded by the number of fields.
- */
+/** The part of `patch` still worth sending after `error`. Returns the patch unchanged when the
+ * failure is transient. On a terminal rejection it drops the named fields and keeps the rest,
+ * so one bad value cannot take the other settings with it; when no field can be named,
+ * everything is dropped. `progressed` is only true when the patch got strictly smaller, which
+ * is what bounds the retry loop by the number of fields. */
export function retryablePatchAfterFailure<T extends object>(
patch: T,
error: unknown,
@@ -102,17 +82,15 @@ export function retryablePatchAfterFailure<T extends object>(
}
// The Fetch standard caps the TOTAL in-flight keepalive body at 64 KiB, and a valid
-// researchWebsitePolicy carries up to 2000 domains of 253 characters, so a settings
-// patch can be an order of magnitude over it. Over the budget the request fails
-// immediately in every engine (measured: "Failed to fetch" in Chromium,
-// "NetworkError" in Firefox, "Load failed" in WebKit), so sending without keepalive
-// is a chance rather than a certain failure -- and on the visibilitychange flush,
-// where the page is only hidden, it simply succeeds.
+// researchWebsitePolicy carries up to 2000 domains of 253 characters, so a settings patch can
+// be an order of magnitude over it. Over the budget the request fails immediately in every
+// engine, so sending without keepalive is a chance rather than a certain failure -- and on
+// the visibilitychange flush, where the page is only hidden, it simply succeeds.
const KEEPALIVE_BODY_BUDGET_BYTES = 60 * 1024;
export function isUnderKeepaliveBudget(body: string): boolean {
- // A JSON body is at least one byte per UTF-16 unit, so this cheap check settles
- // every ordinary patch without encoding a copy of a large one.
+ // A JSON body is at least one byte per UTF-16 unit, so this cheap check settles every ordinary
+ // patch without encoding a copy of a large one.
if (body.length <= KEEPALIVE_BODY_BUDGET_BYTES / 3) return true;
return new TextEncoder().encode(body).byteLength <= KEEPALIVE_BODY_BUDGET_BYTES;
}
diff --git a/studio/frontend/src/features/chat/utils/stop-chat-thread.ts b/studio/frontend/src/features/chat/utils/stop-chat-thread.ts
index 6e4529f0c9e..79353d97504 100644
--- a/studio/frontend/src/features/chat/utils/stop-chat-thread.ts
+++ b/studio/frontend/src/features/chat/utils/stop-chat-thread.ts
@@ -7,15 +7,11 @@ import {
} from "../api/chat-generation-api";
import { useChatRuntimeStore } from "../stores/chat-runtime-store";
-/**
- * Ask the server to stop whatever it still has running for this thread.
- *
- * The registries below are module-scoped React state, so a page reload empties them, while
- * a durable run is server-owned and outlives the tab that started it: the UI says a reply
- * is generating and every stop path says there is nothing to stop. The server's active-run
- * list still knows, so a thread with no local handle falls back to it. Fire and forget;
- * a thread with no run just reads an empty list.
- */
+/** Ask the server to stop whatever it still has running for this thread. The registries below are
+ * module-scoped React state, so a page reload empties them, while a durable run is server-owned
+ * and outlives the tab that started it: the UI says a reply is generating and every stop path
+ * says there is nothing to stop. The server's active-run list still knows, so a thread with no
+ * local handle falls back to it. Fire and forget. */
function stopServerRunsForThread(threadId: string): void {
void getActiveChatGenerationRuns(threadId)
.then((runs) => {
@@ -24,21 +20,17 @@ function stopServerRunsForThread(threadId: string): void {
}
})
.catch(() => {
- // No durable-run endpoint, or the backend is gone. Either way there is nothing
- // further this client can do about a run it holds no handle for.
+ // No durable-run endpoint, or the backend is gone. Either way there is nothing further this
+ // client can do about a run it holds no handle for.
});
}
-/**
- * Stop one conversation's generation, visible or not. Returns true if a stop was dispatched.
- *
- * `cancelByThreadId` is assistant-ui's `cancelRun()`, registered only for the thread on screen;
- * `serverCancelByThreadId` is registered for every run and POSTs that run's own `cancel_id`, so
- * it is the only handle a background conversation has. Both are per-run. Runs with an unresolved
- * thread id share the "__default" key, so stop every handle filed under it. With none of the
- * three, the server is asked directly rather than the call reporting nothing to do, so the
- * return means "a stop was sent", not "a local handle existed".
- */
+/** Stop one conversation's generation, visible or not. Returns true if a stop was dispatched.
+ * `cancelByThreadId` is assistant-ui's `cancelRun()`, registered only for the thread on screen;
+ * `serverCancelByThreadId` is registered for every run and POSTs that run's own `cancel_id`, so
+ * it is the only handle a background conversation has. Runs with an unresolved thread id share
+ * the "__default" key, so stop every handle filed under it. With none of the three, the server
+ * is asked directly, so the return means "a stop was sent", not "a local handle existed". */
export function stopChatThread(threadId: string | null | undefined): boolean {
if (!threadId) return false;
const { runningByThreadId, cancelByThreadId, serverCancelByThreadId } =
diff --git a/studio/frontend/src/features/chat/utils/stream-pacing.ts b/studio/frontend/src/features/chat/utils/stream-pacing.ts
index 7c731fe066e..245f145625e 100644
--- a/studio/frontend/src/features/chat/utils/stream-pacing.ts
+++ b/studio/frontend/src/features/chat/utils/stream-pacing.ts
@@ -1,28 +1,21 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// A janked window may not paint for a while, so do not wait on frames alone.
-// A scheduled fallback, not a deadline: a hidden page pauses frames and
-// throttles this timer too, which is why the cap below is in arrivals.
+// A janked window may not paint for a while, so do not wait on frames alone. A scheduled
+// fallback, not a deadline: a hidden page pauses frames and throttles this timer too, which is
+// why the cap below is in arrivals.
export const UNPAINTED_REOPEN_MS = 500;
-/**
- * Text a closed gate may hold before it publishes regardless.
- *
- * assistant-ui drops whatever a run yields after an abort, so Stop keeps only the
- * last published text. Frames bound that hold in time, not in volume, so this
- * bounds it in volume. Several frames' worth, to bind only when frames are far apart.
- */
+/** Text a closed gate may hold before it publishes regardless. assistant-ui drops whatever a run
+ * yields after an abort, so Stop keeps only the last published text. Frames bound that hold in
+ * time, not in volume, so this bounds it in volume. */
export const MAX_HELD_CHARS = 256;
-/**
- * Paces streamed publishes to one per frame while capping what a stop would discard.
- *
- * `streamed` counts the characters the caller has received. It must only grow, since
- * the cap is on the arrivals a publish has yet to carry. The gate closes on a publish
- * and reopens on the next frame, or on UNPAINTED_REOPEN_MS when no frame comes; a
- * closed gate publishes anyway once MAX_HELD_CHARS have arrived since the last one.
- */
+/** Paces streamed publishes to one per frame while capping what a stop would discard. `streamed`
+ * counts the characters the caller has received and must only grow, since the cap is on the
+ * arrivals a publish has yet to carry. The gate closes on a publish and reopens on the next
+ * frame, or on UNPAINTED_REOPEN_MS when no frame comes; a closed gate publishes anyway once
+ * MAX_HELD_CHARS have arrived. */
export function createStreamPublishGate(): (streamed: number) => boolean {
let open = true;
let publishedAt = 0;
@@ -35,8 +28,7 @@ export function createStreamPublishGate(): (streamed: number) => boolean {
open = false;
// Per cycle, so the loser of the previous race cannot reopen this one.
let reopened = false;
- // Assigned before reopen closes over them, so a synchronous scheduler
- // cannot hit the temporal dead zone.
+ // Assigned before reopen closes over them, so a synchronous scheduler cannot hit the temporal dead zone.
const handles: {
frame?: number;
timer?: ReturnType<typeof setTimeout>;
diff --git a/studio/frontend/src/features/chat/utils/studio-tool-history.ts b/studio/frontend/src/features/chat/utils/studio-tool-history.ts
index 58dd327145c..5f9a929c85f 100644
--- a/studio/frontend/src/features/chat/utils/studio-tool-history.ts
+++ b/studio/frontend/src/features/chat/utils/studio-tool-history.ts
@@ -13,10 +13,8 @@ export type ToolHistoryMessage = {
};
export type StudioToolHistoryOptions = {
- /**
- * When set, only these tool-call parts count. Use this to ignore provider
- * builtins that OpenAI serialization drops, so ownership matches the payload.
- */
+ /** When set, only these tool-call parts count. Use this to ignore provider builtins that OpenAI
+ * serialization drops, so ownership matches the payload. */
toolCallSurvives?: (part: ToolHistoryPart) => boolean;
};
diff --git a/studio/frontend/src/features/chat/utils/thread-scoped-settings.ts b/studio/frontend/src/features/chat/utils/thread-scoped-settings.ts
index 3d378c2c376..13ba314a115 100644
--- a/studio/frontend/src/features/chat/utils/thread-scoped-settings.ts
+++ b/studio/frontend/src/features/chat/utils/thread-scoped-settings.ts
@@ -2,8 +2,8 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
// The chat settings that describe one conversation rather than the installation: the composer
-// pills, the permission level, the retrieval controls and the sampling params. Editing one with
-// a chat open writes this snapshot onto the thread, and reopening that thread applies it back.
+// pills, the permission level, the retrieval controls and the sampling params. Editing one with a
+// chat open writes this snapshot onto the thread, and reopening that thread applies it back.
import type {
PermissionMode,
@@ -148,7 +148,7 @@ export function isThreadScopedSettingKey(
return THREAD_SCOPED_SETTING_KEY_SET.has(key);
}
-// derived on apply, so unstored, but loadPermissionMode falls back to the confirm toggle: writing
+// Derived on apply, so unstored, but loadPermissionMode falls back to the confirm toggle: writing
// it globally would turn one chat's permission level into every other browser's default.
const THREAD_DERIVED_SETTING_KEYS: ReadonlySet<string> = new Set([
"confirmToolCalls",
diff --git a/studio/frontend/src/features/chat/utils/tool-status.ts b/studio/frontend/src/features/chat/utils/tool-status.ts
index d57cea759b3..d7a2c2057db 100644
--- a/studio/frontend/src/features/chat/utils/tool-status.ts
+++ b/studio/frontend/src/features/chat/utils/tool-status.ts
@@ -6,13 +6,14 @@ export const NUDGE_TOOL_CALLS_STATUS = "Nudging tool calls";
export type ToolStatusKind = "nudge" | "terminal" | "web";
-/** Which glyph the badge shows: exact match for the nudge, "Running"/"Editing" prefix for sandbox tools, globe otherwise. */
+/** Which glyph the badge shows: exact match for the nudge, "Running"/"Editing" prefix for sandbox
+ * tools, globe otherwise. */
export function toolStatusKind(status: string): ToolStatusKind {
if (status === NUDGE_TOOL_CALLS_STATUS) {
return "nudge";
}
- // edit_file reports "Editing: name", not "Running", and it is as local as the
- // other two. Without it a file edit shows the globe, i.e. the web-search glyph.
+ // edit_file reports "Editing: name", not "Running", and it is as local as the other two. Without
+ // it a file edit shows the globe, the web-search glyph.
return status.startsWith("Running") || status.startsWith("Editing")
? "terminal"
: "web";
diff --git a/studio/frontend/src/features/chat/utils/trailing-template-placeholder.ts b/studio/frontend/src/features/chat/utils/trailing-template-placeholder.ts
index ccf6c9e08fa..ca7efeb7a46 100644
--- a/studio/frontend/src/features/chat/utils/trailing-template-placeholder.ts
+++ b/studio/frontend/src/features/chat/utils/trailing-template-placeholder.ts
@@ -1,54 +1,34 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Some external providers (mistral magistral, occasionally) leak a trailing
- * template-literal fragment such as `${answer}` into the stream, so the adapter
- * strips one off the end of the accumulated reply on every arrival.
- *
- * The pattern below is anchored at the end, but a regex engine still tries it at
- * every start offset, so running it over the whole buffer costs O(reply) per
- * arrival and O(reply^2) over a reply. This module runs the same pattern over a
- * bounded suffix instead.
- */
+/** Some external providers leak a trailing template-literal fragment such as `${answer}` into
+ * the stream, so the adapter strips one off the end of the accumulated reply on every
+ * arrival. The pattern is anchored at the end, but a regex engine still tries it at every
+ * start offset, so running it over the whole buffer costs O(reply) per arrival and
+ * O(reply^2) over a reply. This module runs the same pattern over a bounded suffix. */
-/**
- * How far back the opening `${` may sit. Providers emit short placeholders, so
- * an opener further back than this is not treated as one.
- *
- * The guarantee is about what is REMOVED, not about what is left whole: this
- * removes no more than the unbounded pattern would, and always returns a prefix
- * of the input, so it can never delete text the unbounded pattern kept. Asserted
- * across windows of 1, 2, 4, 8 and 16 characters.
- *
- * "The oversized fragment is left whole" is the usual consequence, not the rule.
- * Nested placeholders show the difference: in
- * `"answer ${" + "a".repeat(4196) + "${nested}"` the outer opener is out of
- * window, so the inner `${nested}` is stripped and the outer text stays. The
- * unbounded pattern deletes 4,208 characters here; this deletes 9. Less is
- * removed, the direction that matters, but the outer fragment is not untouched.
- */
+/** How far back the opening `${` may sit. Providers emit short placeholders, so an opener
+ * further back is not treated as one. The guarantee is about what is REMOVED: this removes
+ * no more than the unbounded pattern would and always returns a prefix of the input, so it
+ * can never delete text the unbounded pattern kept. Nested placeholders show the difference:
+ * with the outer opener out of window only the inner `${nested}` is stripped, 9 characters
+ * where the unbounded pattern deletes 4,208. */
export const TRAILING_PLACEHOLDER_WINDOW = 4096;
const TRAILING_TEMPLATE_PLACEHOLDER = /\s*\$\{[^}]*\}\s*$/;
const WHITESPACE = /\s/;
const CLOSE_BRACE = "}";
-/**
- * Remove one trailing `${...}` fragment, with the whitespace around it.
- *
- * Equivalent to `text.replace(/\s*\$\{[^}]*\}\s*$/, "")` for fragments up to
- * `window` characters long, at a cost independent of the reply preceding it.
- */
+/** Remove one trailing `${...}` fragment, with the whitespace around it. Equivalent to
+ * `text.replace(/\s*\$\{[^}]*\}\s*$/, "")` for fragments up to `window` characters long, at
+ * a cost independent of the reply preceding it. */
export function stripTrailingTemplatePlaceholder(
text: string,
window: number = TRAILING_PLACEHOLDER_WINDOW,
): string {
- // `\s*$` can only match the whitespace run at the very end, and the character
- // in front of it has to be the closing brace. Almost every arrival fails here
- // for the cost of that run, which is where the quadratic term goes. The run is
- // walked no further than the window, so a reply ending in a growing field of
- // whitespace cannot reintroduce it.
+ // `\s*$` can only match the whitespace run at the very end, and the character in front of it
+ // has to be the closing brace. Almost every arrival fails here for the cost of that run,
+ // which is where the quadratic term goes. The run is walked no further than the window.
const floor = Math.max(0, text.length - window);
let end = text.length;
while (end > floor && WHITESPACE.test(text[end - 1])) {
@@ -58,10 +38,8 @@ export function stripTrailingTemplatePlaceholder(
return text;
}
- // `[^}]*` cannot span a `}`, so the opening `${` has to sit after the previous
- // `}`. The window keeps the search off the whole buffer; the previous `}` then
- // usually keeps it far shorter still, since brace-heavy replies are the ones
- // that reach here.
+ // `[^}]*` cannot span a `}`, so the opening `${` has to sit after the previous `}`. The window
+ // keeps the search off the whole buffer; the previous `}` usually keeps it shorter still.
const from = Math.max(0, end - 1 - window);
const tail = text.slice(from);
const previousBrace = tail.lastIndexOf(CLOSE_BRACE, end - 2 - from);
@@ -73,12 +51,9 @@ export function stripTrailingTemplatePlaceholder(
return text.slice(0, from + scanFrom + match.index);
}
-/**
- * How far back a re-seed looks after a strip. The scan above never reads
- * further back than `end - 1 - window`, and `end` is itself no further back
- * than `length - window`, so everything it can reach lives in the last
- * `2 * window` characters. The extra two cover the `${` straddling that edge.
- */
+/** How far back a re-seed looks after a strip. The scan above never reads further back than
+ * `end - 1 - window`, and `end` is no further back than `length - window`, so everything it
+ * can reach lives in the last `2 * window` characters, plus two for a straddling `${`. */
const RESEED_WINDOW = 2 * TRAILING_PLACEHOLDER_WINDOW + 2;
const DOLLAR_BRACE = "${";
@@ -88,28 +63,17 @@ export type TrailingPlaceholderWatch = {
append(delta: string): void;
/** Take back the suffix a strip removed. `text` is the buffer after it. */
retract(text: string): void;
- /**
- * Whether `stripTrailingTemplatePlaceholder` could still cut something.
- *
- * Never false when it would, so the strip can be skipped outright whenever
- * this is false. It may be true when the strip then cuts nothing, which
- * costs one wasted scan on an arrival that already looked like a fragment.
- */
+ /** Whether `stripTrailingTemplatePlaceholder` could still cut something. Never false when it
+ * would, so the strip can be skipped whenever this is false. It may be true when the strip
+ * then cuts nothing, which costs one wasted scan. */
isCandidate(): boolean;
};
-/**
- * Decide whether the trailing `${...}` strip has anything to do, from the
- * deltas alone.
- *
- * `stripTrailingTemplatePlaceholder` is bounded, but it still has to touch the
- * end of the accumulated reply, and touching it at all flattens the cons
- * string that `text += delta` built, which costs O(reply) per arrival. The
- * pattern needs the last non-whitespace character to be `}` and a `${` in
- * front of it with no `}` in between, and both facts follow from the deltas:
- * a reply that never ends in a brace, which is almost every arrival of almost
- * every reply, is rejected without the buffer being read.
- */
+/** Decide whether the trailing `${...}` strip has anything to do, from the deltas alone. The
+ * strip is bounded, but it still touches the end of the accumulated reply, and that flattens
+ * the cons string `text += delta` built, costing O(reply) per arrival. The pattern needs the
+ * last non-whitespace character to be `}` with a `${` in front and no `}` between, and both
+ * facts follow from the deltas, so a reply not ending in a brace is rejected unread. */
export function createTrailingPlaceholderWatch(): TrailingPlaceholderWatch {
let length = 0;
// The last non-whitespace character, or "" when there is none.
@@ -145,8 +109,8 @@ export function createTrailingPlaceholderWatch(): TrailingPlaceholderWatch {
if (!delta) {
return;
}
- // The `${` straddling the boundary is the one thing `delta` alone cannot
- // show, because its `$` may have arrived last time.
+ // The `${` straddling the boundary is the one thing `delta` alone cannot show, because its `$`
+ // may have arrived last time.
if (overlap === DOLLAR_BRACE[0] && delta[0] === "{") {
lastDollarBrace = length - 1;
}
@@ -161,9 +125,8 @@ export function createTrailingPlaceholderWatch(): TrailingPlaceholderWatch {
lastCloseBrace = -1;
previousCloseBrace = -1;
overlap = "";
- // Only what the strip itself could reach. A `${` or `}` further back
- // than this cannot take part in a match, so forgetting it cannot hide
- // one: the strip would not look there either.
+ // Only what the strip itself could reach. A `${` or `}` further back cannot take part in a
+ // match, so forgetting it cannot hide one.
const from = Math.max(0, text.length - RESEED_WINDOW);
scan(text.slice(from), from);
if (text.length > 0) {
diff --git a/studio/frontend/src/features/chat/utils/youtube-url.ts b/studio/frontend/src/features/chat/utils/youtube-url.ts
index 29032e4bd19..63bf6956719 100644
--- a/studio/frontend/src/features/chat/utils/youtube-url.ts
+++ b/studio/frontend/src/features/chat/utils/youtube-url.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Mirrors extract_video_id in studio/backend/core/youtube_transcript.py. The
-// backend re-parses the URL, so this only decides whether to offer the prompt.
+// Mirrors extract_video_id in core/youtube_transcript.py. The backend re-parses the URL, so this
+// only decides whether to offer the prompt.
const VIDEO_ID = /^[A-Za-z0-9_-]{11}$/;
// `www.` is stripped before the lookup, so only the bare forms are listed.
diff --git a/studio/frontend/src/features/chat/video-attachment-adapter.ts b/studio/frontend/src/features/chat/video-attachment-adapter.ts
index 3ee92d9fc11..52af713b21b 100644
--- a/studio/frontend/src/features/chat/video-attachment-adapter.ts
+++ b/studio/frontend/src/features/chat/video-attachment-adapter.ts
@@ -25,9 +25,8 @@ function newAttachmentId(): string {
return `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
}
-/** Video shares the "Add photos & files" picker, like audio. llama-server
- * samples the clip into frames with ffmpeg, so the container is forwarded
- * untouched. */
+/** Video shares the "Add photos & files" picker, like audio. llama-server samples the clip into
+ * frames with ffmpeg, so the container is forwarded untouched. */
export class VideoAttachmentAdapter implements AttachmentAdapter {
accept = VIDEO_ACCEPT;
private readonly attachmentIds = new Set<string>();
@@ -39,8 +38,7 @@ export class VideoAttachmentAdapter implements AttachmentAdapter {
const modelLoaded = !!checkpoint && !state.modelLoading;
let unavailableReason: string | null = null;
if (!modelLoaded) {
- // Mirror the image and audio gates: a failed load reads differently from
- // no model picked.
+ // Mirror the image and audio gates: a failed load reads differently from no model picked.
unavailableReason = state.lastModelLoadError
? "The last model failed to load. Check the server logs, then load a model before adding video."
: "Load a model before adding video.";
@@ -50,8 +48,8 @@ export class VideoAttachmentAdapter implements AttachmentAdapter {
externalModelLabel(checkpoint) ||
checkpoint ||
"Current model";
- // Three causes land here and the server does not say which, so name all
- // three: /props reports video only when all of them line up.
+ // Three causes land here and the server does not say which, so name all three: /props reports
+ // video only when all of them line up.
unavailableReason = `${label} cannot accept video. Video needs a GGUF model whose mmproj supports video, a llama.cpp build with video enabled, and ffmpeg installed on this machine.`;
}
if (unavailableReason) {
@@ -95,9 +93,8 @@ export class VideoAttachmentAdapter implements AttachmentAdapter {
type: "file",
filename: attachment.name,
data,
- // Normalised at pick time: the extractor keys off this, and a
- // browser that answered "" or application/octet-stream for an mkv
- // would otherwise cost the clip silently.
+ // Normalised at pick time: the extractor keys off this, and a browser that answered "" or
+ // application/octet-stream for an mkv would otherwise cost the clip silently.
mimeType: attachment.contentType || "video/mp4",
},
],
diff --git a/studio/frontend/src/features/images/api.ts b/studio/frontend/src/features/images/api.ts
index f0ab2903fcf..04bed68b58a 100644
--- a/studio/frontend/src/features/images/api.ts
+++ b/studio/frontend/src/features/images/api.ts
@@ -5,9 +5,10 @@ import { withBackgroundLoadNotice } from "@/lib/model-lifecycle-events";
import { authFetch } from "@/features/auth";
import { readFastApiError } from "@/lib/format-fastapi-error";
-// One Advanced control's resolved value + provenance, for the Advanced-panel badges. `value` is the engaged value (null when
-// off), `requested` is what the caller asked for (null = left to the backend), `source` is "auto" (this backend decided) or
-// "explicit" (the caller set it), `status` says whether the ask survived, and `reason` is the tooltip why.
+// One Advanced control's resolved value and provenance, for the Advanced-panel badges. `value` is
+// the engaged value (null when off), `requested` is what the caller asked for (null = left to
+// the backend), `source` is "auto" or "explicit", `status` says whether the ask survived, and
+// `reason` is the tooltip why.
export interface DiffusionResolvedControl {
value: string | boolean | null;
// Absent on backends predating the requested/actual split.
@@ -31,8 +32,8 @@ export interface DiffusionStatus {
gguf_variant?: string | null;
cpu_offload: boolean;
// The ENGAGED runtime build. The backend has always sent these; declaring them is what lets the UI
- // report what actually ran instead of echoing the load request back at the user.
- // Transformer quant engaged on the dense fast path ("int8" | "fp8" | ...), null = the GGUF ran as-is.
+ // report what actually ran instead of echoing the load request back. Transformer quant
+ // engaged on the dense fast path ("int8" / "fp8" / ...), null = the GGUF ran as-is.
transformer_quant?: string | null;
// Text-encoder quant engaged ("fp8" | "fp8_dynamic" | "int8" | "nvfp4"), null = dense bf16.
text_encoder_quant?: string | null;
@@ -47,13 +48,15 @@ export interface DiffusionStatus {
attention_backend?: string | null;
transformer_cache?: string | null;
vae_tiling?: boolean;
- // Image workflows the loaded family supports (drives tab gating). Absent when nothing is loaded or on the native engine.
+ // Image workflows the loaded family supports (drives tab gating). Absent when nothing is loaded or
+ // on the native engine.
workflows?: string[];
// Whether the loaded model + quantisation can apply LoRA adapters (drives the LoRA picker enabled state).
supports_lora?: boolean;
// Whether the loaded model can apply a ControlNet. Diffusers only, for families with a ControlNet pipeline.
supports_controlnet?: boolean;
- // Per-Advanced-control provenance, keyed by control name. Present only when a model is loaded on a backend that records it; absent on older backends.
+ // Per-Advanced-control provenance, keyed by control name. Present only when a model is loaded on a
+ // backend that records it; absent on older backends.
resolved?: Record<string, DiffusionResolvedControl> | null;
}
@@ -101,11 +104,13 @@ export interface DiffusionLoadRequest {
| "xformers"
| "aiter";
memory_mode?: "auto" | "fast" | "balanced" | "low_vram";
- // CUDA / ROCm physical indices this load may use; omit for automatic. Neither engine shards a checkpoint, so several cards resolve to the one with the most free VRAM.
+ // CUDA / ROCm physical indices this load may use; omit for automatic. Neither engine shards a
+ // checkpoint, so several cards resolve to the one with the most free VRAM.
gpu_ids?: number[];
transformer_cache?: "off" | "fbcache";
- // LoRA adapters to BAKE into a torchao int8/fp8 build: they can only attach to the dense transformer BEFORE quantisation +
- // compile, so a quantized load that omits them rejects every generation. Ignored by bf16 / bnb-4bit, which apply at generate time.
+ // LoRA adapters to BAKE into a torchao int8/fp8 build: they can only attach to the dense
+ // transformer BEFORE quantisation and compile, so a quantized load that omits them rejects
+ // every generation. Ignored by bf16 / bnb-4bit, which apply at generate time.
loras?: LoraSpecInput[];
}
@@ -118,7 +123,8 @@ export interface DiffusionGenerateRequest {
guidance?: number;
seed?: number;
batch_size?: number;
- // Image-conditioned workflows. init_image alone = img2img; + mask_image = inpaint. strength is the denoise amount (0 keeps source).
+ // Image-conditioned workflows. init_image alone = img2img; plus mask_image = inpaint. strength is
+ // the denoise amount (0 keeps source).
init_image?: string;
mask_image?: string;
strength?: number;
@@ -126,7 +132,8 @@ export interface DiffusionGenerateRequest {
upscale?: number;
// Additional reference images for the FLUX.2 reference workflow, combined with init_image.
reference_images?: string[];
- // LoRA adapters for this generation (discovery id + weight, 0..2). Rejected (400) when the loaded model cannot apply LoRA.
+ // LoRA adapters for this generation (discovery id + weight, 0..2). Rejected with a 400 when the
+ // loaded model cannot apply LoRA.
loras?: LoraSpecInput[];
// ControlNet conditioning for this generation. Rejected (400) when the loaded model cannot apply ControlNet.
controlnet?: ControlNetSpecInput;
@@ -143,7 +150,8 @@ export interface ControlNetSpecInput {
id: string;
// Base64/data-URL control image (a source image or an already-made control map).
image: string;
- // "canny" preprocesses edges from a source image; any other type is an already-made map the backend maps to a control mode.
+ // "canny" preprocesses edges from a source image; any other type is an already-made map the
+ // backend maps to a control mode.
control_type: string;
strength: number;
guidance_start?: number;
@@ -186,7 +194,8 @@ export interface GalleryImage {
batch_index: number;
batch_size: number;
model: string | null;
- // The load-time build. The repo id alone does not identify a pipeline (quant choice, torchao scheme, baked adapters), so these are what a recipe needs once the model is unloaded.
+ // The load-time build. The repo id alone does not identify a pipeline (quant choice, torchao
+ // scheme, baked adapters), so these are what a recipe needs once the model is unloaded.
model_kind?: string | null;
gguf_filename?: string | null;
transformer_quant?: string | null;
@@ -197,7 +206,8 @@ export interface GalleryImage {
baked_loras?: string[];
loras?: string[];
controlnet?: string | null;
- // Conditioned-workflow settings. The source/mask/reference/control images are not persisted, so these say what ran and let restore name the inputs to supply again.
+ // Conditioned-workflow settings. The source/mask/reference/control images are not persisted, so
+ // these say what ran and let restore name the inputs to supply again.
workflow?: string | null;
strength?: number | null;
upscale?: number | null;
@@ -226,7 +236,8 @@ export async function getDiffusionStatus(
return parseJson(await authFetch("/api/inference/images/status", { signal }));
}
-// One family's bf16 component sizes + estimated resident footprint per quant scheme. Hardware-independent, so it can be fetched before anything is loaded.
+// One family's bf16 component sizes and estimated resident footprint per quant scheme.
+// Hardware-independent, so it can be fetched before anything is loaded.
export interface DiffusionInferenceInfo {
family: string;
transformer_bf16_gb: number;
@@ -240,7 +251,8 @@ export interface DiffusionInferenceInfoResponse {
families: DiffusionInferenceInfo[];
}
-/** Static per-family footprint summary for the Advanced Dtype tradeoff. Hardware-independent, so it is safe to fetch before a load. */
+/** Static per-family footprint summary for the Advanced Dtype tradeoff. Hardware-independent, so
+ * it is safe to fetch before a load. */
export async function getDiffusionInferenceInfo(): Promise<DiffusionInferenceInfoResponse> {
return parseJson(await authFetch("/api/inference/images/info"));
}
@@ -258,9 +270,9 @@ export async function getGenerateProgress(): Promise<DiffusionGenerateProgress>
}
export async function loadDiffusionModel(body: DiffusionLoadRequest): Promise<DiffusionStatus> {
- // Announced so the loaded models indicator shows the load for as long as the
- // toast does, rather than up to one 5s poll later. This POST only starts the
- // load, so the notice settles from load-progress, not from the response.
+ // Announced so the loaded models indicator shows the load for as long as the toast does, rather
+ // than up to one 5s poll later. This POST only starts the load, so the notice settles from
+ // load-progress, not from the response.
return withBackgroundLoadNotice(
"image",
body.model_path,
@@ -282,7 +294,8 @@ export interface DiffusionDownloadPlan {
files: string[];
bytes: number;
gguf_filename: string | null;
- /** Whether this entry holds the selected model. Only the planner knows, because a gated pick is staged from an ungated mirror under a different repo id. Optional: an older backend omits it. */
+ /** Whether this entry holds the selected model. Only the planner knows, because a gated pick is
+ * staged from an ungated mirror under a different repo id. Absent on an older backend. */
checkpoint?: boolean;
}[];
total_bytes: number;
@@ -290,12 +303,10 @@ export interface DiffusionDownloadPlan {
required_bytes?: number;
/** Selected checkpoint's contribution to required_bytes. */
checkpoint_bytes?: number;
- /**
- * Why this pick cannot load as selected (a FLUX.2 GGUF paired with a different-size base), or
- * null/undefined when nothing is known to be wrong. The backend reads metadata only, so it stays
- * silent rather than guessing; when it does speak, refuse the pick here -- the alternative is the
- * loader saying the same thing after a ~19 GB download. Optional: an older backend omits it.
- */
+ /** Why this pick cannot load as selected (a FLUX.2 GGUF paired with a different-size base), or
+ * null when nothing is known to be wrong. The backend reads metadata only, so it stays silent
+ * rather than guessing; when it does speak, refuse the pick here, since the alternative is
+ * the loader saying the same thing after a ~19 GB download. */
incompatible_reason?: string | null;
}
@@ -312,7 +323,8 @@ export async function getDiffusionDownloadPlan(
);
}
-/** The generate POST response was lost in transit (proxy/tunnel timeout, dropped connection) rather than refused, so the generation is still running. */
+/** The generate POST response was lost in transit (proxy/tunnel timeout, dropped connection) rather
+ * than refused, so the generation is still running. */
export class GenerateResponseLostError extends Error {
constructor(message: string) {
super(message);
@@ -320,10 +332,13 @@ export class GenerateResponseLostError extends Error {
}
}
-// Gateway statuses meaning the origin never got to answer: 524 is Cloudflare's ~100s cap, which a slow run routinely exceeds.
+// Gateway statuses meaning the origin never got to answer: 524 is Cloudflare's ~100s cap, which a
+// slow run routinely exceeds.
const RESPONSE_LOST_STATUSES = new Set([408, 502, 503, 504, 522, 524]);
-/** Generate synchronously: the response carries the finished images. A run past the proxy window throws GenerateResponseLostError with the generation still in flight, so the caller settles it via getGenerateProgress + the gallery instead of retrying. */
+/** Generate synchronously: the response carries the finished images. A run past the proxy window
+ * throws GenerateResponseLostError with the generation still in flight, so the caller settles
+ * it via getGenerateProgress and the gallery instead of retrying. */
export async function generateDiffusionImage(
body: DiffusionGenerateRequest,
): Promise<DiffusionGenerateResponse> {
@@ -342,8 +357,9 @@ export async function generateDiffusionImage(
}
if (!response.ok && RESPONSE_LOST_STATUSES.has(response.status)) {
const detail = await readFastApiError(response);
- // A proxy answers with HTML (or nothing); the app answers with JSON. Only the former means the request may still be running --
- // settling an application error would poll for a generation that never started and hide the reason the backend gave.
+ // A proxy answers with HTML (or nothing); the app answers with JSON. Only the former means the
+ // request may still be running: settling an application error would poll for a generation
+ // that never started and hide the reason the backend gave.
if (
response.status === 503 &&
(response.headers.get("content-type") || "").toLowerCase().includes("application/json")
@@ -355,15 +371,16 @@ export async function generateDiffusionImage(
return parseJson(response);
}
-/** Request a cancel. Best-effort: the diffusers sampler stops at the next step boundary (the native engine kills its sd-cli run outright) and the in-flight generate POST unwinds as a 409 with nothing persisted. `cancelled` is false when there was nothing to stop. */
+/** Request a cancel. Best-effort: the diffusers sampler stops at the next step boundary (the native
+ * engine kills its sd-cli run outright) and the in-flight generate POST unwinds as a 409 with
+ * nothing persisted. `cancelled` is false when there was nothing to stop. */
export async function cancelDiffusionGeneration(
signal?: AbortSignal,
): Promise<{ cancelled: boolean }> {
return parseJson(
- // No network retry, and abortable. The endpoint always targets whichever generation is active
- // NOW, so a retry or a 401 refresh-and-replay firing after the stopped run settled can land on
- // a run the user started meanwhile and stop that one instead. Not retryable, unlike the
- // idempotent GETs; the signal lets the caller drop a pending one when its run is over.
+ // No network retry, and abortable. The endpoint always targets whichever generation is active NOW,
+ // so a retry or a 401 refresh-and-replay firing after the stopped run settled can land on a
+ // run the user started meanwhile. The signal lets the caller drop a pending one.
await authFetch(
"/api/inference/images/generate/cancel",
{ method: "POST", signal },
@@ -441,20 +458,23 @@ export async function fetchGalleryBlob(url: string): Promise<Blob> {
return res.blob();
}
-/** Fetch a gallery PNG (auth-protected, so it cannot be a plain <img src>) and wrap it in an object URL. Callers must revoke it. */
+/** Fetch a gallery PNG (auth-protected, so it cannot be a plain <img src>) and wrap it in an object
+ * URL. Callers must revoke it. */
export async function fetchGalleryObjectUrl(
url: string,
): Promise<{ url: string; bytes: number }> {
- // The blob size travels with the URL: the gallery cache is budgeted in bytes, which the caller cannot work out from the URL.
+ // The blob size travels with the URL: the gallery cache is budgeted in bytes, which the caller
+ // cannot work out from the URL.
const blob = await fetchGalleryBlob(url);
return { url: URL.createObjectURL(blob), bytes: blob.size };
}
-// ── Diffusion LoRA training ───────────────────────────────────────────────────
-// Mirrors DiffusionTrainingStartRequest on the backend; only the paths are required.
+// Diffusion LoRA training. Mirrors DiffusionTrainingStartRequest on the backend; only the paths
+// are required.
export interface DiffusionTrainingStartRequest {
base_model: string;
- // Explicit family. Optional (the backend resolves it from base_model), but the Train tab always sends it so a custom base trains under the intended family.
+ // Explicit family. Optional (the backend resolves it from base_model), but the Train tab always
+ // sends it so a custom base trains under the intended family.
model_family?: string | null;
data_dir: string;
output_dir: string;
@@ -477,7 +497,8 @@ export interface DiffusionTrainingStartRequest {
lr_warmup_steps?: number;
// DiT-family quantised base precision (nf4 QLoRA by default). Ignored for sdxl, which uses mixed_precision.
base_precision?: "nf4" | "bf16" | "int8" | "fp8" | "mxfp8" | "auto";
- // Whether to torch.compile the transformer (any family whose /info reports supports_compile). "auto" lets the backend decide.
+ // Whether to torch.compile the transformer (any family whose /info reports supports_compile).
+ // "auto" lets the backend decide.
compile_transformer?: "off" | "on" | "auto";
// Precompute + cache the VAE latents before the loop (skips re-encoding each epoch).
cache_latents?: boolean;
@@ -490,7 +511,8 @@ export interface DiffusionTrainingStartRequest {
save_steps?: number;
save_total_limit?: number;
// Continue a previous run: its output_dir, or one explicit checkpoint-<N> directory inside it.
- // train_steps then means the TARGET TOTAL, so a checkpoint at 11 with train_steps 500 runs 12..500.
+ // train_steps then means the TARGET TOTAL, so a checkpoint at 11 with train_steps 500 runs
+ // 12..500.
resume_from_checkpoint?: string | null;
// The run being continued. Recorded in the history for lineage only.
resumed_from_job_id?: string | null;
@@ -498,7 +520,8 @@ export interface DiffusionTrainingStartRequest {
hf_token?: string | null;
}
-// Paired step-indexed history arrays for the live loss + LR charts. `lr` entries may be null so a sparse series still aligns by index.
+// Paired step-indexed history arrays for the live loss and LR charts. `lr` entries may be null so
+// a sparse series still aligns by index.
export interface DiffusionMetricHistory {
steps: number[];
loss: number[];
@@ -556,7 +579,8 @@ export async function startDiffusionTraining(
);
}
-// Request a stop of the running job. `save` (default true) writes the current adapter before halting; false discards it.
+// Request a stop of the running job. `save` (default true) writes the current adapter before
+// halting; false discards it.
export async function stopDiffusionTraining(save = true): Promise<{ status: string }> {
return parseJson(
await authFetch("/api/train/diffusion/stop", {
@@ -586,7 +610,7 @@ export interface DiffusionTrainingRunSummary {
// The run's adapter directory, which is what a Resume replays as resume_from_checkpoint.
output_dir?: string | null;
// Whether the run can be continued, re-derived from the checkpoints on disk on every read, with
- // the step the newest bundle holds. When false, resume_blocked_reason says why (tooltip text).
+ // the step the newest bundle holds. When false, resume_blocked_reason says why.
can_resume?: boolean;
checkpoint_step?: number | null;
// The exact bundle a resume would continue; sent back as resume_from_checkpoint.
@@ -642,7 +666,8 @@ export function datasetItemCount(d: {
return d.image_count + (d.clip_count ?? 0);
}
-// Per-family training defaults (from GET /api/train/diffusion/info). Absent on older backends; the Train tab then falls back to a hardcoded list.
+// Per-family training defaults (from GET /api/train/diffusion/info). Absent on older backends; the
+// Train tab then falls back to a hardcoded list.
export interface DiffusionTrainableFamily {
name: string;
label: string;
@@ -655,8 +680,8 @@ export interface DiffusionTrainableFamily {
train_steps?: number;
train_batch_size?: number;
mixed_precision?: "bf16" | "fp16" | "no";
- // The LR ramp, as one pair: a warmup count only ramps under a scheduler that reads it, so
- // the backend advertises both or neither. Absent on a backend older than that pairing.
+ // The LR ramp, as one pair: a warmup count only ramps under a scheduler that reads it, so the
+ // backend advertises both or neither. Absent on a backend older than that pairing.
lr_scheduler?: string;
lr_warmup_steps?: number;
} | null;
@@ -677,7 +702,8 @@ export interface DiffusionTrainableFamily {
supports_checkpoints?: boolean;
/** 1 for a family whose forward covers one packed sequence; null/absent means unrestricted. */
max_train_batch_size?: number | null;
- // When set, deploying a LoRA trained on this family previews it on this repo instead of the checkpoint it trained on (Krea trains on Raw, runs on Turbo).
+ // When set, deploying a LoRA trained on this family previews it on this repo instead of the
+ // checkpoint it trained on (Krea trains on Raw, runs on Turbo).
deploy_base?: string | null;
// Variant-specific training-base to inference-base pairs, including public mirror ids.
deploy_bases?: Record<string, string>;
@@ -710,7 +736,8 @@ export interface DiffusionDatasetUploadResult extends DiffusionDatasetSummary {
uploaded: number;
}
-/** Upload images (+ optional caption .txt / metadata.jsonl) into a named dataset folder. Repeat uploads accumulate; the returned name is a valid data_dir. */
+/** Upload images (plus optional caption .txt / metadata.jsonl) into a named dataset folder. Repeat
+ * uploads accumulate; the returned name is a valid data_dir. */
export async function uploadDiffusionDataset(
name: string,
files: File[],
@@ -723,8 +750,9 @@ export async function uploadDiffusionDataset(
);
}
-// One item in a training dataset folder, with its resolved caption. `caption_source` records where it came from, so the labeling grid can highlight uncaptioned items.
-// `kind` is absent on older backends, which listed images only; treat a missing value as "image".
+// One item in a training dataset folder, with its resolved caption. `caption_source` records where
+// it came from, so the labeling grid can highlight uncaptioned items. `kind` is absent on
+// older backends, which listed images only; treat a missing value as "image".
export interface DiffusionDatasetImageRecord {
filename: string;
caption: string | null;
@@ -757,7 +785,8 @@ export async function listDiffusionDatasetImages(
);
}
-/** Build the auth-protected thumbnail URL for a dataset image. Fetch it via fetchGalleryObjectUrl into an object URL; it cannot be a plain <img src>. */
+/** Build the auth-protected thumbnail URL for a dataset image. Fetch it via fetchGalleryObjectUrl
+ * into an object URL; it cannot be a plain <img src>. */
export function diffusionDatasetImageUrl(
name: string,
filename: string,
@@ -797,7 +826,8 @@ export async function deleteDiffusionDatasetImage(
if (!res.ok) throw new Error(await readFastApiError(res));
}
-// A curated, one-click-importable example image dataset. `license` is shown verbatim so users see the terms before importing.
+// A curated, one-click-importable example image dataset. `license` is shown verbatim so users see
+// the terms before importing.
export interface DiffusionDatasetExample {
id: string;
label: string;
diff --git a/studio/frontend/src/features/images/images-page.tsx b/studio/frontend/src/features/images/images-page.tsx
index 3db566ce1d1..fee8ed8a75c 100644
--- a/studio/frontend/src/features/images/images-page.tsx
+++ b/studio/frontend/src/features/images/images-page.tsx
@@ -172,16 +172,16 @@ import {
type TrainFamilyOption,
} from "./train/train-base-selector";
-// Curated models come from the shared catalog: one canonical group per model with its artifacts (GGUF / FP8 / bnb-4bit / BF16) as data, and the load kind per artifact via loadSpecFor.
-// Host-dependent, so it is built per render rather than once at module load: a host that can
-// only run the native engine is not offered the pipeline rows it would be refused at load.
+// Curated models come from the shared catalog, one group per model with its artifacts as data and
+// the load kind per artifact from loadSpecFor. Built per render, since a host that can only run
+// the native engine is not offered pipeline rows.
function useImageModels(host: HostClass): ModelOption[] {
return useMemo(() => catalogToModelOptions(IMAGE_CATALOG, host), [host]);
}
-// Workflow tabs. `requires` is the backend workflow id (status.workflows) the loaded model must support; null = always available.
-// The images each conditioned workflow consumed, named for the restore toast: a recipe keeps the scalar settings but not the uploads.
-// Keys are the backend's own workflow strings; txt2img is absent because it restores completely.
+// Workflow tabs. `requires` is the backend workflow id the model must support; null = always.
+// Each conditioned workflow names the images it consumed for the restore toast, since a
+// recipe keeps the scalar settings but not the uploads. txt2img restores completely.
const CONDITIONED_WORKFLOW_INPUTS: Record<string, string> = {
img2img: "the source image",
inpaint: "the source image and mask",
@@ -191,7 +191,8 @@ const CONDITIONED_WORKFLOW_INPUTS: Record<string, string> = {
controlnet: "the control image",
};
-// Common aspect ratios (landscape; Flip mirrors to portrait). Picking one locks the W:H proportion; the sliders set the size.
+// Common aspect ratios (landscape; Flip mirrors to portrait). Picking one locks W:H; the
+// sliders set the size.
const ASPECT_RATIOS: Record<string, [number, number]> = {
"1:1": [1, 1],
"3:2": [3, 2],
@@ -217,7 +218,7 @@ const CONTROL_TYPE_LABELS: Record<string, string> = {
pose: "Pose (map)",
};
-// Z-Image accepts 256–2048, in multiples of 16. Snap any value into range.
+// Z-Image accepts 256-2048, in multiples of 16. Snap any value into range.
const MIN_DIM = 256;
const MAX_DIM = 2048;
// Convenient drag range for the Runs slider; the number box accepts higher typed values on purpose.
@@ -308,8 +309,7 @@ function DimensionSelect({
);
}
-// Hidden until the row is hovered or focused, so a quiet control stays quiet.
-// The ratio key (compared long:short, so it survives orientation) matching width/height, plus whether portrait.
+// Hidden until the row is hovered or focused. The ratio key is compared long:short, so it survives orientation.
function matchAspect(width: number, height: number): { key: string; portrait: boolean } {
const target = Math.max(width, height) / Math.min(width, height);
const found = Object.entries(ASPECT_RATIOS).find(
@@ -318,8 +318,9 @@ function matchAspect(width: number, height: number): { key: string; portrait: bo
return { key: found ? found[0] : "custom", portrait: height > width };
}
-// Module cache of the backend-persisted gallery, so a tab switch re-renders instantly. Object URLs are revoked only on delete.
-// Blob budget for cached PNGs: 192 MB is ~100-200 images, far more than a viewport holds. On-screen and open images are never evicted.
+// Module cache of the backend-persisted gallery, so a tab switch re-renders instantly; object
+// URLs are revoked only on delete. The 192 MB blob budget never evicts a visible image.
+// 192 MB is ~100-200 images, far more than a viewport holds.
const IMAGE_BLOB_BUDGET_BYTES = 192 * 1024 * 1024;
const galleryCache: {
@@ -328,7 +329,7 @@ const galleryCache: {
selectedId: string | null;
quant: string | null;
srcById: BlobUrlCache;
- // Ids with a fetch in flight, so concurrent ensureSrc calls do not double-fetch (and leak the duplicate object URL).
+ // Ids with a fetch in flight, so concurrent ensureSrc calls do not double-fetch and leak a duplicate object URL.
inflight: Set<string>;
// Ids deleted while their PNG was still downloading: a fetch landing afterwards must throw its blob away.
deleted: Set<string>;
@@ -345,11 +346,12 @@ const galleryCache: {
// Images loaded per infinite-scroll page.
const PAGE_SIZE = 50;
-// Passes a window resync may make before giving up. Each extra pass only happens when pagination
-// moved while it was fetching, which cannot repeat indefinitely without the user scrolling along.
+// Passes a window resync may make before giving up: each extra pass only happens when
+// pagination moved while it was fetching.
const RESYNC_MAX_ATTEMPTS = 3;
-// Export filename, e.g. Unsloth_20260624-143005_123.png. Batch siblings share seed + timestamp, so they get a "_<n>" suffix.
+// Export filename. Batch siblings share seed + timestamp, so they get a "_<n>" suffix.
+// For example Unsloth_20260624-143005_123.png.
type ImageExportFormat = "png" | "jpeg" | "webp";
function exportFilename(image: GalleryImage, format: ImageExportFormat = "png"): string {
@@ -363,7 +365,8 @@ function exportFilename(image: GalleryImage, format: ImageExportFormat = "png"):
return `Unsloth_${stamp}_${image.seed}${suffix}.${ext}`;
}
-// PNG saves the stored bytes verbatim (keeping the embedded recipe); JPEG / WebP re-encode client-side, JPEG flattened onto white.
+// PNG saves the stored bytes verbatim, keeping the embedded recipe; JPEG / WebP re-encode
+// client-side, JPEG flattened onto white.
async function reencodeImage(
src: string,
format: Exclude<ImageExportFormat, "png">,
@@ -391,8 +394,8 @@ async function reencodeImage(
throw new Error(`could not encode ${format}`);
}
if (blob.type !== `image/${format}`) {
- // WebKit can silently return PNG bytes when an encoder is unavailable.
- // Treat that as a failed conversion so the caller uses a matching .png name.
+ // WebKit can silently return PNG bytes when an encoder is unavailable, so treat that as a
+ // failed conversion and use a matching .png name.
throw new Error(`${format} encoding is unavailable`);
}
return blob;
@@ -421,8 +424,8 @@ async function downloadImage(
if (outputBlob) {
await downloadFile(outputBlob, filename, outputBlob.type);
} else if (isTauri) {
- // WebKit can display the cached object URL but fail to fetch it again with
- // "Load failed". Re-fetch the authenticated original for the native save.
+ // WebKit can display the cached object URL but fail to fetch it again, so re-fetch the
+ // authenticated original for the native save.
const originalBlob = await fetchGalleryBlob(image.url);
await downloadFile(originalBlob, filename, originalBlob.type);
} else {
@@ -447,7 +450,7 @@ function formatTimestamp(epochSeconds: number): string {
// Bar label for an in-flight generation: step count plus an ETA once known.
function genStepLabel(p: DiffusionGenerateProgress): string {
- // Text encoding (and warmup) happens before the first scheduler tick, so step 0 means "working, not denoising yet".
+ // Text encoding happens before the first scheduler tick, so step 0 means "working, not denoising yet".
if (p.step === 0) return "Preparing (text encoding + warmup)…";
const base = `Step ${p.step}/${p.total_steps}`;
const eta = p.eta_seconds != null ? formatEta(p.eta_seconds) : "";
@@ -459,7 +462,9 @@ const SETTLE_POLL_MS = 1000;
const SETTLE_MAX_MS = 6 * 60 * 60 * 1000; // hard cap; a native-CPU batch can run for hours
const SETTLE_MAX_FAILS = 5; // consecutive progress failures before calling the backend gone
-/** Wait out a generation that outlived its POST. Idle progress alone is ambiguous, so success needs evidence (progress seen active, or a gallery record that was not there when the POST went out); otherwise report a failed submission. Throws past SETTLE_MAX_MS, or if the backend stays unreachable, so a wedged generation surfaces. */
+/** Wait out a generation that outlived its POST. Idle progress alone is ambiguous, so success
+ * needs evidence: progress seen active, or a gallery record that is new since the POST.
+ * Throws past SETTLE_MAX_MS or if the backend stays unreachable, so a wedge surfaces. */
async function settleLostGeneration(
isCurrent: () => boolean,
baseline: NewRecordProbeBaseline,
@@ -482,7 +487,8 @@ async function settleLostGeneration(
}
if (!idle) continue;
if (sawActive) return;
- // Idle on the very first look: the run may already have finished or never started. A gallery record we had not seen is the proof.
+ // Idle on the very first look: the run may have finished or never started, so a gallery
+ // record we had not seen is the proof.
try {
const sawNew = await hasUnknownRecord(
baseline,
@@ -500,11 +506,12 @@ async function settleLostGeneration(
}
throw new Error("The image generation request did not reach the server.");
}
- // Out of budget with the run still active: returning here would report success and start the next run against a busy backend.
+ // Out of budget with the run still active: returning would report success and start the next
+ // run against a busy backend.
throw new Error("Timed out waiting for the image generation to finish.");
}
-// The chat tab model-load toast styling, reused verbatim so the diffusion load toast is visually identical.
+// The chat tab model-load toast styling, reused verbatim so the diffusion load toast is identical.
const LOAD_TOAST_CLASSNAMES = {
toast: "chat-model-load-toast items-center gap-2.5",
content: "gap-0.5 flex-1 min-w-0",
@@ -512,9 +519,10 @@ const LOAD_TOAST_CLASSNAMES = {
description: "mt-0 w-full",
} as const;
-// Render the chat ModelLoadDescription for a progress poll. The base repo downloads alongside the GGUF, so the total exceeds the quant size.
+// Render the chat ModelLoadDescription for a progress poll. The base repo downloads alongside
+// the GGUF, so the total exceeds the quant size.
function loadToastDescription(p: DiffusionLoadProgress) {
- // "Downloading" only when bytes actually remain: a cached model (or the pre-estimate window) must not claim a download.
+ // "Downloading" only when bytes actually remain: a cached model must not claim a download.
const downloading = p.bytes_total > 0 && p.bytes_downloaded < p.bytes_total * 0.999;
const title = downloading
? "Downloading model requirements…"
@@ -542,10 +550,9 @@ function loadToastDescription(p: DiffusionLoadProgress) {
);
}
-// Toast args mirroring chat: persistent, closeable, content in `description`. Pass `id` to update in place.
-// `onCancel` adds chat's Cancel action, the one control that reaches a load already in flight: the model
-// selector's eject is hidden for exactly the span a first load runs (nothing is resident, so it has no
-// selection to eject), which left a multi-gigabyte pull with no way out.
+// Toast args mirroring chat; `id` updates in place. `onCancel` adds chat's Cancel, the one
+// control that reaches a load in flight: the selector's eject is hidden for exactly the
+// span a first load runs, which left a multi-gigabyte pull with no way out.
function loadToastArgs(
p: DiffusionLoadProgress,
id?: string | number,
@@ -601,7 +608,7 @@ function SliderField({
);
}
-// Matches the field-label style used across Unsloth (export/chat settings).
+// Matches the field-label style used across Unsloth.
function Field({
label,
hint,
@@ -624,9 +631,9 @@ function Field({
);
}
-// The badge for one Advanced control. "Auto: X" when the backend decided it; "FP8 -> OFF" in a
-// warning tone when an EXPLICIT request was declined -- that case used to render nothing at all
-// while the dropdown kept showing the request, so a Q4_K_M GGUF could advertise FP8 it never ran.
+// The badge for one Advanced control: "Auto: X" when the backend decided, "FP8 -> OFF" in a
+// warning tone when an EXPLICIT request was declined. That case used to render nothing while the
+// dropdown still showed the request, so a Q4_K_M GGUF could advertise FP8 it never ran.
function ResolvedBadge({
status,
controlKey,
@@ -671,7 +678,7 @@ function AdvancedSelect({
hint?: ReactNode;
// An optional inline badge next to the label (e.g. the "Auto: X" resolved-value pill).
badge?: ReactNode;
- // A short always-visible description under the row, for controls whose label alone is not enough.
+ // A short always-visible description under the row.
desc?: string;
value: string;
onValueChange: (v: string) => void;
@@ -703,8 +710,8 @@ function AdvancedSelect({
);
}
-// A brush-based mask editor for inpainting: the source image with a paintable overlay, exporting a grayscale PNG mask at
-// the image's NATIVE resolution (white = repaint). `brushPct` sizes the brush as a fraction of the shorter side.
+// Brush-based mask editor for inpainting, exporting a grayscale PNG mask at NATIVE resolution
+// (white = repaint). `brushPct` sizes the brush against the shorter side.
function MaskCanvas({
image,
brushPct,
@@ -853,7 +860,8 @@ function loadImage(src: string): Promise<HTMLImageElement> {
// Which sides to grow when outpainting.
type ExtendSides = { left: boolean; right: boolean; top: boolean; bottom: boolean };
-// Redraw an image/canvas at (w, h). Clamps an outpaint source to a size the browser can back and the backend can decode.
+// Redraw an image/canvas at (w, h). Clamps an outpaint source to a size the browser can back
+// and the backend can decode.
function scaleToCanvas(source: CanvasImageSource, w: number, h: number): HTMLCanvasElement {
const dst = document.createElement("canvas");
dst.width = w;
@@ -864,16 +872,16 @@ function scaleToCanvas(source: CanvasImageSource, w: number, h: number): HTMLCan
return dst;
}
-// Build the (image, mask) pair for outpaint by reusing the inpaint backend: grow the canvas by `pct` per dimension on the
-// selected sides, edge-bleed the original pixels in, and mask the new bands white with a small overlap so the seam blends.
+// Build the (image, mask) pair for outpaint on the inpaint backend: grow the canvas per
+// dimension on the selected sides, edge-bleed the original in, and mask the new bands white.
async function buildOutpaint(
src: string,
sides: ExtendSides,
pct: number,
): Promise<{ image: string; mask: string }> {
const source = await loadImage(src);
- // Scale the SOURCE so the grown canvas fits MAX_SIDE before allocating: growing all four sides by 100% multiplies the area
- // by 9, and a canvas past the browser limit silently no-ops every drawImage. Also keeps both allocations under a gigabyte.
+ // Scale the SOURCE so the grown canvas fits MAX_SIDE before allocating: growing all four
+ // sides by 100% multiplies the area by 9, and an oversized canvas no-ops every drawImage.
const MAX_SIDE = 4096;
const grow = (a: boolean, b: boolean) => 1 + (a ? pct / 100 : 0) + (b ? pct / 100 : 0);
const fit = Math.min(
@@ -927,7 +935,7 @@ async function buildOutpaint(
mctx.fillStyle = "#000000"; // ...except the kept original (inset by the seam overlap).
mctx.fillRect(l + ol, t + ot, w - ol - or, h - ot - ob);
- // The pre-scale sizes the canvases to fit MAX_SIDE, but per-side rounding can overshoot past the backend's 4096px limit; trim the slack.
+ // The pre-scale fits MAX_SIDE, but per-side rounding can overshoot the backend's 4096px limit; trim the slack.
const longest = Math.max(nw, nh);
if (longest > MAX_SIDE) {
const scale = MAX_SIDE / longest;
@@ -1005,22 +1013,20 @@ function RecipePopover({
<RecipeRow label="Negative" value={image.negative_prompt} wrap />
) : null}
{image.model ? <RecipeRow label="Model" value={image.model} /> : null}
- {/* The load-time build, so the recipe still names the pipeline once the model is unloaded: the repo id alone does not say which quant ran or whether an adapter was baked in. */}
+ {/* The load-time build, so the recipe still names the pipeline once the model is unloaded: the
+ repo id alone does not say which quant ran. */}
{image.gguf_filename ? <RecipeRow label="File" value={image.gguf_filename} mono /> : null}
{image.transformer_quant ? (
<RecipeRow label="Quant" value={image.transformer_quant} />
) : null}
- {/* The ENGAGED text-encoder precision and memory placement: the encoder is often the
- largest resident component and its cast changes the conditioning, and the memory mode
- decides whether the torchao encoder modes could run at all. */}
+ {/* The ENGAGED text-encoder precision and memory placement: the encoder is often the largest
+ resident component, and the memory mode decides whether torchao modes could run at all. */}
{image.text_encoder_quant ? (
<RecipeRow label="TE quant" value={image.text_encoder_quant} />
) : null}
- {/* Either field is placement information. The native engine reports no memory_mode at
- all (it has no torchao path to choose one for) while still recording an active
- offload, so gating on memory_mode hid the offload on exactly the configuration
- this row was extended for. Absent memory_mode stays absent: substituting "auto"
- there would claim the memory planner picked a mode on a path that never ran it. */}
+ {/* Either field is placement information. The native engine reports no memory_mode while still
+ recording an active offload, so gating on it hid the offload on exactly the configuration
+ this row was extended for. Absent stays absent: "auto" would claim a planner that never ran. */}
{image.memory_mode ||
(image.offload_policy && image.offload_policy !== "none") ? (
<RecipeRow
@@ -1060,13 +1066,9 @@ function BuildRow({ label, value, badge }: { label: string; value: string; badge
);
}
-/**
- * What the LOADED model is actually running, read from status (never from the request): the
- * transformer and text-encoder precision, the memory mode with its resolved offload behaviour, and
- * the attention backend. The Advanced selects above say what was ASKED for; this says what
- * happened, and any control whose request was declined carries its reason in the badge tooltip.
- * Without it a successful load said nothing at all about the precision it ran at.
- */
+/** What the LOADED model is actually running, read from status and never from the request:
+ * transformer and text-encoder precision, memory mode with its offload behaviour, and the
+ * attention backend. The Advanced selects say what was ASKED for; this says what happened. */
function LoadedBuildSummary({ status }: { status: DiffusionStatus | null }) {
if (!status?.loaded) return null;
const offload = status.offload_policy ?? "none";
@@ -1112,9 +1114,9 @@ function LoadedBuildSummary({ status }: { status: DiffusionStatus | null }) {
value={
status.attention_backend
? formatResolvedValue("attention_backend", status.attention_backend)
- : // The sd.cpp engine reports no backend because it has none of ours: its attention
- // is chosen by native flags, not by the diffusers/PyTorch dispatcher, so calling it
- // "Native SDPA" is wrong on the default CPU image path.
+ // The sd.cpp engine reports no backend because its attention comes from native flags
+ // rather than the diffusers dispatcher, so "Native SDPA" is wrong on the CPU image path.
+ :
isNativeEngineStatus(status)
? "sd.cpp built-in"
: "Native SDPA"
@@ -1124,12 +1126,8 @@ function LoadedBuildSummary({ status }: { status: DiffusionStatus | null }) {
);
}
-/**
- * Report a failed load. A refused precision is a long actionable sentence ("Choose Auto ... or
- * Off ..."), so it becomes a toast description under a short title rather than one unreadable
- * line. Nothing is left half-loaded either way: the backend refuses before it starts the load, or
- * has already torn the partial build down by the time it reports the error.
- */
+/** Report a failed load. A refused precision is a long actionable sentence, so it becomes a
+ * toast description under a short title. Nothing is left half-loaded either way. */
function reportLoadFailure(message: string | null | undefined, fallback: string): void {
const text = (message || "").trim();
if (text && isPrecisionRefusal(text)) {
@@ -1141,8 +1139,8 @@ function reportLoadFailure(message: string | null | undefined, fallback: string)
type Busy = "loading" | "unloading" | "generating" | null;
-// What a pick optimistically replaced, so a load that never takes can put all of it back. The quant
-// label and the generation recipe move together at pick time, so they have to roll back together too.
+// What a pick optimistically replaced, so a load that never takes can put it all back. The
+// quant label and the recipe move together at pick time, so they roll back together.
type PickRevert = {
prev: string | null;
steps: number;
@@ -1154,7 +1152,8 @@ type PickRevert = {
appliedGuidance?: number;
};
-// The Advanced controls a load sends, with "auto" sentinels resolved to omitted. A staged download pins one at pick time.
+// The Advanced controls a load sends, with "auto" sentinels resolved to omitted. A staged
+// download pins one at pick time.
type LoadAdvanced = Pick<
DiffusionLoadRequest,
| "cpu_offload"
@@ -1191,7 +1190,8 @@ export function ImagesPage({
onScroll: onSettingsScroll,
className: settingsFadeClass,
} = useScrollFades();
- // width/height are the source of truth; `aspect` locks their proportion and `portrait` tracks orientation, so Flip keeps the lock.
+ // width/height are the source of truth; `aspect` locks their proportion and `portrait` tracks
+ // orientation, so Flip keeps the lock.
const [width, setWidth] = useState(1024);
const [height, setHeight] = useState(1024);
const [aspect, setAspect] = useState("1:1");
@@ -1199,16 +1199,15 @@ export function ImagesPage({
// Z-Image-Turbo official defaults: 9 steps (= 8 DiT forwards), guidance 0 (distilled, CFG-free).
const [steps, setSteps] = useState(DEFAULT_GEN.steps);
const [guidance, setGuidance] = useState(DEFAULT_GEN.guidance);
- // Whether the user has taken the recipe since the pick that is still waiting for its status: a
- // preset selected while the model downloaded is newer than that pick, so its rollback is not the
- // one to restore.
+ // Whether the user has taken the recipe since the pick still waiting for its status: a preset
+ // selected while the model downloaded is newer than that pick.
const pickRecipeSuperseded = useRef<(() => boolean) | null>(null);
// Put back everything a pick optimistically applied. Setters are stable, so this never re-renders on its own.
const revertPick = useCallback((r: PickRevert) => {
setQuant(r.prev);
setPendingModelDefaults(null);
// Equality alone cannot tell "nobody touched this" from "the user chose the same number": a
- // preset selected after the pick owns these fields even where it matches what the pick applied.
+ // preset selected after the pick owns these fields.
if (!pickRecipeSuperseded.current?.()) {
setSteps((cur) => (cur === r.appliedSteps ? r.steps : cur));
setGuidance((cur) => (cur === r.appliedGuidance ? r.guidance : cur));
@@ -1217,8 +1216,8 @@ export function ImagesPage({
r.releaseRecipeClaim?.();
r.releaseRecipeClaim = undefined;
}, []);
- // The recipe a pick optimistically claimed, until status confirms it or a failed load reverts
- // it. Without this the Default preset would read as "modified" for the whole download.
+ // The recipe a pick optimistically claimed until status confirms it or a failed load reverts
+ // it; without this the Default preset reads as "modified" for the whole download.
const [pendingModelDefaults, setPendingModelDefaults] = useState<{
steps: number;
guidance: number;
@@ -1227,8 +1226,8 @@ export function ImagesPage({
// Batch size = images per forward pass (VRAM-heavy); count = sequential loops.
const [batchSize, setBatchSize] = useState(1);
const [count, setCount] = useState(1);
- // Active workflow tab: "create" = text-to-image, "transform" = img2img, "inpaint" = mask-guided redraw. More tabs slot in here.
- // Workflow and page mode live in a store so the sidebar's Images submenu can drive them.
+ // Active workflow tab: create = text-to-image, transform = img2img, inpaint = mask-guided
+ // redraw. Workflow and page mode live in a store so the sidebar submenu can drive them.
const workflow = useImageWorkflowStore((s) => s.workflow);
const setWorkflow = useImageWorkflowStore((s) => s.setWorkflow);
const supported = useImageWorkflowStore((s) => s.supported);
@@ -1236,7 +1235,8 @@ export function ImagesPage({
// Transform (img2img) / Inpaint inputs: the source image as a data URL, and the denoise strength.
const [initImage, setInitImage] = useState<string | null>(null);
const [strength, setStrength] = useState(0.6);
- // Inpaint mask (grayscale PNG data URL, white = repaint), brush size as a percent of the shorter side, and a clear key.
+ // Inpaint mask (grayscale PNG data URL, white = repaint), brush size as a percent of the
+ // shorter side, and a clear key.
const [maskImage, setMaskImage] = useState<string | null>(null);
const [brushPct, setBrushPct] = useState(8);
const [maskResetKey, setMaskResetKey] = useState(0);
@@ -1251,7 +1251,7 @@ export function ImagesPage({
// Upscale (hires fix): the enlargement factor and the low denoise strength that re-details the result.
const [upscaleFactor, setUpscaleFactor] = useState(2);
const [upscaleStrength, setUpscaleStrength] = useState(0.35);
- // Reference (FLUX.2): up to 3 ADDITIONAL reference images beyond the primary one, combined by the model.
+ // Reference (FLUX.2): up to 3 ADDITIONAL reference images beyond the primary one.
const [referenceImages, setReferenceImages] = useState<string[]>([]);
// LoRA adapters selected for the next generation (id + weight), plus the list the picker offers.
const [loras, setLoras] = useState<LoraSpecInput[]>([]);
@@ -1268,16 +1268,17 @@ export function ImagesPage({
// ControlNet for the next generation: model id, control image, how to derive the map, and the strength.
const [controlnetId, setControlnetId] = useState<string>("");
const [controlImage, setControlImage] = useState<string | null>(null);
- // Free-form: a union ControlNet advertises depth/pose alongside "canny"; the picker is built from its own control_types.
+ // Free-form: a union ControlNet advertises depth/pose alongside "canny", so the picker is
+ // built from its own control_types.
const [controlType, setControlType] = useState<string>("passthrough");
const [controlStrength, setControlStrength] = useState(0.7);
const [availableControlNets, setAvailableControlNets] = useState<DiffusionControlNetInfo[]>([]);
- // Advanced options live in a right-docked panel, closed by default; one fixed top-bar toggle opens it.
- // Sits inline under Seed; the open state is remembered across visits.
+ // Advanced options live in a right-docked panel, closed by default; the open state is remembered across visits.
const [advancedOpen, setAdvancedOpen] = usePersistedToggle(
"unsloth_images_advanced_open",
);
- // Advanced (load-time) options; "auto"/"off"/"none" map to the backend defaults. Changing them while loaded shows "Reapply".
+ // Advanced (load-time) options; "auto"/"off"/"none" map to the backend defaults. Changing one
+ // while loaded shows "Reapply".
const [speedMode, setSpeedMode] = useState<"auto" | "off" | "eager" | "default" | "max">("auto");
const [transformerQuant, setTransformerQuant] = useState<
"none" | "auto" | "int8" | "fp8" | "nvfp4" | "mxfp8"
@@ -1286,11 +1287,9 @@ export function ImagesPage({
"auto",
);
const [memoryMode, setMemoryMode] = useState<"auto" | "fast" | "balanced" | "low_vram">("auto");
- // "auto", or the physical index to pin this load to. Only offered on a multi-card CUDA / ROCm host.
- // Persisted, unlike the selects around it: those are reseeded from the loaded build, and the
- // status carries the device a pipeline is on but not which card, so a refresh would reset this
- // one to Auto while the model stayed put and the next Reapply would move it to the default GPU.
- // A stored id is only a hint; the send path below still drops one whose card is no longer there.
+ // "auto", or the physical index to pin this load to; offered only on a multi-card CUDA/ROCm
+ // host. Persisted, unlike the selects around it: status carries the device a pipeline is on
+ // but not which card, so a refresh would reset it to Auto. A stale id is dropped on send.
const [selectedGpu, setSelectedGpu] = usePersistedChoice(
"unsloth_image_gpu_choice",
"auto",
@@ -1298,22 +1297,24 @@ export function ImagesPage({
const gpuChoices = useDiffusionGpuChoices();
const [transformerCache, setTransformerCache] = useState<"auto" | "off" | "fbcache">("auto");
const [cpuOffload, setCpuOffload] = useState(false);
- // The last load descriptor, so "Reapply" can reload the same model with new advanced options without the user re-picking it.
+ // The last load descriptor, so "Reapply" can reload the same model with new advanced options without re-picking it.
const lastLoad = useRef<{ repoId: string; kind: "gguf" | "single_file" | "pipeline"; filename?: string } | null>(
null,
);
// Render-safe mirror of whether a page-initiated load supplied a complete Reapply target.
const [canReapply, setCanReapply] = useState(false);
- // Repo id whose defaults were already seeded from a discovered resident model, so we seed once and never clobber a manual edit.
+ // Repo id whose defaults were already seeded from a discovered resident model, so we seed
+ // once and never clobber a manual edit.
const seededResident = useRef<string | null>(null);
const [busy, setBusy] = useState<Busy>(null);
- // {done, total} while a multi-run generation is in flight (null = idle); the total is just `count`.
+ // {done, total} while a multi-run generation is in flight (null = idle).
const [genDone, setGenDone] = useState<number | null>(null);
// Live per-step progress (step / total + ETA) polled during generation.
const [genStep, setGenStep] = useState<DiffusionGenerateProgress | null>(null);
const genPollTimer = useRef<ReturnType<typeof setInterval> | null>(null);
- // visibilitychange handler active while a generation poll runs: background tabs clamp setInterval, so returning fires one immediate poll.
+ // visibilitychange handler active while a generation poll runs: background tabs clamp
+ // setInterval, so returning fires one immediate poll.
const genVisibilityListener = useRef<(() => void) | null>(null);
const [status, setStatus] = useState<DiffusionStatus | null>(null);
// Controlled so the body-portaled overlays force-close while this page is mounted but off-tab.
@@ -1326,7 +1327,7 @@ export function ImagesPage({
const [srcById, setSrcById] = useState<Record<string, string>>(() =>
galleryCache.srcById.toRecord(),
);
- // Guards a "load more" so a fast scroll can't fire several at once.
+ // Guards a "load more" so a fast scroll cannot fire several at once.
const loadingMore = useRef(false);
// The gallery strip, used as the IntersectionObserver root so a tile PNG is fetched as it nears view.
const stripRef = useRef<HTMLDivElement | null>(null);
@@ -1334,49 +1335,44 @@ export function ImagesPage({
const visibleIds = useRef<Set<string>>(new Set());
// False once the page truly unmounts. Tab switches keep it mounted, so a batch keeps generating off-tab.
const isMounted = useRef(true);
- // Set by Stop for the duration of one handleGenerate call. The backend cancel only reaches the
- // denoise that is running RIGHT NOW, so a multi-run request (count > 1) would start its next run
- // straight after; this breaks the loop as well. Cleared when the run settles.
+ // Set by Stop for one handleGenerate call: the backend cancel only reaches the denoise running
+ // RIGHT NOW, so a count > 1 request would start its next run straight after.
const cancelRequested = useRef(false);
- // True only once the backend has answered {cancelled: true}, i.e. it really stopped a running
- // denoise. Anything else -- a POST that never landed, or a {cancelled: false} because the run
- // was already past its last cancellation check -- means the run was NOT stopped, so an error it
- // raises afterwards is a real failure and not the user's own Stop coming back.
+ // True only once the backend answered {cancelled: true}. Anything else means the run was NOT
+ // stopped, so an error it raises afterwards is a real failure.
+ // Anything else -- a POST that never landed, or {cancelled: false} because the run was already past
+ // its last cancellation check -- means it was NOT stopped.
const cancelAcked = useRef(false);
// Bumped once per handleGenerate call, so a cancel can tell its own run from a later one.
const runToken = useRef(0);
- // The run token owning the Stop currently on the wire, or null. Without it each extra click
- // posts again, and a late duplicate can land after the run settled and stop whichever
- // generation is running by then. Held as a token rather than a flag because the clear is
- // asynchronous: a slow cancel from the PREVIOUS run must neither swallow this run's Stop nor,
- // when it finally settles, release this run's guard.
+ // The run token owning the Stop on the wire, or null: without it each extra click posts again
+ // and a late duplicate stops whichever generation is running by then. A token, not a flag,
+ // because the clear is asynchronous.
const cancelInFlight = useRef<number | null>(null);
- // Aborts the Stop still on the wire, if any. A pending cancel outlives its run when it is
- // waiting on a 401 refresh-and-replay, and the replay would then target whatever is generating
- // by the time it lands. Dropping it as the next run starts is what keeps that from happening.
+ // Aborts the Stop still on the wire: a pending cancel outlives its run while waiting on a 401
+ // refresh-and-replay, and the replay would target whatever is generating by then.
const cancelAbort = useRef<AbortController | null>(null);
const pollTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
- // The persistent load toast's id, so each poll updates it in place (chat-style).
+ // The persistent load toast's id, so each poll updates it in place.
const loadToastId = useRef<string | number | null>(null);
// Last load-progress signature shown, so a tick that moved nothing skips the toast.
const lastLoadSig = useRef<string | null>(null);
- // The quant to restore if the optimistic swap fails: a same-repo change sets `quant` immediately for picker feedback, but a
- // load failing AFTER starting leaves the old pipeline. `{ prev }` distinguishes "revert to null" from "nothing pending".
- // A pick also applies its own step/guidance recipe, so the rollback carries those too: a cancelled Turbo pick
- // otherwise leaves a 4-step, guidance-0 recipe applied to the non-distilled model that is still resident.
+ // The quant to restore if the optimistic swap fails; `{ prev }` distinguishes "revert to
+ // null" from "nothing pending". A pick also applies its step/guidance recipe, so the
+ // rollback carries that too, or a cancelled Turbo pick leaves a 4-step recipe behind.
const quantRevert = useRef<PickRevert | null>(null);
- // Which quantRevert entry the live staged download belongs to. Staging does not set `busy`, so a second pick can overwrite
- // quantRevert while the first plan is still resolving; without this the dying first job reverts the newer pick's label.
+ // Which quantRevert entry the live staged download belongs to: staging does not set `busy`, so
+ // a second pick can overwrite quantRevert while the first plan resolves.
const stagedQuantRevert = useRef<PickRevert | null>(null);
// Bumped per Hub pick, so a plan that resolves after a newer pick can tell it has been superseded.
const pickSeq = useRef(0);
- // The Reapply target to restore if the optimistic swap fails: handleLoad overwrites lastLoad.current at load start, and a
- // load failing after that leaves the previous pipeline resident. Mirrors quantRevert.
+ // The Reapply target to restore if the optimistic swap fails: handleLoad overwrites
+ // lastLoad.current at load start. Mirrors quantRevert.
const lastLoadRevert = useRef<{ prev: typeof lastLoad.current } | null>(null);
// A trained adapter awaiting deployment: applied once the base is loaded and LoRA-capable for its family.
const pendingDeploy = useRef<{ loraId: string; family: string } | null>(null);
- // Which pick owns the page: resolving and staging are requests that do not set `busy`, so a pick can land on an awaiting
- // one. Lazy state, not a ref: a ref cannot be written during render.
+ // Which pick owns the page: resolving and staging do not set `busy`, so a pick can land on an
+ // awaiting one. Lazy state, not a ref, since a ref cannot be written during render.
const [pickGuard] = useState(createPickGuard);
const imagePresetParams = useMemo<ImageGenerationPresetParams>(
@@ -1407,8 +1403,8 @@ export function ImagesPage({
}, [pendingModelDefaults, status?.base_repo, status?.repo_id]);
const applyImagePresetParams = useCallback((params: ImageGenerationPresetParams) => {
setNegativePrompt(params.negativePrompt);
- // Same rule restoreSettings follows: a negative prompt that is in effect has to be visible, or
- // the user generates against a setting the collapsed field is hiding.
+ // Same rule restoreSettings follows: a negative prompt in effect has to be visible, or the
+ // user generates against a setting the collapsed field is hiding.
if (params.negativePrompt) setNegativeOpen(true);
setWidth(params.width);
setHeight(params.height);
@@ -1437,8 +1433,8 @@ export function ImagesPage({
revert.commitRecipeClaim = claim.commit;
revert.releaseRecipeClaim = claim.release;
}
- // Baselined per pick, including a pick that inherits an earlier one's rollback: the question
- // is whether the user takes the form after THIS pick, not after the one it replaced.
+ // Baselined per pick, including one that inherits an earlier pick's rollback: the question is
+ // whether the user takes the form after THIS pick.
const claimedAt = imageFormClaimId();
pickRecipeSuperseded.current = () => imageFormClaimId() !== claimedAt;
const recommended = defaultsFor(repoId);
@@ -1458,46 +1454,37 @@ export function ImagesPage({
loadToastId.current = null;
}, []);
- // The load toast is built by handleLoad and the progress poll, both of which are defined above
- // handleCancelLoad (it needs handleUnload, which needs them). Route the action through a ref so
- // the toast keeps a stable onClick instead of dragging the whole load graph into its deps.
+ // The load toast is built by handleLoad and the progress poll, both defined above
+ // handleCancelLoad, so the action goes through a ref to keep a stable onClick.
const cancelLoadRef = useRef<() => void>(() => {});
const cancelLoadFromToast = useCallback(() => cancelLoadRef.current(), []);
- // Bumped by every cancel / eject (see dropResidentState). Requests that were already awaiting a
- // response when the cancel landed compare against it and discard their own result.
+ // Bumped by every cancel / eject (see dropResidentState): requests already awaiting a response
+ // compare against it and discard their own result.
const cancelSeq = useRef(0);
- // Bumped by every load start. The compensating unload below carries no identity, so it must not
- // fire once a newer load owns the page -- it would tear that one down instead.
+ // Bumped by every load start. The compensating unload below carries no identity, so it must
+ // not fire once a newer load owns the page.
const loadSeq = useRef(0);
- // The load currently in flight, if any, as a promise that settles only once handleLoad has run
- // to the end -- including the compensating unload it may issue. A cancel that lands before the
- // backend has registered the load has to wait for all of it: begin_load REFUSES a second load
- // while one is live ("a load is already in progress"), so a model picked in that window would be
- // rejected while the cancelled one kept going, and the compensating unload names no load, so one
- // still in flight would tear down whatever the user picked next. Holding busy shuts both.
+ // The load in flight, as a promise that settles only once handleLoad has run to the end,
+ // compensating unload included. begin_load REFUSES a second load while one is live, so a
+ // model picked in that window would be rejected while the cancelled one kept going.
const pendingStart = useRef<Promise<unknown> | null>(null);
// Set by restoreLoadTracking: handleLoad's compensating unload failed, so the load it was
- // cancelling is STILL running and its toast and poll are back up. handleUnload reads it to
- // report that the eject stopped nothing, rather than claiming success over a live load.
+ // cancelling is STILL running. handleUnload reads it to report that the eject did nothing.
const loadTrackingRestored = useRef(false);
- // Client-side state that only means anything while a model is resident: the
- // in-flight replacement load's tracking, and the Reapply target. Shared with
- // the indicator eject, which frees the runtime without going through the
- // page's own Unload.
+ // Client-side state that only means anything while a model is resident: the replacement
+ // load's tracking and the Reapply target. Shared with the indicator eject.
const dropResidentState = useCallback(() => {
- // Cancel, not release: a resolving pick or a staged download would load
- // back what was just ejected. In here rather than only in handleUnload, so
- // an eject driven from the loaded models card is covered by it too.
+ // Cancel, not release: a resolving pick or a staged download would load back what was just
+ // ejected. Here rather than in handleUnload, so the loaded-models card is covered too.
pickGuard.cancel();
- // Everything already in flight is now stale. Clearing the timer below stops the NEXT poll
- // tick, but not a poll or a start request currently awaiting its response, and those still
- // apply terminal state when they land. The counter is what they compare against.
+ // Everything in flight is now stale. Clearing the timer stops the NEXT poll tick but not a
+ // request awaiting its response, and those still apply terminal state; the counter is what
+ // they compare against.
cancelSeq.current += 1;
- // A deploy that was cancelled must not resurface: this ref outlives the load and is applied
- // to whatever LoRA-capable model becomes resident next, which would silently mix a discarded
- // adapter into an unrelated model's output.
+ // A cancelled deploy must not resurface: this ref outlives the load and would silently mix a
+ // discarded adapter into an unrelated model's output.
pendingDeploy.current = null;
if (pollTimer.current) clearTimeout(pollTimer.current);
pollTimer.current = null;
@@ -1506,10 +1493,8 @@ export function ImagesPage({
// Leaving this set would let Reapply reload the model that was just freed.
lastLoad.current = null;
setCanReapply(false);
- // Stopping the poll above also stops its "the load was cancelled or evicted" branch, which is
- // what hands back a pick that never became resident. Do it here, exactly as that branch would:
- // an unreleased recipe claim leaves hydration parked behind a load that is never coming, and a
- // rollback left behind is one a later pick would inherit in place of its own.
+ // Stopping the poll also stops its "cancelled or evicted" branch, which is what hands back a
+ // pick that never became resident, so do it here exactly as that branch would.
if (quantRevert.current) {
revertPick(quantRevert.current);
quantRevert.current = null;
@@ -1524,15 +1509,15 @@ export function ImagesPage({
galleryCache.quant = quant;
}, [images, hasMore, selectedId, quant]);
- // Refresh the LoRA picker when the loaded family changes: a LoRA is family-specific, so a real SWAP invalidates the selection. Not on
- // first load or unload (a restore can precede the load), so track the family in a ref. Picks are not filtered against the catalog (free-text ids).
+ // Refresh the LoRA picker when the loaded family changes, since a LoRA is family-specific.
+ // Not on first load or unload (a restore can precede the load), so track it in a ref.
const loraCapable = Boolean(status?.loaded && status?.supports_lora);
const prevLoraFamilyRef = useRef<string | null | undefined>(undefined);
// Whether the load in flight baked the LoRA selection into the build (see handleLoad).
const bakedLorasOnLoad = useRef(false);
useEffect(() => {
if (!loraCapable) {
- // Options are gone with the model, but keep the selection: it may have just been restored while the model reloads.
+ // Options are gone with the model, but keep the selection: it may have just been restored.
setAvailableLoras([]);
return;
}
@@ -1561,8 +1546,8 @@ export function ImagesPage({
if (!cancelled) setAvailableLoras(list);
})
.catch(() => {
- // Clear only the OPTIONS on a failed catalog refresh: this free-text picker holds selections valid without being in the
- // catalog, so a transient failure must not wipe them. Stale cross-family picks are cleared by the family-swap check above.
+ // Clear only the OPTIONS on a failed catalog refresh: this free-text picker holds selections
+ // that are valid without being in the catalog.
if (!cancelled) setAvailableLoras([]);
});
return () => {
@@ -1570,8 +1555,8 @@ export function ImagesPage({
};
}, [loraCapable, status?.family, loraRefreshKey]);
- // A torchao int8/fp8 build takes adapters ONLY at load time. Switching artifact within one family keeps the selection while
- // the new load did not bake it, so drop it once per resident build and say why, rather than 400 on the next Generate.
+ // A torchao int8/fp8 build takes adapters ONLY at load time, so drop the selection once per
+ // resident build and say why, rather than 400 on the next Generate.
const residentBuildKey = `${status?.repo_id ?? ""}|${String(
status?.resolved?.transformer_quant?.value ?? "",
)}`;
@@ -1589,7 +1574,7 @@ export function ImagesPage({
});
}, [loraCapable, residentBuildKey, status?.resolved, loras]);
- // Refresh the ControlNet options when the loaded family changes, and clear a stale selection the new model cannot use.
+ // Refresh the ControlNet options when the loaded family changes, and clear a stale selection.
const controlnetCapable = Boolean(status?.loaded && status?.supports_controlnet);
useEffect(() => {
if (!controlnetCapable) {
@@ -1613,7 +1598,7 @@ export function ImagesPage({
};
}, [controlnetCapable, status?.family]);
- // The control types offered for the selected ControlNet: a union model advertises several, a plain model its own.
+ // The control types offered for the selected ControlNet: a union model advertises several.
const controlTypeOptions = useMemo(() => {
const cn = availableControlNets.find((c) => c.id === controlnetId);
const types = cn?.control_types?.length ? cn.control_types : ["passthrough", "canny"];
@@ -1662,23 +1647,22 @@ export function ImagesPage({
}
}, []);
- // Bumped by every LOCAL change to the strip (pin, archive, delete, merged generation). A resync
- // started before one holds a snapshot the listing cannot reconcile with what the user just did.
+ // Bumped by every LOCAL change to the strip. A resync started before one holds a snapshot the
+ // listing cannot reconcile with what the user just did.
const stripEpoch = useRef(0);
- // Bumped by the window growing from the server (a load, an appended page). Not a conflict: the
- // resync just sized itself against a smaller window, so it refetches.
+ // Bumped by the window growing from the server. Not a conflict: the resync merely sized itself
+ // against a smaller window, so it refetches.
const pageEpoch = useRef(0);
- // Only the newest resync may apply: two restores in a row would otherwise let the older snapshot
- // land last and drop what the newer one showed.
+ // Only the newest resync may apply: two restores in a row would otherwise let the older snapshot land last.
const resyncSeq = useRef(0);
- // Shelf mutations in flight. The epoch is an EDGE, so a page starting after the bump and landing
- // before the row is dropped sees it hold still. A page is only trusted while this is zero.
+ // Shelf mutations in flight. The epoch is an EDGE, so a page starting after the bump and
+ // landing before the row is dropped sees it hold still.
const pendingShelfMutations = useRef(0);
const loadGallery = useCallback(async () => {
try {
// Fenced: this page renders from the module cache while the load runs, so its tiles are
- // actionable, and a pre-pin snapshot would undo the action with nothing to correct it.
+ // actionable and a pre-pin snapshot would undo the action.
const page = await fetchWhileStable(
() => stripEpoch.current,
() => getGallery(0, PAGE_SIZE),
@@ -1694,7 +1678,7 @@ export function ImagesPage({
page.images.forEach((image) => void ensureSrc(image));
}
} catch {
- // Best-effort: a failed gallery load shouldn't block the page.
+ // Best-effort: a failed gallery load should not block the page.
}
}, [ensureSrc]);
@@ -1703,8 +1687,8 @@ export function ImagesPage({
if (loadingMore.current || !galleryCache.hasMore) return;
loadingMore.current = true;
try {
- // Guarded on all three counters: an archive landing anywhere across this GET shortens the
- // shelf, and the record that shifts over the page boundary is returned by no page at all.
+ // Guarded on all three counters: an archive landing across this GET shortens the shelf, and
+ // the record that shifts over the page boundary is returned by no page at all.
const result = await fetchNextPage(
() => galleryCache.images.length,
() => stripEpoch.current,
@@ -1732,8 +1716,8 @@ export function ImagesPage({
}
}, [ensureSrc]);
- // A gallery page holds PAGE_SIZE multi-megabyte PNGs and an object URL lives until the page closes, so fetching every
- // record up front grew memory without bound. Fetch a tile as it nears the strip edge instead, re-run per page. Mirrors Video.
+ // A gallery page holds PAGE_SIZE multi-megabyte PNGs and an object URL lives until the page
+ // closes, so fetch a tile as it nears the strip edge instead. Mirrors Video.
useEffect(() => {
const root = stripRef.current;
if (!root || typeof IntersectionObserver === "undefined") return;
@@ -1753,7 +1737,8 @@ export function ImagesPage({
if (image) void ensureSrc(image);
}
},
- // rootMargin applies to the ROOT box only, so the root must be the scrolling strip; the sideways margin fetches a few tiles early.
+ // rootMargin applies to the ROOT box only, so the root must be the scrolling strip; the
+ // sideways margin fetches a few tiles early.
{ root, rootMargin: "0px 600px" },
);
for (const tile of root.querySelectorAll("[data-image-id]")) io.observe(tile);
@@ -1768,9 +1753,8 @@ export function ImagesPage({
})();
}, [selected, ensureSrc]);
- // Drop an image from the strip. `discardBlob` is for a real delete: the bytes are gone, so the
- // cached object URL must be revoked and any in-flight fetch told to throw its blob away. An
- // archived image keeps both, since the archived view shows the same thumbnail.
+ // Drop an image from the strip. `discardBlob` is for a real delete: the bytes are gone, so
+ // the object URL is revoked and any in-flight fetch discards. An archived image keeps both.
const dropFromStrip = useCallback((id: string, discardBlob: boolean) => {
if (discardBlob) {
galleryCache.srcById.delete(id); // revokes the URL with the entry
@@ -1783,8 +1767,8 @@ export function ImagesPage({
}
visibleIds.current.delete(id);
stripEpoch.current += 1;
- // Read the list from the cache (kept in sync with state every render) rather than nesting a
- // setSelectedId inside a setImages updater, which would run a side effect during dispatch.
+ // Read the list from the cache rather than nesting a setSelectedId inside a setImages
+ // updater, which would run a side effect during dispatch.
const at = galleryCache.images.findIndex((i) => i.id === id);
const next = removeGalleryItem(galleryCache.images, id);
galleryCache.images = next;
@@ -1795,7 +1779,7 @@ export function ImagesPage({
const handleDelete = useCallback(
async (id: string) => {
// Held for the whole round trip: the server shortens the shelf when it processes this, and a
- // page read inside that window sees the shortened list at an offset nothing contradicts.
+ // page read inside that window sees the shortened list at a consistent offset.
stripEpoch.current += 1;
pendingShelfMutations.current += 1;
try {
@@ -1811,28 +1795,21 @@ export function ImagesPage({
[dropFromStrip],
);
- /**
- * Refetch the loaded window from offset 0.
- *
- * Unpinning can drop an image past the end of the loaded window and promote a previously
- * unloaded one into it. The local reorder cannot know about the promoted image, and the next
- * loadMore still pages from the unchanged length, so that image would be skipped entirely until
- * a reload. Re-reading the window is the only way to see it.
- */
+ /** Refetch the loaded window from offset 0. Unpinning can drop an image past the end of the
+ * window and promote an unloaded one into it, which the local reorder cannot know about. */
const resyncWindow = useCallback(
async (count: number, stillFresh?: () => boolean) => {
const ticket = (resyncSeq.current += 1);
for (let attempt = 0; attempt < RESYNC_MAX_ATTEMPTS; attempt += 1) {
const paged = pageEpoch.current;
- // Sized against the live window, so a page appended while this ran is covered rather than
- // cut back off the bottom of the strip when the snapshot lands.
+ // Sized against the live window, so a page appended while this ran is covered rather than cut
+ // off the bottom of the strip.
const wanted = Math.max(count, galleryCache.images.length, PAGE_SIZE);
const collected: GalleryImage[] = [];
let more = false;
while (collected.length < wanted) {
- // The REMAINDER, not a whole page: a window of 51 (a page plus one new generation) would
- // otherwise ask for 100 and grow the strip to match, reading 49 recipes off disk and
- // rendering their tiles for a one-row shortfall.
+ // The REMAINDER, not a whole page: a window of 51 would otherwise ask for 100 and read 49
+ // recipes off disk for a one-row shortfall.
const page = await getGallery(
collected.length,
Math.min(PAGE_SIZE, wanted - collected.length),
@@ -1841,12 +1818,12 @@ export function ImagesPage({
more = page.has_more;
if (!page.has_more || page.images.length === 0) break;
}
- // Checked here, not by the caller: by the time this returns the window is already applied,
- // so a stale snapshot has to be dropped before it overwrites a newer local change.
+ // Checked here, not by the caller: by the time this returns the window is already applied, so
+ // a stale snapshot has to be dropped first.
if (stillFresh && !stillFresh()) return;
if (resyncSeq.current !== ticket) return;
- // Pagination moved under this pass. That is only server data, so cover it with another
- // pass instead of giving up: giving up is what left an unpin's promoted image missing.
+ // Pagination moved under this pass. That is only server data, so cover it with another pass
+ // instead of giving up, which is what left an unpin's promoted image missing.
if (pageEpoch.current !== paged) continue;
galleryCache.images = collected;
galleryCache.hasMore = more;
@@ -1861,17 +1838,16 @@ export function ImagesPage({
[ensureSrc],
);
- // This page stays mounted across route changes, so a restore from the Settings archive would
- // otherwise not reach the strip until a full reload. Resync the window that is actually loaded:
- // loadGallery would cut it back to the first page and throw away everything scrolled to.
+ // This page stays mounted across route changes, so an archive restore would not reach the
+ // strip until a reload. Resync the loaded window: loadGallery would cut it to page one.
useEffect(
() =>
subscribeGalleryChanged("images", () => {
- // Bumped FIRST: a restore changes the shelf, so reads already in flight must be discarded.
- // Capturing without advancing let them pass their own checks and land on the new window.
+ // Bumped FIRST: a restore changes the shelf, so reads already in flight must be discarded, or
+ // they pass their own checks and land on the new window.
stripEpoch.current += 1;
- // Fenced like the unpin resync: a generation or a new page landing while this GET runs
- // would otherwise be overwritten by a snapshot taken before it.
+ // Fenced like the unpin resync: a generation or a new page landing while this GET runs would
+ // be overwritten by a snapshot taken before it.
const epoch = stripEpoch.current;
void resyncWindow(
galleryCache.images.length,
@@ -1881,20 +1857,19 @@ export function ImagesPage({
[loadGallery, resyncWindow],
);
- // The pin state each id was last CLICKED into, so a failing request can tell whether it is still
- // the current intent. Without it, a slow first click failing after a later click succeeded would
- // roll the strip back onto the state the user has since moved off.
+ // The pin state each id was last CLICKED into, so a failing request can tell whether it is
+ // still the current intent; without it a slow failure rolls back a later success.
const pinAttempt = useRef(new Map<string, number>());
const pinSeq = useRef(0);
const handleTogglePin = useCallback(
async (id: string, pinned: boolean) => {
const loadedCount = galleryCache.images.length;
- // The pinned order as it stands BEFORE the click, so a failed unpin can put the image back
- // where it was instead of at the front of the pins.
+ // The pinned order BEFORE the click, so a failed unpin can put the image back where it was
+ // instead of at the front.
const orderBefore = pinnedOrder(galleryCache.images);
- // A per-attempt token, not the target boolean: pin, unpin, pin stores true twice, so the FIRST
- // attempt's failure would roll back the THIRD attempt's pin and leave the two disagreeing.
+ // A per-attempt token, not the target boolean: pin, unpin, pin stores true twice, so the first
+ // attempt's failure would roll back the third attempt's pin.
const attempt = (pinSeq.current += 1);
pinAttempt.current.set(id, attempt);
stripEpoch.current += 1;
@@ -1906,21 +1881,19 @@ export function ImagesPage({
return next;
});
try {
- // One queue for the whole gallery, not one per image. The server stamps `pinned_at` when
- // it runs the PATCH and orders pins by that stamp, so two requests in flight together can
- // be stamped in either order and the strip disagrees with the next load. Issuing them one
- // at a time makes the stamps follow the clicks.
+ // One queue for the whole gallery: the server stamps `pinned_at` when it runs the PATCH, so
+ // two requests in flight can be stamped in either order. One at a time follows the clicks.
await serializeById("image-pin", () => setGalleryImageFlags(id, { pinned }));
} catch (err) {
toast.error(err instanceof Error ? err.message : "Failed to pin image");
- // Put the old order back rather than leave the strip lying about server state, but only
- // while this is still what the user last asked for.
+ // Put the old order back rather than leave the strip lying about server state, but only while
+ // this is still what the user last asked for.
if (pinAttempt.current.get(id) === attempt) {
pinAttempt.current.delete(id);
stripEpoch.current += 1;
setImages((prev) => {
- // A failed pin simply goes back to unpinned; a failed unpin has to be restored to its
- // old position among the pins, which applyPin cannot do (it means "freshly pinned").
+ // A failed pin goes back to unpinned; a failed unpin has to be restored to its old position
+ // among the pins, which applyPin cannot do.
const next = pinned
? applyPin(prev, id, false)
: restorePinOrder(prev, id, orderBefore);
@@ -1932,12 +1905,10 @@ export function ImagesPage({
}
if (pinAttempt.current.get(id) !== attempt) return; // superseded by a later click
pinAttempt.current.delete(id);
- // Pinning keeps the same set in the window (it only moves an already-loaded image to the
- // front), so only unpinning can open a gap.
+ // Pinning keeps the same set in the window, so only unpinning can open a gap.
if (!pinned && loadedCount > 0) {
try {
- // Fenced: a pin clicked while this GET is in flight would otherwise be overwritten by a
- // snapshot taken before it, leaving the strip unpinned while the server is pinned.
+ // Fenced: a pin clicked while this GET is in flight would be overwritten by a snapshot taken before it.
await resyncWindow(loadedCount, () => stripEpoch.current === epoch);
} catch {
// Best-effort: the strip is still usable, just possibly short one image until a reload.
@@ -1949,8 +1920,8 @@ export function ImagesPage({
const handleArchive = useCallback(
async (id: string) => {
- // Held for the whole round trip: the server shortens the shelf when it processes this, and a
- // page read inside that window sees the shortened list at an offset nothing contradicts.
+ // Held for the whole round trip: the server shortens the shelf when it processes this, so a
+ // page read inside that window still sees a consistent offset.
stripEpoch.current += 1;
pendingShelfMutations.current += 1;
try {
@@ -1982,13 +1953,14 @@ export function ImagesPage({
// Load an image's recipe back into the form inputs.
const restoreSettings = useCallback((image: GalleryImage) => {
setPrompt(image.prompt);
- // Negative prompt only applies when guidance>0; don't restore a hidden value.
+ // Negative prompt only applies when guidance>0; do not restore a hidden value.
const restoredNegative = image.guidance > 0 ? (image.negative_prompt ?? "") : "";
setNegativePrompt(restoredNegative);
if (restoredNegative) setNegativeOpen(true);
setSteps(image.steps);
setGuidance(image.guidance);
- // Restore from the BASE batch seed, not this image's derived seed, or replaying with batch_size would advance again.
+ // Restore from the BASE batch seed, not this image's derived seed, or a replay with batch_size
+ // advances it again.
setSeed(String(image.batch_seed ?? image.seed));
setWidth(image.width);
setHeight(image.height);
@@ -1997,8 +1969,8 @@ export function ImagesPage({
const m = matchAspect(image.width, image.height);
setAspect(m.key);
setPortrait(m.portrait);
- // Restore selected LoRA adapters from the recipe ("id:weight"); split on the LAST colon so an id containing ':' survives.
- // A recipe with no LoRAs clears the selection, so the restore reproduces the image faithfully.
+ // Restore LoRA adapters from the recipe ("id:weight"), splitting on the LAST colon so an id
+ // containing ':' survives. A recipe with no LoRAs clears the selection.
const restoredLoras: LoraSpecInput[] = [];
for (const entry of image.loras ?? []) {
const idx = entry.lastIndexOf(":");
@@ -2014,12 +1986,13 @@ export function ImagesPage({
else setStrength(image.strength);
}
if (typeof image.upscale === "number") setUpscaleFactor(image.upscale);
- // None of the conditioning images are persisted, so a restore must clear the Transform / Inpaint / Edit uploads and return to Create.
+ // None of the conditioning images are persisted, so a restore must clear the Transform /
+ // Inpaint / Edit uploads and return to Create.
setWorkflow("create");
setInitImage(null);
setMaskImage(null);
setReferenceImages([]);
- // The control image isn't persisted, so clear any stale ControlNet selection.
+ // The control image is not persisted, so clear any stale ControlNet selection.
setControlnetId("");
setControlImage(null);
// Say so, rather than letting a conditioned image restore as a plain Create that generates something unrelated.
@@ -2057,10 +2030,8 @@ export function ImagesPage({
setPortrait((p) => !p);
};
- // A status read started before an eject can answer after the one that
- // followed it, and this page has no periodic poll to correct it: the controls
- // would go on offering to generate against a runtime that is already free.
- // So every read takes a ticket and only the newest may write.
+ // A status read started before an eject can answer after the one that followed it, and this
+ // page has no periodic poll to correct it, so only the newest ticket may write.
const statusTicket = useRef(0);
const setStatusIfNewest = useCallback(
(ticket: number, next: DiffusionStatus) => {
@@ -2074,12 +2045,12 @@ export function ImagesPage({
try {
setStatusIfNewest(ticket, await getDiffusionStatus());
} catch {
- // Status is best-effort; a failed poll shouldn't surface an error toast.
+ // Status is best-effort; a failed poll should not surface an error toast.
}
}, [setStatusIfNewest]);
- // Track mount so a long generate run stops issuing GPU work only on a true unmount; the page stays mounted across tab
- // switches, so a batch keeps generating off-tab. The mount-time refresh and cleanup live in the load-resume effect below.
+ // Track mount so a long generate run stops issuing GPU work only on a true unmount; the page
+ // stays mounted across tab switches, so a batch keeps generating off-tab.
useEffect(() => {
isMounted.current = true;
return () => {
@@ -2116,25 +2087,15 @@ export function ImagesPage({
};
}, [active, ensureSrc, loadGallery, onInitialReady, refreshStatus]);
- // Ejected from the loaded models indicator, which does not run handleUnload:
- // without this the controls keep offering to generate on a freed runtime, and
- // Reapply still points at the model that was just ejected. The runtime is
- // already free, so this is handleUnload without the unload call.
+ // Ejected from the loaded models indicator, which does not run handleUnload: without this the
+ // controls keep offering to generate on a freed runtime. So: handleUnload minus the unload.
useEffect(
() =>
subscribeModelEjected("image", () => {
dropResidentState();
- // That eject cancelled the replacement load, and its progress poll is
- // the only thing that clears `busy` -- which dropResidentState has just
- // stopped. Leaving it set locks the page: the picker ignores every
- // choice while busy, and Unload is not offered once the status read
- // comes back empty, so only an app reload recovered. Narrowed to
- // "loading" so a generation in flight is left alone, as handleUnload's
- // own finally does.
- // ...but not while a load start is still in flight. begin_load refuses a second load
- // while one is registered, so a model picked in that window is rejected while the load
- // this eject was meant to cancel carries on, and handleLoad's compensating unload names
- // no load. Hold busy until the whole path settles, exactly as handleCancelLoad does.
+ // That eject cancelled the replacement load, and its progress poll is the only thing that
+ // clears `busy`, which dropResidentState just stopped; leaving it set locks the page.
+ // Narrowed to "loading" so a generation is left alone, and held until the start settles.
const pending = pendingStart.current;
if (pending) {
setBusy((prev) => (prev === "loading" ? "unloading" : prev));
@@ -2150,14 +2111,15 @@ export function ImagesPage({
[refreshStatus, dropResidentState],
);
- // Collapse the body-ported popovers when leaving the tab: the open flag stays set, so returning would pop them back open.
+ // Collapse the body-ported popovers when leaving the tab: the open flag stays set, so
+ // returning would pop them back open.
useEffect(() => {
if (active) return;
setSelectorOpen(false);
setAspectOpen(false);
}, [active]);
- // Poll load-progress until the background load reaches "ready" or "error", updating the persistent toast in place each tick.
+ // Poll load-progress until the background load reaches "ready" or "error", updating the persistent toast in place.
const pollLoadProgress = useCallback(async () => {
// This tick's cancellation fence: clearing pollTimer stops the next tick, not the awaits below.
const seq = cancelSeq.current;
@@ -2169,10 +2131,8 @@ export function ImagesPage({
const ticket = ++statusTicket.current;
const loaded = await getDiffusionStatus();
if (seq !== cancelSeq.current) {
- // Cancelled while this read was in flight, so it describes a pipeline being torn down.
- // Drop it and refresh NOTHING: the unload's own response is authoritative and already
- // holds the newest ticket, and a status read issued from here would take a newer one
- // still and could re-report the model mid-teardown.
+ // Cancelled while this read was in flight, so it describes a pipeline being torn down. Drop
+ // it and refresh NOTHING: the unload's own response is authoritative.
return;
}
setStatusIfNewest(ticket, loaded);
@@ -2191,7 +2151,8 @@ export function ImagesPage({
dismissLoadToast();
reportLoadFailure(p.error, "Failed to load model");
setBusy(null);
- // A load that failed AFTER starting leaves the previous pipeline loaded, so roll the optimistic quant label back.
+ // A load that failed AFTER starting leaves the previous pipeline loaded, so roll the
+ // optimistic quant label back.
if (quantRevert.current) {
revertPick(quantRevert.current);
quantRevert.current = null;
@@ -2207,7 +2168,8 @@ export function ImagesPage({
return;
}
if (p.phase === null) {
- // No load in flight and nothing loaded: the load was cancelled or evicted. Terminal, else this loop spins forever.
+ // No load in flight and nothing loaded: the load was cancelled or evicted. Terminal, else this
+ // loop spins forever.
dismissLoadToast();
setBusy(null);
// Same optimistic-quant rollback as the error path: the swap did not take.
@@ -2215,7 +2177,7 @@ export function ImagesPage({
revertPick(quantRevert.current);
quantRevert.current = null;
}
- // Restore the Reapply target too, so it never lingers on the failed pick after a cancel or eviction.
+ // Restore the Reapply target too, so it never lingers on the failed pick.
if (lastLoadRevert.current) {
lastLoad.current = lastLoadRevert.current.prev;
setCanReapply(lastLoadRevert.current.prev != null);
@@ -2238,9 +2200,8 @@ export function ImagesPage({
}, [dismissLoadToast, refreshStatus, cancelLoadFromToast]);
// Put back what a teardown removed when the load it was tearing down is still running: the
- // unload failed, so the poll and the toast were stopped for nothing. refreshStatus cannot do
- // this -- a first load is not resident yet, so status has nothing to report -- and without it a
- // multi-gigabyte load continues with no progress and no way to cancel it a second time.
+ // unload failed, so the poll and toast were stopped for nothing. refreshStatus cannot do
+ // this, since a first load is not resident yet.
const restoreLoadTracking = useCallback(() => {
loadTrackingRestored.current = true;
setBusy("loading");
@@ -2249,8 +2210,8 @@ export function ImagesPage({
void pollLoadProgress();
}, [pollLoadProgress, cancelLoadFromToast]);
- // Re-enter the per-step poll for a generation already in flight that this page did not start, instead of a stale idle view.
- // generate-progress carries no terminal record, so refresh the gallery on completion to merge any image saved after mount.
+ // Re-enter the per-step poll for a generation in flight that this page did not start.
+ // generate-progress carries no terminal record, so refresh the gallery on completion.
const resumeGeneratePoll = useCallback(() => {
if (genPollTimer.current) clearInterval(genPollTimer.current);
if (genVisibilityListener.current)
@@ -2333,8 +2294,8 @@ export function ImagesPage({
};
}, [refreshStatus, dismissLoadToast, pollLoadProgress, resumeGeneratePoll, cancelLoadFromToast]);
- // Seed the generation sliders from a resident model's recipe when the page finds one it did not load itself, else they keep the
- // unrecognised-model fallback and a resident flux.1-dev generates garbage at 9 steps. Guarded by lastLoad.current === null and a per-repo ref.
+ // Seed the sliders from a resident model's recipe when the page finds one it did not load,
+ // else a resident flux.1-dev generates garbage at 9 steps. Guarded by a per-repo ref.
const residentSeeded = useRef(false);
useEffect(() => {
const repoId = status?.loaded ? status.repo_id : null;
@@ -2342,35 +2303,29 @@ export function ImagesPage({
if (lastLoad.current) return;
if (seededResident.current === repoId) return;
seededResident.current = repoId;
- // Wire Reapply to the resident model too, so an advanced-option reload works without
- // re-picking. Only a full pipeline is reloadable by repo id alone; a resident GGUF/single_file
- // carries no checkpoint filename, so leave the target null for those and the button hidden.
- // Set before the recipe decision below: whether Reapply has a target is a separate question
- // from whether the model's defaults should seed the form.
+ // Wire Reapply to the resident model too. Only a full pipeline is reloadable by repo id
+ // alone; a resident GGUF carries no checkpoint filename, so the target stays null and the
+ // button hidden. Set before the recipe decision below, which is a separate question.
if (status?.model_kind === "pipeline") {
lastLoad.current = { repoId, kind: "pipeline" };
}
- // A stored recipe is the user's own choice, so it outranks the resident model's defaults on
- // the first seed. Later resident changes still seed, as picking a model always has.
+ // A stored recipe is the user's own choice, so it outranks the resident model's defaults on the first seed.
if (!residentSeeded.current) {
residentSeeded.current = true;
if (imagePresets.storedRecipe) return;
}
- // Seed from base_repo (the resolved diffusers base, holding the family), not repo_id: a GGUF resident has no family substring.
- // Status is the authority for a resident model, so this is not a pick's optimistic claim.
+ // Seed from base_repo (the resolved diffusers base, holding the family), not repo_id: a GGUF
+ // resident has no family substring. Status is the authority for a resident model.
const d = defaultsFor(status?.base_repo ?? repoId);
setPendingModelDefaults(null);
setSteps(d.steps);
setGuidance(d.guidance);
}, [imagePresets.storedRecipe, status?.loaded, status?.repo_id, status?.base_repo, status?.model_kind]);
- // Reseed the Advanced selects from the LOADED build, so they stop being pure local request state.
- // An honored request re-selects itself (a no-op); a declined one snaps to what actually engaged,
- // which is the input-side half of P1-2: the Precision dropdown must never go on advertising a
- // scheme the loaded model is not running. Keyed on the LOAD-TIME half of the resolved record, so
- // a user edit made after the load survives until the next load replaces it -- the backend
- // rewrites the speed/attention/cache entries at GENERATION time, and serializing the whole record
- // made the 3rd image of a session throw away a Precision the user had picked but not yet loaded.
+ // Reseed the Advanced selects from the LOADED build, so a declined request snaps to what
+ // engaged and Precision never advertises a scheme the model is not running. Keyed on the
+ // LOAD-TIME half of the record: the backend rewrites the speed/attention entries at
+ // GENERATION time, and the whole record threw away a Precision picked but not yet loaded.
const resolvedKey = status?.loaded ? resolvedSeedKey(status.resolved) : null;
useEffect(() => {
const record = status?.loaded ? status.resolved : null;
@@ -2419,7 +2374,7 @@ export function ImagesPage({
memory_mode: memoryMode === "auto" ? undefined : memoryMode,
transformer_cache: transformerCache === "auto" ? undefined : transformerCache,
loras: baked.length > 0 ? baked : undefined,
- // Dropped when the chosen card is gone (a driver reset, an eGPU unplugged), so a stale pick loads automatically instead of 400ing.
+ // Dropped when the chosen card is gone, so a stale pick loads automatically instead of 400ing.
gpu_ids:
selectedGpu !== "auto" &&
gpuChoices.some((d) => String(d.index) === selectedGpu)
@@ -2448,15 +2403,14 @@ export function ImagesPage({
kind: "gguf" | "single_file" | "pipeline";
filename?: string;
},
- // The Advanced values this load must use, when pinned earlier: a staged download plans its file set at pick time and loads
- // minutes later, so reading live state here could run a load the staged files do not cover.
+ // The Advanced values this load must use when pinned earlier: a staged download plans its file
+ // set at pick time and loads minutes later, so live state could outrun the staged files.
pinned?: LoadAdvanced,
): Promise<boolean> => {
- // Cancel any prior poll loop so two can't run at once.
+ // Cancel any prior poll loop so two cannot run at once.
if (pollTimer.current) clearTimeout(pollTimer.current);
// Read BEFORE the start request goes out: a Cancel pressed while it is in flight sends an
- // unload that can reach the backend first, find no load registered, and succeed without
- // stopping anything.
+ // unload that can reach the backend first, find no load registered, and stop nothing.
const startSeq = cancelSeq.current;
const startLoad = ++loadSeq.current;
// Published now and settled in the finally below, so a cancel waits for the WHOLE path.
@@ -2465,8 +2419,7 @@ export function ImagesPage({
settleLoad = resolve;
});
pendingStart.current = inFlight;
- // Every exit below goes through this: it settles the promise a cancel is waiting on and
- // releases the ref, so the page cannot stay busy on a load that has already finished.
+ // Every exit below goes through this: it settles the promise a cancel is waiting on and releases the ref.
const settle = (started: boolean): boolean => {
settleLoad();
if (pendingStart.current === inFlight) pendingStart.current = null;
@@ -2477,11 +2430,11 @@ export function ImagesPage({
dismissLoadToast();
lastLoadSig.current = null;
loadToastId.current = toast(null, loadToastArgs(IDLE_PROGRESS, undefined, cancelLoadFromToast));
- // Remember what was loaded so "Reapply" can reload it with new advanced options. Snapshot the prior target first: a load
- // that fails to START leaves the previous model resident, so Reapply must keep pointing at it.
+ // Remember what was loaded so "Reapply" can reload it. Snapshot the prior target first: a load
+ // that fails to START leaves the previous model resident.
const prevLastLoad = lastLoad.current;
- // A torchao int8/fp8 transformer (what the default GGUF fast path picks on a capable GPU) takes adapters only at LOAD time, and
- // /images/generate then rejects a new set, so a reload must keep the selection. Ignored by bf16 / bnb-4bit, which apply at generation time.
+ // A torchao int8/fp8 transformer takes adapters only at LOAD time and /images/generate then
+ // rejects a new set, so a reload must keep the selection. Ignored by bf16 / bnb-4bit.
const advanced = pinned ?? currentLoadAdvanced(repoId);
const bakeLoras = advanced.loras ?? [];
// Whether THIS load carries the selection into the build, so a quantized load that did not can drop it.
@@ -2491,8 +2444,8 @@ export function ImagesPage({
// Carry the prior target so the async poll can restore it if the background load fails after starting.
lastLoadRevert.current = { prev: prevLastLoad };
try {
- // Returns immediately -- the load runs in the background and we poll. The backend infers the family + base repo from the id;
- // forward the saved HF token for gated bases. A pipeline load carries no filename; the "auto" sentinels map to omitted.
+ // Returns immediately; the load runs in the background and we poll. The backend infers the
+ // family and base repo from the id, and the saved HF token covers gated bases.
const startRequest = loadDiffusionModel({
model_path: repoId,
model_kind: opts.kind,
@@ -2500,10 +2453,9 @@ export function ImagesPage({
hf_token: hfApiToken(getHfToken()),
cpu_offload: advanced.cpu_offload,
speed_mode: advanced.speed_mode,
- // GGUF picks only: the dense fast path replaces a GGUF transformer, every other kind runs
- // the precision its checkpoint carries. The Precision control is hidden for those kinds,
- // but the state persists across picks, so a stale scheme would reach a load that can only
- // decline it -- and the backend now refuses a request it cannot honor.
+ // GGUF picks only: the dense fast path replaces a GGUF transformer, and every other kind runs
+ // its checkpoint's own precision. The control is hidden there but the state persists across
+ // picks, so a stale scheme would reach a load that can only decline it.
transformer_quant: opts.kind === "gguf" ? advanced.transformer_quant : undefined,
attention_backend: advanced.attention_backend,
memory_mode: advanced.memory_mode,
@@ -2523,19 +2475,15 @@ export function ImagesPage({
return settle(false);
}
if (startSeq !== cancelSeq.current) {
- // Cancelled during the start request. The unload it sent may have landed before this load
- // registered, in which case it stopped nothing and the model is loading right now with no
- // toast and no Cancel button. The load exists on the backend as of this line, so unload
- // once more -- that one cannot miss it. Unless a NEWER load has since taken the page:
- // this unload names nothing, so firing it then would cancel that load instead of this
- // one, and the newer start has already superseded this load's token on the backend.
+ // Cancelled during the start request: the unload it sent may have landed before this load
+ // registered, leaving it running with no toast and no Cancel. The load exists as of this
+ // line, so unload once more, unless a NEWER load has taken the page.
if (startLoad === loadSeq.current) {
try {
await unloadDiffusionModel();
} catch {
- // This request is the ONLY one that can still stop the load the first unload missed,
- // so a failure here is not best-effort: the load is running, untracked. Put the
- // tracking back exactly as a failed cancel does, so it stays visible and cancellable.
+ // This request is the ONLY one that can still stop the load the first unload missed, so a
+ // failure here is not best-effort: put the tracking back exactly as a failed cancel does.
restoreLoadTracking();
return settle(false);
}
@@ -2549,34 +2497,33 @@ export function ImagesPage({
[pollLoadProgress, refreshStatus, dismissLoadToast, currentLoadAdvanced, cancelLoadFromToast],
);
- // Set (or clear) the Transform/Inpaint source image; always drop any painted mask, which is sized to the previous source.
+ // Set or clear the Transform/Inpaint source image; always drop the painted mask, which is
+ // sized to the previous source.
const handleInitChange = useCallback((dataUrl: string | null) => {
setInitImage(dataUrl);
setMaskImage(null);
setMaskResetKey((k) => k + 1);
}, []);
- // Downloads go through the Hub download manager like every other model, so the load finds a warm cache. In a ref so the callback is not a render dep.
+ // Downloads go through the Hub download manager like every other model, so the load finds a
+ // warm cache. In a ref, so the callback is not a render dep.
const pendingStagedLoad = useRef<{
repoId: string;
opts: { kind: "gguf" | "single_file" | "pipeline"; filename?: string };
- // The Advanced values the plan was built from. Staging does not set `busy`, so the user can change precision or LoRAs while
- // the download runs; without this the completed load would use the new values against the old file set.
+ // The Advanced values the plan was built from: staging does not set `busy`, so the user can
+ // change precision or LoRAs while the download runs.
advanced: LoadAdvanced;
// The pick that staged it: a download outlives its pick, so it must not evict a newer one when it lands.
token: number;
} | null>(null);
const handleLoadRef = useRef(handleLoad);
handleLoadRef.current = handleLoad;
- // Set when a staged download finished while this page was hidden: both diffusion pages stay mounted and a load evicts
- // whatever holds the GPU. The pick is not dropped; it fires when this page comes back.
+ // Set when a staged download finished while this page was hidden: both diffusion pages stay
+ // mounted and a load evicts whatever holds the GPU. The pick fires on return.
const stagedLoadDeferred = useRef(false);
// Both deferred paths run the load minutes after the pick was reported started, so both need
- // the same rollback: onReady when the page is active, and the effect below when the download
- // finished off-tab. The deferred load can still be REFUSED, by a training run or another load
- // claiming the slot while the download ran. Staging started no load, so nothing polls and the
- // poll's own rollback never runs; without this the selector keeps advertising a quant that was
- // never loaded. `owned` is read BEFORE the call, so a newer pick's label is left alone.
+ // the same rollback: a deferred load can still be REFUSED, and staging polls nothing.
+ // `owned` is read BEFORE the call, so a newer pick's label is left alone.
const runStagedLoad = useCallback(
(pending: NonNullable<typeof pendingStagedLoad.current>) => {
if (pendingStagedLoad.current === pending) pendingStagedLoad.current = null;
@@ -2604,14 +2551,12 @@ export function ImagesPage({
if (pending) runStagedLoad(pending);
},
onCancelled: () => {
- // The selected model is only an intent until every dependency is ready. A cancelled
- // companion must not leave that intent behind for a late completion/deferred effect to load.
+ // The selected model is only an intent until every dependency is ready: a cancelled companion
+ // must not leave that intent behind for a late completion to load.
pendingStagedLoad.current = null;
stagedLoadDeferred.current = false;
- // Staging starts no load, so nothing polls and the poll's own rollback never runs: the
- // optimistic label has to come back here or the selector keeps describing the resident
- // model with a quant that was never loaded (and the gallery cache persists it). Only for
- // the pick that staged THIS job: a newer pick owns the label from the moment it is made.
+ // Staging starts no load, so the optimistic label must come back here or the selector keeps
+ // describing the resident model with a quant that never loaded. Only for this job's pick.
if (quantRevert.current && quantRevert.current === stagedQuantRevert.current) {
revertPick(quantRevert.current);
quantRevert.current = null;
@@ -2627,9 +2572,8 @@ export function ImagesPage({
if (pending) runStagedLoad(pending);
}, [active, runStagedLoad]);
- // Ask for a cache-aware plan for every Hub pick. The picker only knows whether the selected
- // checkpoint is cached; image models can still need a separate text encoder/VAE repository.
- // Returns true when the pick was accepted either way.
+ // Ask for a cache-aware plan on every Hub pick: the picker knows only whether the checkpoint
+ // is cached, and image models can need a separate text encoder/VAE repo. True = accepted.
const requestDownloadPlan = useCallback(
(
repoId: string,
@@ -2640,19 +2584,20 @@ export function ImagesPage({
model_path: repoId,
gguf_filename: opts.filename,
model_kind: opts.kind,
- // The same token and Advanced values handleLoad sends, so the plan describes the load that will actually run. Without the
- // token a gated base plans no companion entry; without the memory/quant controls it stages shards the load never opens.
+ // The same token and Advanced values handleLoad sends, so the plan describes the load that
+ // will run: without the token a gated base plans no companion, and without the controls it
+ // stages shards the load never opens.
hf_token: hfApiToken(getHfToken()),
cpu_offload: advanced.cpu_offload,
speed_mode: advanced.speed_mode,
// Non-GGUF loads ignore this control; the plan must describe the same request as handleLoad.
transformer_quant: opts.kind === "gguf" ? advanced.transformer_quant : undefined,
memory_mode: advanced.memory_mode,
- // The backend prefetch decision reads the adapter selection too: a baked LoRA always runs the dense build path. Omitting it
- // planned a quantized file set and staged too little. Same list handleLoad bakes.
+ // The backend prefetch decision reads the adapter selection too: a baked LoRA always runs the
+ // dense build path, and omitting it staged too little.
loras: advanced.loras,
- // The plan route preflights precision and sizes the file set against the card the load
- // will use, so a selection the load carries has to reach the plan as well.
+ // The plan route preflights precision and sizes the file set against the card the load will
+ // use, so a selection the load carries has to reach the plan.
gpu_ids: advanced.gpu_ids,
}),
[],
@@ -2665,14 +2610,14 @@ export function ImagesPage({
source: ModelSelectorChangeMeta["source"] = "hub",
token?: number,
): Promise<boolean> => {
- // Staging never sets `busy`, so a second pick passes handleModelSelect's guard while this
- // plan is still in flight. Plans then resolve in response order, not pick order: without
- // this the older one restages over the newer queue, or loads the model the user left.
- // Bumped before the non-hub return too: a local pick must invalidate an in-flight hub plan.
+ // Staging never sets `busy`, so a second pick passes the guard while this plan is in flight,
+ // and plans resolve in response order rather than pick order. Bumped before the non-hub
+ // return too, so a local pick invalidates an in-flight hub plan.
+ // Plans resolve in response order, not pick order. Bumped before the non-hub return too: a local
+ // pick must invalidate an in-flight hub plan.
const pick = ++pickSeq.current;
- // The previous pick's staged intent dies with it. A pick that stages nothing (fully cached,
- // local, no plan) never calls stage(), so the hook's queue keeps running the older job and
- // its onReady would load the model the user moved away from, evicting this one.
+ // The previous pick's staged intent dies with it: a pick that stages nothing never calls
+ // stage(), so the queue keeps the older job and its onReady loads the abandoned model.
pendingStagedLoad.current = null;
stagedLoadDeferred.current = false;
stagedQuantRevert.current = null;
@@ -2681,10 +2626,11 @@ export function ImagesPage({
if (source !== "hub") return handleLoadRef.current(repoId, opts);
// ONE snapshot for the plan and the load it fires: the download runs for minutes without setting `busy`.
const advanced = currentLoadAdvanced(repoId);
- // Read before the await: a pick made while the plan resolves replaces quantRevert, and this job must not revert it.
+ // Read before the await: a pick made while the plan resolves replaces quantRevert, and this
+ // job must not revert it.
const ownRevert = quantRevert.current;
// Read inside the try, acted on outside it: refusing from in there would fall through to the
- // load if the refusal itself threw, which is the one outcome that must not happen.
+ // load if the refusal itself threw.
let incompatible: string | null = null;
try {
const plan = await requestDownloadPlan(repoId, opts, advanced);
@@ -2705,15 +2651,11 @@ export function ImagesPage({
files: e.files,
bytes: e.bytes,
ggufFilename: e.gguf_filename,
- // The entry carrying the picked checkpoint file, so the panel can label it without
- // guessing: filenames cannot tell the two apart once a checkpoint ships as
- // .safetensors like its companions do. Repo identity alone is not enough, because a
- // checkpoint that shares its repo with the companions and is already cached leaves an
- // entry of companion files only. A pipeline pick has no one file: the repo IS it.
- // The backend's own answer wins: a gated pipeline is staged from an ungated MIRROR,
- // so its entry no longer carries the id we picked and the id test below reads the
- // whole selected model as companion assets. `??`, not `||`: a planner that says false
- // is answering, and the fallback exists only for a backend too old to send the key.
+ // The entry carrying the picked checkpoint file, so the panel can label it without guessing:
+ // filenames cannot tell the two apart, and repo identity is not enough when a checkpoint
+ // shares its repo with cached companions. The backend's answer wins, since a gated pipeline
+ // is staged from an ungated MIRROR. Nullish coalescing, not `or`: a planner
+ // answering false is still an answer.
checkpoint:
e.checkpoint ??
(opts.filename
@@ -2756,8 +2698,8 @@ export function ImagesPage({
[currentLoadAdvanced, requestDownloadPlan, pickGuard, stage],
);
- // A GGUF pick can arrive with only a repo id (a pinned row, a curated artifact, a local GGUF directory). The backend
- // rejects a gguf load with no filename and a pipeline load of a GGUF repo, so name the file from the listing first.
+ // A GGUF pick can arrive with only a repo id. The backend rejects a gguf load with no filename
+ // and a pipeline load of a GGUF repo, so name the file from the listing first.
const loadGgufRepoPick = useCallback(
async (
repoId: string,
@@ -2804,9 +2746,9 @@ export function ImagesPage({
if (!active) pickGuard.release();
}, [active, pickGuard]);
- // A diffusion model picked from the chat picker arrives as ?model= on this route. Load it once, then clear the params.
- // This route's own match, never `strict: false`: that resolves to the ROOT match, whose search is whatever route is live, and
- // /hub names its selection with the same param. `active` cannot fence that off, since it lags the matches by a render.
+ // A diffusion model picked from the chat picker arrives as ?model= on this route. This route's
+ // own match, never `strict: false`: that resolves to the ROOT match, whose search is
+ // whatever route is live, and /hub names its selection with the same param.
const routeSearch = useSearch({ from: "/images", shouldThrow: false });
const navigateSelf = useNavigate();
const handledRouteModel = useRef<string | null>(null);
@@ -2815,14 +2757,14 @@ export function ImagesPage({
if (!active) return;
if (!imagePresets.hydrated) return;
const wanted = routeSearch?.model;
- // Key on the model AND the quant, and release the marker once the query is gone: this page stays mounted, so a marker that
- // outlived the query made re-picking the same checkpoint a click that neither loaded nor cleared the URL.
+ // Key on the model AND the quant, and release the marker once the query is gone: this page
+ // stays mounted, so a marker outliving the query made re-picking the same file a dead click.
if (!wanted) {
handledRouteModel.current = null;
return;
}
- // `quant` is used verbatim as a filename; a label there (a hand-built link, an older producer) is resolved instead.
- // The two fields, not the object: `routeSearch` is rebuilt every render, so it would churn the deps.
+ // `quant` is used verbatim as a filename, so a label there is resolved instead. The two
+ // fields, not the object: `routeSearch` is rebuilt every render.
const routed = { quant: routeSearch?.quant, ggufQuant: routeSearch?.ggufQuant };
const routedFilename = routedGgufFilename(routed);
const routedLabel = routedGgufLabel(routed);
@@ -2832,8 +2774,8 @@ export function ImagesPage({
// This arrival owns the page like a direct pick, so a download staged by an earlier one cannot land on top.
const token = pickGuard.claim();
void navigateSelf({ to: "/images", search: {}, replace: true });
- // A label means a GGUF repo whatever the catalog says, and is not loadable, so resolve it instead of routing it as a
- // filename.
+ // A label means a GGUF repo whatever the catalog says, and is not loadable, so resolve it
+ // rather than routing it as a filename.
if (routedLabel) {
// Deferred, not inline: resolution is a request, and the load it fires owns the state a direct pick sets.
void Promise.resolve().then(() =>
@@ -2853,7 +2795,7 @@ export function ImagesPage({
return;
}
// Match every direct picker branch: the routed intent owns both the visible build label and
- // the model-specific Default recipe, and a load that never becomes resident rolls both back.
+ // the Default recipe, and a load that never becomes resident rolls both back.
const revert: PickRevert = quantRevert.current ?? { prev: quant, steps, guidance };
quantRevert.current = revert;
setQuant(pick.opts.kind === "pipeline" ? null : (pick.opts.filename ?? null));
@@ -2885,13 +2827,10 @@ export function ImagesPage({
if (l) void handleLoad(l.repoId, { kind: l.kind, filename: l.filename });
}, [handleLoad]);
- // Every pick supersedes the one before it, whichever route it takes. A staged download outlives
- // its pick, and the direct-local branches call handleLoad rather than loadOrStage, so clearing
- // only inside loadOrStage left the old job's onReady free to load the abandoned model over the
- // one just chosen. Bumping the sequence here also invalidates any plan still in flight.
- // A pick that is rejected after beginPick() has already retired the staged pick it replaced, so
- // nothing will load and nothing else will restore the label. Hand the resident state back here or
- // the selector keeps showing the abandoned pick's quant and recipe for good.
+ // Every pick supersedes the one before it, whichever route it takes: a staged download
+ // outlives its pick, and the direct-local branches call handleLoad rather than loadOrStage,
+ // so clearing only inside loadOrStage left the old job free to load the abandoned model. A
+ // pick rejected after beginPick() has already retired its predecessor, so restore here.
const abandonPick = useCallback(() => {
if (quantRevert.current) {
revertPick(quantRevert.current);
@@ -2912,18 +2851,15 @@ export function ImagesPage({
// Ignore picks while a load/generation/unload is in flight: the backend rejects a second load with a 409.
if (busy !== null) return;
beginPick();
- // This pick owns the page now, so one still awaiting a listing or a plan drops out. Before any branch: staging never
- // sets `busy`, so any pick can land on an awaiting one.
+ // This pick owns the page now, so one still awaiting a listing or a plan drops out. Before any
+ // branch, since staging never sets `busy`.
const token = pickGuard.claim();
// Curated non-GGUF model: load as a full pipeline or single-file safetensors.
const spec = loadSpecFor(id, IMAGE_CATALOG);
if (spec && spec.kind !== "gguf") {
- // Carried forward when one is already pending: a superseded staged pick left its
- // optimistic quant and recipe in state, so snapshotting now would record THAT and
- // restore a model which never loaded. The live entry already holds the resident one.
- // Registers its own rollback like every other branch. Leaving the previous pick's entry in
- // place would let that older staged download, on cancelling, revert to state from before it
- // -- over a selection this pick already replaced -- and leave this one with no rollback.
+ // Carried forward when one is already pending: a superseded staged pick left its optimistic
+ // quant and recipe in state, so snapshotting now would record THAT and restore a model that
+ // never loaded. Leaving the old entry would also let that download revert this pick.
const revert: PickRevert = quantRevert.current ?? { prev: quant, steps, guidance };
quantRevert.current = revert;
setQuant(null);
@@ -2941,8 +2877,8 @@ export function ImagesPage({
});
return;
}
- // GGUF quant pick from the variant expander. Optimistic for instant picker feedback, but revert if the load fails to START
- // or LATER in the poll: the old pipeline stays loaded either way. The poll owns the after-start revert via quantRevert.
+ // GGUF quant pick from the variant expander. Optimistic for instant feedback, but reverted if
+ // the load fails to START or later in the poll: the old pipeline stays loaded either way.
if (meta.ggufVariant && meta.ggufFilename) {
const revert: PickRevert = quantRevert.current ?? { prev: quant, steps, guidance };
quantRevert.current = revert;
@@ -2969,8 +2905,8 @@ export function ImagesPage({
const filename = slash >= 0 ? norm.slice(slash + 1) : norm;
const dir = slash >= 0 ? norm.slice(0, slash) : ".";
if (!filename.toLowerCase().endsWith(".gguf")) {
- // A repo id or local directory, not a file. The listing names its .gguf and the label picks between siblings; a
- // local pick passes its directory so the listing reads that path, not a hub repo.
+ // A repo id or local directory, not a file: the listing names its .gguf and the label picks
+ // between siblings, and a local pick passes its directory so the listing reads that path.
void loadGgufRepoPick(
id,
meta.ggufVariant ?? null,
@@ -2979,8 +2915,8 @@ export function ImagesPage({
);
return;
}
- // A direct pick carries no curated variant label; surface the filename so the selector stops advertising the old quant.
- // Optimistic, reverted if the load fails to start OR later in the poll (mirrors the curated branch above).
+ // A direct pick carries no curated variant label, so surface the filename or the selector
+ // keeps advertising the old quant. Optimistic, reverted if the load never starts.
const revert: PickRevert = quantRevert.current ?? { prev: quant, steps, guidance };
quantRevert.current = revert;
setQuant(filename);
@@ -2993,8 +2929,8 @@ export function ImagesPage({
});
return;
}
- // A direct local single-file .safetensors pick must load via from_single_file: the pipeline route rejects a bare file, and
- // only after evicting the resident model. Split into (parent dir, basename) like the local GGUF branch above.
+ // A direct local single-file .safetensors pick must load via from_single_file: the pipeline
+ // route rejects a bare file, and only after evicting the resident model.
if (meta.source === "local" && id.toLowerCase().endsWith(".safetensors")) {
const norm = id.replace(/\\/g, "/");
const slash = norm.lastIndexOf("/");
@@ -3012,8 +2948,8 @@ export function ImagesPage({
});
return;
}
- // A GGUF repo with no filename: these used to fall through to the pipeline branch below, which the backend rejects
- // for a single-file GGUF repo.
+ // A GGUF repo with no filename: these used to fall through to the pipeline branch below, which
+ // the backend rejects for a single-file GGUF repo.
if (spec?.kind === "gguf" || meta.ggufVariant) {
// An artifact that names its file short-circuits the listing; otherwise the label is the hint.
void loadGgufRepoPick(
@@ -3056,7 +2992,8 @@ export function ImagesPage({
],
);
- // Deploy a freshly-trained adapter from the Train tab: switch to Create, load the base, and queue the adapter for the LoRA discovery effect.
+ // Deploy a freshly-trained adapter from the Train tab: switch to Create, load the base, and
+ // queue the adapter for the LoRA discovery effect.
const handleDeployAdapter = useCallback(
(args: { baseRepo: string; family: string; catalogPath: string; trigger: string }) => {
if (busy !== null) {
@@ -3094,21 +3031,19 @@ export function ImagesPage({
// Resolves true when the backend accepted the unload; handleCancelLoad reports the cancel only then.
const handleUnload = useCallback(async (): Promise<boolean> => {
- // Ejecting cancels any in-flight replacement load, so tear down its client-side tracking too, or the toast leaks forever.
+ // Ejecting cancels any in-flight replacement load, so tear down its client-side tracking too
+ // or the toast leaks forever.
dropResidentState();
loadTrackingRestored.current = false;
setBusy("unloading");
try {
setStatusIfNewest(++statusTicket.current, await unloadDiffusionModel());
setQuant(null);
- // Hold the page until any load start still in flight has run to its END, compensating
- // unload and all. The selector's eject routes straight here, so without the fence an eject
- // landing before the start registered returned success and cleared busy: the user picks
- // another model, the backend refuses it ("a load is already in progress") because the older
- // start won the race, and that older handler -- seeing the newer loadSeq -- skips its
- // compensating unload and returns without restarting its poll, leaving a multi-gigabyte
- // load running with no toast and no cancel control. Same fence handleCancelLoad and the
- // external-eject listener take.
+ // Hold the page until any load start still in flight has run to its END, compensating unload
+ // and all. Without the fence an eject landing before the start registered returned success
+ // and cleared busy, so the next pick was refused while the older load carried on.
+ // That older handler, seeing the newer loadSeq, skips its compensating unload and returns without
+ // restarting its poll, leaving a multi-gigabyte load running with no toast and no cancel control.
const pending = pendingStart.current;
if (pending) {
try {
@@ -3117,38 +3052,35 @@ export function ImagesPage({
// Its own handler reports the failure; this only waits for the window to close.
}
}
- // The wait above can end with the tracking RESTORED: handleLoad's compensating unload
- // failed, so the load is still running. This eject stopped nothing, so do not report
- // success -- the caller would toast "stopped loading" over a live load.
+ // The wait above can end with the tracking RESTORED: handleLoad's compensating unload failed,
+ // so the load is still running and this eject stopped nothing.
return !loadTrackingRestored.current;
} catch (err) {
toast.error(err instanceof Error ? err.message : "Failed to unload model");
void refreshStatus();
return false;
} finally {
- // Not an unconditional clear. A restore during the wait above put the page back to
- // "loading" deliberately; wiping it hides the toast's Cancel and the "Cancel load"
- // button and re-enables the picker over a load that is still running.
+ // Not an unconditional clear: a restore during the wait put the page back to "loading"
+ // deliberately, and wiping it hides the Cancel controls over a load still running.
setBusy((prev) => (prev === "unloading" ? null : prev));
}
}, [refreshStatus, dropResidentState]);
- // Cancelling a load IS the unload: it sets the running load's cancel event, bumps the load token so the
- // worker can never commit, and drops the load marker. What it leaves behind is only cache: bytes already
- // fetched stay in the HF cache, so loading the same model again resumes instead of restarting, and no
- // half-built pipeline survives (the worker's commit is token-gated and unload clears the GPU state).
+ // Cancelling a load IS the unload: it sets the load's cancel event, bumps the load token so
+ // the worker can never commit, and drops the load marker. What it leaves is only cache, so
+ // loading the same model again resumes instead of restarting.
const handleCancelLoad = useCallback(async () => {
const wasLoading = busy === "loading";
if (await handleUnload()) {
- // handleUnload holds the page for the whole pending-start path before it returns, so by
- // here the window the backend's "a load is already in progress" refusal lives in is shut.
+ // handleUnload holds the page for the whole pending-start path, so by here the window for the
+ // backend's "a load is already in progress" refusal is shut.
toast.info("Stopped loading the model", {
description: "Anything already downloaded stays cached, so loading it again resumes.",
});
return;
}
- // Already restored inside handleUnload (its own compensating-unload failure), so the toast
- // and the poll are up: a second restore would raise a duplicate toast and a second poll loop.
+ // Already restored inside handleUnload, so the toast and poll are up: a second restore would
+ // raise a duplicate toast and a second poll loop.
if (!wasLoading || loadTrackingRestored.current) return;
// The unload failed, so the load is still running and its tracking was torn down for nothing.
restoreLoadTracking();
@@ -3194,7 +3126,8 @@ export function ImagesPage({
return;
}
- // Resolve the conditioning image/mask/strength up front. Extend is built here by padding + masking, then sent through inpaint.
+ // Resolve the conditioning image/mask/strength up front. Extend is built here by padding and
+ // masking, then sent through inpaint.
let condInit: string | undefined;
let condMask: string | undefined;
let condStrength: number | undefined;
@@ -3214,12 +3147,14 @@ export function ImagesPage({
condMask = built.mask;
condStrength = 1; // the new border is blank canvas: redraw it fully
} else if (isUpscale) {
- // Hires fix: the backend enlarges the source and re-denoises at this low strength, gaining detail without changing content.
+ // Hires fix: the backend enlarges the source and re-denoises at this low strength, gaining
+ // detail without changing content.
condInit = initImage ?? undefined;
condUpscale = upscaleFactor;
condStrength = upscaleStrength;
} else if (isReference) {
- // FLUX.2 reference conditioning: send the primary + extra references. A fresh image is generated at the slider size.
+ // FLUX.2 reference conditioning: send the primary plus extra references. A fresh image is
+ // generated at the slider size.
condInit = initImage ?? undefined;
const extras = referenceImages.filter(Boolean);
if (extras.length) condRefImages = extras;
@@ -3244,16 +3179,16 @@ export function ImagesPage({
baseSeed = Math.floor(Math.random() * 2 ** 32);
}
- // Snap custom dims to the model's grid so a half-typed value can't 422.
+ // Snap custom dims to the model's grid so a half-typed value cannot 422.
const w = snapDim(width);
const h = snapDim(height);
- // A large run count is legitimate, so no upper cap: floor at 1 and ignore non-numeric input (the box can yield NaN).
+ // A large run count is legitimate, so no upper cap: floor at 1 and ignore non-numeric input.
const runs = Number.isFinite(count) && count >= 1 ? Math.floor(count) : 1;
if (runs !== count) setCount(runs);
- // An explicit seed near the 2**53-1 backend cap can overflow once the per-run and in-batch offsets are added, 422ing a later
- // run AFTER earlier images generated. Fail before any GPU work; subtraction keeps the comparison exact.
+ // An explicit seed near the 2**53-1 backend cap can overflow once the per-run and in-batch
+ // offsets are added, 422ing a later run after earlier images generated. Fail before GPU work.
if (baseSeed > Number.MAX_SAFE_INTEGER - (runs * batchSize - 1)) {
toast.error("Seed too large for this run count and batch size; use a smaller seed");
return;
@@ -3265,17 +3200,15 @@ export function ImagesPage({
// Fresh run: a Stop from the PREVIOUS run must not cancel this one.
cancelRequested.current = false;
cancelAcked.current = false;
- // Per-run, like the two above. A cancel POST still outstanding from the PREVIOUS run (a 401
- // refresh-and-replay is the slow case) would otherwise leave the guard set and swallow this
- // run's own Stop entirely.
+ // Per-run, like the two above: a cancel POST still outstanding from the PREVIOUS run would
+ // leave the guard set and swallow this run's own Stop.
cancelInFlight.current = null;
- // Any Stop still pending belongs to the run that just ended, so it must not reach the server
- // now that a new one is starting.
+ // Any Stop still pending belongs to the run that just ended, so it must not reach the server now.
cancelAbort.current?.abort();
cancelAbort.current = null;
runToken.current += 1;
- // Poll the backend's per-step progress across the whole run so the bar tracks live denoising steps. A named poll body
- // (guarded against overlap) also serves the visibilitychange listener, so a throttled tab catches up when visible.
+ // Poll the backend's per-step progress across the whole run. A named poll body also serves the
+ // visibilitychange listener, so a throttled tab catches up when visible.
let pollInFlight = false;
const pollGenerateOnce = async () => {
if (pollInFlight) return;
@@ -3301,14 +3234,13 @@ export function ImagesPage({
};
document.addEventListener("visibilitychange", genVisibilityListener.current);
genPollTimer.current = setInterval(() => void pollGenerateOnce(), 300);
- // Every gallery id this page has seen, captured BEFORE the first POST and grown as the run produces records:
- // settleLostGeneration proves a lost POST landed by finding a record outside this set, so it must not be rebuilt in the catch.
+ // Every gallery id this page has seen, captured BEFORE the first POST and grown as records
+ // arrive: settleLostGeneration proves a lost POST landed by finding a record outside it.
const knownIds = new Set(galleryCache.images.map((image) => image.id));
try {
for (let i = 0; i < runs; i++) {
- // Stop issuing more GPU generations once the page truly unmounted (a plain tab switch
- // keeps it mounted), or once Stop was pressed: the backend cancel only reaches the denoise
- // in flight, so the remaining runs of a count > 1 request would otherwise start anyway.
+ // Stop issuing more GPU generations once the page unmounted or Stop was pressed: the backend
+ // cancel only reaches the denoise in flight, so a count > 1 request would run on.
if (
!shouldContinueGenerating({
mounted: isMounted.current,
@@ -3316,13 +3248,9 @@ export function ImagesPage({
})
)
break;
- // Frozen BEFORE the POST goes out, because both halves of it have to describe the same
- // moment. `knownIds` is from before the request, so deriving the window half in the catch
- // mixes the two: scrolling while the request is in flight pages in historical unpinned
- // records, and a window that was all pinned (which refuses to judge) then looks like one
- // that can, with the newest historical row reading as proof the generation landed.
- // Sharing the `knownIds` set is deliberate: it only ever grows, and a larger set can only
- // make the probe more conservative.
+ // Frozen BEFORE the POST, because both halves must describe the same moment: `knownIds` is
+ // from before the request, so deriving the window half in the catch mixes the two and the
+ // newest historical row could read as proof the generation landed.
const probeBaseline = newRecordProbeBaseline(
galleryCache.images,
galleryCache.hasMore,
@@ -3338,17 +3266,19 @@ export function ImagesPage({
height: h,
steps,
guidance,
- // Offset runs by the batch size: the native engine seeds image j at seed+j, so a +1 offset would regenerate batch-mates.
+ // Offset runs by the batch size: the native engine seeds image j at seed+j, so a +1 offset
+ // would regenerate batch-mates.
seed: baseSeed + i * batchSize,
batch_size: batchSize,
- // Transform/Inpaint/Extend send the source image (+ mask) and a strength. The backend derives output size from the image.
+ // Transform/Inpaint/Extend send the source image (and mask) with a strength; the backend
+ // derives output size from the image.
init_image: condInit,
mask_image: condMask,
strength: condStrength,
upscale: condUpscale,
reference_images: condRefImages,
- // Drop empty and zero-weight rows and trim hand-typed repo ids, so the recipe records only adapters that applied.
- // Gate on loraCapable: a restore can leave adapters in state while the loaded model does not support LoRA.
+ // Drop empty and zero-weight rows and trim hand-typed repo ids, so the recipe records only
+ // adapters that applied. Gated on loraCapable, since a restore can leave adapters in state.
loras: (() => {
if (!loraCapable) return undefined;
const active = loras
@@ -3368,26 +3298,26 @@ export function ImagesPage({
: undefined,
});
} catch (err) {
- // The POST response was lost while the backend kept generating (the secure-mode tunnel caps the response near 100s).
- // Retrying would duplicate the work, so wait the run out and read its images off the gallery.
+ // The POST response was lost while the backend kept generating (the secure-mode tunnel caps
+ // the response near 100s). Retrying would duplicate the work, so wait the run out.
if (!(err instanceof GenerateResponseLostError)) throw err;
- // A record outside the baseline proves the request reached the backend. Taken per
- // attempt, so it reflects what the client could see when THIS post went out.
+ // A record outside the baseline proves the request reached the backend. Taken per attempt, so
+ // it reflects what the client could see when THIS post went out.
await settleLostGeneration(() => isMounted.current, probeBaseline);
if (!isMounted.current) break;
await loadGallery();
- // loadGallery refreshes the module cache synchronously, so this run's records are folded in before the next run.
+ // loadGallery refreshes the module cache synchronously, so this run's records are folded in
+ // before the next run.
galleryCache.images.forEach((image) => knownIds.add(image.id));
setGenDone(i + 1);
continue;
}
if (!isMounted.current) break;
// Merge this run's records and load their blobs. Sorted, not prepended: a new image is
- // unpinned, so the server puts it after the pinned group, and a bare prepend would show it
- // ahead of pins until the next reload.
+ // unpinned, so the server puts it after the pinned group.
stripEpoch.current += 1;
- // Deduplicated: a resync in flight can fetch the saved record first, and prepending it
- // again duplicates a React key and inflates the next page's offset, skipping a record.
+ // Deduplicated: a resync in flight can fetch the saved record first, and prepending it again
+ // duplicates a React key and inflates the next page's offset.
setImages((prev) => mergeGenerated(prev, res.images));
res.images.forEach((image) => knownIds.add(image.id));
if (res.images[0]) setSelectedId(res.images[0].id);
@@ -3396,12 +3326,9 @@ export function ImagesPage({
}
} catch (err) {
const msg = err instanceof Error ? err.message : "Image generation failed";
- // The user's own Stop comes back as the backend's cancelled sentinel (409). Not an error,
- // so do not toast it. Same treatment the video page gives its Cancel.
- // Only a Stop the backend confirmed it acted on explains an error away. If the POST never
- // landed, or the backend answered {cancelled: false} because the run was already past its
- // last cancellation check while the route was still persisting, the generation was not
- // stopped, so whatever it raised is a real failure rather than "the user stopped it".
+ // The user's own Stop comes back as the backend's cancelled sentinel (409), so it is not
+ // toasted. Only a Stop the backend confirmed explains an error away: a POST that never
+ // landed, or {cancelled: false}, means whatever it raised is a real failure.
if (
shouldReportGenerateError({
message: msg,
@@ -3417,12 +3344,10 @@ export function ImagesPage({
genVisibilityListener.current = null;
}
cancelRequested.current = false;
- // Refresh on EVERY exit, not just the successful one, and AWAIT it before Generate comes
- // back. A generation can change server-side status (Speed=Auto compiles on the 3rd LoRA-free
- // run and supports_lora flips false), and a cancelled native run can leave no model at all:
- // when the native cancel is not reflected within its grace window, sd-server is stopped
- // outright. Re-enabling Generate first would offer a button that 409s against a backend with
- // nothing loaded. Cheap status GET, so the wait is not felt.
+ // Refresh on EVERY exit, not just the successful one, and AWAIT it before Generate comes back:
+ // a generation can change server-side status and a cancelled native run can leave no model
+ // at all, so re-enabling first would offer a button that 409s.
+ // Speed=Auto compiles on the 3rd LoRA-free run and supports_lora flips false.
if (isMounted.current) await refreshStatus();
setBusy(null);
setGenDone(null);
@@ -3431,12 +3356,11 @@ export function ImagesPage({
}, [prompt, negativePrompt, width, height, steps, guidance, seed, batchSize, count, workflow, initImage, maskImage, strength, extendPct, extendSides, upscaleFactor, upscaleStrength, referenceImages, loras, loraCapable, controlnetCapable, controlnetId, controlImage, controlType, controlStrength, ensureSrc, loadGallery, refreshStatus]);
// Stop the in-flight generation. Latch FIRST, so a multi-run request stops even if the POST
- // races the run that is already finishing, then ask the backend to break out of the sampler.
+ // races the run that is already finishing.
const handleCancelGenerate = useCallback(async () => {
cancelRequested.current = true;
// One Stop on the wire at a time. The button stays enabled so the click still latches, but a
- // second POST would target whatever is active when IT arrives, which after the stopped run
- // settles can be a generation the user started since.
+ // second POST would target whatever is active when IT arrives.
const token = runToken.current;
if (cancelInFlight.current === token) return;
cancelInFlight.current = token;
@@ -3446,17 +3370,14 @@ export function ImagesPage({
const { cancelled } = await cancelDiffusionGeneration(abort.signal);
cancelAcked.current = Boolean(cancelled);
} catch {
- // An abort means the next run dropped this one on purpose; it belongs to a generation that
- // is already over, so there is nothing to report. Otherwise the request never landed, the
- // denoise in flight keeps running, and the click cannot be treated as handled: say so, and
- // stop it explaining away an error the run raises later.
+ // An abort means the next run dropped this one on purpose, so there is nothing to report.
+ // Otherwise the request never landed and the denoise runs on, so the click is not handled.
if (!abort.signal.aborted) {
cancelAcked.current = false;
toast.error("Could not reach the server to stop this generation; it is still running");
}
} finally {
- // Only if they are still ours: a slow cancel from an earlier run must not release the guard
- // a later run set, or a duplicate click gets through and can land on a generation after it.
+ // Only if they are still ours: a slow cancel from an earlier run must not release the guard a later run set.
if (cancelInFlight.current === token) cancelInFlight.current = null;
if (cancelAbort.current === abort) cancelAbort.current = null;
}
@@ -3506,7 +3427,8 @@ export function ImagesPage({
["max", "Max"],
]}
/>
- {/* The dense transformer_quant fast path only engages on the GGUF kind, so gate the control to GGUF (or nothing loaded) and otherwise show why it is unavailable. */}
+ {/* The dense transformer_quant fast path engages only on the GGUF kind, so gate the control to
+ GGUF (or nothing loaded) and otherwise say why it is unavailable. */}
{!status?.loaded || status.model_kind === "gguf" ? (
<AdvancedSelect
label="Precision"
@@ -3597,7 +3519,8 @@ export function ImagesPage({
<Switch checked={cpuOffload} onCheckedChange={setCpuOffload} />
</div>
<LoadedBuildSummary status={status} />
- {/* A resident full pipeline is reloadable by repo id alone, so it keeps Reapply even before a user-initiated load; GGUF/single_file residents hide the button. */}
+ {/* A resident full pipeline is reloadable by repo id alone, so it keeps Reapply even before a
+ user-initiated load; GGUF/single_file residents hide the button. */}
{status?.loaded && (canReapply || status?.model_kind === "pipeline") && (
<Tooltip>
<TooltipTrigger asChild={true}>
@@ -3618,11 +3541,11 @@ export function ImagesPage({
);
return (
- // The chat-style layout gives this page no outer top inset, so clear the custom
- // titlebar here (34px on win/linux, 0 under macOS's native one) as chat does.
+ // The chat-style layout gives this page no outer top inset, so clear the custom titlebar here as chat does.
+ // 34px on win/linux, 0 under macOS's native one.
<div className="diffusion-surface @container flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden pt-[var(--studio-content-top-inset,0px)]">
- {/* Keep the tabs centered over the preview region at every width. The model rail
- holds at 408px when space permits and shrinks only to preserve the controls. */}
+ {/* Keep the tabs centered over the preview at every width: the model rail holds at 408px when
+ space permits and shrinks only to preserve the controls. */}
<div className="pointer-events-none relative z-40 grid h-[48px] shrink-0 grid-cols-[minmax(0,408px)_minmax(13rem,1fr)]">
<div
className={cn(
@@ -3709,13 +3632,14 @@ export function ImagesPage({
</div>
</div>
</div>
- {/* Train mode: the full-page training workspace. Unmounted in Create mode so its polling stops; Create's own state is untouched. */}
+ {/* Train mode: the full-page training workspace. Unmounted in Create mode so its polling stops. */}
{pageMode === "train" ? (
<DiffusionTrainPanel
active={active && pageMode === "train"}
loadedFamily={status?.family ?? null}
loadedBaseRepo={
- // Prefer base_repo (the full diffusers pipeline) over repo_id: for a GGUF load repo_id is a checkpoint path, not a trainable base.
+ // Prefer base_repo (the full diffusers pipeline) over repo_id: for a GGUF load repo_id is a
+ // checkpoint path, not a trainable base.
status?.base_repo ?? status?.repo_id ?? null
}
onTrainingComplete={() => setLoraRefreshKey((k) => k + 1)}
@@ -3727,9 +3651,8 @@ export function ImagesPage({
onFamiliesChange={setTrainFamilies}
/>
) : (
- /* Settings column + preview canvas. Structural borders stay edge-to-edge;
- spacing belongs inside each pane. The same 50rem page-container breakpoint
- drives this body and the header above, regardless of sidebar width. */
+ /* Settings column + preview canvas. Structural borders stay edge-to-edge; spacing belongs inside each pane.
+ The same 50rem page-container breakpoint drives this body and the header above. */
<div className="flex min-h-0 w-full min-w-0 flex-1 flex-col overflow-y-auto overflow-x-hidden @[50rem]:flex-row @[50rem]:overflow-hidden">
<div className="flex w-full shrink-0 flex-col border-b border-border/60 @[50rem]:w-[408px] @[50rem]:overflow-hidden @[50rem]:border-r @[50rem]:border-b-0">
{/* pl-0.5 keeps focus rings off the scroll container's edge. */}
@@ -3742,8 +3665,7 @@ export function ImagesPage({
)}
>
{/* The sidebar submenu is the switcher, so name the active workflow over its controls. */}
- {/* Icon rides the heading; the line below runs the full width.
- Same shape on the Video page, so the two stay level. */}
+ {/* Icon rides the heading; the line below runs the full width. Same shape on the Video page. */}
<div className="mb-2 flex items-start justify-between gap-3">
<div className="min-w-0 grid gap-1.5">
<h2 className="flex items-center gap-2 font-heading text-xl font-medium leading-none text-foreground">
@@ -3898,8 +3820,8 @@ export function ImagesPage({
aria-pressed={on}
onClick={() => setExtendSides((s) => ({ ...s, [key]: !s[key] }))}
className={cn(
- // No border in either mode: the fill alone marks the state. A ring
- // would not survive mouse focus anyway (index.css blanks it).
+ // No border in either mode: the fill alone marks the state, and a ring would not survive
+ // mouse focus anyway.
"rounded-lg px-2 py-1.5 text-xs font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
on
? "bg-primary/15 text-foreground hover:bg-primary/20 dark:bg-primary/25 dark:hover:bg-primary/30"
@@ -3962,7 +3884,8 @@ export function ImagesPage({
<ImageDropzone
value={img}
onChange={(v) =>
- // Keep the slot in place (empty string when cleared) so other slots do not renumber mid-edit; empty slots are dropped at send time.
+ // Keep the slot in place (empty string when cleared) so other slots do not renumber mid-edit;
+ // empty slots are dropped at send time.
setReferenceImages((prev) =>
prev.map((p, j) => (j === i ? (v ?? "") : p)),
)
@@ -4023,7 +3946,7 @@ export function ImagesPage({
onOpenChange={setNegativeOpen}
hint="What to steer the image away from. Only used when guidance is above 0."
/>
- {/* LoRA adapters: shown whenever the loaded model + quant can apply them. Type a Hugging Face repo id or pick a discovered adapter; each carries a 0-2 weight. */}
+ {/* LoRA adapters: shown whenever the loaded model + quant can apply them. Each carries a 0-2 weight. */}
{loraCapable && (
<Field
label="LoRAs"
@@ -4041,11 +3964,9 @@ export function ImagesPage({
)}
{loras.map((sel, i) => (
<div
- // Key on the index, not sel.id: sel.id is the value of the editable repo-id
- // Input below, so keying on it changes the key on the first character typed,
- // which remounts the row and drops input focus. The list is index-addressed
- // (every mutation matches j === i) and rows are removed explicitly, so the
- // index is stable for as long as the row lives.
+ // Key on the index, not sel.id: sel.id is the editable repo-id Input's value, so keying on it
+ // remounts the row and drops focus on the first character typed. The list is
+ // index-addressed and rows are removed explicitly.
key={i}
className="space-y-1.5 rounded-lg border border-border bg-muted/30 p-2"
>
@@ -4110,7 +4031,7 @@ export function ImagesPage({
</div>
</Field>
)}
- {/* ControlNet: shown when the model supports it, one is discoverable, and the txt2img workflow is active (v1 conditions txt2img only). */}
+ {/* ControlNet: shown when the model supports it, one is discoverable, and txt2img is active. */}
{controlnetCapable && availableControlNets.length > 0 && workflow === "create" && (
<Field
label="ControlNet"
@@ -4213,8 +4134,8 @@ export function ImagesPage({
<Field
label="Resolution"
hint={
- // Image-conditioned workflows size from the source, so "this is the output size"
- // is wrong there: Transform caps the source by this box, the rest ignore it.
+ // Image-conditioned workflows size from the source, so "this is the output size" is wrong
+ // there: Transform caps the source by this box, the rest ignore it.
workflow === "transform"
? "Caps the output size. The source image is scaled down to fit inside this box, keeping its aspect ratio, so the result may be smaller than the values shown."
: workflow === "inpaint" ||
@@ -4305,9 +4226,8 @@ export function ImagesPage({
{/* The scroll mask provides the fade; leave the footer unpainted to avoid dark-mode banding. */}
<div className="relative z-10 flex shrink-0 justify-center px-10 pt-0.5 pb-4">
{busy === "generating" ? (
- /* Replaces Generate while a run is in flight, mirroring the video page. Every workflow
- (Create, Transform, Inpaint, Extend, Upscale, Reference, Edit) funnels through the
- same handler, so one control stops all of them. */
+ /* Replaces Generate while a run is in flight. Every workflow funnels through the same
+ handler, so one control stops all of them. */
<Button
className="relative z-10 h-11 px-8 hover:bg-muted dark:hover:bg-muted"
variant="outline"
@@ -4337,7 +4257,8 @@ export function ImagesPage({
alt={selected.prompt}
className="max-h-full max-w-full object-contain shadow-sm"
/>
- {/* Actions grouped in one glass toolbar so they stay legible over any image. Size/seed live in the Recipe popover. */}
+ {/* Actions grouped in one glass toolbar so they stay legible over any image. Size and seed
+ live in the Recipe popover. */}
<div className="absolute bottom-4 right-4 flex items-center gap-0.5 rounded-xl bg-background/80 p-1 shadow-lg ring-1 ring-border backdrop-blur">
<RecipePopover image={selected} onRestore={restoreSettings} active={active} />
<DropdownMenu>
@@ -4379,7 +4300,7 @@ export function ImagesPage({
</div>
</>
) : selected ? (
- // The selected record's blob is still loading — spin in place.
+ // The selected record's blob is still loading; spin in place.
<div className="flex flex-col items-center gap-3 text-muted-foreground">
<Spinner className="size-8" />
<p className="text-sm">Loading…</p>
@@ -4433,15 +4354,16 @@ export function ImagesPage({
if (el.scrollWidth - el.scrollLeft - el.clientWidth < 400) void loadMore();
}}
>
- {/* In-progress generation: a placeholder tile at the front so past images stay visible and browsable while the new one renders. */}
+ {/* In-progress generation: a placeholder tile at the front so past images stay browsable while
+ the new one renders. */}
{busy === "generating" && (
<div className="flex size-16 shrink-0 animate-pulse items-center justify-center rounded-lg bg-muted/50 ring-2 ring-primary/30">
<Spinner className="size-5 text-muted-foreground" />
</div>
)}
- {/* The tile is a wrapper, not a button: the actions menu has to be the select
- button's SIBLING, since a button inside a button is invalid and would swallow
- its own clicks. data-image-id rides the wrapper so the observer still sees it. */}
+ {/* The tile is a wrapper, not a button: the actions menu must be the select button's SIBLING,
+ since a button inside a button is invalid. data-image-id rides the wrapper so the
+ observer still sees it. */}
{images.map((image) => (
<div
key={image.id}
@@ -4464,7 +4386,7 @@ export function ImagesPage({
<Spinner className="size-4 text-muted-foreground" />
</span>
)}
- {/* Selection marker on a non-focusable overlay, so the button own focus state can never mask it. */}
+ {/* Selection marker on a non-focusable overlay, so the button's focus state cannot mask it. */}
{image.id === selected?.id && (
<span className="pointer-events-none absolute inset-0 rounded-[10px] border border-border bg-white/35 dark:border-white/25 dark:bg-white/20" />
)}
diff --git a/studio/frontend/src/features/images/lib/generation-stop.ts b/studio/frontend/src/features/images/lib/generation-stop.ts
index f88cccee7af..c50c96257a0 100644
--- a/studio/frontend/src/features/images/lib/generation-stop.ts
+++ b/studio/frontend/src/features/images/lib/generation-stop.ts
@@ -1,11 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/** Stop semantics for an in-flight image generation, kept out of the page so they are testable.
- *
- * A generate request is a LOOP of `count` backend calls, but the backend cancel only reaches the
- * denoise running right now. Stopping therefore has two halves: ask the backend to break out of
- * the sampler, and stop the page issuing the runs that have not started yet. */
+/** Stop semantics for an in-flight image generation, kept out of the page so they are testable. A
+ * generate request is a LOOP of `count` backend calls, but the backend cancel only reaches the
+ * denoise running right now. Stopping therefore has two halves: ask the backend to break out of
+ * the sampler, and stop the page issuing the runs that have not started yet. */
/** Whether the run loop should issue another backend generation. */
export function shouldContinueGenerating(input: {
@@ -19,11 +18,9 @@ export function shouldContinueGenerating(input: {
export const GENERATION_CANCELLED_SENTINEL =
"Diffusion generation was cancelled.";
-/** Whether a failed generation should be toasted as an error.
- *
- * A user's own Stop comes back as the cancelled sentinel on a 409, which is the requested
- * outcome, not a failure. The `stopRequested` latch covers the case where the message never
- * reaches the page verbatim (a proxy rewrites it, or the run unwinds some other way). */
+/** Whether a failed generation should be toasted as an error. A user's own Stop comes back as the
+ * cancelled sentinel on a 409, which is the requested outcome. The `stopRequested` latch covers
+ * the case where the message never reaches the page verbatim. */
export function shouldReportGenerateError(input: {
message: string;
stopRequested: boolean;
diff --git a/studio/frontend/src/features/images/stores/image-workflow-store.ts b/studio/frontend/src/features/images/stores/image-workflow-store.ts
index ceb2f6d995e..ee91caf0f55 100644
--- a/studio/frontend/src/features/images/stores/image-workflow-store.ts
+++ b/studio/frontend/src/features/images/stores/image-workflow-store.ts
@@ -4,11 +4,9 @@
import { create } from "zustand";
import type { WorkflowId } from "../workflows";
-/**
- * The Images page's active workflow, lifted out of the page so the sidebar submenu can drive it.
- * `supported` is what the loaded model can do, published by the page; null means nothing is
- * loaded, in which case every workflow stays selectable.
- */
+/** The Images page's active workflow, lifted out of the page so the sidebar submenu can drive it.
+ * `supported` is what the loaded model can do, published by the page; null means nothing is
+ * loaded, in which case every workflow stays selectable. */
interface ImageWorkflowState {
workflow: WorkflowId;
pageMode: "create" | "train";
diff --git a/studio/frontend/src/features/images/train/dataset-files.ts b/studio/frontend/src/features/images/train/dataset-files.ts
index ac931c4727d..8a19b866bd1 100644
--- a/studio/frontend/src/features/images/train/dataset-files.ts
+++ b/studio/frontend/src/features/images/train/dataset-files.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// mirrors _DIFFUSION_DATASET_IMAGE_EXTS, _DIFFUSION_DATASET_CLIP_EXTS and
-// _DIFFUSION_DATASET_TEXT_EXTS in backend/routes/training.py.
+// Mirrors _DIFFUSION_DATASET_IMAGE_EXTS, _CLIP_EXTS and _TEXT_EXTS in
+// backend/routes/training.py.
export const DATASET_IMAGE_EXTS = [".png", ".jpg", ".jpeg", ".webp", ".bmp"];
// video containers, for the families that train from clips rather than stills.
export const DATASET_CLIP_EXTS = [".mp4", ".mov", ".mkv", ".webm", ".m4v", ".avi"];
@@ -16,14 +16,14 @@ const ACCEPTED = new Set([...DATASET_MEDIA_EXTS, ...DATASET_TEXT_EXTS]);
// a caption jsonl runs to kilobytes, so scan a prefix rather than decoding the whole upload.
const METADATA_SCAN_BYTES = 1024 * 1024;
-// starlette caps a multipart body at 1000 file parts and fastapi does not raise it, so a larger
-// selection is sent in slices; the endpoint accumulates repeat uploads into the same folder.
+// starlette caps a multipart body at 1000 file parts and fastapi does not raise it, so a
+// larger selection is sent in slices; the endpoint accumulates uploads into one folder.
export const DATASET_UPLOAD_CHUNK = 500;
-/** slice `files` so no request exceeds the part cap or `maxBytes`, keeping casefold-equal
- * names together and sending those groups first: the backend can only compare names inside
- * one request, and splitting a group is exactly what lets the second request overwrite the
- * first. Such a group ships whole even over `maxBytes`; `oversizedChunk` catches that. */
+/** Slice `files` so no request exceeds the part cap or `maxBytes`, keeping casefold-equal names
+ * together and sending those groups first: the backend can only compare names inside one
+ * request, and splitting a group lets the second request overwrite the first. Such a group
+ * ships whole even over `maxBytes`; `oversizedChunk` catches that. */
export function chunkDatasetUpload(files: File[], maxBytes: number): File[][] {
const groups = new Map<string, File[]>();
for (const file of files) {
@@ -32,9 +32,9 @@ export function chunkDatasetUpload(files: File[], maxBytes: number): File[][] {
if (group) group.push(file);
else groups.set(key, [file]);
}
- // a group of more than one is a case-variant set, which the backend refuses outright on a
- // case-insensitive dataset folder. Send those first so that refusal lands while there is
- // still nothing committed, rather than behind slices that have already been written.
+ // A group of more than one is a case-variant set, which the backend refuses outright on a
+ // case-insensitive dataset folder. Send those first so the refusal lands while nothing is
+ // committed, rather than behind slices already written.
const all = [...groups.values()];
const chunks: File[][] = [];
let current: File[] = [];
@@ -55,9 +55,8 @@ export function chunkDatasetUpload(files: File[], maxBytes: number): File[][] {
return chunks;
}
-/** the destination name in the first chunk no split can fit under `maxBytes`, or null.
- *
- * uploads accumulate, so a chunk the endpoint 413s leaves every chunk before it on disk. */
+/** The destination name in the first chunk no split can fit under `maxBytes`, or null. Uploads
+ * accumulate, so a chunk the endpoint 413s leaves every chunk before it on disk. */
export function oversizedChunk(chunks: File[][], maxBytes: number): string | null {
for (const chunk of chunks) {
if (chunk.reduce((sum, f) => sum + f.size, 0) > maxBytes) return destinationName(chunk[0]);
@@ -65,8 +64,8 @@ export function oversizedChunk(chunks: File[][], maxBytes: number): string | nul
return null;
}
-/** the first metadata file whose captions are keyed on a subfolder path, or null. a folder pick
- * flattens the tree, so rows keyed "images/001.png" silently resolve to no caption at all. */
+/** The first metadata file whose captions are keyed on a subfolder path, or null. A folder pick
+ * flattens the tree, so rows keyed "images/001.png" silently resolve to no caption. */
export async function metadataKeyedOnSubfolders(files: File[]): Promise<string | null> {
for (const file of files) {
if (!file.name.toLowerCase().endsWith(".jsonl")) continue;
@@ -96,10 +95,10 @@ export async function metadataKeyedOnSubfolders(files: File[]): Promise<string |
return null;
}
-// str.strip() is not trim(): Python keeps U+FEFF and strips U+001C-001F and U+0085. trim() read
-// a name ending in U+FEFF as an accepted "cat.png", but the multipart part carries the name
-// whole, so the endpoint saw a suffix with the U+FEFF still on it and refused the slice.
-// This class is exactly str.isspace() over the BMP; NUL is stripped separately, as it is there.
+// str.strip() is not trim(): Python keeps U+FEFF and strips U+001C-001F and U+0085. trim()
+// read a name ending in U+FEFF as an accepted "cat.png", but the multipart part carries the
+// name whole, so the endpoint refused the slice. This class is exactly str.isspace() over
+// the BMP; NUL is stripped separately, as it is there.
const PY_SPACE_CLASS =
"\\t\\n\\v\\f\\r\\u001c-\\u001f\\u0020\\u0085\\u00a0\\u1680" +
"\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
@@ -134,9 +133,9 @@ function inHiddenPath(file: File): boolean {
return relative ? relative.split("/").slice(1).some(isHidden) : false;
}
-/** whether two item names resolve to one `<stem>.txt` sidecar, as `_shares_sidecar` does:
- * the stems clash on an exact match, so only an extension-case pair of one spelling is exempt.
- * images and clips are compared together, since the sidecar is keyed on the stem alone. */
+/** Whether two item names resolve to one `<stem>.txt` sidecar, as `_shares_sidecar` does: the
+ * stems clash on an exact match, so only an extension-case pair of one spelling is exempt.
+ * Images and clips are compared together, since the sidecar keys on the stem alone. */
function sharesSidecar(other: string, name: string): boolean {
const otherExt = extensionOf(other);
if (other === name || !DATASET_MEDIA_EXTS.includes(otherExt)) return false;
@@ -184,8 +183,8 @@ export function selectDatasetFiles(input: File[]): DatasetFileSelection {
skipped += 1;
continue;
}
- // a dataset folder is flat, so two tree paths sharing a basename are one destination.
- // matched exactly: only the backend knows whether the dataset filesystem folds case.
+ // A dataset folder is flat, so two tree paths sharing a basename are one destination. Matched
+ // exactly: only the backend knows whether the dataset filesystem folds case.
const path = displayPath(file);
const previous = seen.get(dest);
if (previous !== undefined) {
@@ -194,8 +193,8 @@ export function selectDatasetFiles(input: File[]): DatasetFileSelection {
}
const isImage = DATASET_IMAGE_EXTS.includes(ext);
const isClip = DATASET_CLIP_EXTS.includes(ext);
- // two items sharing a stem resolve to one <stem>.txt caption, which the backend refuses.
- // every accepted variant is kept and compared, since the exemption is not transitive.
+ // Two items sharing a stem resolve to one <stem>.txt caption, which the backend refuses. Every
+ // accepted variant is kept and compared, since the exemption is not transitive.
if (isImage || isClip) {
const key = dest.slice(0, dest.length - ext.length).toLowerCase();
const variants = mediaStems.get(key) ?? [];
@@ -217,8 +216,8 @@ export function selectDatasetFiles(input: File[]): DatasetFileSelection {
return { files, imageCount, clipCount, captionCount, skipped, collisions };
}
-/** the first selected item the dataset folder already holds under a sidecar-sharing name, or
- * null. the backend compares each upload against the folder, so on a chunked top-up that 400
+/** The first selected item the dataset folder already holds under a sidecar-sharing name, or
+ * null. The backend compares each upload against the folder, so on a chunked top-up that 400
* lands with the slices before it already written. `existing` is the folder's item names. */
export function existingStemClash(files: File[], existing: string[]): DatasetCollision | null {
for (const file of files) {
diff --git a/studio/frontend/src/features/images/train/dataset-labeling-grid.tsx b/studio/frontend/src/features/images/train/dataset-labeling-grid.tsx
index 6eb0d6d057f..53ea21a9147 100644
--- a/studio/frontend/src/features/images/train/dataset-labeling-grid.tsx
+++ b/studio/frontend/src/features/images/train/dataset-labeling-grid.tsx
@@ -37,8 +37,9 @@ import {
setDiffusionDatasetCaption,
} from "../api";
-// One tile: an auth-fetched thumbnail (object URL, revoked on unmount) plus a caption Textarea saved on blur.
-// Uncaptioned tiles get a highlighted ring so a user labeling a small set sees what still needs a caption.
+// One tile: an auth-fetched thumbnail (object URL, revoked on unmount) plus a caption Textarea
+// saved on blur. Uncaptioned tiles get a highlighted ring so a user labeling a small set sees
+// what still needs a caption.
function LabelTile({
dataset,
record,
@@ -63,7 +64,8 @@ function LabelTile({
let url: string | null = null;
let cancelled = false;
fetchGalleryObjectUrl(diffusionDatasetImageUrl(dataset, record.filename, 256))
- // The fetch returns the blob's size alongside the URL for the gallery's byte budget; a dataset thumbnail only needs the URL.
+ // The fetch returns the blob's size alongside the URL for the gallery's byte budget; a dataset
+ // thumbnail only needs the URL.
.then(({ url: u }) => {
if (cancelled) {
URL.revokeObjectURL(u);
@@ -175,7 +177,8 @@ function LabelTile({
);
}
-// A responsive grid over a dataset folder's images with per-image caption editing. Fetches the list on open and whenever `refreshKey` changes.
+// A responsive grid over a dataset folder's images with per-image caption editing. Fetches the
+// list on open and whenever `refreshKey` changes.
export function DatasetLabelingGrid({
dataset,
refreshKey = 0,
@@ -285,7 +288,8 @@ export function DatasetLabelingGrid({
)}
</div>
{/* Two columns at any width: the column is fixed, so viewport breakpoints do not apply. */}
- {/* auto-rows-min: past max-h the height is definite and auto rows split it, flattening every tile until its thumbnail collapses. */}
+ {/* auto-rows-min: past max-h the height is definite and auto rows split it, flattening every
+ tile until its thumbnail collapses. */}
<div className="hover-scrollbar grid max-h-[420px] auto-rows-min grid-cols-2 gap-2.5 overflow-y-auto pb-0.5 pr-1">
{pageRecords.map((r) => (
<LabelTile
diff --git a/studio/frontend/src/features/images/train/dataset-showcase.tsx b/studio/frontend/src/features/images/train/dataset-showcase.tsx
index 60026c588b2..45d6bcb71c2 100644
--- a/studio/frontend/src/features/images/train/dataset-showcase.tsx
+++ b/studio/frontend/src/features/images/train/dataset-showcase.tsx
@@ -25,8 +25,8 @@ import {
// Rows of 5 in the settings column, so this fills two and a bit before "+N more".
const MAX_TILES = 12;
-// A single thumbnail tile: auth-fetches its object URL and revokes it on unmount.
-// The tile opens the labeling grid; its own corner button removes the image.
+// A single thumbnail tile: auth-fetches its object URL and revokes it on unmount. The tile opens
+// the labeling grid; its own corner button removes the image.
function ShowcaseTile({
dataset,
filename,
@@ -45,7 +45,8 @@ function ShowcaseTile({
let obj: string | null = null;
let cancelled = false;
fetchGalleryObjectUrl(diffusionDatasetImageUrl(dataset, filename, 256))
- // The fetch returns the blob's size alongside the URL for the gallery's byte budget; a single tile only needs the URL.
+ // The fetch returns the blob's size alongside the URL for the gallery's byte budget; a single
+ // tile only needs the URL.
.then(({ url: u }) => {
if (cancelled) {
URL.revokeObjectURL(u);
@@ -120,8 +121,8 @@ function ShowcaseTile({
);
}
-// A compact preview of a dataset's images: up to MAX_TILES sampled thumbnails plus a "+N more" tile that opens the full
-// labeling grid. Refreshes on selection or `refreshKey` change.
+// A compact preview of a dataset's images: up to MAX_TILES sampled thumbnails plus a "+N more"
+// tile that opens the full labeling grid. Refreshes on selection or `refreshKey` change.
export function DatasetShowcase({
dataset,
imageCount,
@@ -144,8 +145,8 @@ export function DatasetShowcase({
listDiffusionDatasetImages(dataset)
.then((r) => {
if (cancelled) return;
- // Sample up to MAX_TILES evenly across the folder so the strip represents the whole set, not just the first few files.
- // clips have no thumbnail endpoint, so the strip shows images only.
+ // Sample up to MAX_TILES evenly across the folder so the strip represents the whole set, not
+ // just the first few files. Clips have no thumbnail endpoint, so the strip shows images only.
const all = imageRecordsOnly(r.images).map((im) => im.filename);
if (all.length <= MAX_TILES) {
setNames(all);
diff --git a/studio/frontend/src/features/images/train/diffusion-charts.tsx b/studio/frontend/src/features/images/train/diffusion-charts.tsx
index ace0b6246d7..ec1ad3d2206 100644
--- a/studio/frontend/src/features/images/train/diffusion-charts.tsx
+++ b/studio/frontend/src/features/images/train/diffusion-charts.tsx
@@ -4,7 +4,8 @@
import { type ReactElement, useMemo } from "react";
import type { TrainingSeriesPoint } from "@/features/training";
-// The loss + grad-norm cards are pure presentational (props only), so reuse them directly, not ChartsSection/ChartsContent (those also render LR and Eval Loss).
+// The loss and grad-norm cards are pure presentational (props only), so reuse them directly, not
+// ChartsSection/ChartsContent, which also render LR and Eval Loss.
// eslint-disable-next-line no-restricted-imports
import { GradNormChartCard } from "@/features/studio/sections/charts/grad-norm-chart-card";
// eslint-disable-next-line no-restricted-imports
@@ -18,8 +19,9 @@ import {
ema,
} from "@/features/studio/sections/charts/utils";
-// Fixed presentation defaults (the LLM tab exposes these via a settings sheet): EMA smoothing on, linear scale, raw +
-// smoothed + average lines, no outlier trimming (diffusion loss is naturally noisy, not spiky-with-outliers).
+// Fixed presentation defaults (the LLM tab exposes these via a settings sheet): EMA smoothing on,
+// linear scale, raw plus smoothed plus average lines, no outlier trimming, since diffusion loss
+// is naturally noisy rather than spiky-with-outliers.
const SMOOTHING = 0.8;
function toLossItems(series: TrainingSeriesPoint[]): { step: number; loss: number }[] {
@@ -38,8 +40,9 @@ function fullStepDomain(steps: number[]): [number, number] {
return [min, max];
}
-// A diffusion-only metrics view: Training Loss and Grad Norm side by side, with a note under the loss card explaining why
-// per-step loss looks noisy. Always renders both cards so the parent decides when to mount them.
+// A diffusion-only metrics view: Training Loss and Grad Norm side by side, with a note under the
+// loss card explaining why per-step loss looks noisy. Always renders both cards so the parent
+// decides when to mount them.
export function DiffusionCharts({
lossHistory,
gradNormHistory,
diff --git a/studio/frontend/src/features/images/train/diffusion-train-deploy.ts b/studio/frontend/src/features/images/train/diffusion-train-deploy.ts
index d2addad94d5..c1b492fa8a7 100644
--- a/studio/frontend/src/features/images/train/diffusion-train-deploy.ts
+++ b/studio/frontend/src/features/images/train/diffusion-train-deploy.ts
@@ -28,18 +28,12 @@ export function resolveDiffusionDeployBase(
return trainedBase;
}
-/**
- * The TRAINING base paired with a checkpoint that is currently loaded for inference, or null.
- *
- * The inverse of the mapping above, and it is what the Train panel needs to preselect: the
- * distilled variants a user generates with are not trainable, so a loaded `...klein-9B` never
- * appears in `base_repos` and an exact-match preselect silently falls through to the FIRST entry.
- * For FLUX.2 Klein that is the 4B base, so opening Train with the 9B model loaded started a 9B
- * workflow from 4B weights unless the user noticed the selector.
- *
- * Only a pairing the family actually declares is returned, and only when the paired training repo
- * is offered, so this can never invent a base the backend would refuse.
- */
+/** The TRAINING base paired with a checkpoint currently loaded for inference, or null. The inverse
+ * of the mapping above, and what the Train panel needs to preselect: the distilled variants a
+ * user generates with are not trainable, so a loaded `...klein-9B` never appears in `base_repos`
+ * and an exact-match preselect falls through to the FIRST entry, which for FLUX.2 Klein is the
+ * 4B base. Only a pairing the family declares is returned, and only when the paired training
+ * repo is offered, so this can never invent a base the backend would refuse. */
export function resolveDiffusionTrainingBase(
family: DiffusionTrainableFamily | undefined,
loadedBase: string,
@@ -56,10 +50,9 @@ export function resolveDiffusionTrainingBase(
);
if (exact) return exact;
// A checkpoint loaded from the ungated MIRROR pairs with the mirror training id, and
- // /diffusion/info offers only the vendor ids, so the exact match above finds nothing and the
- // panel falls back to the first base -- for Klein the 4B, which is the very mix-up this
- // function exists to prevent, reached through Deploy's own mirror pairing. A mirror keeps the
- // upstream repo NAME, so fold to that; still only ever returning a base the backend offers.
+ // /diffusion/info offers only the vendor ids, so the exact match finds nothing and the panel
+ // falls back to the first base -- for Klein the 4B, the very mix-up this function prevents. A
+ // mirror keeps the upstream repo NAME, so fold to that, still only returning an offered base.
const name = normalizedRepo(trainingRepo.split("/").pop() ?? "");
if (!name) return null;
return (
diff --git a/studio/frontend/src/features/images/train/diffusion-train-lr-schedule.ts b/studio/frontend/src/features/images/train/diffusion-train-lr-schedule.ts
index 6505eb1abf3..8ca613324a7 100644
--- a/studio/frontend/src/features/images/train/diffusion-train-lr-schedule.ts
+++ b/studio/frontend/src/features/images/train/diffusion-train-lr-schedule.ts
@@ -4,8 +4,8 @@
import type { DiffusionTrainableFamily } from "../api";
/** The LR schedules the Train panel offers. The backend accepts more (cosine_with_restarts,
- * polynomial), so a family naming one of those is dropped rather than seeded into a Select that
- * cannot show it. */
+ * polynomial), so a family naming one of those is dropped rather than seeded into a Select that
+ * cannot show it. */
export type LrScheduler = "constant" | "constant_with_warmup" | "cosine" | "linear";
export const LR_SCHEDULERS: readonly LrScheduler[] = [
@@ -16,11 +16,9 @@ export const LR_SCHEDULERS: readonly LrScheduler[] = [
];
/** A family's LR ramp, as one unit. The backend pairs `lr_scheduler` with `lr_warmup_steps`
- * because diffusers' `get_scheduler` returns before it reads `num_warmup_steps` under
- * "constant", so a warmup count on its own ramps nothing. Half a pair is therefore dropped:
- * a schedule with no ramp, or a ramp its schedule ignores, is exactly what the pairing exists to
- * prevent. Returns {} for a family that recommends no ramp, so a spread of it leaves the fields
- * undefined rather than null. */
+ * because diffusers' `get_scheduler` returns before it reads `num_warmup_steps` under
+ * "constant", so a warmup count on its own ramps nothing. Half a pair is therefore dropped.
+ * Returns {} for a family that recommends no ramp, so a spread leaves the fields undefined. */
export function lrSchedulePreset(
defaults: DiffusionTrainableFamily["defaults"],
): { lrScheduler: LrScheduler; lrWarmupSteps: number } | Record<string, never> {
diff --git a/studio/frontend/src/features/images/train/diffusion-train-panel.tsx b/studio/frontend/src/features/images/train/diffusion-train-panel.tsx
index f2edf35f797..c6303966cfc 100644
--- a/studio/frontend/src/features/images/train/diffusion-train-panel.tsx
+++ b/studio/frontend/src/features/images/train/diffusion-train-panel.tsx
@@ -108,7 +108,7 @@ import {
import { resolveDiffusionTrainingFacts } from "./diffusion-train-family-facts";
import { type LrScheduler, lrSchedulePreset } from "./diffusion-train-lr-schedule";
-// The families the Train tab can train, in popularity order; a fallback for an older backend whose /info reports none.
+// Families the Train tab can train, in popularity order; a fallback for an older backend whose /info reports none.
type FamilyPreset = {
name: string;
label: string;
@@ -117,10 +117,8 @@ type FamilyPreset = {
rank: number;
lr: number;
resolution: number;
- // The family's LR ramp, when it recommends one. Both or neither: a warmup count is inert
- // under "constant", so seeding one without the other reinstates the bug this carries.
- // Only the backend reports them, so the static presets below leave the pair unset rather
- // than keeping a second copy of a pairing that can drift.
+ // The family's LR ramp: both or neither, since a warmup count is inert under "constant". Only
+ // the backend reports them, so the static presets leave the pair unset.
lrScheduler?: LrScheduler;
lrWarmupSteps?: number;
};
@@ -181,7 +179,8 @@ const FAMILY_PRESETS: FamilyPreset[] = [
const CUSTOM_BASE = "__custom__";
const UPLOAD_DATASET = "__upload__";
-// Dense DiT base precisions: they load a dense (bf16) base and quantise it, so the backend rejects them for a bnb-4bit repo.
+// Dense DiT base precisions: they load a dense (bf16) base and quantise it, so the backend
+// rejects them for a bnb-4bit repo.
const DENSE_PRECISIONS = new Set(["bf16", "int8", "fp8", "mxfp8"]);
// Mirror the backend repo_is_prequantized heuristic: a bitsandbytes 4-bit repo cannot serve the dense base precisions.
function repoIsPrequantized(baseModel: string): boolean {
@@ -196,21 +195,21 @@ function repoIsPrequantized(baseModel: string): boolean {
// Dataset-select option value prefix for a not-yet-imported example; picking it imports.
const EXAMPLE_PREFIX = "example:";
-/** "12 images", "12 clips", or "12 items" for a mixed folder. The picker has one line per
- * dataset, and a clip folder reading "0 images" is exactly the bug this labels away. */
+/** "12 images", "12 clips", or "12 items" for a mixed folder: a clip folder reading "0 images"
+ * is the bug this labels away. */
function datasetItemLabel(d: { image_count: number; clip_count?: number }): string {
const clips = d.clip_count ?? 0;
const total = d.image_count + clips;
const noun = clips === 0 ? "image" : d.image_count === 0 ? "clip" : "item";
return `${total} ${noun}${total === 1 ? "" : "s"}`;
}
-// min-w-0 + a truncating value: a long option would otherwise set the grid column min width and push into its neighbour.
+// min-w-0 with a truncating value: a long option would otherwise set the grid column min width
+// and push into its neighbour.
const selectClass =
"h-8 w-full min-w-0 text-xs *:data-[slot=select-value]:min-w-0 *:data-[slot=select-value]:truncate";
-// Every settings cell is a grid item, so it needs min-w-0 to be allowed to shrink.
-// grid-cols-1 is what carries that shrink to the contents: a bare `grid` leaves the
-// implicit column auto-sized, so the track froze at its widest child's min-content
-// (150px for the run-length pair) and the cell painted over the next column.
+// Every settings cell is a grid item, so it needs min-w-0 to shrink; grid-cols-1 carries that
+// shrink to the contents, since a bare `grid` froze the implicit track at its widest child's
+// min-content (150px) and painted over the next column.
const fieldClass = "grid grid-cols-1 min-w-0 gap-2";
/** opens the folder picker; icon-only because the 416px rail already holds the select and the file-pick button. */
@@ -241,8 +240,7 @@ function FolderPickButton({
);
}
-/** A field's label with its guidance behind an "i" tooltip, keeping the grid scannable.
- * Only facts a user must act on stay on the page as text. */
+/** A field's label with its guidance behind an "i" tooltip; only facts a user must act on stay on the page as text. */
function FieldLabel({
hint,
children,
@@ -252,16 +250,16 @@ function FieldLabel({
}) {
return (
<div className="flex min-w-0 items-center gap-1">
- {/* block, not Label's default flex: text-overflow does nothing on a flex
- container, so truncate cut the text mid-glyph instead of ellipsing it. */}
+ {/* block, not Label's default flex: text-overflow does nothing on a flex container, so truncate
+ cut the text mid-glyph. */}
<Label className="block min-w-0 truncate text-xs">{children}</Label>
{hint ? <InfoHint>{hint}</InfoHint> : null}
</div>
);
}
-/** The family's training facts as chips: size, QLoRA VRAM floor, access. What a chip cannot
- * carry stays as a line below, as does the prose from a backend too old to send the fields. */
+/** The family's training facts as chips: size, QLoRA VRAM floor, access. What a chip cannot carry
+ * stays as a line below. */
function FamilyFacts({ family, baseModel }: { family?: FamilyPreset; baseModel?: string }) {
if (!family) return null;
const facts = resolveDiffusionTrainingFacts(family, baseModel);
@@ -307,7 +305,7 @@ function FamilyFacts({ family, baseModel }: { family?: FamilyPreset; baseModel?:
);
}
-// Merge the backend-reported families over the presets, keeping the preset ordering and filling anything the backend omits.
+// Merge backend-reported families over the presets, keeping preset ordering and filling anything the backend omits.
function mergeFamilies(reported?: DiffusionTrainableFamily[]): FamilyPreset[] {
if (!reported || reported.length === 0) return FAMILY_PRESETS;
const byName = new Map(reported.map((f) => [f.name, f]));
@@ -323,15 +321,15 @@ function mergeFamilies(reported?: DiffusionTrainableFamily[]): FamilyPreset[] {
rank: r.defaults?.lora_rank ?? p.defaults.rank,
lr: r.defaults?.learning_rate ?? p.defaults.lr,
resolution: r.defaults?.resolution ?? p.defaults.resolution,
- // No preset fallback for the ramp: a reported family owns it outright, so a backend that
- // drops its warmup preset drops the ramp here too instead of a stale copy resurrecting it.
+ // No preset fallback for the ramp: a reported family owns it outright, so a backend that drops
+ // its warmup preset drops the ramp too.
...lrSchedulePreset(r.defaults),
},
vram_note: r.vram_note || p.vram_note,
gated: r.gated ?? p.gated,
base_specs: r.base_specs,
- // The chips travel together: a backend reporting any of them owns the whole set, so a
- // preset value cannot sit beside a live one describing a different build.
+ // The chips travel together: a backend reporting any of them owns the whole set, so a preset
+ // value cannot sit beside a live one.
...(r.params != null || r.qlora_vram_gb != null || r.note != null
? {
params: r.params ?? "",
@@ -364,7 +362,8 @@ function mergeFamilies(reported?: DiffusionTrainableFamily[]): FamilyPreset[] {
return merged;
}
-// A full-page training workspace: left = configure, right = live run. Kept mounted with the page so a long run survives tab switches; polling is gated on `active`.
+// Full-page training workspace: left = configure, right = live run. Kept mounted with the page
+// so a long run survives tab switches; polling is gated on `active`.
export function DiffusionTrainPanel({
active,
loadedFamily,
@@ -378,7 +377,7 @@ export function DiffusionTrainPanel({
onFamiliesChange,
}: {
active: boolean;
- // The currently loaded generation model family / base repo, to preselect a matching training base when it is one we can train.
+ // Loaded generation model family / base repo, to preselect a matching training base when it is one we can train.
loadedFamily?: string | null;
loadedBaseRepo?: string | null;
// Family + base are controlled by the page: the top bar picks the training base while Train is showing.
@@ -409,20 +408,23 @@ export function DiffusionTrainPanel({
() => families.find((f) => f.name === familyName) ?? families[0],
[families, familyName],
);
- // The raw backend family record (precision_modes / recommended_precision / supports_compile live only here). Absent on an older backend.
+ // Raw backend family record (precision_modes / recommended_precision / supports_compile live
+ // only here). Absent on an older backend.
const reportedFamily = useMemo(
() => info?.families?.find((f) => f.name === familyName),
[info?.families, familyName],
);
- // sdxl trains the U-Net in mixed precision, so it uses mixed_precision instead of base_precision. Everything else is a DiT family.
+ // sdxl trains the U-Net in mixed precision, so it uses mixed_precision instead of base_precision.
+ // Everything else is a DiT family.
const isDiT = familyName !== "sdxl";
- // An EMPTY precision_modes list on a DiT family is the backend's signal that this host cannot train it at all (the reason rides
- // in vram_note). Only an ABSENT field means an older backend. SDXL reports [] too but is not precision-gated, hence the isDiT scope.
+ // An EMPTY precision_modes list on a DiT family means this host cannot train it at all (the
+ // reason rides in vram_note); only an ABSENT field means an older backend. SDXL reports []
+ // too but is not precision-gated, hence the isDiT scope.
const familyUntrainable =
isDiT &&
reportedFamily?.precision_modes != null &&
reportedFamily.precision_modes.length === 0;
- // The quantised base precisions this family can train in, with a stable fallback when the backend does not report them.
+ // Quantised base precisions this family can train in, with a stable fallback when the backend does not report them.
const precisionModes = useMemo<
Array<"nf4" | "bf16" | "int8" | "fp8" | "mxfp8" | "auto">
>(() => {
@@ -435,17 +437,14 @@ export function DiffusionTrainPanel({
// Fallback without a backend report: the GPU-independent modes only (mxfp8 needs a Blackwell probe).
return ["auto", "nf4", "bf16", "int8", "fp8"];
}, [reportedFamily?.precision_modes, familyUntrainable]);
- // Whether to show the torch.compile control. The backend advertises it per family; default on for DiT families on an older backend.
+ // Whether to show the torch.compile control; default on for DiT families on an older backend.
const supportsCompile = reportedFamily?.supports_compile ?? isDiT;
- // Same idea for checkpoints. MiniMax-H3's loop writes no resume bundle and its validation
- // REFUSES a nonzero save_steps rather than ignoring it, so leaving the field on offer meant a
- // user could set it and get a rejected Start with nothing on the control saying why.
+ // Same for checkpoints: MiniMax-H3's loop writes no resume bundle and its validation REFUSES a
+ // nonzero save_steps, so offering the field meant a rejected Start with nothing saying why.
const supportsCheckpoints = reportedFamily?.supports_checkpoints ?? true;
- // And the batch axis, third of the same kind. MiniMax-H3's forward covers ONE packed
- // sequence, so its validation REFUSES a batch above 1 rather than clamping. The field was
- // rendered unrestricted and always sent, so a 2 typed here -- or simply carried over from the
- // family the user was on a moment ago -- rejected Start with nothing on the control to say
- // why. Hidden when the family caps it at 1, and the cap is what gets sent.
+ // Same for the batch axis: MiniMax-H3's forward covers ONE packed sequence and its validation
+ // REFUSES a batch above 1 rather than clamping, so a value carried over from another family
+ // rejected Start with nothing saying why. Hidden when the family caps it at 1.
const maxBatchSize = reportedFamily?.max_train_batch_size ?? null;
const batchIsFixed = maxBatchSize != null && maxBatchSize <= 1;
@@ -483,18 +482,20 @@ export function DiffusionTrainPanel({
const effectiveBatchSize = maxBatchSize == null ? batchSize : Math.min(batchSize, maxBatchSize);
const [gradAccum, setGradAccum] = useState(1);
const [seed, setSeed] = useState(42);
- // Periodic resume points. 0 (off) keeps the default behaviour: only a stop-and-save writes one,
- // so nothing is spent on disk unless the user asks to survive a crash.
+ // Periodic resume points. 0 (off) keeps the default: only a stop-and-save writes one, so nothing
+ // is spent on disk unless asked.
const [saveSteps, setSaveSteps] = useState(0);
- // LR schedule. Warmup only applies to the non-constant schedules; plain "constant" ignores it.
- // Seeded from the family below, which is where the flow-matching DiTs' short ramp comes from.
+ // LR schedule. Warmup applies only to non-constant schedules. Seeded from the family, which is
+ // where the flow-matching DiTs' short ramp comes from.
const [lrScheduler, setLrScheduler] = useState<LrScheduler>("constant");
const [lrWarmupSteps, setLrWarmupSteps] = useState(0);
// Gradient checkpointing trades ~20-30% step time for a large activation-VRAM saving.
const [gradCheckpoint, setGradCheckpoint] = useState(true);
- // sdxl (U-Net) trains in a mixed-precision autocast; the DiT families quantise the frozen base weights and ignore this.
+ // sdxl (U-Net) trains in a mixed-precision autocast; the DiT families quantise the frozen base
+ // weights and ignore this.
const [precision, setPrecision] = useState<"bf16" | "fp16" | "no">("bf16");
- // Quantised base precision for DiT families (nf4 QLoRA default). "auto" lets the backend pick the family's recommendation.
+ // Quantised base precision for DiT families (nf4 QLoRA default). "auto" lets the backend pick the
+ // family's recommendation.
const [basePrecision, setBasePrecision] = useState<
"nf4" | "bf16" | "int8" | "fp8" | "mxfp8" | "auto"
>("auto");
@@ -502,18 +503,18 @@ export function DiffusionTrainPanel({
const [compileTransformer, setCompileTransformer] = useState<"off" | "on" | "auto">(
"auto",
);
- // Track whether the user hand-edited the numeric settings; if not, a family change re-seeds them from that family defaults.
+ // Track whether the user hand-edited the numeric settings; if not, a family change re-seeds them
+ // from that family's defaults.
const settingsDirty = useRef(false);
- // The LR schedule pair tracks its own edits rather than riding on settingsDirty. It is the one
- // setting whose control DISAPPEARS -- "Warmup steps" is hidden under plain "constant" -- so a
- // value carried past a family change is invisible rather than merely stale, and an edit to
- // something unrelated like Steps or Seed would otherwise leave a flow-matching DiT on
- // "constant" with the family's ramp silently never applied.
+ // The LR schedule pair tracks its own edits rather than riding on settingsDirty: it is the one
+ // setting whose control DISAPPEARS ("Warmup steps" is hidden under plain "constant"), so a
+ // value carried past a family change is invisible rather than merely stale.
const lrScheduleDirty = useRef(false);
- // Track whether the user hand-picked a base precision; if not, a family change re-seeds it from recommended_precision.
+ // Track whether the user hand-picked a base precision; if not, a family change re-seeds it from
+ // recommended_precision.
const precisionDirty = useRef(false);
- // Same for the base repo: once the user picks one, only a real family change may re-seed it. `family` is a fresh object after every
- // refreshInfo(), so the seeding effect re-runs on an unrelated refresh and would replace the pick; track the seeded family by name.
+ // Same for the base repo. `family` is a fresh object after every refreshInfo(), so the seeding
+ // effect re-runs on an unrelated refresh and would replace the pick; track it by name.
const baseDirty = useRef(false);
const seededBaseFamily = useRef<string | null>(null);
@@ -544,7 +545,8 @@ export function DiffusionTrainPanel({
}
}, []);
- // On first activation, load the dataset list and preselect a base matching the loaded generation model when it is trainable.
+ // On first activation, load the dataset list and preselect a base matching the loaded generation
+ // model when it is trainable.
useEffect(() => {
if (!active) return;
void refreshInfo().then((i) => {
@@ -571,7 +573,8 @@ export function DiffusionTrainPanel({
};
}, [active]);
- // Examples whose folder is not on disk yet. An example imports into a folder named after its id, so a matching dataset name means it is already imported.
+ // Examples whose folder is not on disk yet. An example imports into a folder named after its id,
+ // so a matching dataset name means it is already imported.
const importedNames = useMemo(
() => new Set((info?.datasets ?? []).map((d) => d.name)),
[info?.datasets],
@@ -581,7 +584,8 @@ export function DiffusionTrainPanel({
[examples, importedNames],
);
- // Import a curated example, then select the resulting folder. Seeds the trigger prompt only when the field is meaningful.
+ // Import a curated example, then select the resulting folder. Seeds the trigger prompt only when
+ // the field is meaningful.
const importExample = useCallback(
async (ex: DiffusionDatasetExample) => {
setImportingId(ex.id);
@@ -614,21 +618,20 @@ export function DiffusionTrainPanel({
}
}, [loadedFamily, families]);
- // Re-seed base + numeric settings from the family defaults on family change (unless the user edited them). Prefer the loaded base repo when it belongs to this family.
+ // Re-seed base and numeric settings from family defaults on family change (unless the user edited
+ // them). Prefer the loaded base repo when it belongs to this family.
useEffect(() => {
if (!family) return;
- // A NEW family invalidates any earlier base pick; a mere info refresh does not, so compare by name rather than object identity.
+ // A NEW family invalidates any earlier base pick; a mere info refresh does not, so compare by
+ // name rather than object identity.
if (seededBaseFamily.current !== family.name) {
seededBaseFamily.current = family.name;
baseDirty.current = false;
}
- // An already-valid base wins: the top bar sets family and base together, so this must not snap back to the family's first repo.
- // A loaded checkpoint may be the DISTILLED half of a pair, which is not trainable and so is
- // never in base_repos. Fall back to the training base the family pairs it with before
- // dropping to base_repos[0], or opening Train with the 9B model loaded seeds the 4B base.
- // reportedFamily, not family: the pairing lives in deploy_bases, which only the backend
- // reports. The static presets have no pairings, so an older backend simply keeps today's
- // behaviour here.
+ // An already-valid base wins: the top bar sets family and base together. A loaded checkpoint may
+ // be the DISTILLED half of a pair, which is never in base_repos, so fall back to the paired
+ // training base before base_repos[0], or opening Train with the 9B model loaded seeds the 4B
+ // base. reportedFamily, since deploy_bases is backend-only.
const pairedTrainingBase = loadedBaseRepo
? resolveDiffusionTrainingBase(reportedFamily, loadedBaseRepo)
: null;
@@ -643,14 +646,14 @@ export function DiffusionTrainPanel({
setRank(family.defaults.rank);
setResolution(family.defaults.resolution);
}
- // The ramp is seeded from the family or reset with it: a family with no warmup preset goes
- // back to the plain default, or the 20 steps recommended for a flow-matching DiT ride along
- // into SDXL, which never asked for one.
+ // The ramp is seeded from the family or reset with it, or the 20 steps recommended for a
+ // flow-matching DiT ride along into SDXL, which never asked for one.
if (!lrScheduleDirty.current) {
setLrScheduler(family.defaults.lrScheduler ?? "constant");
setLrWarmupSteps(family.defaults.lrWarmupSteps ?? 0);
}
- // Re-seed the DiT base precision from the family recommendation (unless the user picked one); "auto" is always safe.
+ // Re-seed the DiT base precision from the family recommendation (unless the user picked one);
+ // "auto" is always safe.
if (!precisionDirty.current) {
const rec = reportedFamily?.recommended_precision;
setBasePrecision(
@@ -661,25 +664,28 @@ export function DiffusionTrainPanel({
}
}, [family, loadedBaseRepo, reportedFamily]);
- // mixed_precision is an SDXL-only lever. A dense DiT base precision requires bf16 compute and every DiT family trains in bf16,
- // so reset to bf16 on a change to a DiT family, or an fp16 value left from SDXL rides along and the backend rejects it.
+ // mixed_precision is an SDXL-only lever and every DiT family trains in bf16, so reset to bf16 on
+ // a change to a DiT family, or an fp16 value left from SDXL rides along and is rejected.
useEffect(() => {
if (isDiT) setPrecision("bf16");
}, [isDiT]);
- // The base actually used everywhere (request, deploy, select value). baseChoice can briefly hold another family's repo, where a
- // raw <select value> would DISPLAY the first option while the request carried the stale repo, so clamp to the current family.
+ // The base actually used everywhere. baseChoice can briefly hold another family's repo, where a
+ // raw <select value> would DISPLAY the first option while the request carried the stale repo,
+ // so clamp to the current family.
const effectiveBase =
baseChoice === CUSTOM_BASE || (family?.base_repos ?? []).includes(baseChoice)
? baseChoice
: family?.base_repos[0] ?? CUSTOM_BASE;
- // The resolved base repo/path the request will carry, and whether it looks prequantized. The dense precisions are invalid for such a repo.
+ // The resolved base repo/path the request will carry, and whether it looks prequantized. The
+ // dense precisions are invalid for such a repo.
const resolvedBase = (effectiveBase === CUSTOM_BASE ? customBase : effectiveBase).trim();
const basePrequantized = isDiT && repoIsPrequantized(resolvedBase);
- // A prequantized base cannot serve the dense precisions; auto-flip a dense selection back to "auto" (which resolves to nf4)
- // so the run does not fail at the backend validator. Reuses precisionDirty so a later family change still re-seeds.
+ // A prequantized base cannot serve the dense precisions; auto-flip a dense selection back to
+ // "auto" (nf4) so the run does not fail at the backend validator. Reuses precisionDirty so a
+ // later family change still re-seeds.
useEffect(() => {
if (basePrequantized && DENSE_PRECISIONS.has(basePrecision)) {
precisionDirty.current = false;
@@ -703,7 +709,8 @@ export function DiffusionTrainPanel({
return () => window.clearInterval(id);
}, [active, poll]);
- // "Train another" dismisses the completed run card locally: the backend keeps the terminal status until the next start.
+ // "Train another" dismisses the completed run card locally: the backend keeps the terminal status
+ // until the next start.
const [dismissedJobId, setDismissedJobId] = useState<string | null>(null);
const running = Boolean(status?.active) || status?.status === "running";
const completed =
@@ -718,19 +725,19 @@ export function DiffusionTrainPanel({
? Math.min(100, Math.round((status.step / status.total_steps) * 100))
: 0;
- // The pending-stop flag only matters while a run is active; clamping at read time means a fresh run never inherits a stale "Stopping...".
+ // The pending-stop flag only matters while a run is active; clamping at read time means a fresh
+ // run never inherits a stale "Stopping...".
const stopRequested = running && stopRequestedLocal;
- // The just-finished run's persisted record, which is where can_resume lives (the live status has
- // no way to know whether the checkpoint survived on disk). It appears a beat after the terminal
- // status, via the delayed refetch below, so the Resume action enables itself once it lands.
+ // The just-finished run's persisted record, where can_resume lives. It appears a beat after the
+ // terminal status via the delayed refetch below, so Resume enables itself once it lands.
const liveRunSummary = useMemo(
() => prevRuns.find((r) => r.job_id === status?.job_id) ?? null,
[prevRuns, status?.job_id],
);
- // Whether there is a run to show live: running, or ANY terminal run the user has not dismissed. Dismissing must cover every
- // terminal status, or "Train another" after a stop would trap the run view with no way back to the settings.
+ // Whether there is a run to show live: running, or ANY terminal run the user has not dismissed.
+ // Dismissing must cover every terminal status, or "Train another" traps the run view.
const terminalStatuses = ["completed", "stopped", "error"];
const hasRun = Boolean(
status &&
@@ -738,8 +745,8 @@ export function DiffusionTrainPanel({
!(terminalStatuses.includes(status.status) && status.job_id === dismissedJobId),
);
- // Notify the parent once per run that produced an adapter so it rescans the LoRA picker. The flag is re-armed here and in
- // onStart, so a second run still notifies even if the poll never catches the intermediate "running" state.
+ // Notify the parent once per run that produced an adapter so it rescans the LoRA picker. Re-armed
+ // here and in onStart, so a second run still notifies even if "running" is never observed.
const notifiedComplete = useRef(false);
useEffect(() => {
const producedAdapter =
@@ -758,7 +765,7 @@ export function DiffusionTrainPanel({
// A deleted dataset leaves a name that no longer resolves; fall back to the upload form.
const uploadMode = dataset === UPLOAD_DATASET || (info !== null && !selectedDataset);
// Trainable items in the picked dataset, images and clips alike. caption_count is the folder
- // total over both kinds, so every ratio below has to be against this and not image_count.
+ // total over both kinds, so every ratio must be against this and not image_count.
const selectedItemCount = selectedDataset ? datasetItemCount(selectedDataset) : 0;
// A dataset where every item already ships a caption needs no trigger prompt; hide the field and explain why.
const fullyCaptioned = Boolean(
@@ -794,8 +801,8 @@ export function DiffusionTrainPanel({
.catch(() => {});
};
refetch();
- // The service exposes a terminal status before the pump has necessarily written the run JSON, so the one-shot refetch can
- // win that race. A short delayed second refetch lets the record land so the newest run reliably appears.
+ // The service exposes a terminal status before the pump has necessarily written the run JSON, so
+ // a short delayed second refetch lets the record land.
let delayed: ReturnType<typeof setTimeout> | undefined;
if (status?.status === "completed" || status?.status === "stopped" || status?.status === "error") {
delayed = setTimeout(refetch, 1500);
@@ -832,7 +839,7 @@ export function DiffusionTrainPanel({
// `picked` can come from a file pick, a folder pick or a drop, so it is filtered here.
const uploadTo = useCallback(
async (name: string, picked: File[]) => {
- if (picked.length === 0) return; // the picker was cancelled
+ if (picked.length === 0) return; // the picker was cancelled
if (!name) {
toast.error("Give the dataset a folder name, e.g. my-style-photos.");
return;
@@ -861,8 +868,8 @@ export function DiffusionTrainPanel({
);
return;
}
- // /diffusion/info only lists folders holding a trainable item, so say why the set will
- // not appear yet rather than refusing a captions-first upload.
+ // /diffusion/info only lists folders holding a trainable item, so say why the set will not appear
+ // yet rather than refusing a captions-first upload.
const newCaptionsOnly =
imageCount === 0 && clipCount === 0 && !(info?.datasets ?? []).some((d) => d.name === name);
if (uploadInFlight.current) {
@@ -873,11 +880,9 @@ export function DiffusionTrainPanel({
setUploading(true);
try {
const misKeyed = await metadataKeyedOnSubfolders(files);
- // the endpoint accumulates into the same folder, so a tree past the multipart part or
- // byte cap goes up in slices rather than being refused outright.
- // the cap decides where the slices fall, so a guess makes the refusal below either too
- // strict or useless. Forced past the cache, which another tab can leave behind, and a
- // cap that cannot be read stops the upload.
+ // the endpoint accumulates into the same folder, so a tree past the multipart part or byte cap
+ // goes up in slices. The cap decides where the slices fall, so it is forced past the cache,
+ // and a cap that cannot be read stops the upload.
let maxBytes: number;
try {
maxBytes = (await loadUploadLimitSettings({ force: true })).maxUploadSizeBytes;
@@ -889,8 +894,8 @@ export function DiffusionTrainPanel({
return;
}
const chunks = chunkDatasetUpload(files, maxBytes);
- // a slice no split can fit under the cap 413s, so refuse before the first request:
- // otherwise the slices ahead of it are already committed.
+ // a slice no split can fit under the cap 413s, so refuse before the first request: otherwise the
+ // slices ahead of it are already committed.
const over = oversizedChunk(chunks, maxBytes);
if (over) {
toast.error(
@@ -899,11 +904,10 @@ export function DiffusionTrainPanel({
);
return;
}
- // one request is all-or-nothing on the backend, so only a split top-up needs the folder
- // checked as well: there a stem it already holds would 400 a later slice, mid-commit.
+ // one request is all-or-nothing on the backend, so only a split top-up needs the folder checked
+ // as well: there a stem it already holds would 400 a later slice, mid-commit.
if (chunks.length > 1) {
// the list held here is the one from mount, so it cannot say the folder is new.
- // One more GET is nothing against a selection already going up in several requests.
const known = await refreshInfo();
if (!known) {
toast.error(
@@ -911,15 +915,14 @@ export function DiffusionTrainPanel({
);
return;
}
- // a case-insensitive dataset root resolves "photos" onto an existing "Photos", so an
- // exact match would call that folder new and skip the check. Fall back to a folded
- // match and list the folder under the name the backend reports.
+ // a case-insensitive dataset root resolves "photos" onto an existing "Photos", so an exact match
+ // would call that folder new and skip the check. Fall back to a folded match.
const folder =
known.datasets.find((d) => d.name === name) ??
known.datasets.find((d) => d.name.toLowerCase() === name.toLowerCase());
if (folder) {
- // no listing, no guarantee: skipping the check would upload the slices it exists to
- // hold back, so a failure here stops the upload rather than proceeding blind.
+ // no listing, no guarantee: skipping the check would upload the slices it exists to hold back, so
+ // a failure here stops the upload.
let held: Awaited<ReturnType<typeof listDiffusionDatasetImages>>;
try {
held = await listDiffusionDatasetImages(folder.name);
@@ -1055,7 +1058,8 @@ export function DiffusionTrainPanel({
toast.error("Name the adapter (this becomes its folder under Unsloth outputs).");
return;
}
- // Require a trigger prompt whenever ANY image lacks a caption: without an instance_prompt the backend silently skips every uncaptioned image.
+ // Require a trigger prompt whenever ANY image lacks a caption: without an instance_prompt the
+ // backend silently skips every uncaptioned image.
if (
selectedDataset &&
selectedDataset.caption_count < selectedItemCount &&
@@ -1085,9 +1089,10 @@ export function DiffusionTrainPanel({
if (learningRate <= 0) return toast.error("Learning rate must be greater than 0.");
if (lrWarmupSteps < 0) return toast.error("Warmup steps cannot be negative.");
setStarting(true);
- // A previous run's confirmed stop must not leak into this run: the read-time clamp would otherwise re-arm a permanently disabled "Stopping..." button.
+ // A previous run's confirmed stop must not leak into this run: the read-time clamp would re-arm a
+ // permanently disabled "Stopping..." button.
setStopRequestedLocal(false);
- // Re-arm the completion notification for this run, so a second run still notifies even if its "running" phase is never observed.
+ // Re-arm the completion notification, so a second run still notifies even if its "running" phase is never observed.
notifiedComplete.current = false;
// A history view must not shadow the new live run.
setViewRun(null);
@@ -1103,8 +1108,8 @@ export function DiffusionTrainPanel({
train_steps: durationUnit === "epochs" ? undefined : steps,
num_epochs: durationUnit === "epochs" ? epochs : undefined,
learning_rate: learningRate,
- // The family cap, not the field: it is only hidden, not reset, so a value typed for
- // another family would otherwise still be sent and refused.
+ // The family cap, not the field: it is only hidden, not reset, so a value typed for another
+ // family would otherwise still be sent and refused.
train_batch_size: effectiveBatchSize,
gradient_accumulation_steps: gradAccum,
seed,
@@ -1112,12 +1117,12 @@ export function DiffusionTrainPanel({
lr_scheduler: lrScheduler,
lr_warmup_steps: lrScheduler === "constant" ? 0 : lrWarmupSteps,
lora_rank: rank,
- // Zero rather than the field's value when the family has none, because the field is
- // only hidden, not reset: a value typed for one family would otherwise still be sent
- // after switching to a checkpointless one, and refused.
+ // Zero rather than the field's value when the family has none: the field is only hidden, not
+ // reset, so a value typed for one family would otherwise still be sent and refused.
save_steps: supportsCheckpoints ? Math.max(0, Math.floor(saveSteps)) : 0,
mixed_precision: precision,
- // DiT families quantise the base weights; sdxl uses mixed_precision above and ignores this. Only send compile where supported.
+ // DiT families quantise the base weights; sdxl uses mixed_precision above and ignores this. Only
+ // send compile where supported.
base_precision: isDiT ? basePrecision : undefined,
compile_transformer: supportsCompile ? compileTransformer : undefined,
hf_token: hfApiToken(getHfToken()) || undefined,
@@ -1161,11 +1166,9 @@ export function DiffusionTrainPanel({
poll,
]);
- // Continue a stopped run from its newest checkpoint. The stored config is replayed verbatim
- // (same hyperparameters, same output folder, same TARGET step count), with the run's output
- // directory as resume_from_checkpoint -- the same approach as the LLM tab's resumeTrainingRun.
- // The detail is re-fetched on click so `can_resume` reflects the checkpoints on disk right now,
- // not whatever the list was showing when it was last polled.
+ // Continue a stopped run from its newest checkpoint: the stored config is replayed verbatim with
+ // the run's output directory as resume_from_checkpoint. The detail is re-fetched on click so
+ // `can_resume` reflects the checkpoints on disk right now.
const onResume = useCallback(
async (jobId: string) => {
setResumingJobId(jobId);
@@ -1175,8 +1178,8 @@ export function DiffusionTrainPanel({
hfToken: hfApiToken(getHfToken()) || undefined,
});
await startDiffusionTraining(payload);
- // Only after the start is ACCEPTED: a refusal (identity mismatch, a job already running)
- // must leave the history detail the user was reading exactly as it was.
+ // Only after the start is ACCEPTED: a refusal must leave the history detail the user was reading
+ // exactly as it was.
setStopRequestedLocal(false);
notifiedComplete.current = false;
setViewRun(null);
@@ -1196,7 +1199,7 @@ export function DiffusionTrainPanel({
[poll],
);
- // Confirm-then-stop, mirroring the LLM Train tab. `save` writes the current adapter before halting; false discards it.
+ // Confirm-then-stop. `save` writes the current adapter before halting; false discards it.
const onStop = useCallback(
async (save: boolean) => {
setStopDialogOpen(false);
@@ -1217,8 +1220,8 @@ export function DiffusionTrainPanel({
[poll],
);
- // Resolve the repo an adapter should be previewed on. Variant-specific pairs cover FLUX.2
- // Klein's 4B and 9B bases; the scalar fallback keeps older backends and Krea 2 working.
+ // Resolve the repo an adapter should be previewed on. Variant-specific pairs cover FLUX.2 Klein's
+ // 4B and 9B bases; the scalar fallback keeps older backends and Krea 2 working.
const deployBaseFor = useCallback(
(trainedBase: string, famName: string): string => {
const rec = info?.families?.find((f) => f.name === famName);
@@ -1253,8 +1256,7 @@ export function DiffusionTrainPanel({
fallback: number,
// markDirty overrides which dirty flag an edit claims. Only "Warmup steps" passes one: it is
// seeded from the family like rank/LR/resolution but tracked by lrScheduleDirty, so charging
- // it to the shared flag would mean tuning the ramp froze the OTHER three at the previous
- // family's values. That field is newly visible by default, so this is reachable now.
+ // it to the shared flag would freeze the other three at the previous family's values.
extra?: { min?: number; step?: number; hint?: ReactNode; markDirty?: () => void },
) => (
<div className={fieldClass}>
@@ -1267,7 +1269,8 @@ export function DiffusionTrainPanel({
onChange={(e) => {
if (extra?.markDirty) extra.markDirty();
else settingsDirty.current = true;
- // Only fall back when the input parses to NaN; a real 0 is legal for zero-legal fields (Seed, LR warmup steps).
+ // Only fall back when the input parses to NaN; a real 0 is legal for zero-legal fields (Seed, LR
+ // warmup steps).
const parsed = Number(e.target.value);
set(Number.isNaN(parsed) ? fallback : parsed);
}}
@@ -1276,7 +1279,8 @@ export function DiffusionTrainPanel({
</div>
);
- // Run length: a number paired with a compact unit select. Epochs mode trains that many full passes; the backend resolves it to steps.
+ // Run length: a number paired with a compact unit select. Epochs mode trains that many full
+ // passes; the backend resolves it to steps.
const durationField = (
<div className={fieldClass}>
<FieldLabel
@@ -1336,12 +1340,10 @@ export function DiffusionTrainPanel({
return "fp8 (experimental)";
};
- // The training settings, shown as the run area's MAIN content before a run starts; the run view replaces them afterwards.
- // Columns key off this pane's OWN width, not the window's: the pane is whatever is
- // left beside the 416px form column, so a viewport breakpoint put three columns in a
- // ~280px pane and every cell spilled into its neighbour. A cell needs 150px (the run
- // length pair's floor: 66px number field + 6px gap + 78px unit select), hence 324px
- // for two columns and 498px for three.
+ // The training settings, the run area's MAIN content before a run starts. Columns key off this
+ // pane's OWN width, not the window's, or a viewport breakpoint put three columns in a ~280px
+ // pane. A cell needs 150px (66px number field + 6px gap + 78px unit select), hence 324px for
+ // two columns and 498px for three.
const trainingSettings = (
<div className="@container flex flex-col gap-6">
<div className="grid grid-cols-1 gap-x-6 gap-y-5 @min-[324px]:grid-cols-2 @min-[498px]:grid-cols-3">
@@ -1387,8 +1389,8 @@ export function DiffusionTrainPanel({
<Select
value={lrScheduler}
onValueChange={(v) => {
- // The family re-seed now writes this field, so without a dirty mark a hand-picked
- // schedule is replaced on the next family switch.
+ // The family re-seed now writes this field, so without a dirty mark a hand-picked schedule is
+ // replaced on the next family switch.
lrScheduleDirty.current = true;
setLrScheduler(v as LrScheduler);
}}
@@ -1408,9 +1410,8 @@ export function DiffusionTrainPanel({
numberField("Warmup steps", lrWarmupSteps, setLrWarmupSteps, 0, {
min: 0,
hint: "Ramps the learning rate up over the first steps instead of starting at full size.",
- // The other half of the pair, so an edit claims the pair's flag and only that: a
- // hand-typed ramp length survives the next family change the way the schedule does,
- // without freezing rank/LR/resolution on their way to the new family.
+ // The other half of the pair, so an edit claims the pair's flag and only that, without freezing
+ // rank/LR/resolution on their way to the new family.
markDirty: () => {
lrScheduleDirty.current = true;
},
@@ -1464,12 +1465,12 @@ export function DiffusionTrainPanel({
))}
</SelectContent>
</Select>
- {/* Only state stays on the page: both lines say why the control is limited right
- now. The general guidance is in the label's tooltip. */}
+ {/* Only state stays on the page: both lines say why the control is limited right now. The general
+ guidance is in the label's tooltip. */}
{(familyUntrainable || basePrequantized) && (
<p className="text-ui-11 leading-snug text-muted-foreground">
{familyUntrainable
- ? // The reason itself already shows in the family picker note above.
+ ? // The reason itself already shows in the family picker note above.
"This GPU cannot train this model family."
: "This base is already 4-bit, so only nf4/auto apply."}
</p>
@@ -1521,8 +1522,8 @@ export function DiffusionTrainPanel({
</div>
);
- // overflow-x-hidden: an unset overflow-x computes to auto beside overflow-y-auto,
- // letting a wide row pan the page sideways on a phone.
+ // overflow-x-hidden: an unset overflow-x computes to auto beside overflow-y-auto, letting a wide
+ // row pan the page sideways on a phone.
return (
<div className="flex min-h-0 w-full min-w-0 flex-1 flex-col overflow-y-auto overflow-x-hidden pr-5 sm:pr-8 @[50rem]:flex-row @[50rem]:overflow-hidden">
{/* Left: configure. The 408px rail and container breakpoint match Create and the shared header. */}
@@ -1578,7 +1579,7 @@ export function DiffusionTrainPanel({
<Select
value={effectiveBase}
onValueChange={(v) => {
- baseDirty.current = true; // an explicit pick survives later info refreshes
+ baseDirty.current = true; // an explicit pick survives later info refreshes
setBaseChoice(v);
}}
>
@@ -1606,7 +1607,8 @@ export function DiffusionTrainPanel({
</div>
{/* Dataset */}
- {/* the whole field is the drop zone, so a folder can land on the picker, the thumbnails or the caption grid. */}
+ {/* the whole field is the drop zone, so a folder can land on the picker, the thumbnails or the
+ caption grid. */}
<div
className={cn(
fieldClass,
@@ -1639,7 +1641,7 @@ export function DiffusionTrainPanel({
if (v.startsWith(EXAMPLE_PREFIX)) {
const ex = pendingExamples.find((x) => x.id === v.slice(EXAMPLE_PREFIX.length));
if (ex) void importExample(ex);
- return; // the controlled value stays put while the import runs
+ return; // the controlled value stays put while the import runs
}
setDataset(v);
setGridOpen(false);
@@ -1734,7 +1736,8 @@ export function DiffusionTrainPanel({
className="h-8 min-w-0 flex-1 text-xs"
aria-label="New dataset name"
/>
- {/* The native input is the file picker but never the control the user sees, so the row keeps the app button styling. */}
+ {/* The native input is the file picker but never the control the user sees, so the row keeps the
+ app button styling. */}
<input
ref={fileInputRef}
type="file"
@@ -1795,10 +1798,9 @@ export function DiffusionTrainPanel({
onChanged={() => void refreshInfo()}
/>
)}
- {/* The grid renders thumbnails, so it is offered only where there are images.
- One guard over the toggle AND the grid: gating only the toggle would leave
- an open grid with nothing to close it once a mixed folder's last image is
- deleted, since the clips keep the folder listed. */}
+ {/* Thumbnails, so offered only where there are images. One guard over the toggle AND the grid:
+ gating only the toggle would leave an open grid with nothing to close it once a mixed
+ folder's last image is deleted. */}
{selectedDataset.image_count > 0 && (
<>
<LabelingGridToggle
@@ -1833,7 +1835,7 @@ export function DiffusionTrainPanel({
/>
</div>
- {/* Trigger + adapter name (trigger first: it describes the dataset, the name just labels the output) */}
+ {/* Trigger first: it describes the dataset, the name just labels the output. */}
{fullyCaptioned ? (
<p className="text-ui-11 leading-snug text-muted-foreground">
Every item in {selectedDataset?.name} has a caption, so no trigger prompt
@@ -1866,9 +1868,8 @@ export function DiffusionTrainPanel({
</div>
</div>
- {/* In its own footer, as Create's Generate is. The scroll mask provides the fade,
- so the footer stays unpainted to avoid dark-mode banding.
- Stop lives in the run card next to the live stats. */}
+ {/* In its own footer, as Create's Generate is. The scroll mask provides the fade, so the footer
+ stays unpainted to avoid dark-mode banding. */}
<div className="relative z-10 flex shrink-0 justify-center pt-0.5 pb-4 pl-8 pr-8">
<Button
type="button"
@@ -1887,8 +1888,9 @@ export function DiffusionTrainPanel({
</div>
</div>
- {/* Right: the run area. Before a run: training settings + previous-runs history; during/after: the live view. Selecting a previous run re-plots its logs read-only. */}
- {/* Sections carry no card of their own: spacing and a rule separate them. p-1.5 keeps the chart cards' outer ring from being clipped. */}
+ {/* Right: the run area. Before a run: training settings + previous-runs history; during/after: the live view. */}
+ {/* Sections carry no card of their own: spacing and a rule separate them. p-1.5 keeps the chart
+ cards' outer ring from being clipped. */}
{/* 40px off the rule, the gutter the settings column has off the page edge. */}
{/* This pane remains a query container for its own stat and chart breakpoints. */}
<div className="@container hover-scrollbar relative flex min-w-0 flex-1 flex-col gap-5 pb-7 pl-10 pr-1.5 pt-4 @[50rem]:overflow-y-auto @[50rem]:pt-[42px]">
@@ -1931,10 +1933,8 @@ export function DiffusionTrainPanel({
? ` - ${new Date(viewRun.ended_at * 1000).toLocaleString()}`
: ""}
</p>
- {/* Deploy and Resume sit together: a stopped run's two next steps are "use what I
- have" and "carry on training it". Resume stays visible but disabled when the
- backend says it cannot, with the reason as the tooltip, so the absence of a
- checkpoint is explained rather than silently hiding the action. */}
+ {/* Deploy and Resume sit together. Resume stays visible but disabled when the backend says it
+ cannot, with the reason as the tooltip, so its absence is explained rather than hidden. */}
<div className="flex flex-wrap items-center gap-2">
{viewRun.saved && viewRun.catalog_path && (
<Button
@@ -1981,8 +1981,7 @@ export function DiffusionTrainPanel({
) : !hasRun ? (
<>
<div className="flex flex-col gap-4">
- {/* mb-2 + gap-4 matches the left column's mb-1 + gap-5, so Steps
- starts level with Model family. */}
+ {/* mb-2 + gap-4 matches the left column's mb-1 + gap-5, so Steps starts level with Model family. */}
<div className="mb-2 flex items-center justify-between">
<div className="grid gap-1.5">
<span className="flex items-center gap-2 font-heading text-xl font-medium leading-none">
@@ -2104,9 +2103,8 @@ export function DiffusionTrainPanel({
{stopRequested ? "Stopping..." : "Stop training"}
</Button>
)}
- {/* Terminal runs WITHOUT an adapter card (error, or a discarded stop) still need a way back to the settings.
- They can also be the most worth resuming: a crash mid-run leaves no adapter but may well have left a
- periodic checkpoint, so offer Resume here too when the backend says there is one. */}
+ {/* Terminal runs WITHOUT an adapter card still need a way back to the settings, and can be the
+ most worth resuming: a crash leaves no adapter but may have left a periodic checkpoint. */}
{!running &&
status &&
terminalStatuses.includes(status.status) &&
@@ -2147,10 +2145,9 @@ export function DiffusionTrainPanel({
? "Trained"
: "Stopped early; the adapter as of the last finished step was saved"}
{status?.family ? ` (${status.family})` : ""}
- {/* The LoRA picker and Deploy to Create are the Images surface, and only an
- adapter published into the diffusion LoRA catalog reaches them. A family
- trained here without that catalog (video) still saves a loadable adapter,
- so report the file and claim nothing more. */}
+ {/* Only an adapter published into the diffusion LoRA catalog reaches the Images surface. A family
+ trained here without that catalog (video) still saves a loadable adapter, so report the
+ file and claim nothing more. */}
{status?.catalog_path
? " and added to the LoRA picker."
: ". Load it from the path below."}
@@ -2168,9 +2165,8 @@ export function DiffusionTrainPanel({
Deploy to Create
</Button>
)}
- {/* Only a run stopped short can be continued; a completed one is already at its
- target. Disabled until the run record lands (and enabled only if its
- checkpoint really is on disk), with the backend's reason as the tooltip. */}
+ {/* Only a run stopped short can be continued. Disabled until the run record lands, with the
+ backend's reason as the tooltip. */}
{!completed && status?.job_id && (
<Button
type="button"
@@ -2214,7 +2210,7 @@ export function DiffusionTrainPanel({
)}
</div>
- {/* Confirm-stop dialog (mirrors the LLM Train tab): Continue / Stop / Stop and save. */}
+ {/* Confirm-stop dialog: Continue / Stop / Stop and save. */}
<AlertDialog open={stopDialogOpen} onOpenChange={setStopDialogOpen}>
<AlertDialogContent overlayClassName="bg-background/40 supports-backdrop-filter:backdrop-blur-[1px]">
<AlertDialogHeader>
@@ -2224,7 +2220,8 @@ export function DiffusionTrainPanel({
finishes first.
</AlertDialogDescription>
</AlertDialogHeader>
- {/* flex-wrap keeps all three buttons visible at narrow widths; items-center + a real label on the destructive action, since a bare "Stop" read as misaligned. */}
+ {/* flex-wrap keeps all three buttons visible at narrow widths; a real label on the destructive
+ action, since a bare "Stop" read as misaligned. */}
<AlertDialogFooter className="flex-wrap items-center">
<AlertDialogCancel>Continue training</AlertDialogCancel>
<AlertDialogAction variant="destructive" onClick={() => void onStop(false)}>
diff --git a/studio/frontend/src/features/images/train/example-dataset-cards.tsx b/studio/frontend/src/features/images/train/example-dataset-cards.tsx
index f75baed370c..612a45d9c47 100644
--- a/studio/frontend/src/features/images/train/example-dataset-cards.tsx
+++ b/studio/frontend/src/features/images/train/example-dataset-cards.tsx
@@ -18,8 +18,9 @@ import {
importDiffusionDatasetExample,
} from "../api";
-// Best-effort preview thumbnails from the public HF datasets-server, cached per repo (module level) so re-renders do not
-// refetch. A repo the server cannot serve resolves to an empty list and the card renders without previews.
+// Best-effort preview thumbnails from the public HF datasets-server, cached per repo at module
+// level so re-renders do not refetch. A repo the server cannot serve resolves to an empty list
+// and the card renders without previews.
const _previewCache = new Map<string, Promise<string[]>>();
async function fetchPreviews(repo: string): Promise<string[]> {
@@ -83,8 +84,9 @@ function ExamplePreviews({ repo }: { repo: string }) {
);
}
-// One-click example-dataset importers. Each card shows the license (terms before import) plus preview thumbnails; on success the parent refreshes
-// its dataset list and selects the folder. One card per row: the config column is narrow, so a two-column grid wrapped titles one word per line.
+// One-click example-dataset importers. Each card shows the license before import plus preview
+// thumbnails; on success the parent refreshes its dataset list and selects the folder. One card
+// per row: the config column is narrow, so a two-column grid wrapped titles one word per line.
export function ExampleDatasetCards({
examples,
busyId,
diff --git a/studio/frontend/src/features/images/train/resume-diffusion-run.ts b/studio/frontend/src/features/images/train/resume-diffusion-run.ts
index 9fc8e9d79ac..9de643dc4d4 100644
--- a/studio/frontend/src/features/images/train/resume-diffusion-run.ts
+++ b/studio/frontend/src/features/images/train/resume-diffusion-run.ts
@@ -11,20 +11,13 @@ export const RESUME_UNAVAILABLE_MESSAGE =
"This run has no checkpoint to continue from. Start a new run instead.";
-/**
- * Replay a finished diffusion run's stored start config as a resume request.
- *
- * The same shape as the LLM tab's `resumeTrainingRun`: the run's own config is the source of
- * truth (so the resumed job trains with exactly the hyperparameters it was stopped with), with
- * `resume_from_checkpoint` pointed at the run's output directory. The backend picks the newest
- * complete `checkpoint-<N>` bundle inside it and continues at N+1, up to the SAME `train_steps`
- * target the original run had -- resuming a stop at 11 of 500 trains steps 12..500, it does not
- * queue another 500.
- *
- * Pure and throwing: the caller owns the toast. The thrown message is the backend's
- * `resume_blocked_reason` whenever it has one, since the server explains a refusal precisely and
- * a generic message here would contradict it.
- */
+/** Replay a finished diffusion run's stored start config as a resume request, like the LLM tab's
+ * `resumeTrainingRun`: the run's own config is the source of truth, with
+ * `resume_from_checkpoint` pointed at its output directory. The backend picks the newest
+ * complete `checkpoint-<N>` bundle and continues at N+1, up to the SAME `train_steps` target
+ * the original had. Pure and throwing: the caller owns the toast. The thrown message is the
+ * backend's `resume_blocked_reason` whenever it has one, since a generic message would
+ * contradict the server's own explanation. */
export function buildDiffusionResumePayload(
detail: DiffusionTrainingRunDetail,
options: { hfToken?: string | null } = {},
@@ -35,16 +28,15 @@ export function buildDiffusionResumePayload(
}
const config = (detail.config ?? {}) as Partial<DiffusionTrainingStartRequest>;
if (!config.base_model || !config.data_dir || !config.output_dir) {
- // A record from a much older backend (or a hand-edited one) cannot be replayed safely: a
- // missing path would resolve somewhere else entirely and train the wrong thing.
+ // A record from a much older backend, or a hand-edited one, cannot be replayed safely: a missing
+ // path would resolve somewhere else entirely and train the wrong thing.
throw new Error(
"This run's saved settings are incomplete, so it cannot be resumed automatically.",
);
}
// Destructured out rather than deleted off a widened copy: these three are replaced below, not
- // inherited. Replaying them verbatim would point the new run at whatever the OLD run resumed,
- // and the token is never persisted anyway.
+ // inherited. Replaying them verbatim would point the new run at whatever the OLD run resumed.
const {
hf_token: _replacedToken,
resume_from_checkpoint: _replacedCheckpoint,
@@ -57,9 +49,9 @@ export function buildDiffusionResumePayload(
base_model: config.base_model,
data_dir: config.data_dir,
output_dir: config.output_dir,
- // The EXACT bundle the backend advertised, when it named one. Sending only the run folder
- // would let the server re-pick "newest", which in a folder two runs share is not necessarily
- // the one whose step this UI is showing.
+ // The EXACT bundle the backend advertised, when it named one. Sending only the run folder would
+ // let the server re-pick "newest", which in a folder two runs share is not necessarily the one
+ // whose step this UI is showing.
resume_from_checkpoint: detail.checkpoint_path || outputDir,
resumed_from_job_id: detail.job_id,
hf_token: options.hfToken || undefined,
diff --git a/studio/frontend/src/features/images/train/train-base-selector.tsx b/studio/frontend/src/features/images/train/train-base-selector.tsx
index ae5afead92f..cdbfe56ed54 100644
--- a/studio/frontend/src/features/images/train/train-base-selector.tsx
+++ b/studio/frontend/src/features/images/train/train-base-selector.tsx
@@ -21,8 +21,9 @@ export type TrainFamilyOption = {
base_repos: string[];
};
-// The Train tab's base picker, in the top bar where Create shows the generation model. Training never runs on a GGUF, so
-// this lists only the trainable bases the panel's own selects offer, and picking one drives both.
+// The Train tab's base picker, in the top bar where Create shows the generation model. Training
+// never runs on a GGUF, so this lists only the trainable bases the panel's own selects offer,
+// and picking one drives both.
export function TrainBaseSelector({
families,
familyName,
@@ -35,7 +36,8 @@ export function TrainBaseSelector({
onSelect: (family: string, repo: string) => void;
}) {
const family = families.find((f) => f.name === familyName);
- // Before /info answers there is nothing to pick from; the label still reads sensibly. The owner prefix is dropped as the model selector does, family as the description.
+ // Before /info answers there is nothing to pick from; the label still reads sensibly. The owner
+ // prefix is dropped as the model selector does, family as the description.
const label = base ? base.split("/").pop() || base : "Select base model";
return (
diff --git a/studio/frontend/src/features/images/workflows.ts b/studio/frontend/src/features/images/workflows.ts
index 58037d2e76b..ee7c6968681 100644
--- a/studio/frontend/src/features/images/workflows.ts
+++ b/studio/frontend/src/features/images/workflows.ts
@@ -26,8 +26,7 @@ export type WorkflowId =
export const WORKFLOW_TABS: Array<{
id: WorkflowId;
label: string;
- /** Page heading, when the sidebar's short label would read oddly on its own.
- * Falls back to `label`. */
+ /** Page heading, when the sidebar's short label would read oddly on its own. Falls back to `label`. */
heading?: string;
requires: string | null;
icon: IconSvgElement;
diff --git a/studio/frontend/src/features/model-picker/api/llama-flags.ts b/studio/frontend/src/features/model-picker/api/llama-flags.ts
index d1026e77af6..2fad882d222 100644
--- a/studio/frontend/src/features/model-picker/api/llama-flags.ts
+++ b/studio/frontend/src/features/model-picker/api/llama-flags.ts
@@ -13,29 +13,19 @@ export type LlamaFlagCatalog = {
switches: ReadonlySet<string>;
/** Size limit this host applies, which is smaller on Windows. */
maxBytes: number;
- /**
- * Characters the quoted command may take on Windows, or 0 elsewhere. Mirrored
- * because the quoting can double a backslash-heavy value, so bytes alone do not
- * say whether the launch will fit.
- */
+ /** Characters the quoted command may take on Windows, or 0 elsewhere. Mirrored because
+ * quoting can double a backslash-heavy value, so bytes alone do not say whether the launch
+ * will fit. */
windowsCommandBudget: number;
- /**
- * Slots a load gets when it names none, the server-wide --parallel. Needed to
- * judge a pass-through --batch-size: llama-server aborts on a batch below the
- * slots it serves, and with the Slots field blank this is what the launch uses.
- */
+ /** Slots a load gets when it names none, the server-wide --parallel. Needed to judge a
+ * pass-through --batch-size: llama-server aborts on a batch below the slots it serves. */
defaultParallelSlots: number;
- /**
- * True when this build serves ONE slot however many are asked for (no
- * --kv-unified, so load_model falls back). defaultParallelSlots is already
- * effective, but an EXPLICIT Slots value chosen in this panel is not, and sizing
- * the batch floor from it refuses a --batch-size the backend accepts.
- */
+ /** True when this build serves ONE slot however many are asked for (no --kv-unified).
+ * defaultParallelSlots is already effective, but an EXPLICIT Slots value here is not, and
+ * sizing the batch floor from it refuses a --batch-size the backend accepts. */
parallelSlotsClamped: boolean;
- /**
- * False when `--help` could not be read. Nothing may then be reported as a typo:
- * an unverifiable flag is not a wrong one.
- */
+ /** False when `--help` could not be read. Nothing may then be reported as a typo: an
+ * unverifiable flag is not a wrong one. */
probeOk: boolean;
};
@@ -56,22 +46,18 @@ type ApiLlamaFlagCatalog = {
probe_ok?: boolean;
};
-// Describes the binary, so it is worth caching: several rows can mount at once and
-// none of them should pay a separate probe. Not for the whole session though. A
-// llama.cpp update and a backend switch (CUDA to Vulkan, say) both replace the
-// binary in place while the tab stays open, and the flag list moves with it, so a
-// session-long cache would keep reporting a newly added flag as unknown. A minute
-// is short enough that nobody meets that, and long enough that a panel being opened
-// and closed does not re-ask. The backend keys its own cache on the binary's
-// revision, so a repeat read is a dict lookup and not another --help.
+// Describes the binary, so it is worth caching across the several rows that can mount at
+// once. Not for the whole session: a llama.cpp update or a backend switch replaces the
+// binary in place while the tab stays open, so a session-long cache would report a newly
+// added flag as unknown. A minute is short enough that nobody meets that. The backend keys
+// its own cache on the binary's revision, so a repeat read is a dict lookup.
const CATALOG_TTL_MS = 60_000;
let inFlightCatalog: Promise<LlamaFlagCatalog | null> | null = null;
let cachedCatalog: LlamaFlagCatalog | null = null;
let cachedAt = 0;
-// Bumped by every invalidation. A read that started before the binary changed is
-// describing the old one, so it must neither be cached nor handed to a caller that
-// asked after the change: without this the stale promise is reused by the next call
-// and writes the previous backend's flags back for the whole TTL.
+// Bumped by every invalidation. A read that started before the binary changed describes the
+// old one, so it must neither be cached nor handed to a later caller: without this the
+// stale promise writes the previous backend's flags back for the whole TTL.
let catalogGeneration = 0;
/** What a caller gets without paying for the --help probe. */
@@ -80,12 +66,9 @@ export type LlamaManagedFlags = {
maxBytes: number;
windowsCommandBudget: number;
defaultParallelSlots: number;
- /**
- * True when this build serves ONE slot however many are asked for (no
- * --kv-unified, so load_model falls back). defaultParallelSlots is already
- * effective, but an EXPLICIT Slots value chosen in this panel is not, and sizing
- * the batch floor from it refuses a --batch-size the backend accepts.
- */
+ /** True when this build serves ONE slot however many are asked for (no --kv-unified). An
+ * EXPLICIT Slots value chosen in this panel is not effective, and sizing the batch floor
+ * from it refuses a --batch-size the backend accepts. */
parallelSlotsClamped: boolean;
};
@@ -94,14 +77,9 @@ let cachedManaged: LlamaManagedFlags | null = null;
const catalogListeners = new Set<() => void>();
-/**
- * Told when the catalogue is invalidated, for a consumer holding one in state.
- *
- * Clearing the module cache is not enough for a row that already read it: updating
- * llama.cpp from the banner replaces the binary while the panel stays mounted, and
- * the row would go on judging arity against the old build's help until the section
- * is closed and reopened.
- */
+/** Told when the catalogue is invalidated, for a consumer holding one in state. Clearing the
+ * module cache is not enough for a row that already read it: updating llama.cpp from the
+ * banner replaces the binary while the panel stays mounted. */
export function subscribeLlamaFlagCatalog(listener: () => void): () => void {
catalogListeners.add(listener);
return () => {
@@ -114,15 +92,12 @@ export function invalidateLlamaFlagCatalog(): void {
cachedCatalog = null;
cachedAt = 0;
catalogGeneration += 1;
- // Dropped as well as cleared: a request already on the wire answers for the
- // binary that has just been replaced.
+ // Dropped as well as cleared: a request already on the wire answers for the binary that has just been replaced.
inFlightCatalog = null;
- // The managed answer too. Its denylist is Unsloth's own and no binary changes it,
- // but it carries defaultParallelSlots beside it, and that is the EFFECTIVE count:
- // a build without --kv-unified serves one slot however many are configured. A tab
- // that had already fetched it went on sizing the hidden hydration check's batch
- // floor from the previous backend, letting "--batch-size 2" through on a build
- // that now serves four slots, or refusing it after a clamp to one.
+ // The managed answer too. Its denylist is Unsloth's own, but it carries defaultParallelSlots,
+ // which is the EFFECTIVE count: a build without --kv-unified serves one slot however many
+ // are configured. A tab that had already fetched it went on sizing the hydration check's
+ // batch floor from the previous backend.
cachedManaged = null;
inFlightManaged = null;
for (const listener of catalogListeners) {
@@ -130,14 +105,10 @@ export function invalidateLlamaFlagCatalog(): void {
}
}
-/**
- * Just the flags Unsloth refuses, without the `--help` probe behind the catalogue.
- *
- * The panel sanitizes a stored list with this before turning it into an explicit
- * request, and that must not wait on a cold probe (up to ten seconds), or a flag
- * denied since the list was saved stays in the request for as long as it runs.
- * Cached for the session: unlike the flag map, it describes this build of Unsloth.
- */
+/** Just the flags Unsloth refuses, without the `--help` probe behind the catalogue. The panel
+ * sanitizes a stored list with this before making it an explicit request, and that must not
+ * wait on a cold probe (up to ten seconds). Cached for the session: unlike the flag map,
+ * it describes this build of Unsloth. */
export function loadManagedLlamaFlags(): Promise<LlamaManagedFlags | null> {
if (cachedManaged) {
return Promise.resolve(cachedManaged);
@@ -145,12 +116,10 @@ export function loadManagedLlamaFlags(): Promise<LlamaManagedFlags | null> {
if (cachedCatalog) {
return Promise.resolve(cachedCatalog);
}
- // Read before the request goes out, and checked before its answer is published,
- // exactly as the full catalogue does. The denylist in this answer is Unsloth's own
- // and no binary changes it, but defaultParallelSlots beside it is the EFFECTIVE
- // count, which depends on the probed binary: a request already on the wire when
- // llama.cpp is replaced would otherwise repopulate the cache the invalidation had
- // just cleared, and keep the old build's slot floor for the rest of the session.
+ // Read before the request goes out and checked before its answer is published, as the full
+ // catalogue does. defaultParallelSlots in this answer is the EFFECTIVE count and depends
+ // on the probed binary: a request already on the wire when llama.cpp is replaced would
+ // repopulate the cache the invalidation had just cleared.
const generation = catalogGeneration;
inFlightManaged ??= (async () => {
try {
@@ -161,22 +130,22 @@ export function loadManagedLlamaFlags(): Promise<LlamaManagedFlags | null> {
return null;
}
const body = (await res.json()) as ApiLlamaFlagCatalog;
- // An older backend answers the route without the parameter and returns its
- // full catalogue, which carries the same list.
+ // An older backend answers the route without the parameter and returns its full catalogue,
+ // which carries the same list.
const managed: LlamaManagedFlags = {
managed: new Set(body.managed ?? []),
maxBytes: body.max_bytes ?? 0,
windowsCommandBudget: body.windows_command_budget ?? 0,
- // 0 on a backend that predates the field, which reads as "not known" and
- // leaves the editor's own hard floor of 2 in charge.
+ // 0 on a backend that predates the field, which reads as "not known" and leaves the editor's
+ // own hard floor of 2 in charge.
defaultParallelSlots: body.default_parallel_slots ?? 0,
- // Absent on a backend that predates the field, and false is the safe read:
- // the floor then follows the asked-for count, exactly as it did before.
+ // Absent on a backend that predates the field, and false is the safe read: the floor then
+ // follows the asked-for count, as it did before.
parallelSlotsClamped: Boolean(body.parallel_slots_clamped),
};
if (generation !== catalogGeneration) {
- // The binary changed while this was in flight: answer "cannot verify"
- // rather than the old build's limits, and cache nothing.
+ // The binary changed while this was in flight: answer "cannot verify" rather than the old
+ // build's limits, and cache nothing.
return null;
}
cachedManaged = managed;
@@ -184,8 +153,8 @@ export function loadManagedLlamaFlags(): Promise<LlamaManagedFlags | null> {
} catch {
return null;
} finally {
- // Only if it is still ours: an invalidation may have cleared it already, and
- // a later call may have started its own.
+ // Only if it is still ours: an invalidation may have cleared it already, and a later call
+ // may have started its own.
if (generation === catalogGeneration) {
inFlightManaged = null;
}
@@ -194,12 +163,9 @@ export function loadManagedLlamaFlags(): Promise<LlamaManagedFlags | null> {
return inFlightManaged;
}
-/**
- * Read the flag catalogue, or null when this backend has no such route.
- *
- * Null and `probeOk: false` mean the same thing to the caller (cannot verify), but
- * they are kept apart so an older backend is not reported as a broken llama.cpp.
- */
+/** Read the flag catalogue, or null when this backend has no such route. Null and
+ * `probeOk: false` both mean "cannot verify" but are kept apart so an older backend is not
+ * reported as a broken llama.cpp. */
export function loadLlamaFlagCatalog(): Promise<LlamaFlagCatalog | null> {
if (cachedCatalog && Date.now() - cachedAt < CATALOG_TTL_MS) {
return Promise.resolve(cachedCatalog);
@@ -216,19 +182,19 @@ export function loadLlamaFlagCatalog(): Promise<LlamaFlagCatalog | null> {
flags: body.flags ?? {},
managed: new Set(body.managed ?? []),
switches: new Set(body.switch_flags ?? []),
- // An older backend sends neither, and 0 reads as "no limit of my own",
- // leaving the editor's own default in charge.
+ // An older backend sends neither, and 0 reads as "no limit of my own", leaving the editor's
+ // own default in charge.
maxBytes: body.max_bytes ?? 0,
windowsCommandBudget: body.windows_command_budget ?? 0,
- // 0 on a backend that predates the field, which reads as "not known" and
- // leaves the editor's own hard floor of 2 in charge.
+ // 0 on a backend that predates the field, which reads as "not known" and leaves the editor's
+ // own hard floor of 2 in charge.
defaultParallelSlots: body.default_parallel_slots ?? 0,
parallelSlotsClamped: Boolean(body.parallel_slots_clamped),
probeOk: Boolean(body.probe_ok),
};
if (generation !== catalogGeneration) {
- // The binary changed while this was in flight: answer the caller with
- // "cannot verify" rather than the old build's flags, and cache nothing.
+ // The binary changed while this was in flight: answer "cannot verify" rather than the old
+ // build's flags, and cache nothing.
return null;
}
cachedCatalog = catalog;
@@ -237,8 +203,8 @@ export function loadLlamaFlagCatalog(): Promise<LlamaFlagCatalog | null> {
} catch {
return null;
} finally {
- // Only if it is still ours: an invalidation may have cleared it already, and
- // a later call may have started its own.
+ // Only if it is still ours: an invalidation may have cleared it already, and a later call
+ // may have started its own.
if (generation === catalogGeneration) {
inFlightCatalog = null;
}
diff --git a/studio/frontend/src/features/model-picker/api/memory-estimate.ts b/studio/frontend/src/features/model-picker/api/memory-estimate.ts
index cd30046819b..36e41eafef4 100644
--- a/studio/frontend/src/features/model-picker/api/memory-estimate.ts
+++ b/studio/frontend/src/features/model-picker/api/memory-estimate.ts
@@ -4,9 +4,8 @@
import { authFetch } from "@/features/auth";
import { consumeNativePathToken } from "@/features/native-intents/api";
-/** GB, to two decimals. Lives in the import-free module beside the fit rules so the
- * node test runner can reach it; re-exported here because every caller of the row's
- * figures already imports from this file. */
+/** GB, to two decimals. Lives in the import-free module beside the fit rules so the node test
+ * runner can reach it; re-exported here because every caller already imports this file. */
export { formatMemoryGb } from "../model-config/memory-fit";
/** Why no breakdown came back. The panel maps these to its own copy. */
@@ -27,14 +26,14 @@ export interface MemoryEstimate {
computeBytes: number;
/** A separate drafter's own cache and rollback state, on top of its file. */
drafterRuntimeBytes: number;
- /** The share of the above that lands on the GPU. A figure rather than a flag: under
- * MTP the target-side verification state follows the TARGET cache and the draft
- * cache follows the drafter, so the two halves can be placed differently. */
+ /** The share of the above that lands on the GPU. A figure rather than a flag: under MTP the
+ * target-side verification state follows the TARGET cache and the draft cache follows the
+ * drafter, so the two halves can be placed differently. */
drafterRuntimeGpuBytes: number;
/** The vision encoder's buffers, about 0.4x the projector file on top of it. */
projectorRuntimeBytes: number;
- /** A charged drafter whose cache could not be sized: `--spec-draft-hf` names a
- * repository, so its header is not on this disk. The total is a floor. */
+ /** A charged drafter whose cache could not be sized: `--spec-draft-hf` names a repository, so
+ * its header is not on this disk. The total is a floor. */
drafterKvUnsized: boolean;
/** A pass-through adapter file that could not be sized, so the total is a floor. */
adaptersUnsized: boolean;
@@ -42,11 +41,8 @@ export interface MemoryEstimate {
totalBytes: number;
/** The share of `totalBytes` that lands on the GPU under the requested offload. */
gpuBytes: number;
- /**
- * False when the GGUF header lacks the attention dims needed to size the cache. The
- * numbers above are then a lower bound, not an estimate: at a long context the cache
- * is most of the footprint.
- */
+ /** False when the GGUF header lacks the attention dims needed to size the cache. The numbers
+ * above are then a lower bound, not an estimate: at a long context the cache dominates. */
kvEstimable: boolean;
/** False under `--no-kv-offload`, which moves the cache to host RAM. */
kvOnGpu: boolean;
@@ -159,23 +155,19 @@ function estimateRequestBody(
});
}
-/**
- * A byte count the row can show, or the fallback.
- *
- * `??` defends against null and undefined, not against a value: JSON.parse turns
- * `1e999` into Infinity, a field can arrive stringified, and a negative byte count is
- * not a footprint. All three reach classifyMemoryFit, where NaN used to come back
- * "fits". Both skew fallbacks are preserved: an ABSENT key falls through to the
- * caller's fallback, and an explicit 0 is a real answer.
- */
+/** A byte count the row can show, or the fallback. `??` defends against null and undefined,
+ * not against a value: JSON.parse turns `1e999` into Infinity, a field can arrive
+ * stringified, and a negative byte count is not a footprint. All three reach
+ * classifyMemoryFit, where NaN used to come back "fits". Both skew fallbacks are preserved:
+ * an ABSENT key falls through to the caller's fallback, and an explicit 0 is a real answer. */
function finiteBytes(value: unknown, fallback: number): number {
return typeof value === "number" && Number.isFinite(value) && value >= 0
? value
: fallback;
}
-/** A count (context, slots, layers) rather than a byte size: same guard, no fallback
- * chain, and null stays null where null is the "unknown" the row prints. */
+/** A count (context, slots, layers) rather than a byte size: same guard, no fallback chain, and
+ * null stays null where null is the "unknown" the row prints. */
function finiteCount(value: unknown, fallback: number): number {
return typeof value === "number" && Number.isFinite(value) && value >= 0
? Math.trunc(value)
@@ -188,8 +180,8 @@ function nullableCount(value: unknown): number | null {
: null;
}
-/** A flag whose ABSENCE is meaningful, so a non-boolean is treated as absent rather
- * than coerced: `Boolean("false")` is true, and these decide what the row claims. */
+/** A flag whose ABSENCE is meaningful, so a non-boolean is treated as absent rather than
+ * coerced: `Boolean("false")` is true, and these decide what the row claims. */
function flag(value: unknown, fallback: boolean): boolean {
return typeof value === "boolean" ? value : fallback;
}
@@ -205,8 +197,8 @@ function toMemoryEstimate(body: ApiEstimateResponse): MemoryEstimate {
const drafterRuntimeBytes = finiteBytes(body.drafter_runtime_bytes, 0);
return {
available: flag(body.available, false),
- // A reason the panel has no copy for is not a reason. An unknown string would
- // reach the copy map and render nothing at all.
+ // A reason the panel has no copy for is not a reason. An unknown string would reach the copy
+ // map and render nothing at all.
reason: ESTIMATE_REASONS.includes(body.reason as MemoryEstimateReason)
? (body.reason as MemoryEstimateReason)
: null,
@@ -214,22 +206,22 @@ function toMemoryEstimate(body: ApiEstimateResponse): MemoryEstimate {
kvBytes: finiteBytes(body.kv_bytes, 0),
computeBytes: finiteBytes(body.compute_bytes, 0),
drafterRuntimeBytes,
- // Absent on a backend predating the split: fall back to the whole term, which
- // keeps the old "all of it is on the GPU" reading rather than inventing a zero
- // that would silently drop a real VRAM charge off the row.
+ // Absent on a backend predating the split: fall back to the whole term, which keeps the old
+ // "all of it is on the GPU" reading rather than inventing a zero that would drop a real
+ // VRAM charge off the row.
drafterRuntimeGpuBytes: finiteBytes(
body.drafter_runtime_gpu_bytes,
drafterRuntimeBytes,
),
projectorRuntimeBytes: finiteBytes(body.projector_runtime_bytes, 0),
drafterKvUnsized: flag(body.drafter_kv_unsized, false),
- // Absent on a backend that predates the adapter term, and false is the right
- // reading there: it charged no adapters, so it claimed no floor.
+ // Absent on a backend that predates the adapter term, and false is the right reading there:
+ // it charged no adapters, so it claimed no floor.
adaptersUnsized: flag(body.adapters_unsized, false),
totalBytes: finiteBytes(body.total_bytes, 0),
gpuBytes: finiteBytes(body.gpu_bytes, 0),
- // Absent on an older backend: treat the KV figure as unverified, the safe
- // direction for the one number that can dwarf all the others.
+ // Absent on an older backend: treat the KV figure as unverified, the safe direction for the
+ // one number that can dwarf all the others.
kvEstimable: flag(body.kv_estimable, false),
kvOnGpu: flag(body.kv_on_gpu, true),
nCtx: finiteCount(body.n_ctx, 0),
@@ -242,48 +234,32 @@ function toMemoryEstimate(body: ApiEstimateResponse): MemoryEstimate {
};
}
-/**
- * Statuses that say the ROUTE is not there, as distinct from this request failing.
- *
- * 404 and 405 are a backend predating it (405 because a router that owns the path for
- * another method answers that instead), 501 one that answers but declines. Everything
- * else -- 401, 422, 500, a gateway error, a body that is not JSON -- is about this
- * request or this moment, and says nothing about whether the route exists.
- */
+/** Statuses that say the ROUTE is not there, as distinct from this request failing. 404 and
+ * 405 are a backend predating it (405 because a router owning the path for another method
+ * answers that), 501 one that answers but declines. Everything else -- 401, 422, 500, a
+ * gateway error, a non-JSON body -- is about this request, not the route's existence. */
function routeAbsentStatus(status: number): boolean {
return status === 404 || status === 405 || status === 501;
}
-/**
- * How long a structural miss is trusted before the next qualifying change re-probes.
- *
- * Worth memoing: this route is POSTed after EVERY settings change, so a new bundle
- * against an old backend fires one debounced request per slider release for the life
- * of the tab, each landing in the API monitor as a 404.
- *
- * Not PERMANENT, though. Studio replaces its own backend in place, and a latched miss
- * would keep the row hidden on a backend that has since gained the route until the
- * window reloads. A TTL costs one wasted POST per window and heals with no plumbing to
- * a restart signal.
- */
+/** How long a structural miss is trusted before the next qualifying change re-probes. Worth
+ * memoing: this route is POSTed after EVERY settings change, so a new bundle against an old
+ * backend fires one debounced 404 per slider release for the life of the tab. Not permanent,
+ * though: Studio replaces its own backend in place, and a latched miss would keep the row
+ * hidden until reload. A TTL costs one wasted POST per window and needs no restart signal. */
const ROUTE_ABSENT_TTL_MS = 5 * 60 * 1000;
let routeAbsentAt: number | null = null;
-/** Forget a recorded miss. For tests, and for any caller that learns the backend
- * changed underneath it before the window is up. */
+/** Forget a recorded miss. For tests, and for any caller that learns the backend changed
+ * underneath it before the window is up. */
export function resetMemoryEstimateRouteMemo(): void {
routeAbsentAt = null;
}
-/**
- * Price a prospective GGUF load from its header. Allocates nothing, loads nothing.
- *
- * Never throws for a backend answer: every failure -- an absent route, an auth
- * expiry, a 500, a proxy's HTML error page served as 200, a truncated body -- comes
- * back as an unavailable estimate, so the panel hides the row and the Load button
- * beside it is untouched. The statuses are told apart only to decide whether the miss
- * is worth remembering; the row's own reading of them is identical.
- */
+/** Price a prospective GGUF load from its header. Allocates nothing, loads nothing. Never
+ * throws for a backend answer: an absent route, an auth expiry, a 500, an HTML error page
+ * served as 200 or a truncated body all come back as an unavailable estimate, so the panel
+ * hides the row. The statuses are told apart only to decide whether the miss is memoable. */
export async function fetchMemoryEstimate(
payload: MemoryEstimateRequest,
signal?: AbortSignal,
@@ -312,9 +288,8 @@ export async function fetchMemoryEstimate(
body: estimateRequestBody(payload, nativePathLease),
});
if (!response.ok) {
- // Only a structural miss latches. A transient 500 explicitly CLEARS the memo
- // rather than leaving an older one standing: a backend that is answering at all,
- // however badly, is not one that is missing the route.
+ // Only a structural miss latches. A transient 500 explicitly CLEARS the memo rather than
+ // leaving an older one standing: a backend answering at all is not one missing the route.
routeAbsentAt = routeAbsentStatus(response.status) ? Date.now() : null;
return UNAVAILABLE;
}
@@ -323,8 +298,8 @@ export async function fetchMemoryEstimate(
try {
body = await response.json();
} catch {
- // A 200 that is not JSON: a captive portal or a dev proxy serving its own HTML,
- // or a body cut short. Nothing was measured, so there is nothing to show.
+ // A 200 that is not JSON: a captive portal, a dev proxy serving its own HTML, or a body cut
+ // short. Nothing was measured, so there is nothing to show.
return UNAVAILABLE;
}
if (typeof body !== "object" || body === null) {
diff --git a/studio/frontend/src/features/model-picker/api/migrate-model-overrides.ts b/studio/frontend/src/features/model-picker/api/migrate-model-overrides.ts
index 58771023ce2..d3e22ad9bee 100644
--- a/studio/frontend/src/features/model-picker/api/migrate-model-overrides.ts
+++ b/studio/frontend/src/features/model-picker/api/migrate-model-overrides.ts
@@ -42,11 +42,10 @@ function markRan(): void {
}
}
-/**
-* A server key under the same identity this browser stores. `app_settings` has no schema
-* version, so an old install holds keys the backend resolves to this model while an exact
-* lookup calls them missing and overwrites them. The split folds repo ids while preserving
-* POSIX path identity. */
+/** A server key under the same identity this browser stores. `app_settings` has no schema
+ * version, so an old install holds keys the backend resolves to this model while an exact
+ * lookup calls them missing and overwrites them. The split folds repo ids while preserving
+ * POSIX path identity. */
function normalizedOverrideKey(key: string): string {
const split = splitQuantSuffix(key);
if (!split) {
@@ -58,9 +57,8 @@ function normalizedOverrideKey(key: string): string {
);
}
-/**
-* The fields *config* would contribute that the stored entry does not hold. A malformed entry
-* (nothing constrains what an older install wrote) counts as holding nothing. */
+/** The fields *config* would contribute that the stored entry does not hold. A malformed entry
+ * (nothing constrains what an older install wrote) counts as holding nothing. */
function absentFields(
stored: ApiModelOverride,
config: Parameters<typeof toApiOverride>[0],
@@ -72,10 +70,9 @@ function absentFields(
return fields.filter((field) => !(field in stored));
}
-/**
-* Push local settings the server does not hold. Never deletes and never overwrites: a value
-* already there is the newer authority. Field by field, not entry by entry, since a legacy
-* entry holds only llama_extra_args and max_seq_length. */
+/** Push local settings the server does not hold. Never deletes and never overwrites: a value
+ * already there is the newer authority. Field by field, not entry by entry, since a legacy
+ * entry holds only llama_extra_args and max_seq_length. */
export async function backfillModelOverrides(): Promise<void> {
if (alreadyRan()) {
return;
diff --git a/studio/frontend/src/features/model-picker/api/model-overrides.ts b/studio/frontend/src/features/model-picker/api/model-overrides.ts
index aee92e2fe48..d92acbcba7f 100644
--- a/studio/frontend/src/features/model-picker/api/model-overrides.ts
+++ b/studio/frontend/src/features/model-picker/api/model-overrides.ts
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Server-side mirror of the per-model config. ../model-config/per-model-config.ts lives in
-// browser localStorage, so an API auto-switch load (no browser in the loop) came up with none of
-// the user's settings. routes/inference.py reads this map and rebuilds the picker's LoadRequest.
+// Server-side mirror of the per-model config. per-model-config.ts lives in browser
+// localStorage, so an API auto-switch load came up with none of the user's settings.
+// routes/inference.py reads this map and rebuilds the picker's LoadRequest.
import { authFetch } from "@/features/auth";
import { readFastApiError } from "@/lib/format-fastapi-error";
@@ -69,11 +69,8 @@ export interface ApiModelOverride {
export type ApiModelOverrides = Record<string, ApiModelOverride>;
-/**
- * The key one model's config is stored under: the `repo:VARIANT` form an OpenAI
- * request names a quant by, so two quants of one repo keep separate configs and the
- * backend matches the requested name directly. Bare id when there is no variant.
- */
+/** The key one model's config is stored under: the `repo:VARIANT` form an OpenAI request
+ * names a quant by, so two quants of one repo keep separate configs. Bare id if no variant. */
export function modelOverrideKey(
modelId: string,
ggufVariant?: string | null,
@@ -81,16 +78,10 @@ export function modelOverrideKey(
return ggufVariant ? `${modelId}:${ggufVariant}` : modelId;
}
-/**
- * A path that names one file whatever the casing, folded for comparison, or null
- * when the path is case-sensitive.
- *
- * The rules are _fold_case_insensitive_path's, and they have to be, because the
- * server applies an override this resolver has to find: a Windows drive path, a UNC
- * share and a WSL drive mount all fold, the separator is interchangeable, and
- * trailing separators are trimmed down to the root. A POSIX path is not folded, or
- * "/models/Foo.gguf" would collect the arguments stored for "/models/foo.gguf".
- */
+/** A path that names one file whatever the casing, folded for comparison, or null when the
+ * path is case-sensitive. Rules are _fold_case_insensitive_path's and must be, since the
+ * server applies an override this resolver has to find. A POSIX path is not folded, or
+ * "/models/Foo.gguf" would collect the arguments stored for "/models/foo.gguf". */
const WINDOWS_DRIVE_PATH = /^[a-zA-Z]:[\\/]/;
const WSL_DRIVE_PATH = /^\/mnt\/[a-zA-Z](\/|$)/;
@@ -120,28 +111,20 @@ export function foldOverrideKey(key: string): string {
return path;
}
// splitQuantSuffix, not the last colon: a colon is legal in a POSIX filename, so
- // "/models/foo:Bar.gguf" is a whole path and reading "Bar.gguf" as a quant would
- // fold it onto the real, different file "/models/foo:bar.gguf". This is the check
- // the backend's split_quant_suffix makes.
+ // "/models/foo:Bar.gguf" is a whole path and reading "Bar.gguf" as a quant would fold it
+ // onto a different real file. Mirrors the backend's split_quant_suffix.
const split = splitQuantSuffix(key);
const id = split ? split[0] : key;
const quant = split ? `:${split[1].toLowerCase()}` : "";
- // A POSIX path keeps its case; only the quant folds, because the browser
- // lowercases that before storing. A repo id folds whole.
+ // A POSIX path keeps its case; only the quant folds, because the browser lowercases that
+ // before storing. A repo id folds whole.
return id.startsWith("/") ? `${id}${quant}` : `${id.toLowerCase()}${quant}`;
}
-/**
- * The stored arguments, with "the row carried an empty list" kept apart from "no
- * row carried the field at all".
- *
- * The distinction is what the settings page's tombstone is for: clearing the box
- * for one quant saves an explicit [], and that is what stops the server's lookup
- * before it reaches a legacy bare-repository row that still holds arguments.
- * Collapsing the two left the panel with llamaExtraArgs undefined, its next Load
- * omitted the field, and /load carried the resident model's arguments over: the
- * very ones that had just been cleared.
- */
+/** The stored arguments, keeping "the row carried an empty list" apart from "no row carried
+ * the field". That is what the settings page's tombstone is for: clearing the box saves an
+ * explicit [], which stops the lookup before a legacy bare-repository row. Collapsing the
+ * two left llamaExtraArgs undefined and /load carried the cleared arguments over. */
export type ResolvedExtraArgs = {
tokens: string[];
explicit: boolean;
@@ -163,25 +146,21 @@ export function resolveStoredOverride(
overrides: ApiModelOverrides,
keys: readonly string[],
): ApiModelOverride | null {
- // The overrides route folds identities before it reads a row: a repo id and a
- // quant differing only in case resolve to the same entry, and it falls back from
- // `repo:QUANT` to the bare repo. Keys are tried most specific first, then folded.
- // Whole ENTRIES, in the order the backend tries them, stopping at the first one
- // that exists. The auto-switch loader breaks on the first non-empty override and
- // reads its fields from there, so falling through to a bare repo id because the
- // variant row happens to carry no arguments would launch the picker with flags an
- // API load would not use.
- // An entry with no fields is skipped rather than stopping the search, because
- // that is what `if override: break` does on the server.
+ // The overrides route folds identities before reading a row and falls back from `repo:QUANT`
+ // to the bare repo. Keys are tried most specific first, then folded, as whole ENTRIES,
+ // stopping at the first that exists. The auto-switch loader breaks on the first non-empty
+ // override, so falling through to a bare repo because the variant row carries no arguments
+ // would launch with flags an API load would not use. An entry with no fields is skipped,
+ // because that is what `if override: break` does on the server.
const folded = new Map<string, ApiModelOverride | null>();
for (const [key, value] of Object.entries(overrides)) {
if (!presentOverride(value)) {
continue;
}
const foldedKey = foldOverrideKey(key);
- // Ambiguous folds resolve to nothing, as resolve_model_override_key does:
- // duplicate case variants left by an upgrade must not have one of them picked
- // at enumeration order, which is another model's settings half the time.
+ // Ambiguous folds resolve to nothing, as resolve_model_override_key does: duplicate case
+ // variants left by an upgrade must not be picked at enumeration order, which is another
+ // model's settings half the time.
folded.set(foldedKey, folded.has(foldedKey) ? null : value);
}
for (const key of keys) {
@@ -189,11 +168,9 @@ export function resolveStoredOverride(
if (exact) {
return exact;
}
- // Folding, by the same rule the backend resolves with: a POSIX path is
- // case-sensitive, so lowercasing one whole would hand /models/foo.gguf the
- // arguments stored for /models/Foo.gguf and then send them explicitly on Load.
- // Windows, UNC and WSL paths do fold, and so does the quant suffix the browser
- // lowercases before storing.
+ // Folding by the backend's rule: a POSIX path is case-sensitive, so lowercasing one whole
+ // would hand /models/foo.gguf the arguments stored for /models/Foo.gguf. Windows, UNC and
+ // WSL paths do fold, as does the quant suffix the browser lowercases before storing.
const match = folded.get(foldOverrideKey(key));
if (match) {
return match;
@@ -221,18 +198,11 @@ export async function fetchModelOverrides(): Promise<ApiModelOverrides> {
return body.overrides ?? {};
}
-/**
- * The pass-through arguments the LOAD of this model would apply.
- *
- * Asked of the server rather than worked out here, because the resolution is
- * Python's: casefold is not toLowerCase (Straße and STRASSE are one path to the
- * loader and two to a browser), and an ambiguous fold deliberately matches nothing.
- * A client mirroring those rules can only approximate them, and the approximation
- * shows up as a cold load launching without arguments an API load applies.
- *
- * Falls back to resolving locally against the whole map when the backend predates
- * the parameter, which is the same answer in every case but the exotic ones.
- */
+/** The pass-through arguments the LOAD of this model would apply. Asked of the server rather
+ * than derived here, because the resolution is Python's: casefold is not toLowerCase, and
+ * an ambiguous fold deliberately matches nothing. A client can only approximate that, and
+ * the approximation shows as a cold load launching without arguments an API load applies.
+ * Falls back to resolving locally when the backend predates the parameter. */
export async function fetchLoadModelOverride(
loadId: string,
aliasId: string,
@@ -258,12 +228,10 @@ export async function fetchLoadModelOverride(
if (body.resolved !== undefined) {
return presentOverride(body.resolved);
}
- // A backend that predates the resolved field answers with the whole map, and the
- // caller has to say which keys to look under. Derived here when it did not: the
- // panel passes its own richer list (it knows the alias the settings page used),
- // while the auto-load and compare callers have only these two identities, and
- // defaulting to none made them read an empty map and launch without the stored
- // arguments against an older server.
+ // A backend that predates the resolved field answers with the whole map and needs to be told
+ // which keys to look under. Derived here when it was not: the panel passes its own richer
+ // list, while the auto-load and compare callers have only these two identities, and
+ // defaulting to none made them read an empty map against an older server.
const derived =
fallbackKeys.length > 0
? fallbackKeys
@@ -288,37 +256,29 @@ export async function fetchLoadExtraArgs(
ggufVariant,
fallbackKeys,
);
- // An explicit empty list is a cleared box, not an absence, and the caller has
- // to send it as one: omitting the field lets /load carry the resident model's
- // arguments over, which is exactly what was cleared.
+ // An explicit empty list is a cleared box, not an absence, and must be sent as one: omitting
+ // the field lets /load carry the resident model's arguments over.
return resolvedFrom(resolved ?? {});
}
-/**
- * Translate one server-resolved override into the config shape the picker uses.
- *
- * The row is authoritative for the fields it CARRIES, and only for those. An absent
- * field is not evidence the user chose the default: the mirror is best-effort and
- * lossy, so a PUT that never landed, a value this build's normalizer refused, and a
- * row written before a field reached the route all leave the same gap. `localConfig`
- * fills it, or opening the panel would delete settings the user typed here -- a
- * migrated legacy config, or one whose mirror failed, came back as bare defaults and
- * was then persisted over the original. The cost is that clearing ONE field on another
- * origin does not travel until this one saves again, which the schema cannot express
- * anyway: an omitted field and an app default are written the same way.
- */
+/** Translate one server-resolved override into the picker's config shape. The row is
+ * authoritative for the fields it CARRIES and only those: an absent field is not evidence
+ * the user chose the default, since a failed PUT, a refused value and an old row all leave
+ * the same gap. `localConfig` fills it, or opening the panel would delete settings typed
+ * here. The cost is that clearing ONE field on another origin does not travel until this
+ * one saves again, which the schema cannot express anyway. */
export function fromApiOverride(
override: ApiModelOverride,
localConfig?: PerModelConfig,
): PerModelConfig {
const local = localConfig ?? DEFAULT_PER_MODEL_CONFIG;
- // Three states, so the winning source is chosen once and restored below: the row's
- // list when it has one, else whatever this browser held.
+ // Three states, so the winning source is chosen once and restored below: the row's list when
+ // it has one, else whatever this browser held.
const extraArgs = Array.isArray(override.llama_extra_args)
? override.llama_extra_args
: local.llamaExtraArgs;
- // Only a physical pin travels (toApiOverride drops the rest), so a row without ids
- // says nothing about placement and a local Vulkan ordinal keeps its own namespace.
+ // Only a physical pin travels (toApiOverride drops the rest), so a row without ids says
+ // nothing about placement and a local Vulkan ordinal keeps its own namespace.
const serverGpuIds = override.gpu_ids?.length ? override.gpu_ids : null;
// The pin is ONE setting in one of two fields, and an edit clears the other
// (contextPinPatch). Filling them from different sources mints a record that loads at
@@ -360,21 +320,17 @@ export function fromApiOverride(
? "physical"
: (local.selectedGpuIndexKind ?? null),
});
- // normalizePerModelConfig intentionally collapses an empty list to null. The server
- // uses [] as a tombstone that stops fallback to a broader override, so hydration
- // must retain that third state.
+ // normalizePerModelConfig collapses an empty list to null. The server uses [] as a tombstone
+ // that stops fallback to a broader override, so hydration must retain that third state.
if (Array.isArray(extraArgs)) {
normalized.llamaExtraArgs = [...extraArgs];
}
return normalized;
}
-/**
- * Translate the UI's per-model config into the backend's schema. Only fields the user set are
- * sent: an absent field reads as "app default", so nulls would pin defaults and stop the model
- * following later global changes. A `null` config clears the entry. Exported so the backfill
- * can compare it against the stored entry.
- */
+/** Translate the UI's per-model config into the backend's schema. Only fields the user set are
+ * sent: an absent field reads as "app default", so nulls would pin defaults and stop the
+ * model following later global changes. A `null` config clears the entry. */
export function toApiOverride(config: PerModelConfig | null): ApiModelOverride {
if (!config) {
return {};
@@ -389,8 +345,7 @@ export function toApiOverride(config: PerModelConfig | null): ApiModelOverride {
if (config.kvCacheDtype) {
payload.kv_cache_dtype = config.kvCacheDtype;
}
- // Travels beside kv_cache_dtype, or an API auto-switch loads a remembered MLX
- // model at full precision.
+ // Travels beside kv_cache_dtype, or an API auto-switch loads a remembered MLX model at full precision.
if (config.mlxKvBits != null) {
payload.mlx_kv_bits = config.mlxKvBits;
}
@@ -411,9 +366,8 @@ export function toApiOverride(config: PerModelConfig | null): ApiModelOverride {
if (config.nUbatch && config.nUbatch > 0) {
payload.n_ubatch = config.nUbatch;
}
- // Blank follows llama.cpp's own default for each: auto, f16, 32 and 8192.
- // cacheRam is compared against null rather than tested for truth, because 0 and
- // -1 are both meaningful values here (disable, and no limit).
+ // Blank follows llama.cpp's own default for each: auto, f16, 32 and 8192. cacheRam is
+ // compared against null rather than truth, since 0 and -1 both mean something.
if (config.loadMode) {
payload.load_mode = config.loadMode;
}
@@ -436,9 +390,8 @@ export function toApiOverride(config: PerModelConfig | null): ApiModelOverride {
payload.chat_template_override = config.chatTemplateOverride;
}
// The one field where absent does NOT mean "app default": the route preserves
- // llama_extra_args it is not sent, which is what kept CLI-set flags alive while
- // this panel had no control for them. So `undefined` (never read) stays omitted,
- // and a cleared box has to say so with an explicit empty list.
+ // llama_extra_args it is not sent, which kept CLI-set flags alive while this panel had no
+ // control. So `undefined` stays omitted and a cleared box sends an explicit empty list.
if (config.llamaExtraArgs !== undefined) {
payload.llama_extra_args = config.llamaExtraArgs ?? [];
}
@@ -454,9 +407,8 @@ export function toApiOverride(config: PerModelConfig | null): ApiModelOverride {
payload.n_cpu_moe = config.nCpuMoe;
}
// Only a physical pin travels. The same integers are a Vulkan ordinal under Vulkan and a
- // CUDA/ROCm index elsewhere, and the override carries no namespace, so after a backend change
- // the server would pin a different device with ids its availability check accepts. An absent
- // kind predates the field, which was physical-only. Dropping the pin leaves auto placement.
+ // CUDA/ROCm index elsewhere, and the override carries no namespace, so after a backend
+ // change the server would pin a different device. An absent kind predates the field.
const gpuIndexKind = config.selectedGpuIndexKind ?? "physical";
if (
config.selectedGpuIds &&
@@ -479,17 +431,13 @@ export interface ModelOverrideWriteResult {
}
export interface PutModelOverrideOptions {
- /**
- * Fill in only what is missing: every value already on the server stays as it is. The backfill
- * reads the map once then writes each model in turn, so another tab's save would be overwritten
- * by this browser's older copy. Field level, so a legacy entry gains the browser-only fields.
- */
+ /** Fill in only what is missing: every value already on the server stays. The backfill reads
+ * the map once then writes each model in turn, so another tab's save would be overwritten
+ * by this browser's older copy. Field level, so a legacy entry gains browser-only fields. */
fillAbsentFields?: boolean;
- /**
- * Clear the fields this UI mirrors but leave the server's own launch flags alone. Evicting a
- * local entry for the storage budget is not a forget, so it must not take `llama_extra_args`
- * the page can neither show nor restore. The route drops the row once nothing is left in it.
- */
+ /** Clear the fields this UI mirrors but leave the server's own launch flags alone. Evicting a
+ * local entry for the storage budget is not a forget, so it must not take
+ * `llama_extra_args` the page can neither show nor restore. */
keepLaunchFlags?: boolean;
}
@@ -545,12 +493,11 @@ async function sendModelOverride(
? // biome-ignore lint/style/useNamingConvention: API schema
{ fill_absent_fields: true }
: {}),
- // Say which operation this is: an all-default save carries no fields, shape-identical
- // to a forget, and guessing wrong wipes flags the UI cannot show or restore.
+ // Say which operation this is: an all-default save carries no fields, shape-identical to a
+ // forget, and guessing wrong wipes flags the UI cannot show or restore.
remove: config === null && !options?.keepLaunchFlags,
- // The backend preserves the flags when omitted, which is what a save that
- // never opened the box relies on; a forget means all of it, so that path
- // sends an explicit [].
+ // The backend preserves the flags when omitted, which a save that never opened the box relies
+ // on; a forget means all of it, so that path sends an explicit [].
...(config === null && !options?.keepLaunchFlags
? // biome-ignore lint/style/useNamingConvention: API schema
{ llama_extra_args: [] }
diff --git a/studio/frontend/src/features/model-picker/api/templates.ts b/studio/frontend/src/features/model-picker/api/templates.ts
index 29f18cce298..ec21910015c 100644
--- a/studio/frontend/src/features/model-picker/api/templates.ts
+++ b/studio/frontend/src/features/model-picker/api/templates.ts
@@ -38,11 +38,10 @@ export async function fetchDefaultChatTemplate(
signal?: AbortSignal,
nativePathToken?: string | null,
): Promise<string | null> {
- // A native (picked / drag-drop) GGUF lives at a path only its signed lease
- // knows, and the picker chat-template GET has no lease plumbing, so redeem a
- // one-shot validate-model lease and read the embedded template through the
- // lease-aware /api/inference/validate probe instead (mirrors the staged
- // header-dims fetch). Non-native models keep the plain GET path.
+ // A native (picked or drag-dropped) GGUF lives at a path only its signed lease knows, and the
+ // picker chat-template GET has no lease plumbing, so redeem a one-shot validate-model lease and
+ // read the embedded template through the lease-aware /api/inference/validate probe instead.
+ // Non-native models keep the plain GET path.
if (nativePathToken) {
let nativePathLease: string | null = null;
try {
@@ -50,8 +49,7 @@ export async function fetchDefaultChatTemplate(
await consumeNativePathToken(nativePathToken, "validate-model")
).nativePathLease;
} catch {
- // Lease expired / revoked: no readable path, so no default template (the
- // subsequent load re-mints its own lease).
+ // Lease expired / revoked: no readable path, so no default template (the subsequent load re-mints its own lease).
return null;
}
const response = await authFetch("/api/inference/validate", {
diff --git a/studio/frontend/src/features/model-picker/components/chat-template-editor-dialog.tsx b/studio/frontend/src/features/model-picker/components/chat-template-editor-dialog.tsx
index cc9d48b911c..6448f6651b7 100644
--- a/studio/frontend/src/features/model-picker/components/chat-template-editor-dialog.tsx
+++ b/studio/frontend/src/features/model-picker/components/chat-template-editor-dialog.tsx
@@ -42,8 +42,8 @@ export function ChatTemplateEditorDialog({
const [draft, setDraft] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [validating, setValidating] = useState(false);
- // Bumped whenever the dialog closes so a validation still in flight cannot
- // apply a template the user has already dismissed.
+ // Bumped whenever the dialog closes so a validation still in flight cannot apply a template the
+ // user has already dismissed.
const validationToken = useRef(0);
const renderedDraft = draft ?? value ?? defaultTemplate ?? "";
@@ -74,8 +74,7 @@ export function ChatTemplateEditorDialog({
const token = validationToken.current;
try {
const result = await validateChatTemplate(renderedDraft);
- // Dialog was closed (or reopened) while validating; drop the result so a
- // discarded template is never applied.
+ // Dialog was closed (or reopened) while validating; drop the result so a discarded template is never applied.
if (token !== validationToken.current) {
return;
}
diff --git a/studio/frontend/src/features/model-picker/components/model-config-page.tsx b/studio/frontend/src/features/model-picker/components/model-config-page.tsx
index 6b91524d263..9a52c3f5e61 100644
--- a/studio/frontend/src/features/model-picker/components/model-config-page.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-config-page.tsx
@@ -184,8 +184,7 @@ const SPECULATIVE_TYPE_LABELS: Record<
off: "Off",
};
-// Lower-case where the value is the flag's own spelling, so the pick reads the
-// same as the launch command.
+// Lower-case where the value is the flag's own spelling, so the pick reads the same as the launch command.
const LOAD_MODE_LABELS: Record<(typeof LOAD_MODES)[number], string> = {
auto: "Auto",
none: "None",
@@ -195,16 +194,14 @@ const LOAD_MODE_LABELS: Record<(typeof LOAD_MODES)[number], string> = {
dio: "DirectIO",
};
-// What "Don't reserve system RAM" vetoes: mmap maps and dio streams, so neither
-// holds a full host copy. Mirrors _LOAD_MODE_MLOCK_VALUES |
-// _LOAD_MODE_RESERVING_VALUES in llama_server_args.py.
+// What "Don't reserve system RAM" vetoes: mmap maps and dio streams, so neither holds a full
+// host copy. Mirrors _LOAD_MODE_MLOCK_VALUES and _LOAD_MODE_RESERVING_VALUES.
+// Mirrors _LOAD_MODE_MLOCK_VALUES | _LOAD_MODE_RESERVING_VALUES in llama_server_args.py.
const RAM_RESERVING_LOAD_MODES = new Set(["none", "mlock", "mmap+mlock"]);
-/**
- * What Model Memory does to this load mode, or null when the pick reaches the
- * command line untouched: a row showing the pick alone would name a flag the
- * child never sees. Keep resident is first because it wins outright.
- */
+/** What Model Memory does to this load mode, or null when the pick reaches the command line
+ * untouched: a row showing the pick alone would name a flag the child never sees. Keep
+ * resident is first, because it wins outright. */
function loadModeOverrideNotice(
mode: string | null,
settings: ModelMemorySettings | null,
@@ -223,17 +220,9 @@ function loadModeOverrideNotice(
return null;
}
-/**
- * The batch size llama-server will not go below for this load.
- *
- * Two is its own hard floor (it aborts on a batch of 1 at any slot count), and above
- * that the floor follows the slots the launch SERVES. That is not always the number
- * chosen here: a build without --kv-unified serves one slot however many are asked
- * for, and load_model clamps to it, so sizing the floor from an explicit Slots value
- * refused "--batch-size 2" against a command the backend accepts. With the field
- * blank the server-wide default applies, which the catalogue already publishes
- * effective. A backend that publishes neither leaves the hard floor in charge.
- */
+/** The batch size llama-server will not go below for this load. Two is its own hard floor, and
+ * above that the floor follows the slots the launch SERVES, which is not always the number
+ * chosen here: a build without --kv-unified serves one slot however many are asked for. */
function effectiveBatchFloor(
requestedSlots: number | null | undefined,
limits:
@@ -262,8 +251,8 @@ function hasNonDefaultAdvanced(config: PerModelConfig): boolean {
config.tensorParallel ||
config.disableVision ||
config.chatTemplateOverride != null ||
- // Hidden flags can change what the model does, so a panel that opens collapsed
- // over them says "defaults" about a load that is anything but.
+ // Hidden flags can change what the model does, so a panel that opens collapsed over them says
+ // "defaults" about a load that is anything but.
(config.llamaExtraArgs != null && config.llamaExtraArgs.length > 0) ||
(config.gpuMemoryMode ?? "auto") !== "auto" ||
(config.gpuLayers != null && config.gpuLayers >= 0) ||
@@ -303,9 +292,9 @@ function withoutUnsupportedDiffusionSettings(
// the diffusion runner ignores the llama-server batch flags
nBatch: null,
nUbatch: null,
- // The diffusion shim never appends llama-server flags to its command, but the
- // load records them as though it had, so a box filled before classification
- // flipped would leave the model running without what it says.
+ // The diffusion shim never appends llama-server flags to its command, but the load records
+ // them as though it had, so a box filled before classification flipped would leave the
+ // model running without what it says.
llamaExtraArgs: null,
...(hasUnsupportedGpuPick
? {
@@ -508,15 +497,13 @@ function AdvancedGpuSlider({
);
}
-// How much of each card a load may claim. SERVER-WIDE, not a per-model override:
-// it replaces two constants the fit reads, so there is nothing per-model to attach
-// it to, and the label says so. Driven in whole percent so a dragged value
-// round-trips exactly; the fraction is rebuilt at the API boundary.
+// How much of each card a load may claim. SERVER-WIDE, not a per-model override: it replaces
+// two constants the fit reads. Driven in whole percent so a dragged value round-trips
+// exactly; the fraction is rebuilt at the API boundary.
function VramBudgetRow() {
- // The caller already gates on a discrete GPU and non-Manual mode. macOS is gated
- // here too: it reports no discrete GPUs, so the Metal path sizes itself from
- // _APPLE_UNIFIED_MEMORY_FRACTION and fits with budget_frac = 1.0. Showing the
- // slider would promise "applies on the next load" for a setting it ignores.
+ // macOS is gated here too: it reports no discrete GPUs, so the Metal path sizes itself from
+ // _APPLE_UNIFIED_MEMORY_FRACTION and fits with budget_frac = 1.0, and the slider would
+ // promise "applies on the next load" for a setting it ignores.
const isMac = usePlatformStore((s) => s.deviceType === "mac");
const [settings, setSettings] = useState<VramBudgetSettings | null>(null);
const [percent, setPercent] = useState<number | null>(null);
@@ -524,15 +511,14 @@ function VramBudgetRow() {
const adviceId = useId();
const modelLoading = useChatRuntimeStore((s) => s.modelLoading);
const wasModelLoading = useRef(false);
- // Closed while a Run is settling this budget: an edit made in that window would
- // be flushed by the teardown alongside the load request.
+ // Closed while a Run is settling this budget: an edit made in that window would be flushed by
+ // the teardown alongside the load request.
const [locked, setLocked] = useState(isVramBudgetLocked);
useEffect(() => subscribeVramBudgetLock(setLocked), []);
- // Adopt what the last save or read published: the row's own GET races the PUT a
- // previous unmount fired, so reopening Advanced straight after a drag could show
- // a value the server no longer holds. A queued edit outranks the publish, or a
- // save landing mid-drag would pull the slider out from under the pointer.
+ // Adopt what the last save or read published: the row's own GET races the PUT a previous
+ // unmount fired. A queued edit outranks the publish, or a save landing mid-drag would pull
+ // the slider out from under the pointer.
useEffect(() => {
if (isMac) {
return;
@@ -565,11 +551,9 @@ function VramBudgetRow() {
// deviceType settles once /api/health answers, so re-run if the seed guess flips.
}, [isMac]);
- // reloadRequired describes the running child, so it goes stale the moment a load
- // finishes. In the sidebar editor nothing remounts this row on a reload, so the
- // notice below would keep telling the user to reload a model already sized with
- // this budget. Only the falling edge: the read during a load answers about the
- // child being replaced.
+ // reloadRequired describes the running child, so it goes stale the moment a load finishes and
+ // nothing remounts this row. Only the falling edge: the read during a load answers about
+ // the child being replaced.
useEffect(() => {
const finished = wasModelLoading.current && !modelLoading;
wasModelLoading.current = modelLoading;
@@ -577,8 +561,8 @@ function VramBudgetRow() {
return;
}
let cancelled = false;
- // Forced: a read that began before the load finished describes the child being
- // replaced, and sharing it would republish the notice this refresh is clearing.
+ // Forced: a read that began before the load finished describes the child being replaced, and
+ // sharing it would republish the notice this refresh is clearing.
loadVramBudgetSettings({ force: true }).then((loaded) => {
if (cancelled || !loaded) {
return;
@@ -590,10 +574,9 @@ function VramBudgetRow() {
};
}, [isMac, modelLoading]);
- // Flush, don't drop: the timer is cleared so nothing fires against a torn-down
- // view, but the fraction is still sent. It is held nowhere else, so a drag
- // followed within the debounce by Run, the Advanced toggle or closing the panel
- // would be lost. The view is gone, so the response only reaches subscribers.
+ // Flush, do not drop: the timer is cleared so nothing fires against a torn-down view, but the
+ // fraction is still sent, since it is held nowhere else. The response only reaches
+ // subscribers, the view being gone.
useEffect(
() => () => {
if (saveTimer.current) {
@@ -615,10 +598,8 @@ function VramBudgetRow() {
}
const defaultPercent = vramFractionToPercent(settings.defaultFraction);
- // Stored beats UNSLOTH_VRAM_FRACTION, so once the slider has been touched there
- // is otherwise no way back to inheriting it: dragging to the same number stores
- // that number, and a later change to the variable stays masked. Clearing is what
- // the null the API already accepts is for.
+ // Stored beats UNSLOTH_VRAM_FRACTION, so once the slider has been touched there is otherwise
+ // no way back to inheriting it. Clearing is what the null the API accepts is for.
const resetBudget = () => {
if (saveTimer.current) {
clearTimeout(saveTimer.current);
@@ -639,8 +620,8 @@ function VramBudgetRow() {
};
const commit = (next: number) => {
setPercent(next);
- // Debounced: the slider fires per pointer move, so a drag would be dozens of
- // writes, each invalidating the read cache.
+ // Debounced: the slider fires per pointer move, so a drag would be dozens of writes, each
+ // invalidating the read cache.
if (saveTimer.current) {
clearTimeout(saveTimer.current);
}
@@ -650,8 +631,7 @@ function VramBudgetRow() {
flushVramBudgetSave()
?.then(setSettings)
.catch((error: unknown) => {
- // The client re-stages it, where the write generation can say whether
- // it is still the newest intent.
+ // The client re-stages it, where the write generation can say whether it is still the newest intent.
toast.error(
error instanceof Error ? error.message : "Failed to save VRAM budget",
);
@@ -717,8 +697,8 @@ function VramBudgetRow() {
);
}
-// GPU Memory placement controls (mode / GPU Layers / MoE offload / GPU picker), GGUF only.
-// Slider ceilings come from the GGUF header dims; --tensor-split is not persisted per model.
+// GPU Memory placement controls, GGUF only. Slider ceilings come from the GGUF header dims;
+// --tensor-split is not persisted per model.
function GpuMemorySettings({
config,
update,
@@ -741,7 +721,7 @@ function GpuMemorySettings({
const mode = config.gpuMemoryMode ?? "auto";
const isManual = mode === "manual";
const gpuLayers = config.gpuLayers ?? GPU_LAYERS_AUTO;
- // Slider at Auto: llama.cpp --fit owns the layout, so MoE-offload doesn't apply.
+ // Slider at Auto: llama.cpp --fit owns the layout, so MoE-offload does not apply.
const autoLayers = isManual && gpuLayers < 0;
// Ceiling = layer count + 1 (llama.cpp counts the output layer as offloadable), else a fallback.
const gpuLayersMax = layerCount != null ? layerCount + 1 : 256;
@@ -819,10 +799,9 @@ function GpuMemorySettings({
</SelectContent>
</Select>
</div>
- {/* A fixed manual layer count is the one placement the budget cannot move,
- and a CPU-only host has no GPU to act on, so "applies on the next load"
- would be false. Manual + Auto is included: it leaves the planner no device
- list, but --fit-target still carries the budget to llama.cpp's fitter. */}
+ {/* A fixed manual layer count is the one placement the budget cannot move, and a CPU-only host
+ has no GPU to act on, so "applies on the next load" would be false. Manual + Auto is
+ included: --fit-target still carries the budget to llama.cpp's fitter. */}
{!isDiffusion && (!isManual || autoLayers) && gpuDevices.length > 0 && (
<VramBudgetRow />
)}
@@ -988,14 +967,9 @@ function MemoryBreakdownLine({
);
}
-/**
- * "Estimated Memory Usage": what the settings above would cost, before they are run.
- *
- * Figures come from the loader's own sizing, not a weights-times-a-constant rule of
- * thumb, which stops working the moment the KV cache is no longer a rounding error.
- * So KV gets its own line, and when the header cannot size it the row quotes a floor
- * instead of a confident total.
- */
+/** "Estimated Memory Usage": what the settings above would cost, before they are run. Figures
+ * come from the loader's own sizing, not a weights-times-a-constant rule of thumb, so KV
+ * gets its own line and an unsizable header quotes a floor instead of a confident total. */
function MemoryEstimateRow({
estimate,
loading,
@@ -1017,14 +991,12 @@ function MemoryEstimateRow({
gpuCapacityGb: number;
/** GPU plus host RAM, the ceiling an offloaded load works against. 0 when unknown. */
totalCapacityGb: number;
- /** Host RAM alone. The bytes a load pins OUTSIDE the GPU have to fit in this, and
- * unused VRAM cannot help them, so it is a separate question from the total. */
+ /** Host RAM alone. The bytes a load pins OUTSIDE the GPU have to fit in this, and unused VRAM cannot help them. */
systemRamCapacityGb: number;
- /** VRAM free on the usable cards right now. Warns only: see the note at the call
- * site for why this may not refuse a load. 0 when nothing was probed. */
+ /** VRAM free on the usable cards right now. Warns only: see the note at the call site. 0 when nothing was probed. */
freeGpuCapacityGb: number;
- /** Host RAM the machine can hand out right now, less the reserve the loader keeps.
- * Warns only, for the same reason the free-VRAM figure does. 0 when unknown. */
+ /** Host RAM the machine can hand out right now, less the reserve the loader keeps. Warns only,
+ * for the same reason. 0 when unknown. */
usableSystemRamGb: number;
isUnifiedMemory: boolean;
/** GPU and host draw on the same memory, so an offloaded byte is not a freed one. */
@@ -1037,10 +1009,9 @@ function MemoryEstimateRow({
// Nothing honest to show. Silent while loading too, so the row does not flicker.
return null;
}
- // Every verdict and the one advisory paragraph, resolved in ../model-config/memory-fit.
- // Kept out of this file so the node test runner can reach it: the chain is long, the
- // cases it distinguishes are all hardware shapes nobody has on the desk, and while it
- // lived here an arm that could never be taken shipped unnoticed.
+ // Every verdict and the one advisory paragraph, resolved in ../model-config/memory-fit. Kept
+ // out of this file so the node test runner can reach it: while it lived here an arm that
+ // could never be taken shipped unnoticed.
const { gpuFit, totalFit, prefix, advisory } = resolveMemoryFit(estimate, {
gpuCapacityGb,
totalCapacityGb,
@@ -1056,12 +1027,10 @@ function MemoryEstimateRow({
);
return (
<div className="space-y-2">
- {/* Wraps, unlike the other rows, because this one is the only header carrying a
- title AND two figures. The panel is w-[min(468px,...)], so under a ~460px
- window it shrinks with the viewport, and the figures do not shrink: the
- title absorbed the whole shortfall and truncated to "E..." at 320px. Letting
- the figures drop to their own line costs a line only where they would not
- have fitted anyway, and is identical above that width. */}
+ {/* Wraps, unlike the other rows, because this is the only header carrying a title AND two
+ figures: under a ~460px window the title absorbed the whole shortfall and truncated to
+ "E..." at 320px. The panel is w-[min(468px,...)], so it shrinks with the viewport while
+ the figures do not. Letting them drop to their own line is identical above that. */}
<div className={`${ROW_CLASS} flex-wrap gap-y-1`}>
<button
type="button"
@@ -1080,14 +1049,13 @@ function MemoryEstimateRow({
strokeWidth={1.75}
/>
</button>
- {/* ml-auto so that on the wrapped line, where justify-between has nothing to
- push against, the figures still sit under the right edge they had. */}
+ {/* ml-auto so that on the wrapped line, where justify-between has nothing to push against, the
+ figures still sit under the right edge they had. */}
<div
className={`ml-auto flex shrink-0 items-center gap-3 transition-opacity ${stale || loading ? "opacity-50" : ""}`}
>
- {/* One pool: offloading a layer moves it within the same memory rather
- than out of it, so the honest single figure is the total. Reporting
- the GPU share here let a zero-layer load read as almost free. */}
+ {/* One pool: offloading a layer moves it within the same memory rather than out of it, so the
+ honest single figure is the total. The GPU share let a zero-layer load read as free. */}
<MemoryFigure
label={singleMemoryPool ? (isUnifiedMemory ? "Unified" : "Shared") : "GPU"}
value={`${prefix}${formatMemoryGb(
@@ -1127,16 +1095,16 @@ function MemoryEstimateRow({
label="Compute buffers"
value={formatMemoryGb(estimate.computeBytes)}
/>
- {/* The encoder's buffers, which run about 1.3x the projector file. Only on a
- vision load, and named separately since the file is already in Weights. */}
+ {/* The encoder's buffers, about 1.3x the projector file. Only on a vision load, and named
+ separately since the file is already in Weights. */}
{estimate.projectorRuntimeBytes > 0 && (
<MemoryBreakdownLine
label="Vision encoder"
value={formatMemoryGb(estimate.projectorRuntimeBytes)}
/>
)}
- {/* Only when speculation loads a separate drafter, and it is the term most
- likely to surprise: its cache grows with context like the target's. */}
+ {/* Only when speculation loads a separate drafter, and it is the term most likely to surprise:
+ its cache grows with context like the target's. */}
{estimate.drafterRuntimeBytes > 0 && (
<MemoryBreakdownLine
label="Draft cache"
@@ -1233,11 +1201,8 @@ function MlxAdvancedSettings({
);
}
-/**
- * Mmap/Mlock, with the note that says when Settings wins. Its own component
- * because it subscribes to the Model Memory settings and must keep following
- * them: the settings page is reachable without unmounting this panel.
- */
+/** Mmap/Mlock, with the note that says when Settings wins. Its own component because it
+ * subscribes to the Model Memory settings and must keep following them. */
function LoadModeRow({
config,
update,
@@ -1347,20 +1312,18 @@ function GgufAdvancedSettings({
}) {
const batchAdviceId = useId();
const ubatchAdviceId = useId();
- // llama-server aborts below 2 (GGML_ASSERT(n_tokens_all <= cparams.n_batch)) and below
- // the slot count (GGML_ASSERT(n_outputs_max <= cparams.n_outputs_max)), so the loader
- // raises the emitted value to max(slots, 2). Surfaced so the number typed here is not
- // silently different from the one that runs. With Slots blank the count is the server
- // default this page cannot see, so only the hard floor of 2 is asserted.
+ // llama-server aborts below 2 and below the slot count, so the loader raises the emitted value
+ // to max(slots, 2). Surfaced so the number typed here is not silently different from the
+ // one that runs. With Slots blank only the hard floor of 2 is asserted.
+ // GGML_ASSERT(n_tokens_all <= cparams.n_batch) below 2, and
+ // GGML_ASSERT(n_outputs_max <= cparams.n_outputs_max) below the slot count.
const batchFloor = Math.max(2, config.nParallel ?? 2);
const batchBelowFloor = config.nBatch != null && config.nBatch < batchFloor;
- // llama.cpp runs at min(batch, ubatch) and the /status echo is the REQUESTED size, so
- // the control would otherwise keep showing a value the server never used (batch 8 /
- // ubatch 4096 measured 8.8x slower). Against the EMITTED batch, or the two advisories
- // contradict: batch 4 / slots 8 launches at 8, so saying it runs at 4 is wrong. A blank
- // batch emits no flag and runs llama.cpp's own 2048, which caps the micro-batch just the
- // same, so it is the default and not "unbounded". Extras or LLAMA_ARG_BATCH can move
- // that, but this page cannot see them, the same limit the slot floor above carries.
+ // llama.cpp runs at min(batch, ubatch) and the /status echo is the REQUESTED size, so the
+ // control would keep showing a value the server never used: batch 8 / ubatch 4096 measured 8.8x
+ // slower. Measured against the EMITTED batch, or the two advisories contradict, since batch 4 /
+ // slots 8 launches at 8. A blank batch runs llama.cpp's own 2048, which extras or
+ // LLAMA_ARG_BATCH can move without this page seeing it.
const effectiveBatch =
config.nBatch != null
? Math.max(config.nBatch, batchFloor)
@@ -1426,8 +1389,8 @@ function GgufAdvancedSettings({
specDraftNMax: DRAFT_N_MAX_SPEC_TYPES.has(v)
? config.specDraftNMax
: null,
- // Dropped with the drafter, like the depth above: the draft
- // context exists only while a separate model is loaded.
+ // Dropped with the drafter, like the depth above: the draft context exists only while a
+ // separate model is loaded.
specDraftCacheDtype: SEPARATE_DRAFT_MODEL_SPEC_TYPES.has(v)
? config.specDraftCacheDtype
: null,
@@ -1659,9 +1622,8 @@ function GgufAdvancedSettings({
</div>
)}
- {/* withoutUnsupportedDiffusionSettings forces tensorParallel back to false and
- the diffusion runner never reads it, so the switch flipped back under the
- pointer. Same gate as the Vision and batch rows around it. */}
+ {/* withoutUnsupportedDiffusionSettings forces tensorParallel back to false and the diffusion
+ runner never reads it, so the switch flipped back under the pointer. */}
{!isDiffusion && (
<div className={ROW_CLASS}>
<div className="flex min-w-0 items-center gap-1.5">
@@ -1679,9 +1641,8 @@ function GgufAdvancedSettings({
</div>
)}
- {/* withoutUnsupportedDiffusionSettings forces disableVision back to false on a
- diffusion model and the diffusion runner never reads it, so the switch would
- flip back under the pointer. Gated for the same reason the batch rows are. */}
+ {/* withoutUnsupportedDiffusionSettings forces disableVision back to false on a diffusion model
+ and the runner never reads it, so the switch would flip back under the pointer. */}
{!isDiffusion && (
<div className={ROW_CLASS}>
<div className="flex min-w-0 items-center gap-1.5">
@@ -1715,9 +1676,8 @@ function GgufAdvancedSettings({
<ChatTemplateSetting config={config} onEditTemplate={onEditTemplate} />
- {/* Host-side knobs, after the ones that shape the model itself. All three
- are llama-server's own, and the diffusion runner launches no
- llama-server, so they are hidden for it like the batch sizes above. */}
+ {/* Host-side knobs, after the ones that shape the model itself. All three are llama-server's
+ own, and the diffusion runner launches no llama-server, so they are hidden for it. */}
{!isDiffusion && (
<>
<LoadModeRow config={config} update={update} />
@@ -1803,9 +1763,8 @@ function GgufAdvancedSettings({
</>
)}
- {/* Last, because it is the escape hatch for everything the rows above do not
- cover, and because llama.cpp's last-wins parsing means these really are
- appended after them. GGUF only: the flags are llama-server's. */}
+ {/* Last, because it is the escape hatch for everything the rows above do not cover, and
+ llama.cpp's last-wins parsing means these really are appended after them. GGUF only. */}
{!isDiffusion && (
<ExtraArgsRow
config={config}
@@ -1817,15 +1776,10 @@ function GgufAdvancedSettings({
);
}
-/**
- * Pass-through llama-server arguments for this model.
- *
- * llama-server documents 283 flags and Unsloth already emits or manages about 115
- * of them, so the long tail is a text box rather than 168 more controls. The
- * boundary is `validate_extra_args` on the backend, which refuses the flags Unsloth
- * owns; this row is the same judgement shown early, plus a check against the flags
- * THIS build documents, which a list shipped with Unsloth could not do.
- */
+/** Pass-through llama-server arguments for this model. llama-server documents 283 flags and
+ * Unsloth manages about 115, so the long tail is a text box rather than 168 more controls.
+ * The boundary is `validate_extra_args`; this row shows that judgement early, plus a check
+ * against the flags THIS build documents. */
function ExtraArgsRow({
config,
update,
@@ -1836,18 +1790,15 @@ function ExtraArgsRow({
onLoadableChange: (loadable: boolean) => void;
}) {
const [catalog, setCatalog] = useState<LlamaFlagCatalog | null>(null);
- // What is typed, which is not what is stored: the stored value is argv tokens, so
- // the text only exists here. Seeded from the config, then owned by the box, or
- // every re-render would re-quote what the user is halfway through typing.
+ // What is typed, which is not what is stored: the stored value is argv tokens. Seeded from the
+ // config, then owned by the box, or a re-render would re-quote a half-typed line.
const [text, setText] = useState(() =>
formatExtraArgs(config.llamaExtraArgs),
);
const adviceId = useId();
- // What the box last put INTO the config, so an external change can be told apart
- // from the echo of the user's own typing. Reset and the parent's hydration both
- // replace llamaExtraArgs while this row is mounted, and a box that kept its old
- // text would then disagree with what Load sends. Re-seeding on every config
- // change instead would re-quote a half-typed line on each keystroke.
+ // What the box last put INTO the config, so an external change can be told from the echo of
+ // the user's own typing. Reset and hydration both replace llamaExtraArgs while this row is
+ // mounted, and re-seeding on every config change would re-quote on each keystroke.
const selfWritten = useRef(formatExtraArgs(config.llamaExtraArgs));
const external = formatExtraArgs(config.llamaExtraArgs);
useEffect(() => {
@@ -1858,10 +1809,9 @@ function ExtraArgsRow({
setText(external);
}, [external]);
- // Re-read on invalidation, not only on mount: updating llama.cpp from the banner
- // replaces the binary while this panel stays open, and a row holding the old
- // build's catalogue would go on judging arity, and calling flags unknown, against
- // help text that no longer describes the server it is about to launch.
+ // Re-read on invalidation, not only on mount: updating llama.cpp from the banner replaces the
+ // binary while this panel stays open, and the old catalogue would judge arity against help
+ // text that no longer describes the server.
const [catalogEpoch, setCatalogEpoch] = useState(0);
useEffect(
() => subscribeLlamaFlagCatalog(() => setCatalogEpoch((epoch) => epoch + 1)),
@@ -1871,8 +1821,8 @@ function ExtraArgsRow({
let cancelled = false;
loadLlamaFlagCatalog().then((loaded) => {
if (!cancelled) {
- // Null is "cannot verify": adopted as well, or the row would keep checking
- // against the previous binary after an update it cannot read.
+ // Null is "cannot verify": adopted as well, or the row would keep checking against the
+ // previous binary after an update it cannot read.
setCatalog(loaded);
}
});
@@ -1881,11 +1831,9 @@ function ExtraArgsRow({
};
}, [catalogEpoch]);
- // Model Memory removes some of these before the launch: apply_model_memory_policy
- // emits its own load mode and strips the rest, so an --mlock typed here would be
- // shown, saved, and never passed. Read once and then kept in sync, like the section
- // in Settings that owns it. A failed read leaves the row quiet rather than claiming
- // a removal it cannot confirm.
+ // Model Memory removes some of these before the launch: apply_model_memory_policy emits its
+ // own load mode and strips the rest, so an --mlock typed here would be shown, saved, and
+ // never passed. A failed read leaves the row quiet rather than claiming a removal.
const [modelMemory, setModelMemory] = useState<ModelMemorySettings | null>(
null,
);
@@ -1908,36 +1856,32 @@ function ExtraArgsRow({
const diagnostics = diagnoseExtraArgs(text, catalog, {
gpuSelectionActive: config.selectedGpuIds != null,
manualGpuMemory: config.gpuMemoryMode === "manual",
- // The same floor the batch control shows, for the same reason: with Slots blank
- // the count is the server default this page cannot see, so only the hard 2 holds.
- // A build that clamps serves one slot whatever is chosen here, so an explicit
- // Slots value must not raise the floor above a batch the backend would accept.
+ // The same floor the batch control shows: with Slots blank the count is the server default
+ // this page cannot see, so only the hard 2 holds. A build that clamps serves one slot
+ // whatever is chosen, so an explicit Slots value must not raise the floor.
batchFloor: effectiveBatchFloor(config.nParallel, catalog),
keepResident: modelMemory?.keepResident ?? false,
noRamReserve: modelMemory?.noRamReserve ?? false,
});
const tokenCount = parseExtraArgs(text).tokens.length;
- // The panel owns the Load button, and an error here is one validate_extra_args
- // would refuse. Reported up rather than only painted red, so the load is not
- // started just to come back as a failure.
+ // The panel owns the Load button, and an error here is one validate_extra_args would refuse.
+ // Reported up rather than only painted red, so the load is not started just to fail.
const loadable = extraArgsAreLoadable(diagnostics);
useEffect(() => {
onLoadableChange(loadable);
- // Deliberately no cleanup. Collapsing Advanced settings unmounts this row while
- // the tokens stay in the config and still go out with the load, so withdrawing
- // the objection there would re-enable a button for a request the backend
- // refuses. The panel clears it when the model changes instead.
+ // Deliberately no cleanup: collapsing Advanced settings unmounts this row while the tokens
+ // stay in the config and still go out with the load. The panel clears it on model change.
}, [loadable, onLoadableChange]);
const commit = (next: string) => {
setText(next);
const { tokens } = parseExtraArgs(next);
- // Recorded before the update, so the config change this causes reads as the
- // box's own and does not bounce back through the effect above.
+ // Recorded before the update, so the config change this causes reads as the box's own and does
+ // not bounce back through the effect above.
selfWritten.current = formatExtraArgs(tokens.length > 0 ? tokens : null);
- // null, not [], so the panel's own "no flags" reads the same as the stored one;
- // toApiOverride turns it into the explicit [] that clears the server's copy.
+ // null, not [], so the panel's own "no flags" reads the same as the stored one; toApiOverride
+ // turns it into the explicit [] that clears the server's copy.
update({ llamaExtraArgs: tokens.length > 0 ? tokens : null });
};
@@ -2007,9 +1951,8 @@ interface ModelConfigPageProps {
initialConfig?: PerModelConfig | null;
isDiffusion?: boolean;
variant?: "page" | "sidebar";
- /**
- * Page variant only: render the built-in "Run settings" title block. A host that already
- * shows the model name as its page heading turns this off. */
+ /** Page variant only: render the built-in "Run settings" title block. A host that already shows
+ * the model name as its page heading turns this off. */
showHeader?: boolean;
}
@@ -2026,11 +1969,8 @@ export function ModelConfigPage({
}: ModelConfigPageProps) {
const rememberId = useId();
const platformDeviceType = usePlatformStore((s) => s.deviceType);
- // Apple Silicon specifically, and used ONLY for wording: "Unified" is what
- // Apple calls its pool, where an AMD APU's is a "Shared" one. It is NOT the
- // right signal for the capacity question below -- see hasUnifiedMemory.
- //
- // Unified memory, not just Darwin: an Intel Mac spills to system RAM like a PC.
+ // Apple Silicon specifically, and used ONLY for wording: "Unified" is what Apple calls its
+ // pool, where an AMD APU's is "Shared". NOT the signal for capacity - see hasUnifiedMemory.
const isAppleUnifiedMemory = usePlatformStore((s) => s.appleSilicon);
const platformChatOnlyReason = usePlatformStore((s) => s.chatOnlyReason);
const mlxKvQuantReason = useChatRuntimeStore((s) => s.mlxKvQuantReason);
@@ -2077,8 +2017,8 @@ export function ModelConfigPage({
const [configState, setConfig] = useState<PerModelConfig>(() =>
reconcileConfigGpuSelection(initial.config, isDiffusion, gpuDevices),
);
- // The live config, for the async reads below: an effect that closed over it would
- // hold whatever it was when the request started.
+ // The live config, for the async reads below: an effect that closed over it would hold
+ // whatever it was when the request started.
const configRef = useRef(configState);
configRef.current = configState;
const [remember, setRemember] = useState(() => initial.remembered);
@@ -2086,35 +2026,30 @@ export function ModelConfigPage({
const rememberRef = useRef(remember);
rememberRef.current = remember;
const [speculativeFallback] = useState(readPersistedSpeculativeType);
- // Same substitution as speculativeFallback, for the same reason: only "manual" is
- // persisted per model, so an absent mode means "follow the standing preference"
- // rather than Auto. applyPerModelConfigToRuntime resolves it that way at load, so
- // pricing the absence as Auto rated a Manual launch against the wrong plan.
+ // Same substitution as speculativeFallback: only "manual" is persisted per model, so an absent
+ // mode means "follow the standing preference" rather than Auto, and pricing the absence as
+ // Auto rated a Manual launch against the wrong plan.
const [gpuMemoryModeFallback] = useState(readPersistedGpuMemoryMode);
const [templateOpen, setTemplateOpen] = useState(false);
- // Raised by the extra-arguments row when what is typed is something
- // validate_extra_args would refuse, so the load is not started to fail. Held by
- // the panel rather than the row because the row unmounts whenever Advanced
- // settings are collapsed, while its tokens stay in the config.
+ // Raised by the extra-arguments row when what is typed is something validate_extra_args would
+ // refuse. Held by the panel rather than the row, since the row unmounts whenever Advanced
+ // settings collapse while its tokens stay in the config.
const [extraArgsLoadable, setExtraArgsLoadable] = useState(true);
- // True until the stored-arguments read below settles. A load started before it
- // lands sends no llama_extra_args, and /load cannot inherit them from a process
- // that is not running, so a fast click would launch a cold model without the
- // arguments that were about to appear on screen.
+ // True until the stored-arguments read below settles: a load started before it lands sends no
+ // llama_extra_args, and /load cannot inherit them from a process that is not running.
const [extraArgsHydrating, setExtraArgsHydrating] = useState(
() => target.isGguf && !isDiffusion,
);
- // The row does not withdraw its own objection when it unmounts, or collapsing
- // Advanced settings would re-enable Load for arguments the backend refuses. A
- // different model is the one thing that really does retire it.
+ // The row does not withdraw its own objection when it unmounts, or collapsing Advanced settings
+ // would re-enable Load for arguments the backend refuses. Only a different model retires it.
// biome-ignore lint/correctness/useExhaustiveDependencies: keyed on the model, not on the setter
useEffect(() => {
setExtraArgsLoadable(true);
setExtraArgsHydrating(target.isGguf && !isDiffusion);
}, [configId, target.ggufVariant, target.isGguf, isDiffusion]);
- // Compare against what the backend was asked for, not what it applied: staging a
- // new value must retire a verdict that answered a different request.
+ // Compare against what the backend was asked for, not what it applied: staging a new value
+ // must retire a verdict that answered a different request.
const chatTemplateOutcome =
isActiveModel &&
(configState.chatTemplateOverride ?? null) ===
@@ -2124,8 +2059,7 @@ export function ModelConfigPage({
const mlxKvQuantOutcome =
isActiveModel &&
(configState.mlxKvBits ?? null) === (loadedMlxKvBitsRequested ?? null)
- ? // Both, not either: dropping the note promises savings before the offset
- // where quantization actually starts.
+ ? // Both, not either: dropping the note promises savings before the offset where quantization actually starts.
[mlxKvQuantReason, mlxKvQuantNote].filter(Boolean).join(". ") || null
: null;
const servedByMlx = isServedByMlx(
@@ -2144,11 +2078,11 @@ export function ModelConfigPage({
const [autoOpenAdvanced, setAutoOpenAdvanced] = useState(() =>
hasNonDefaultAdvanced(configState),
);
- // Frozen like the rest of the auto-open decision, so editing the width does not
- // reopen the section the user just closed.
+ // Frozen like the rest of the auto-open decision, so editing the width does not reopen the
+ // section the user just closed.
const [initialMlxKvBits] = useState(() => configState.mlxKvBits ?? null);
- // Applicability stays live, unlike the snapshot above: MLX can become available
- // after mount, and a width that starts applying then has to surface.
+ // Applicability stays live, unlike the snapshot above: MLX can become available after mount,
+ // and a width that starts applying then has to surface.
const autoOpenForMlxKvBits = servedByMlx && initialMlxKvBits != null;
const showAdvanced =
advancedPreference ?? (autoOpenAdvanced || autoOpenForMlxKvBits);
@@ -2248,24 +2182,11 @@ export function ModelConfigPage({
);
const resolvedIsDiffusion = classifiedIsDiffusion === true;
- // The one field on this page whose stored value the local config may never have
- // seen. Everything else here is written by this panel, but llama_extra_args can be
- // set through the overrides API with no UI involved, which is exactly why that
- // route preserves it when omitted. Showing an empty box for a model that has flags
- // would read as "none", and the first edit would then submit a list that silently
- // dropped them. Fetched, not guessed.
- //
- // The row judges what is typed while it is mounted, and deliberately leaves its
- // verdict standing when Advanced settings collapse, because the tokens still go
- // out with the load. But a verdict reached before the catalogue arrived was
- // reached without knowing which flags this build documents, and collapsing the
- // section freezes it: a bare --threads typed during a cold probe would keep Load
- // enabled and fail at llama-server startup. So while the row is unmounted, the
- // panel re-judges what the config holds once the catalogue lands.
- // The row holds its own subscription, but it is unmounted whenever this check is
- // the one running, so the panel needs its own: an in-app llama.cpp update with
- // Advanced collapsed would otherwise leave a verdict reached against the previous
- // binary standing, and Load live for a flag the new one has removed.
+ // The one field on this page whose stored value the local config may never have seen:
+ // llama_extra_args can be set through the overrides API with no UI involved, so an empty box
+ // would read as "none" and the first edit would drop them. Fetched, not guessed. A verdict
+ // reached before the catalogue arrived did not know this build's flags, so while the row is
+ // unmounted the panel re-judges the config once the catalogue lands.
const [hiddenCatalogEpoch, setHiddenCatalogEpoch] = useState(0);
useEffect(
() =>
@@ -2281,35 +2202,31 @@ export function ModelConfigPage({
}
const args = configState.llamaExtraArgs;
if (args == null || args.length === 0) {
- // The row leaves its objection standing when it unmounts, because the tokens
- // it objected to still go out with the load. Once there are none, there is
- // nothing left to object to: Reset with Advanced collapsed used to leave Load
- // disabled over arguments the request no longer carries.
+ // The row leaves its objection standing when it unmounts, because those tokens still go out
+ // with the load. Once there are none there is nothing to object to: Reset with Advanced
+ // collapsed used to leave Load disabled.
setExtraArgsLoadable(true);
return;
}
let cancelled = false;
loadLlamaFlagCatalog().then((catalog) => {
if (cancelled || !catalog) {
- // Null is "cannot verify": leaving the standing verdict alone is the same
- // benefit of the doubt the row gives an unprobed build.
+ // Null is "cannot verify": leaving the standing verdict alone is the same benefit of the doubt
+ // the row gives an unprobed build.
return;
}
const loadable = extraArgsAreLoadable(
diagnoseExtraArgs(formatExtraArgs(args), catalog, {
gpuSelectionActive: configState.selectedGpuIds != null,
manualGpuMemory: configState.gpuMemoryMode === "manual",
- // The server-wide default when the Slots field is blank: that is the
- // count the launch will serve, and llama-server aborts on a batch below
- // it. Clamped builds serve one whatever is chosen.
+ // The server-wide default when the Slots field is blank: that is the count the launch will
+ // serve, and llama-server aborts on a batch below it.
batchFloor: effectiveBatchFloor(configState.nParallel, catalog),
}),
);
- // Only ever tightens. This judges the TOKENS, and formatExtraArgs quotes them
- // back into a balanced string, so an unclosed quote the row objected to reads
- // as fine here; raising the verdict would re-enable Load over the unfinished
- // value the user is still typing. A list that is genuinely clean keeps
- // whatever verdict already stands, which is true unless the row set it.
+ // Only ever tightens. This judges the TOKENS, and formatExtraArgs quotes them back into a
+ // balanced string, so an unclosed quote the row objected to reads as fine here and raising
+ // the verdict would re-enable Load over unfinished input.
if (!loadable) {
setExtraArgsLoadable(false);
}
@@ -2328,27 +2245,22 @@ export function ModelConfigPage({
hiddenCatalogEpoch,
]);
- // The server copy is shared by Desktop, LAN, and tunnel origins. Hydrate the
- // whole remembered GGUF config here; localStorage is only the immediate seed.
- // This also has to run while Advanced is closed because extra arguments live in
- // that section but affect every load.
+ // The server copy is shared by Desktop, LAN, and tunnel origins, so hydrate the whole
+ // remembered GGUF config here; localStorage is only the immediate seed. This also runs while
+ // Advanced is closed, since extra arguments affect every load.
const extraArgsHydrated = useRef<string | null>(null);
// biome-ignore lint/correctness/useExhaustiveDependencies: the model is the identity
useEffect(() => {
if (!target.isGguf || resolvedIsDiffusion) {
- // Nothing else even has this field: the row is GGUF-only and so is the load
- // payload, so a Transformers or MLX model must not wait on either request.
- // A diffusion GGUF is GGUF-shaped but runs through the diffusion shim, which
- // appends no llama-server flags and whose config strips them, so it must not
- // wait either.
+ // Nothing else even has this field: the row is GGUF-only and so is the load payload. A
+ // diffusion GGUF is GGUF-shaped but runs through the shim, which appends no llama-server
+ // flags, so it must not wait either.
setExtraArgsHydrating(false);
return;
}
- // The order the auto-switch loader tries, and for the same reason: the settings
- // UI keys a local row by the path it loads from, while configId is a derived
- // alias, so reading the alias first lets an older entry stay the one API loads
- // apply while the panel shows none of it. A loose .gguf was keyed by its
- // filename label in an early build, which is the fourth candidate here.
+ // The order the auto-switch loader tries, and for the same reason: the settings UI keys a local
+ // row by the path it loads from while configId is a derived alias, so reading the alias
+ // first let an older entry stay the one API loads apply.
const loadId = target.id;
const fileVariant =
!target.ggufVariant && loadId.toLowerCase().endsWith(".gguf")
@@ -2368,33 +2280,25 @@ export function ModelConfigPage({
return;
}
let cancelled = false;
- // What the config held BEFORE the request went out. Anything else at the end of
- // it was typed by the user while it was in flight, and sanitizing that would
- // rewrite live input: typing --agent during the window cleared the box instead
- // of showing the error, and a long paste could be trimmed behind the cursor.
+ // What the config held BEFORE the request went out. Anything else at the end of it was typed
+ // while the request was in flight, and sanitizing that would rewrite live input.
const configAtStart = configRef.current;
const storedAtStart = resolveInitialConfig(configId, target.ggufVariant);
const rememberAtStart = rememberRef.current;
const localAtStart = configAtStart.llamaExtraArgs;
- // The denylist, not the catalogue: sanitizing a stored list needs only the flags
- // Unsloth refuses, and that route answers without running `llama-server --help`.
- // Waiting on the probe instead would hold Load shut for as long as a cold --help
- // takes, and releasing on a deadline would leave a legacy flag in an explicit
- // request that /load then refuses.
- // A last-resort release, so a request that never settles cannot disable Load for
- // good: past this a load behaves as it did before the feature.
+ // The denylist, not the catalogue: sanitizing a stored list needs only the flags Unsloth
+ // refuses, and that route answers without running `llama-server --help`. A last-resort
+ // release, so a request that never settles cannot disable Load for good.
const release = setTimeout(() => setExtraArgsHydrating(false), 15000);
Promise.all([
- // Resolved by the backend, which owns the rules: the local resolver stays as
- // the fallback for a backend that predates the parameter.
+ // Resolved by the backend, which owns the rules; the local resolver stays as the fallback for
+ // a backend that predates the parameter.
fetchLoadModelOverride(loadId, configId, target.ggufVariant, keys),
loadManagedLlamaFlags(),
])
.then(([resolvedOverride, managed]) => {
- // Marked here rather than before the request: StrictMode replays the effect
- // (setup, cleanup, setup), so a key marked up front would leave the first
- // fetch cancelled and the second setup returning early, and the box would
- // never fill in development.
+ // Marked here rather than before the request: StrictMode replays the effect, so a key marked
+ // up front would leave the first fetch cancelled and the box never filled in development.
if (cancelled) {
return;
}
@@ -2403,33 +2307,25 @@ export function ModelConfigPage({
tokens: resolvedOverride?.llama_extra_args ?? [],
explicit: Array.isArray(resolvedOverride?.llama_extra_args),
};
- // Through the resolver, not a literal lookup: the backend folds identities
- // and reads whole entries in its own order before it reads a field.
- //
- // Sanitised before it becomes a request: hydrating turns a stored list into
- // an EXPLICIT one, which /load validates strictly instead of putting it
- // through the carry-over paths that drop a newly denied flag quietly.
- // Without this, an install upgraded across a denylist change stops loading a
- // model that worked the day before.
+ // Through the resolver, not a literal lookup: the backend folds identities and reads whole
+ // entries in its own order. Sanitised before it becomes a request, since hydrating turns a
+ // stored list into an EXPLICIT one that /load validates strictly, so an install upgraded
+ // across a denylist change would otherwise stop loading a model that worked yesterday.
const stored = sanitizeStoredExtraArgs(
resolvedArgs.tokens,
managed?.managed ?? new Set<string>(),
- // The host's bounds, not the constants: a Windows server takes 24 KiB and
- // holds a quoted-command budget as well, so trimming to 32 KiB here sends a
- // list /load answers 400 on.
+ // The host's bounds, not the constants: a Windows server takes 24 KiB and holds a
+ // quoted-command budget as well, so trimming to 32 KiB here sends a list /load 400s on.
{
maxBytes: managed?.maxBytes,
windowsCommandBudget: managed?.windowsCommandBudget,
},
);
- // A list this build refuses can equally have come from local storage, saved
- // by a build that still allowed it. The server copy is sanitised above; this
- // is the same treatment for the one already in hand, which nothing else
- // would catch while Advanced stays collapsed.
+ // A list this build refuses can equally have come from local storage, saved by a build that
+ // still allowed it, and nothing else would catch it while Advanced stays collapsed.
const local = configRef.current.llamaExtraArgs;
- // Kept for the merge below as well as for the box: a row that carries no
- // arguments leaves the local list standing, and handing back the list this
- // build refuses would re-enable Load for a request /load answers 400 on.
+ // Kept for the merge below as well as for the box: a row that carries no arguments leaves the
+ // local list standing, and handing back a refused list would re-enable Load.
let sanitizedLocal = localAtStart;
if (local != null && local.length > 0 && local === localAtStart) {
const cleaned = sanitizeStoredExtraArgs(
@@ -2449,8 +2345,8 @@ export function ModelConfigPage({
);
}
}
- // The row as it should be read: its own fields, with the arguments it carries
- // sanitized against what this build refuses.
+ // The row as it should be read: its own fields, with the arguments it carries sanitized against
+ // what this build refuses.
const resolvedRow = resolvedOverride
? {
...resolvedOverride,
@@ -2463,12 +2359,9 @@ export function ModelConfigPage({
llamaExtraArgs: sanitizedLocal,
})
: null;
- // What hydration would leave in the config: the row's list when it carries
- // one, this browser's when it does not, since a row that says nothing about
- // arguments cannot overrule the list already here. Judged as a whole, or a
- // local flag the expanded row has ALREADY refused is declared loadable by a
- // verdict read off the empty server list, and the row republishes its own
- // only when its verdict changes, so the objection never comes back.
+ // What hydration would leave in the config: the row's list when it carries one, this browser's
+ // when it does not. Judged as a whole, or a local flag the expanded row already refused is
+ // declared loadable by a verdict read off the empty server list.
const hydratedArgs = serverConfig?.llamaExtraArgs ?? stored;
const hydratedIsLoadable =
hydratedArgs.length === 0
@@ -2496,11 +2389,10 @@ export function ModelConfigPage({
),
);
- // The shared row outranks the local seed for every field it carries, so LAN
- // and Desktop cannot show different remembered values; fromApiOverride keeps
- // the rest of this browser's config rather than resetting it, since an
- // absent field is as much a gap in the mirror as a chosen default. Never
- // over an edit made while the request was in flight.
+ // The shared row outranks the local seed for every field it carries, so LAN and Desktop cannot
+ // show different remembered values; fromApiOverride keeps the rest of this browser's config,
+ // since an absent field is as much a gap in the mirror as a chosen default. Never over an
+ // edit made while the request was in flight.
if (
resolvedRow &&
serverConfig &&
@@ -2521,33 +2413,24 @@ export function ModelConfigPage({
if (hasNonDefaultAdvanced(serverConfig)) {
setAutoOpenAdvanced(true);
}
- // Unconditionally, because savePerModelConfig says "no settings" by
- // DELETING the entry: a merge that comes out default is a clear that has
- // to travel, not a write to skip. Clearing a model's only remembered
- // flag leaves the row as an explicit empty list, which is exactly that
- // case, and skipping it stranded the old flag here for model-selector's
- // quick select to reload without ever opening this panel. Writing when
- // no record exists is already a no-op.
+ // Unconditionally, because savePerModelConfig says "no settings" by DELETING the entry: a merge
+ // that comes out default is a clear that has to travel. Skipping it stranded the old flag
+ // for model-selector's quick select; writing when no record exists is already a no-op.
const rememberedConfig = fromApiOverride(
resolvedRow,
storedConfig.config,
);
- // Same budget as any other write, so the same clean-up: eviction is silent
- // and still reports success, and a dropped model would keep applying its
- // server row to API loads while the picker showed defaults, with nothing
- // able to forget it. Not a Forget, only the mirrored fields go.
+ // Same budget as any other write, so the same clean-up: eviction is silent and still reports
+ // success, and a dropped model would keep applying its server row to API loads. Not a
+ // Forget: only the mirrored fields go.
const hydrationEvicted: {
modelId: string;
ggufVariant: string | null;
}[] = [];
- // Checked for the same reason the Save path checks it: the write can fail
- // outright (storage full or unavailable, or a record from a newer build that
- // must not be replaced) and say so in the return rather than throwing. Marked
- // saved regardless, the panel would claim the server settings are remembered
- // here while quick select and background loads, which read resolveInitialConfig
- // and never open this panel, still saw the stale record or none. Leaving it
- // unsaved makes it a pending change instead, so Save is reachable and reports
- // the failure the way every other write does.
+ // Checked for the same reason the Save path checks it: the write can fail outright and say so
+ // in the return rather than throwing. Marked saved regardless, the panel would claim the
+ // settings are remembered while quick select and background loads, which read
+ // resolveInitialConfig and never open this panel, still saw the stale record or none.
const hydrationSaved = savePerModelConfig(
configId,
target.ggufVariant,
@@ -2563,12 +2446,10 @@ export function ModelConfigPage({
return;
}
if (stored.length === 0) {
- // An EXPLICIT empty row is a decision, not an absence: the settings page
- // writes one when the box is cleared for a quant whose bare-repository
- // row still carries arguments, and it is what stops the server's lookup
- // there. Left undefined the panel omits the field on Load, and /load
- // carries the resident model's arguments over: the very ones just
- // cleared. Only when nothing was typed here in the meantime.
+ // An EXPLICIT empty row is a decision, not an absence: the settings page writes one when the
+ // box is cleared for a quant whose bare-repository row still carries arguments. Left
+ // undefined the panel omits the field on Load, and /load carries the resident model's
+ // arguments over. Only when nothing was typed here meanwhile.
const local = configRef.current.llamaExtraArgs;
if (resolvedArgs.explicit && local === undefined) {
setExtraArgsLoadable(true);
@@ -2580,12 +2461,11 @@ export function ModelConfigPage({
}
return;
}
- // Read from a ref rather than inside the updater below: an updater must stay
- // free of side effects (StrictMode calls it twice), and this decides one.
+ // Read from a ref rather than inside the updater below: an updater must stay free of side
+ // effects (StrictMode calls it twice), and this decides one.
if (configRef.current.llamaExtraArgs !== undefined) {
- // Typed into while this was in flight. The row is judging that text and its
- // verdict is the live one, so this answer about the stored list must not
- // overwrite either of them: doing so re-enabled Load for invalid input.
+ // Typed into while this was in flight: the row is judging that text and its verdict is the
+ // live one, so this answer about the stored list must not overwrite either.
return;
}
setExtraArgsLoadable(hydratedIsLoadable);
@@ -2595,19 +2475,16 @@ export function ModelConfigPage({
? { ...current, llamaExtraArgs: stored }
: current,
);
- // The frozen snapshot above was taken before this answer arrived, so without
- // this a model whose arguments live only on the server opens looking like
- // every default. An explicit preference still wins: this only feeds the
- // fallback.
+ // The frozen snapshot above was taken before this answer arrived, so without this a model whose
+ // arguments live only on the server opens looking like every default. A preference wins.
setAutoOpenAdvanced(true);
})
.catch(() => {
- // Nothing to say: the panel is still usable, and the load would report a
- // real problem with the overrides service far more clearly than this could.
+ // Nothing to say: the panel is still usable, and the load would report a real problem with the
+ // overrides service far more clearly.
})
.finally(() => {
- // Including the failure: an overrides service that is down must not leave
- // Load disabled for good.
+ // Including the failure: an overrides service that is down must not leave Load disabled for good.
if (!cancelled) {
setExtraArgsHydrating(false);
}
@@ -2628,13 +2505,11 @@ export function ModelConfigPage({
resolvedIsDiffusion,
gpuDevices,
);
- // Held for the window where Run is waiting on a budget PUT rather than on the
- // load it looks like it started.
+ // Held for the window where Run is waiting on a budget PUT rather than on the load it looks like it started.
const [budgetSettling, setBudgetSettling] = useState(false);
- // The budget is on no per-model field, so changing it leaves this page at its
- // baseline and the Reload button disabled, with the row asking for a reload the
- // user could not start. Read off the same publish the row uses, so nothing is
- // threaded through GpuMemorySettings and a row that never mounts leaves it false.
+ // The budget is on no per-model field, so changing it leaves this page at its baseline and the
+ // Reload button disabled, with the row asking for a reload the user could not start. Read
+ // off the same publish the row uses.
const [budgetReloadRequired, setBudgetReloadRequired] = useState(false);
useEffect(
() =>
@@ -2658,14 +2533,13 @@ export function ModelConfigPage({
...patch,
}));
- // True for every mode that takes a draft depth, DSpark included, so this
- // gates the Draft Tokens row rather than naming a drafter.
+ // True for every mode that takes a draft depth, DSpark included, so this gates the Draft
+ // Tokens row rather than naming a drafter.
const showDraftTokens =
config.speculativeType != null &&
DRAFT_N_MAX_SPEC_TYPES.has(config.speculativeType);
- // Narrower than the row above: the dtype needs a second context, and only the
- // sidecar modes always load one. MTP may read baked-in heads out of the target
- // GGUF, which the loader knows and this panel does not.
+ // Narrower than the row above: the dtype needs a second context, and only the sidecar modes
+ // always load one. MTP may read baked-in heads out of the target GGUF.
const showSpecDraftCacheDtype =
config.speculativeType != null &&
SEPARATE_DRAFT_MODEL_SPEC_TYPES.has(config.speculativeType);
@@ -2673,8 +2547,8 @@ export function ModelConfigPage({
target.meta.contextLength ?? stagedDims?.contextLength ?? null;
const activeLoadedContext =
isActiveModel && target.isGguf ? loadedContextLength : null;
- // resolveLoadMaxSeqLength returns 0 for a builtin-default GGUF load before it looks
- // at the resident context, so the estimate must not fall back to it either.
+ // resolveLoadMaxSeqLength returns 0 for a builtin-default GGUF load before it looks at the
+ // resident context, so the estimate must not fall back to it either.
const activePresetSource = useChatRuntimeStore((s) => s.activePresetSource);
const minContext = CONTEXT_LENGTH_MIN;
const maxContext = Math.max(
@@ -2772,10 +2646,9 @@ export function ModelConfigPage({
const loadableConfig = resolvedIsDiffusion
? withoutUnsupportedDiffusionSettings(config, gpuIndexKind)
: config;
- // A model classified as diffusion after the box was typed into keeps the row's
- // objection while the arguments themselves are stripped from what loads, and the
- // row does not withdraw it when it unmounts. Retired here, or Load stays disabled
- // over arguments the request no longer carries.
+ // A model classified as diffusion after the box was typed into keeps the row's objection while
+ // the arguments are stripped from what loads, and the row does not withdraw it on unmount.
+ // Retired here, or Load stays disabled over arguments the request no longer carries.
// biome-ignore lint/correctness/useExhaustiveDependencies: keyed on the classification, not on the setter
useEffect(() => {
if (resolvedIsDiffusion) {
@@ -2798,17 +2671,10 @@ export function ModelConfigPage({
: loadableConfig;
const runtimeGpuMemoryMode =
runtimeConfig.gpuMemoryMode ?? gpuMemoryModeFallback;
- // Priced against the config that would actually load, at the context on screen, so
- // the figures answer for what the Load button will do. Diffusion stands down: its
- // runner allocates on a different plan than llama-server's.
- //
- // The tri-state is read as a tri-state here, not through resolvedIsDiffusion, which
- // folds "not yet classified" in with "not diffusion". A GGUF whose classification is
- // still in flight may be DiffusionGemma, and starting the llama-server estimate on
- // that guess paints a footprint from the wrong allocation plan -- one that stays on
- // screen indefinitely if the classifying probe never answers, since the tri-state
- // then never leaves undefined. Waiting costs a moment of no row; guessing costs a
- // confident wrong number, which is the trade this whole panel is written around.
+ // Priced against the config that would actually load, at the context on screen. Diffusion
+ // stands down, since its runner allocates on a different plan. The tri-state is read as a
+ // tri-state, not through resolvedIsDiffusion: a GGUF still being classified may be
+ // DiffusionGemma, and guessing paints a footprint from the wrong plan that never clears.
const memoryEstimateRequest =
target.isGguf && classifiedIsDiffusion === false
? {
@@ -2816,14 +2682,13 @@ export function ModelConfigPage({
ggufVariant: target.ggufVariant ?? null,
hfToken: hfToken || null,
nativePathToken,
- // The context the Load button sends, not the one the control displays: an
- // unset length with no header yet shows 32,768 and loads at native.
+ // The context the Load button sends, not the one the control displays: an unset length with no
+ // header yet shows 32,768 and loads at native.
nCtx: resolveEstimateContext(
runtimeConfig.customContextLength ?? null,
activeLoadedContext,
- // The two shapes where resolveLoadMaxSeqLength answers 0 before it
- // reaches the resident context: --fit owns the sizing, or a
- // builtin-default preset on a GGUF load.
+ // The two shapes where resolveLoadMaxSeqLength answers 0 before it reaches the resident
+ // context: --fit owns the sizing, or a builtin-default preset on a GGUF load.
(target.isGguf === true &&
runtimeGpuMemoryMode === "manual" &&
(runtimeConfig.gpuLayers ?? GPU_LAYERS_AUTO) < 0) ||
@@ -2834,10 +2699,9 @@ export function ModelConfigPage({
nBatch: runtimeConfig.nBatch,
nUbatch: runtimeConfig.nUbatch,
ctxCheckpoints: runtimeConfig.ctxCheckpoints ?? null,
- // The same substitution applyPerModelConfigToRuntime makes at load: a model
- // with no per-model override sends null, which the backend reads as Auto,
- // while the selector has been showing the global fallback all along. With
- // the global set to Off and an 11 GB DSpark sidecar in the repo, the row
+ // The same substitution applyPerModelConfigToRuntime makes at load: a model with no per-model
+ // override sends null, which the backend reads as Auto, while the selector has been showing
+ // the global fallback. With the global Off and an 11 GB DSpark sidecar in the repo, the row
// charged the sidecar for a load that disables it.
speculativeType: runtimeConfig.speculativeType ?? speculativeFallback ?? null,
specDraftNMax: runtimeConfig.specDraftNMax,
@@ -2858,41 +2722,17 @@ export function ModelConfigPage({
const memoryEstimate = useMemoryEstimate(memoryEstimateRequest);
const [memoryBreakdownOpen, setMemoryBreakdownOpen] = useState(false);
const inferenceGpu = useInferenceGpuInfo();
- // A pin can only draw on the cards it names, so the verdict is measured against
- // those. Judging a one-card pin against a two-card total called an 8 GB load a fit
- // on 16 GB of VRAM it could not reach.
+ // A pin can only draw on the cards it names, so the verdict is measured against those: judging
+ // a one-card pin against a two-card total called an 8 GB load a fit on 16 GB it cannot reach.
const pinnedGpuIds = runtimeConfig.selectedGpuIds;
- // Whether the devices THIS LOAD will use draw on one pool with the rest of the
- // system, from the backend's per-device unified_memory flag rather than from
- // the platform. Apple Silicon is the obvious case and a ROCm APU is the one
- // that was being missed: both share the pool, so adding VRAM to system RAM to
- // reach a machine-wide ceiling counts the same bytes twice.
- //
- // Scoped to the devices this load will actually use, and true only when EVERY
- // one of them is unified.
- //
- // Two separate mistakes were made here, so both are written down. Reading the
- // host-wide flag was the first: an APU beside a discrete card makes it true,
- // and a pin on the discrete card then collapsed totalCapacityGb from 143.5 GiB
- // to 15.5 GiB. Narrowing to the pin but keeping `.some()` was the second: an
- // unpinned load, or a pin naming BOTH, still marked the whole set unified and
- // reported 62.1 GiB instead of 143.5 GiB. Both produce false "more than this
- // machine holds" warnings for loads that fit comfortably.
- //
- // `.every()` is the right question for CAPACITY: one independent-memory device
- // in the set means there is real VRAM beside system RAM, so the two are not
- // one pool. Note use-gpu-info.ts deliberately uses `.some()` for its own flag,
- // and that is not an inconsistency -- it answers a different question, whether
- // the aggregate is still a VRAM ceiling a verdict can be measured against, and
- // one unified part is enough to spoil that.
- //
- // The empty set is excluded explicitly, because `[].every()` is true and a
- // host with no devices at all is not a unified-memory machine.
- //
- // The Apple fallback stays host-wide and unconditional: there every device is
- // the one pool whatever is pinned, and it also covers the window before the
- // per-device probe lands, so this is never a weaker answer than the platform
- // check it replaced.
+ // Whether the devices THIS LOAD will use draw on one pool with the rest of the system, from
+ // the backend's per-device unified_memory flag rather than from the platform: adding VRAM to
+ // system RAM on a shared pool counts the same bytes twice. `.every()` is the right question
+ // for CAPACITY, since one independent-memory device means real VRAM beside system RAM: reading
+ // the host-wide flag collapsed totalCapacityGb from 143.5 to 15.5 GiB, and `.some()` under a pin
+ // reported 62.1. The empty set is excluded explicitly, since `[].every()` is true, and the Apple
+ // fallback stays host-wide, covering the window before the per-device probe lands. use-gpu-info.ts
+ // deliberately uses `.some()` for its own flag, which answers a different question.
const hasUnifiedMemory = useMemo(() => {
if (isAppleUnifiedMemory) return true;
const governing =
@@ -2902,15 +2742,13 @@ export function ModelConfigPage({
if (governing.length === 0) return false;
return governing.every((device) => device.unifiedMemory === true);
}, [gpuDevices, pinnedGpuIds, isAppleUnifiedMemory]);
- // The VRAM Budget slider sits in this same panel and caps what the next load may
- // claim per GPU, so the verdict has to be measured against the capped figure or the
- // row contradicts the control directly above it. Subscribed as well as read once:
- // dragging that slider must re-classify without a remount.
- // Seeded with the loader's own default rather than a full card. The read is async
- // and returns null on failure -- an older backend has no such route -- and in both
- // windows the launch still applies VRAM_FRACTION_DEFAULT. Starting at 1 claimed a
- // reserve no setting of that slider ever gives back, so the row read a hair
- // optimistic until the answer arrived and stayed there if it never did.
+ // The VRAM Budget slider caps what the next load may claim per GPU, so the verdict is measured
+ // against the capped figure or the row contradicts the control above it. Subscribed as well
+ // as read once, since dragging must re-classify without a remount. Seeded with the loader's
+ // own default rather than a full card, since the read is async and null on failure.
+ // Seeded with the loader's own default: the read is async and returns null on an older backend, and
+ // in both windows the launch still applies VRAM_FRACTION_DEFAULT. Starting at 1 claimed a reserve
+ // no setting of that slider ever gives back.
const [memoryVramBudgetFraction, setMemoryVramBudgetFraction] =
useState(DEFAULT_VRAM_FRACTION);
useEffect(() => {
@@ -2928,38 +2766,22 @@ export function ModelConfigPage({
unsubscribe();
};
}, []);
- // What is free RIGHT NOW on the cards this load may use: _select_gpus admits on
- // free-minus-reserve, not on totals, so another process holding VRAM changes what
- // happens to this load.
- //
- // WARNS, never refuses. The bytes a pending load reclaims are mostly the resident
- // model's own, which Studio unloads first, and cannot be attributed per device from
- // here, so raw free memory would call reloading the loaded model impossible. Capping
- // the verdict at "tight" is honest either way.
- //
- // A fixed Manual placement is launched verbatim -- load_model empties its probed GPU
- // set and emits --gpu-layers N --fit off -- so the server-wide VRAM Budget never
- // reaches the planner. Discounting capacity by it called a 16 GiB fixed placement on
- // a 24 GiB card over budget at 50%. Auto is still budgeted: Auto is the mode that
- // hands the decision to the planner.
+ // What is free RIGHT NOW on the cards this load may use: _select_gpus admits on free-minus-
+ // reserve, not on totals. WARNS, never refuses: the bytes a pending load reclaims are mostly
+ // the resident model's own, which cannot be attributed per device from here. A fixed Manual
+ // placement is launched verbatim, so the server-wide budget never reaches the planner.
+ // A fixed Manual placement is launched verbatim -- load_model empties its probed GPU set and emits
+ // --gpu-layers N --fit off -- so the VRAM Budget never reaches the planner. Discounting capacity
+ // by it called a 16 GiB fixed placement on a 24 GiB card over budget at 50%. Auto is still budgeted.
const memoryBudgetGovernsLaunch =
runtimeGpuMemoryMode !== "manual" ||
(runtimeConfig.gpuLayers ?? GPU_LAYERS_AUTO) < 0;
const memoryEffectiveBudgetFraction = memoryBudgetGovernsLaunch
? memoryVramBudgetFraction
: 1;
- // _HOST_RAM_HEADROOM_MIB: the 2 GiB the loader keeps for the rest of the system
- // before admitting an offloaded load.
- //
- // The HOST reading, not the sum-shaped one beside it, which is zeroed whenever
- // ANY device shares system RAM -- every dGPU + iGPU box -- so that was
- // permanently 0 there and the host-pressure advisory could not fire even under
- // a pin on the discrete card, the one case where host RAM really is a separate
- // pool.
- //
- // Hoisted because the free-capacity memo below needs the same figure: a ROCm
- // APU's free pool IS this, and two copies of the subtraction is how they would
- // drift apart.
+ // _HOST_RAM_HEADROOM_MIB: the 2 GiB the loader keeps for the rest of the system before
+ // admitting an offloaded load. The HOST reading, not the sum-shaped one beside it, which is
+ // zeroed whenever ANY device shares system RAM. Hoisted for the free-capacity memo.
const memoryUsableSystemRamGb = Math.max(
0,
(inferenceGpu.systemRamAvailableHostGb || 0) - 2,
@@ -2969,29 +2791,20 @@ export function ModelConfigPage({
pinnedGpuIds && pinnedGpuIds.length > 0
? gpuDevices.filter((device) => pinnedGpuIds.includes(device.index))
: gpuDevices;
- // Per device, and by the loader's absolute-reserve rule rather than a
- // multiplication: the budget is subtracted from the card, not applied to what
- // happens to be free, and the two agree only on an idle one.
- //
- // Aggregated with the same count-the-shared-pool-once rule the TOTALS use. A
- // plain sum over a mixed inventory counted the iGPU's share of host RAM a second
- // time, and this figure is what freeGpuFit -- and through it the fit verdict --
- // is measured against.
- // The reserve keeps its budget-derived floor even for a fixed Manual placement:
- // what is free right now still constrains the launch, whatever set the number.
+ // Per device, and by the loader's absolute-reserve rule rather than a multiplication: the
+ // budget is subtracted from the card, not applied to what happens to be free. Aggregated
+ // with the same count-the-shared-pool-once rule the TOTALS use, since a plain sum over a
+ // mixed inventory counted the iGPU's share of host RAM twice.
const freeVram = aggregateUsableFreeVramGb(
pinned,
memoryEffectiveBudgetFraction,
);
- // On a ROCm APU this figure is the free space inside a BIOS-carved window,
- // and resolveMemoryFit asks it the WHOLE-LOAD question as soon as the pool is
- // single. Those two together warned that a 60 GiB load does not fit a 96 GiB
- // machine with 60+ GiB free, purely because it exceeds a 48 GiB window.
- //
- // The pool's real free memory is the host's, exactly as its real CAPACITY is
- // the host's RAM rather than the window. Same discriminator as the capacity
- // side, so the two cannot answer differently: Apple's GPU figure already IS
- // the pool and is left alone.
+ // On a ROCm APU this figure is the free space inside a BIOS-carved window, and resolveMemoryFit
+ // asks it the WHOLE-LOAD question as soon as the pool is single, so together they warned
+ // that a 60 GiB load does not fit a 96 GiB machine with 60+ GiB free. The pool's real free
+ // memory is the host's; Apple's GPU figure already IS the pool.
+ // Those two together warned that a 60 GiB load does not fit a 96 GiB machine with 60+ GiB free,
+ // purely because it exceeds a 48 GiB window.
if (hasUnifiedMemory && !isAppleUnifiedMemory) {
return Math.max(freeVram, memoryUsableSystemRamGb);
}
@@ -3019,19 +2832,13 @@ export function ModelConfigPage({
hostDedicatedGpuTotalGb: inferenceGpu.dedicatedMemoryTotalGb,
hostSharesSystemRam: inferenceGpu.sharedMemory,
systemRamTotalGb: inferenceGpu.systemRamTotalGb,
- // The GENERAL signal, not the Apple-only one. A ROCm APU reports
- // unified_memory per device and shares one pool exactly as Apple does, but
- // reading appleSilicon here charged it as discrete VRAM PLUS host RAM --
- // the same bytes counted twice, so the panel could report a fit that
- // cannot happen. The Hub bar gets this right and abstains on this very
- // flag; this is the panel catching up.
+ // The GENERAL signal, not the Apple-only one: a ROCm APU reports unified_memory per device and
+ // shares one pool exactly as Apple does, but reading appleSilicon here charged it as
+ // discrete VRAM PLUS host RAM, the same bytes twice.
unifiedMemory: hasUnifiedMemory,
- // ...but "one pool" and "how big is the pool" are different questions, and
- // the two platforms answer the second differently. Apple's memory_total_gb
- // IS the machine's unified memory; a ROCm APU's is a BIOS-carved window
- // onto system RAM, so taking it as the ceiling reported 46.56 GiB on a
- // 96 GiB machine and warned that a 60 GiB load exceeds the host. Only the
- // Apple half may be read as the whole pool.
+ // "One pool" and "how big is the pool" are different questions: Apple's memory_total_gb IS the
+ // machine's unified memory, while a ROCm APU's is a BIOS-carved window onto system RAM, so
+ // taking it as the ceiling reported 46.56 GiB on a 96 GiB machine.
unifiedPoolReportedAsGpuMemory: isAppleUnifiedMemory,
});
@@ -3050,8 +2857,8 @@ export function ModelConfigPage({
return;
}
// Same-click Load/Reload: a numeric draft the user just typed is flushed only by that input's
- // blur handler, which runs after this click closure captured the stale value. Commit every
- // numeric input imperatively so the staged load honors what was typed, not just Context.
+ // blur handler, which runs after this click closure captured the stale value, so commit
+ // every numeric input imperatively.
const committedContext = target.isGguf
? contextInputRef.current?.commit()
: undefined;
@@ -3091,8 +2898,8 @@ export function ModelConfigPage({
? withoutUnsupportedDiffusionSettings(committedConfig, gpuIndexKind)
: committedConfig;
// pinFixedLayerContext above was computed from the render-time config, before the same-click
- // GPU Layers draft was committed. Recompute from effectiveConfig so a positive fixed-layer
- // value still pins the fitted context, else a later fresh load recreates the OOM.
+ // GPU Layers draft was committed, so recompute from effectiveConfig or a later fresh load
+ // recreates the OOM.
const effectivePinFixedLayerContext =
target.isGguf &&
effectiveConfig.gpuMemoryMode === "manual" &&
@@ -3148,7 +2955,7 @@ export function ModelConfigPage({
);
}
// Saving can push the local map over budget and drop other models, whose server entries would
- // keep applying with nothing able to forget them. Not a Forget: only the mirrored fields go.
+ // keep applying with nothing able to forget them. Not a Forget: only mirrored fields go.
for (const dropped of evicted) {
syncModelOverride(dropped.modelId, dropped.ggufVariant, null, {
keepLaunchFlags: true,
@@ -3190,20 +2997,16 @@ export function ModelConfigPage({
setVramBudgetLocked(true);
const stagedBudget = settleVramBudgetSave();
if (stagedBudget) {
- // The click is answered by a network round trip now, so the button stays live
- // for as long as that takes. A second click in that window would settle the
- // same chain again and run onRun twice, i.e. two loads from one intent.
+ // The click is answered by a network round trip now, so the button stays live for as long as
+ // that takes and a second click would run onRun twice.
setBudgetSettling(true);
- // Caught, not voided: finally alone re-rejects into an unhandled rejection,
- // and the load would proceed on the old fraction with nothing said.
+ // Caught, not voided: finally alone re-rejects into an unhandled rejection, and the load would
+ // proceed on the old fraction with nothing said.
void stagedBudget
.catch((error: unknown) => {
- // Dropped rather than left for the next flush: the picker teardown that
- // onRun triggers flushes it, and that PUT would then race the load
- // request this click is about to send. Either fraction could win, which
- // is the one thing this control promises cannot happen. Only the retry
- // goes, never a newer edit staged over it. The toast says the budget
- // did not change, and the slider is still there to retry.
+ // Dropped rather than left for the next flush: the picker teardown that onRun triggers flushes
+ // it, and that PUT would race the load request this click is about to send. Only the retry
+ // goes, never a newer edit staged over it.
dropVramBudgetRetry();
toast.error(
error instanceof Error ? error.message : "Failed to save VRAM budget",
@@ -3252,8 +3055,8 @@ export function ModelConfigPage({
<div className="space-y-3.5">
{target.isGguf && (
<>
- {/* Above Context Length on purpose: that is the control moving this
- number most, and a readout below it is one you go looking for. */}
+ {/* Above Context Length on purpose: that is the control moving this number most, and a readout
+ below it is one you go looking for. */}
<MemoryEstimateRow
estimate={memoryEstimate.estimate}
loading={memoryEstimate.loading}
@@ -3308,11 +3111,8 @@ export function ModelConfigPage({
onValueChange={([v]) => setContextSliderValue(v)}
className="panel-slider"
aria-label="Context Length"
- // Position 0 is Auto, not a zero-token context, so
- // aria-valuenow alone reads as a length no model has. The
- // number is only spoken once one exists: before a load
- // contextInputValue is the offload fallback used to seed the
- // input, not a selection, and Auto may still fit native.
+ // Position 0 is Auto, not a zero-token context, so aria-valuenow alone reads as a length no
+ // model has. The number is only spoken once one exists.
thumbValueText={(v) =>
v !== 0
? `${v.toLocaleString()} tokens`
@@ -3441,9 +3241,8 @@ export function ModelConfigPage({
disabled={atDefault}
onClick={() =>
setConfig({
- // null, not the default's absent: absent omits the field, and the
- // load then INHERITS the running process's arguments, so a reload
- // after Reset kept the very flags the empty box says are gone.
+ // null, not the default's absent: absent omits the field, and the load then INHERITS the
+ // running process's arguments, so a reload after Reset kept the flags the box says are gone.
...DEFAULT_PER_MODEL_CONFIG,
llamaExtraArgs: null,
})
diff --git a/studio/frontend/src/features/model-picker/components/model-selector.tsx b/studio/frontend/src/features/model-picker/components/model-selector.tsx
index bba535cef2c..2f81ff30250 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector.tsx
@@ -132,26 +132,21 @@ export type { ExternalConnectionRef } from "./model-selector/missing-external-mo
interface ModelSelectorProps {
models: ModelOption[];
- /** Models a task-specific runtime confirms are locally loadable even when
- * their specialized cache layout is absent from the generic Hub inventory. */
+ /** Models a task-specific runtime confirms are locally loadable even when their specialized
+ * cache layout is absent from the generic Hub inventory. */
additionalOnDeviceModels?: ModelOption[];
/** Task-owned runtime residency when it is separate from Chat's main slot. */
loadedModelIdOverride?: string;
loraModels?: LoraModelOption[];
externalModels?: ExternalModelOption[];
- /**
- * The connections behind `externalModels`, carrying each one's cached catalogue.
- * `externalModels` lists only the models the user ticked, so it cannot tell a model the
- * user turned off from one the provider withdrew; the catalogue can.
- */
+ /** The connections behind `externalModels`, carrying each one's cached catalogue.
+ * `externalModels` lists only the models the user ticked, so it cannot tell a model the user
+ * turned off from one the provider withdrew; the catalogue can. */
externalConnections?: ExternalConnectionRef[];
value?: string;
defaultValue?: string;
- /**
- * Whether the selection is actually resident. Omitted means "a selection is
- * a load", which is what every caller assumed until an image or video load
- * started evicting the chat model out from under this tick.
- */
+ /** Whether the selection is actually resident. Omitted means "a selection is a load", which
+ * every caller assumed until an image or video load started evicting the chat model. */
loaded?: boolean;
activeGgufVariant?: string | null;
activeModelConfig?: PerModelConfig | null;
@@ -178,12 +173,14 @@ interface ModelSelectorProps {
showCloudIndicator?: boolean;
/** Restrict the Hub tab to a pipeline task (e.g. text-to-image). */
task?: HfTaskFilter;
- /** Canonical model groups (Images / Video pages): collapses a model's artifact repos into one row with a format second level and device-aware routing. Undefined (chat) changes nothing. */
+ /** Canonical model groups (Images / Video pages): collapses a model's artifact repos into one
+ * row with a format second level and device-aware routing. Undefined (chat) changes nothing. */
catalog?: CatalogGroup[];
- /** Also list community (non-unsloth) models for `task`. Opt-in: only pages
- * whose runtime loads arbitrary publishers. */
+ /** Also list community (non-unsloth) models for `task`. Opt-in: only pages whose runtime loads
+ * arbitrary publishers. */
communityModelPolicy?: CommunityModelPolicy;
- /** Trigger text when nothing is loaded. Defaults to "Select model"; task pages name what they pick so it reads as separate from the chat model. */
+ /** Trigger text when nothing is loaded. Defaults to "Select model"; task pages name what they
+ * pick so it reads as separate from the chat model. */
placeholder?: string;
}
@@ -236,8 +233,8 @@ function ModelSelectorTrigger({
{isLoaded &&
(onEject ? (
// Loaded status doubles as a mouse eject shortcut (checkmark at rest, eject on hover). A plain
- // span keeps it out of the trigger button's content model; keyboard/SR users eject via the
- // "Eject model" button. stopPropagation stops the popover toggling, and on touch (no hover)
+ // span keeps it out of the trigger button's content model; keyboard and SR users eject via
+ // the "Eject model" button. stopPropagation stops the popover toggling, and on touch
// pointer-events-none disables it so taps open the picker instead.
<span
aria-hidden={true}
@@ -449,8 +446,8 @@ function ModelSelectorContent({
null,
);
- // The picker remounts on each open but this section state does not, so
- // re-derive the default section on the open edge.
+ // The picker remounts on each open but this section state does not, so re-derive the default
+ // section on the open edge.
const wasOpen = useRef(open);
useEffect(() => {
if (open && !wasOpen.current) {
@@ -516,7 +513,8 @@ function ModelSelectorContent({
displayName: meta.ggufVariant ? `${leaf} · ${meta.ggufVariant}` : leaf,
ggufVariant: meta.ggufVariant ?? null,
isGguf,
- // Ollama's models sit under a link dir the resolver skips, so mirroring their settings would advertise an impossible load.
+ // Ollama's models sit under a link dir the resolver skips, so mirroring their settings would
+ // advertise an impossible load.
apiLoadable: isGguf && !isOllamaLinkPath(id),
meta,
});
@@ -550,8 +548,8 @@ function ModelSelectorContent({
"max-h-[var(--radix-popover-content-available-height)] w-[min(468px,calc(100vw-1rem))] overflow-hidden p-0"
: cn(
"pt-4 pb-0 pl-4",
- // Sized so the left-packed row keeps uniform gaps and the last dropdown's right gap matches the
- // pill's left gap. Widths track the controls they hold so the single-line row does not wrap.
+ // Sized so the left-packed row keeps uniform gaps and the last dropdown's right gap matches
+ // the pill's left gap. Widths track the controls they hold so the row does not wrap.
hasExternal
? "w-[min(var(--picker-panel-w-external),calc(100vw-1rem))] pr-4"
: "w-[min(var(--picker-panel-w),calc(100vw-1rem))] pr-2",
@@ -694,8 +692,8 @@ export function ModelSelector({
const [uncontrolled, setUncontrolled] = useState(defaultValue ?? "");
const selected = value ?? uncontrolled;
- // A selection is only a load when the caller has not said otherwise: the chat
- // model can be evicted by an image or video load while the pick survives.
+ // A selection is only a load when the caller has not said otherwise: the chat model can be
+ // evicted by an image or video load while the pick survives.
const isLoaded = selected !== "" && (loaded ?? true);
const optionById = useMemo(() => {
@@ -747,18 +745,16 @@ export function ModelSelector({
const currentModel = useMemo(() => {
if (!selected) return undefined;
const found = optionById.get(selected);
- // A pick whose connection no longer offers it takes its option away and leaves the
- // id in the checkpoint, and the generic fallback below cannot shorten an
- // `external::` id. Name the model the user picked, and say why it is unusable: it
- // cannot be loaded, so a tidy name on its own would hide the failure until the next
- // send (#8405). The connections carry the cached catalogue, which is what separates a
- // model the user unticked from one the provider withdrew.
+ // A pick whose connection no longer offers it takes its option away and leaves the id in the
+ // checkpoint, and the generic fallback cannot shorten an `external::` id. Name the model the
+ // user picked and say why it is unusable, or a tidy name would hide the failure until the
+ // next send (#8405). The connections carry the cached catalogue, which separates a model the
+ // user unticked from one the provider withdrew.
const missingExternal = found
? null
: missingExternalModel(selected, externalModels, externalConnections);
- // No catalog entry (yet, or ever); a cached GGUF's checkpoint is a snapshot path.
- // The leaf, not the namespaced public id (#7966), matches the catalog row that
- // later replaces this one.
+ // No catalog entry (yet, or ever); a cached GGUF's checkpoint is a snapshot path. The leaf,
+ // not the namespaced public id (#7966), matches the catalog row that later replaces this one.
const fallbackName = missingExternal?.modelName ?? modelDisplayName(selected);
if (activeGgufVariant) {
const desc = `GGUF · ${activeGgufVariant}`;
@@ -841,8 +837,8 @@ export function ModelSelector({
resolveDownloadFootprint={resolveDownloadFootprint}
onEject={onEject ? handleEject : undefined}
onFoldersChange={onFoldersChange}
- // A curated task picker (Images / Video) is self-contained, so it omits this.
- // A community-enabled one (Audio) already lists past unsloth, so it keeps it.
+ // A curated task picker (Images / Video) is self-contained, so it omits this. A
+ // community-enabled one (Audio) already lists past unsloth, so it keeps it.
onBrowseHub={
task && communityModelPolicy === "none" ? undefined : handleBrowseHub
}
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/audio-picker-policy.ts b/studio/frontend/src/features/model-picker/components/model-selector/audio-picker-policy.ts
index c353b112c76..ed6eb44bab7 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/audio-picker-policy.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/audio-picker-policy.ts
@@ -50,9 +50,9 @@ export function nativeAudioCheckpointIsLoadable(
return !audioType || !NATIVE_AUDIO_TYPES.has(audioType) || exportType === "merged";
}
-/** Community ASR runs through the Transformers Whisper sidecar. Curated GGUF/MTMD
- * artifacts are handled by the catalog before this gate, so an uncurated row must
- * identify a non-GGUF Whisper checkpoint. */
+/** Community ASR runs through the Transformers Whisper sidecar. Curated GGUF/MTMD artifacts
+ * are handled by the catalog before this gate, so an uncurated row must identify a
+ * non-GGUF Whisper checkpoint. */
export function communityAudioRowIsRunnable({
isStt,
isTts,
@@ -87,12 +87,11 @@ export function communityAudioRowIsRunnable({
if (audioType && NATIVE_AUDIO_TYPES.has(audioType)) return true;
- // The main-slot TTS backend decodes only the four codec families below.
- // Hub's text-to-speech tag also covers Bark, VITS, SpeechT5, and many other
- // architectures that can load as language models but cannot emit a WAV here.
- // Llasa is NOT here despite being a well-known TTS family: it speaks XCodec2, which
- // AudioCodecManager cannot decode, so admitting it produced a row that loaded and then
- // failed at generation. The list and the comment above must stay in step.
+ // The main-slot TTS backend decodes only the four codec families below. Hub's
+ // text-to-speech tag also covers Bark, VITS, SpeechT5 and others that load as language
+ // models but cannot emit a WAV here. Llasa is excluded despite being well known: it speaks
+ // XCodec2, which AudioCodecManager cannot decode, so it produced a row that loaded and
+ // then failed at generation. This list and the comment above must stay in step.
const normalizedAudioType = (audioType ?? "").toLowerCase();
if (["snac", "bicodec", "dac"].includes(normalizedAudioType)) return true;
if (normalizedAudioType === "csm") return !isGguf;
@@ -105,7 +104,7 @@ export function communityAudioRowIsRunnable({
}
/** A GGUF llama.cpp cannot decode, however it was found: CSM is Transformers-only, so it
- * never loads in llama-server. Beside communityAudioRowIsRunnable's list to stay in step. */
+ * never loads in llama-server. Beside communityAudioRowIsRunnable's list to stay in step. */
export function speechGgufIsUndecodable({
isGguf,
id,
@@ -123,14 +122,14 @@ export function speechGgufIsUndecodable({
.some((value) => CSM_PATH_SEGMENT.test(value));
}
-/** `csm` as its own path or name segment. The separator class carries a BACKSLASH as well as a
- * slash: this is handed local checkpoint paths, and on Windows `C:\models\csm-1b\model.gguf`
- * reads as one long segment to a posix-only class, and clears. */
+/** `csm` as its own path or name segment. The separator class carries a BACKSLASH as well as
+ * a slash: local checkpoint paths arrive here, and on Windows
+ * `C:\models\csm-1b\model.gguf` reads as one segment to a posix-only class. */
const CSM_PATH_SEGMENT = /(?:^|[-_./\\])csm(?:$|[-_./\\])/;
/** Whether a fine-tuned or exported row is a CSM checkpoint in a GGUF container, which no
- * runtime here decodes. `audioType` is read off the checkpoint by the backend, so it holds
- * even where nothing in the path says "csm". */
+ * runtime here decodes. `audioType` is read off the checkpoint by the backend, so it holds
+ * even where nothing in the path says "csm". */
export function localAudioRowIsUndecodableGguf({
audioType,
exportType,
@@ -144,12 +143,10 @@ export function localAudioRowIsUndecodableGguf({
return isGguf && (audioType ?? "").toLowerCase() === "csm";
}
-/** Whether an audio pick from the chat picker may be routed to the Audio page.
- *
- * The page's own lists apply `communityAudioRowIsRunnable`, so routing a repo that fails
- * it lands on a page that cannot show the row, and its `?model=` handoff evicts the chat
- * model before reporting the repo unsupported. Curated ids always route: the catalog,
- * not the tag, is their runtime contract. */
+/** Whether an audio pick from the chat picker may be routed to the Audio page. The page's own
+ * lists apply `communityAudioRowIsRunnable`, so routing a repo that fails it lands on a page
+ * that cannot show the row, and its `?model=` handoff evicts the chat model first. Curated
+ * ids always route: the catalog, not the tag, is their runtime contract. */
export function audioPickIsRoutable({
id,
task,
@@ -175,31 +172,30 @@ export function audioPickIsRoutable({
libraryName?: string | null;
audioType?: string | null;
}): boolean {
- // GGUF speech tasks have two provenances: Orpheus retains the ordinary llama
- // architecture and is runnable by Audio's SNAC path; the dedicated CSM speech
- // architectures remain unsupported. Unknown old-backend rows fail closed.
+ // GGUF speech tasks have two provenances: Orpheus retains the ordinary llama architecture and
+ // runs on Audio's SNAC path, while the dedicated CSM speech architectures are unsupported.
+ // Unknown old-backend rows fail closed.
if (taskFromGgufArch && isGguf && task === "text-to-speech") {
const codec = (audioType ?? "").toLowerCase();
if (codec === "csm" || !codec) return false;
return ["snac", "bicodec", "dac"].includes(codec);
}
if (isCurated) return true;
- // A checkpoint from outputs/ has no Hub identity for communityAudioRowIsRunnable to
- // judge, and the family-name heuristic it applies would reject it on its directory
- // name. Its task came from the backend reading the checkpoint, which is the stronger
- // signal, and the Audio page lists it off that same tag.
+ // A checkpoint from outputs/ has no Hub identity to judge, and the family-name heuristic would
+ // reject it on its directory name. Its task came from the backend reading the checkpoint,
+ // the stronger signal, and the Audio page lists it off that same tag.
if (isLocalCheckpoint) {
- // Provenance says nothing about the decoder: a CSM GGUF found on disk is as
- // unrunnable as a cached one, and routing it hands Audio a row it cannot show after
- // the ?model= handoff already evicted the chat model.
+ // Provenance says nothing about the decoder: a CSM GGUF found on disk is as unrunnable as a
+ // cached one, and routing it hands Audio a row it cannot show after the handoff already
+ // evicted the chat model.
if (speechGgufIsUndecodable({ isGguf, id, baseModel, tags })) return false;
return (
task === "text-to-speech" || task === "automatic-speech-recognition"
);
}
- // The same Hub evidence the Audio page's own lists judge on. Passing the id alone
- // rejected a checkpoint whose family is in its tags or base model rather than its
- // name, so the page listed it but the chat picker refused to route to it.
+ // The same Hub evidence the Audio page's own lists judge on. Passing the id alone rejected a
+ // checkpoint whose family is in its tags or base model rather than its name, so the page
+ // listed it but the chat picker refused to route.
return communityAudioRowIsRunnable({
isStt: task === "automatic-speech-recognition",
isTts: task === "text-to-speech",
@@ -212,9 +208,8 @@ export function audioPickIsRoutable({
});
}
-/** The macOS audio runtime can execute TTS only through llama.cpp GGUF. Curated
- * families may expose a non-GGUF canonical row because Audio resolves it to a published
- * GGUF sibling before loading. */
+/** The macOS audio runtime can execute TTS only through llama.cpp GGUF. Curated families may
+ * expose a non-GGUF canonical row because Audio resolves it to a published GGUF sibling. */
export function macTtsHubRowIsRunnable({
isMac,
isTts,
@@ -275,9 +270,9 @@ export function taskPickerRowMatches({
return format === "all" ? isRecommendable : matchesFormat;
}
-/** A downloaded GGUF often exposes only its base architecture (Orpheus reports
- * `llama`), so generic inventory correctly calls it chat. An exact curated Audio
- * artifact is a stronger contract, but only for its own active Audio mode. */
+/** A downloaded GGUF often exposes only its base architecture (Orpheus reports `llama`), so
+ * generic inventory correctly calls it chat. An exact curated Audio artifact is a stronger
+ * contract, but only for its own active Audio mode. */
export function curatedAudioInventoryMatches({
isActiveCatalogArtifact,
catalogScope,
@@ -303,9 +298,8 @@ export function curatedAudioInventoryMatches({
: pickerTask === expected;
}
-/** A cached Audio GGUF can be classified from its base architecture as text-generation.
- * Only an exact catalog artifact may replace that fallback when Chat decides which task
- * page owns the pick. */
+/** A cached Audio GGUF can be classified from its base architecture as text-generation. Only
+ * an exact catalog artifact may replace that fallback when Chat decides which page owns it. */
export function curatedAudioInventoryTask({
inventoryTask,
isExactCatalogArtifact,
@@ -330,8 +324,8 @@ export function curatedAudioInventoryTask({
: "automatic-speech-recognition";
}
-/** Hidden infrastructure rows remain hidden unless the active task page passed
- * their exact normalized artifact id as an explicit runtime contract. */
+/** Hidden infrastructure rows stay hidden unless the active task page passed their exact
+ * normalized artifact id as an explicit runtime contract. */
export function allowedHiddenModelIdMatches(
allowedHiddenModelIds: ReadonlySet<string> | undefined,
...modelIds: (string | null | undefined)[]
@@ -343,30 +337,29 @@ export function allowedHiddenModelIdMatches(
);
}
-/** Backend tag for a diffusion GGUF the Images backend cannot assemble; both pickers hide
- * these rows. */
+/** Backend tag for a diffusion GGUF the Images backend cannot assemble; both pickers hide these rows. */
const UNSUPPORTED_DIFFUSION_TASK = "image-diffusion-unsupported";
/** Fresh Hub rows carry their authoritative task in selection metadata, while
- * downloaded/local rows retain their inventory task as a fallback. */
+ * downloaded/local rows retain their inventory task as a fallback. */
export function taskForMediaPick(
pipelineTag: string | null | undefined,
inventoryTask: string | null | undefined,
): string | null {
- // Such a GGUF still carries an ordinary text-to-image tag on the Hub. The on-device
- // verdict is the one the loader enforces, so it outranks the tag: trusting the tag routes
- // the pick at a page whose picker omits the row and whose load would be refused.
+ // Such a GGUF still carries an ordinary text-to-image tag on the Hub. The on-device verdict is
+ // what the loader enforces, so it outranks the tag: trusting the tag routes the pick at a
+ // page whose picker omits the row and whose load would be refused.
if (inventoryTask === UNSUPPORTED_DIFFUSION_TASK) return inventoryTask;
- // Cache inventory commonly reports Audio GGUFs as generic text-generation;
- // an exact catalog task is the stronger runtime contract in that case.
+ // Cache inventory commonly reports Audio GGUFs as generic text-generation; an exact catalog
+ // task is the stronger runtime contract there.
return pipelineTag && pipelineTag !== "text-generation"
? pipelineTag
: (inventoryTask ?? pipelineTag ?? null);
}
-/** Filesystem checkpoints cannot be served by the STT sidecars yet. Keep cached Hub
- * snapshots and curated artifacts visible, but not local-directory rows that would send
- * an absolute path to the Hub-only API. */
+/** Filesystem checkpoints cannot be served by the STT sidecars yet. Keep cached Hub snapshots
+ * and curated artifacts visible, but not local-directory rows that would send an absolute
+ * path to the Hub-only API. */
export function filesystemRowsSupportedForTask(
pickerTask: string | readonly string[] | null | undefined,
rowTask?: string | null,
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/folder-browser.tsx b/studio/frontend/src/features/model-picker/components/model-selector/folder-browser.tsx
index 0a5401cdc91..283d04abb21 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/folder-browser.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/folder-browser.tsx
@@ -38,9 +38,8 @@ export interface FolderBrowserProps {
function splitBreadcrumb(path: string): { label: string; value: string }[] {
if (!path) return [];
- // Detect path style BEFORE normalizing: on POSIX `\` is a valid filename char,
- // so rewriting `\` -> `/` would mangle names like `my\backup`. Only Windows
- // paths (drive letter or UNC) convert.
+ // Detect path style BEFORE normalizing: on POSIX `\` is a valid filename char, so rewriting it
+ // would mangle names like `my\backup`. Only drive-letter or UNC paths convert.
const isWindowsDrive =
/^[A-Za-z]:[\\/]/.test(path) || /^[A-Za-z]:$/.test(path);
const isUnc = /^\\\\/.test(path);
@@ -61,8 +60,8 @@ function splitBreadcrumb(path: string): { label: string; value: string }[] {
return parts;
}
- // Windows drive path: use `C:/` as the crumb value so clicking the drive root
- // goes to the drive root, not the drive-relative CWD (`C:` alone is CWD-on-C).
+ // Windows drive path: use `C:/` as the crumb value so clicking the drive root goes to the drive
+ // root, not the drive-relative CWD (`C:` alone is CWD-on-C).
if (/^[A-Za-z]:$/.test(segments[0])) {
const driveRoot = `${segments[0]}/`;
let cur = driveRoot;
@@ -107,8 +106,7 @@ export function FolderBrowser({
abortRef.current = ctrl;
setLoading(true);
setError(null);
- // Forward the signal so cancelled navigation aborts the backend
- // enumeration, not just the response.
+ // Forward the signal so cancelled navigation aborts the backend enumeration, not just the response.
browseFolders(target, hidden, ctrl.signal)
.then((res) => {
if (ctrl.signal.aborted) return;
@@ -117,13 +115,13 @@ export function FolderBrowser({
})
.catch((err) => {
if (ctrl.signal.aborted) return;
- // Surface the error; if the first request (e.g. a bad initialPath)
- // fails, fall back to HOME so the modal stays navigable.
+ // Surface the error; if the first request (e.g. a bad initialPath) fails, fall back to HOME so
+ // the modal stays navigable.
const message = err instanceof Error ? err.message : String(err);
setError(message);
if (opts?.fallbackOnError && target !== undefined) {
- // Re-issue without a target -> backend defaults to HOME.
- // Don't recurse if HOME itself fails (allowlist always has HOME).
+ // Re-issue without a target, so the backend defaults to HOME. Do not recurse if HOME itself
+ // fails (the allowlist always has HOME).
queueMicrotask(() => navigate(undefined, hidden));
}
})
@@ -137,8 +135,7 @@ export function FolderBrowser({
// biome-ignore lint/correctness/useExhaustiveDependencies: only reopening resets navigation to the initial path
useEffect(() => {
if (!open) return;
- // fallbackOnError: recover into HOME if initialPath is bad, rather than
- // showing an empty modal.
+ // fallbackOnError: recover into HOME if initialPath is bad, rather than showing an empty modal.
navigate(initialPath, showHidden, { fallbackOnError: true });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open]);
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/host-artifact-policy.ts b/studio/frontend/src/features/model-picker/components/model-selector/host-artifact-policy.ts
index 28121c8be52..065964eba0b 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/host-artifact-policy.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/host-artifact-policy.ts
@@ -1,16 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * What the host can actually run, as opposed to what fits on it.
- *
- * Capability, not size: `curatedArtifactFitsDevice` already answers "is there room", and the two
- * must stay apart. A 128 GB Mac has room for the H3 pipeline and still cannot load it.
- *
- * `unknown` is not a formality. The GPU hook starts at `available: false, budgetKnown: false`, so
- * a boolean would read every first render as CPU-only and blink the non-GGUF rows out and back on
- * a real GPU host.
- */
+/** What the host can actually run, as opposed to what fits on it. Capability, not size:
+ * `curatedArtifactFitsDevice` already answers "is there room", and the two must stay apart. A
+ * 128 GB Mac has room for the H3 pipeline and still cannot load it. `unknown` is not a
+ * formality: the GPU hook starts at `available: false, budgetKnown: false`, so a boolean would
+ * read every first render as CPU-only and blink the non-GGUF rows out on a real GPU host. */
export type HostClass = "unknown" | "gguf-only" | "accelerated";
/** Backends whose diffusion pipelines the media pages can place. */
@@ -29,8 +24,8 @@ export function classifyHost({
budgetKnown?: boolean;
}): HostClass {
// Mac outranks the backend string. Apple GPUs report as available and Unsloth may name the
- // backend mlx or cpu depending on what torch found, but no Mac can place a Modular Diffusers
- // workflow: it needs mem_get_info, which torch.mps does not expose. video.py refuses the load.
+ // backend mlx or cpu, but no Mac can place a Modular Diffusers workflow: it needs
+ // mem_get_info, which torch.mps does not expose, and video.py refuses the load.
if (deviceType === "mac") return "gguf-only";
const backend = (deviceBackend ?? "").trim().toLowerCase();
if (!(backend && budgetKnown)) return "unknown";
@@ -43,21 +38,15 @@ export function classifyHost({
/** The H3 group, whose two rows differ by roughly 10x in throughput. */
const H3_PIPELINE_ID = "minimaxai/minimax-h3";
-/**
- * The curated artifacts a host without an accelerator is refused at load, by repo id.
- *
- * Keyed on the id, NOT on `format !== "gguf"`. Everything else in the catalogs loads here: the
- * diffusion pipelines resolve their device through the shared target and run on MPS (Z-Image is
- * tuned for MPS bfloat16 by name), video_capability() certifies Apple Silicon, and the STT rows
- * run through the whisper.cpp sidecar whatever format the catalog labels them. Only MiniMax-H3's
- * Modular Diffusers workflow is genuinely unplaceable: enable_auto_cpu_offload needs
- * mem_get_info, torch.mps has none, and video.py raises before the download.
- */
+/** The curated artifacts a host without an accelerator is refused at load, by repo id. Keyed on
+ * the id, NOT on `format !== "gguf"`: everything else in the catalogs loads here, since the
+ * diffusion pipelines run on MPS, video_capability() certifies Apple Silicon, and the STT rows
+ * run through the whisper.cpp sidecar. Only MiniMax-H3's Modular Diffusers workflow is
+ * genuinely unplaceable, since enable_auto_cpu_offload needs mem_get_info. */
const UNPLACEABLE_WITHOUT_ACCELERATOR = new Set([H3_PIPELINE_ID]);
-/** Whether a curated artifact is worth offering on this host.
- *
- * Only browse rows are filtered. A model already on disk keeps its row wherever it came from. */
+/** Whether a curated artifact is worth offering on this host. Only browse rows are filtered. A
+ * model already on disk keeps its row wherever it came from. */
export function curatedArtifactIsOfferable(
repoId: string,
host: HostClass,
@@ -68,13 +57,9 @@ export function curatedArtifactIsOfferable(
const H3_GGUF_ID = "unsloth/minimax-h3-gguf";
-/**
- * The speed qualifier for a curated row, or null when it earns none.
- *
- * Deliberately keyed on the two H3 ids rather than on `format !== "gguf"`. Most non-GGUF rows are
- * plain bf16 or bnb-4bit, Auto precision resolves to int8 or bf16 on cards without fp8, and only
- * H3 has the measured gap this wording claims.
- */
+/** The speed qualifier for a curated row, or null when it earns none. Deliberately keyed on the
+ * two H3 ids rather than on `format !== "gguf"`: most non-GGUF rows are plain bf16 or
+ * bnb-4bit, and only H3 has the measured gap this wording claims. */
export function h3PerfSuffix(repoId: string, host: HostClass): string | null {
if (host !== "accelerated") return null;
const id = repoId.trim().toLowerCase();
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/local-gguf-policy.ts b/studio/frontend/src/features/model-picker/components/model-selector/local-gguf-policy.ts
index d493ae08e12..8ae1e5b8482 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/local-gguf-policy.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/local-gguf-policy.ts
@@ -5,8 +5,8 @@ import type { LoraModelOption } from "./types";
export type LocalGgufKind = "direct" | "variants" | null;
-/** Resolve local GGUF interaction semantics without letting repo-name heuristics
- * override an explicit one-artifact inventory source such as Ollama. */
+/** Resolve local GGUF interaction semantics without letting repo-name heuristics override an
+ * explicit one-artifact inventory source such as Ollama. */
export function localGgufKindFor(
option: Pick<LoraModelOption, "source" | "isDirectGguf">,
looksLikeVariantRepo: boolean,
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/missing-external-model.ts b/studio/frontend/src/features/model-picker/components/model-selector/missing-external-model.ts
index a7ef314120c..b80dfe2b0ad 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/missing-external-model.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/missing-external-model.ts
@@ -15,11 +15,9 @@ export interface ExternalModelRef {
providerType: string;
}
-/**
- * The connection-level fields this helper needs beyond the enabled options: a connection
- * caches the provider's whole catalogue in `availableModels`, while `models` holds only the
- * subset the user ticked, and `externalModels` is built from `models` alone.
- */
+/** The connection-level fields this helper needs beyond the enabled options: a connection caches
+ * the provider's whole catalogue in `availableModels`, while `models` holds only the subset
+ * the user ticked, and `externalModels` is built from `models` alone. */
export interface ExternalConnectionRef {
id: string;
name: string;
@@ -28,12 +26,8 @@ export interface ExternalConnectionRef {
availableModels?: readonly string[];
}
-/**
- * Why the selection has no option behind it.
- *
- * `disabled` is the user's own doing and reversible from the connection dialog; `dropped` is
- * the provider's, and no amount of re-ticking brings the model back.
- */
+/** Why the selection has no option behind it. `disabled` is the user's own doing and reversible
+ * from the connection dialog; `dropped` is the provider's, and no re-ticking brings it back. */
export type MissingExternalModelState = "disabled" | "dropped";
export interface MissingExternalModel {
@@ -47,37 +41,17 @@ export interface MissingExternalModel {
state: MissingExternalModelState;
}
-/**
- * Describes an `external::<connectionId>::<modelId>` selection with no option behind it, or
- * null for any other selection.
- *
- * Fetch Models replaces a connection's model list wholesale, and nothing reconciles the
- * active pick against it, so a model the provider dropped leaves the picker's option list
- * while its id stays in `params.checkpoint`. The picker's generic fallback cannot shorten
- * that id: `modelDisplayName` is an exact identity function for it, since the id is neither
- * path-shaped nor a `org/name` repo id, so the trigger printed the raw `external::…` string
- * for a model that can no longer be loaded (#8405).
- *
- * Parsing recovers the name the user actually picked. The connection's display name comes
- * from any sibling model still listed under the same connection id, and for a disabled pick
- * from the connection itself, which is still there even when every one of its models is
- * unticked.
- *
- * An option can go away two ways, and only one of them is the provider's doing. The
- * connection dialog saves the ticked ids as `models` and the fetched catalogue as
- * `availableModels`, so unticking the active model drops it from `models` while it stays in
- * `availableModels`. Since `externalModels` is flat-mapped from `models`, the enabled list
- * alone cannot tell the two apart and naming the provider there accuses it of a withdrawal
- * the user performed. `dropped` is therefore claimed only on positive evidence: a catalogue
- * that was cached, is non-empty, does not list the model, and could have listed it in the
- * first place. That last clause rules out the connections whose dialog offers a manual
- * model-ID box beside the fetched list, since a typed-in ID is saved to `models` only and a
- * catalogue that never carried it says nothing when the user later deletes it. Anything
- * less, including a connection saved before `availableModels` existed, is reported as
- * `disabled`, whose wording is true whenever the id is absent from `models` and which claims
- * nothing about the provider. A connection that is not in *connections* at all is not a
- * claim we can soften, so it keeps the `dropped` reading it has always had.
- */
+/** Describes an `external::<connectionId>::<modelId>` selection with no option behind it, or null
+ * for any other selection. Fetch Models replaces a connection's model list wholesale and
+ * nothing reconciles the active pick, so a dropped model leaves the option list while its id
+ * stays in `params.checkpoint`, and the generic fallback cannot shorten that id, so the trigger
+ * printed the raw `external::` string (#8405). Parsing recovers the name the user picked.
+ * An option can go away two ways and only one is the provider's doing: unticking drops the id
+ * from `models` while it stays in `availableModels`, so the enabled list alone cannot tell
+ * them apart. `dropped` is therefore claimed only on positive evidence: a catalogue that was
+ * cached, is non-empty, does not list the model, and could have listed it. That last clause
+ * rules out connections whose dialog offers a manual model-ID box. Anything less is reported
+ * as `disabled`, which claims nothing about the provider. */
export function missingExternalModel(
selected: string | null | undefined,
externalModels: readonly ExternalModelRef[],
@@ -98,15 +72,15 @@ export function missingExternalModel(
(entry) => entry.id === parsed.providerId,
);
const catalog = connection?.availableModels;
- // Ollama, vLLM, llama.cpp and OpenRouter take typed-in model IDs beside the fetched list,
- // and the dialog saves those to `models` alone, so their catalogue never carried them and
- // its silence about one is not evidence of anything.
+ // Ollama, vLLM, llama.cpp and OpenRouter take typed-in model IDs beside the fetched list, and
+ // the dialog saves those to `models` alone, so their catalogue never carried them and its
+ // silence about one is not evidence of anything.
const catalogCoversEveryId = !allowsManualModelIdsWithCatalog(
connection?.providerType,
);
- // `modelId` is decoded and `availableModels` holds raw provider ids, so these compare
- // directly; an empty catalogue is unknown rather than empty, since a connection with no
- // enabled models never gets one written.
+ // `modelId` is decoded and `availableModels` holds raw provider ids, so these compare directly;
+ // an empty catalogue is unknown rather than empty, since a connection with no enabled models
+ // never gets one written.
const dropped =
connection == null ||
(catalogCoversEveryId &&
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/model-capabilities.ts b/studio/frontend/src/features/model-picker/components/model-selector/model-capabilities.ts
index 84ebe0f61a2..419db742942 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/model-capabilities.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/model-capabilities.ts
@@ -1,9 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Pure helpers that infer what a model can do (vision / reasoning / audio / media
-// generation) from its HF tags + pipeline tag, falling back to repo-name keywords.
-// No React/DOM deps so they stay easy to test.
+// Pure helpers that infer what a model can do (vision / reasoning / audio / media generation)
+// from its HF tags and pipeline tag, falling back to repo-name keywords. No React/DOM deps.
export interface ModelCapabilities {
vision: boolean;
@@ -42,8 +41,8 @@ const IMAGE_GEN_TAGS = new Set([
"inpainting",
]);
// Every tag here must be one the Video page can run, or a row draws a glyph promising something
-// no page in Unsloth delivers. video-to-video is deliberately absent: the backend takes a prompt
-// and reference IMAGES (routes/video.py), never a source video, so such a model has nowhere to go.
+// no page delivers. video-to-video is deliberately absent: the backend takes a prompt and
+// reference IMAGES (routes/video.py), never a source video.
const VIDEO_GEN_TAGS = new Set([
"text-to-video",
"image-to-video",
@@ -66,13 +65,11 @@ const AUDIO_NAME_RE = new RegExp(
`${SEP}(?:whisper|asr|tts|parakeet|parler|musicgen|bark|orpheus|csm|voice|speech|audio)${END}`,
"i",
);
-// Families, not the word "image": a local GGUF carries no tags, and "Z-Image-Turbo-GGUF" has to
+// Families, not the word "image": a local GGUF carries no tags, so "Z-Image-Turbo-GGUF" has to
// read as an image generator from its name alone. Video is matched FIRST below, since
-// "HunyuanVideo" and "hunyuanimage" share a stem and the video families are the narrower set.
-//
-// These end on a letter boundary rather than END: a family stem runs straight into its version
-// ("flux1", "sd3", "Wan2.2"), so what must not follow is more WORD. Without it "fluxion-7b",
-// "ltxtra-2b" and "SVDQuant" all read as generators.
+// "HunyuanVideo" and "hunyuanimage" share a stem and the video families are narrower. These
+// end on a letter boundary rather than END, since a family stem runs into its version
+// ("flux1", "sd3"); without it "fluxion-7b" and "SVDQuant" read as generators.
const FAMILY_END = "(?![a-z])";
const IMAGE_GEN_NAME_RE = new RegExp(
`${SEP}(?:flux|sdxl|sd3|stable[-_]?diffusion|z[-_]?image|qwen[-_]?image|hidream|ideogram|lumina|hunyuanimage|krea|kolors|playground|pixart)${FAMILY_END}`,
@@ -99,8 +96,7 @@ export function detectCapabilities(opts: {
const tagSet = new Set((tags ?? []).map((t) => t.toLowerCase()));
if (pipelineTag) tagSet.add(pipelineTag.toLowerCase());
// Name part only. These two match model FAMILIES, and a family word turns up in owners too:
- // "hunyuanvideo-community/HunyuanImage-2.1" is an image model published by a video org, and
- // matching the whole id badges it as video.
+ // "hunyuanvideo-community/HunyuanImage-2.1" is an image model published by a video org.
const name = id.split("/").pop() ?? id;
const videoGen = hasAny(tagSet, VIDEO_GEN_TAGS) || VIDEO_GEN_NAME_RE.test(name);
return {
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.check.ts b/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.check.ts
index 8f226c78907..1cf8575d706 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.check.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.check.ts
@@ -1,13 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Assertions over the model catalog: keys, aliases, integrity, quant routing ladders, search matching. Plain node:assert like i18n:check. Run: npm run catalog:check
-//
-// `--network` adds an OPT-IN pass that asks the Hub whether every declared artifact is actually
-// downloadable and whether each `gated` flag matches reality. It is deliberately not part of
-// `npm run catalog:check`: it needs the network, and a Hub hiccup must never fail an unrelated PR.
-// Run it on a schedule instead (.github/workflows/model-catalog-network-check.yml), or by hand
-// with `npm run catalog:check:network`.
+// Assertions over the model catalog: keys, aliases, integrity, quant ladders, search.
+// `--network` adds an OPT-IN Hub reachability/gated-flag pass, kept out of
+// `npm run catalog:check` so a Hub hiccup cannot fail an unrelated PR.
+// Run it on a schedule (.github/workflows/model-catalog-network-check.yml) or by hand with
+// `npm run catalog:check:network`.
import assert from "node:assert/strict";
@@ -33,7 +31,6 @@ import {
stripArtifactSuffixesForDisplay,
} from "./model-catalog.ts";
-// ── canonicalKeyFor: suffix stripping, owner preserved ─────────────────────────
assert.equal(canonicalKeyFor("unsloth/Qwen-Image-2512-GGUF"), "unsloth/qwen-image-2512");
assert.equal(canonicalKeyFor("unsloth/Qwen-Image-2512-FP8"), "unsloth/qwen-image-2512");
@@ -55,7 +52,6 @@ assert.equal(canonicalKeyFor("unsloth/Qwen-Image-2512-NVFP4"), "unsloth/qwen-ima
assert.equal(canonicalKeyFor("unsloth/qwen-image-2512-gguf"), "unsloth/qwen-image-2512");
assert.equal(canonicalKeyFor("unsloth/qwen-image-2512-fp8"), "unsloth/qwen-image-2512");
-// ── stripArtifactSuffixesForDisplay: case-preserving base name for row labels ──
assert.equal(
stripArtifactSuffixesForDisplay("unsloth/ERNIE-Image-Turbo-GGUF"),
@@ -93,7 +89,6 @@ assert.notEqual(
// Stripping never empties a name that IS a suffix-looking token.
assert.equal(canonicalKeyFor("someone/fp8"), "someone/fp8");
-// ── groupForRepoId: artifacts, aliases, canonical keys, unknowns ───────────────
const qwen2512 = groupForRepoId("unsloth/Qwen-Image-2512-GGUF", IMAGE_CATALOG);
assert.ok(qwen2512);
@@ -140,7 +135,6 @@ assert.equal(
),
);
-// ── catalog integrity: unique ids, artifacts resolve to exactly one group ──────
for (const catalog of [IMAGE_CATALOG, VIDEO_CATALOG, AUDIO_CATALOG]) {
const seen = new Set<string>();
@@ -168,7 +162,6 @@ for (const catalog of [IMAGE_CATALOG, VIDEO_CATALOG, AUDIO_CATALOG]) {
}
}
-// ── loadSpecFor reproduces the old page lookup tables exactly ──────────────────
const OLD_SAFETENSORS_MODELS: Record<
string,
@@ -230,8 +223,8 @@ for (const id of [
assert.ok(videoOptionIds.has(id), `video option missing: ${id}`);
}
-// H3 publishes both denoiser partitions in its official bundle. One artifact lets the lister's
-// partition-aware labels expose both in Recommended and On Device without a community mirror.
+// H3 publishes both denoiser partitions officially. One artifact lets the lister's
+// partition-aware labels expose both without a community mirror.
const h3Group = groupForRepoId("unsloth/MiniMax-H3-GGUF", VIDEO_CATALOG);
assert.ok(h3Group);
assert.deepEqual(
@@ -245,9 +238,8 @@ assert.equal(
"MiniMax H3 (GGUF)",
);
-// ── curatedRowLabelFor ─────────────────────────────────────────────────────────
-// The row form: format (and a resolution when that is the only difference) become chips, and only
-// what actually names the variant stays in brackets.
+// Row form: format (plus resolution when that is the only difference) becomes chips, and
+// only what names the variant stays in brackets.
assert.deepEqual(curatedRowLabelFor("MiniMaxAI/MiniMax-H3", VIDEO_CATALOG), {
name: "MiniMax H3",
@@ -263,9 +255,8 @@ assert.deepEqual(curatedRowLabelFor("unsloth/Z-Image-Turbo-GGUF", IMAGE_CATALOG)
tags: [],
});
-// ── host-aware rows ────────────────────────────────────────────────────────────
-// On a host that can place a diffusion pipeline, the two H3 rows say which is which. The gap is
-// roughly 10x, and the names alone gave the user nothing to choose on.
+// On a host that can place a diffusion pipeline the two H3 rows say which is which: the
+// gap is roughly 10x and the names alone gave nothing to choose on.
assert.deepEqual(curatedRowLabelFor("MiniMaxAI/MiniMax-H3", VIDEO_CATALOG, "accelerated"), {
name: "MiniMax H3 (Fast FP8)",
tags: ["BF16"],
@@ -274,8 +265,7 @@ assert.deepEqual(
curatedRowLabelFor("unsloth/MiniMax-H3-GGUF", VIDEO_CATALOG, "accelerated"),
{ name: "MiniMax-H3-GGUF (Slow)", tags: [] },
);
-// A host that can only run the native engine has nothing to compare against, so the GGUF row
-// keeps its plain name.
+// A host that can only run the native engine has nothing to compare against, so the GGUF row keeps its plain name.
assert.deepEqual(
curatedRowLabelFor("unsloth/MiniMax-H3-GGUF", VIDEO_CATALOG, "gguf-only"),
{ name: "MiniMax-H3-GGUF", tags: [] },
@@ -295,9 +285,8 @@ assert.deepEqual(
curatedRowLabelFor("Lightricks/LTX-2", VIDEO_CATALOG),
);
-// A gguf-only host loses exactly the artifacts the backend refuses there, and keeps the rest.
-// Non-GGUF is NOT the test: the diffusion pipelines are device-neutral and run on MPS, and the
-// audio STT rows run through the whisper.cpp sidecar whatever format the catalog labels them.
+// A gguf-only host loses exactly the artifacts the backend refuses there. Non-GGUF is NOT
+// the test: diffusion runs on MPS and audio STT runs through the whisper.cpp sidecar.
for (const [label, catalog, refused] of [
["video", VIDEO_CATALOG, ["MiniMaxAI/MiniMax-H3"]],
["image", IMAGE_CATALOG, []],
@@ -311,8 +300,8 @@ for (const [label, catalog, refused] of [
`${label}: a gguf-only host lost a row it can load`,
);
}
-// Every group keeps at least one row on a gguf-only host: a Mac must never open a picker with a
-// whole model family missing (H3 keeps its GGUF sibling).
+// Every group keeps a row on a gguf-only host: a Mac must never open the picker with a
+// whole model family missing.
for (const [label, catalog] of [
["video", VIDEO_CATALOG],
["image", IMAGE_CATALOG],
@@ -382,10 +371,9 @@ for (const catalog of [IMAGE_CATALOG, VIDEO_CATALOG]) {
}
}
-// A non-GGUF artifact that states a parameter count must state its size too. The picker's VRAM
-// badge falls back to the QLoRA estimator when it cannot size a row, and that formula reads a
-// diffusion pipeline as a language model it can quantize: 5B params says 5.9 GB where the Wan 2.2
-// TI2V pipeline is 30. The curated size is what keeps that fallback off these rows.
+// A non-GGUF artifact stating a parameter count must state its size, or the VRAM badge
+// falls back to the QLoRA estimator, which reads a pipeline as a language model
+// (5B says 5.9 GB where Wan 2.2 TI2V is 30).
for (const catalog of [IMAGE_CATALOG, VIDEO_CATALOG, AUDIO_CATALOG]) {
for (const group of catalog) {
for (const artifact of group.artifacts) {
@@ -398,15 +386,15 @@ for (const catalog of [IMAGE_CATALOG, VIDEO_CATALOG, AUDIO_CATALOG]) {
}
}
-// ── curatedArtifactFitsDevice ──────────────────────────────────────────────────
-const WAN = "Wan-AI/Wan2.2-TI2V-5B-Diffusers"; // 30 GB, no offload tiers
-const H3 = "MiniMaxAI/MiniMax-H3"; // 145 GB, tiers at 74/140 and 123/80
+const WAN = "Wan-AI/Wan2.2-TI2V-5B-Diffusers"; // 30 GB, no offload tiers
+const H3 = "MiniMaxAI/MiniMax-H3"; // 145 GB, tiers at 74/140 and 123/80
const fitsCurated = (id: string, gpuGb: number, systemRamGb: number) =>
curatedArtifactFitsDevice(id, VIDEO_CATALOG, { gpuGb, systemRamGb });
-// The resident 70% rule, on the card alone. RAM is not a discrete GPU's budget: a pipeline with
-// no measured tier is placed wholly on the card, so 64 GB of RAM does not rescue a 12 GB one.
+// The resident 70% rule on the card alone. RAM is not a discrete GPU's budget: a pipeline
+// with no measured tier is placed wholly on the card.
+// 64 GB of RAM does not rescue a 12 GB card.
assert.equal(fitsCurated(WAN, 48, 0), true);
assert.equal(fitsCurated(WAN, 40, 0), false);
assert.equal(fitsCurated(WAN, 12, 64), false);
@@ -414,17 +402,17 @@ assert.equal(fitsCurated(WAN, 12, 64), false);
assert.equal(fitsCurated(WAN, 0, 64), true);
// Nothing to judge against: no verdict rather than a scary one.
assert.equal(fitsCurated(WAN, 0, 0), undefined);
-// Measured offload tiers override the 70% rule, in both directions. 123/80 is the case the
-// generic size test gets wrong: 0.7 * (123 + 80) is 142, under 145, yet the tier was measured.
+// Measured offload tiers override the 70% rule both ways. 123/80 is the case the generic
+// size test gets wrong: 0.7 * 203 is 142, under 145, yet the tier was measured.
assert.equal(fitsCurated(H3, 74, 140), true);
assert.equal(fitsCurated(H3, 123, 80), true);
assert.equal(fitsCurated(H3, 74, 100), false);
// A GGUF ladder self-fits via pickDefaultQuant, and an unknown id is not ours to judge.
assert.equal(fitsCurated("unsloth/MiniMax-H3-GGUF", 12, 64), undefined);
assert.equal(fitsCurated("someone/not-in-the-catalog", 12, 64), undefined);
-// Transcription retries a failed device load on CPU (stt_sidecar.py), so RAM is a real budget for
-// an stt row: Whisper Large runs on a card too small to hold it. A tts load rejects CPU offload
-// (inference.py raise_if_offloaded), so Orpheus is judged on the card alone.
+// Transcription retries a failed device load on CPU (stt_sidecar.py), so RAM is a real
+// budget for stt: Whisper Large runs on a card too small to hold it. A tts load rejects CPU
+// offload (inference.py raise_if_offloaded), so Orpheus is judged on the card.
assert.equal(
curatedArtifactFitsDevice("unsloth/whisper-large-v3", AUDIO_CATALOG, {
gpuGb: 4,
@@ -439,8 +427,8 @@ assert.equal(
}),
false,
);
-// The whole model goes to whichever device takes it, so the budget is the LARGER of the two and
-// never their sum: 3 GB of card and 3 GB of RAM hold a 4 GB checkpoint on neither.
+// The whole model goes to whichever device takes it, so the budget is the LARGER of the
+// two and never their sum: 3 GB card + 3 GB RAM hold a 4 GB checkpoint on neither.
assert.equal(
curatedArtifactFitsDevice("unsloth/whisper-large-v3", AUDIO_CATALOG, {
gpuGb: 3,
@@ -463,12 +451,11 @@ assert.equal(
false,
);
-// ── classifyGgufFit ────────────────────────────────────────────────────────────
const GB = 1024 ** 3;
-// Delegated to lib/gguf-fit, the formula the Hub badge uses: 0.97 of the card (or the saved VRAM
-// Budget) against weights + KV, then RAM offload at 0.5. The rule here used to be 0.7 of each
-// against the raw file size, which matched neither the Hub nor the loader.
+// Delegated to lib/gguf-fit, the Hub badge formula: 0.97 of the card (or saved VRAM budget)
+// against weights + KV, then RAM offload at 0.5. The old 0.7-of-each rule on raw file
+// size matched neither the Hub nor the loader.
assert.equal(classifyGgufFit(10 * GB, { gpuGb: 24, systemRamGb: 64 }), "fits");
// 20 GiB needs 24.0: past the 23.28 budget, still inside the 24 GiB card.
assert.equal(classifyGgufFit(20 * GB, { gpuGb: 24, systemRamGb: 64 }), "marginal");
@@ -480,8 +467,7 @@ assert.equal(classifyGgufFit(100 * GB, { gpuGb: 0, systemRamGb: 0 }), "fits");
// No GPU at all: RAM alone, at the 0.5 offload share.
assert.equal(classifyGgufFit(20 * GB, { gpuGb: 0, systemRamGb: 64 }), "ram");
assert.equal(classifyGgufFit(60 * GB, { gpuGb: 0, systemRamGb: 64 }), "oom");
-// The saved VRAM Budget moves the line. The old rule ignored the setting entirely, so lowering it
-// changed the Hub's verdict and left the picker's untouched.
+// The saved VRAM Budget moves the line. The old rule ignored the setting entirely.
assert.equal(
classifyGgufFit(16 * GB, { gpuGb: 24, systemRamGb: 0, budgetFraction: 0.97 }),
"fits",
@@ -490,9 +476,8 @@ assert.equal(
classifyGgufFit(16 * GB, { gpuGb: 24, systemRamGb: 0, budgetFraction: 0.8 }),
"marginal",
);
-// At the top of the slider the loader still keeps its 512 MiB floor (_VRAM_FLOOR_RESERVE_MIB), so
-// a 20 GiB quant needing 24.0 GiB is handed to --fit rather than admitted. `gpuGb * fraction` said
-// otherwise, and the whole point of this file is that the badge matches _select_gpus.
+// At the top of the slider the loader keeps its 512 MiB floor, so a 20 GiB quant needing
+// 24.0 GiB goes to --fit rather than being admitted. `gpuGb * fraction` said otherwise.
assert.equal(
classifyGgufFit(20 * GB, { gpuGb: 24, systemRamGb: 0, budgetFraction: 1 }),
"marginal",
@@ -502,8 +487,8 @@ assert.equal(
classifyGgufFit(19 * GB, { gpuGb: 24, systemRamGb: 0, budgetFraction: 0.97 }),
"fits",
);
-// The floor is charged once per CARD: _select_gpus calls _vram_usable_mib for every device and
-// sums, so two 24 GiB cards at 1.0 offer 47.0 GiB, not 47.5. A 40.2 GiB file needs 47.23.
+// The floor is charged once per CARD: _select_gpus sums per-device usable MiB, so two
+// 24 GiB cards at 1.0 offer 47.0 GiB, not 47.5. A 40.2 GiB file needs 47.23.
assert.equal(
classifyGgufFit(40.2 * GB, {
gpuGb: 48,
@@ -531,14 +516,14 @@ for (const gpuCount of [1, 2, 4]) {
);
}
-// ── classifyMediaGgufFit ──────────────────────────────────────────────────────
-// Images / Video place a GGUF through the diffusion backend, whose budget on a 64 GiB unified host
-// is (total - 20% reserve) * 0.85 = 43.5 GiB (diffusion_memory.py). This rule allows 44.8; the
-// llama.cpp one allows 62.1 and would promise loads the planner refuses.
-assert.equal(classifyMediaGgufFit(40 * GB, 64, 0), "fits"); // 40 <= 44.8
-assert.equal(classifyMediaGgufFit(50 * GB, 64, 0), "oom"); // past 44.8, no RAM tier
-// The same 50 GiB file reads as fitting under the llama.cpp rule, which is the regression this
-// guard exists to prevent: 50 * 1.15 + 1 = 58.5 <= 64 * 0.97.
+// Images / Video place a GGUF through the diffusion backend, whose 64 GiB unified budget is
+// (total - 20%) * 0.85 = 43.5 GiB. This rule allows 44.8; the llama.cpp one allows 62.1
+// and would promise loads the planner refuses.
+// classifyMediaGgufFit. The 43.5 GiB figure is (total - 20% reserve) * 0.85, per diffusion_memory.py.
+assert.equal(classifyMediaGgufFit(40 * GB, 64, 0), "fits"); // 40 <= 44.8
+assert.equal(classifyMediaGgufFit(50 * GB, 64, 0), "oom"); // past 44.8, no RAM tier
+// The same 50 GiB file reads as fitting under the llama.cpp rule, the regression this guard
+// prevents: 50 * 1.15 + 1 = 58.5 <= 64 * 0.97.
assert.equal(classifyGgufFit(50 * GB, { gpuGb: 64, systemRamGb: 0 }), "fits");
// A discrete card with RAM keeps the offload tier the rule always had.
assert.equal(classifyMediaGgufFit(20 * GB, 24, 64), "partial");
@@ -551,7 +536,6 @@ assert.equal(classifyMediaGgufFit(60 * GB, 0, 64), "oom");
assert.equal(ggufFitRuns("partial"), true);
assert.equal(ggufFitRuns("oom"), false);
-// ── pickDefaultQuant ───────────────────────────────────────────────────────────
const variants = [
{ quant: "Q4_K_M", filename: "m-Q4_K_M.gguf", size_bytes: 12 * GB },
@@ -592,7 +576,6 @@ assert.equal(
);
assert.equal(pickDefaultQuant([], "Q4_K_M", budget24), null);
-// ── pickDefaultArtifact ────────────────────────────────────────────────────────
const notDownloaded = () => false;
const qwenGroup = qwen2512;
@@ -608,9 +591,9 @@ assert.equal(
.format,
"bnb-4bit",
);
-// 48 GB: still bnb-4bit. The group has no fp8 artifact -- fp8 is family-denied for qwen-image
-// (it renders black), and the -FP8 repo ships prequant .pt checkpoints, not a single-file
-// safetensors, so the row that used to win here auto-routed to a download that 404s.
+// 48 GB: still bnb-4bit. fp8 is family-denied for qwen-image (renders black) and the -FP8
+// repo ships prequant .pt rather than single-file safetensors, so the old winner
+// auto-routed to a download that 404s.
assert.equal(
pickDefaultArtifact(qwenGroup, { gpuGb: 48, systemRamGb: 64, isDownloaded: notDownloaded })
.format,
@@ -648,7 +631,8 @@ assert.equal(
.repoId,
"ideogram-ai/ideogram-4-nf4-diffusers",
);
-// A gated BF16 artifact (FLUX.1-dev) is NOT auto-routed when undownloaded even on a big GPU: the download would fail without license/token access.
+// A gated BF16 artifact (FLUX.1-dev) is NOT auto-routed when undownloaded even on a big
+// GPU: the download would fail without license/token access.
const fluxDevRoute = groupForRepoId("unsloth/FLUX.1-dev", IMAGE_CATALOG);
assert.ok(fluxDevRoute);
assert.equal(
@@ -665,7 +649,8 @@ assert.equal(
}).repoId,
"black-forest-labs/FLUX.1-dev",
);
-// FLUX.1 Krea dev: gated BF16 skipped when undownloaded -> the open QuantStack GGUF, whose repo id also resolves to the group.
+// FLUX.1 Krea dev: gated BF16 skipped when undownloaded, so the open QuantStack GGUF wins;
+// its repo id also resolves to the group.
const kreaDevRoute = groupForRepoId("black-forest-labs/FLUX.1-Krea-dev", IMAGE_CATALOG);
assert.ok(kreaDevRoute);
assert.equal(
@@ -686,15 +671,16 @@ assert.equal(
"Alpha-VLLM/Lumina-Image-2.0",
);
assert.equal(loadSpecFor("Alpha-VLLM/Lumina-Image-2.0", IMAGE_CATALOG)?.kind, "pipeline");
-// HunyuanImage 2.1: the 50 GB bf16 pipeline misses a 24 GB card so a bare click routes to the QuantStack GGUF; on a large GPU bf16 wins. Both ids share one group.
+// HunyuanImage 2.1: the 50 GB bf16 pipeline misses a 24 GB card so a bare click routes to
+// the QuantStack GGUF; on a large GPU bf16 wins. Both ids share one group.
const hyimage = groupForRepoId(
"hunyuanvideo-community/HunyuanImage-2.1-Diffusers",
IMAGE_CATALOG,
);
assert.ok(hyimage);
-// bf16 at both ends now: the QuantStack GGUF that used to be the 24 GB route was unpublished
-// from the Hub, so the group has no quant ladder left. 50 GB still fits a 24 GB card's 61.6 GB
-// budget, so this asserts the group did not silently vanish along with its GGUF.
+// bf16 at both ends now: the QuantStack GGUF was unpublished, so the group has no quant
+// ladder left. 50 GB still fits a 24 GB card's 61.6 GB budget, so this asserts the
+// group did not vanish with its GGUF.
assert.equal(
pickDefaultArtifact(hyimage, { gpuGb: 24, systemRamGb: 64, isDownloaded: notDownloaded })
.repoId,
@@ -705,7 +691,8 @@ assert.equal(
.format,
"bf16",
);
-// HiDream I1: all three variants group together, a datacenter GPU auto-routes to the Full bf16 (catalog order wins among equal sizes), and 24 GB hides the group.
+// HiDream I1: all three variants group together, a datacenter GPU auto-routes to Full bf16
+// (catalog order wins among equal sizes), and 24 GB hides the group.
const hidream = groupForRepoId("HiDream-ai/HiDream-I1-Full", IMAGE_CATALOG);
assert.ok(hidream);
assert.equal(groupForRepoId("HiDream-ai/HiDream-I1-Dev", IMAGE_CATALOG), hidream);
@@ -719,8 +706,8 @@ assert.equal(
catalogGroupFitsDevice(hidream, { gpuGb: 24, systemRamGb: 32 }, notDownloaded),
false,
);
-// FLUX.1-schnell is Apache-2.0 but gated on the Hub, so a not-downloaded BF16 is skipped and the
-// open GGUF is auto-routed instead, even on a GPU that fits the pipeline.
+// FLUX.1-schnell is Apache-2.0 but gated on the Hub, so an undownloaded BF16 is skipped and
+// the open GGUF wins even on a GPU that fits the pipeline.
const fluxSchnellRoute = groupForRepoId("unsloth/FLUX.1-schnell", IMAGE_CATALOG);
assert.ok(fluxSchnellRoute);
assert.equal(
@@ -739,7 +726,8 @@ assert.equal(
.label,
"BF16 - 720p",
);
-// Same-format artifacts keep declaration order, so 720p is listed first and the fit loop returns it; a budget of 42 skips it (52 > 42) and falls back to 480p (40).
+// Same-format artifacts keep declaration order, so 720p is listed first; a budget of 42
+// skips it and falls back to 480p (40).
assert.equal(
pickDefaultArtifact(hunyuan, { gpuGb: 60, systemRamGb: 128, isDownloaded: notDownloaded })
.label,
@@ -774,18 +762,17 @@ assert.equal(
.format,
"bf16",
);
-// The upper tier, in the units the picker is actually handed: 132 GiB of VRAM is 141.7 decimal
-// GB, past the 132 GB at which the backend estimator drops its host-RAM floor to 85 GB, and
-// 85 GiB of RAM is 91.3 GB. So this host fits, and a tier table written in decimal GB would
-// wrongly send it to GGUF.
+// The upper tier in the picker's units: 132 GiB VRAM is 141.7 decimal GB, past the 132 GB
+// where the estimator drops its host-RAM floor to 85 GB, and 85 GiB RAM is 91.3 GB.
+// A decimal-GB tier table would wrongly send this host to GGUF.
assert.equal(
pickDefaultArtifact(h3, { gpuGb: 132, systemRamGb: 85, isDownloaded: notDownloaded })
.format,
"bf16",
);
-// ── official BF16 artifacts (added so groups are not unsloth-quant-only) ────────
-// Qwen-Image-2512 BF16 (54 GB) misses a 24/48 GB budget (bnb-4bit/fp8 win there, asserted above) but on an 80 GB GPU (budget 56) it fits and wins.
+// Qwen-Image-2512 BF16 (54 GB) misses a 24/48 GB budget but fits an 80 GB GPU (budget 56)
+// and wins there.
assert.equal(
pickDefaultArtifact(qwenGroup, { gpuGb: 80, systemRamGb: 128, isDownloaded: notDownloaded })
.format,
@@ -809,7 +796,8 @@ assert.equal(
.format,
"bf16",
);
-// FLUX.1-dev BF16 (32 GB) fits a 48 GB GPU but is GATED, so a bare click routes to the open GGUF unless it is already downloaded. Small GPU -> GGUF.
+// FLUX.1-dev BF16 (32 GB) fits a 48 GB GPU but is GATED, so a bare click routes to the open
+// GGUF unless already downloaded. Small GPU also goes to GGUF.
const fluxDev = groupForRepoId("black-forest-labs/FLUX.1-dev", IMAGE_CATALOG);
assert.ok(fluxDev);
assert.equal(fluxDev.canonicalId, "unsloth/FLUX.1-dev");
@@ -831,10 +819,10 @@ assert.equal(
.format,
"gguf",
);
-// LTX-2.3 video carries the official BF16 single file (no FP8: the loader refuses its scaled-fp8 one), which keeps the ~50 GB Gemma3 encoder resident, so B200-class only.
-// Looked up by the retired unsloth/LTX-2.3 id on purpose: it is no longer the canonicalId (that
-// repo does not exist), and a pasted or persisted copy must still land on this group through the
-// GGUF artifact's suffix-stripped key.
+// LTX-2.3 video carries the official BF16 single file (no FP8: the loader refuses its
+// scaled-fp8 one), keeping the ~50 GB Gemma3 encoder resident, so B200-class only.
+// Looked up by the retired unsloth/LTX-2.3 id on purpose: a pasted or persisted copy
+// must still land on this group through the GGUF artifact's suffix-stripped key.
const ltxGroup = groupForRepoId("unsloth/LTX-2.3", VIDEO_CATALOG);
assert.ok(ltxGroup);
assert.equal(ltxGroup.canonicalId, "Lightricks/LTX-2.3");
@@ -866,7 +854,6 @@ assert.equal(
assert.equal(loadSpecFor("Tongyi-MAI/Z-Image-Turbo", IMAGE_CATALOG)?.kind, "pipeline");
assert.equal(loadSpecFor("Qwen/Qwen-Image-2512", IMAGE_CATALOG)?.kind, "pipeline");
-// ── catalogGroupFitsDevice (the fit-on-device toggle for catalog rows) ─────────
const wanA14b = groupForRepoId("Wan-AI/Wan2.2-T2V-A14B-Diffusers", VIDEO_CATALOG);
const ltxBase = groupForRepoId("Lightricks/LTX-2", VIDEO_CATALOG);
@@ -875,10 +862,10 @@ const hunyuanFit = groupForRepoId(
VIDEO_CATALOG,
);
assert.ok(wanA14b && ltxBase && hunyuanFit && ltxGroup);
-const consumer = { gpuGb: 24, systemRamGb: 64 }; // budget 61.6 GB
+const consumer = { gpuGb: 24, systemRamGb: 64 }; // budget 61.6 GB
// A bare-bf16 group over budget is hidden: this is the OOM the toggle must catch.
-assert.equal(catalogGroupFitsDevice(wanA14b, consumer, notDownloaded), false); // 114 GB
-assert.equal(catalogGroupFitsDevice(ltxBase, consumer, notDownloaded), false); // 90 GB
+assert.equal(catalogGroupFitsDevice(wanA14b, consumer, notDownloaded), false); // 114 GB
+assert.equal(catalogGroupFitsDevice(ltxBase, consumer, notDownloaded), false); // 90 GB
// A sized bf16 group that fits the budget stays visible (Hunyuan 40/52 GB <= 61.6).
assert.equal(catalogGroupFitsDevice(hunyuanFit, consumer, notDownloaded), true);
// But on a tiny device even those are hidden.
@@ -886,7 +873,7 @@ assert.equal(
catalogGroupFitsDevice(hunyuanFit, { gpuGb: 8, systemRamGb: 8 }, notDownloaded),
false,
);
-// A GGUF in the group is always runnable (its quant ladder self-fits + offloads), so LTX-2.3 stays visible on a tiny card despite its 90 GB BF16 sibling.
+// A GGUF in the group is always runnable, so LTX-2.3 stays visible on a tiny card despite its 90 GB BF16 sibling.
assert.equal(
catalogGroupFitsDevice(ltxGroup, { gpuGb: 4, systemRamGb: 4 }, notDownloaded),
true,
@@ -900,7 +887,7 @@ assert.equal(
),
true,
);
-// Unknown device budget keeps everything (we cannot tell), even a 114 GB group.
+// Unknown device budget keeps everything, even a 114 GB group.
assert.equal(catalogGroupFitsDevice(wanA14b, { gpuGb: 0, systemRamGb: 0 }, notDownloaded), true);
// On a B200-class budget the large bf16 groups fit and stay visible.
assert.equal(
@@ -908,7 +895,6 @@ assert.equal(
true,
);
-// ── audio catalog: task tags, grouping, load specs ─────────────────────────────
// Every audio group carries a task tag; the other catalogs carry none.
for (const group of AUDIO_CATALOG) {
@@ -932,8 +918,8 @@ assert.equal(groupForRepoId("unsloth/whisper-large-v3-turbo", AUDIO_CATALOG)?.ta
assert.equal(groupForRepoId("unslothai/Qwen3-ASR-0.6B-GGUF", AUDIO_CATALOG)?.task, "stt");
// A chat model stays unknown to the audio catalog.
assert.equal(groupForRepoId("unsloth/Llama-3.3-70B-GGUF", AUDIO_CATALOG), null);
-// MiniMax Music3 publishes a 67 GB repository, but its official BF16 modular loader
-// fits a 24 GB CUDA card. Download bytes must not become a false OOM badge.
+// MiniMax Music3 publishes a 67 GB repository, but its official BF16 modular loader fits a
+// 24 GB CUDA card, so download bytes must not become a false OOM badge.
const minimaxMusic = groupForRepoId("MiniMaxAI/MiniMax-Music3", AUDIO_CATALOG);
assert.ok(minimaxMusic);
assert.equal(
@@ -953,13 +939,11 @@ assert.equal(
false,
);
-// ── groupMatchesQuery ──────────────────────────────────────────────────────────
assert.ok(groupMatchesQuery(qwenGroup, "qwen"));
assert.ok(groupMatchesQuery(qwenGroup, "2512"));
assert.ok(groupMatchesQuery(qwenGroup, "gguf"));
-// Still reachable by "fp8" and by the full prequant repo id, now through the group alias rather
-// than an artifact row.
+// Still reachable by "fp8" and by the full prequant repo id, now through the group alias rather than an artifact row.
assert.ok(groupMatchesQuery(qwenGroup, "fp8"));
assert.ok(groupMatchesQuery(qwenGroup, "4bit"));
assert.ok(groupMatchesQuery(qwenGroup, "q4_k_m"));
@@ -971,33 +955,19 @@ assert.ok(groupMatchesQuery(ltx23, "lightricks/ltx-2.3"));
console.log("model-catalog check: all assertions passed");
-// ── opt-in: does the Hub agree? (`--network`) ─────────────────────────────────
-//
-// Every failure this catches was reported by hand: a preconfigured link that 401s because its
-// repo is gated and the entry never said so, or 404s because the repo/file was renamed or never
-// published. Neither is visible to a pure-data check, and both surface to the user as a download
-// that dies partway through.
-//
-// Anonymous on purpose: that is what a fresh install sees, and it is exactly the request whose
-// 401 the `gated` flag exists to predict. Retries cover Hub flakiness; only a definitive verdict
-// (a repo that answers "gone", a filename the repo does not list, a `gated` flag that disagrees
-// with the API) fails the run.
+// Opt-in `--network` pass: every failure it catches was reported by hand, a link that 401s
+// on an undeclared gated repo or 404s after a rename. Anonymous on purpose, since that
+// is what a fresh install sees; only a definitive verdict fails the run.
const HF_API = "https://huggingface.co/api/models";
const HF_RESOLVE = "https://huggingface.co";
const NETWORK_ATTEMPTS = 3;
/** Per-attempt wall clock, headers and body together. */
const NETWORK_TIMEOUT_MS = 20_000;
-/**
- * Wall clock for the whole network pass, comfortably under the workflow's 10-minute
- * timeout-minutes.
- *
- * Bounding each attempt is not enough on its own: ~53 repos at NETWORK_BATCH 4 is 14 serial
- * batches, and a peer that stalls every request costs 3 x 20s plus 1.5s of backoff per batch,
- * so a fully stalled Hub takes about 14.3 minutes to fail open -- and the job is killed at 10,
- * which is the red run this retry logic exists to avoid. Past this deadline every remaining
- * request short-circuits to "no opinion", so the check still reaches its warnings and exits 0.
- */
+/** Wall clock for the whole network pass, under the workflow's 10-minute timeout. Bounding
+ * each attempt is not enough: ~53 repos at NETWORK_BATCH 4 is 14 serial batches, so a
+ * stalling peer would be killed at 10 as a red run. Past the deadline requests
+ * short-circuit to "no opinion" and the check exits 0. */
const NETWORK_DEADLINE_MS = 7 * 60 * 1000;
/** Set when the network pass starts; Infinity keeps the offline assertions unbounded. */
let networkDeadlineAt = Number.POSITIVE_INFINITY;
@@ -1011,12 +981,9 @@ interface HubRepo {
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
-/**
- * Fetch with retries. Never throws and never rejects: an answer the Hub could not give is
- * `{ response: null }`, which callers must treat as "no opinion". A rate limit, a 5xx window or a
- * DNS blip is the Hub having a moment, and turning one of those into a red nightly is exactly
- * what this whole check promises not to do.
- */
+/** Fetch with retries. Never throws and never rejects: an answer the Hub could not give is
+ * `{ response: null }`, treated as "no opinion". A rate limit or DNS blip must not
+ * turn into a red nightly. */
async function fetchWithRetry(
url: string,
init?: RequestInit,
@@ -1028,14 +995,11 @@ async function fetchWithRetry(
}
try {
// A per-attempt deadline, or a peer that accepts the connection and then stalls has no
- // retry and no fail-open: fetch simply never settles, and the scheduled job dies on its own
- // 10-minute timeout as a RED run. Which is the one outcome this check promises not to
- // produce for a network blip.
+ // retry and no fail-open: fetch never settles and the job dies as a RED run.
const response = await fetch(url, { ...init, signal: AbortSignal.timeout(NETWORK_TIMEOUT_MS) });
- // Read the body HERE, under that same deadline. Headers can arrive promptly and the body
- // still stall, and the caller parses outside this function where an abort would surface as
- // "the Hub answered 200 with unreadable JSON" -- a catalog failure, for a stalled socket.
- // It also drains the 429/5xx responses we are about to retry, rather than leaking them.
+ // Read the body HERE, under the same deadline: headers can arrive while the body stalls, and
+ // the caller parses outside this function where an abort looks like unreadable JSON.
+ // It also drains the 429/5xx responses being retried.
const body = init?.method === "HEAD" ? "" : await response.text();
if (response.status !== 429 && response.status < 500) return { response, body, why: "" };
why = `HTTP ${response.status}`;
@@ -1077,8 +1041,8 @@ async function checkCatalogAgainstTheHub(catalogs: CatalogGroup[][]): Promise<st
return;
}
if (!response.ok) {
- // 401 on the METADATA endpoint means private-or-absent (the Hub will not say which);
- // a gated-but-public repo answers 200 with gated set, so this is never just "needs a licence".
+ // 401 on the METADATA endpoint means private-or-absent (the Hub will not say which); a
+ // gated-but-public repo answers 200 with gated set, so this is never just "needs a licence".
failures.push(
`${repoId}: HTTP ${response.status} from ${HF_API}/${repoId} -- the repo is missing, renamed or private, so no user can download it`,
);
@@ -1088,7 +1052,7 @@ async function checkCatalogAgainstTheHub(catalogs: CatalogGroup[][]): Promise<st
try {
repo = JSON.parse(body) as HubRepo;
} catch (err) {
- // A 200 that is not JSON is a captive portal or a proxy, not a catalog problem. Reject the
+ // A 200 that is not JSON is a captive portal or proxy, not a catalog problem. Reject the
// run rather than throwing out of Promise.all with a raw stack.
failures.push(`${repoId}: the Hub answered 200 with unreadable JSON (${err})`);
return;
@@ -1117,7 +1081,7 @@ async function checkCatalogAgainstTheHub(catalogs: CatalogGroup[][]): Promise<st
failures.push(`${repoId}: declares '${filename}', which the repo does not contain`);
continue;
}
- if (hubGated) continue; // resolve/ 401s without a token; the sibling list is the check.
+ if (hubGated) continue; // resolve/ 401s without a token; the sibling list is the check.
// Listed is not the same as fetchable: resolve/ is the endpoint the download hits.
const head = await fetchWithRetry(`${HF_RESOLVE}/${repoId}/resolve/main/${filename}`, {
method: "HEAD",
@@ -1134,9 +1098,8 @@ async function checkCatalogAgainstTheHub(catalogs: CatalogGroup[][]): Promise<st
}
});
- // Advisory only. A canonicalId is a display/grouping key, and 15 of them are deliberately not
- // repos; but one that is BOTH `unsloth/*`-shaped and dead clears every owner guard in the app,
- // so it is worth naming without failing a scheduled job over it.
+ // Advisory only. A canonicalId is a display/grouping key and 15 are deliberately not repos;
+ // but one that is both `unsloth/*`-shaped and dead clears every owner guard in the app.
const artifactIds = new Set([...artifactsByRepo.keys()].map((id) => id.toLowerCase()));
const orphans = groups
.map((g) => g.canonicalId)
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.ts b/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.ts
index 106fa25da75..3e1a1bb4a10 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/model-catalog.ts
@@ -1,8 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// One canonical name per diffusion model, its published artifacts (GGUF quants, prequant FP8 / bnb-4bit repos, official BF16 pipelines), and a deterministic router picking the best artifact for the device.
-// Pure helpers, no React/DOM deps. See model-catalog.check.ts (`npm run catalog:check`).
+// One canonical name per diffusion model, its published artifacts (GGUF quants, prequant FP8 /
+// bnb-4bit repos, official BF16 pipelines), and a deterministic router picking the best
+// artifact for the device. Pure helpers, no React/DOM deps. See model-catalog.check.ts.
import {
type GgufFitClass,
@@ -20,7 +21,7 @@ export type ArtifactFormat = "gguf" | "fp8" | "bnb-4bit" | "bf16";
export type LoadKind = "gguf" | "single_file" | "pipeline";
export interface ModelArtifact {
- /** Exact artifact repo id (the pre-grouping id -- stays loadable/searchable). */
+ /** Exact artifact repo id (the pre-grouping id, stays loadable/searchable). */
repoId: string;
format: ArtifactFormat;
loadKind: LoadKind;
@@ -28,20 +29,20 @@ export interface ModelArtifact {
filename?: string;
/** Second-level row label ("GGUF", "FP8", "BF16 (official)", "BF16 - 720p"). */
label: string;
- /** Curated resident-size estimate for routing. Omitted = unknown: never auto-picked unless downloaded. GGUF omits it too, since its quant ladder self-fits via pickDefaultQuant. */
+ /** Curated resident-size estimate for routing. Omitted = unknown: never auto-picked unless
+ * downloaded. GGUF omits it too, since its quant ladder self-fits via pickDefaultQuant. */
approxSizeGb?: number;
- /** Measured CPU-offload fit tiers. Any tier whose GPU and available-RAM floors
- * are both met can auto-route this artifact without applying the resident 70% rule. */
+ /** Measured CPU-offload fit tiers. Any tier whose GPU and available-RAM floors are both met can
+ * auto-route this artifact without the resident 70% rule. */
offloadFitTiers?: readonly { gpuGb: number; systemRamGb: number }[];
/** Extra search tokens beyond the id/label ("4bit", "nf4", ...). */
keywords?: readonly string[];
- /** Parameter count of THIS artifact's checkpoint, for the row's size chip.
- * Only a fallback: the Hub listing's own `expand=gguf` total wins wherever it
- * reports one. Rows the listing never returns (a repo it does not index, a
- * non-unsloth owner, one this account cannot see) have no other source, and
- * ids like "MiniMax-H3-GGUF" carry no "<n>B" token to guess from. */
+ /** Parameter count of THIS artifact's checkpoint, for the row's size chip. Only a fallback: the
+ * Hub listing's `expand=gguf` total wins wherever it reports one, and rows the listing never
+ * returns have no other source (ids like "MiniMax-H3-GGUF" carry no "<n>B" token). */
totalParams?: number;
- /** Gated on the Hub (license + token). A bare group click skips it when not downloaded and falls through to an open artifact (e.g. the GGUF); an already-downloaded gated artifact is still returned. */
+ /** Gated on the Hub (license + token). A bare group click skips it when not downloaded and falls
+ * through to an open artifact; an already-downloaded gated artifact is still returned. */
gated?: boolean;
/** Fixed quant when a specialized runtime pins one exact GGUF file. */
deviceQuant?: string;
@@ -61,13 +62,12 @@ export interface CatalogGroup {
/** Cross-owner ids that resolve to this group. Suffix stripping never merges two owners on its own. */
aliases?: readonly string[];
/** What the model can do, for the row's capability glyphs. Same fallback rule as
- * `ModelArtifact.totalParams`: the Hub listing's tags win where it returns the row.
- * `detectCapabilities` reads tags then repo-name keywords, and a name like
- * "MiniMax-H3-GGUF" says nothing about the audio track the model actually emits. */
+ * `ModelArtifact.totalParams`: the Hub listing's tags win where it returns the row, since
+ * `detectCapabilities` reads tags then repo-name keywords and a name like "MiniMax-H3-GGUF"
+ * says nothing about the audio track the model emits. */
capabilities?: Partial<ModelCapabilities>;
}
-// ── artifact constructors (keep the data tables terse) ─────────────────────────
const gguf = (repoId: string, extra?: Partial<ModelArtifact>): ModelArtifact => ({
repoId,
@@ -120,7 +120,8 @@ const bf16Pipeline = (
...extra,
});
-// A bf16 single-file DiT checkpoint (e.g. Lightricks' distilled LTX-2.3): from_single_file against the family base repo for the VAE / text encoder, like the fp8 single-file checkpoints.
+// A bf16 single-file DiT checkpoint: from_single_file against the family base repo for the VAE /
+// text encoder, like the fp8 single-file checkpoints.
const bf16Single = (
repoId: string,
filename: string,
@@ -137,8 +138,8 @@ const bf16Single = (
...extra,
});
-// ── curated catalogs ────────────────────────────────────────────────────────────
-// Sizes are steady resident estimates (GB) used only for routing; missing = never auto-pick unless downloaded. GGUF entries carry none: pickDefaultQuant sizes the individual .gguf files.
+// Sizes are steady resident estimates (GB) used only for routing; missing = never auto-pick
+// unless downloaded. GGUF entries carry none: pickDefaultQuant sizes the .gguf files.
export const IMAGE_CATALOG: CatalogGroup[] = [
{
@@ -164,16 +165,14 @@ export const IMAGE_CATALOG: CatalogGroup[] = [
displayName: "Qwen-Image 2512",
description: "Text-to-image",
scope: "image",
- // The prequant repo is real and public, and the backend reaches its int8 half through
- // prequant_repos. It has no artifact row here (see below), so alias it to keep a pasted
- // repo id finding this group.
+ // The prequant repo is real and public and the backend reaches its int8 half through
+ // prequant_repos. It has no artifact row here, so alias it to keep a pasted id finding it.
aliases: ["unsloth/Qwen-Image-2512-FP8"],
artifacts: [
bf16Pipeline("Qwen/Qwen-Image-2512", 54, { totalParams: 20430401088 }),
- // No FP8 row: unsloth/Qwen-Image-2512-FP8 holds torch prequant .pt checkpoints, not a
- // single-file .safetensors, and fp8 is denied for this family anyway
- // (_FAMILY_SCHEME_DENY: qwen-image renders every frame black under fp8). The repo's int8
- // half is reached through the backend prequant path, not from here.
+ // No FP8 row: unsloth/Qwen-Image-2512-FP8 holds torch prequant .pt checkpoints, not a single-file
+ // .safetensors, and fp8 is denied for this family anyway (_FAMILY_SCHEME_DENY: qwen-image
+ // renders every frame black under fp8).
bnb4bit("unsloth/Qwen-Image-2512-unsloth-bnb-4bit", 14, { totalParams: 10850871408 }),
gguf("unsloth/Qwen-Image-2512-GGUF"),
],
@@ -194,8 +193,8 @@ export const IMAGE_CATALOG: CatalogGroup[] = [
description: "Text-to-image",
scope: "image",
artifacts: [
- // Apache-2.0 but still gated on the Hub (gated: "auto", a contact-info form), so an
- // anonymous download 401s exactly like dev. The licence and the gate are independent.
+ // Apache-2.0 but still gated on the Hub (gated: "auto", a contact-info form), so an anonymous
+ // download 401s exactly like dev. The licence and the gate are independent.
bf16Pipeline("black-forest-labs/FLUX.1-schnell", 32, { gated: true, totalParams: 11891178560 }),
gguf("unsloth/FLUX.1-schnell-GGUF"),
],
@@ -212,7 +211,8 @@ export const IMAGE_CATALOG: CatalogGroup[] = [
],
},
{
- // Krea guidance-distilled FLUX.1-dev finetune: same arch/layout as dev, so it runs under the flux.1 family. The base repo is gated like dev; QuantStack publishes the open GGUF quants.
+ // Krea guidance-distilled FLUX.1-dev finetune: same arch/layout as dev, so it runs under the
+ // flux.1 family. The base repo is gated like dev; QuantStack publishes the open GGUF quants.
canonicalId: "black-forest-labs/FLUX.1-Krea-dev",
displayName: "FLUX.1 Krea dev",
description: "Text-to-image",
@@ -267,7 +267,8 @@ export const IMAGE_CATALOG: CatalogGroup[] = [
artifacts: [bf16Pipeline("krea/Krea-2-Turbo", 18, { gated: true, totalParams: 12820073036 })],
},
{
- // 2.6B DiT + Gemma2-2B encoder, ~11 GB bf16-resident (ships fp32, cast on load). Apache-2.0, ungated. No upstream GGUF quants, so the official pipeline is the only artifact.
+ // 2.6B DiT + Gemma2-2B encoder, ~11 GB bf16-resident (ships fp32, cast on load). Apache-2.0,
+ // ungated. No upstream GGUF quants, so the official pipeline is the only artifact.
canonicalId: "Alpha-VLLM/Lumina-Image-2.0",
displayName: "Lumina Image 2.0",
description: "Text-to-image",
@@ -275,15 +276,14 @@ export const IMAGE_CATALOG: CatalogGroup[] = [
artifacts: [bf16Pipeline("Alpha-VLLM/Lumina-Image-2.0", 11, { totalParams: 2609769152 })],
},
{
- // 17B dual-stream 2K-native DiT with a Qwen2.5-VL encoder; the mirror guider components load natively on diffusers 0.39.
- // bf16 only: this used to carry gguf("QuantStack/HunyuanImage-2.1-GGUF") as the consumer route, and that repo was
- // unpublished (404 to an authed request, 401 anonymous). An entry the Hub cannot serve is worse than no entry, because
- // it renders as a one-click download that fails partway through, which is the exact case model-catalog-network-check
- // exists to find. QuantStack itself is alive and still ships its other GGUF repos, so this is one model withdrawn
- // rather than a publisher going away. No vetted replacement: unsloth has an FP8 mirror but no GGUF, and the
- // third-party HunyuanImage GGUF repos on the Hub are a different lineage (calcuis ships "lite" variants), so picking
- // one is a decision about which weights users download and not a CI fix.
- // At 24 GB the bf16 still fits the 61.6 GB budget, so the group stays visible; it is the quant ladder that is gone.
+ // 17B dual-stream 2K-native DiT with a Qwen2.5-VL encoder. bf16 only: the QuantStack GGUF
+ // consumer route was unpublished, and an entry the Hub cannot serve renders as a one-click
+ // download that fails partway through. No vetted replacement: unsloth has an FP8 mirror but
+ // no GGUF, and the third-party GGUF repos are a different lineage. At 24 GB the bf16 still
+ // fits the 61.6 GB budget, so the group stays visible; the quant ladder is what is gone.
+ // The mirror guider components load natively on diffusers 0.39. The retired GGUF repo 404s to an
+ // authed request and 401s anonymously; QuantStack itself is alive and still ships its other GGUF
+ // repos, so this is one model withdrawn rather than a publisher going away.
canonicalId: "hunyuanvideo-community/HunyuanImage-2.1-Diffusers",
displayName: "HunyuanImage 2.1",
description: "Text-to-image",
@@ -293,8 +293,9 @@ export const IMAGE_CATALOG: CatalogGroup[] = [
],
},
{
- // 17B MoE DiT + four text encoders. The MIT repos ship no Llama text_encoder_4, so the backend assembles it from the open unsloth mirror
- // at load time (+16 GB): ~63 GB bf16-resident, a datacenter pick. Full is the undistilled base, Dev and Fast its guidance-free distillations.
+ // 17B MoE DiT + four text encoders. The MIT repos ship no Llama text_encoder_4, so the backend
+ // assembles it from the open unsloth mirror at load time (+16 GB): ~63 GB bf16-resident, a
+ // datacenter pick. Full is the undistilled base, Dev and Fast its distillations.
canonicalId: "HiDream-ai/HiDream-I1-Full",
displayName: "HiDream I1",
description: "Text-to-image",
@@ -314,7 +315,8 @@ export const IMAGE_CATALOG: CatalogGroup[] = [
],
},
{
- // No bf16 repo exists for Ideogram 4: -fp8 stores its two DiTs as raw float8 (~46 GB after the bf16 cast); -nf4-diffusers is the bnb-4bit export (~11 GB).
+ // No bf16 repo exists for Ideogram 4: -fp8 stores its two DiTs as raw float8 (~46 GB after the
+ // bf16 cast); -nf4-diffusers is the bnb-4bit export (~11 GB).
canonicalId: "ideogram-ai/ideogram-4",
displayName: "Ideogram 4",
description: "Text-to-image",
@@ -357,24 +359,20 @@ export const VIDEO_CATALOG: CatalogGroup[] = [
capabilities: { audio: true },
artifacts: [
bf16Pipeline("MiniMaxAI/MiniMax-H3", 145, {
- // Cluster measurements at the default 1344x768, 124-frame preset. The lower-GPU tier
- // holds one 66 GB component at a time and keeps the full model in RAM; the higher tier
- // retains more weights on-device.
- //
- // THESE ARE GiB, and the backend estimators they mirror are decimal GB, so the two sets
- // of numbers must never be copied across. The hardware API divides MiB and bytes by
- // 1024-based units (nvidia.py memory_total_gb, main.py available_gb) while the generation
- // guard in video.py divides runtime bytes by 1_000_000_000. Converted, these tiers are
- // 79.5 / 150.3 GB and 132.1 / 85.9 GB, which is exactly the estimators' 78.74 / 150 and
- // 132 / 85 with a small margin. Raising them to the decimal figures applies the
- // conversion twice and sends capable hosts to GGUF.
+ // Cluster measurements at the default 1344x768, 124-frame preset: the lower-GPU tier holds one
+ // 66 GB component at a time and keeps the full model in RAM. THESE ARE GiB and the backend
+ // estimators they mirror are decimal GB, so the two sets must never be copied across:
+ // nvidia.py memory_total_gb and main.py available_gb divide by 1024, while video.py divides
+ // runtime bytes by 1_000_000_000. Converted, these are 79.5 / 150.3 and 132.1 / 85.9 GB,
+ // matching the estimators' 78.74 / 150 and 132 / 85. Copying the decimal figures across
+ // applies the conversion twice and sends capable hosts to GGUF.
offloadFitTiers: [
{ gpuGb: 74, systemRamGb: 140 },
{ gpuGb: 123, systemRamGb: 80 },
],
}),
- // One official bundle for both denoiser partitions. The GGUF lister labels every variant
- // Text & frames or References plus its build, so both stay explicit under one repo id.
+ // One official bundle for both denoiser partitions. The GGUF lister labels every variant, so both
+ // stay explicit under one repo id.
gguf("unsloth/MiniMax-H3-GGUF", {
label: "GGUF",
keywords: [
@@ -390,14 +388,13 @@ export const VIDEO_CATALOG: CatalogGroup[] = [
],
},
{
- // The distilled 2.3 release: Lightricks' own bf16/fp8 single-file DiT checkpoints (loaded against the already-trusted LTX-2
- // base for the VAE / Gemma3 encoder) plus the GGUF quants. The single-file ones keep the ~50 GB encoder in bf16, so consumer GPUs route to GGUF.
+ // The distilled 2.3 release: Lightricks' own bf16/fp8 single-file DiT checkpoints (loaded against
+ // the already-trusted LTX-2 base for the VAE / Gemma3 encoder) plus the GGUF quants. The
+ // single-file ones keep the ~50 GB encoder in bf16, so consumer GPUs route to GGUF.
// Keyed on the artifact that exists: unsloth/LTX-2.3 was never published (404), and an
// `unsloth/*` id that is not an artifact clears both the picker's owner guard and the
// backend's, so a pick that reached the fall-through was loaded as a pipeline and only died
// at the Hub. Lightricks/LTX-2.3 IS an artifact below, so that fall-through cannot fire.
- // unsloth/LTX-2.3 still resolves to this group: the GGUF artifact claims the same
- // suffix-stripped key.
canonicalId: "Lightricks/LTX-2.3",
displayName: "LTX 2.3 distilled",
description: "Text-to-video with audio",
@@ -409,8 +406,9 @@ export const VIDEO_CATALOG: CatalogGroup[] = [
"ltx-2.3-22b-distilled.safetensors",
90,
),
- // No FP8 artifact: the LTX-2.3 loader refuses the official scaled-FP8 single file (it carries .weight_scale/.input_scale), so a click would start a ~76 GB download that always fails.
- // 21.0B is what the Hub reports for this repo; carrying it keeps the row identical when the listing is unavailable (offline, rate-limited).
+ // No FP8 artifact: the LTX-2.3 loader refuses the official scaled-FP8 single file (it carries
+ // .weight_scale/.input_scale), so a click would start a ~76 GB download that always fails.
+ // 21.0B is what the Hub reports, and carrying it keeps the row identical when offline.
gguf("unsloth/LTX-2.3-GGUF", { totalParams: 21_005_004_544 }),
],
},
@@ -442,8 +440,9 @@ export const VIDEO_CATALOG: CatalogGroup[] = [
description: "Text-to-video",
scope: "video",
artifacts: [
- // Highest-quality first: pickDefaultArtifact sorts only by FORMAT, so these two bf16 artifacts keep catalog order and the fit
- // loop returns the first that fits. 720p (52 GB) precedes 480p (40 GB), so an 80 GB card (0.7*budget=56) picks 720p.
+ // Highest-quality first: pickDefaultArtifact sorts only by FORMAT, so these two bf16 artifacts
+ // keep catalog order and the fit loop returns the first that fits. 720p (52 GB) precedes
+ // 480p (40 GB), so an 80 GB card picks 720p.
bf16Pipeline("hunyuanvideo-community/HunyuanVideo-1.5-Diffusers-720p_t2v", 52, {
label: "BF16 - 720p",
keywords: ["bf16", "720p"],
@@ -460,8 +459,8 @@ export const VIDEO_CATALOG: CatalogGroup[] = [
// The Audio page's curated list. tts groups load into the main slot via /api/inference/load
// (Orpheus is the only family the llama.cpp TTS path also serves as GGUF); native TTS
-// families use their official Transformers/Diffusers interfaces. stt groups map to
-// the dictation sidecar models in stt-model-catalog.ts, so their sizes are informational only.
+// families use their official Transformers/Diffusers interfaces. stt groups map to the
+// dictation sidecar models in stt-model-catalog.ts, so their sizes are informational only.
export const AUDIO_CATALOG: CatalogGroup[] = [
{
canonicalId: "unsloth/orpheus-3b-0.1-ft",
@@ -558,17 +557,16 @@ export const AUDIO_CATALOG: CatalogGroup[] = [
artifacts: [
bf16Pipeline("MiniMaxAI/MiniMax-Music3", 67, {
label: "Diffusers",
- // 67 GB is the repository/download footprint, not resident VRAM. The
- // publisher's ModularPipeline path loads in BF16 on a 24 GB CUDA GPU.
+ // 67 GB is the repository/download footprint, not resident VRAM: the publisher's ModularPipeline
+ // path loads in BF16 on a 24 GB CUDA GPU.
offloadFitTiers: [{ gpuGb: 24, systemRamGb: 0 }],
}),
],
},
- // Llasa is deliberately absent. It speaks XCodec2 (65,536 <|s_N|> tokens), which is
- // neither in _AUDIO_TOKEN_PATTERNS nor in AudioCodecManager, so a curated row here
- // loaded and then failed at generation with "not a supported TTS model". Unsloth can
- // still TRAIN Llasa (unsloth_Llasa-3B.yaml); this catalog only feeds the Generate
- // picker. Re-add both rows together with an xcodec2 decoder.
+ // Llasa is deliberately absent: it speaks XCodec2 (65,536 <|s_N|> tokens), which is in neither
+ // _AUDIO_TOKEN_PATTERNS nor AudioCodecManager, so a curated row loaded and then failed at
+ // generation. Unsloth can still TRAIN Llasa (unsloth_Llasa-3B.yaml); this catalog only feeds the
+ // Generate picker. Re-add both rows together with an xcodec2 decoder.
{
canonicalId: "unslothai/Qwen3-ASR-0.6B-GGUF",
displayName: "Qwen3-ASR 0.6B",
@@ -617,8 +615,8 @@ export const AUDIO_CATALOG: CatalogGroup[] = [
task: "stt",
artifacts: [bf16Pipeline("unsloth/whisper-small", 1, { label: "Safetensors" })],
},
- // Both sidecars carry tiny/base (GGML_STT_REPOS, STT_MODEL_REPOS) and Voice
- // settings lists them; only this picker was missing them.
+ // Both sidecars carry tiny/base (GGML_STT_REPOS, STT_MODEL_REPOS) and Voice settings lists them;
+ // only this picker was missing them.
{
canonicalId: "unsloth/whisper-base",
displayName: "Whisper Base",
@@ -637,9 +635,9 @@ export const AUDIO_CATALOG: CatalogGroup[] = [
},
];
-// ── canonical keys and lookups ──────────────────────────────────────────────────
-// Artifact/format suffixes stripped (repeatedly, longest-first) off the NAME part of a repo id. Owner is preserved: cross-owner merges happen only via the alias tables above.
+// Artifact/format suffixes stripped (repeatedly, longest-first) off the NAME part of a repo id.
+// Owner is preserved: cross-owner merges happen only via the alias tables above.
const ARTIFACT_SUFFIXES = [
"-unsloth-bnb-4bit",
"-nf4-diffusers",
@@ -657,7 +655,8 @@ const ARTIFACT_SUFFIXES = [
"-bf16",
] as const;
-/** Owner-preserving generic key: lowercase, artifact suffixes stripped off the name. "unsloth/Qwen-Image-2512-GGUF" -> "unsloth/qwen-image-2512". */
+/** Owner-preserving generic key: lowercase, artifact suffixes stripped off the name.
+ * "unsloth/Qwen-Image-2512-GGUF" to "unsloth/qwen-image-2512". */
export function canonicalKeyFor(repoId: string): string {
const lowered = repoId.trim().toLowerCase();
const slash = lowered.indexOf("/");
@@ -676,7 +675,8 @@ export function canonicalKeyFor(repoId: string): string {
return owner + name;
}
-/** Case-preserving display name with artifact suffixes stripped, so uncurated rows read as their base model ("ERNIE-Image-Turbo-GGUF" -> "ERNIE-Image-Turbo"). The format badge carries the artifact kind; the load id is untouched. */
+/** Case-preserving display name with artifact suffixes stripped, so uncurated rows read as their
+ * base model. The format badge carries the artifact kind; the load id is untouched. */
export function stripArtifactSuffixesForDisplay(repoId: string): string {
const trimmed = repoId.trim();
const slash = trimmed.indexOf("/");
@@ -706,7 +706,8 @@ interface CatalogIndex {
artifactById: Map<string, ModelArtifact>;
}
-// Rebuilt only on a new catalog array identity; the curated arrays are module constants, so in practice once per catalog (images, video, audio).
+// Rebuilt only on a new catalog array identity; the curated arrays are module constants, so in
+// practice once per catalog.
const indexCache = new WeakMap<CatalogGroup[], CatalogIndex>();
function indexFor(catalog: CatalogGroup[]): CatalogIndex {
@@ -758,7 +759,8 @@ export function artifactForRepoId(
const BYTES_PER_GB = 1024 ** 3;
-/** Curated size (bytes) of an exact artifact id; undefined when unsized (every GGUF entry: its quant ladder self-fits). */
+/** Curated size (bytes) of an exact artifact id; undefined when unsized (every GGUF entry: its
+ * quant ladder self-fits). */
export function curatedSizeBytesFor(
repoId: string,
catalog: CatalogGroup[],
@@ -767,9 +769,8 @@ export function curatedSizeBytesFor(
return gb && gb > 0 ? gb * BYTES_PER_GB : undefined;
}
-/** Curated parameter count for an exact artifact id, or undefined when the catalog
- * carries none. A FALLBACK for rows the Hub listing does not return: callers must
- * prefer the listing's own total wherever it reports one. */
+/** Curated parameter count for an exact artifact id, or undefined. A FALLBACK for rows the Hub
+ * listing does not return: callers must prefer the listing's own total. */
export function curatedTotalParamsFor(
repoId: string,
catalog: CatalogGroup[],
@@ -778,10 +779,9 @@ export function curatedTotalParamsFor(
return params && params > 0 ? params : undefined;
}
-/** Curated capabilities for any id belonging to a group that declares them, or
- * undefined. Same fallback rule as `curatedTotalParamsFor`: the listing's tags win.
- * Group-level, not artifact-level: every artifact of a model can do what the model
- * can do. */
+/** Curated capabilities for any id belonging to a group that declares them. Same fallback rule as
+ * `curatedTotalParamsFor`: the listing's tags win. Group-level, since every artifact of a
+ * model can do what the model can do. */
export function curatedCapabilitiesFor(
repoId: string,
catalog: CatalogGroup[],
@@ -793,16 +793,16 @@ export function curatedCapabilitiesFor(
vision: declared?.vision ?? false,
reasoning: declared?.reasoning ?? false,
audio: declared?.audio ?? false,
- // From the group's scope rather than a declaration: every catalog entry is a generator of one
- // or the other, and the scope already says which. Beats the name heuristic, which has to guess
+ // From the group's scope rather than a declaration: every catalog entry is a generator of one or
+ // the other, and the scope already says which. Beats the name heuristic, which has to guess
// a family from a repo id.
imageGen: declared?.imageGen ?? group.scope === "image",
videoGen: declared?.videoGen ?? group.scope === "video",
};
}
-/** Human-facing name of an exact curated artifact, including the artifact label
- * when its model has more than one selectable representation. */
+/** Human-facing name of an exact curated artifact, including the artifact label when its model has
+ * more than one selectable representation. */
export function curatedDisplayNameFor(
repoId: string,
catalog: CatalogGroup[],
@@ -810,9 +810,9 @@ export function curatedDisplayNameFor(
): string | null {
const hit = artifactForRepoId(repoId, catalog);
if (!hit) return null;
- // A row that earns a speed qualifier reads the same closed as open: this helper names the
- // trigger and curatedRowLabelFor names the row, so a divergence would rename the model as the
- // popover opens.
+ // A row that earns a speed qualifier must read the same closed as open: this helper names the
+ // trigger and curatedRowLabelFor names the row, so a divergence would rename the model as
+ // the popover opens.
if (h3PerfSuffix(repoId, host)) {
return curatedRowLabelFor(repoId, catalog, host)?.name ?? hit.group.displayName;
}
@@ -821,20 +821,17 @@ export function curatedDisplayNameFor(
: hit.group.displayName;
}
-// Artifact labels are written as "FORMAT" or "FORMAT - QUALIFIER": "BF16 (official)", "GGUF",
-// "BF16 - 720p", "GGUF - Text and frames". The format head and a resolution qualifier are chips;
-// anything else stays in the name, since it is the only thing telling two rows of one group apart.
+// Artifact labels are written as "FORMAT" or "FORMAT - QUALIFIER". The format head and a
+// resolution qualifier are chips; anything else stays in the name, since it is the only thing
+// telling two rows of one group apart.
const LABEL_PART_SEPARATOR = " - ";
const OFFICIAL_SUFFIX_RE = /\s*\(official\)$/i;
const GGUF_SUFFIX_RE = /-gguf$/i;
const RESOLUTION_RE = /^\d{3,4}p$/i;
-/**
- * A curated row as name plus chips.
- *
- * The name used to carry the artifact inside brackets ("MiniMax H3 (BF16 (official))"), which
- * pushed the part a user scans for behind the part they do not. Null for ids outside the catalog.
- */
+/** A curated row as name plus chips. The name used to carry the artifact inside brackets ("MiniMax
+ * H3 (BF16 (official))"), which pushed the part a user scans for behind the part they do
+ * not. Null for ids outside the catalog. */
export function curatedRowLabelFor(
repoId: string,
catalog: CatalogGroup[],
@@ -846,13 +843,12 @@ export function curatedRowLabelFor(
// between rather than advertising a speed they cannot have.
const perf = h3PerfSuffix(repoId, host);
const qualify = (name: string) => (perf ? `${name} (${perf})` : name);
- // GGUF reads like a text model's row: the repo name already ends in -GGUF, so show the repo
- // name and let it say so. A chip would only repeat the suffix.
+ // GGUF reads like a text model's row: the repo name already ends in -GGUF, so a chip would only repeat the suffix.
if (hit.artifact.format === "gguf") {
const leaf = hit.artifact.repoId.split("/").pop() ?? hit.artifact.repoId;
return { name: qualify(GGUF_SUFFIX_RE.test(leaf) ? leaf : `${leaf}-GGUF`), tags: [] };
}
- // A group with one artifact has nothing to distinguish, so it stays bare, exactly as before.
+ // A group with one artifact has nothing to distinguish, so it stays bare.
if (hit.group.artifacts.length <= 1) return { name: qualify(hit.group.displayName), tags: [] };
const [format, ...rest] = hit.artifact.label.split(LABEL_PART_SEPARATOR);
const tags = [format.replace(OFFICIAL_SUFFIX_RE, "").trim()].filter(Boolean);
@@ -876,9 +872,9 @@ export function catalogToModelOptions(
const options: ModelOption[] = [];
for (const group of catalog) {
for (const artifact of group.artifacts) {
- // A host that can only run the native engine is not offered the pipeline rows it would be
- // refused at load. This is the one place the `models` prop is built, so filtering here
- // covers the trigger name and the picker's seed ids together.
+ // A host that can only run the native engine is not offered the pipeline rows it would be refused
+ // at load. This is the one place the `models` prop is built, so filtering here covers the
+ // trigger name and the picker's seed ids together.
if (!curatedArtifactIsOfferable(artifact.repoId, host)) continue;
options.push({
id: artifact.repoId,
@@ -892,7 +888,8 @@ export function catalogToModelOptions(
return options;
}
-/** How to load a curated artifact. Null for unknown ids (GGUF picks carry their own variant metadata; local paths and hub GGUFs resolve elsewhere). */
+/** How to load a curated artifact. Null for unknown ids (GGUF picks carry their own variant
+ * metadata; local paths and hub GGUFs resolve elsewhere). */
export function loadSpecFor(
repoId: string,
catalog: CatalogGroup[],
@@ -902,7 +899,7 @@ export function loadSpecFor(
return { kind: hit.artifact.loadKind, filename: hit.artifact.filename };
}
-// Quant-class tokens that should match every GGUF artifact ("q4" finds the group whose GGUF repo publishes Q4_K_M, etc.).
+// Quant-class tokens that should match every GGUF artifact ("q4" finds the group whose GGUF repo publishes Q4_K_M).
const GGUF_QUANT_TOKENS = [
"q2",
"q3",
@@ -918,7 +915,8 @@ const GGUF_QUANT_TOKENS = [
"f16",
] as const;
-/** Whether a (lowercased, trimmed) query matches the group: canonical id, display name, artifact id, label/keyword, or quant-class token. */
+/** Whether a (lowercased, trimmed) query matches the group: canonical id, display name, artifact
+ * id, label/keyword, or quant-class token. */
export function groupMatchesQuery(group: CatalogGroup, query: string): boolean {
const q = query.trim().toLowerCase();
if (!q) return true;
@@ -941,7 +939,6 @@ export function groupMatchesQuery(group: CatalogGroup, query: string): boolean {
return false;
}
-// ── device fit + routing ─────────────────────────────────────────────────────────
export interface DeviceBudget {
/** Total GPU memory in GB (0/undefined = unknown or none). */
@@ -954,33 +951,25 @@ export interface DeviceBudget {
gpuCount?: number;
}
-/** GGUF fit, delegated to the one formula the Hub badge already uses.
- *
- * This used to carry its own rule (0.7 * GPU + 0.7 * RAM, raw file size) whose comment claimed to
- * match `_select_gpus`. It did not: the loader admits against `_active_vram_fraction()`, the saved
- * VRAM Budget or 0.97, over weights PLUS estimated KV cache. 0.7 appeared nowhere in the backend,
- * so chat hid quants the loader would have taken and ignored the budget setting entirely. */
+/** GGUF fit, delegated to the one formula the Hub badge already uses. This used to carry its own
+ * rule (0.7 * GPU + 0.7 * RAM, raw file size) whose comment claimed to match `_select_gpus`;
+ * it did not, so chat hid quants the loader would have taken and ignored the VRAM Budget. */
export function classifyGgufFit(
sizeBytes: number,
budget: DeviceBudget,
): GgufFitClass {
// Nothing measured, so a verdict would be invented. Callers that know the budget really is zero
- // (a probed Vulkan device) decide that for themselves.
+ // decide that for themselves.
if ((budget.gpuGb || 0) <= 0 && (budget.systemRamGb || 0) <= 0) return "fits";
if (sizeBytes <= 0) return "fits";
return classifyGgufFitForDevice(sizeBytes, budget);
}
-/** Fit rule for a GGUF the IMAGES / VIDEO / AUDIO pickers offer, which is the one case the shared
- * llama.cpp formula must not judge: those loads go through the diffusion backend, whose budget is
- * free memory minus a reserve at a 0.85 margin (`diffusion_memory.py`, `_reserve_mib` is 20% on
- * unified memory), and which explicitly cannot offload there. On a 64 GiB Mac that planner allows
- * about 43.5 GiB where llama.cpp allows 62.1. This rule, the picker's own from before the
- * classifiers were merged, allows 44.8, so it is the closer of the two by 18 GiB.
- *
- * It is not the diffusion planner either. It is here so unifying chat's verdict with the Hub's
- * does not quietly promise media loads that cannot be placed; a real diffusion predicate is its
- * own change. */
+/** Fit rule for a GGUF the IMAGES / VIDEO / AUDIO pickers offer, the one case the shared llama.cpp
+ * formula must not judge: those loads go through the diffusion backend, whose budget is free
+ * memory minus a reserve at a 0.85 margin (`diffusion_memory.py`) and which cannot offload.
+ * On a 64 GiB Mac that planner allows about 43.5 GiB where llama.cpp allows 62.1; this rule
+ * allows 44.8. It is not the diffusion planner either, only the closer of the two. */
export function classifyMediaGgufFit(
sizeBytes: number,
gpuGb: number,
@@ -992,7 +981,7 @@ export function classifyMediaGgufFit(
if (gb <= 0 || gb <= gpuBudgetGb) return "fits";
if (gpuBudgetGb <= 0) return gb <= totalBudgetGb ? "fits" : "oom";
// "partial", where this rule used to say "tight": the state it describes is a spill out of the
- // card, which is what partial means. Tight now means a full GPU load with no room to spare.
+ // card. Tight now means a full GPU load with no room to spare.
return gb <= totalBudgetGb ? "partial" : "oom";
}
@@ -1009,7 +998,8 @@ export interface QuantVariant {
downloaded?: boolean;
}
-/** The quant a bare group/repo click loads: largest downloaded non-OOM quant, else the repo default when non-OOM, else the largest fitting, else the smallest overall. */
+/** The quant a bare group/repo click loads: largest downloaded non-OOM quant, else the repo default
+ * when non-OOM, else the largest fitting, else the smallest overall. */
export function pickDefaultQuant(
variants: QuantVariant[],
defaultVariant: string | null,
@@ -1061,7 +1051,8 @@ function fitsArtifactBudget(artifact: ModelArtifact, budget: DeviceBudget): bool
return artifact.approxSizeGb <= budget.gpuGb * 0.7;
}
-/** The artifact a bare group click loads. Sized artifacts normally use the 0.7 * GPU budget; measured offload tiers can override that fit check. */
+/** The artifact a bare group click loads. Sized artifacts normally use the 0.7 * GPU budget;
+ * measured offload tiers can override that fit check. */
export function pickDefaultArtifact(
group: CatalogGroup,
input: RoutingInput,
@@ -1086,7 +1077,8 @@ export function pickDefaultArtifact(
return ggufArtifact ?? artifacts[0];
}
for (const artifact of artifacts) {
- // Skip a gated, NOT-downloaded artifact: auto-routing there fails the download without license/token access, so fall through to an open one. The downloaded branch above still returns gated artifacts.
+ // Skip a gated, NOT-downloaded artifact: auto-routing there fails the download without
+ // license/token access. The downloaded branch above still returns gated artifacts.
if (artifact.format !== "gguf" && !artifact.gated && fitsArtifactBudget(artifact, input)) {
return artifact;
}
@@ -1097,19 +1089,12 @@ export function pickDefaultArtifact(
)[0];
}
-/**
- * Whether ONE curated artifact loads on this device, by the rule `pickDefaultArtifact` routes
- * with, since a row click loads that exact artifact.
- *
- * System RAM is not part of a discrete-GPU budget: a pipeline goes wholly on the card unless the
- * catalog states a measured offload tier, which is what `offloadFitTiers` is for, or unless the
- * loader falls back to CPU, which only transcription does. A unified-memory host reports RAM and
- * no GPU, and there the RAM is the card.
- *
- * Undefined where nothing can be judged, so the caller shows no verdict rather than a wrong one:
- * an unknown budget, an id the catalog does not carry, a GGUF quant ladder (it self-fits via
- * `pickDefaultQuant`), or an artifact with no stated size.
- */
+/** Whether ONE curated artifact loads on this device, by the rule `pickDefaultArtifact` routes
+ * with, since a row click loads that exact artifact. System RAM is not part of a
+ * discrete-GPU budget: a pipeline goes wholly on the card unless the catalog states a
+ * measured offload tier or the loader falls back to CPU, which only transcription does. A
+ * unified-memory host reports RAM and no GPU, and there the RAM is the card. Undefined where
+ * nothing can be judged, so the caller shows no verdict rather than a wrong one. */
export function curatedArtifactFitsDevice(
repoId: string,
catalog: CatalogGroup[],
@@ -1122,9 +1107,8 @@ export function curatedArtifactFitsDevice(
if (artifact.offloadFitTiers?.length) return fitsArtifactBudget(artifact, budget);
if (artifact.approxSizeGb === undefined) return undefined;
// Transcription retries a failed device load on CPU (stt_sidecar.py), so RAM is a real budget
- // there -- but the WHOLE model goes to whichever device it lands on, never split across both,
- // so it is the larger of the two and not their sum. An image, video or TTS load rejects CPU
- // offload, so those get the card alone.
+ // there, but the WHOLE model goes to whichever device it lands on, so it is the larger of
+ // the two and not their sum. An image, video or TTS load rejects CPU offload.
const deviceGb =
group.task === "stt"
? Math.max(budget.gpuGb, budget.systemRamGb)
@@ -1134,7 +1118,8 @@ export function curatedArtifactFitsDevice(
return artifact.approxSizeGb <= deviceGb * 0.7;
}
-/** Whether the "fit on device" toggle keeps a group, including measured offload tiers when an artifact provides them. */
+/** Whether the "fit on device" toggle keeps a group, including measured offload tiers when an
+ * artifact provides them. */
export function catalogGroupFitsDevice(
group: CatalogGroup,
budget: DeviceBudget,
@@ -1146,7 +1131,8 @@ export function catalogGroupFitsDevice(
if (budgetGb <= 0) return true;
return group.artifacts.some((a) => {
if (isDownloaded(a.repoId)) return true;
- // A GGUF quant ladder self-fits (llama-server offloads), so it is always a runnable fallback, matching pickDefaultArtifact.
+ // A GGUF quant ladder self-fits (llama-server offloads), so it is always a runnable fallback,
+ // matching pickDefaultArtifact.
if (a.format === "gguf") return true;
if (a.offloadFitTiers?.length) {
return a.offloadFitTiers.some(
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/model-load-settings-action.tsx b/studio/frontend/src/features/model-picker/components/model-selector/model-load-settings-action.tsx
index 7138be61788..9cd02a756d9 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/model-load-settings-action.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/model-load-settings-action.tsx
@@ -30,8 +30,8 @@ export function ModelLoadSettingsAction({
}}
aria-label={ariaLabel}
className={cn(
- // Fixed box, not padding around the glyph, so this and the dots
- // menu hover as one size. Callers can still size it up.
+ // Fixed box, not padding around the glyph, so this and the dots menu hover as one size. Callers
+ // can still size it up.
"flex size-5 shrink-0 items-center justify-center rounded-md text-muted-foreground/60 transition-colors hover:bg-black/5 hover:text-foreground dark:hover:bg-white/10",
className,
)}
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/model-row-menu.tsx b/studio/frontend/src/features/model-picker/components/model-selector/model-row-menu.tsx
index 2354248d40a..fe08f9e1fed 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/model-row-menu.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/model-row-menu.tsx
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Condensed row actions for model rows: everything except the run-settings
-// gear collapses into one dots menu (pin, update, delete) so rows don't grow
-// an icon strip. Mirrors the sidebar chat rows' MoreVertical menu pattern.
+// Condensed row actions for model rows: everything except the run-settings gear collapses into
+// one dots menu (pin, update, delete) so rows do not grow an icon strip. Mirrors the sidebar
+// chat rows' MoreVertical menu pattern.
import {
DropdownMenu,
@@ -64,8 +64,8 @@ interface ModelRowMenuUpdate {
interface ModelRowMenuDelete {
title: string;
description: ReactNode;
- /** Repo (and quant) to preview the delete for, so the dialog can state what it actually
- * reclaims and what shared assets it leaves behind. Omit to keep the plain wording. */
+ /** Repo (and quant) to preview the delete for, so the dialog can state what it actually reclaims
+ * and what shared assets it leaves behind. Omit to keep the plain wording. */
impact?: { repoId: string; variant?: string | null };
successMessage: string;
disabled?: boolean;
@@ -157,8 +157,8 @@ export function ModelRowMenu({
const onUpdateConfirm = update?.onConfirm;
const handleUpdateConfirm = useCallback(() => {
- // Start the re-download and close the dialog; the Downloads panel owns
- // progress + cancel. Only a failure to START toasts.
+ // Start the re-download and close the dialog; the Downloads panel owns progress and cancel. Only
+ // a failure to START toasts.
void Promise.resolve()
.then(onUpdateConfirm)
.catch((err) => {
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/model-usage.ts b/studio/frontend/src/features/model-picker/components/model-selector/model-usage.ts
index c6665e76582..281a66c71e0 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/model-usage.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/model-usage.ts
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Tracks when each model was last loaded so the "Recent" sort can order by usage
-// (distinct from "Downloaded", which orders by the file's download date). Kept in
-// localStorage; ids are lowercased to match how the picker compares them.
+// Tracks when each model was last loaded so the "Recent" sort can order by usage, distinct from
+// "Downloaded", which orders by the file's download date. Kept in localStorage; ids are
+// lowercased to match how the picker compares them.
import { useEffect, useState } from "react";
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
index bf1d04c5ef3..b8fc160e081 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
@@ -415,8 +415,7 @@ function ListLabel({
action?: ReactNode;
collapsed?: boolean;
onToggle?: () => void;
- /** Draw a divider line above, evenly spaced, to separate it from the section
- * above (omit on the first section). */
+ /** Draw a divider line above to separate it from the section above (omit on the first section). */
divider?: boolean;
}) {
return (
@@ -457,10 +456,10 @@ function ListLabel({
function formatBytes(bytes: number): string {
// Guard non-positive / non-finite sizes so we never render "NaN undefined".
if (!Number.isFinite(bytes) || bytes <= 0) return "0 B";
- // Decimal (base-1000) units to match Hugging Face's reported file sizes (GPU-fit
- // math below stays base-1024 since VRAM is binary). Divide iteratively rather
- // than via Math.log, which has float error at exact powers of 1000 (mislabeling
- // 1 TB as "1000 GB") and could run off the end of units.
+ // Decimal (base-1000) units to match Hugging Face's reported sizes; the GPU-fit math stays
+ // base-1024. Divide iteratively, not via Math.log, which is off at exact powers of 1000.
+ // Divide iteratively rather than via Math.log, which has float error at exact powers of 1000
+ // (mislabeling 1 TB as "1000 GB") and could run off the end of units.
const units = ["B", "KB", "MB", "GB", "TB"];
let i = 0;
let value = bytes;
@@ -472,12 +471,8 @@ function formatBytes(bytes: number): string {
return `${value.toFixed(value < 10 ? 1 : 0)}${units[i]}`;
}
-// Small icon badges for what a model can do (vision / reasoning / audio).
-// Vision and reasoning badges were dropped to keep rows uncluttered.
-// Most distinguishing first, because only the first MAX_CAPABILITY_BADGES are drawn: what a model
-// GENERATES separates it from the whole list, reasoning separates it from very little. Each glyph
-// is the one the rest of the app already uses for that thing -- the Images and Video page icons
-// from the sidebar nav, the eye the On Device vision badge draws, the composer's thinking bulb.
+// Most distinguishing first, since only the first MAX_CAPABILITY_BADGES are drawn: what a
+// model GENERATES separates it from the list, reasoning very little.
const CAPABILITY_BADGES: {
key: keyof ModelCapabilities;
title: string;
@@ -499,8 +494,8 @@ const CAPABILITY_BADGES: {
},
{
key: "audio",
- // Direction-neutral, unlike the two above: `audio` covers ASR and audio classification as
- // well as speech synthesis, so a Whisper row would be claiming to generate what it consumes.
+ // Direction-neutral, unlike the two above: `audio` covers ASR and classification as well as
+ // synthesis, so a Whisper row would claim to generate what it consumes.
title: "Audio",
Glyph: (props) => (
<HugeiconsIcon icon={AudioWave01Icon} strokeWidth={1.8} {...props} />
@@ -508,20 +503,14 @@ const CAPABILITY_BADGES: {
},
];
-/**
- * Which capability glyphs are worth drawing in the current picker; null draws them all.
- *
- * A media picker has already filtered the list to one kind of model, so its own kind is not
- * information: every row of the Images picker generates images. Audio is the exception on Video,
- * where only some models carry a soundtrack. Context rather than a prop because every row in the
- * tree wants the same answer and it comes from the picker, not the row.
- */
+/** Which capability glyphs are worth drawing in the current picker; null draws them all. A
+ * media picker has already filtered to one kind, so its own kind is not information (Audio
+ * on Video is the exception). Context, not a prop, since it comes from the picker. */
const CapabilityScope = createContext<readonly (keyof ModelCapabilities)[] | null>(
null,
);
-// The row reserves a fixed slot for these (META_COLUMN.badge), so the cap is what keeps every
-// column after it lined up.
+// The row reserves a fixed slot for these (META_COLUMN.badge), so the cap is what keeps every column after it lined up.
const MAX_CAPABILITY_BADGES = 3;
/** The glyphs this row actually draws, so the caller can size the slot and skip an empty one. */
@@ -593,8 +582,8 @@ const FORMAT_TONE_DOT: Record<FormatTone, string> = {
adapter: "bg-format-adapter",
};
-/** Format as a coloured dot ahead of the name, named on hover. A word like
- * "Safetensors" is wide enough to shove the rest of the row around. */
+/** Format as a coloured dot ahead of the name, named on hover: a word like "Safetensors" is
+ * wide enough to shove the rest of the row around. */
function FormatTag({ tone, label }: { tone: FormatTone; label: string }) {
return (
<Tooltip delayDuration={0}>
@@ -617,9 +606,8 @@ function FormatTag({ tone, label }: { tone: FormatTone; label: string }) {
);
}
-/** "Already on disk", shown on Hub rows that are also downloaded. The Hub's own
- * on-device dot: a download arrow read as "click to fetch" on the one row that
- * needs no fetching. Hit area and tooltip as FormatTag. */
+/** "Already on disk", shown on Hub rows that are also downloaded: their own download arrow
+ * reads as "click to fetch" on the one row that needs no fetching. */
function DownloadedBadge() {
return (
<Tooltip delayDuration={0}>
@@ -679,26 +667,23 @@ const ORANGE = "!text-orange-600 dark:!text-orange-300";
/** Over budget but still card-sized. `_select_gpus` scores against FREE VRAM, so whether this
* lands on the GPU depends on what else is resident; missing it costs speed, not the load. */
-/** Over the VRAM Budget, still smaller than the card. Not conditional: `_vram_usable_mib` gives
- * `free - reserve`, which on a completely idle card IS the budget this tier has already passed,
- * so `_select_gpus` hands it to --fit every time. Other apps only shrink `free` further. Raising
- * the budget is the lever that keeps it resident, which is why the card size is worth saying. */
+/** Over the VRAM Budget, still smaller than the card. Not conditional: `_vram_usable_mib`
+ * gives `free - reserve`, which on an idle card IS the budget this tier passed, so
+ * `_select_gpus` hands it to --fit every time. Raising the budget is the lever. */
const MIGHT_FIT: FitVerdict = {
label: "Over budget",
tone: AMBER,
hint: "Larger than your VRAM Budget allows, so part of it offloads even on an idle GPU. It is still smaller than the card, so raising the budget can keep it resident.",
};
-/** Past the card. Every GGUF over budget lands here, however far over, because llama-server never
- * refuses one on size: `_select_gpus` returns `(None, use_fit=True)` and `--fit` offloads the
- * rest to CPU. Splitting this by how far over only mattered on hosts where the RAM tier exists,
- * and it cost the honest answer on the ones where it does not. */
+/** Past the card. Every GGUF over budget lands here however far over, since llama-server never
+ * refuses one on size: `_select_gpus` returns `(None, use_fit=True)` and --fit offloads. */
const OFFLOADS: FitVerdict = {
label: "Does not fit",
tone: ORANGE,
hint: "Model may not fit but still works with offloading. Expect slower inference.",
};
-/** checkVramFit's 75-100% band, which is the ONLY source of `tight` here: a torch estimate that
- * still fits on the card entirely. It has no --fit, so it neither spills nor offloads. */
+/** checkVramFit's 75-100% band, the ONLY source of `tight` here: a torch estimate that still
+ * fits on the card entirely, with no --fit, so it neither spills nor offloads. */
const DEVICE_TIGHT: FitVerdict = {
label: "Tight fit",
tone: AMBER,
@@ -708,15 +693,13 @@ const DEVICE_TIGHT: FitVerdict = {
const WONT_FIT: FitVerdict = {
label: "Does not fit",
tone: ORANGE,
- // "memory", not "VRAM": this also carries a diffusion refusal on a shared pool, where the two
- // are the same bytes and calling it VRAM would read as a different number than the user has.
+ // "memory", not "VRAM": this also carries a diffusion refusal on a shared pool, where the two are the same bytes.
hint: "Needs more memory than this device has. This model will not load.",
};
/** What each fit verdict marks and says, keyed by the Hub's classes so one question has one
- * vocabulary. `tight` and `exceeds` are the training estimator's words and reach here ONLY from a
- * torch pipeline or the QLoRA estimate, never from a GGUF, so both describe a load with no --fit
- * to fall back on. That is why neither says anything about offloading. */
+ * vocabulary. `tight` and `exceeds` are the training estimator's words and reach here only
+ * from a torch pipeline or the QLoRA estimate, which have no --fit and never offload. */
const VRAM_VERDICT: Record<GgufFitClass | VramFitStatus, FitVerdict | null> = {
fits: null,
marginal: MIGHT_FIT,
@@ -731,23 +714,11 @@ const VRAM_VERDICT: Record<GgufFitClass | VramFitStatus, FitVerdict | null> = {
exceeds: WONT_FIT,
};
-/** Whether a diffusion `oom` is a REFUSAL rather than an offload.
- *
- * On discrete VRAM an oversized pipeline still loads: `diffusion_memory.py` degrades it to group
- * or whole-module CPU offload and streams from host RAM. On a shared pool there is nowhere to
- * stream from, so `unified_memory_shortfall_message` refuses up front, and the docstring is blunt
- * about why: without the refusal the load "allocates past physical memory", with no torch OOM to
- * catch because the MPS high-watermark is disabled, so "the failure is the OS killing the process
- * with no Python exception".
- *
- * Telling that user the model "still works with offloading" is the worst thing this badge could
- * say, so a diffusion oom on a host pool takes the verdict that means it will not load.
- *
- * `hostPooled` is the LOAD DEVICE's answer, and folds unified_memory in: hardware.py sets
- * shared_memory only on Windows, so a Linux ROCm APU arrives as unified true / shared false while
- * `diffusion_memory.py` still classifies it `unified_memory` and refuses. Reading the aggregate
- * shared flag missed that host, and reading a plain some(unified) would refuse on a discrete card
- * that merely sits beside an iGPU. */
+/** Whether a diffusion `oom` is a REFUSAL rather than an offload. On discrete VRAM an oversized
+ * pipeline still streams from host RAM; on a shared pool the loader refuses up front, since
+ * the MPS high-watermark is disabled and the OS kills the process with no exception.
+ * `hostPooled` is the LOAD DEVICE's answer and folds unified_memory in, since hardware.py
+ * sets shared_memory only on Windows while diffusion_memory.py still refuses on Linux APUs. */
function diffusionRefuses(
fit: GgufFitClass,
diffusionLoad: boolean,
@@ -757,16 +728,14 @@ function diffusionRefuses(
}
/** The RAM a DIFFUSION verdict may add to the GPU budget. Zero on a host pool: offload there
- * moves bytes inside the one pool and frees nothing, so `classifyMediaGgufFit` adding a RAM tier
- * promised an offload the planner refuses. llama.cpp is not this case -- a GGUF really does spill
- * into whatever host RAM the GPU window does not already cover, and `systemRamAvailableGb` now
- * has that window subtracted out. */
+ * moves bytes inside one pool and frees nothing. llama.cpp differs, since a GGUF really
+ * does spill into host RAM the GPU window does not cover. */
function mediaRamBudgetGb(systemRamGb: number, hostPooled: boolean): number {
return hostPooled ? 0 : systemRamGb;
}
/** The verdicts that read as over budget, which is what dims a row. `marginal` does not: it is
- * a full GPU load, just without much room to spare. */
+ * a full GPU load with little room to spare. */
function isOverBudget(status?: GgufFitClass | VramFitStatus | null): boolean {
return (
status === "partial" ||
@@ -790,8 +759,8 @@ function VramBadge({
return (
<Tooltip delayDuration={0}>
<TooltipTrigger asChild={true}>
- {/* The mark sits inside the row/variant button, so a tap meant to read the explanation
- would otherwise select the model or start its download. */}
+ {/* The mark sits inside the row/variant button, so a tap meant to read the explanation would
+ otherwise select the model or start its download. */}
{/* biome-ignore lint/a11y/useKeyWithClickEvents: the handler suppresses the enclosing
button, it does not add an interaction of its own */}
<span
@@ -846,10 +815,8 @@ function SizeText({ value }: { value: string }) {
);
}
-/** Keep the row's size treatment consistent with every other model. Diffusion
- * GGUFs get one small explanation affordance because their checkpoint is only
- * part of what the loader must keep on disk. The icon is decorative: the
- * explanation hangs off the row button, the one focusable element here. */
+/** Keep the row's size treatment consistent with every other model; diffusion GGUFs get one
+ * small explanation affordance, since their checkpoint is only part of what is kept on disk. */
export function GgufDownloadFootprint({
checkpointBytes,
companionBytes,
@@ -858,8 +825,8 @@ export function GgufDownloadFootprint({
companionBytes: number;
}) {
const totalBytes = checkpointBytes + companionBytes;
- // Whole-GB rounding is too lossy for a sum: "2.6 GB + 8.2 GB = 11 GB"
- // looks contradictory. Keep one decimal for the aggregate through GB/TB.
+ // Whole-GB rounding is too lossy for a sum ("2.6 GB + 8.2 GB = 11 GB" looks contradictory),
+ // so keep one decimal through GB/TB.
const totalLabel =
totalBytes >= 1_000_000_000 && totalBytes < 1_000_000_000_000
? `${(totalBytes / 1_000_000_000).toFixed(1)} GB`
@@ -927,11 +894,8 @@ function isRuntimeLoadedModel(
: !hasActiveGgufVariant;
}
-// Shared row columns, so the meta lines up down the list instead of drifting
-// with each name's length. Widths are em of the slot's own text, so they
-// follow the UI font scale, and collapse below the picker's full width.
-// min-w-min means a width is the column held open, not a clamp: an outsized
-// badge grows its own slot rather than spilling over the next one.
+// Shared row columns, so meta lines up down the list. Widths are em of the slot's own text, so
+// they follow the UI font scale; min-w-min holds a width open rather than clamping.
/** A GPU inventory in the shape the catalog's fit rules take. */
function artifactBudget(gpu: {
memoryTotalGb: number;
@@ -943,10 +907,10 @@ function artifactBudget(gpu: {
const META_COLUMN = {
// Fits "UD-Q4_K_XL"; a hard cap, so longer quants clip.
quant: "min-[560px]:w-[7.2em]",
- // The badge slot holds capability glyphs (18px), the vision badge (24px) and the Hub lists'
- // "on disk" mark (14px), gap-1 between them. Each width below is the widest set its scope can
- // draw, since anything wider makes min-w-min expand the slot and shift every column after it.
- // Scope draws no glyph: the vision badge alone, or the disk mark alone.
+ // Each width below is the widest set its scope can draw: anything wider makes min-w-min expand
+ // the slot and shift every column after it.
+ // The slot holds capability glyphs (18px), the vision badge (24px) and the "on disk" mark (14px),
+ // gap-1 between them. Scope draws no glyph: the vision badge alone, or the disk mark alone.
badge: "min-w-min min-[560px]:w-[24px]",
// One glyph plus the disk mark (18 + 4 + 14).
badgeMid: "min-w-min min-[560px]:w-[36px]",
@@ -967,16 +931,14 @@ const META_COLUMN = {
// as its gap to the modality mark. Hub keeps its own width for "2779.5B".
param: "min-w-min min-[560px]:w-[4.4em]",
paramWide: "min-w-min min-[560px]:w-[5.2em]",
- // formatBytes writes no space ("536MB"), so the widest this holds is 29.5px, not the ~40px a
- // spaced "536 MB" needs. The surplus sat between the parameter chip and the size.
+ // formatBytes writes no space ("536MB"), so the widest this holds is 29.5px, not the ~40px a spaced "536 MB" needs.
size: "min-w-min min-[560px]:w-[3.2em]",
// The format dot that leads the row; the name lives in its tooltip.
format: "min-[560px]:w-[14px]",
} as const;
-// One gutter for every row, gear or no gear, so the columns never shift by a
-// button. The buttons show on the hovered row, or while their menu is open;
-// the gutter stays open so nothing moves as they appear.
+// One gutter for every row, gear or no gear, so the columns never shift by a button; the
+// buttons show on hover or while their menu is open.
const ROW_ACTIONS_CLASS =
"mr-0.5 flex w-[38px] shrink-0 items-center justify-end -space-x-0.5 opacity-0 transition-opacity focus-within:opacity-100 group-hover:opacity-100 group-focus-within:opacity-100 has-[[data-state=open]]:opacity-100 [@media(hover:none)]:opacity-100";
@@ -1023,9 +985,8 @@ function ModelRow({
vramEst?: number;
gpuGb?: number;
tooltipText?: ReactNode;
- /** Hugging Face address (e.g. "huggingface.co/owner/name") for online/Hub
- * rows; surfaced on hover so their repo id / URL is discoverable the same
- * way local rows show an on-disk path. Omit to show no address line. */
+ /** Hugging Face address for online/Hub rows, surfaced on hover the way local rows show an
+ * on-disk path. Omit to show no address line. */
hubUrl?: string;
optionProps?: ModelRowOptionProps;
onArrowDownIntoChildren?: () => boolean;
@@ -1046,17 +1007,14 @@ function ModelRow({
/** Grey chip beside the name, for rows that load one specific quant. */
quantChip?: string | null;
/** Chips for what used to sit in brackets after the name: the artifact format, and a
- * resolution when one variant differs from another only by it. Same chip as On Device. */
+ * resolution when variants differ only by it. */
tags?: string[];
- /** Column layout (see META_COLUMN): "device" reserves the quant chip,
- * "hub" the download and VRAM badges those lists carry instead. */
+ /** Column layout (see META_COLUMN): "device" reserves the quant chip, "hub" the download and VRAM badges. */
alignMeta?: "device" | "hub";
- /** Hold the size column open. Hub rows pass this on the MLX and Safetensors
- * filters, where a repo is one download with one size. */
+ /** Hold the size column open. Hub rows pass this on the MLX and Safetensors filters, where a
+ * repo is one download with one size. */
showSize?: boolean;
- /** Identifies the on-disk model whose VRAM split the row should chart.
- * Omit for rows that are not downloaded -- there is nothing to size until
- * the weights exist locally. */
+ /** Identifies the on-disk model whose VRAM split the row should chart; omit for rows that are not downloaded. */
memory?: ModelMemorySource;
className?: string;
}) {
@@ -1066,9 +1024,9 @@ function ModelRow({
const vramTooltipText =
showVramTooltip && vramStatus
? exceeds
- ? // "memory", not "VRAM": every over-budget verdict here is a total. A GGUF at
- // `partial` splits across VRAM and RAM, and the figure is weights plus activations
- // plus KV, so "Needs ~47GB VRAM" contradicted the offload verdict beside it.
+ // "memory", not "VRAM": a GGUF at `partial` splits across VRAM and RAM and the figure
+ // is weights plus activations plus KV, so "Needs ~47GB VRAM" contradicted the verdict.
+ ?
`Needs ~${vramEst}GB memory (GPU: ${gpuGb}GB)`
: vramStatus === "tight" || vramStatus === "marginal"
? `~${vramEst}GB VRAM (tight fit on ${gpuGb}GB)`
@@ -1076,8 +1034,8 @@ function ModelRow({
: null;
const { owner, name } = splitRepoLabel(label);
- // Drop our own owner: the list is nearly all unsloth/, so it is noise.
- // Other owners still show, which is what tells the two apart.
+ // Drop our own owner: the list is nearly all unsloth/, so it is noise, and other owners still
+ // showing is what tells the two apart.
const showOwner = !!owner && !hideOwner && !isUnslothOwner(owner);
const parsed = parseMetaTokens(meta);
// Param chip from meta, else derived from the name so GGUF rows show it too.
@@ -1088,9 +1046,8 @@ function ModelRow({
const capabilityBadges = visibleCapabilityBadges(caps, capabilityScope);
const showCaps = capabilityBadges.length > 0;
const aligned = alignMeta !== undefined;
- // Reserve only what this picker's scope can draw. The Images and Audio pickers draw no glyph and
- // Video draws one, so holding the chat row's slot open there is dead space on every row.
- // Unscoped chat splits again by list: On Device marks vision, Hub marks the disk.
+ // Reserve only what this picker's scope can draw: Images and Audio draw no glyph and Video
+ // draws one, so holding the chat row's slot open there is dead space.
const badgeColumn =
capabilityScope === null || capabilityScope.length > 1
? alignMeta === "device"
@@ -1099,8 +1056,7 @@ function ModelRow({
: capabilityScope.length === 1
? META_COLUMN.badgeMid
: META_COLUMN.badge;
- // One dot per row. A second format shares the first's colour anyway, so it
- // rides along in the tooltip instead of pushing the name out of line.
+ // One dot per row: a second format shares the first's colour anyway, so it rides along in the tooltip.
const formatDot = parsed.formats[0]
? {
tone: parsed.formats[0].tone,
@@ -1108,9 +1064,7 @@ function ModelRow({
}
: null;
- // Only the selected row charts itself. A meter under every row turns a list
- // you scan into a wall of charts, and a model that comfortably fits is not
- // news; the one you are actually considering is. At most one bar is on screen.
+ // Only the selected row charts itself: a meter under every row turns a list you scan into a wall of charts.
const memorySegments = useModelMemory(selected ? memory : undefined, gpuGb);
const showMemoryBar = memorySegments.status !== "unknown";
@@ -1127,9 +1081,8 @@ function ModelRow({
}}
onClick={onClick}
className={cn(
- // pl-[5.5px]: the dot is centred in a 14px hover target, so 5.5 + (14 - 5) / 2 lands it
- // on 10px, level with the section labels at px-2.5. Inside a w-full button, so the hover
- // pill itself does not move.
+ // pl-[5.5px]: the dot is centred in a 14px hover target, so 5.5 + (14 - 5) / 2 lands it on
+ // 10px, level with the section labels at px-2.5.
"group/row flex w-full flex-col items-stretch py-1.5 pl-[5.5px] pr-2 text-left text-sm transition-colors hover:bg-[#ececec] focus-visible:bg-[#ececec] focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring dark:hover:bg-[var(--sidebar-accent)] dark:focus-visible:bg-[var(--sidebar-accent)]",
showMemoryBar ? "rounded-2xl" : "rounded-full",
selected && "bg-[#ececec] dark:bg-[var(--sidebar-accent)]",
@@ -1137,12 +1090,11 @@ function ModelRow({
)}
>
{/* gap-1: the quant chip ends the name group, so what this separates is that chip from the
- first meta mark. 4px is the rhythm the meta columns already keep among themselves. */}
+ first meta mark, on the rhythm the meta columns keep. */}
<span
className={cn(
"flex w-full items-center gap-1",
- // Over budget reads as a dimmed row, which scans; hover restores it and shows the pill.
- // The selected row keeps full weight, since it is the one being considered.
+ // Over budget reads as a dimmed row, which scans; hover restores it. The selected row keeps full weight.
exceeds &&
!selected &&
"opacity-60 transition-opacity group-hover/row:opacity-100 group-focus-visible/row:opacity-100",
@@ -1285,8 +1237,7 @@ function ModelRow({
{text}
</span>
))}
- {/* GGUF repos hold several quants of different sizes, so their rows
- report one only once expanded, leaving the column an empty gap. */}
+ {/* GGUF repos hold several quants of different sizes, so their rows report one only once expanded. */}
{alignMeta === "device" || showSize ? (
<span
className={cn(
@@ -1311,16 +1262,14 @@ function ModelRow({
</button>
);
- // Optional Hugging Face address line for online/Hub rows, rendered under
- // whichever tooltip shows so the repo id / URL is always visible on hover.
+ // Optional Hugging Face address line for online/Hub rows, rendered under whichever tooltip shows.
const hubUrlLine = hubUrl ? (
<span className="block mt-1 text-ui-10 text-muted-foreground break-all">
{hubUrl}
</span>
) : null;
- // The dot names its format on hover only, which keyboard focus never
- // reaches, so the row tooltip carries it too.
+ // The dot names its format on hover only, which keyboard focus never reaches, so the row tooltip carries it too.
const formatLine = formatDot ? (
<span className="block text-ui-10 mt-1">{formatDot.label}</span>
) : null;
@@ -1367,7 +1316,6 @@ function ModelRow({
return content;
}
-// ── GGUF Variant Expander ────────────────────────────────────
function isValidGgufVariant(variant: unknown): variant is GgufVariantDetail {
if (!variant || typeof variant !== "object") return false;
@@ -1385,9 +1333,8 @@ function isValidGgufVariant(variant: unknown): variant is GgufVariantDetail {
candidate.shard_count >= 0)) &&
(candidate.downloaded === undefined ||
typeof candidate.downloaded === "boolean") &&
- // Carried through so each row can look up its own dependency group's
- // footprint. Absent or null on an older backend, which groups the repo as
- // one, so it must never reject the row.
+ // Carried through so each row can look up its own dependency group's footprint. Absent on an
+ // older backend, which groups the repo as one, so it must never reject the row.
(candidate.dependency_key === undefined ||
candidate.dependency_key === null ||
typeof candidate.dependency_key === "string")
@@ -1428,9 +1375,8 @@ function normalizeGgufVariantsResponse(
contextLength >= 0
? contextLength
: null,
- // The backend's own verdict, which resolves existence-first: a marker-less relative name
- // that exists on disk is a local model even though no path prefix says so. A server that
- // predates the field omits it, leaving the prefix test to answer alone as before.
+ // The backend's own verdict, which resolves existence-first: a marker-less relative name that
+ // exists on disk is a local model. A server predating the field leaves the prefix test.
resolvedLocally: res?.resolved_locally === true,
};
}
@@ -1444,15 +1390,15 @@ function ggufVariantExpectedBytes(variant: GgufVariantDetail): number {
: variant.size_bytes;
}
-/** The one quant a repo holds, plus the vision flag read with it. The
- * collapsed row never mounts the expander, so this is its only source. */
+/** The one quant a repo holds, plus the vision flag read with it. The collapsed row never
+ * mounts the expander, so this is its only source. */
interface SoleDownloadedQuant {
variant: GgufVariantDetail;
hasVision: boolean;
}
-/** The repo's one complete quant, or null when it holds none, holds several,
- * or could not be read. Disk-only and client-cached: no remote listing. */
+/** The repo's one complete quant, or null when it holds none, holds several, or could not be
+ * read. Disk-only and client-cached. */
async function readSoleQuant(
target: SoleQuantTarget,
hfToken?: string,
@@ -1464,8 +1410,7 @@ async function readSoleQuant(
});
const normalized = normalizeGgufVariantsResponse(res);
const local = normalized.variants;
- // One file on disk and nothing torn beside it. A partial quant keeps the
- // expander, where it can be resumed.
+ // One file on disk and nothing torn beside it; a partial quant keeps the expander, where it can be resumed.
if (local.length !== 1 || local[0].downloaded !== true) return null;
return { variant: local[0], hasVision: normalized.hasVision };
} catch {
@@ -1477,14 +1422,13 @@ const EMPTY_SOLE_QUANT_ENTRIES: ReadonlyMap<
string,
SoleQuantEntry<SoleDownloadedQuant>
> = new Map();
-// Reads run a few at a time, so a large cache doesn't fire one request per
-// repo. A worker pool, not fixed batches: one slow repo holds up only itself.
+// Reads run a few at a time so a large cache does not fire one request per repo. A worker
+// pool, not fixed batches: one slow repo holds up only itself.
const SOLE_QUANT_WORKERS = 6;
-/** On Device repos holding exactly one quant on disk, keyed by repo id. With
- * "Show all quantizations" off there is nothing else to pick, so those repos
- * collapse into one pinned-style row. Results are kept per repo, so one
- * repo's download or delete leaves every other row as it was. */
+/** On Device repos holding exactly one quant on disk, keyed by repo id: with "Show all
+ * quantizations" off those collapse into one pinned-style row. Kept per repo, so one
+ * repo's download or delete leaves the others alone. */
function useSoleDownloadedQuants(
repos: readonly CachedGgufRepo[],
{ enabled, hfToken }: { enabled: boolean; hfToken?: string },
@@ -1517,9 +1461,8 @@ function useSoleDownloadedQuants(
[targets, entries, enabled],
);
- // A change outside this tab, another window or the CLI, moves the row's
- // bytes without touching this instance's variants cache. Drop that repo's
- // cached listing so the read, and every other reader, sees disk again.
+ // A change outside this tab moves the row's bytes without touching this instance's variants
+ // cache, so drop that repo's cached listing.
const fingerprintsRef = useRef(new Map<string, string>());
useEffect(() => {
for (const repoId of takeDriftedRepos(targets, fingerprintsRef.current)) {
@@ -1527,14 +1470,14 @@ function useSoleDownloadedQuants(
}
}, [targets]);
- // Reads outlive a render, so they run outside it. The token is read at call
- // time, so a change to it does not strand the reader.
+ // Reads outlive a render, so they run outside it; the token is read at call time, so a change
+ // to it does not strand the reader.
const hfTokenRef = useRef(hfToken);
hfTokenRef.current = hfToken;
const mountedRef = useRef(true);
useEffect(() => {
- // Set on setup, not just cleared on teardown: StrictMode replays effects,
- // and a ref left false would discard every later read.
+ // Set on setup, not just cleared on teardown: StrictMode replays effects, and a ref left false
+ // would discard every later read.
mountedRef.current = true;
return () => {
mountedRef.current = false;
@@ -1609,16 +1552,14 @@ function GgufVariantExpander({
gpuGb?: number;
systemRamGb?: number;
budgetKnown?: boolean;
- /** HF token threaded into the variant fetch so private/gated repos resolve
- * their GGUF variants (and update badges). */
+ /** HF token threaded into the variant fetch so private/gated repos resolve their GGUF variants. */
hfToken?: string;
parentOptionKey?: string;
onNavigatePastStart?: () => void;
onNavigatePastEnd?: () => void;
onConfigure?: (id: string, meta: ModelSelectorChangeMeta) => void;
sourceOverride?: ModelSelectorChangeMeta["source"];
- /** Update/delete actions for cached variant rows. Omitted by browse-only
- * expanders (Recommended, etc.) that don't manage on-disk variants. */
+ /** Update/delete actions for cached variant rows; omitted by browse-only expanders. */
variantActions?: {
onUpdate?: (quant: string, expectedBytes: number) => Promise<void> | void;
updateTitle?: string;
@@ -1631,11 +1572,10 @@ function GgufVariantExpander({
getDeleteSuccessMessage?: (quant: string) => string;
deleteDisabled?: boolean;
};
- /** On Device rows honor the Show all quantizations setting; Recommended and
- * other browse lists always show every quant. */
+ /** On Device rows honor the Show all quantizations setting; browse lists always show every quant. */
onDevice?: boolean;
- /** Only managed cached-Hub rows can surface quant pins in the Pinned
- * section. Local-path expanders deliberately leave this false. */
+ /** Only managed cached-Hub rows can surface quant pins in the Pinned section; local-path
+ * expanders leave this false. */
allowPin?: boolean;
/** Report GGUF vision support up so the parent row can badge it. */
onHasVision?: (hasVision: boolean) => void;
@@ -1671,20 +1611,20 @@ function GgufVariantExpander({
useEffect(() => {
let canceled = false;
- // Collapsing the row drops the request: a stalled one otherwise holds a
- // per-host connection, and enough of them stall download and load too.
+ // Collapsing the row drops the request: a stalled one holds a per-host connection, and enough
+ // of them stall download and load too.
const controller = new AbortController();
queueMicrotask(() => {
if (canceled) return;
setLoading(true);
setError(null);
- // Belongs to the identifier being listed: carrying it over would apply the previous
- // row's locality to this one's footprint arithmetic.
+ // Belongs to the identifier being listed: carrying it over would apply the previous row's
+ // locality to this one's footprint arithmetic.
setResolvedLocally(false);
});
// The row's own directory, so disk contents count against that cache, not the active one. No
- // preferLocalCache: it answers from disk alone and drops the undownloaded.
+ // preferLocalCache: it answers from disk alone.
listGgufVariants(repoId, hfToken, {
...(localSource ? { localPath: localSource } : {}),
signal: controller.signal,
@@ -1713,17 +1653,17 @@ function GgufVariantExpander({
};
}, [repoId, localSource, refreshKey, hfToken]);
- // Covers Unix absolute (/), Windows drive (C:\, D:/), UNC (\\server), relative (./, ../), tilde (~/)
+ // Covers Unix absolute, Windows drive, UNC, relative and tilde paths.
const isLocalPath = /^(\/|\.{1,2}[\\/]|~[\\/]|[A-Za-z]:[\\/]|\\\\)/.test(
repoId,
);
// The prefix test cannot see a marker-less relative directory like "models/my-image-model",
- // which the backend loads off disk. Whether the checkpoint is on disk decides the footprint
- // arithmetic, so that question is asked of the listing, not of the spelling.
+ // so whether the checkpoint is on disk is asked of the listing, not of the spelling.
const checkpointIsLocal = isLocalPath || resolvedLocally;
const handleVariantClick = useCallback(
- // ``filename`` is required, not decorative: the diffusion pages load a quant with {kind: "gguf", filename} and gate that branch on meta.ggufFilename, so a quant label alone made every Images/Video GGUF pick a dead click.
+ // `filename` is required: the diffusion pages gate their GGUF branch on meta.ggufFilename, so
+ // a quant label alone made every Images/Video GGUF pick a dead click.
(
quant: string,
filename: string,
@@ -1756,15 +1696,15 @@ function GgufVariantExpander({
],
);
- // The user's saved VRAM Budget, which is what the loader admits against. The picker used to
- // ignore it, so moving the slider changed the Hub's verdicts and left chat's untouched.
+ // The user's saved VRAM Budget, which is what the loader admits against; the picker used to
+ // ignore it, so moving the slider changed the Hub's verdicts only.
const budgetFraction = useVramBudgetFraction() ?? undefined;
const anyBudgetGb = (gpuGb ?? 0) > 0 || (systemRamGb ?? 0) > 0;
const getGgufFit = useCallback(
(sizeBytes: number): GgufFitClass => {
- // Permissive only when no budget was measured. A known zero Vulkan budget means every
- // non-empty variant is OOM, which classifyGgufFit cannot tell apart from "not probed yet".
+ // Permissive only when no budget was measured: a known zero Vulkan budget means every
+ // non-empty variant is OOM, which classifyGgufFit cannot tell from "not probed yet".
if (!anyBudgetGb) return budgetKnown ? "oom" : "fits";
if (diffusionLoad) {
return classifyMediaGgufFit(
@@ -1801,8 +1741,8 @@ function GgufVariantExpander({
[variantGroups, defaultVariant],
);
- // Each workflow gets its own recommendation. If its preferred variant is
- // OOM, use the largest one that can run; if all are OOM, use the smallest.
+ // Each workflow gets its own recommendation: if its preferred variant is OOM use the largest
+ // that can run, and if all are OOM the smallest.
const effectiveRecommendedByGroup = useMemo(() => {
const recommended = new Map<string, string>();
for (const group of variantGroups) {
@@ -1829,12 +1769,8 @@ function GgufVariantExpander({
}
return recommended;
}, [variantGroups, preferredByGroup, anyBudgetGb, budgetKnown, getGgufFit]);
- // The same recommendations, reachable from a row. `effectiveRecommendedByGroup`
- // is keyed by PRESENTATION group ("quantizations", "text-frames",
- // "reference-media"); the footprint pass below buckets by the backend's
- // dependency_key ("flux.2-klein:<digest>"). Those are different key spaces,
- // so that pass has to ask through the variant itself, which is the object
- // the presentation grouping already placed.
+ // `effectiveRecommendedByGroup` is keyed by PRESENTATION group while the footprint pass
+ // buckets by the backend's dependency_key, so that pass asks through the variant itself.
const recommendedQuantForVariant = useMemo(() => {
const byVariant = new Map<GgufVariantDetail, string>();
for (const group of variantGroups) {
@@ -1866,8 +1802,7 @@ function GgufVariantExpander({
const bIsRec = b.quant === recommended;
if (aIsRec !== bIsRec) return aIsRec ? -1 : 1;
- // fits: largest first (best quality that fits in GPU)
- // tight/OOM: smallest first (closest to fitting, fastest to run)
+ // fits: largest first (best quality that fits); tight/OOM: smallest first (closest to fitting).
const fitsInGpu = aTier === 0 || aTier === 2;
return fitsInGpu
? b.size_bytes - a.size_bytes
@@ -1876,8 +1811,7 @@ function GgufVariantExpander({
});
}, [variants, variantGroups, effectiveRecommendedByGroup, getGgufFit]);
- // On Device only: when Show all quantizations is off, list quants already on
- // disk, torn ones included. Browse lists always show every quant.
+ // On Device only: with Show all quantizations off, list quants already on disk, torn ones included.
const showAllQuantizations = useChatRuntimeStore(
(s) => s.showAllQuantizations,
);
@@ -1897,32 +1831,22 @@ function GgufVariantExpander({
[displayVariants],
);
- // A diffusion GGUF is not self-contained: the loader also needs a text
- // encoder, VAE, tokenizer and configs. That companion set is NOT
- // repository-wide, so one representative's footprint cannot speak for the
- // whole listing: a neutral repo can hold GGUFs of different families with
- // different base repos, and FLUX.2-klein picks a different text encoder for
- // its 9B checkpoints than for its 4B ones. Both are folded into the
- // backend's dependency_key, so grouping by it is what keeps a non
- // representative row from advertising a GB-wrong total. One request per
- // distinct key: the ordinary repo has exactly one, which is the cost this
- // representative scheme exists to protect.
+ // A diffusion GGUF is not self-contained: the loader also needs a text encoder, VAE, tokenizer
+ // and configs, and that companion set is NOT repository-wide (one repo can hold GGUFs of
+ // different families). Both are folded into the backend's dependency_key, so grouping by it
+ // keeps a non-representative row from advertising a GB-wrong total.
const footprintVariants = useMemo(() => {
const byKey = new Map<string, GgufVariantDetail>();
for (const variant of displayVariants ?? []) {
- // An unkeyed repo (older backend, or no family resolved) collapses to one
- // group, which is exactly the previous repo-wide behavior.
+ // An unkeyed repo (older backend, or no family resolved) collapses to one group, the previous repo-wide behavior.
const key = variant.dependency_key ?? "";
const current = byKey.get(key);
if (current === undefined) {
byKey.set(key, variant);
continue;
}
- // The recommended quant is the representative of its own group when it
- // has one; otherwise the group's first row stands. Asked per variant, not
- // of a flattened set: two families in one neutral repo can share quant
- // names, so global membership would let the other group's pick stand in
- // here and resolve companions against the wrong base repo.
+ // The recommended quant represents its own group when it has one, else the group's first row.
+ // Asked per variant, since two families in one repo can share quant names.
const recommended = recommendedQuantForVariant.get(variant);
if (
recommended !== undefined &&
@@ -1940,9 +1864,8 @@ function GgufVariantExpander({
useEffect(() => {
let cancelled = false;
setCompanionBytesByKey(new Map());
- // A local path is resolved too: only the CHECKPOINT is on disk. Its text encoder, VAE,
- // tokenizer and configs still come from the remote base, which is the larger half of the
- // footprint, so suppressing the request understated a local row by many gigabytes.
+ // A local path is resolved too: only the CHECKPOINT is on disk, and its remote base is the
+ // larger half, so suppressing the request understated a local row by many gigabytes.
if (!resolveDownloadFootprint) {
return () => {
cancelled = true;
@@ -1963,10 +1886,9 @@ function GgufVariantExpander({
})
.then((footprint) => {
if (cancelled || !footprint) return;
- // A checkpoint already on disk is not part of required_bytes at all, so nothing may
- // be subtracted for it: the whole figure IS the remote companion set. Subtracting
- // anyway drove the total to zero and hid a multi-GB companion set behind the
- // checkpoint size. Only a hub pick carries its checkpoint inside the total, and
+ // A checkpoint already on disk is not part of required_bytes, so nothing may be subtracted for
+ // it: subtracting drove the total to zero and hid a multi-GB companion set. Only a hub pick
+ // carries its checkpoint inside the total.
// expectedBytes stands in when the planner could not size it.
const checkpoint = checkpointIsLocal
? 0
@@ -1975,9 +1897,8 @@ function GgufVariantExpander({
: expectedBytes;
const companion = footprint.requiredBytes - checkpoint;
if (Number.isFinite(companion) && companion > 0) {
- // A fresh Map per resolution: React compares state by identity, and
- // the groups resolve independently, so a mutation would drop the
- // rows whose request landed first.
+ // A fresh Map per resolution: React compares state by identity and the groups resolve
+ // independently, so a mutation would drop the rows whose request landed first.
setCompanionBytesByKey((previous) => {
const next = new Map(previous);
next.set(dependencyKey, companion);
@@ -1986,8 +1907,8 @@ function GgufVariantExpander({
}
})
.catch(() => {
- // The checkpoint size remains useful when an older backend or a Hub
- // metadata failure cannot provide the companion footprint.
+ // The checkpoint size stays useful when an older backend or a Hub failure cannot provide the
+ // companion footprint.
});
}
return () => {
@@ -2058,8 +1979,8 @@ function GgufVariantExpander({
}
className="pl-4 border-l-2 border-accent/50 ml-3 my-1"
>
- {/* On Device shows the model name above, so the Quantizations heading is
- redundant; its Vision badge is relayed to the name instead. */}
+ {/* On Device shows the model name above, so the Quantizations heading is redundant; its Vision
+ badge is relayed to the name instead. */}
{!onDevice && !displayVariantGroups.some((group) => group.title) && (
<div className="px-2 py-1 flex items-center gap-1.5">
<span className="text-ui-10 font-semibold uppercase tracking-wider text-muted-foreground">
@@ -2097,16 +2018,15 @@ function GgufVariantExpander({
);
const showGroupHeading =
group?.title != null && group.variants[0]?.filename === v.filename;
- // Its own group's pick. Matching on the quant alone happens to work only
- // because an H3 key is unique per file, which is the backend's rule.
+ // Its own group's pick. Matching on the quant alone works only because an H3 key is unique per
+ // file, which is the backend's rule.
const isRecommended =
group != null &&
effectiveRecommendedByGroup.get(group.key) === v.quant;
const fit = getGgufFit(v.size_bytes);
const oom = fit === "oom";
const expectedBytes = ggufVariantExpectedBytes(v);
- // This row's own dependency group, never the listing's: see the
- // footprintVariants comment above.
+ // This row's own dependency group, never the listing's: see the footprintVariants comment above.
const companionBytes =
companionBytesByKey.get(v.dependency_key ?? "") ?? null;
// A folder has no download to resume; a quant short a shard has no files to load.
@@ -2303,14 +2223,13 @@ function GgufVariantExpander({
disabled: deleteDisabled,
onConfirm: async () => {
await onDeleteVariant(v.quant);
- // Drop the pin too: a pinned row for a deleted file
- // would try to load something that no longer exists.
+ // Drop the pin too: a pinned row for a deleted file loads something gone.
if (pinnedKeys.includes(pinKey(repoId, v.quant))) {
togglePinnedQuant(repoId, v.quant);
}
- // Re-fetch this expander's variants so the deleted
- // quant stops showing as downloaded (and clickable to
- // reload) while the repo still has other cached quants.
+ // Re-fetch this expander's variants so the deleted quant stops showing as
+ // downloaded while other cached quants remain.
+ // repo still has other cached quants.
setRefreshKey((key) => key + 1);
},
}
@@ -2325,7 +2244,6 @@ function GgufVariantExpander({
);
}
-// ── Detect GGUF repos by naming convention or hub tag ────────────────────
function hasGgufSuffix(id: string): boolean {
return /-GGUF(?:$|-)/i.test(id);
@@ -2335,9 +2253,8 @@ function isGgufRepo(id: string, hintedIsGguf?: boolean): boolean {
return Boolean(hintedIsGguf) || hasGgufSuffix(id);
}
-// ── Task scoping: which pages own which pipeline tasks ───────────────────
-// True when a repo's inferred task is within the picker's task filter (or no filter). Unknown task (null) passes only with no filter.
+// True when a repo's inferred task is within the picker's task filter; unknown task (null) passes only with no filter.
function taskMatchesFilter(
repoTask: string | null | undefined,
filter: HfTaskFilter,
@@ -2347,42 +2264,43 @@ function taskMatchesFilter(
return repoTask != null && (wanted as readonly string[]).includes(repoTask);
}
-// Image-generation pipeline tasks: owned by the Images page, never chat-loadable. The backend reports "text-to-image" for diffusion-arch GGUFs, and the Images page reuses this as its picker `task` filter.
+// Image-generation pipeline tasks: owned by the Images page, never chat-loadable. The backend
+// reports "text-to-image" for diffusion-arch GGUFs.
export const IMAGE_GEN_TASKS = [
"text-to-image",
"image-to-image",
"image-text-to-image",
] as const;
-// Video-generation pipeline tasks: owned by the Video page, never chat-loadable. The backend reports "text-to-video" for video-diffusion GGUFs.
-// image-to-video is included because HF gives the LTX-2 family that pipeline_tag, so a text-to-video-only filter dropped it out of Video Hub search.
-// image-text-to-video is MiniMax-H3's tag: a frame plus a prompt in, video out. Without it the
-// Video picker never finds such a model and chat tries to load it as a language model.
+// Video-generation pipeline tasks: owned by the Video page, never chat-loadable. The backend
+// reports "text-to-video" for video-diffusion GGUFs; HF gives the LTX-2 family the image-to-video
+// pipeline_tag, and image-text-to-video is MiniMax-H3's. Without them the Video picker misses such
+// a model and chat loads it as a language model.
export const VIDEO_GEN_TASKS = [
"text-to-video",
"image-to-video",
"image-text-to-video",
] as const;
-/** The tasks whose GGUFs are placed by the DIFFUSION backend, which is the only reason a picker
- * scores against `classifyMediaGgufFit`. Audio is task-scoped too but does not belong here: its
- * GGUFs go to llama.cpp (TTS) or the whisper sidecars (stt_ggml_sidecar.py, stt_mtmd_sidecar.py),
- * so scoring them at 70% hid runnable models and picked a smaller quant than needed. */
+/** The tasks whose GGUFs are placed by the DIFFUSION backend, the only reason a picker scores
+ * against `classifyMediaGgufFit`. Audio is task-scoped but excluded: its GGUFs go to
+ * llama.cpp or the whisper sidecars, so scoring them at 70% hid runnable models. */
const DIFFUSION_TASKS: ReadonlySet<string> = new Set([
...IMAGE_GEN_TASKS,
...VIDEO_GEN_TASKS,
]);
-// Speech pipeline tasks: owned by the Audio page. TTS picks load there rather than into chat; ASR picks map to the dictation sidecar.
+// Speech pipeline tasks: owned by the Audio page. TTS picks load there; ASR picks map to the dictation sidecar.
export const AUDIO_GEN_TASKS = [
"text-to-speech",
"automatic-speech-recognition",
] as const;
-// Diffusion GGUF archs the Images backend cannot assemble yet (SD/SDXL/PixArt/Wan/...). The backend tags them with this task so the chat picker hides them and the Images picker leaves them out (they would 400 on load).
+// Diffusion GGUF archs the Images backend cannot assemble yet. The backend tags them with this
+// task so both pickers leave them out; they would 400 on load.
const UNSUPPORTED_DIFFUSION_TASK = "image-diffusion-unsupported";
-// Generation tasks the Images / Video / Audio pages own. Not chat-loadable, so an on-device pick routes to its page instead.
+// Generation tasks the Images / Video / Audio pages own. Not chat-loadable, so an on-device pick routes to its page.
const MEDIA_PAGE_TASKS: readonly string[] = [
...IMAGE_GEN_TASKS,
...VIDEO_GEN_TASKS,
@@ -2399,13 +2317,15 @@ function mediaPageForTask(
return "images";
}
-// Editing/inpaint checkpoints are tagged image-to-image but need an input image the text-to-image backend rejects (mirrors
-// its _EDIT_KEYWORDS), so they are hidden by id. The task itself must stay: FLUX.2-klein carries it too. "layered" hides Qwen-Image-Layered, which needs a dedicated pipeline.
+// Editing/inpaint checkpoints are tagged image-to-image but need an input image the
+// text-to-image backend rejects, so they are hidden by id (mirrors _EDIT_KEYWORDS). The
+// task itself must stay, since FLUX.2-klein carries it too.
const IMAGE_EDIT_KEYWORDS = ["edit", "kontext", "inpaint", "layered"] as const;
-// Editing families the backend now SUPPORTS (their own Edit workflow): not hidden despite the edit keyword. Mirrors the backend's qwen-image-edit family.
+// Editing families the backend now SUPPORTS: not hidden despite the edit keyword. Mirrors the
+// backend's qwen-image-edit family.
const SUPPORTED_EDIT_KEYWORDS = ["qwen-image-edit", "kontext"] as const;
-// Match a keyword as a whole path/name segment, not a raw substring, so "edit" does not hide ".../edited/..." and "kontext"
-// does not hide ".../kontextual/...". The keywords are [a-z-] literals, so no escaping. Mirrors _token_in_needle.
+// Match a keyword as a whole path/name segment, not a raw substring, so "edit" does not hide
+// ".../edited/...". Keywords are [a-z-] literals, so no escaping. Mirrors _token_in_needle.
function idHasSegment(id: string, keyword: string): boolean {
return new RegExp(`(?:^|[-_./\\\\])${keyword}(?:$|[-_./\\\\])`).test(id);
}
@@ -2416,7 +2336,8 @@ function isImageEditModel(repoId: string | null | undefined): boolean {
return IMAGE_EDIT_KEYWORDS.some((kw) => idHasSegment(id, kw));
}
-// Gate an on-device model by the picker's task scope: with a filter (Images) keep only matching, non-editing tasks; with none (chat) drop image-generation models.
+// Gate an on-device model by the picker's task scope: with a filter keep only matching,
+// non-editing tasks; with none drop image-generation models.
function passesTaskGate(
repoTask: string | null | undefined,
repoId: string | null | undefined,
@@ -2441,11 +2362,12 @@ function passesTaskGate(
!isImageEditModel(repoId)
);
}
- // Unfiltered (chat) picker: an on-device diffusion model stays listed and routes to the Images/Video page on click; only the never-loadable tag is hidden.
+ // Unfiltered (chat) picker: an on-device diffusion model stays listed and routes to its page
+ // on click; only the never-loadable tag is hidden.
return repoTask !== UNSUPPORTED_DIFFUSION_TASK;
}
-// Module-level caches so re-mounting the popover shows results instantly
+// Module-level caches so re-mounting the popover shows results instantly.
let _cachedGgufCache: CachedGgufRepo[] = [];
let _cachedModelsCache: CachedModelRepo[] = [];
let _lmStudioCache: LocalModelInfo[] = [];
@@ -2490,10 +2412,9 @@ function canDeleteLoraModel(model: LoraModelOption): boolean {
return (isTraining || isExported) && !isExportedGguf;
}
-// ── Hub Model Picker ──────────────────────────────────────────
-// Recommended section sort. "recommended" = newly created unsloth GGUF/MLX that
-// fit the device; the rest are plain HF sort keys over all unsloth models.
+// Recommended section sort: "recommended" = newly created unsloth GGUF/MLX that fit the
+// device; the rest are plain HF sort keys.
type RecommendedSortKey = "recommended" | "trendingScore" | "lastModified";
const RECOMMENDED_SORT_OPTIONS: HubOption<RecommendedSortKey>[] = [
@@ -2502,8 +2423,7 @@ const RECOMMENDED_SORT_OPTIONS: HubOption<RecommendedSortKey>[] = [
{ value: "lastModified", label: "Recent" },
];
-// Sort for the On Device lists. "recent" = last loaded;
-// "downloaded" = file download date.
+// Sort for the On Device lists: "recent" = last loaded, "downloaded" = file download date.
type LocalSortKey = "recent" | "downloaded" | "size" | "name";
const LOCAL_SORT_OPTIONS: HubOption<LocalSortKey>[] = [
@@ -2513,8 +2433,7 @@ const LOCAL_SORT_OPTIONS: HubOption<LocalSortKey>[] = [
{ value: "downloaded", label: "Downloaded" },
];
-// Format filter dropdown for the Unsloth listing. Plain labels are reused in
-// the empty-state copy below.
+// Format filter dropdown for the Unsloth listing; the plain labels are reused in the empty-state copy.
const FORMAT_FILTER_LABELS: Record<FormatFilter, string> = {
all: "All",
gguf: "GGUF",
@@ -2601,8 +2520,8 @@ function localModelIsGguf(m: LocalModelInfo): boolean {
function localPathTooltip(
name: string,
path: string,
- // Which checkpoint, since the path cannot say: an H3 repo holds its keyframe
- // and reference partitions in one directory.
+ // Which checkpoint, since the path cannot say: an H3 repo holds its keyframe and reference
+ // partitions in one directory.
detail?: string,
): ReactNode {
return (
@@ -2637,15 +2556,15 @@ function localDirectGgufMeta(
return localModelMeta(true, pipelineTag);
}
-/** Hugging Face address for an online/Hub row, or undefined when the repo id is
- * missing so the row shows no (empty) address line on hover. */
+/** Hugging Face address for an online/Hub row, or undefined when the repo id is missing so the
+ * row shows no empty address line. */
function hubRepoUrl(id: string | null | undefined): string | undefined {
const trimmed = id?.trim();
return trimmed ? `huggingface.co/${trimmed}` : undefined;
}
-/** Whether a local model is an MLX build (name hint). MLX runs on Mac only, so
- * callers gate visibility on the host being a Mac. */
+/** Whether a local model is an MLX build (name hint). MLX runs on Mac only, so callers gate
+ * visibility on the host being a Mac. */
function localModelIsMlx(m: LocalModelInfo): boolean {
return isMlxId(m.id) || isMlxId(m.display_name) || isMlxId(m.model_id ?? "");
}
@@ -2684,8 +2603,8 @@ export function HubModelPicker({
communityModelPolicy = "none",
}: {
models: ModelOption[];
- /** Task-runtime downloads that use a cache layout the shared Hub inventory
- * cannot represent (for example the two-file STT sidecars). */
+ /** Task-runtime downloads using a cache layout the shared Hub inventory cannot represent (for
+ * example the two-file STT sidecars). */
additionalOnDeviceModels?: ModelOption[];
loadedModelIdOverride?: string;
/** Fine-tuned models, shown as a section in the On Device view. */
@@ -2706,32 +2625,28 @@ export function HubModelPicker({
/** Section toggle rendered under the search bar. */
sectionToggle?: ReactNode;
onEject?: () => void;
- /** Restrict results to a pipeline task (e.g. text-to-image for the Images page). Undefined = all tasks (the chat default). */
+ /** Restrict results to a pipeline task; undefined = all tasks (the chat default). */
task?: HfTaskFilter;
- /** Curated catalog for a task-scoped picker: one canonical row per model, with its published formats as the second level. */
+ /** Curated catalog for a task-scoped picker: one canonical row per model, formats as the second level. */
catalog?: CatalogGroup[];
- /** Also surface community (non-unsloth) models carrying `task`'s pipeline tags, below
- * the unsloth rows and in search. Opt-in, since the runtime has to load an arbitrary
- * publisher's checkpoint: true of audio, not of the curated pages. */
+ /** Also surface community models carrying `task`'s pipeline tags, below the unsloth rows.
+ * Opt-in, since the runtime has to load an arbitrary publisher's checkpoint: true of audio. */
communityModelPolicy?: CommunityModelPolicy;
}) {
const gpu = useGpuInfo();
const inferenceGpu = useInferenceGpuInfo();
- // The saved VRAM Budget, threaded into every fit call in this component. Passing it to the quant
- // rows alone left the parent rows and the "Fits on device" filter scoring against the 0.97
- // default, so lowering the setting moved one and not the other.
+ // The saved VRAM Budget, threaded into every fit call here. Passing it to the quant rows alone
+ // left the parent rows and the "Fits on device" filter on the 0.97 default.
const budgetFraction = useVramBudgetFraction() ?? undefined;
// Whether THIS picker's rows load through the diffusion backend. Not `Boolean(task)`: Audio is
- // task-scoped for the single-device budget but runs its GGUFs under llama.cpp / whisper.
+ // task-scoped but runs its GGUFs under llama.cpp / whisper.
const diffusionLoad = useMemo(() => {
const tasks = task ? (typeof task === "string" ? [task] : task) : [];
return tasks.some((entry) => DIFFUSION_TASKS.has(entry));
}, [task]);
- // What the backend actually holds, not the dropdown highlight, which can be a
- // staged pick. The selection alone was wrong: an image or video load evicts
- // the chat model and leaves the pick untouched, so its rows kept the "Loaded"
- // badge with nothing resident. Same predicate as the header tick, so the two
- // cannot disagree.
+ // What the backend actually holds, not the dropdown highlight: an image or video load evicts
+ // the chat model and leaves the pick untouched, so rows kept a "Loaded" badge with nothing
+ // resident. Same predicate as the header tick.
const selectedCheckpoint = useChatRuntimeStore((s) => s.params.checkpoint);
const residentCheckpoint = useChatRuntimeStore((s) => s.residentCheckpoint);
const chatLoadedModelId = chatModelLoaded({
@@ -2746,20 +2661,17 @@ export function HubModelPicker({
const activeGgufVariant = useChatRuntimeStore((s) => s.activeGgufVariant);
// Last-loaded timestamps power the "Recent" sort (vs "Downloaded" = file date).
const loadTimes = useModelLoadTimes(value);
- // Fade the list's top edge once scrolled, and its bottom edge while more
- // rows sit below the fold.
+ // Fade the list's top edge once scrolled, and its bottom edge while more rows sit below the fold.
const [listScrolled, setListScrolled] = useState(false);
const [listMoreBelow, setListMoreBelow] = useState(false);
const hfToken = useHfTokenStore((s) => s.token);
const [query, setQuery] = useState("");
const debouncedQuery = useDebouncedValue(query);
- // Shared Hub search stack (the same hooks the Hub page uses) so the picker
- // and Hub run one implementation. Scoped to unsloth like the old listing.
+ // Shared Hub search stack so the picker and Hub run one implementation. Scoped to unsloth like the old listing.
const online = useOnlineStatus();
// Sanitize to anonymous on a malformed token, matching the Hub page.
const accessToken = hfApiToken(hfToken);
- // Recommended section: a live unsloth listing sorted by the dropdown. The
- // same sort drives the search results so the dropdown works while searching.
+ // Recommended section: a live unsloth listing sorted by the dropdown, the same sort that drives search results.
const [recommendedSort, setRecommendedSort] =
useState<RecommendedSortKey>("trendingScore");
// "recommended" surfaces the most recently created Unsloth repos.
@@ -2779,8 +2691,8 @@ export function HubModelPicker({
pinUnslothFirst: true,
keepUnsupportedTags: true,
accessToken,
- // Only Recommended renders Hub results, so keep the Hub hooks idle on other
- // tabs to avoid needless requests and preserve offline-local behavior.
+ // Only Recommended renders Hub results, so keep the Hub hooks idle on other tabs and preserve
+ // offline-local behavior.
enabled: online && section === "recommended",
});
const recommendedSearch = useHubModelSearch("", {
@@ -2793,9 +2705,9 @@ export function HubModelPicker({
enabled: online && section === "recommended",
});
- // Two hooks for the same reason the unsloth pair exists: browse must not refetch
- // per keystroke, search must not be pinned to the empty query. pinUnslothFirst is
- // off since the unsloth rows already sit above these.
+ // Two hooks for the same reason the unsloth pair exists: browse must not refetch per
+ // keystroke, search must not be pinned to the empty query. pinUnslothFirst is off, since
+ // the unsloth rows already sit above.
const communityDiscoveryEnabled =
shouldDiscoverCommunityModels(communityModelPolicy) &&
Boolean(task) &&
@@ -2823,9 +2735,8 @@ export function HubModelPicker({
enabled: communityRecommendedEnabled && debouncedQuery.trim().length === 0,
});
- // Lowercased repo ids confirmed GGUF by the store or HF search. Absence means
- // "no hint" -> hasGgufSuffix is the fallback (don't conflate unknown with
- // known-not-GGUF). Lowercased so store and HF IDs match regardless of casing.
+ // Lowercased repo ids confirmed GGUF by the store or HF search. Absence means "no hint", so
+ // hasGgufSuffix is the fallback rather than conflating unknown with known-not-GGUF.
const modelGgufIds = useMemo(() => {
const ids = new Set<string>();
for (const model of models) {
@@ -2833,8 +2744,7 @@ export function HubModelPicker({
}
return ids;
}, [models]);
- // Both listings contribute GGUF hints so a tag-only GGUF (no "-GGUF" suffix)
- // in Recommended still expands variants instead of loading as a checkpoint.
+ // Both listings contribute GGUF hints so a tag-only GGUF still expands variants instead of loading as a checkpoint.
const resultGgufIds = useMemo(() => {
const ids = new Set<string>();
for (const result of [
@@ -2860,10 +2770,9 @@ export function HubModelPicker({
[modelGgufIds, resultGgufIds],
);
- // Track which GGUF repo is expanded for variant selection
const [expandedGguf, setExpandedGguf] = useState<string | null>(null);
- // GGUF vision support per repo, reported by the expander once it has read the
- // metadata, so On Device rows can show a Vision badge on the name.
+ // GGUF vision support per repo, reported by the expander once it has read the metadata, so On
+ // Device rows can badge the name.
const [visionByRepo, setVisionByRepo] = useState<Record<string, boolean>>({});
const reportVision = useCallback((repoId: string, hasVision: boolean) => {
setVisionByRepo((prev) =>
@@ -2872,17 +2781,15 @@ export function HubModelPicker({
}, []);
// When on, On Device GGUF repos show their quantizations without a click.
const expandQuantizations = useChatRuntimeStore((s) => s.expandQuantizations);
- // Off: On Device lists only downloaded quants, so a repo holding one collapses
- // into a single row instead of hiding it behind an expander.
+ // Off: On Device lists only downloaded quants, so a repo holding one collapses into a single row.
const showAllQuantizations = useChatRuntimeStore(
(s) => s.showAllQuantizations,
);
// Shared with the Hub page: list only models sized within the device budget.
const fitOnDeviceOnly = useChatRuntimeStore((s) => s.fitOnDeviceOnly);
const setFitOnDeviceOnly = useChatRuntimeStore((s) => s.setFitOnDeviceOnly);
- // Repos the user clicked to collapse while expand-by-default is on, and the
- // ones they clicked back open. Kept in memory only, so both reset on reload
- // (and when the setting is toggled).
+ // Repos the user clicked to collapse while expand-by-default is on, and the ones clicked back
+ // open. In memory only, so both reset on reload.
const [collapsedGgufState, setCollapsedGgufState] = useState<{
expandQuantizations: boolean;
value: Set<string>;
@@ -2901,12 +2808,11 @@ export function HubModelPicker({
expandQuantizations ? !collapsedGguf.has(id) : expandedGguf === id,
[expandQuantizations, collapsedGguf, expandedGguf],
);
- // Toggle a repo's quantizations: flip the collapse set when expand-by-default
- // is on, otherwise drive the single-open expandedGguf state.
+ // Toggle a repo's quantizations: flip the collapse set when expand-by-default is on, else
+ // drive the single-open expandedGguf state.
const toggleGgufExpanded = useCallback(
- // `showing` is what the row actually renders, which is not the collapse
- // set alone: a row held back by its sole-quant probe shows nothing, and a
- // click on it should open it rather than collapse what is already hidden.
+ // `showing` is what the row actually renders, which is not the collapse set alone: a row held
+ // back by its sole-quant probe shows nothing, and a click should open it.
(id: string, showing = isGgufExpanded(id)) => {
if (!expandQuantizations) {
setExpandedGguf((prev) => (prev === id ? null : id));
@@ -2938,13 +2844,11 @@ export function HubModelPicker({
const [fineTunedCollapsed, setFineTunedCollapsed] = useState(false);
const [lmStudioCollapsed, setLmStudioCollapsed] = useState(false);
const [localDirCollapsed, setLocalDirCollapsed] = useState(false);
- // The Fine-tuned section header; the train icon on the Unsloth header scrolls
- // here so users can jump to their trained models.
+ // The Fine-tuned section header; the train icon on the Unsloth header scrolls here.
const fineTunedSectionRef = useRef<HTMLDivElement>(null);
const scrollToFineTuned = useCallback(() => {
setFineTunedCollapsed(false);
- // Two frames so the expand renders before we scroll the section to the top
- // of the list.
+ // Two frames so the expand renders before the section is scrolled to the top.
requestAnimationFrame(() => {
requestAnimationFrame(() => {
fineTunedSectionRef.current?.scrollIntoView({
@@ -2954,8 +2858,7 @@ export function HubModelPicker({
});
});
}, []);
- // The Other models header; the directions icon on the Unsloth header scrolls
- // here.
+ // The Other models header; the directions icon on the Unsloth header scrolls here.
const otherModelsSectionRef = useRef<HTMLDivElement>(null);
const scrollToOtherModels = useCallback(() => {
setOtherModelsCollapsed(false);
@@ -2968,8 +2871,7 @@ export function HubModelPicker({
});
});
}, []);
- // The Custom Folders header; the folder icon on the Unsloth header scrolls
- // here instead of opening the browse popup.
+ // The Custom Folders header; the folder icon scrolls here instead of opening the browse popup.
const customFolderSectionRef = useRef<HTMLDivElement>(null);
const scrollToCustomFolders = useCallback(() => {
setCustomFoldersCollapsed(false);
@@ -2983,9 +2885,8 @@ export function HubModelPicker({
});
}, []);
- // `models` is already narrowed to this Audio mode and platform (notably the
- // macOS TTS runtime). Use only exact artifacts from that active contract for
- // downloaded-task and hidden-chat-sidecar exceptions.
+ // `models` is already narrowed to this Audio mode and platform, so use only exact artifacts
+ // from that contract for the downloaded-task and hidden-sidecar exceptions.
const activeCatalogArtifactIds = useMemo(
() =>
new Set(
@@ -3020,9 +2921,8 @@ export function HubModelPicker({
() => pickerInventory.localModels.filter((m) => m.source === "models_dir"),
[pickerInventory.localModels],
);
- // Ollama rows list alongside custom folders: both are user-managed stores
- // outside ./models, and an Ollama root added as a custom folder is where
- // the missing rows were expected (#9226).
+ // Ollama rows list alongside custom folders: both are user-managed stores outside ./models,
+ // and an Ollama root added as a custom folder is where the rows were expected (#9226).
const customFolderModels = useMemo(
() =>
pickerInventory.localModels.filter(
@@ -3066,7 +2966,6 @@ export function HubModelPicker({
setUpdateConflictKey(null);
}, [updateConflictKey]);
- // Custom scan folders management
const [scanFolders, setScanFolders] =
useState<ScanFolderInfo[]>(_scanFoldersCache);
const [folderInput, setFolderInput] = useState("");
@@ -3091,15 +2990,14 @@ export function HubModelPicker({
const handleAddFolder = useCallback(
async (overridePath?: string) => {
- // Explicit path lets the folder browser submit in the same tick it
- // calls `setFolderInput`; reading `folderInput` would race the update.
+ // An explicit path lets the folder browser submit in the same tick it calls `setFolderInput`;
+ // reading `folderInput` would race the update.
const raw = overridePath !== undefined ? overridePath : folderInput;
const trimmed = raw.trim();
if (!trimmed || folderLoading) return;
setFolderError(null);
setFolderLoading(true);
- // From the folder browser's "Use this folder": the typed-input panel is
- // closed, so surface failures (denylisted path, sandbox 403) via toast.
+ // From the folder browser's "Use this folder": the typed-input panel is closed, so surface failures via toast.
const fromBrowser = overridePath !== undefined;
try {
const created = await addScanFolder(trimmed);
@@ -3115,7 +3013,6 @@ export function HubModelPicker({
setShowFolderInput(false);
refreshLocalModelsList();
onFoldersChange?.();
- // Background reconciliation with the server
void refreshScanFolders();
} catch (e) {
const message = e instanceof Error ? e.message : "Failed to add folder";
@@ -3159,8 +3056,8 @@ export function HubModelPicker({
void pickerInventory.refreshInventory();
}, [pickerInventory.refreshInventory]);
- // Updates run as managed downloads (Downloads panel: progress + Cancel), not a blocking
- // call. The worker pulls only changed blobs, so the cached copy stays usable until done.
+ // Updates run as managed downloads, not a blocking call; the worker pulls only changed blobs,
+ // so the cached copy stays usable.
const startManagedUpdate = useCallback(
(repoId: string, variant: string, expectedBytes: number) => {
return downloadManager
@@ -3174,9 +3071,8 @@ export function HubModelPicker({
if (outcome === "conflict") {
setUpdateConflictKey(jobKeyOf("model", repoId, variant));
} else if (outcome === "busy") {
- // A sibling variant/snapshot for this repo is already downloading,
- // so this update did not start. Say so instead of closing the
- // dialog as if it began and leaving the cached copy stale.
+ // A sibling variant/snapshot for this repo is already downloading, so this update did not
+ // start; say so instead of leaving the cached copy stale.
toast.info("A download for this model is already in progress", {
description: "Try updating again once it finishes.",
});
@@ -3251,7 +3147,8 @@ export function HubModelPicker({
const isMac = deviceType === "mac";
const hostClass = useHostClass();
- // Drop models Unsloth cannot run for chat. A task-scoped picker wants exactly the tasks the chat classifier calls unsupported, so it gates on the task.
+ // Drop models Unsloth cannot run for chat. A task-scoped picker wants exactly the tasks the
+ // chat classifier calls unsupported, so it gates on the task.
const isChatSupported = useCallback(
(r: HfModelResult) => {
// Image/Video tab (task set): only task-matching, non-editing results.
@@ -3321,18 +3218,17 @@ export function HubModelPicker({
allowedHiddenModelIdMatches(taskCatalogSeedIds, id),
)
.filter((id) => !downloadedSet.has(id.toLowerCase()))
- // Task-scoped pages load single-file GGUF only; chat-only keeps runnable formats (GGUF anywhere, plus MLX/safetensors on Mac).
- // A curated artifact stays listed whatever its format: loadSpecFor knows how to load each, and a GGUF-only rule hid every non-GGUF curated model.
+ // Task-scoped pages load single-file GGUF only; chat-only keeps runnable formats. A curated
+ // artifact stays listed whatever its format, since loadSpecFor knows how to load each.
.filter((id) =>
task
? isKnownGgufRepo(id) ||
Boolean(catalog && artifactForRepoId(id, catalog))
: !chatOnly || isRecommendableFormat(id, isKnownGgufRepo(id), isMac),
)
- // Member repos of a catalog group would collapse into the canonical group row, but nothing renders those rows yet and a
- // task-scoped picker's `models` is exactly group members, so suppressing them emptied Recommended. Keep them until the grouped UI lands.
+ // Member repos would collapse into the canonical group row, but nothing renders those rows yet
+ // and a task-scoped picker's `models` is exactly group members, so hiding them emptied it.
.filter((id) => !/-FP8[-.]|FP8-Dynamic/i.test(id));
- // Sort: GGUFs first, then hub models
const gguf: string[] = [];
const hub: string[] = [];
for (const id of all) {
@@ -3359,8 +3255,8 @@ export function HubModelPicker({
const [customSort, setCustomSort] = useState<LocalSortKey>("recent");
// Format filter toggle for the Unsloth listing.
const [formatFilter, setFormatFilter] = useState<FormatFilter>("all");
- // What this picker's task filter has already established about every row it can show. The Images
- // and Video pages pass their generation tasks; chat passes none and keeps the full set.
+ // What this picker's task filter has already established about every row it can show; chat
+ // passes none and keeps the full set.
const capabilityScope = useMemo<readonly (keyof ModelCapabilities)[] | null>(() => {
const tasks: readonly string[] = task
? typeof task === "string"
@@ -3379,8 +3275,7 @@ export function HubModelPicker({
const hubRowsShowSize =
formatFilter === "mlx" || formatFilter === "safetensors";
- // A curated row's name and its chips. Ids outside the catalog have neither, so they show the
- // raw repo id exactly as before.
+ // A curated row's name and its chips; ids outside the catalog have neither and show the raw repo id.
const curatedRow = useCallback(
(id: string) =>
(catalog && curatedRowLabelFor(id, catalog, hostClass)) ?? {
@@ -3390,13 +3285,11 @@ export function HubModelPicker({
[catalog, hostClass],
);
- /** Whether this host can run a curated id at all, as opposed to whether it has room for it.
- * Browse rows only: an id already on disk keeps its row wherever it came from. */
+ /** Whether this host can run a curated id at all, as opposed to whether it has room for it. Browse rows only. */
const curatedOfferable = useCallback(
(id: string) => {
if (!catalog) return true;
- // Downloaded weights keep their row. They may have been pulled on a machine that could run
- // them, and hiding what is already on disk reads as Unsloth having lost the model.
+ // Downloaded weights keep their row: hiding what is already on disk reads as Unsloth having lost the model.
if (downloadedSet.has(id.toLowerCase())) return true;
const hit = artifactForRepoId(id, catalog);
return hit ? curatedArtifactIsOfferable(hit.artifact.repoId, hostClass) : true;
@@ -3404,8 +3297,8 @@ export function HubModelPicker({
[catalog, downloadedSet, hostClass],
);
- // Paint curated rows before any request, so a task-scoped picker whose models
- // are already in memory does not sit on a spinner for a round trip.
+ // Paint curated rows before any request, so a task-scoped picker whose models are already in
+ // memory does not sit on a spinner.
const catalogSeedRows = useMemo<HfModelResult[]>(() => {
if (!task) return [];
return dedupe(models.map((model) => model.id))
@@ -3424,19 +3317,17 @@ export function HubModelPicker({
downloads: 0,
likes: 0,
isGguf: isKnownGgufRepo(id),
- // Size from the catalog, not an id "<n>B" guess: the guess is missing for
- // most curated ids and wrong for others (Wan2.2-TI2V-5B is 30 GB, not 2),
- // and non-unsloth ids never get a listing row to correct it.
+ // Size from the catalog, not an id "<n>B" guess: the guess is missing for most curated ids
+ // and wrong for others (Wan2.2-TI2V-5B is 30 GB, not 2).
curatedSizeBytes: catalog ? curatedSizeBytesFor(id, catalog) : undefined,
- // Same reason the size is curated: a seed the listing does not return has no
- // other source for its param chip, and most curated ids carry no "<n>B" token.
+ // Same reason the size is curated: a seed the listing does not return has no other source for its param chip.
totalParams: catalog ? curatedTotalParamsFor(id, catalog) : undefined,
}));
}, [catalog, models, formatFilter, isKnownGgufRepo, task, curatedOfferable]);
- /** The catalog's own fit verdict for a curated artifact, or undefined where it has none.
- * Every list that judges a row against the device goes through this, so a badge and the
- * filters around it cannot end up telling the user two different things. */
+ /** The catalog's own fit verdict for a curated artifact, or undefined where it has none. Every
+ * list that judges a row against the device goes through this, so a badge and its filters
+ * cannot disagree. */
const catalogFit = useCallback(
(id: string, budget: DeviceBudget) =>
catalog ? curatedArtifactFitsDevice(id, catalog, budget) : undefined,
@@ -3448,9 +3339,8 @@ export function HubModelPicker({
[],
);
- /** Pipeline tag is the Hub's only signal, since the real test is the checkpoint's
- * tokenizer and that needs the download first. Exports in another serialization are
- * never loadable, so drop those by name (as FP8 does above). */
+ /** Pipeline tag is the Hub's only signal, since the real test is the checkpoint's tokenizer and
+ * that needs the download first. Exports in another serialization are dropped by name. */
const isLoadableCommunityRepo = useCallback(
(id: string) =>
!/(^|[-_/.])(onnx|openvino|tflite|coreml)([-_./]|$)/i.test(id),
@@ -3462,9 +3352,8 @@ export function HubModelPicker({
[catalogSeedRows],
);
- // Recommended suggests GGUF anywhere; on Mac also MLX and safetensors. The
- // "recommended" sort also drops models too big for the device. Already-
- // downloaded models stay visible (badged), never hidden.
+ // Recommended suggests GGUF anywhere, plus MLX and safetensors on Mac; the "recommended" sort
+ // also drops models too big for the device. Downloaded models stay visible.
const recommendedRows = useMemo(() => {
const catalogSeedIds = new Set(
catalogSeedRows.map((row) => row.id.toLowerCase()),
@@ -3490,13 +3379,12 @@ export function HubModelPicker({
(!task ||
r.isGguf ||
Boolean(catalog && artifactForRepoId(r.id, catalog)));
- // A community row has no catalog artifact by definition, so the curated clause
- // above would drop every third-party safetensors checkpoint. The rest applies.
+ // A community row has no catalog artifact by definition, so the curated clause above would
+ // drop every third-party safetensors checkpoint.
const keepCommunity = (r: HfModelResult) =>
keepCommon(r) && isTaskRuntimeSupported(r);
- // Members are not filtered here (see recommendedIds): it dropped them from
- // Hub search too. "recommended" always device-filters; the "Fits on device"
- // tick extends that to the other sorts.
+ // Members are not filtered here (see recommendedIds): that dropped them from Hub search too.
+ // "recommended" always device-filters; the "Fits on device" tick extends it to other sorts.
const deviceFiltered = recommendedSort === "recommended" || fitOnDeviceOnly;
const taskScoped = Boolean(task);
const rowGpu = loadScopedGpu(gpu, taskScoped);
@@ -3505,14 +3393,13 @@ export function HubModelPicker({
const fits = (r: HfModelResult) =>
// Downloaded models show regardless of fit.
downloadedSet.has(r.id.toLowerCase()) ||
- // The catalog's own verdict where it has one, so this list and the OOM badge on its rows
- // cannot disagree: hfModelFitsDevice counts RAM toward a load that never leaves the card.
+ // The catalog's own verdict where it has one, so this list and the OOM badge cannot disagree:
+ // hfModelFitsDevice counts RAM toward a load that never leaves the card.
(catalogFit(r.id, pipelineBudget) ??
hfModelFitsDevice(r, diffusionLoad || !r.isGguf ? rowGpu : rowInferenceGpu, {
budgetFraction,
- // Not `&& r.isGguf`: on a task page a safetensors row is placed by the same backend, and
- // this rule IS the budget those rows had before the classifiers were merged. Restricting
- // it to GGUF left this gate disagreeing with searchRowFitsDevice about the same row.
+ // Not `&& r.isGguf`: on a task page a safetensors row is placed by the same backend, and this
+ // rule IS the budget those rows had before the classifiers were merged.
mediaLoad: diffusionLoad,
hostPooledMemory: gpu.loadDeviceSharesHostMemory,
// The scoped inventory's own count, so it always describes rowInferenceGpu's capacity.
@@ -3526,8 +3413,8 @@ export function HubModelPicker({
fits,
});
if (!communityRecommendedEnabled) return unslothRows;
- // Appended below everything unsloth publishes, so scrolling past the unsloth
- // uploads continues into the wider Hub. Same keep/fits gates.
+ // Appended below everything unsloth publishes, so scrolling past the unsloth uploads continues
+ // into the wider Hub. Same keep/fits gates.
const above = new Set(unslothRows.map((r) => r.id.toLowerCase()));
const communityRows = communityBrowse.results
.filter((r) => !r.id.toLowerCase().startsWith("unsloth/"))
@@ -3559,9 +3446,8 @@ export function HubModelPicker({
isLoadableCommunityRepo,
]);
- // Per-row meta + VRAM badge from the recommended listing's own metadata, with the
- // curated seeds behind it: a listing row wins wherever there is one, and a curated
- // row the listing never returns still gets its size chip instead of rendering bare.
+ // Per-row meta and VRAM badge from the recommended listing's own metadata, with the curated
+ // seeds behind it: a listing row wins, and a curated row it never returns keeps its chip.
const recommendedMeta = useMemo(() => {
const map = new Map<
string,
@@ -3573,12 +3459,8 @@ export function HubModelPicker({
}
>();
/** Size-based verdict for a row whose real footprint we know, against the budget that row
- * actually loads into. Same classifier as the quant rows below it, so the badge and the
- * "Fits on device" gate cannot disagree about one row.
- *
- * Returns the verdict, not a boolean. A boolean collapsed `marginal` and `partial` into "no
- * badge", so a repo whose SMALLEST quant already needs offload rendered as a clean fit beside
- * expanded rows saying the opposite. */
+ * actually loads into. Returns the verdict, not a boolean: a boolean collapsed `marginal`
+ * and `partial` into "no badge", so a repo needing offload rendered as a clean fit. */
const ggufRowFit = (
sizeBytes: number | undefined,
budget: typeof inferenceGpu,
@@ -3589,9 +3471,8 @@ export function HubModelPicker({
if (sizeBytes == null) return null;
// Probed and genuinely zero (a Vulkan device reporting nothing) means nothing fits.
if (!anyBudget) return "oom";
- // Images / Video place this GGUF through the diffusion backend, so it takes the same rule
- // its quant rows take. Judging the parent by llama.cpp's budget and the children by the
- // media one let a row read as fitting while everything inside it read as oom.
+ // Images / Video place this GGUF through the diffusion backend, so it takes the same rule its
+ // quant rows take; different budgets let a row read as fitting while its children read oom.
const fit = diffusionLoad
? classifyMediaGgufFit(
sizeBytes,
@@ -3612,20 +3493,17 @@ export function HubModelPicker({
? "exceeds"
: fit;
};
- // A curated pipeline loads through torch, and a task load puts the whole thing on ONE
- // device, so it is judged there. inferenceGpu is the GGUF backend's inventory, which can
- // be a different install (Vulkan llama.cpp) or the sum of several cards.
+ // A curated pipeline loads through torch and a task load puts the whole thing on ONE device,
+ // so it is judged there. inferenceGpu is the GGUF backend's inventory, a different install.
const rowGpu = loadScopedGpu(gpu, Boolean(task));
const pipelineBudget = artifactBudget(rowGpu);
- // The inventory of the runtime that PLACES the row, not of its file format. An Images or
- // Video GGUF goes to the diffusion backend, which is torch, and on a Vulkan chat build
- // inferenceGpu is a different device set entirely: it can see a card torch cannot, so the
- // media rule was scoring against capacity the diffusion loader never gets to use.
+ // The inventory of the runtime that PLACES the row, not of its file format: on a Vulkan chat
+ // build inferenceGpu can see a card torch cannot, so the media rule scored against capacity
+ // the diffusion loader never gets.
const rowInferenceGpu = diffusionLoad
? rowGpu
: loadScopedGpu(inferenceGpu, Boolean(task));
- // Community rows come from their own listing; without them folded in here
- // they render with no size or VRAM chip.
+ // Community rows come from their own listing; without them folded in here they render with no size or VRAM chip.
for (const r of [
...recommendedSearch.results,
...catalogSeedRows,
@@ -3633,8 +3511,8 @@ export function HubModelPicker({
]) {
if (map.has(r.id)) continue;
const isG = isKnownGgufRepo(r.id);
- // GGUF param count comes from the repo name or the GGUF metadata, so even
- // repos with no "<n>B" token (Kimi, MiniMax) show a param chip.
+ // GGUF param count comes from the repo name or the GGUF metadata, so even repos with no "<n>B"
+ // token show a param chip.
const ggufParams = r.totalParams ?? paramsFromId(r.id);
const meta = isG
? [
@@ -3655,24 +3533,21 @@ export function HubModelPicker({
.filter(Boolean)
.join(" · ") || null;
if (isG) {
- // GGUF fit is size-based: flag OOM when even the smallest quant we can
- // size exceeds the device budget. Repos we cannot size show no badge.
+ // GGUF fit is size-based: flag OOM when even the smallest quant we can size exceeds the
+ // budget. Repos we cannot size show no badge.
const params = ggufParams;
const sizeBytes =
r.estimatedSizeBytes ??
(params ? estimateQuantBytes(params) : undefined);
map.set(r.id, {
meta,
- // The classifier's own verdict, so a GGUF row never borrows "exceeds", which is the
- // torch-only refusal used by the curated branch below.
- // The device the load LANDS on, same as the quant rows under it and the fit filter
- // beside it. The curated branch below already scoped; this one did not, so a downloaded
- // media parent could read as safe while every variant inside it read as oom.
+ // The classifier's own verdict, so a GGUF row never borrows the torch-only "exceeds", scoped
+ // to the device the load LANDS on: unscoped, a downloaded media parent read as safe while
+ // every variant inside it read as oom.
status: ggufRowFit(sizeBytes, rowInferenceGpu),
- // The figure the verdict was reached with, not the raw file size. classifyGgufFit scores
- // weights PLUS activations and KV, so a 20 GiB quant needing 24 GiB read "tight fit"
- // beside a tooltip saying "~20GB VRAM". The media rule scores the raw size, so that one
- // keeps it.
+ // The figure the verdict was reached with, not the raw file size: classifyGgufFit scores
+ // weights plus activations and KV, so a 20 GiB quant needing 24 GiB read "tight fit" beside
+ // "~20GB VRAM". The media rule scores the raw size and keeps it.
est: sizeBytes
? Math.round(
diffusionLoad
@@ -3683,9 +3558,9 @@ export function HubModelPicker({
});
continue;
}
- // A curated pipeline is judged by the catalog, which knows its resident size and any
- // measured offload tier. The QLoRA estimator below reads a diffusion pipeline as a
- // language model it can 4-bit quantize: Wan 2.2 TI2V is 30 GB, and 5B params says 5.9.
+ // A curated pipeline is judged by the catalog, which knows its resident size; the QLoRA
+ // estimator reads a diffusion pipeline as a language model it can 4-bit quantize (Wan 2.2
+ // TI2V is 30 GB, where 5B params says 5.9).
const curatedFits = catalogFit(r.id, pipelineBudget);
if (curatedFits !== undefined) {
const curatedBytes = catalog
@@ -3720,8 +3595,7 @@ export function HubModelPicker({
inferenceGpu,
]);
- // Hub pipeline tag per repo id, handed to the page on pick so a task page can
- // classify an uncurated repo it has no catalog entry for.
+ // Hub pipeline tag per repo id, handed to the page on pick so a task page can classify an uncurated repo.
const pipelineTagById = useMemo(() => {
const map = new Map<string, string>();
for (const r of [
@@ -3740,9 +3614,8 @@ export function HubModelPicker({
communityBrowse.results,
]);
- // The rest of the Hub evidence the Audio page judges a community row on. Keyed the
- // same way, so a chat pick is routed on what the page would have listed it on rather
- // than on its repo name alone.
+ // The rest of the Hub evidence the Audio page judges a community row on, keyed the same way so
+ // a chat pick is routed on what the page would have listed it on.
const hubEvidenceById = useMemo(() => {
const map = new Map<
string,
@@ -3766,10 +3639,8 @@ export function HubModelPicker({
libraryName: r.libraryName,
});
}
- // Downloaded rows too. The backend tags a cached Whisper checkpoint as ASR even when
- // its repo name says nothing, and the Audio page lists it on those tags; without them
- // here the same row picked from the unscoped Chat picker was judged on its id alone
- // and refused routing to the page that does list it.
+ // Downloaded rows too: the backend tags a cached Whisper checkpoint as ASR even when its repo
+ // name says nothing, and without those tags the same row was judged on its id alone.
for (const c of cachedModels) {
const existing = map.get(c.repo_id);
if (existing) {
@@ -3807,10 +3678,8 @@ export function HubModelPicker({
cachedGguf,
]);
- // Tag-accurate capabilities keyed by repo id, pooled from both HF listings, then the
- // catalog for curated ids neither listing returned. Rows look it up by id and fall
- // back to repo-name detection when absent, which cannot see an audio track a name
- // does not mention. Listings first: real tags outrank curated data.
+ // Tag-accurate capabilities keyed by repo id, pooled from both HF listings then the catalog
+ // for curated ids neither returned. Listings first: real tags outrank curated data.
const capsById = useMemo(() => {
const map = new Map<string, ModelCapabilities>();
for (const r of [
@@ -3834,10 +3703,8 @@ export function HubModelPicker({
const curated = curatedCapabilitiesFor(row.id, catalog);
if (!curated) continue;
const detected = map.get(row.id);
- // Merged, not skipped when the listing already answered. A curated entry states what the
- // model does (H3's audio track, which no tag on the repo mentions); the listing only ever
- // adds to that. Taking whichever arrived first dropped the declaration on any row the
- // listing happened to return.
+ // Merged, not skipped when the listing already answered: a curated entry states what the model
+ // does (H3's audio track, which no repo tag mentions), and first-wins dropped that.
map.set(
row.id,
detected
@@ -3862,7 +3729,7 @@ export function HubModelPicker({
catalogSeedRows,
]);
- // Ordered by the On Device dropdown (recent/download date/size/name). The gate keeps diffusion GGUFs in the Images/Video picker and out of chat.
+ // Ordered by the On Device dropdown. The gate keeps diffusion GGUFs in the Images/Video picker and out of chat.
const sortedCachedGguf = useMemo(
() =>
sortCachedRepos(
@@ -3875,16 +3742,16 @@ export function HubModelPicker({
catalog,
activeCatalogArtifactIds,
) &&
- // A speech GGUF no backend here can decode (CSM) would otherwise be listed as
- // a chat model and only fail in llama-server. Non-audio rows always pass.
+ // A speech GGUF no backend here can decode (CSM) would otherwise be listed as a chat model and
+ // fail only in llama-server. Non-audio rows always pass.
audioPickIsRoutable({
id: c.repo_id,
task: c.task,
audioType: c.audio_type,
isGguf: true,
isCurated: artifactForRepoId(c.repo_id, AUDIO_CATALOG) !== null,
- // The task and codec both came from GGUF classification; codec provenance
- // separates runnable Orpheus from unsupported CSM.
+ // The task and codec both came from GGUF classification; codec provenance separates runnable
+ // Orpheus from unsupported CSM.
taskFromGgufArch: true,
}),
),
@@ -3900,7 +3767,8 @@ export function HubModelPicker({
activeCatalogArtifactIds,
],
);
- // Cached non-GGUF repos. In chat, passesTaskGate drops diffusers image repos; the Images picker keeps them, but only unsloth-hosted ones this backend can load. Base repos are cached as dependencies and fail the trust gate.
+ // Cached non-GGUF repos. In chat, passesTaskGate drops diffusers image repos; the Images
+ // picker keeps only unsloth-hosted ones this backend can load.
const sortedCachedModels = useMemo(
() =>
sortCachedRepos(
@@ -3917,8 +3785,10 @@ export function HubModelPicker({
catalog,
activeCatalogArtifactIds,
) &&
- // Diffusion pickers: unsloth repos plus any repo the backend can LOAD. Gate on a curated ARTIFACT (what loadSpecFor resolves), not a group-key match: a base / uncurated-quant sibling matches the group by key but dead-ends at the trust gate.
- // An unsloth repo must also be a full pipeline: the fall-through loads uncataloged rows as "pipeline", and from_pretrained on a single-file checkpoint repo fails. Curated single-file artifacts stay, since loadSpecFor carries their filename.
+ // Diffusion pickers: unsloth repos plus any repo the backend can LOAD. Gate on a curated
+ // ARTIFACT, not a group-key match: a base sibling matches by key but dead-ends at the trust
+ // gate. An unsloth repo must also be a full pipeline, since from_pretrained fails on a
+ // single-file checkpoint repo.
(!task ||
(isUnslothRepoId(c.repo_id) && !c.single_file) ||
((c.task === "automatic-speech-recognition" ||
@@ -3961,20 +3831,20 @@ export function HubModelPicker({
isMac,
],
);
- // Task-scoped loads put the whole pipeline on ONE device, so quant fit uses the device the load lands on (the lowest visible ordinal), not the multi-GPU sum or the largest card: sizing against the bigger card OOMs the smaller one. Chat keeps the sum.
- // The source is picked per row (a GGUF row sizes against the inference GPU, anything else against the system view); this only decides how much of it a row may claim.
+ // Task-scoped loads put the whole pipeline on ONE device, so quant fit uses the device the
+ // load lands on (the lowest visible ordinal), not the multi-GPU sum: sizing against the
+ // bigger card OOMs the smaller one. Chat keeps the sum.
const expanderGpuGbFrom = (info: typeof inferenceGpu) =>
info.available
? loadScopedGpu(info, Boolean(task)).memoryTotalGb
: undefined;
// Images / Video place through torch even where llama.cpp is a Vulkan build, so their budget
- // comes from the torch inventory. Everything else keeps the GGUF backend's own.
+ // comes from the torch inventory.
const expanderBudgetGpu = diffusionLoad ? gpu : inferenceGpu;
const expanderGpuGb = expanderGpuGbFrom(expanderBudgetGpu);
const expanderSystemGpuGb = expanderGpuGbFrom(gpu);
- // From the SAME scoping decision as the capacity above: loadScopedGpu narrows the count to 1
- // with memoryTotalGb, so the per-card reserve is never charged host-wide against one card,
- // and the RAM tier is the one that belongs to the device the load lands on.
+ // From the SAME scoping decision as the capacity above: loadScopedGpu narrows the count to 1,
+ // so the per-card reserve is never charged host-wide against one card.
const expanderScopedGpu = loadScopedGpu(expanderBudgetGpu, Boolean(task));
const expanderGpuCount = expanderScopedGpu.deviceCount;
const expanderRamGb = expanderScopedGpu.systemRamAvailableGb;
@@ -3992,20 +3862,20 @@ export function HubModelPicker({
lmStudioModels.filter(
(m) =>
filesystemRowsSupportedForTask(task, m.task) &&
- // The same speech gate the cached GGUF rows get: a CSM file found in LM
- // Unsloth, ./models or a scan folder is just as undecodable, and routing it to
- // Audio evicts the chat model before the row is reported unsupported.
+ // The same speech gate the cached GGUF rows get: a CSM file found locally is just as
+ // undecodable, and routing it to Audio evicts the chat model before the row is refused.
audioPickIsRoutable({
id: m.model_id ?? m.id,
task: m.task,
audioType: m.audio_type,
isGguf: localModelIsGguf(m),
isCurated: artifactForRepoId(m.model_id ?? m.id, AUDIO_CATALOG) !== null,
- // Task and codec came from the filesystem classifier, so a renamed CSM file
- // cannot borrow an Orpheus-looking path to clear the gate.
+ // Task and codec came from the filesystem classifier, so a renamed CSM file cannot borrow an
+ // Orpheus-looking path.
taskFromGgufArch: true,
}) &&
- // The backend tags every local model with its task for exactly this: on the Images/Video pages a chat GGUF must not be offered.
+ // The backend tags every local model with its task for exactly this: on the Images/Video pages
+ // a chat GGUF must not be offered.
passesTaskGate(
m.task,
m.model_id ?? m.id,
@@ -4031,25 +3901,25 @@ export function HubModelPicker({
activeCatalogArtifactIds,
],
);
- // Local ./models entries. Chat-only Unsloth runs GGUF (any host) and MLX (Mac only), so raw checkpoints there are hidden (mirrors the cached
- // non-GGUF rule); an MLX build a Mac user dropped in stays selectable. A task-scoped picker (Images) is exempt: the image backend loads local pipelines even there.
+ // Local ./models entries. Chat-only Unsloth runs GGUF anywhere and MLX on Mac, so raw
+ // checkpoints there are hidden; a task-scoped picker is exempt, since the image backend
+ // loads local pipelines.
const sortedLocalDir = useMemo(
() =>
sortLocalModels(
localDirModels.filter(
(m) =>
filesystemRowsSupportedForTask(task, m.task) &&
- // The same speech gate the cached GGUF rows get: a CSM file found in LM
- // Unsloth, ./models or a scan folder is just as undecodable, and routing it to
- // Audio evicts the chat model before the row is reported unsupported.
+ // The same speech gate the cached GGUF rows get: a CSM file found locally is just as
+ // undecodable, and routing it to Audio evicts the chat model first.
audioPickIsRoutable({
id: m.model_id ?? m.id,
task: m.task,
audioType: m.audio_type,
isGguf: localModelIsGguf(m),
isCurated: artifactForRepoId(m.model_id ?? m.id, AUDIO_CATALOG) !== null,
- // Task and codec came from the filesystem classifier, so a renamed CSM file
- // cannot borrow an Orpheus-looking path to clear the gate.
+ // Task and codec came from the filesystem classifier, so a renamed CSM file cannot borrow an
+ // Orpheus-looking path.
taskFromGgufArch: true,
}) &&
passesTaskGate(
@@ -4089,17 +3959,16 @@ export function HubModelPicker({
customFolderModels.filter(
(m) =>
filesystemRowsSupportedForTask(task, m.task) &&
- // The same speech gate the cached GGUF rows get: a CSM file found in LM
- // Unsloth, ./models or a scan folder is just as undecodable, and routing it to
- // Audio evicts the chat model before the row is reported unsupported.
+ // The same speech gate the cached GGUF rows get: a CSM file found locally is just as
+ // undecodable, and routing it to Audio evicts the chat model first.
audioPickIsRoutable({
id: m.model_id ?? m.id,
task: m.task,
audioType: m.audio_type,
isGguf: localModelIsGguf(m),
isCurated: artifactForRepoId(m.model_id ?? m.id, AUDIO_CATALOG) !== null,
- // Task and codec came from the filesystem classifier, so a renamed CSM file
- // cannot borrow an Orpheus-looking path to clear the gate.
+ // Task and codec came from the filesystem classifier, so a renamed CSM file cannot borrow an
+ // Orpheus-looking path.
taskFromGgufArch: true,
}) &&
passesTaskGate(
@@ -4128,7 +3997,8 @@ export function HubModelPicker({
],
);
- // Chat cannot load a diffusion model but the Images/Video pages can, so a pick routes to the page that runs it instead of hiding it or letting it 400. Task-scoped pickers select normally.
+ // Chat cannot load a diffusion model but the Images/Video pages can, so a pick routes to the
+ // page that runs it rather than 400ing. Task-scoped pickers select normally.
const navigateToPage = useNavigate();
const diffusionTaskById = useMemo(() => {
const byId = new Map<string, string>();
@@ -4147,7 +4017,8 @@ export function HubModelPicker({
};
for (const c of cachedGguf) put(c.repo_id, c.task);
for (const c of cachedModels) put(c.repo_id, c.task);
- // Both ids: a local row's click passes m.id (a filesystem path for models_dir / LM Studio entries) while m.model_id is its HF-style name, so keying on one alone makes the lookup below miss.
+ // Both ids: a local row's click passes m.id (a filesystem path) while m.model_id is its
+ // HF-style name, so keying on one alone makes the lookup miss.
const putLocal = (m: LocalModelInfo) => {
const exactAudioArtifact = m.model_id
? artifactForRepoId(m.model_id, AUDIO_CATALOG)
@@ -4200,8 +4071,8 @@ export function HubModelPicker({
if (page) {
void navigateToPage({
to: `/${page}`,
- // `quant` is used verbatim as the gguf filename, so a label like "Q4_K_M" rides ggufQuant instead; dropping it
- // made every non-curated GGUF repo arrive as a bare repo id.
+ // `quant` is used verbatim as the gguf filename, so a label like "Q4_K_M" rides ggufQuant
+ // instead; dropping it made every non-curated GGUF repo arrive as a bare repo id.
search:
page === "audio"
? {
@@ -4225,17 +4096,15 @@ export function HubModelPicker({
[task, diffusionTaskById, hubEvidenceById, navigateToPage, onSelectProp],
);
- // Fine-tuned models for the On Device "Fine-tuned" section: flat, query-
- // filtered, newest first.
+ // Fine-tuned models for the On Device section: flat, query-filtered, newest first.
const fineTunedRows = useMemo(() => {
const needle = normalizeForSearch(debouncedQuery.trim());
return loraModels
.filter(
(m) =>
- // A CSM export in a GGUF container loads nowhere: llama.cpp has no decoder and the
- // Audio page does not list speech GGUFs either. audioType comes off the checkpoint,
- // so it catches an exported row regardless of path; local rows have no audioType
- // and are refused at load instead.
+ // A CSM export in a GGUF container loads nowhere: llama.cpp has no decoder and the Audio page
+ // does not list speech GGUFs. audioType comes off the checkpoint, so a renamed path is
+ // still caught.
!localAudioRowIsUndecodableGguf({
audioType: m.audioType,
exportType: m.exportType,
@@ -4260,17 +4129,15 @@ export function HubModelPicker({
});
}, [loraModels, debouncedQuery]);
- // While searching, filter Downloaded by the query instead of hiding it, so a
- // downloaded model the user is searching for stays visible.
+ // While searching, filter Downloaded by the query instead of hiding it, so a downloaded model
+ // the user is searching for stays visible.
const visibleCachedGguf = useMemo(() => {
if (!showHfSection)
return sortedCachedGguf.filter((c) =>
matchesFormatFilter(c.repo_id, true, formatFilter),
);
const q = normalizeForSearch(debouncedQuery.trim());
- // Keep the format filter active while searching so the dropdown stays
- // consistent with the no-query branch (Safetensors selected shouldn't show
- // GGUF downloads just because the user typed).
+ // Keep the format filter active while searching so the dropdown stays consistent with the no-query branch.
return sortedCachedGguf.filter(
(c) =>
matchesFormatFilter(c.repo_id, true, formatFilter) &&
@@ -4290,8 +4157,10 @@ export function HubModelPicker({
);
}, [sortedCachedModels, showHfSection, debouncedQuery, formatFilter]);
- // Non-GGUF cached rows are not shown in chat-only mode, so the empty-state logic must use this (not visibleCachedModels) or the picker can go
- // blank. A task-scoped picker (Images) is exempt: the image backend loads local diffusers/safetensors pipelines even on chat-only hosts.
+ // Non-GGUF cached rows are not shown in chat-only mode, so the empty-state logic must use this
+ // or the picker can go blank. A task-scoped picker is exempt.
+ // Not visibleCachedModels, or the picker can go blank. A task-scoped picker (Images) is exempt:
+ // the image backend loads local diffusers/safetensors pipelines even on chat-only hosts.
const visibleCachedModelRows = chatOnly && !task ? [] : visibleCachedModels;
const visibleAdditionalOnDeviceModels = useMemo(() => {
@@ -4347,26 +4216,24 @@ export function HubModelPicker({
[visibleAdditionalOnDeviceModels],
);
- // Unfiltered list, so typing a query doesn't re-run resolution.
+ // Unfiltered list, so typing a query does not re-run resolution.
const soleQuants = useSoleDownloadedQuants(sortedCachedGguf, {
enabled: section === "downloaded" && !showAllQuantizations,
hfToken: hfToken || undefined,
});
- // Pinned entries surface in their own section above the Unsloth heading.
- // GGUF quants pin individually and their repo stays listed below; non-GGUF
- // repos pin whole and leave the Unsloth / Other models groups.
+ // Pinned entries surface in their own section above the Unsloth heading: GGUF quants pin
+ // individually with their repo still listed below, non-GGUF repos pin whole.
const pinnedIds = usePinnedModelsStore((s) => s.pinned);
const togglePinned = usePinnedModelsStore((s) => s.togglePinned);
const unpinRepo = usePinnedModelsStore((s) => s.unpinRepo);
const pinnedSet = useMemo(() => new Set(pinnedIds), [pinnedIds]);
- // Candidate pins whose repo still exists in the cache. Per-quant validation
- // below is needed because deleting one variant can leave a sibling cached.
+ // Candidate pins whose repo still exists in the cache; per-quant validation below is needed
+ // because deleting one variant can leave a sibling cached.
const pinnedQuantCandidates = useMemo(() => {
- // The existence check ignores the text query (keeps the format filter) so a
- // pinned quant stays findable by quant name; querying visibleCachedGguf would
- // drop the repo before the `${repoId} ${quant}` predicate could surface it.
+ // The existence check ignores the text query so a pinned quant stays findable by quant name;
+ // querying visibleCachedGguf would drop the repo first.
const cached = new Set(
sortedCachedGguf
.filter((c) => matchesFormatFilter(c.repo_id, true, formatFilter))
@@ -4412,8 +4279,8 @@ export function HubModelPicker({
.variants.filter((variant) => variant.downloaded === true)
.map((variant) => pinKey(repoId, variant.quant));
} catch {
- // If the backend cannot verify a quant, hiding the direct-load row
- // is safer than claiming a missing file is downloaded.
+ // If the backend cannot verify a quant, hiding the direct-load row is safer than claiming a
+ // missing file is downloaded.
return [];
}
}),
@@ -4473,8 +4340,7 @@ export function HubModelPicker({
return rows;
}, [pinnedIds, pinnedQuants, pinnedCachedModelRows]);
- // Split downloaded models so non-Unsloth repos get their own "Other models"
- // section above Fine-tuned.
+ // Split downloaded models so non-Unsloth repos get their own "Other models" section above Fine-tuned.
const unslothCachedGguf = useMemo(
() => visibleCachedGguf.filter((c) => isUnslothPublisherRepoId(c.repo_id)),
[visibleCachedGguf],
@@ -4502,8 +4368,8 @@ export function HubModelPicker({
[visibleCachedModelRows, pinnedSet],
);
- // Param counts come straight off the unsloth listings the picker already
- // loaded, so no extra per-id fetch is needed for the VRAM badges.
+ // Param counts come straight off the unsloth listings the picker already loaded, so the VRAM
+ // badges need no extra fetch.
const recommendedParamCountById = useMemo(() => {
const map = new Map<string, number>();
for (const r of [...results, ...recommendedSearch.results]) {
@@ -4512,8 +4378,7 @@ export function HubModelPicker({
return map;
}, [results, recommendedSearch.results]);
- // Shared by both search lists so a curated id one drops cannot return via the
- // other as a raw Hub row.
+ // Shared by both search lists so a curated id one drops cannot return via the other as a raw Hub row.
const searchRowFits = useCallback(
(row: {
id: string;
@@ -4525,10 +4390,8 @@ export function HubModelPicker({
searchRowFitsDevice(
{
...row,
- // Curated params last, same rule as the curated size below: a listing
- // total wins, but a repo no listing returns must still be sizable or
- // `requireKnown` hides it from search while the unfiltered Recommended
- // list, which reads the seed row's own metadata, keeps painting it.
+ // Curated params last, same rule as the curated size: a listing total wins, but a repo no
+ // listing returns must still be sizable or `requireKnown` hides it from search.
totalParams:
row.totalParams ??
recommendedParamCountById.get(row.id) ??
@@ -4542,8 +4405,8 @@ export function HubModelPicker({
gpu,
inferenceGpu,
taskScoped: Boolean(task),
- // Separate from taskScoped: that picks the single-device budget for every task page,
- // this picks the diffusion RULE, which only Images and Video use.
+ // Separate from taskScoped: that picks the single-device budget for every task page, this
+ // picks the diffusion RULE, which only Images and Video use.
diffusionLoad,
budgetFraction,
hostPooledMemory: gpu.loadDeviceSharesHostMemory,
@@ -4562,21 +4425,20 @@ export function HubModelPicker({
],
);
- // Recommended models that match the current search query
+ // Recommended models that match the current search query.
const filteredRecommendedIds = useMemo(() => {
if (!showHfSection) return [];
const q = normalizeForSearch(debouncedQuery.trim());
return (
- // Seeds included: recommendedIds hides downloaded models, which the unfiltered
- // Recommended list still paints, so without them a curated pick vanishes from
- // search the moment it is on disk unless a Hub listing row happens to carry it.
+ // Seeds included: recommendedIds hides downloaded models, which the unfiltered Recommended
+ // list still paints, so without them a curated pick vanishes from search once on disk.
searchableRecommendedIds(catalogSeedIds, recommendedIds)
.filter((id) => normalizeForSearch(id).includes(q))
.filter((id) =>
matchesFormatFilter(id, isKnownGgufRepo(id), formatFilter),
)
- // Curated defaults obey the fit toggle like the live HF rows, else large
- // defaults resurface in search results with the filter on.
+ // Curated defaults obey the fit toggle like the live HF rows, else large defaults resurface in
+ // search with the filter on.
.filter(
(id) =>
!fitOnDeviceOnly ||
@@ -4601,8 +4463,7 @@ export function HubModelPicker({
[filteredRecommendedIds],
);
- // One pipeline for both listings, so community rows clear the same gates;
- // `owned` is the only difference.
+ // One pipeline for both listings, so community rows clear the same gates; `owned` is the only difference.
const searchIdsFrom = useCallback(
(rows: readonly HfModelResult[], owned: (id: string) => boolean) =>
rows
@@ -4617,14 +4478,12 @@ export function HubModelPicker({
.map((result) => result.id)
.filter((id) => !isHiddenModelId(id))
.filter(owned)
- // Search reaches the live Hub, so without this a query re-lands the exact curated row the
- // seed and Recommended filters just dropped: the Mac format check below admits
- // safetensors, so MiniMaxAI/MiniMax-H3 would come back clickable and still be refused at
- // load. Same predicate as the other two lists, downloaded exception included.
+ // Search reaches the live Hub, so without this a query re-lands the exact curated row the seed
+ // and Recommended filters just dropped, clickable and still refused at load.
.filter(curatedOfferable)
.filter((id) => !recommendedSet.has(id))
- // Chat-only keeps runnable formats: GGUF anywhere, plus MLX/safetensors
- // on Mac (matches the empty Recommended view so search stays consistent).
+ // Chat-only keeps runnable formats: GGUF anywhere, plus MLX/safetensors on Mac, matching the
+ // empty Recommended view.
.filter(
(id) =>
!chatOnly || isRecommendableFormat(id, isKnownGgufRepo(id), isMac),
@@ -4654,8 +4513,7 @@ export function HubModelPicker({
return searchIdsFrom(results, isUnslothOwned);
}, [results, showHfSection, section, searchIdsFrom, isUnslothOwned]);
- // Community search hits, listed after the unsloth ones. Deduped against them so
- // a repo the unsloth listing already returned is not shown twice.
+ // Community search hits, listed after the unsloth ones and deduped against them.
const communitySearchIds = useMemo(() => {
if (!communityDiscoveryEnabled || !showHfSection) return [];
const above = new Set(hfIds.map((id) => id.toLowerCase()));
@@ -4682,8 +4540,7 @@ export function HubModelPicker({
isTaskRuntimeSupported,
]);
- /** Unsloth first, then community: one list so rows, keyboard order and the
- * empty state cannot drift apart. */
+ /** Unsloth first, then community: one list so rows, keyboard order and the empty state cannot drift apart. */
const searchRowIds = useMemo(
() => [...hfIds, ...communitySearchIds],
[hfIds, communitySearchIds],
@@ -4896,14 +4753,13 @@ export function HubModelPicker({
// GGUF fit is size-based and badged elsewhere; skip the qlora estimate.
if (isKnownGgufRepo(id)) continue;
const totalParams = recommendedParamCountById.get(id) ?? paramsFromId(id);
- // Same verdict the unfiltered list gives this row: searching for a model must not change
- // what it says about the device. paramsFromId reads "5B" out of the Wan id on its own, so
- // the estimator answers here even where the catalog is the only real source of a size.
+ // Same verdict the unfiltered list gives this row: searching for a model must not change what
+ // it says about the device.
const curatedFits = catalogFit(id, pipelineBudget);
if (catalog && curatedFits !== undefined) {
const curatedBytes = curatedSizeBytesFor(id, catalog);
- // The catalog is the only source of a count for a curated repo the listing never
- // returns and whose id spells no "<n>B", so the chip must fall back to it here too.
+ // The catalog is the only source of a count for a curated repo the listing never returns and
+ // whose id spells no "<n>B".
const params = totalParams ?? curatedTotalParamsFor(id, catalog);
map.set(id, {
est: curatedBytes ? Math.round(curatedBytes / 1024 ** 3) : 0,
@@ -4982,9 +4838,9 @@ export function HubModelPicker({
return () => observer.disconnect();
}, [scrollRef, updateListFades]);
- // Sentinel + IntersectionObserver for recommended infinite scroll. Re-running
- // per loaded page re-attaches the observer so a heavily filtered list keeps
- // paging until the viewport fills; fetchMore is a no-op while a page is in flight.
+ // Sentinel and IntersectionObserver for recommended infinite scroll. Re-running per loaded
+ // page re-attaches the observer so a heavily filtered list keeps paging; fetchMore no-ops
+ // while a page is in flight.
const [recommendedSentinel, setRecommendedSentinel] =
useState<HTMLDivElement | null>(null);
const recommendedSentinelRef = useCallback((node: HTMLDivElement | null) => {
@@ -5035,7 +4891,6 @@ export function HubModelPicker({
const handleModelClick = useCallback(
(id: string) => {
if (isKnownGgufRepo(id)) {
- // Toggle GGUF variant expander
setExpandedGguf((prev) => (prev === id ? null : id));
} else {
// Cached repos load now; uncached ones download via the Hub manager.
@@ -5050,8 +4905,8 @@ export function HubModelPicker({
[onSelect, isKnownGgufRepo, downloadedSet, pipelineTagById],
);
- // On Device owns the downloaded and custom-folder models; the Unsloth tab
- // searches the HF listing (below). Both filter locally by the query.
+ // On Device owns the downloaded and custom-folder models; the Unsloth tab searches the HF
+ // listing. Both filter locally by the query.
const showDownloaded = section === "downloaded";
const showCustom = section === "downloaded";
const showRecommendedSection = !showHfSection && section === "recommended";
@@ -5062,22 +4917,18 @@ export function HubModelPicker({
visibleAdditionalOnDeviceModels.length === 0 &&
sortedLmStudio.length === 0 &&
sortedLocalDir.length === 0 &&
- // Fine-tuned models are on-device too: don't show the empty state above a
- // non-empty Fine-tuned section.
+ // Fine-tuned models are on-device too: do not show the empty state above a non-empty Fine-tuned section.
fineTunedRows.length === 0;
- // Sort dropdown inline right of the section toggle; options depend on the tab
- // and stay visible while searching. Fixed width matches the Search Hub button
- // so it and the format dropdown line up. Trigger label clips; the menu shows full.
+ // Sort dropdown inline right of the section toggle; options depend on the tab and stay visible
+ // while searching. Fixed width matches the Search Hub button.
const sortTriggerClassName =
"h-(--picker-control-h) w-(--picker-control-w) shrink-0 justify-between pr-2.5 !border-0 text-xs [&>span]:!text-clip";
- // Tighter menu (less padding, text-xs) matching the trigger. Keep the option's
- // right padding so the selected-item checkmark never overlaps the label.
+ // Tighter menu matching the trigger; keep the option's right padding so the checkmark never overlaps the label.
const sortMenuContentClassName =
"!p-1 !rounded-[14px] [&_[role=option]]:!pl-2 [&_[role=option]]:!py-1.5 [&_[role=option]]:!text-xs [&_[role=option]]:!rounded-[10px]";
- // Device-fit toggle inside the sort menu (shared with the Hub page). The whole
- // row is the button: a Checkbox renders as a <button> and label-click forwarding
- // to it is unreliable, so the row owns the toggle and the Checkbox is presentational.
+ // Device-fit toggle inside the sort menu. The whole row is the button: a Checkbox renders as a
+ // <button> and label-click forwarding is unreliable, so the Checkbox is presentational.
const fitOnDeviceFooter = (
<Tooltip>
<TooltipTrigger asChild={true}>
@@ -5114,8 +4965,7 @@ export function HubModelPicker({
<span className="truncate">{label}</span>
</span>
);
- // On Device rows are already on disk, so the device-fit filter
- // only applies to the Unsloth listing.
+ // On Device rows are already on disk, so the device-fit filter only applies to the Unsloth listing.
const sectionSortDropdown =
section === "recommended" ? (
<HubOptionMenu
@@ -5199,8 +5049,7 @@ export function HubModelPicker({
.sort((a, b) => a.providerName.localeCompare(b.providerName));
}, [externalModels, debouncedQuery]);
const showConnected = section === "connected";
- // The Connected layout uses a wider box, so it drops the search inset to keep
- // Search Hub on the last dropdown's edge while the right gap matches the left.
+ // The Connected layout uses a wider box, so it drops the search inset to keep Search Hub on the last dropdown's edge.
const hasConnected = externalModels.length > 0;
// The Other models section and its shortcut only show with non-Unsloth downloads.
const hasOtherModels =
@@ -5210,13 +5059,9 @@ export function HubModelPicker({
const downloadedRowButtonClassName =
"bg-transparent pr-1 hover:bg-transparent focus-visible:bg-transparent dark:bg-transparent dark:hover:bg-transparent dark:focus-visible:bg-transparent";
- // Not focus-within: the dots menu returns focus to its trigger on close, so
- // the row stayed lit after the pointer left. Keyboard focus and an open menu
- // still light it.
- //
- // A row carrying a memory bar is two lines tall, and the shell paints the
- // hover and selected background (the inner button is transparent at these
- // sites), so the radius relaxes with it or the row renders as a stadium.
+ // Not focus-within: the dots menu returns focus to its trigger on close, so the row stayed lit
+ // after the pointer left. A row carrying a memory bar is two lines tall and the shell paints
+ // the background, so the radius relaxes with it or the row renders as a stadium.
const downloadedRowShellClassName = (
selected: boolean,
hasMemoryBar = false,
@@ -5227,8 +5072,7 @@ export function HubModelPicker({
selected && "bg-[#ececec] dark:bg-[var(--sidebar-accent)]",
);
- // A pinned quant: repo name with the quant as a grey chip. One click loads
- // that quant directly, no expansion needed.
+ // A pinned quant: repo name with the quant as a grey chip, loaded in one click.
const renderPinnedQuantRow = (entry: { repoId: string; quant: string }) => {
const optionKey = makeModelOptionKey(
"pinned-quant",
@@ -5245,8 +5089,7 @@ export function HubModelPicker({
key={optionKey}
className={downloadedRowShellClassName(isSelected, true)}
>
- {/* Through ModelRow, so a pinned quant lands in the same columns as
- the rows below it. */}
+ {/* Through ModelRow, so a pinned quant lands in the same columns as the rows below it. */}
<div className="min-w-0 flex-1">
<ModelRow
label={entry.repoId}
@@ -5255,12 +5098,9 @@ export function HubModelPicker({
)})`}
meta="GGUF"
quantChip={ggufQuantChipLabel(entry.quant)}
- // Same runtime gate the sole-quant row below applies. A pinned quant
- // can belong to an image, video or audio task, and those load through
- // the media planner rather than llama.cpp, so the KV estimator is
- // measuring the wrong runtime -- and it falls back to the file size
- // when it cannot size the model, so the row draws a confident
- // weights-only verdict rather than nothing.
+ // Same runtime gate the sole-quant row applies: a pinned quant can belong to an image, video
+ // or audio task, which load through the media planner, so the KV estimator would measure
+ // the wrong runtime and fall back to the file size.
memory={
mediaPageForTask(
diffusionTaskById.get(entry.repoId.toLowerCase()),
@@ -5279,8 +5119,8 @@ export function HubModelPicker({
isLora: false,
ggufVariant: entry.quant,
isDownloaded: true,
- // The row loads one quant, so it is a GGUF pick like the expander's; without this the pages asked for a
- // pipeline, which a GGUF repo rejects. No filename: the pin stores a label, resolved against the listing.
+ // The row loads one quant, so it is a GGUF pick like the expander's; without this the pages
+ // asked for a pipeline, which a GGUF repo rejects. No filename: the pin stores a label.
isGguf: true,
pipelineTag:
diffusionTaskById.get(entry.repoId.toLowerCase()) ?? null,
@@ -5318,9 +5158,8 @@ export function HubModelPicker({
}}
del={{
title: "Delete cached model?",
- // Same preview the Hub On Device row asks for, so a companion base an
- // installed image model still needs shows the reason and a disabled
- // Delete rather than an enabled one that comes back 400.
+ // Same preview the Hub On Device row asks for, so a companion base an installed image model
+ // still needs shows the reason and a disabled Delete.
impact: { repoId: entry.repoId, variant: entry.quant },
description: (
<>
@@ -5350,9 +5189,8 @@ export function HubModelPicker({
);
};
- // One quant on disk with "Show all quantizations" off: the expander would
- // list just that quant, so the row carries it as a chip and loads it in one
- // click, like a pinned quant.
+ // One quant on disk with "Show all quantizations" off: the expander would list just that
+ // quant, so the row carries it as a chip and loads it in one click.
const renderSoleQuantGgufRow = (
c: (typeof visibleCachedGguf)[number],
sole: SoleDownloadedQuant,
@@ -5492,8 +5330,8 @@ export function HubModelPicker({
const isSelected = value === c.repo_id;
const soleQuant = soleQuants.quants.get(c.repo_id);
if (soleQuant) return renderSoleQuantGgufRow(c, soleQuant);
- // Auto-expansion waits for the probe: expanding every row first would
- // mount an expander, and its remote listing, for repos about to collapse.
+ // Auto-expansion waits for the probe: expanding every row first would mount an expander, and
+ // its remote listing, for repos about to collapse.
const expanderOpen = shouldMountVariantExpander({
expanded: isGgufExpanded(c.repo_id),
autoExpand: expandQuantizations && !reopenedGguf.has(c.repo_id),
@@ -5733,8 +5571,7 @@ export function HubModelPicker({
const optionKey = makeModelOptionKey("additional-on-device", model.id);
const isSelected = value === model.id;
const pipelineTag = typeof task === "string" ? task : (task?.[0] ?? null);
- // A checkpoint trained here is identified by its directory, not a repo id, so
- // show the name and drop the Hub link -- the raw path reads as neither.
+ // A checkpoint trained here is identified by its directory, not a repo id, so show the name and drop the Hub link.
const isLocalPath = /^(?:[a-zA-Z]:[\\/]|[\\/]|~)/.test(model.id);
return (
<div key={model.id} className={downloadedRowShellClassName(isSelected)}>
@@ -5778,8 +5615,7 @@ export function HubModelPicker({
return (
<CapabilityScope.Provider value={capabilityScope}>
<div className="relative space-y-2">
- {/* A small right inset shortens the search bar so Search Hub lands on the
- last dropdown's right edge (none on the wider Connected box). */}
+ {/* A small right inset shortens the search bar so Search Hub lands on the last dropdown's right edge. */}
<div
className={cn(
"flex items-center gap-2 pb-1",
@@ -5827,9 +5663,8 @@ export function HubModelPicker({
) : null}
</div>
- {/* Keep the left-packed controls on one line while they fit, then wrap
- whole groups before their intrinsic widths cross the picker edge.
- Dropdowns hide on Connected. */}
+ {/* Keep the left-packed controls on one line while they fit, then wrap whole groups before
+ their intrinsic widths cross the picker edge. */}
<div
className={cn(
"flex flex-wrap items-center gap-2",
@@ -5857,9 +5692,8 @@ export function HubModelPicker({
ref={scrollRef}
onScroll={(e) => updateListFades(e.currentTarget)}
className={cn(
- // List sits within the menu padding so gaps match; height tracks content
- // up to the cap. scroll-py + symmetric px keep the focus ring off the
- // overflow clip edges during keyboard nav.
+ // The list sits within the menu padding so gaps match; scroll-py and symmetric px keep the
+ // focus ring off the overflow clip edges during keyboard nav.
"model-list-scroll max-h-[335px] overflow-y-auto scroll-py-1.5 px-0.5 mr-1",
listScrolled && "is-scrolled",
listMoreBelow && "is-bottom-faded",
@@ -5870,8 +5704,8 @@ export function HubModelPicker({
className={cn(
// Keep row actions clear of overlay scrollbars, overflowing or not.
"overlay-scrollbar-gutter",
- // On Device pulls the heading block tight to the controls; Recommended
- // keeps a little more top room above its first row.
+ // On Device pulls the heading block tight to the controls; Recommended keeps more top room
+ // above its first row.
showDownloaded ? "pt-0" : "pt-[4px]",
onEject ? "pb-[60px]" : "pb-4",
)}
@@ -5886,8 +5720,7 @@ export function HubModelPicker({
) : (
connectedGroups.map((group) => (
<div key={group.providerId}>
- {/* Wider than the On Device section labels: nothing
- divides these groups but the gap. */}
+ {/* Wider than the On Device section labels: nothing divides these groups but the gap. */}
<div className="flex items-center gap-2 px-2.5 pb-1 pt-5 text-ui-10 font-semibold uppercase tracking-wider text-muted-foreground">
<ApiProviderLogo
providerType={group.providerType}
@@ -5935,8 +5768,8 @@ export function HubModelPicker({
</div>
) : null}
- {/* Empty On Device: a search miss vs nothing downloaded yet. Hidden
- when custom folders below still have matches. */}
+ {/* Empty On Device: a search miss versus nothing downloaded yet. Hidden when custom folders
+ below still have matches. */}
{showDownloaded &&
cachedReady &&
downloadedEmpty &&
@@ -5950,9 +5783,8 @@ export function HubModelPicker({
</div>
) : null}
- {/* Pinned quants and models sit above the Unsloth heading so
- favorites are always first. Filtered by the query like the
- sections below. */}
+ {/* Pinned quants and models sit above the Unsloth heading, filtered by the query like the
+ sections below. */}
{showDownloaded && pinnedRows.length > 0 ? (
<>
<ListLabel
@@ -6055,8 +5887,7 @@ export function HubModelPicker({
</>
}
>
- {/* Rows drop the unsloth/ prefix; the heading carries
- it for the group. */}
+ {/* Rows drop the unsloth/ prefix; the heading carries it for the group. */}
Unsloth
</ListLabel>
{!downloadedCollapsed &&
@@ -6070,8 +5901,7 @@ export function HubModelPicker({
</>
) : null}
- {/* Other models: non-Unsloth downloads, grouped just above
- Fine-tuned. Shown only when such models exist. */}
+ {/* Other models: non-Unsloth downloads, shown only when such models exist. */}
{showDownloaded && hasOtherModels ? (
<div ref={otherModelsSectionRef}>
<ListLabel
@@ -6095,7 +5925,8 @@ export function HubModelPicker({
</div>
) : null}
- {/* Fine-tuned models: a section above Custom Folders, always shown on On Device so the train shortcut has a target. Hidden under a task filter (e.g. Images). */}
+ {/* Fine-tuned models: always shown on On Device so the train shortcut has a target. Hidden
+ under a task filter. */}
{section === "downloaded" && !task ? (
<>
<div
@@ -6370,8 +6201,7 @@ export function HubModelPicker({
onSelect={(picked) => {
setFolderInput(picked);
setFolderError(null);
- // Pass the path explicitly: `folderInput` state hasn't
- // flushed yet when "Use this folder" submits.
+ // Pass the path explicitly: `folderInput` state has not flushed when "Use this folder" submits.
void handleAddFolder(picked);
}}
/>
@@ -6382,14 +6212,11 @@ export function HubModelPicker({
const isGgufFile = m.path
.toLowerCase()
.endsWith(".gguf");
- // Honor the backend model_format hint (suffixless GGUF
- // folders) in addition to name/path so the row classifies
- // and loads through the same GGUF path as the filter.
+ // Honor the backend model_format hint (suffixless GGUF folders) as well as name/path, so the
+ // row classifies and loads through the same GGUF path as the filter.
const isGguf = localModelIsGguf(m);
- // Single .gguf files load directly; GGUF repos and
- // directories expand to pick a variant. An Ollama
- // manifest reference names one blob, so it is direct
- // too: POST /load materializes its .gguf link.
+ // Single .gguf files load directly; GGUF repos and directories expand to pick a variant. An
+ // Ollama manifest reference names one blob, so it is direct too.
const isDirectGguf =
isGgufFile || m.source === "ollama";
const optionKey = makeModelOptionKey(
@@ -6412,8 +6239,7 @@ export function HubModelPicker({
loadedModelId,
activeGgufVariant,
m.id,
- // Direct loads set no active variant, so
- // requiring one never reads as loaded.
+ // Direct loads set no active variant, so requiring one never reads as loaded.
isDirectGguf
? "ignore"
: isGguf
@@ -6534,9 +6360,8 @@ export function HubModelPicker({
const isGgufFile = m.path
.toLowerCase()
.endsWith(".gguf");
- // LM Studio dirs are GGUF but rarely carry a -GGUF suffix;
- // use the shared helper (model_format hint) so the row,
- // filter, and load path agree.
+ // LM Studio dirs are GGUF but rarely carry a -GGUF suffix, so use the shared helper for row,
+ // filter and load path to agree.
const isGguf = localModelIsGguf(m);
const optionKey = makeModelOptionKey("lm-studio", m.id);
return (
@@ -6663,10 +6488,8 @@ export function HubModelPicker({
</ListLabel>
{!localDirCollapsed &&
sortedLocalDir.map((m) => {
- // A loose ./models/*.gguf loads directly; a GGUF repo dir
- // expands to pick a variant. The variant scanner returns
- // nothing for a config-less loose file, so expanding it would
- // dead-end at "No GGUF variants".
+ // A loose ./models/*.gguf loads directly; a GGUF repo dir expands. The variant scanner returns
+ // nothing for a config-less loose file, so expanding it would dead-end.
const isGgufFile = m.path
.toLowerCase()
.endsWith(".gguf");
@@ -6809,8 +6632,8 @@ export function HubModelPicker({
hubUrl={hubRepoUrl(id)}
alignMeta="hub"
showSize={hubRowsShowSize}
- // A community row without its owner reads as an
- // unsloth upload, and two publishers would collide.
+ // A community row without its owner reads as an unsloth upload, and two
+ // publishers would collide.
hideOwner={isUnslothOwned(id)}
downloaded={downloadedSet.has(id.toLowerCase())}
partial={partialSet.has(id.toLowerCase())}
@@ -6927,9 +6750,9 @@ export function HubModelPicker({
id.toLowerCase(),
)}
capabilities={capsById.get(id)}
- // Same meta the unfiltered Recommended row shows, so a
- // model does not lose its size chip just because it was
- // reached by typing its name.
+ // Same meta the unfiltered Recommended row shows, so a model keeps its size
+ // chip when reached by typing.
+ // because it was reached by typing.
meta={
isKnownGgufRepo(id)
? (recommendedMeta.get(id)?.meta ?? "GGUF")
@@ -7173,8 +6996,8 @@ export function HubModelPicker({
);
}
-/** Fine-tuned model rows for the On Device tab's Fine-tuned section. Plugs into
- * that section's roving list and shared GGUF-expand state. */
+/** Fine-tuned model rows for the On Device tab's section; plugs into that section's roving list
+ * and shared GGUF-expand state. */
function FineTunedRows({
adapters,
value,
@@ -7227,9 +7050,9 @@ function FineTunedRows({
);
const isLocalGgufDir = localGgufKind === "variants";
const isLocalDirectGguf = localGgufKind === "direct";
- // A checkpoint that fine-tunes a TTS/STT model has to reach the Audio page:
- // chat/completions cannot serve it and reports the adapter as "not downloaded".
- // The pipeline tag is what onSelect routes on, so carry the detected codec as one.
+ // A checkpoint that fine-tunes a TTS/STT model has to reach the Audio page, since
+ // chat/completions cannot serve it. onSelect routes on the pipeline tag, so carry the
+ // detected codec as one.
const selectionMeta: ModelSelectorChangeMeta = {
source: isLocal ? "local" : isExported ? "exported" : "lora",
isLora,
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/pill-tabs.tsx b/studio/frontend/src/features/model-picker/components/model-selector/pill-tabs.tsx
index 81c5e660eae..ef484a221fa 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/pill-tabs.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/pill-tabs.tsx
@@ -10,9 +10,8 @@ export interface PillTab {
icon?: ReactNode;
}
-/** Segmented pill toggle reusing the Hub's .hub-tab-toggle styling (extended in
- * hub.css to also match .unsloth-model-selector-menu). Keeps tab roles for
- * keyboard nav. */
+/** Segmented pill toggle reusing the Hub's .hub-tab-toggle styling (extended in hub.css to also
+ * match .unsloth-model-selector-menu). Keeps tab roles for keyboard nav. */
export function PillTabs({
tabs,
value,
@@ -47,8 +46,8 @@ export function PillTabs({
className={cn(
"hub-menu-trigger hub-tab-toggle relative inline-flex items-center rounded-full",
compact ? "h-7" : "h-(--picker-control-h)",
- // Don't stretch to fill a flex-column parent (the popover) in fit mode,
- // and never compress so the last tab keeps its padding.
+ // Do not stretch to fill a flex-column parent (the popover) in fit mode, and never compress so
+ // the last tab keeps its padding.
fit && "w-fit max-w-full shrink-0 self-start",
className,
)}
@@ -69,9 +68,8 @@ export function PillTabs({
type="button"
role="tab"
aria-selected={value === tab.value}
- // Roving tabindex: only the active tab is in the tab order; Arrow
- // Left/Right move between tabs (WAI-ARIA tablist pattern). ArrowDown
- // is left to bubble so the picker's "enter the list" handler still runs.
+ // Roving tabindex: only the active tab is in the tab order; Arrow Left/Right move between tabs
+ // (WAI-ARIA tablist pattern). ArrowDown bubbles so the picker's "enter the list" handler runs.
tabIndex={value === tab.value ? 0 : -1}
disabled={disabled}
onKeyDown={(e) => {
@@ -97,8 +95,7 @@ export function PillTabs({
value === tab.value
? "text-foreground"
: "text-muted-foreground hover:text-foreground",
- // The active tab carries the pill; pin its hover bg so an
- // already-selected tab shows no hover change.
+ // The active tab carries the pill; pin its hover bg so an already-selected tab shows no hover change.
fit &&
value === tab.value &&
"hub-tab-toggle-pill hover:!bg-[var(--background)] dark:hover:!bg-[color-mix(in_srgb,var(--foreground)_10%,transparent)]",
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/pinned-models.ts b/studio/frontend/src/features/model-picker/components/model-selector/pinned-models.ts
index b2da618a642..d6d8c9baff0 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/pinned-models.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/pinned-models.ts
@@ -1,17 +1,16 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Pinned models for the model selector's On Device list, persisted in
-// localStorage so pins survive reloads. GGUF quants pin individually
-// (repoId + quant); non-GGUF repos pin as a whole. Pinned entries surface
-// in a "Pinned" section above the Unsloth/Downloaded group.
+// Pinned models for the model selector's On Device list, persisted in localStorage. GGUF quants
+// pin individually (repoId + quant); non-GGUF repos pin as a whole. Pinned entries surface in
+// a "Pinned" section above the Unsloth/Downloaded group.
import { create } from "zustand";
const KEY = "unsloth_pinned_models";
-// Entries are stored as strings: "repoId" pins a whole (non-GGUF) repo,
-// "repoId::quant" pins one GGUF quant. Neither part contains "::".
+// Entries are stored as strings: "repoId" pins a whole (non-GGUF) repo, "repoId::quant" pins
+// one GGUF quant. Neither part contains "::".
export function pinKey(repoId: string, quant?: string): string {
return quant ? `${repoId}::${quant}` : repoId;
}
@@ -60,22 +59,18 @@ function writePinned(pinned: string[]): void {
}
}
-// A drag reorders live under the cursor, so `movePinned` runs on every
-// dragenter. Persisting each of those would write localStorage dozens of times
-// per drag and, worse, would make a drag the user cancels with Escape or by
-// releasing outside the grid permanent: dragend can clear the drag marker but
-// cannot undo writes. So a drag session snapshots the order up front, keeps
-// every intermediate move in memory only, and either commits once on drop or
-// restores the snapshot when the drag ends without one.
+// A drag reorders live under the cursor, so `movePinned` runs on every dragenter. Persisting
+// each would write localStorage dozens of times per drag and would make a drag the user
+// cancels permanent, since dragend can clear the marker but cannot undo writes. So a drag
+// session snapshots the order up front, keeps intermediate moves in memory only, and either
+// commits once on drop or restores the snapshot.
let dragSnapshot: string[] | null = null;
-// Another window can rewrite the whole list mid-drag through the storage
-// listener below, which makes the snapshot stale: it describes an order that is
-// no longer in localStorage. Rolling back to it would leave this window
-// disagreeing with the record silently, since a cancel writes nothing, and the
-// next write from here would persist that stale order over the other window's
-// change. So the listener records the order it installed and the session falls
-// back to that instead. Null unless a storage event landed inside the session.
+// Another window can rewrite the whole list mid-drag through the storage listener below, which
+// makes the snapshot stale. Rolling back to it would leave this window silently disagreeing
+// with the record, and the next write would persist that stale order over the other window's
+// change. So the listener records the order it installed and the session falls back to that.
+// Null unless a storage event landed inside the session.
let dragExternalOrder: string[] | null = null;
function sameOrder(a: readonly string[], b: readonly string[]): boolean {
@@ -97,11 +92,9 @@ interface PinnedModelsState {
movePinned: (fromKey: string, toKey: string) => void;
/** Snapshot the current order so a cancelled drag can be undone. */
beginPinnedDrag: () => void;
- /**
- * End a drag session. `commit` persists the reordered list; otherwise the
- * snapshot taken by `beginPinnedDrag` is restored. Idempotent, because drop
- * is followed by dragend and only the first of the two may decide.
- */
+ /** End a drag session. `commit` persists the reordered list; otherwise the snapshot taken by
+ * `beginPinnedDrag` is restored. Idempotent, because drop is followed by dragend and only the
+ * first of the two may decide. */
endPinnedDrag: (commit: boolean) => void;
}
@@ -110,8 +103,7 @@ export const usePinnedModelsStore = create<PinnedModelsState>((set) => ({
togglePinned: (repoId, quant) =>
set((state) => {
const key = pinKey(repoId, quant);
- // Newest pin first, so "Pin to top" literally lands on top of the
- // pinned group rather than under earlier pins.
+ // Newest pin first, so "Pin to top" literally lands on top of the pinned group rather than under earlier pins.
const next = state.pinned.includes(key)
? state.pinned.filter((id) => id !== key)
: [key, ...state.pinned];
@@ -151,8 +143,8 @@ export const usePinnedModelsStore = create<PinnedModelsState>((set) => ({
dragSnapshot = null;
dragExternalOrder = null;
if (snapshot === null) return state;
- // What this window and localStorage last agreed on: the order another
- // window installed mid-drag if there was one, else the pre-drag snapshot.
+ // What this window and localStorage last agreed on: the order another window installed mid-drag
+ // if there was one, else the pre-drag snapshot.
const base = external ?? snapshot;
if (commit) {
// Nothing moved on top of that, so there is nothing to persist.
@@ -160,10 +152,9 @@ export const usePinnedModelsStore = create<PinnedModelsState>((set) => ({
writePinned(state.pinned);
return state;
}
- // A cancel writes nothing, so it has to land on the order that is already
- // in localStorage. That is the snapshot for an ordinary drag and the
- // other window's list when one landed mid-drag, whatever it did to the
- // keys; the moves this drag previewed on top of it are dropped either way.
+ // A cancel writes nothing, so it has to land on the order already in localStorage. That is the
+ // snapshot for an ordinary drag and the other window's list when one landed mid-drag; the
+ // moves this drag previewed on top of it are dropped either way.
if (sameOrder(base, state.pinned)) return state;
return { pinned: base };
}),
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/recommended-fit.ts b/studio/frontend/src/features/model-picker/components/model-selector/recommended-fit.ts
index f26aef202e2..abde74ab59e 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/recommended-fit.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/recommended-fit.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Pure helpers for the Recommended list: which formats to surface and whether a
-// model fits the device. No React/DOM deps so they are easy to test.
+// Pure helpers for the Recommended list: which formats to surface and whether a model fits
+// the device. No React/DOM deps so they are easy to test.
import { classifyGgufFit } from "../../../../lib/gguf-fit.ts";
import { classifyMediaGgufFit } from "./model-catalog.ts";
@@ -18,8 +18,7 @@ export function isMlxId(id: string): boolean {
return MLX_RE.test(id);
}
-// "mobile" build token (e.g. "gemma-4-E4B-it-qat-mobile-GGUF"); bounded so it
-// never matches inside a longer word.
+// "mobile" build token (e.g. "gemma-4-E4B-it-qat-mobile-GGUF"); bounded so it never matches inside a longer word.
const MOBILE_RE = /(?:^|[-_/. ])mobile(?:$|[-_/. ])/i;
/** A mobile-targeted build, which we keep out of the Recommended list. */
@@ -27,8 +26,8 @@ export function isMobileVariant(id: string): boolean {
return MOBILE_RE.test(id);
}
-/** What Recommended is allowed to suggest: GGUF anywhere; on Mac also MLX and
- * safetensors (both now run locally there). GPU keeps GGUF-only recommendations. */
+/** What Recommended may suggest: GGUF anywhere; on Mac also MLX and safetensors. GPU keeps
+ * GGUF-only recommendations. */
export function isRecommendableFormat(
id: string,
hintedIsGguf: boolean | undefined,
@@ -38,8 +37,7 @@ export function isRecommendableFormat(
return isMac;
}
-/** Format filter for the listing toggle. "safetensors" means anything that is
- * neither GGUF nor MLX. */
+/** Format filter for the listing toggle. "safetensors" means anything that is neither GGUF nor MLX. */
export type FormatFilter = "all" | "gguf" | "mlx" | "safetensors";
export function matchesFormatFilter(
@@ -59,13 +57,12 @@ export function matchesFormatFilter(
}
}
-// First "<n>B" token in a repo id, e.g. "Qwen3-30B-A3B" -> 30 (MoE total),
-// "gemma-4-E4B" -> 4. Digits must be separator-bounded so we never read "16"
-// from "bf16" or the "2" in "Kimi-K2".
+// First "<n>B" token in a repo id, e.g. "Qwen3-30B-A3B" -> 30. Digits must be
+// separator-bounded so "16" is never read from "bf16".
const PARAM_RE = /(?:^|[-_/. ])[eE]?(\d+(?:\.\d+)?)\s*[bB](?=$|[-_./ ])/;
-/** Parameter count (absolute, e.g. 4e9) parsed from a repo id, or undefined
- * when the id has no size token (so callers can treat the size as unknown). */
+/** Parameter count parsed from a repo id, or undefined when it has no size token, so callers
+ * can treat the size as unknown. */
export function paramsFromId(id: string): number | undefined {
const match = PARAM_RE.exec(id);
if (!match) return undefined;
@@ -73,8 +70,8 @@ export function paramsFromId(id: string): number | undefined {
return Number.isFinite(billions) && billions > 0 ? billions * 1e9 : undefined;
}
-// Smallest practical GGUF/MLX quant (~Q2_K). The fit check asks whether a model
-// can run at all, so it uses this rather than a default 4-bit size.
+// Smallest practical GGUF/MLX quant (~Q2_K). The fit check asks whether a model can run at
+// all, so it uses this rather than a default 4-bit size.
const MIN_QUANT_BYTES_PER_PARAM = 0.4;
/** Rough on-disk bytes for the smallest practical quant of `params` weights. */
@@ -83,13 +80,10 @@ export function estimateQuantBytes(params: number): number {
}
/** A model fits when it can run at all: `classifyGgufFit` short of `oom`, so a partial CPU
- * offload counts, since that loads and merely runs slower. Shares the loader's formula with the
- * Hub badge and the quant rows, because this predicate ALSO gates the "Fits on device" filter,
- * and a filter that hides a row the quant list would badge as runnable is the same bug twice.
- *
- * Unknown device means we cannot tell, so treat it as fitting. Unknown size normally fits too,
- * but Recommended passes `requireKnown` so a model we cannot size (e.g. a huge GGUF with no
- * metadata or size token) is hidden rather than wrongly shown. */
+ * offload counts. Shares the loader's formula with the Hub badge and the quant rows, since
+ * this predicate ALSO gates the "Fits on device" filter. Unknown device means we cannot
+ * tell, so treat it as fitting. Unknown size normally fits too, but Recommended passes
+ * `requireKnown` so an unsizable model is hidden rather than wrongly shown. */
export function fitsDevice(opts: {
sizeBytes?: number;
gpuGb?: number;
@@ -100,9 +94,8 @@ export function fitsDevice(opts: {
/** How many GPUs gpuGb sums, so the gate charges the loader's per-card VRAM reserve the same
* number of times the badge does. Absent means one. */
gpuCount?: number;
- /** Images / Video: the row is placed by the diffusion backend, not llama-server, so it takes the
- * media rule the quant rows under it use. Applies to every format on those pages, GGUF or not:
- * that rule is the budget all of them had before the classifiers were merged. */
+ /** Images / Video: the row is placed by the diffusion backend, not llama-server, so it takes
+ * the media rule the quant rows use. Applies to every format on those pages. */
mediaLoad?: boolean;
/** The load device's memory is a window into host RAM, so RAM is not a second budget to add.
* Media rule only; the llama.cpp one already takes a RAM figure with the pool removed. */
@@ -119,16 +112,15 @@ export function fitsDevice(opts: {
mediaLoad,
hostPooledMemory,
} = opts;
- // Unified-memory hosts (Mac / no discrete GPU) report system RAM but no GPU, so the budget must
- // include RAM. Only an entirely unknown budget fits freely.
+ // Unified-memory hosts report system RAM but no GPU, so the budget must include RAM. Only an
+ // entirely unknown budget fits freely.
const anyBudget =
Math.max(0, gpuGb ?? 0) > 0 || Math.max(0, systemRamGb ?? 0) > 0;
if (!anyBudget) return !budgetKnown;
if (sizeBytes && sizeBytes > 0) {
if (mediaLoad) {
// No RAM tier on a host pool: diffusion offload moves bytes inside that one pool and frees
- // nothing. llama.cpp below keeps its tier, because a GGUF really does spill into whatever
- // host RAM the GPU window does not already cover.
+ // nothing. llama.cpp keeps its tier, since a GGUF really does spill into host RAM.
return (
classifyMediaGgufFit(
sizeBytes,
@@ -149,15 +141,11 @@ export function fitsDevice(opts: {
return requireKnown ? false : true;
}
-/** Fit predicate for one Hub listing row, shared by the chat model selector
- * and the Hub page "Fits on device" filter. GGUF repos: metadata size (actual
- * weights) or the smallest-quant estimate from the param count. Safetensors /
- * MLX repos: always the params-based smallest-quant estimate, matching the
- * VRAM badge's quantized-load assumption; their estimatedSizeBytes is the
- * full-precision checkpoint and would wrongly hide models the quantized load
- * path can run. `curatedSizeBytes` outranks both: real data over estimates.
- * Anything still unsizable is hidden (requireKnown) so over-budget models with
- * no metadata don't slip through. An unknown device budget keeps everything. */
+/** Fit predicate for one Hub listing row, shared by the chat model selector and the Hub page
+ * filter. GGUF repos use metadata size or the smallest-quant estimate. Safetensors / MLX
+ * always use the params-based estimate, matching the badge's quantized-load assumption,
+ * since their estimatedSizeBytes is the full-precision checkpoint. `curatedSizeBytes`
+ * outranks both. Anything still unsizable is hidden; an unknown device budget keeps all. */
export function hfModelFitsDevice(
model: {
id: string;
@@ -171,9 +159,9 @@ export function hfModelFitsDevice(
systemRamAvailableGb: number;
budgetKnown?: boolean;
},
- /** `budgetFraction` is the user's saved VRAM Budget: omitted scores against the loader's default,
- * so a caller that forgets it judges rows on a budget the user has already replaced.
- * `mediaLoad` picks the diffusion rule for an Images / Video row. */
+ /** `budgetFraction` is the user's saved VRAM Budget: omitted scores against the loader's
+ * default, so a caller that forgets it judges rows on a replaced budget. `mediaLoad` picks
+ * the diffusion rule for an Images / Video row. */
opts: {
budgetFraction?: number;
gpuCount?: number;
@@ -207,10 +195,9 @@ export function hfModelFitsDevice(
});
}
-/** The budget a task-scoped (Images / Video) row may claim. Those loads put the
- * whole pipeline on ONE device (a bare "cuda", the lowest visible ordinal), so
- * fit is judged against that card, never the multi-GPU sum, which would
- * recommend a checkpoint that OOMs where the load lands. Chat keeps the sum. */
+/** The budget a task-scoped (Images / Video) row may claim. Those loads put the whole pipeline
+ * on ONE device, so fit is judged against that card, never the multi-GPU sum, which would
+ * recommend a checkpoint that OOMs where the load lands. Chat keeps the sum. */
export function loadScopedGpu<
T extends {
available: boolean;
@@ -230,14 +217,13 @@ export function loadScopedGpu<
return {
...gpu,
memoryTotalGb: deviceGb,
- // Narrowed with the capacity it describes, or the loader's per-card VRAM reserve gets charged
- // once per HOST GPU against a ONE-card budget. Two 24 GiB cards at a 1.0 setting scored an
- // audio quant against 23.28 GiB where the loader offers the selected card's 23.5.
+ // Narrowed with the capacity it describes, or the per-card VRAM reserve gets charged once per
+ // HOST GPU against a ONE-card budget. Two 24 GiB cards at 1.0 scored an audio quant
+ // against 23.28 GiB where the loader offers the selected card's 23.5.
deviceCount: 1,
// The raw-host figure is the RAM a DEDICATED task device may claim back from a shared GPU's
- // reservation. Gated on the folded flag, not shared_memory: that one is Windows-only, so a
- // Linux ROCm APU took this branch and undid the very subtraction that keeps its GTT window
- // out of the RAM tier.
+ // reservation. Gated on the folded flag, not shared_memory, which is Windows-only: a Linux
+ // ROCm APU took this branch and undid the subtraction keeping its GTT window out.
systemRamAvailableGb: hostPooledLoadDevice(gpu)
? gpu.systemRamAvailableGb
: (gpu.systemRamAvailableHostGb ?? gpu.systemRamAvailableGb),
@@ -245,7 +231,7 @@ export function loadScopedGpu<
}
/** Whether the device a task load lands on draws from host RAM. Prefers the folded flag, which
- * counts a Linux APU that reports unified_memory without shared_memory. */
+ * counts a Linux APU reporting unified_memory without shared_memory. */
function hostPooledLoadDevice(gpu: {
loadDeviceSharedMemory?: boolean;
loadDeviceSharesHostMemory?: boolean;
@@ -253,10 +239,9 @@ function hostPooledLoadDevice(gpu: {
return gpu.loadDeviceSharesHostMemory ?? gpu.loadDeviceSharedMemory === true;
}
-/** One fit predicate for both search lists (curated matches and the Hub rows
- * below them). The curated list only suppresses ids it kept, so a row it drops
- * reappears from the Hub list: judge both on the same size and budget, or the
- * toggle leaks an oversized row. */
+/** One fit predicate for both search lists. The curated list only suppresses ids it kept, so a
+ * row it drops reappears from the Hub list: judge both on the same size and budget, or the
+ * toggle leaks an oversized row. */
export function searchRowFitsDevice<
G extends {
available: boolean;
@@ -284,8 +269,8 @@ export function searchRowFitsDevice<
* picks the diffusion RULE, which Audio must not get: its GGUFs run under llama.cpp. */
diffusionLoad?: boolean;
budgetFraction?: number;
- /** How many GPUs the aggregate sums, when the caller's inventory does not carry the count
- * itself. loadScopedGpu narrows it to 1 with the capacity, so a task page needs nothing here. */
+ /** How many GPUs the aggregate sums, when the caller's inventory does not carry the count.
+ * loadScopedGpu narrows it to 1 with the capacity, so a task page needs nothing here. */
gpuCount?: number;
/** The image/video load device's pool is host RAM, so RAM is not a second budget. */
hostPooledMemory?: boolean;
@@ -314,18 +299,11 @@ export function searchRowFitsDevice<
);
}
-/** The id pool the Recommended SEARCH matches a query against: the curated seeds the
- * unfiltered list paints, then the listing ids, each id once and seeds first (the
- * order `orderRecommendedRows` renders them in).
- *
- * The listing pool drops every id already on disk, because a downloaded model has its
- * own On Device row. The seed pool does not, and the unfiltered Recommended list keeps
- * painting a downloaded curated model (badged "downloaded"), so search has to keep it
- * too. Without this a curated pick disappears from Recommended search the moment it is
- * downloaded, and the only thing that can bring it back is a live Hub listing row --
- * which a repo the listing does not return (new, non-unsloth owner, invisible to this
- * account) never gets. The row then sits in the unfiltered list yet cannot be found by
- * typing its name. */
+/** The id pool the Recommended SEARCH matches against: curated seeds first, then listing ids,
+ * each id once. The listing pool drops ids already on disk, since a downloaded model has
+ * its own On Device row; the seed pool does not, because the unfiltered list keeps painting
+ * a downloaded curated model. Without this a curated pick disappears from search once
+ * downloaded, and only a live Hub listing row could bring it back. */
export function searchableRecommendedIds(
seedIds: readonly string[],
listingIds: readonly string[],
@@ -341,13 +319,11 @@ export function searchableRecommendedIds(
return out;
}
-/** Order Recommended: curated seeds first in catalog order, then the rest of the
- * listing, each id once. A seed hands off only to a row that survived `keep`, so
- * a painted curated row does not vanish when the listing reports it with
- * metadata the filters reject. Fit is judged on whichever row renders, and the
- * taking-over row inherits the seed's curated size: a Hub listing carries none,
- * so a prequantized artifact would otherwise flip to the params guess (which
- * assumes a quant still to come). */
+/** Order Recommended: curated seeds first in catalog order, then the rest of the listing, each
+ * id once. A seed hands off only to a row that survived `keep`, so a painted curated row
+ * does not vanish when the listing reports it with rejected metadata. The taking-over row
+ * inherits the seed's curated size, or a prequantized artifact would flip to the params
+ * guess, which assumes a quant still to come. */
export function orderRecommendedRows<
T extends { id: string; curatedSizeBytes?: number },
>(opts: {
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/row-identity.ts b/studio/frontend/src/features/model-picker/components/model-selector/row-identity.ts
index 0bda944fdfd..61568201f0d 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/row-identity.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/row-identity.ts
@@ -1,9 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Pure identity helpers for picker rows: id and quant comparison, and the
-// selected/loaded state of a row that stands for one exact quant. No
-// React/DOM deps so they stay easy to test.
+// Pure identity helpers for picker rows: id and quant comparison, and the selected/loaded state
+// of a row that stands for one exact quant. No React/DOM deps so they stay easy to test.
export function normalizeModelIdForPicker(modelId: string): string {
const trimmed = modelId.trim();
@@ -42,14 +41,11 @@ export function ggufVariantsMatchForPicker(
);
}
-/** Selected and loaded state for a row that names one quant, such as a pinned
- * quant or an On Device repo holding a single quant.
- *
- * Loaded is exact: only the running quant wears the badge. Selected follows
- * the picker's value, minus the case where the repo is running a different
- * quant, so the row cannot claim a pick that points elsewhere. Compare panes
- * hold a staged value while another pane's model is resident, so a repo that
- * is not the loaded one stays selected on its value alone. */
+/** Selected and loaded state for a row that names one quant, such as a pinned quant or an On
+ * Device repo holding a single quant. Loaded is exact: only the running quant wears the badge.
+ * Selected follows the picker's value, minus the case where the repo is running a different
+ * quant. Compare panes hold a staged value while another pane's model is resident, so a repo
+ * that is not the loaded one stays selected on its value alone. */
export function soleQuantRowState({
pickerValue,
repoId,
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/row-meta.ts b/studio/frontend/src/features/model-picker/components/model-selector/row-meta.ts
index fc82c2ed6db..bb100a3abb5 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/row-meta.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/row-meta.ts
@@ -1,16 +1,14 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Pure helpers for model-row presentation: owner/name split, lora trigger
-// label, format pills, param chip, tabular size. No React/DOM deps so they
-// stay easy to test.
+// Pure helpers for model-row presentation: owner/name split, lora trigger label, format pills,
+// param chip, tabular size. No React/DOM deps so they stay easy to test.
import type { LoraModelOption } from "./types";
export type FormatTone = "gguf" | "mlx" | "checkpoint" | "adapter";
-// Format keyword to DotTag tone. Looked up by full token and by first word,
-// so "Full finetune" resolves via "full".
+// Format keyword to DotTag tone. Looked up by full token and by first word, so "Full finetune" resolves via "full".
export const FORMAT_TONE: Record<string, FormatTone> = {
gguf: "gguf",
mlx: "mlx",
@@ -36,16 +34,15 @@ export function splitRepoLabel(label: string): {
return { owner: label.slice(0, slash), name: label.slice(slash + 1) };
}
-/** Our official Hub owners. Rows drop the owner prefix, since the Unsloth
- * section already carries it; the full repo id stays in the row tooltip. */
+/** Our official Hub owners. Rows drop the owner prefix, since the Unsloth section already carries
+ * it; the full repo id stays in the row tooltip. */
export function isUnslothOwner(owner: string | null | undefined): boolean {
const normalized = owner?.toLowerCase();
return normalized === "unsloth" || normalized === "unslothai";
}
-/** Selector trigger label. Training and exported runs are named "<run>/<model>"
- * ("foo_123/foo"), so the run is the label; local inventory rows keep theirs,
- * since an Ollama repo name carries real slashes ("hf.co/unsloth/...:Q4_K_M"). */
+/** Selector trigger label. Training and exported runs are named "<run>/<model>", so the run is the
+ * label; local inventory rows keep theirs, since an Ollama repo name carries real slashes. */
export function loraOptionLabel(
option: Pick<LoraModelOption, "name" | "source">,
): string {
@@ -66,8 +63,8 @@ const META_APPROX_RE = /^~/;
const META_PARAM_RE = /^\d+(?:\.\d+)?B$/i;
const META_WHITESPACE_RE = /\s+/;
-/** Classify a meta token: size (has KB/MB/GB/TB or leading "~"), param (bare
- * "<n>B" like "4B"), format keyword, or plain text. */
+/** Classify a meta token: size (has KB/MB/GB/TB or leading "~"), param (bare "<n>B"), format
+ * keyword, or plain text. */
export function classifyMetaToken(raw: string): MetaToken | null {
const t = raw.trim();
if (!t) return null;
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/sole-quant-cache.ts b/studio/frontend/src/features/model-picker/components/model-selector/sole-quant-cache.ts
index 4817f891e92..dbce2042f24 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/sole-quant-cache.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/sole-quant-cache.ts
@@ -1,13 +1,12 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Bookkeeping for the On Device sole-quant probe: what is known, what is
-// still being read, and what needs reading. Per repo, so one repo's
-// invalidation leaves the rest of the list alone. No React/DOM deps so it
-// stays easy to test.
+// Bookkeeping for the On Device sole-quant probe: what is known, what is being read, and what
+// needs reading. Per repo, so one repo's invalidation leaves the rest alone. No React/DOM
+// deps so it stays easy to test.
-/** A repo to probe, tagged with the cache version and location it reads at,
- * and separately with the bytes on disk behind it. */
+/** A repo to probe, tagged with the cache version and location it reads at, and separately with
+ * the bytes on disk behind it. */
export interface SoleQuantTarget {
repoId: string;
localSource: string | null;
@@ -15,16 +14,15 @@ export interface SoleQuantTarget {
key: string;
}
-/** A probe result. A null quant means the repo has no single complete quant,
- * including when the read failed: either way the row stays expandable. */
+/** A probe result. A null quant means the repo has no single complete quant, including when the
+ * read failed: either way the row stays expandable. */
export interface SoleQuantEntry<T> {
key: string;
quant: T | null;
}
-/** Identity of one repo's probe. Moves when that repo's variants cache is
- * invalidated, the row points at another directory, or the bytes on disk
- * change under us. */
+/** Identity of one repo's probe. Moves when that repo's variants cache is invalidated, the row
+ * points at another directory, or the bytes on disk change under us. */
export function soleQuantKey(
version: string | undefined,
localSource: string | null,
@@ -33,12 +31,10 @@ export function soleQuantKey(
return `${version ?? ""}::${localSource ?? ""}::${fingerprint}`;
}
-/** What the inventory reports about a repo's files. A change here means disk
- * moved, including from outside this tab, so the cached listing is suspect.
- *
- * Download state is part of it: a sibling cancelled before any file landed
- * moves neither the bytes nor the mtime, and the repo's own partial flag
- * stays false while another quant is clean, so nothing else would notice. */
+/** What the inventory reports about a repo's files. A change here means disk moved, including
+ * from outside this tab, so the cached listing is suspect. Download state is part of it: a
+ * sibling cancelled before any file landed moves neither the bytes nor the mtime, and the
+ * repo's own partial flag stays false while another quant is clean. */
export function soleQuantFingerprint(repo: {
size_bytes?: number;
last_modified?: number;
@@ -49,8 +45,8 @@ export function soleQuantFingerprint(repo: {
}`;
}
-/** Split the listed repos into resolved rows, repos still being read, and
- * repos needing a read. A repo holds its result until its own key moves. */
+/** Split the listed repos into resolved rows, repos still being read, and repos needing a read.
+ * A repo holds its result until its own key moves. */
export function partitionSoleQuants<T>(
targets: readonly SoleQuantTarget[],
entries: ReadonlyMap<string, SoleQuantEntry<T>>,
@@ -76,9 +72,9 @@ export function partitionSoleQuants<T>(
return { quants, pending, stale };
}
-/** Runs the reads. Kept apart from React so the ordering rules below can be
- * exercised directly: a repo is read once per key, and a read whose repo has
- * since moved on is dropped rather than committed over the newer result. */
+/** Runs the reads. Kept apart from React so the ordering rules below can be exercised directly:
+ * a repo is read once per key, and a read whose repo has since moved on is dropped rather
+ * than committed over the newer result. */
export function createSoleQuantReader<T>({
workers,
read,
@@ -124,12 +120,10 @@ export function createSoleQuantReader<T>({
};
}
-/** Repos whose bytes moved since they were last seen, recording what is seen
- * now. Their cached listing predates the change, so it should be dropped.
- *
- * Compares the fingerprint alone, never the probe key: the key also carries
- * the variants cache version, and dropping a listing bumps that version, so
- * comparing keys would invalidate on its own effect forever. */
+/** Repos whose bytes moved since they were last seen, recording what is seen now. Their cached
+ * listing predates the change, so it should be dropped. Compares the fingerprint alone, never
+ * the probe key: the key also carries the variants cache version, and dropping a listing
+ * bumps that version, so comparing keys would invalidate on its own effect forever. */
export function takeDriftedRepos(
targets: readonly SoleQuantTarget[],
seen: Map<string, string>,
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/source-tabs.ts b/studio/frontend/src/features/model-picker/components/model-selector/source-tabs.ts
index 92c2d47ed21..300c441ea11 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/source-tabs.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/source-tabs.ts
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/** Local models (LM Studio, Ollama, custom folders) are not fine-tuned;
- * they live in the Hub picker's On Device section. */
+/** Local models (LM Studio, Ollama, custom folders) are not fine-tuned; they live in the Hub
+ * picker's On Device section. */
export function isFineTunedSource(source?: string): boolean {
return source !== "local";
}
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/types.ts b/studio/frontend/src/features/model-picker/components/model-selector/types.ts
index bf6800809a6..afd14d825a9 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/types.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/types.ts
@@ -10,11 +10,11 @@ export interface ModelOption {
description?: string;
icon?: ReactNode;
isGguf?: boolean;
- /** Fixed quant used by a specialized on-device runtime. Generic Hub GGUF
- * rows discover their variants dynamically instead. */
+ /** Fixed quant used by a specialized on-device runtime. Generic Hub GGUF rows discover their
+ * variants dynamically instead. */
deviceQuant?: string;
- /** Fallback metadata for task-owned caches that an older generic inventory
- * cannot describe. Current servers normally provide a full cached row. */
+ /** Fallback metadata for task-owned caches that an older generic inventory cannot describe.
+ * Current servers normally provide a full cached row. */
deviceSize?: string;
deviceSizeBytes?: number;
deviceLoaded?: boolean;
@@ -27,8 +27,7 @@ export interface LoraModelOption extends ModelOption {
updatedAt?: number;
source?: "training" | "exported" | "local";
- /** This local GGUF is one directly loadable artifact, not a repo whose quant
- * variants must be listed first. */
+ /** This local GGUF is one directly loadable artifact, not a repo whose quant variants must be listed first. */
isDirectGguf?: boolean;
exportType?: "lora" | "merged" | "gguf";
/** Codec when the checkpoint fine-tunes an audio model, else null. */
@@ -46,14 +45,15 @@ export interface ModelSelectorChangeMeta {
source: "hub" | "lora" | "exported" | "local" | "external";
isLora: boolean;
ggufVariant?: string;
- /** Exact GGUF filename for the picked quant (filenames do not always follow the repo name, e.g. FLUX.1-schnell -> flux1-schnell-*.gguf). */
+ /** Exact GGUF filename for the picked quant; filenames do not always follow the repo name
+ * (FLUX.1-schnell -> flux1-schnell-*.gguf). */
ggufFilename?: string;
isDownloaded?: boolean;
expectedBytes?: number;
/** Native GGUF context, threaded so a staged pick can seed the slider. */
contextLength?: number | null;
- /** Direct local .gguf file picked without a variant (custom folder / LM
- * Unsloth). Marks it as a GGUF source for the deferred-load staging flow. */
+ /** Direct local .gguf file picked without a variant (custom folder / LM Unsloth). Marks it as a
+ * GGUF source for the deferred-load staging flow. */
isGguf?: boolean;
/** Staged metadata confirmed the separate DiffusionGemma runner. */
isDiffusion?: boolean;
@@ -63,16 +63,16 @@ export interface ModelSelectorChangeMeta {
loadId?: string | null;
/** Native path token so an active-model reload can reopen a file-picked GGUF. */
nativePathToken?: string;
- /** Hub pipeline tag for an uncurated pick, so a task page can tell which task
- * the repo does when it is not in the page's catalog. */
+ /** Hub pipeline tag for an uncurated pick, so a task page can tell which task the repo does when
+ * it is not in the page's catalog. */
pipelineTag?: string | null;
/** Detected local audio architecture, used when a filesystem path has no Hub id. */
audioType?: string | null;
nativePathExpiresAtMs?: number | null;
}
-/** Full on-disk requirement for a model pick, including its checkpoint and
- * companion assets (text encoders, VAE, tokenizer/config files, etc.). */
+/** Full on-disk requirement for a model pick, including its checkpoint and companion assets
+ * (text encoders, VAE, tokenizer/config files). */
export interface ModelDownloadFootprint {
requiredBytes: number;
checkpointBytes: number;
@@ -88,16 +88,12 @@ export interface ModelPickTarget {
displayName: string;
ggufVariant?: string | null;
isGguf: boolean;
- /**
- * Whether an OpenAI-compatible request can actually load this model. Not the same as isGguf:
- * local_model_resolver skips Ollama's scanner. Defaults to isGguf when unknown.
- */
+ /** Whether an OpenAI-compatible request can actually load this model. Not the same as isGguf:
+ * local_model_resolver skips Ollama's scanner. Defaults to isGguf when unknown. */
apiLoadable?: boolean;
- /**
- * Identity the saved settings are keyed by, when that is not what loads: a repo cached
- * outside the active HF cache loads by snapshot path while its settings key on the repo
- * id. Probes that must open the model keep using `id`.
- */
+ /** Identity the saved settings are keyed by, when that is not what loads: a repo cached outside
+ * the active HF cache loads by snapshot path while its settings key on the repo id. Probes
+ * that must open the model keep using `id`. */
configId?: string;
meta: ModelSelectorChangeMeta;
}
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/variant-listing-error.ts b/studio/frontend/src/features/model-picker/components/model-selector/variant-listing-error.ts
index 7f0d9a20bd7..8420abce20a 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/variant-listing-error.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/variant-listing-error.ts
@@ -1,21 +1,18 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Read by property, not instanceof: DOMException does not inherit from Error in older
-// WebKit, so `err instanceof Error` misses exactly the aborts this needs to classify.
+// Read by property, not instanceof: DOMException does not inherit from Error in older WebKit, so
+// `err instanceof Error` misses exactly the aborts this needs to classify.
function propString(err: unknown, key: "name" | "message"): string {
if (typeof err !== "object" || err === null) return "";
const value = (err as Record<string, unknown>)[key];
return typeof value === "string" ? value : "";
}
-/**
- * Why a repo's quant listing did not arrive. A raw timeout reads as "The operation timed
- * out.", which names neither the Hub nor the retry that usually clears it. Engines without
- * `signal.reason` (Safari < 15.4, older WebKitGTK) report a plain AbortError for a timed-out
- * fetch, so that counts as a timeout too; a deliberate abort never reaches here, because the
- * expander drops its result before rendering.
- */
+/** Why a repo's quant listing did not arrive. A raw timeout reads as "The operation timed out.",
+ * which names neither the Hub nor the retry that usually clears it. Engines without
+ * `signal.reason` report a plain AbortError for a timed-out fetch, so that counts as a timeout
+ * too; a deliberate abort never reaches here, since the expander drops its result first. */
export function describeVariantListingError(err: unknown): string {
const name = propString(err, "name");
if (name === "TimeoutError" || name === "AbortError") {
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/variant-presentation.ts b/studio/frontend/src/features/model-picker/components/model-selector/variant-presentation.ts
index 67fcbeec64e..596e5c79f47 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/variant-presentation.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/variant-presentation.ts
@@ -16,14 +16,14 @@ export type GgufVariantPresentationGroup<T extends PresentableGgufVariant> = {
};
// `.gguf` optional: an H3 quant KEY is the file stem the backend keyed it by
-// (`_unknown_gguf_variant_key`), so the same parser reads both. Lazy quant group
-// so the suffix wins when present.
+// (`_unknown_gguf_variant_key`), so the same parser reads both. Lazy quant group so the suffix
+// wins when present.
const H3_FILENAME = /^minimax_h3_(fl2va|ref2va)(?:_pruned)?-(.+?)(?:\.gguf)?$/i;
const GGUF_SHARD_SUFFIX = /-\d{5}-of-\d{5}$/i;
const PATH_SEPARATOR = /[\\/]/;
-// The backend's own wording (`_apply_gguf_display_labels`), so the Hub card and a
-// row's tooltip name one checkpoint the same way.
+// The backend's own wording (`_apply_gguf_display_labels`), so the Hub card and a row's tooltip
+// name one checkpoint the same way.
const H3_WORKFLOW_LABEL = {
"text-frames": "Text & frames",
"reference-media": "References",
@@ -55,9 +55,9 @@ function h3Presentation(
return h3PresentationFor(variant.filename);
}
-/** A GGUF quant KEY as the row's mono chip: the quant alone, since that column is
- * capped at 7.2em and an H3 key is a whole file stem that clips to nonsense. The
- * workflow and build go to `ggufQuantDetailLabel`. Other quants pass through. */
+/** A GGUF quant KEY as the row's mono chip: the quant alone, since that column is capped at 7.2em
+ * and an H3 key is a whole file stem that clips to nonsense. The workflow and build go to
+ * `ggufQuantDetailLabel`. Other quants pass through. */
export function ggufQuantChipLabel(quant: string): string {
return h3PresentationFor(quant)?.quantLabel ?? quant;
}
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/variant-visibility.ts b/studio/frontend/src/features/model-picker/components/model-selector/variant-visibility.ts
index 01ca2fbbeb5..656c76cf4f0 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/variant-visibility.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/variant-visibility.ts
@@ -1,13 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// What a quantization expander shows, and when it opens. No React/DOM deps
-// so it stays easy to test.
+// What a quantization expander shows, and when it opens. No React/DOM deps so it stays easy to test.
-/** On Device with "Show all quantizations" off lists what the repo holds on
- * disk: complete quants, plus torn ones, which still occupy space and need a
- * resume. Quants that were never downloaded stay hidden. Browse lists
- * (Recommended and the rest) always show every quant. */
+/** On Device with "Show all quantizations" off lists what the repo holds on disk: complete quants,
+ * plus torn ones, which still occupy space and need a resume. Never-downloaded quants stay
+ * hidden. Browse lists always show every quant. */
export function visibleGgufVariants<
T extends { downloaded?: boolean; partial?: boolean },
>(
@@ -18,10 +16,9 @@ export function visibleGgufVariants<
return variants.filter((v) => v.downloaded === true || v.partial === true);
}
-/** Whether a row's expander should mount. Auto-expansion waits for the
- * sole-quant probe: without the wait, every On Device row opens an expander,
- * and its remote listing, moments before collapsing into a single row. A
- * row the user opened is not held back. */
+/** Whether a row's expander should mount. Auto-expansion waits for the sole-quant probe: without
+ * the wait, every On Device row opens an expander, and its remote listing, moments before
+ * collapsing into a single row. A row the user opened is not held back. */
export function shouldMountVariantExpander({
expanded,
autoExpand,
@@ -34,11 +31,10 @@ export function shouldMountVariantExpander({
return expanded && !(autoExpand && soleQuantsPending);
}
-/** Next collapse/reopen sets after a click on an auto-expanded row.
- * `showing` is what the row renders, not what the collapse set says: a row
- * held back by its sole-quant probe shows nothing, so a click opens it. A
- * reopened row stops following the auto-expand preference until it is
- * collapsed again. */
+/** Next collapse/reopen sets after a click on an auto-expanded row. `showing` is what the row
+ * renders, not what the collapse set says: a row held back by its sole-quant probe shows
+ * nothing, so a click opens it. A reopened row stops following the auto-expand preference until
+ * it is collapsed again. */
export function toggleAutoExpandedRow(
state: { collapsed: ReadonlySet<string>; reopened: ReadonlySet<string> },
{ repoId, showing }: { repoId: string; showing: boolean },
diff --git a/studio/frontend/src/features/model-picker/components/numeric-value-input.tsx b/studio/frontend/src/features/model-picker/components/numeric-value-input.tsx
index 27c823602ea..94067340fb4 100644
--- a/studio/frontend/src/features/model-picker/components/numeric-value-input.tsx
+++ b/studio/frontend/src/features/model-picker/components/numeric-value-input.tsx
@@ -75,20 +75,16 @@ export const NumericValueInput = forwardRef<
const cancelBlurCommitRef = useRef(false);
const draftRef = useRef("");
const dirtyRef = useRef(false);
- // Same-click Load: blur commits via onChange and clears dirtyRef before the
- // button onClick runs, while parent `value` is still stale. Keep the blur
- // result for one imperative commit(); clear when `value` catches up or on
- // focus / external edits (Reset, slider).
+ // Same-click Load: blur commits via onChange and clears dirtyRef before the button onClick runs,
+ // while parent `value` is still stale. Keep the blur result for one imperative commit(); clear
+ // when `value` catches up or on focus or an external edit.
const lastBlurCommittedRef = useRef<number | null>(null);
- // The blur bridge is only valid across the single synchronous gesture that set
- // it: blur commits during a button's mousedown and that button's onClick
- // consumes it via commit() before React re-renders. Any settled render means the
- // gesture is over, so drop the cache on every commit. Keying this on [value]
- // alone missed a Reset (or other external edit) that restores the shown value
- // unchanged when the blur did dispatch onChange (final !== value): value nets
- // back to its prior number, so the effect never re-ran, the stale pin survived,
- // and the next Load/Save replayed the override Reset had removed.
+ // The blur bridge is only valid across the single synchronous gesture that set it: blur commits
+ // during a button's mousedown and that button's onClick consumes it before React re-renders. Any
+ // settled render means the gesture is over, so drop the cache on every commit. Keying on
+ // [value] alone missed a Reset that restores the shown value unchanged, so value netted back to
+ // its prior number, the effect never re-ran, and the next Load replayed the removed override.
useEffect(() => {
lastBlurCommittedRef.current = null;
});
diff --git a/studio/frontend/src/features/model-picker/components/sidebar-model-config.tsx b/studio/frontend/src/features/model-picker/components/sidebar-model-config.tsx
index eeca201a34c..c8ee44b0811 100644
--- a/studio/frontend/src/features/model-picker/components/sidebar-model-config.tsx
+++ b/studio/frontend/src/features/model-picker/components/sidebar-model-config.tsx
@@ -43,9 +43,9 @@ export function SidebarModelConfig({
loadedConfig,
onReload,
}: SidebarModelConfigProps) {
- // A standalone .gguf has no quant to choose between, but the loader labels it from its filename
- // and /status echoes that back. Keying settings by it would write "<path>:Q4_K_M" while every
- // other surface uses the bare path. Same rule as settingsGgufVariantForRow.
+ // A standalone .gguf has no quant to choose between, but the loader labels it from its filename and
+ // /status echoes that back. Keying settings by it would write "<path>:Q4_K_M" while every other
+ // surface uses the bare path. Same rule as settingsGgufVariantForRow.
const settingsGgufVariant = isStandaloneGgufPath(modelId)
? null
: ggufVariant;
diff --git a/studio/frontend/src/features/model-picker/hooks/use-memory-estimate.ts b/studio/frontend/src/features/model-picker/hooks/use-memory-estimate.ts
index 9300f1df39a..8b35ad5b01e 100644
--- a/studio/frontend/src/features/model-picker/hooks/use-memory-estimate.ts
+++ b/studio/frontend/src/features/model-picker/hooks/use-memory-estimate.ts
@@ -12,21 +12,20 @@ import {
resolveTokenIdentity as tokenIdentity,
} from "../model-config/estimate-context";
-/** Long enough that a slider drag lands one request, not sixty; short enough that
- * letting go feels immediate. The fetch itself is a header walk, tens of ms. */
+/** Long enough that a slider drag lands one request, not sixty; short enough that letting go
+ * feels immediate. The fetch itself is a header walk, tens of ms. */
const ESTIMATE_DEBOUNCE_MS = 250;
export interface MemoryEstimateState {
estimate: MemoryEstimate | null;
- /** First fetch for this model, nothing to show yet. A re-price keeps the old
- * numbers up and sets `stale` instead, so the row never blinks on a slider step. */
+ /** First fetch for this model, nothing to show yet. A re-price keeps the old numbers up and sets
+ * `stale` instead, so the row never blinks on a slider step. */
loading: boolean;
/** The numbers shown are for older settings; a fresh answer is on its way. */
stale: boolean;
}
-/** Everything that changes the answer. Settings the backend ignores stay out, or
- * the row re-fetches for nothing. */
+/** Everything that changes the answer. Settings the backend ignores stay out, or the row re-fetches for nothing. */
function estimateKey(request: MemoryEstimateRequest | null): string | null {
if (!request) return null;
return JSON.stringify([
@@ -53,20 +52,16 @@ function estimateKey(request: MemoryEstimateRequest | null): string | null {
]);
}
-/**
- * Debounced memory estimate for a prospective load. Pass null to stand down.
- *
- * In-flight requests abort when the settings move again, so a slow answer for a
- * context already dragged past cannot overwrite a newer one.
- */
+/** Debounced memory estimate for a prospective load. Pass null to stand down. In-flight requests
+ * abort when the settings move again, so a slow answer for a context already dragged past
+ * cannot overwrite a newer one. */
export function useMemoryEstimate(
request: MemoryEstimateRequest | null,
): MemoryEstimateState {
const key = estimateKey(request);
- // Computed during render, not read from a ref the effect updates after paint: the
- // effect below still clears on a switch, but it runs after React has painted, so a
- // direct switch between two GGUFs showed the previous model's footprint and fit
- // colour under the new name for a frame. Both helpers are pure, so this is safe.
+ // Computed during render, not read from a ref the effect updates after paint: the effect below
+ // still clears on a switch, but it runs after React has painted, so a direct switch between
+ // two GGUFs showed the previous model's footprint under the new name for a frame.
const currentIdentity =
request == null
? null
@@ -82,14 +77,13 @@ export function useMemoryEstimate(
stale: false,
identity: null,
});
- // Read inside the effect so it depends on the key alone: `request` is a fresh
- // object every render and would restart the debounce on any keystroke.
+ // Read inside the effect so it depends on the key alone: `request` is a fresh object every
+ // render and would restart the debounce on any keystroke.
const latestRequest = useRef(request);
latestRequest.current = request;
- // Which model the numbers belong to. A switch must clear them: one model's
- // footprint under another's name is worse than none. The quantization counts as a
- // switch -- Q4_K_M to F16 on one repository leaves modelPath alone while the
- // weights quadruple -- so this is the source identity, not the path.
+ // Which model the numbers belong to. A switch must clear them: one model's footprint under
+ // another's name is worse than none. The quantization counts as a switch -- Q4_K_M to F16 on
+ // one repository leaves modelPath alone while the weights quadruple.
const shownModel = useRef<string | null>(null);
useEffect(() => {
@@ -132,8 +126,7 @@ export function useMemoryEstimate(
};
}, [key]);
- // State that belongs to a different source is not shown at all, not even for the
- // frame before the effect clears it.
+ // State that belongs to a different source is not shown at all, not even for the frame before the effect clears it.
if (state.identity !== currentIdentity) {
return { estimate: null, loading: currentIdentity != null, stale: false };
}
diff --git a/studio/frontend/src/features/model-picker/hooks/use-model-defaults.ts b/studio/frontend/src/features/model-picker/hooks/use-model-defaults.ts
index 530e6e06d8a..a2ea5c96a5c 100644
--- a/studio/frontend/src/features/model-picker/hooks/use-model-defaults.ts
+++ b/studio/frontend/src/features/model-picker/hooks/use-model-defaults.ts
@@ -54,9 +54,8 @@ export function useDefaultChatTemplate(
): DefaultChatTemplateState {
const token = useHfTokenStore((s) => s.token);
const inventoryVersion = useInventoryVersion();
- // The native token is part of the identity: a picked GGUF resolves its
- // template through the lease, not the model id, so two picks of the same
- // basename must not share a cache entry.
+ // The native token is part of the identity: a picked GGUF resolves its template through the
+ // lease, not the model id, so two picks of the same basename must not share a cache entry.
const cacheKey =
enabled && modelId
? `${modelId}::${ggufVariant ?? ""}::${token}::${inventoryVersion}::${nativePathToken ?? ""}`
@@ -82,9 +81,8 @@ export function useDefaultChatTemplate(
if (controller.signal.aborted) {
return;
}
- // Cache the terminal result, including a null "no default template",
- // so reopening the viewer for such a model reuses it instead of
- // re-running the backend/Hugging Face lookup every time.
+ // Cache the terminal result, including a null "no default template", so reopening the viewer for
+ // such a model reuses it instead of re-running the backend or Hugging Face lookup.
cacheTemplate(cacheKey, template);
setFetched({
key: cacheKey,
diff --git a/studio/frontend/src/features/model-picker/inventory/use-chat-picker-inventory.ts b/studio/frontend/src/features/model-picker/inventory/use-chat-picker-inventory.ts
index 9d7764916c5..954db47d4db 100644
--- a/studio/frontend/src/features/model-picker/inventory/use-chat-picker-inventory.ts
+++ b/studio/frontend/src/features/model-picker/inventory/use-chat-picker-inventory.ts
@@ -68,7 +68,8 @@ function toCachedModelRepo(row: CachedInventoryRow): CachedModelRepo {
audio_type: row.audioType ?? null,
tags: row.tags,
library_name: row.libraryName,
- // Carried through: the diffusion picker drops single-file checkpoint repos (loading one as a pipeline fails after the handoff), and undefined reads as "full pipeline".
+ // Carried through: the diffusion picker drops single-file checkpoint repos, since loading one as
+ // a pipeline fails after the handoff, and undefined reads as "full pipeline".
single_file: row.singleFile ?? false,
};
}
@@ -151,9 +152,11 @@ export function useChatPickerInventory(
.filter(
(row) =>
PICKER_LOCAL_SOURCES.has(row.source) &&
- // Skip non-chat rows (a folder with only config.json classifies "unknown" -> canChat false); selecting one would load a weightless path.
- // toLocalModelInfo drops capabilities, so this is the only place the guard can live. A row the backend classified as a generation task is
- // exempt: canChat is about the chat loader, and dropping it here hid every on-device diffusion model from the pickers that CAN load it.
+ // Skip non-chat rows (a folder with only config.json classifies "unknown"); selecting one would
+ // load a weightless path. toLocalModelInfo drops capabilities, so this is the only place the
+ // guard can live. A row the backend classified as a generation task is exempt: canChat is about
+ // the chat loader, and dropping it here hid every on-device diffusion model from the pickers
+ // that CAN load it.
(row.capabilities.canChat ||
studioPageForTask(row.task) !== undefined) &&
(!isHiddenModelId(row.modelId, row.repoId, row.path) ||
diff --git a/studio/frontend/src/features/model-picker/model-config/apply-per-model-config.ts b/studio/frontend/src/features/model-picker/model-config/apply-per-model-config.ts
index 647c1edf787..c312edae8fc 100644
--- a/studio/frontend/src/features/model-picker/model-config/apply-per-model-config.ts
+++ b/studio/frontend/src/features/model-picker/model-config/apply-per-model-config.ts
@@ -28,10 +28,9 @@ export function applyPerModelConfigToRuntime(
config: PerModelConfig,
options: { isDiffusion?: boolean } = {},
): void {
- // Fall back to the standing default when the model has no saved
- // maxSeqLength. maxSeqLength is the only per-model field carried on
- // params (the rest are reset below), so without this a model with no
- // remembered config would inherit the previously loaded model's value.
+ // Fall back to the standing default when the model has no saved maxSeqLength. It is the only
+ // per-model field carried on params, so without this a model with no remembered config would
+ // inherit the previously loaded model's value.
const maxSeqLength =
normalizeMaxSeqLength(config.maxSeqLength) ??
defaultInferenceParams.maxSeqLength;
@@ -60,31 +59,26 @@ export function applyPerModelConfigToRuntime(
// the diffusion runner ignores the llama-server batch flags
nBatch: options.isDiffusion ? null : (config.nBatch ?? null),
nUbatch: options.isDiffusion ? null : (config.nUbatch ?? null),
- // Same reason as the batch flags: these are llama-server's own, and the
- // diffusion runner never launches one.
+ // Same reason as the batch flags: these are llama-server's own, and the diffusion runner never launches one.
loadMode: options.isDiffusion ? null : (config.loadMode ?? null),
ctxCheckpoints: options.isDiffusion ? null : (config.ctxCheckpoints ?? null),
cacheRam: options.isDiffusion ? null : (config.cacheRam ?? null),
tensorParallel: options.isDiffusion
? false
: (config.tensorParallel ?? false),
- // The diffusion runner has no projector to skip, so the toggle is inert
- // there for the same reason tensorParallel is.
+ // The diffusion runner has no projector to skip, so the toggle is inert there for the same
+ // reason tensorParallel is.
disableVision: options.isDiffusion
? false
: (config.disableVision ?? false),
chatTemplateOverride: cleanTemplate(config.chatTemplateOverride),
- // GPU Memory knobs are per-model (GGUF-only). Absent = defaults; the mode is
- // a standing preference so an absent mode falls back to the persisted one.
- // The per-GPU split ratio is never remembered, so it always resets. The GPU
- // pick is reconciled against the GPUs present now (a saved [1] on a 1-GPU
- // host would otherwise be sent and rejected).
- // A diffusion config is sanitized to gpuMemoryMode "auto" because the mode
- // does not apply to it, not because the user chose Auto. Writing that into
- // the live standing preference would strand the session on Auto: the load
- // itself skips saveGpuMemoryMode for diffusion, so nothing restores it, and
- // the next ordinary GGUF loaded without its own config sends this value and
- // persists it over the user's Manual. Keep the standing choice instead.
+ // GPU Memory knobs are per-model (GGUF-only). Absent = defaults; the mode is a standing
+ // preference so an absent mode falls back to the persisted one. The per-GPU split ratio is
+ // never remembered. The GPU pick is reconciled against the GPUs present now. A diffusion
+ // config is sanitized to gpuMemoryMode "auto" because the mode does not apply, not because
+ // the user chose Auto: writing that into the live standing preference would strand the session
+ // on Auto, since the load skips saveGpuMemoryMode for diffusion and the next ordinary GGUF
+ // would persist it over the user's Manual.
gpuMemoryMode: options.isDiffusion
? readPersistedGpuMemoryMode()
: (config.gpuMemoryMode ?? readPersistedGpuMemoryMode()),
@@ -128,9 +122,8 @@ export function currentRuntimePerModelConfig(
tensorParallel: s.tensorParallel ?? false,
disableVision: s.disableVision ?? false,
chatTemplateOverride: cleanTemplate(s.chatTemplateOverride),
- // Snapshot the live GPU knobs too so a failed switch rolls the previous
- // model's GPU Memory settings back (see applyPerModelConfigToRuntime). The
- // split ratio is intentionally never remembered.
+ // Snapshot the live GPU knobs too so a failed switch rolls the previous model's GPU Memory
+ // settings back. The split ratio is intentionally never remembered.
gpuMemoryMode: s.gpuMemoryMode,
gpuLayers: s.gpuLayers,
nCpuMoe: s.nCpuMoe,
@@ -168,11 +161,9 @@ export function perModelConfigsEqual(
);
}
-/**
- * Compare on the launched command, so "not loaded" and "cleared" are equal here.
- * They differ only in what a SAVE does, and treating them as different would make
- * the row read as an unsaved change the moment it finished reading the server.
- */
+/** Compare on the launched command, so "not loaded" and "cleared" are equal here. They differ
+ * only in what a SAVE does, and treating them as different would make the row read as an
+ * unsaved change the moment it finished reading the server. */
function extraArgsSignature(value: string[] | null | undefined): string {
return (value ?? []).join("\u0000");
}
diff --git a/studio/frontend/src/features/model-picker/model-config/config-signature.ts b/studio/frontend/src/features/model-picker/model-config/config-signature.ts
index 65b4c06b505..de2cffc4d72 100644
--- a/studio/frontend/src/features/model-picker/model-config/config-signature.ts
+++ b/studio/frontend/src/features/model-picker/model-config/config-signature.ts
@@ -3,14 +3,13 @@
// Identity of one ModelConfigPage editor instance. The page seeds its state from `loadedConfig`
// in a useState initializer, so it reads that prop once per mount: a host that opens it before
-// status hydrates gets null first and the live config a moment later, and without the config in
-// the React key the instance survives that flip and keeps showing saved values, which Apply
-// then writes back over the running model.
+// status hydrates gets null first and the live config a moment later, and without the config
+// in the React key the instance keeps showing saved values, which Apply then writes back.
import type { PerModelConfig } from "./per-model-config";
-// Serialize the GPU knobs with the store's "absent == default" coalescing: mode auto,
-// gpuLayers Auto (< 0), nCpuMoe 0, and null / absent GPU picks as automatic.
+// Serialize the GPU knobs with the store's "absent == default" coalescing: mode auto, gpuLayers
+// Auto (< 0), nCpuMoe 0, and null or absent GPU picks as automatic.
export function gpuFieldsSignature(config: PerModelConfig): string {
const gpuSelection =
config.selectedGpuIds == null
@@ -37,10 +36,8 @@ function hashString(value: string): number {
return hash;
}
-/**
- * Signature of the live config an editor was seeded from. `null` (not resident, or status has
- * not answered yet) is its own value: the arrival of the live config must remount.
- */
+/** Signature of the live config an editor was seeded from. `null` (not resident, or status has
+ * not answered yet) is its own value: the arrival of the live config must remount. */
export function loadedConfigSignature(
config: PerModelConfig | null | undefined,
): string {
@@ -70,10 +67,8 @@ export function loadedConfigSignature(
].join("|");
}
-/**
- * React key for one ModelConfigPage instance, so every host agrees on when the editor
- * re-seeds: a different model, a different quant, or a change in the live config.
- */
+/** React key for one ModelConfigPage instance, so every host agrees on when the editor re-seeds:
+ * a different model, a different quant, or a change in the live config. */
export function modelConfigInstanceKey(
modelId: string,
ggufVariant: string | null | undefined,
diff --git a/studio/frontend/src/features/model-picker/model-config/estimate-context.ts b/studio/frontend/src/features/model-picker/model-config/estimate-context.ts
index a5d1b32cc23..b91a180072d 100644
--- a/studio/frontend/src/features/model-picker/model-config/estimate-context.ts
+++ b/studio/frontend/src/features/model-picker/model-config/estimate-context.ts
@@ -1,73 +1,41 @@
// 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 context the memory estimate should PRICE, which is not always the one the
- * Context Length control DISPLAYS.
- *
- * The control must show a number before a new GGUF's header has been read and falls
- * back to 32,768. The Load button does not: an unset length is sent as 0 and llama.cpp
- * fits, or opens at the model's native context. Pricing the displayed fallback quotes
- * an explicit 32k for a load that may open at 262k, understating the KV cache -- the
- * term that grows fastest with context. 0 is the request's own "price the native
- * context", resolved from the header exactly as the launch would.
- *
- * The fallbacks are `resolveLoadMaxSeqLength`'s, in its order and no further: an
- * explicit length is itself, reloading the resident GGUF keeps the context it is
- * resident AT, every other GGUF case is 0. The native context is deliberately NOT a
- * fallback -- --fit can land below it, so quoting it claims an outcome the load has
- * not reached.
- *
- * Import-free so `tests/` can load it under `node --experimental-strip-types`, which
- * does not resolve the `@/` alias.
- */
+/** The context the memory estimate should PRICE, which is not always the one the Context Length
+ * control DISPLAYS. The control must show a number before a new GGUF's header has been read and
+ * falls back to 32,768; the Load button does not, sending an unset length as 0 so llama.cpp
+ * fits or opens at the native context. Pricing the displayed fallback quotes an explicit 32k
+ * for a load that may open at 262k, understating the KV cache. The fallbacks are
+ * `resolveLoadMaxSeqLength`'s, in its order and no further; the native context is deliberately
+ * NOT one, since --fit can land below it. Import-free so `tests/` can load it under
+ * `node --experimental-strip-types`, which does not resolve the `@/` alias. */
export function resolveEstimateContext(
customContextLength: number | null,
activeLoadedContext: number | null,
skipResidentFallback = false,
): number {
if (skipResidentFallback) {
- // Two shapes reach here, and `resolveLoadMaxSeqLength` answers 0 for both before
- // considering the resident context: Manual mode with GPU Layers on Auto, where
- // --fit owns the sizing, and a builtin-default preset on a GGUF load. In both,
- // pricing what is loaded RIGHT NOW quotes the OLD fit -- precisely when a
- // context-sensitive setting has just changed and the next fit lands elsewhere.
+ // Two shapes reach here, and `resolveLoadMaxSeqLength` answers 0 for both before considering the
+ // resident context: Manual mode with GPU Layers on Auto, where --fit owns the sizing, and a
+ // builtin-default preset on a GGUF load. In both, pricing what is loaded RIGHT NOW quotes the
+ // OLD fit, precisely when a context-sensitive setting has just changed.
return customContextLength && customContextLength > 0 ? customContextLength : 0;
}
return customContextLength ?? activeLoadedContext ?? 0;
}
-/**
- * Which MODEL an estimate belongs to, for deciding whether shown numbers still do.
- *
- * Not the same question as "did anything change" -- that is the full request key,
- * and a slider step must keep the old figures up and mark them stale rather than
- * blank the row. This is the narrower one: a change here means the numbers on
- * screen describe a DIFFERENT file, so they have to go, not go grey.
- *
- * The quantization is part of it, which is the whole reason this exists. Switching
- * Q4_K_M to F16 on the same repository leaves `modelPath` untouched while the
- * weights roughly quadruple, so keying on the path alone left one quant's footprint
- * displayed under another's name -- the exact thing the caller says must not happen.
- * The token identity and the native path token are here for the same reason: both
- * select which file the backend resolves.
- */
-/**
- * Identity of a token without the token.
- *
- * The backend resolves and caches gated repositories per token, so swapping one for
- * another has to re-fetch, but the credential itself has no business sitting in a
- * React dependency key.
- *
- * djb2, because this only has to separate two strings held in this tab, never to
- * resist anyone, and it has to be synchronous (SubtleCrypto is not). It is 32 bits, so
- * a collision is constructible: two colliding credentials would leave one's estimate
- * on screen under the other, because the same value keys both the refetch and the
- * blank-the-row check. The bound is the number of tokens compared IN ONE TAB, which is
- * two -- the one that was set and the one that replaced it -- so the odds are ~2^-32
- * per swap and the consequence is a stale byte count on a row already labelled Beta,
- * not an incorrect load. Widening it is not worth a second hash; noting it is.
- */
+/** Which MODEL an estimate belongs to, for deciding whether shown numbers still do. Not the same
+ * question as "did anything change" -- that is the full request key, and a slider step must
+ * keep the old figures up and mark them stale. A change here means the numbers describe a
+ * DIFFERENT file, so they have to go. The quantization is part of it: switching Q4_K_M to F16
+ * on one repository leaves `modelPath` untouched while the weights roughly quadruple. The
+ * token identity and native path token are here for the same reason. */
+/** Identity of a token without the token. The backend resolves and caches gated repositories per
+ * token, so swapping one has to re-fetch, but the credential itself has no business in a React
+ * dependency key. djb2, because this only has to separate two strings held in this tab and has
+ * to be synchronous. It is 32 bits, so a collision is constructible, but the bound is the
+ * number of tokens compared IN ONE TAB, which is two, so the odds are ~2^-32 per swap and the
+ * consequence is a stale byte count on a Beta row, not an incorrect load. */
export function resolveTokenIdentity(
token: string | null | undefined,
): string {
diff --git a/studio/frontend/src/features/model-picker/model-config/llama-extra-args.ts b/studio/frontend/src/features/model-picker/model-config/llama-extra-args.ts
index 29e6b494554..27994e58bf2 100644
--- a/studio/frontend/src/features/model-picker/model-config/llama-extra-args.ts
+++ b/studio/frontend/src/features/model-picker/model-config/llama-extra-args.ts
@@ -1,19 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * Turning what the user types into the argv tokens the API takes.
- *
- * `LoadRequest.llama_extra_args` is a list with one argv token per entry, which is
- * what the CLI already sends and what the overrides table already stores. The
- * single string only ever lives in the control, so the split happens here.
- *
- * Deliberately not a shell: there is no `sh -c` anywhere on this path (the child is
- * spawned from a list), so expanding `$HOME`, globbing, or honouring `;` and `|`
- * would invent meanings the backend does not implement. Quotes and backslashes are
- * handled because a chat template or a grammar genuinely needs a space inside one
- * token; everything else is a literal character.
- */
+/** Turn what the user types into the argv tokens the API takes: `LoadRequest.llama_extra_args` is
+ * one token per entry, so the single string lives only in the control. Deliberately not a
+ * shell, since nothing runs `sh -c` on this path; quotes and backslashes are handled only
+ * because a chat template or grammar needs a space inside one token. */
// Hoisted: these run per token, and biome flags a literal in a hot path.
const NEEDS_QUOTING = /[\s"'\\]/;
@@ -24,26 +15,14 @@ const UNDERSCORE = /_/g;
// The same rule as CONTROL_IN_ARGV below, and as the backend's own check.
const CONTROL_CHARACTERS = /[\u0000-\u0008\u000b-\u001f]/;
const INTEGER = /^-?[0-9]+$/;
-/**
- * A NUL, or any other C0 control except tab and newline.
- *
- * The two exceptions are the backend's, from _has_control_characters, and they are
- * not an oversight there: a grammar, a JSON schema or a chat template is routinely
- * multi-line, and quoting one into a single argv token is exactly what the box is
- * for. Refusing those here would disable Load over a value the launch accepts.
- */
+/** A NUL, or any other C0 control except tab and newline. The two exceptions are the backend's,
+ * from _has_control_characters: a grammar or chat template is routinely multi-line. */
// biome-ignore lint/suspicious/noControlCharactersInRegex: that is exactly what this finds
const CONTROL_IN_ARGV = /[\u0000-\u0008\u000B-\u001F]/;
-/**
- * Whether this token is something execve could not carry.
- *
- * The surrogate half is checked by walking the string rather than with a character
- * class, because a class matches BOTH units of a well-formed pair: an emoji in a
- * chat template or a grammar is a pair, Python encodes it without complaint, and
- * treating it as unusable would quietly drop a valid argument. Only an UNPAIRED
- * surrogate is refused, which is the one Popen raises on while encoding argv.
- */
+/** Whether this token is something execve could not carry. The surrogate half is found by walking
+ * the string rather than with a character class, since a class matches BOTH units of a
+ * well-formed pair. Only an UNPAIRED surrogate is refused, which is what Popen raises on. */
function isUnusableInArgv(token: string): boolean {
if (CONTROL_IN_ARGV.test(token)) {
return true;
@@ -73,15 +52,9 @@ export const EXTRA_ARGS_MAX_TOKENS = 256;
/** The one option in llama-server's help that takes two values. */
const TWO_VALUE_FLAGS = new Set(["--control-vector-layer-range"]);
-/**
- * Options that take a second value on SOME builds.
- *
- * Today's llama.cpp writes the scale into the value ("--lora-scaled FNAME:SCALE"),
- * older ones took it as a separate token ("--lora-scaled FNAME SCALE"), and
- * _sidecar_weight_files already reads both. So the second token is allowed but never
- * required: demanding it would refuse the current syntax, and refusing it broke a
- * list that loaded before the positional check existed.
- */
+/** Options that take a second value on SOME builds. Today's llama.cpp writes the scale into the
+ * value ("--lora-scaled FNAME:SCALE") while older ones took a separate token, and
+ * _sidecar_weight_files reads both, so the second token is allowed but never required. */
const OPTIONAL_SECOND_VALUE_FLAGS = new Set([
"--lora-scaled",
"--control-vector-scaled",
@@ -91,44 +64,27 @@ export type ExtraArgsParse = {
tokens: string[];
/** Set when a quote is left open, so the row can say so instead of silently dropping it. */
unterminatedQuote: '"' | "'" | null;
- /**
- * Indices of tokens the user QUOTED, which argv cannot record.
- *
- * `--chat-template '- hello'` is one option and its value, but the quotes are gone
- * by the time it is a token list, and "- hello" is flag-shaped, so the row called
- * the option's value missing and disabled Load over a list the backend accepts and
- * llama.cpp reads correctly (it takes the next argv element whatever it starts
- * with). Only the editor can tell the two apart, and only while the text is still
- * text, so the marks are recorded here and used by the diagnostics.
- */
+ /** Indices of tokens the user QUOTED, which argv cannot record. `--chat-template '- hello'` is one
+ * option and its value, but the quotes are gone by the time it is a token list and "- hello"
+ * is flag-shaped, so the row called the value missing. Only the editor can tell them apart. */
quotedIndices: ReadonlySet<number>;
};
-/**
- * A stored list with the flags this build refuses removed, values and all.
- *
- * The panel hydrates from the stored override and then sends what it holds as an
- * explicit list, which /load validates strictly rather than putting through the
- * carry-over paths that drop such a flag quietly. An install upgraded across a
- * denylist change would therefore fail to load a model that worked the day before.
- * Mirrors drop_managed_flags: a flag takes its value with it, or llama-server reads
- * the orphan as a positional model path.
- */
-/**
- * What ``subprocess.list2cmdline`` would put on a Windows command line for these.
- *
- * A port of CPython's rule, because that is the exact serializer Popen uses there
- * and it is not a sum of lengths: an argument needing quotes has each backslash run
- * before a quote doubled, so an escape-heavy grammar can grow by half again.
- */
+/** A stored list with the flags this build refuses removed, values and all. The panel sends what
+ * it holds as an explicit list, which /load validates strictly, so an install upgraded across
+ * a denylist change would fail to load a model that worked yesterday. Mirrors
+ * drop_managed_flags: a flag takes its value, or the orphan reads as a model path. */
+/** What `subprocess.list2cmdline` would put on a Windows command line for these. A port of
+ * CPython's rule, because that is the serializer Popen uses and it is not a sum of lengths:
+ * an argument needing quotes has each backslash run before a quote doubled. */
export function windowsCommandLength(tokens: readonly string[]): number {
const result: string[] = [];
for (const token of tokens) {
if (result.length > 0) {
result.push(" ");
}
- // Quoted only for whitespace or emptiness, as CPython does: a token that merely
- // contains a quote is not quoted, though its backslashes still double.
+ // Quoted only for whitespace or emptiness, as CPython does: a token that merely contains a quote
+ // is not quoted, though its backslashes still double.
const needQuote = token.includes(" ") || token.includes("\t") || token === "";
if (needQuote) {
result.push('"');
@@ -165,15 +121,10 @@ export function windowsCommandLength(tokens: readonly string[]): number {
return result.join("").length;
}
-/**
- * Whether this token carries its own value instead of expecting the next one.
- *
- * Not "the name changed": extraArgFlagName also folds llama.cpp's underscore
- * spelling, and the binary takes both, so comparing the folded name against the raw
- * token read "--ctx_size 4096" as attached and then called the 4096 a bare value.
- * Only "=" and an attached short like -np8 are values in the same token. Mirrors
- * _value_is_attached in llama_server_args.py.
- */
+/** Whether this token carries its own value instead of expecting the next one. Not "the name
+ * changed": extraArgFlagName also folds llama.cpp's underscore spelling, so comparing the
+ * folded name against the raw token read "--ctx_size 4096" as attached. Only "=" and an
+ * attached short like -np8 are values in the same token. Mirrors _value_is_attached. */
function valueIsAttached(token: string, flag: string): boolean {
const raw = token.trim();
if (raw.includes("=")) {
@@ -194,19 +145,10 @@ function takesNextToken(
return next !== undefined && extraArgFlagName(next) === null;
}
-/**
- * A stored list reduced to what THIS build would accept.
- *
- * Mirrors drop_managed_flags: denied flags go, tokens carrying control characters or
- * unpaired surrogates go, anything past the size bounds goes, and a flag never
- * outlives the value that went with it (an orphaned value is a bare positional,
- * which llama-server reads as the model path).
- *
- * The panel needs this because hydrating turns a stored list into an EXPLICIT
- * request, which /load validates strictly instead of putting it through the very
- * carry-over paths that exist to drop such a token quietly. Without it, an install
- * upgraded across any of those rules stops loading a model that worked yesterday.
- */
+/** A stored list reduced to what THIS build would accept. Mirrors drop_managed_flags: denied flags
+ * go, tokens carrying control characters or unpaired surrogates go, anything past the size
+ * bounds goes, and a flag never outlives its value (an orphan reads as the model path). The
+ * panel needs this because hydrating turns a stored list into an EXPLICIT request. */
export function sanitizeStoredExtraArgs(
tokens: readonly string[],
managed: ReadonlySet<string>,
@@ -248,17 +190,12 @@ export function sanitizeStoredExtraArgs(
}
kept.push(token);
}
- // Then the shapes validate_extra_args refuses outright, whatever their size: a
- // token belonging to no flag, and a two-value option left half-written. The backend
- // sheds both because its own trimming re-validates after every cut; this mirror
- // trims by size alone, so it has to know the same rules.
+ // Then the shapes validate_extra_args refuses outright, whatever their size: a token belonging to
+ // no flag, and a two-value option left half-written. The backend re-validates after every
+ // cut; this mirror trims by size alone, so it has to know the same rules.
let bounded = dropUnvalidatableTokens(dropUnusableValues(kept));
- // Then the bounds, shed from the tail, never leaving a flag without its value.
- //
- // The host's own limits when the caller has them: a Windows install takes 24 KiB,
- // not 32, and holds a quoted-command budget besides. Trimming to the wider constant
- // and sending the result is a 400 on the load the hydration was meant to enable,
- // which is the failure this sanitising exists to avoid.
+ // Then the bounds, shed from the tail, never leaving a flag without its value. The host's own
+ // limits when the caller has them: a Windows install takes 24 KiB, not 32.
const maxBytes = limits?.maxBytes || EXTRA_ARGS_MAX_BYTES;
const commandBudget = limits?.windowsCommandBudget ?? 0;
const overBounds = (): boolean =>
@@ -269,31 +206,23 @@ export function sanitizeStoredExtraArgs(
bounded.pop();
const last = bounded[bounded.length - 1];
const lastFlag = last === undefined ? null : extraArgFlagName(last);
- // Any flag whose value has just gone, not only one spelled exactly as it
- // normalizes: extraArgFlagName folds llama.cpp's underscores, so comparing the
- // normalized name against the raw token left "--grammar_file" standing after its
- // value was trimmed. The backend cannot infer an ordinary flag's arity, so the
- // orphan reaches llama-server and the load fails after the teardown.
+ // Any flag whose value has just gone, not only one spelled exactly as it normalizes:
+ // extraArgFlagName folds llama.cpp's underscores, so comparing the normalized name against
+ // the raw token left "--grammar_file" standing after its value was trimmed.
if (last !== undefined && lastFlag !== null && !valueIsAttached(last, lastFlag)) {
bounded.pop();
}
- // Re-applied after every cut, exactly as the backend re-validates after its own:
- // shedding one value of a two-value option leaves the other looking ordinary,
- // whether the first was written attached or not.
+ // Re-applied after every cut, exactly as the backend re-validates after its own: shedding one
+ // value of a two-value option leaves the other looking ordinary.
bounded = dropUnvalidatableTokens(dropUnusableValues(bounded));
}
return bounded;
}
-/**
- * A list with the flags whose VALUE the backend's own parsers refuse removed.
- *
- * The same list of flags the row reports on, and the same rules: parse_ctx_override
- * and its siblings raise on a missing or unreadable value, which is a 400 rather than
- * a note. drop_managed_flags repairs these too, by shedding its tail until the whole
- * list validates, which costs whatever followed them; removing just the option and
- * its value keeps the rest of a legacy list working.
- */
+/** A list with the flags whose VALUE the backend's own parsers refuse removed. parse_ctx_override
+ * and its siblings raise on a missing or unreadable value, which is a 400 rather than a note.
+ * drop_managed_flags repairs these by shedding its tail, which costs whatever followed;
+ * removing just the option and its value keeps the rest of a legacy list working. */
function dropUnusableValues(tokens: readonly string[]): string[] {
const out: string[] = [];
let skipNext = false;
@@ -327,28 +256,23 @@ function dropUnusableValues(tokens: readonly string[]): string[] {
out.push(token);
continue;
}
- // The value goes with the flag, or it is left as a bare token, which is the
- // one thing llama-server would read as a model path.
+ // The value goes with the flag, or it is left as a bare token, which is the one thing llama-server
+ // would read as a model path.
skipNext = !attached && !missing;
}
return out;
}
-/**
- * A list reduced to what validate_extra_args would accept on shape alone.
- *
- * Two rules, both of which the backend enforces by re-validating after each cut it
- * makes: a bare token belonging to no flag is refused outright (llama-server reads a
- * positional as the model path, which is what the -m denial exists to prevent), and a
- * two-value option needs both of its values. Everything else is left alone, because
- * this side does not know the arity of an ordinary flag either.
- */
+/** A list reduced to what validate_extra_args would accept on shape alone. Two rules, both
+ * enforced by re-validating after each cut: a bare token belonging to no flag is refused
+ * (llama-server reads a positional as the model path), and a two-value option needs both.
+ * Everything else is left alone, since this side does not know an ordinary flag's arity. */
function dropUnvalidatableTokens(tokens: readonly string[]): string[] {
const out: string[] = [];
let pending = 0;
let twoValuePending = 0;
- // Values still owed to a flag that was itself dropped, so they go rather than
- // being kept as tokens belonging to nothing.
+ // Values still owed to a flag that was itself dropped, so they go rather than being kept as
+ // tokens belonging to nothing.
let droppedOwes = 0;
// Where the incomplete two-value option starts, so the whole of it can go.
let ownerAt = -1;
@@ -361,8 +285,7 @@ function dropUnvalidatableTokens(tokens: readonly string[]): string[] {
droppedOwes = 0;
if (flag === null) {
if (pending <= 0) {
- // Ownerless. Dropped rather than kept, which is what drop_managed_flags
- // does with the same token.
+ // Ownerless. Dropped rather than kept, which is what drop_managed_flags does with the same token.
continue;
}
pending -= 1;
@@ -376,24 +299,20 @@ function dropUnvalidatableTokens(tokens: readonly string[]): string[] {
continue;
}
if (twoValuePending > 0 && ownerAt >= 0) {
- // A new flag arrived while the option still owed a value: the whole option
- // goes, along with the value it did get.
+ // A new flag arrived while the option still owed a value: the whole option goes, along with the value it did get.
out.length = ownerAt;
}
if (token.includes("=")) {
- // llama.cpp looks the whole token up in its option map, so "--top-k=20" is an
- // argument it has never heard of and validate_extra_args refuses it. A stored
- // list holding one would 400 the load this hydration exists to enable; the
- // value is inside the token, so nothing follows it out.
+ // llama.cpp looks the whole token up in its option map, so "--top-k=20" is an argument it has
+ // never heard of and validate_extra_args refuses it. The value is inside the token.
pending = 0;
twoValuePending = 0;
ownerAt = -1;
continue;
}
if (token !== token.trim()) {
- // Refused for the same reason: the padding is part of the token llama.cpp
- // looks up, so a quoted "--top-k " never arrives as a flag. Its value goes
- // with it, or the orphan is left behind as a bare positional.
+ // Refused for the same reason: the padding is part of the token llama.cpp looks up, so a quoted
+ // "--top-k " never arrives as a flag. Its value goes with it, or the orphan is positional.
droppedOwes = valueIsAttached(token, flag) ? 0 : 1;
pending = 0;
twoValuePending = 0;
@@ -446,12 +365,8 @@ export function dropManagedExtraArgs(
return kept;
}
-/**
- * Split a command-line fragment into argv tokens.
- *
- * Newlines are separators like spaces, so a multi-line box reads as one command and
- * a user can put each flag on its own line.
- */
+/** Split a command-line fragment into argv tokens. Newlines are separators like spaces, so a
+ * multi-line box reads as one command and a user can put each flag on its own line. */
export function parseExtraArgs(input: string): ExtraArgsParse {
const tokens: string[] = [];
const quotedIndices = new Set<number>();
@@ -480,8 +395,8 @@ export function parseExtraArgs(input: string): ExtraArgsParse {
continue;
}
- // A backslash escapes the next character, but only where a shell would: inside
- // single quotes it is literal, which is what makes '\d' usable in a grammar.
+ // A backslash escapes the next character, but only where a shell would: inside single quotes it
+ // is literal, which is what makes '\d' usable in a grammar.
if (ch === "\\" && quote !== "'" && i + 1 < input.length) {
const next = input[i + 1];
// Inside double quotes only these are escapes; elsewhere the backslash stands.
@@ -490,11 +405,9 @@ export function parseExtraArgs(input: string): ExtraArgsParse {
started = true;
continue;
}
- // A backslash-newline is a line continuation, so pasting a wrapped command
- // works. It contributes nothing, so `started` is left as it was: setting it
- // would make the indentation on the next line close an empty token, and a
- // wrapped command indented under its first line (the usual shape) would send
- // an empty positional argument that llama-server reads as a model path.
+ // A backslash-newline is a line continuation, so pasting a wrapped command works. It contributes
+ // nothing, so `started` is left as it was: setting it would make the next line's indentation
+ // close an empty token and send an empty positional argument.
if (next === "\n") {
i += 1;
continue;
@@ -531,13 +444,9 @@ export function parseExtraArgs(input: string): ExtraArgsParse {
return { tokens, unterminatedQuote: quote, quotedIndices };
}
-/**
- * Render tokens back into one editable line.
- *
- * Round-tripping matters: the stored value is a token list, so this is what the box
- * shows when the panel reopens. Quote only what has to be quoted, or every reopen
- * would add another layer of escaping to the user's own text.
- */
+/** Render tokens back into one editable line. Round-tripping matters: the stored value is a token
+ * list, so this is what the box shows when the panel reopens. Quote only what has to be
+ * quoted, or every reopen would add another layer of escaping. */
export function formatExtraArgs(
tokens: readonly string[] | null | undefined,
): string {
@@ -552,8 +461,8 @@ export function formatExtraArgs(
if (!NEEDS_QUOTING.test(token)) {
return token;
}
- // Single quotes unless the token contains one, since they escape nothing and
- // leave a grammar or a template readable.
+ // Single quotes unless the token contains one, since they escape nothing and leave a grammar or a
+ // template readable.
if (!token.includes("'")) {
return `'${token}'`;
}
@@ -576,8 +485,8 @@ export function extraArgFlagName(token: string): string | null {
if (name.startsWith("--")) {
name = name.replace(UNDERSCORE, "-");
}
- // Attached `-np8` normalises to `-np`, or a denied flag slips through glued to
- // its value. Mirrors the same branch in _flag_name.
+ // Attached `-np8` normalises to `-np`, or a denied flag slips through glued to its value. Mirrors
+ // the same branch in _flag_name.
if (name.length > 3 && name.startsWith("-np")) {
const suffix = name.slice(3);
if (
@@ -602,43 +511,30 @@ export function extraArgFlags(tokens: readonly string[]): string[] {
return [...seen];
}
-// --- diagnostics ------------------------------------------------------------
-// Kept in this file rather than beside it: the node test harness resolves value
-// imports at runtime with no bundler, so a tested helper importing a sibling by
-// extensionless path cannot load. Every other tested module here is self-contained
-// for the same reason.
+// Kept in this file rather than beside it: the node test harness resolves value imports at
+// runtime with no bundler, so a tested helper importing a sibling by path cannot load.
import type { LlamaFlagCatalog } from "../api/llama-flags";
-/**
- * What to tell the user about what they typed, before the load tries it.
- *
- * Three levels, and the difference is the whole point of the row: an `error` is
- * refused by the backend so the load cannot start, a `warning` is allowed through
- * because we may simply be unable to verify it, and a `note` is correct usage worth
- * stating out loud.
- */
+/** What to tell the user about what they typed, before the load tries it. Three levels: an `error`
+ * is refused by the backend so the load cannot start, a `warning` is allowed through because
+ * we may be unable to verify it, and a `note` is correct usage worth stating. */
export type ExtraArgsDiagnostic = {
level: "error" | "warning" | "note";
message: string;
};
-/**
- * Managed flags whose supported replacement is a control in this panel. This stays
- * available while the async flag catalogue is loading or unavailable: the backend
- * denies these unconditionally, so they must never fall through to the "wins" note.
- */
+/** Managed flags whose supported replacement is a control in this panel. Available while the async
+ * flag catalogue is loading: the backend denies these unconditionally, so they must never
+ * fall through to the "wins" note. */
const MANAGED_CONTROL_FLAGS: Record<string, string> = {
"--parallel": "Parallel Slots",
"--n-parallel": "Parallel Slots",
"-np": "Parallel Slots",
};
-/**
- * Pass-through flags that a control in this panel also emits. The backend appends
- * these last and reconciles the ones that affect its sizing, so the mapping explains
- * which value wins.
- */
+/** Pass-through flags that a control in this panel also emits. The backend appends these last and
+ * reconciles the ones that affect its sizing, so the mapping explains which value wins. */
const CONTROL_OWNED_FLAGS: Record<string, string> = {
"--ctx-size": "Context Length",
"-c": "Context Length",
@@ -663,8 +559,8 @@ const CONTROL_OWNED_FLAGS: Record<string, string> = {
"--chat-template-file": "Chat Template",
"--load-mode": "Mmap/Mlock",
"-lm": "Mmap/Mlock",
- // Both halves, since the control sets one dtype for the pair, and both
- // spellings, since which one a build understands is a version question.
+ // Both halves, since the control sets one dtype for the pair, and both spellings, since which one
+ // a build understands is a version question.
"--spec-draft-type-k": "Spec Decoding KV Cache Dtype",
"-ctkd": "Spec Decoding KV Cache Dtype",
"--cache-type-k-draft": "Spec Decoding KV Cache Dtype",
@@ -679,11 +575,8 @@ const CONTROL_OWNED_FLAGS: Record<string, string> = {
"-cram": "Cache RAM",
};
-/**
- * Flags the launch REMOVES when the GPU picker owns placement. Not a shadow the user
- * wins: `_strip_device_extra_args` deletes these from the command whenever gpu_ids is
- * set, so telling the reader theirs is taken from here would be false.
- */
+/** Flags the launch REMOVES when the GPU picker owns placement. Not a shadow the user wins:
+ * `_strip_device_extra_args` deletes these whenever gpu_ids is set. */
const GPU_SELECTION_STRIPPED_FLAGS: Record<string, string> = {
"--device": "GPU selection",
"-dev": "GPU selection",
@@ -691,15 +584,10 @@ const GPU_SELECTION_STRIPPED_FLAGS: Record<string, string> = {
"-mg": "GPU selection",
};
-/**
- * Flags Manual GPU memory REMOVES without translating, unlike the layer count.
- *
- * `/load` calls strip_shadowing_flags(strip_offload=True) whenever gpu_memory_mode
- * is manual. An -ngl in the extras survives that, because the route reads it with
- * parse_gpu_layers_override and writes it into the first-class field first; nothing
- * does the same for the MoE count or the fitter, so those are dropped and the
- * controls' own values are what runs. Telling the reader theirs wins would be false.
- */
+/** Flags Manual GPU memory REMOVES without translating, unlike the layer count. `/load` calls
+ * strip_shadowing_flags(strip_offload=True) whenever gpu_memory_mode is manual; an -ngl
+ * survives because the route writes it into the first-class field first, while the MoE count
+ * and the fitter are dropped and the controls' own values run. */
const MANUAL_OFFLOAD_STRIPPED_FLAGS: Record<string, string> = {
"--n-cpu-moe": "MoE Layers on CPU",
"-ncmoe": "MoE Layers on CPU",
@@ -709,15 +597,10 @@ const MANUAL_OFFLOAD_STRIPPED_FLAGS: Record<string, string> = {
"-fit": "GPU Memory",
};
-/**
- * Flags Model Memory removes, per setting.
- *
- * apply_model_memory_policy runs before the extras reach the command line. "Keep model
- * in GPU memory" emits its own load mode and strips every other load-mode-bearing flag,
- * because a trailing one resets the whole mode and would drop the lock; "Don't reserve
- * system RAM" drops the flags that would hold a full host copy. Either way the argument
- * never reaches llama-server, so reporting that it wins would be false.
- */
+/** Flags Model Memory removes, per setting. apply_model_memory_policy runs before the extras reach
+ * the command line: "Keep model in GPU memory" emits its own load mode and strips every
+ * other load-mode-bearing flag, since a trailing one resets the whole mode; "Don't reserve
+ * system RAM" drops the flags that would hold a full host copy. */
const KEEP_RESIDENT_STRIPPED_FLAGS: Record<string, string> = {
"--mlock": "Keep model in GPU memory",
"-mlock": "Keep model in GPU memory",
@@ -742,13 +625,9 @@ const NO_RAM_RESERVE_STRIPPED_FLAGS: Record<string, string> = {
"-ndio": "Don't reserve system RAM",
};
-/**
- * Smallest value the backend's own parser accepts, per flag.
- *
- * parse_ctx_override refuses a negative context; parse_gpu_layers_override accepts
- * -1 (all layers) and nothing below it. The rest are checked for being integers
- * only, because that is all those parsers claim.
- */
+/** Smallest value the backend's own parser accepts, per flag. parse_ctx_override refuses a negative
+ * context; parse_gpu_layers_override accepts -1 and nothing below. The rest are checked for
+ * being integers only. */
const INTEGER_VALUE_MINIMUM: Record<string, number> = {
"--ctx-size": 0,
"-c": 0,
@@ -759,10 +638,8 @@ const INTEGER_VALUE_MINIMUM: Record<string, number> = {
/** Values the backend parses as integers, and refuses the load over. */
-/**
- * Flags whose value the backend reads with _last_flag_value, which raises when it is
- * missing or empty. Not integers, so only presence is checked here.
- */
+/** Flags whose value the backend reads with _last_flag_value, which raises when it is missing or
+ * empty. Not integers, so only presence is checked. */
const VALUE_REQUIRED_FLAGS = new Set([
"--cache-type-k",
"-ctk",
@@ -836,12 +713,9 @@ export type ExtraArgsContext = {
export function diagnoseExtraArgs(
input: string,
catalog: LlamaFlagCatalog | null,
- /**
- * What this load's own settings do to the arguments, so the row can say which of
- * them the launch will remove. An object rather than a run of booleans: every
- * setting that owns a flag group adds one, and a positional list of five would be
- * read wrong at the call site long before it stopped compiling.
- */
+ /** What this load's own settings do to the arguments, so the row can say which of them the launch
+ * will remove. An object rather than a run of booleans: a positional list of five would be
+ * read wrong at the call site long before it stopped compiling. */
context: ExtraArgsContext = {},
): ExtraArgsDiagnostic[] {
const gpuSelectionActive = context.gpuSelectionActive ?? false;
@@ -851,12 +725,9 @@ export function diagnoseExtraArgs(
const noRamReserve = context.noRamReserve ?? false;
const out: ExtraArgsDiagnostic[] = [];
const { tokens, unterminatedQuote, quotedIndices } = parseExtraArgs(input);
- // Tokens the walk below consumed as somebody's value. A quoted token in value
- // POSITION is a value whatever it starts with: llama.cpp takes the next argv
- // element for a value-taking option without looking at it, and the backend
- // forwards the list verbatim, so "--chat-template '- hello'" works. Position
- // matters as much as the quotes: a user who quotes out of habit ("--top-k" 20)
- // still wrote a flag, and reading that as a value would refuse a list that runs.
+ // Tokens the walk below consumed as somebody's value. A quoted token in value POSITION is a value
+ // whatever it starts with, since llama.cpp takes the next argv element without looking.
+ // Position matters as much as the quotes: a user quoting out of habit still wrote a flag.
const valueIndices = new Set<number>();
if (unterminatedQuote) {
@@ -871,11 +742,9 @@ export function diagnoseExtraArgs(
message: `Too many arguments: ${tokens.length}, limit ${EXTRA_ARGS_MAX_TOKENS}.`,
});
}
- // The other half of the backend's bounds. A grammar or a JSON schema is one long
- // token, so a payload can sit well inside the token cap and still be refused on
- // size; without this the Load button starts a request that cannot succeed.
- // The host's own limit when it has told us one: it is smaller on Windows, where
- // the whole command line shares a single 32767-character budget.
+ // The other half of the backend's bounds: a grammar or JSON schema is one long token, so a payload
+ // can sit inside the token cap and still be refused on size. The host's own limit when it has
+ // told us one, smaller on Windows, where the whole command line shares a 32767-character budget.
const maxBytes = catalog?.maxBytes || EXTRA_ARGS_MAX_BYTES;
const bytes = TEXT_ENCODER.encode(tokens.join("")).length;
if (bytes > maxBytes) {
@@ -884,8 +753,8 @@ export function diagnoseExtraArgs(
message: `Arguments are too large: ${bytes} bytes, limit ${maxBytes}.`,
});
}
- // And on Windows, what the quoting makes of them: a backslash run before a quote
- // doubles, so bytes alone do not say whether the launch fits.
+ // And on Windows, what the quoting makes of them: a backslash run before a quote doubles, so bytes
+ // alone do not say whether the launch fits.
if (catalog?.windowsCommandBudget) {
const quoted = windowsCommandLength(tokens);
if (quoted > catalog.windowsCommandBudget) {
@@ -896,37 +765,32 @@ export function diagnoseExtraArgs(
}
}
- // The backend refuses any token carrying one, and a command copied out of
- // coloured terminal output is the usual way one arrives.
+ // The backend refuses any token carrying one, and a command copied out of coloured terminal output
+ // is the usual way one arrives.
if (tokens.some((token) => CONTROL_CHARACTERS.test(token))) {
out.push({
level: "error",
message: "Arguments cannot contain control characters.",
});
} else if (tokens.some((token) => isUnusableInArgv(token))) {
- // What is left once control characters are ruled out: half a surrogate pair,
- // which pasting a truncated string can produce. The load refuses it because
- // Popen raises while encoding argv rather than starting llama-server. An emoji
- // is a whole pair and stays perfectly fine.
+ // What is left once control characters are ruled out: half a surrogate pair, which pasting a
+ // truncated string can produce. Popen raises while encoding argv rather than starting
+ // llama-server; an emoji is a whole pair and stays fine.
out.push({
level: "error",
message: "Arguments contain an incomplete character.",
});
}
- // A token belonging to no flag. llama-server answers "invalid argument" and
- // refuses to start, and validate_extra_args refuses it outright, so saying so here
- // is the difference between a red line under the box and a failed load. Two-value
- // flags are allowed for, as they are on the backend.
+ // A token belonging to no flag. llama-server answers "invalid argument" and refuses to start, and
+ // validate_extra_args refuses it outright, so saying so here is the difference between a red
+ // line and a failed load. Two-value flags are allowed for.
let pendingValues = 0;
// The flag those values are owed to, for the checks below.
let pendingOwner: string | null = null;
- // A flag left waiting for its value, either because the next token is another flag
- // or because the text ended. Reported only when the arity is known: the catalogue
- // read this build's own help and lists it as value-taking, or it is the two-value
- // flag whose arity needs no probe. An unverified flag keeps the benefit of the
- // doubt, as it does above. llama-server exits during startup on these, which is a
- // failed load rather than a red line under the box.
+ // A flag left waiting for its value. Reported only when the arity is known: the catalogue read
+ // this build's own help, or it is the two-value flag whose arity needs no probe. An
+ // unverified flag keeps the benefit of the doubt.
const owedValues: string[] = [];
const noteOwed = (owner: string | null, pending: number) => {
if (owner === null || pending <= 0) {
@@ -959,8 +823,8 @@ export function diagnoseExtraArgs(
}
continue;
}
- // Before the obligation is replaced: "--numa --verbose" leaves --numa without
- // the value it needs, and only this point in the walk still knows that.
+ // Before the obligation is replaced: "--numa --verbose" leaves --numa without the value it needs,
+ // and only this point in the walk still knows that.
noteOwed(pendingOwner, pendingValues);
const attached = valueIsAttached(token, flag);
pendingValues = TWO_VALUE_FLAGS.has(flag)
@@ -975,9 +839,9 @@ export function diagnoseExtraArgs(
: 2
: attached
? 0
- : // A flag THIS build documents as taking no value claims none, so the
- // next bare token has no owner. Only when the catalogue actually knows
- // the flag: an unverified one keeps the benefit of the doubt.
+ // A flag THIS build documents as taking no value claims none, so the next bare token has
+ // no owner. Only when the catalogue actually knows the flag.
+ :
catalog?.switches.has(flag)
? 0
: 1;
@@ -995,21 +859,19 @@ export function diagnoseExtraArgs(
const memoryStripped: [string, string][] = [];
const reportedValues = new Set<string>();
for (const [index, token] of tokens.entries()) {
- // Judged the same way the walk above judged it, or a quoted value that begins
- // with a hyphen would be reported here as a flag this build has never heard of.
+ // Judged the same way the walk above judged it, or a quoted value beginning with a hyphen would be
+ // reported here as an unknown flag.
const flag = valueIndices.has(index) ? null : extraArgFlagName(token);
if (flag === null) {
continue;
}
- // Before the de-duplication below, because llama.cpp reads the LAST occurrence:
- // in `-ngl 20 -ngl many` it is the second one the backend parses and refuses, so
- // checking only the first would leave Load enabled for a request that 400s.
+ // Before the de-duplication below, because llama.cpp reads the LAST occurrence: in `-ngl 20 -ngl
+ // many` it is the second one the backend refuses.
if (INTEGER_VALUE_FLAGS.has(flag) || VALUE_REQUIRED_FLAGS.has(flag)) {
const attached = valueIsAttached(token, flag);
const value = attached ? token.split("=")[1] : tokens[index + 1];
- // A flag whose value is the next token has none when that token is itself a
- // flag: `--ctx-size --numa` reads --numa as the value in a shell and as a
- // missing one here, which is what the backend's parser says too.
+ // A flag whose value is the next token has none when that token is itself a flag: `--ctx-size
+ // --numa` reads --numa as the value in a shell and as a missing one here, as the parser does.
const missing =
value === undefined ||
value === "" ||
@@ -1036,9 +898,8 @@ export function diagnoseExtraArgs(
BATCH_SIZE_FLAGS.has(flag) &&
Number(value.trim()) < Math.max(2, batchFloor)
) {
- // Not raised the way the first-class control is: this flag is appended after
- // the launcher's own --batch-size and wins it, so the load starts and
- // llama-server aborts on the assertion instead.
+ // Not raised the way the first-class control is: this flag is appended after the launcher's own
+ // --batch-size and wins it, so the load starts and llama-server aborts on the assertion.
const floor = Math.max(2, batchFloor);
message =
floor > 2
@@ -1059,18 +920,16 @@ export function diagnoseExtraArgs(
managedControl !== undefined || Boolean(catalog?.managed.has(flag));
if (token !== token.trim()) {
- // The padding is part of the token llama.cpp looks up, so a quoted "--top-k "
- // never arrives as a flag: it answers "error: invalid argument: --top-k",
- // naming a flag that looks perfectly correct in the log. Reported whether or
- // not a control owns the flag, since the name is not the problem.
+ // The padding is part of the token llama.cpp looks up, so a quoted "--top-k " never arrives as a
+ // flag: it answers "error: invalid argument: --top-k", naming a flag that looks correct in
+ // the log. Reported whether or not a control owns the flag.
out.push({
level: "error",
message: `Remove the spaces around "${token}". llama-server reads them as part of the flag.`,
});
} else if (token.includes("=") && !isManaged) {
- // Measured on b10342 and b10360: "--top-k=20", "--ctx-size=4096" and
- // "--flash-attn=on" each exit with "error: invalid argument". A managed flag
- // is left to the message below, which names the control that owns it.
+ // Measured on b10342 and b10360: "--top-k=20", "--ctx-size=4096" and "--flash-attn=on" each exit
+ // with "error: invalid argument". A managed flag is left to the message below.
out.push({
level: "error",
message: `llama-server does not read "${flag}=value". Write ${flag} and its value as two arguments.`,
@@ -1095,8 +954,7 @@ export function diagnoseExtraArgs(
manualStripped.push(flag);
continue;
}
- // No-reserve first: with both settings on it is the one that runs, and its own
- // veto is what removes the flag.
+ // No-reserve first: with both settings on it is the one that runs, and its own veto is what removes the flag.
if (noRamReserve && NO_RAM_RESERVE_STRIPPED_FLAGS[flag]) {
memoryStripped.push([flag, NO_RAM_RESERVE_STRIPPED_FLAGS[flag]]);
continue;
@@ -1117,8 +975,8 @@ export function diagnoseExtraArgs(
});
continue;
}
- // Only when the catalogue was actually read: a build we could not probe must
- // not have every one of its flags called a typo.
+ // Only when the catalogue was actually read: a build we could not probe must not have every one of
+ // its flags called a typo.
if (catalog?.probeOk && !(flag in catalog.flags)) {
unknown.push(flag);
}
diff --git a/studio/frontend/src/features/model-picker/model-config/memory-fit.ts b/studio/frontend/src/features/model-picker/model-config/memory-fit.ts
index c268e4a9dae..90bf449d72d 100644
--- a/studio/frontend/src/features/model-picker/model-config/memory-fit.ts
+++ b/studio/frontend/src/features/model-picker/model-config/memory-fit.ts
@@ -1,31 +1,15 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/**
- * How the estimated footprint sits against the memory available to hold it, and the
- * single note the row prints about it.
- *
- * Split out of model-config-page.tsx for the reason the sibling estimate-context.ts
- * gives: kept free of `@/` ALIAS imports so `tests/` can load it under
- * `node --experimental-strip-types`, which does not resolve that alias. Living
- * inside a 3,400-line .tsx made this chain unreachable from the test runner, and a
- * ternary arm that could never be taken survived review there for exactly that
- * reason (see the pool note on the advisory below).
- *
- * The imports below are RELATIVE and with explicit extensions, which the strip-types
- * loader does resolve, so the three tests that load this module directly
- * (memory-fit, memory-estimate-skew, memory-estimate-free-vram) still work without
- * registering a bundler resolver. An `@/` spelling here breaks all three.
- *
- * Everything here is pure and typed structurally, so a MemoryEstimate satisfies the
- * inputs without importing it.
- */
+/** How the estimated footprint sits against the memory available to hold it, and the single
+ * note the row prints about it. Split out of model-config-page.tsx and kept free of `@/`
+ * ALIAS imports so `tests/` can load it under `node --experimental-strip-types`, which does
+ * not resolve that alias. Imports below are RELATIVE with explicit extensions for the same
+ * reason; an `@/` spelling breaks the three tests that load this module directly. */
-// The fit vocabulary and the unit formatting now live in src/lib/memory/, shared
-// with the Hub memory bar so the two surfaces cannot describe one load
-// differently while being fed identical bytes. Re-exported here rather than
-// moved out of reach, because this module is the panel's entry point and its
-// call sites are unchanged.
+// The fit vocabulary and unit formatting live in src/lib/memory/, shared with the Hub memory
+// bar so the two surfaces cannot describe one load differently. Re-exported here because
+// this module is the panel's entry point and its call sites are unchanged.
export {
classifyMemoryFit,
worseMemoryFit,
@@ -37,21 +21,11 @@ import { classifyMemoryFit, worseMemoryFit } from "../../../lib/memory/verdict.t
import type { MemoryFitVerdict } from "../../../lib/memory/verdict.ts";
import { formatBytesGiB } from "../../../lib/memory/format.ts";
-/**
- * A memory figure in bytes, to two decimals.
- *
- * @deprecated Prefer `formatBytesGiB` from `@/lib/memory/format`, whose name
- * says which unit it takes. This alias exists because there used to be a SECOND
- * exported `formatMemoryGb`, in `lib/model-memory.ts`, which took gigabytes
- * rather than bytes and printed a different label -- the same name and the same
- * `(number) => string` signature for two incompatible things.
- *
- * Note the label has changed from "GB" to "GiB". The divide was always by
- * 1024^3, so every figure this printed was a gibibyte value labelled as a
- * gigabyte, overstating each by 7.4%. That is the defect #9570 fixed elsewhere;
- * it reached seven figures on the Load Model panel and the guard test could not
- * see it, because its regex only matches interpolations naming a `*TotalGb`.
- */
+/** A memory figure in bytes, to two decimals. @deprecated Prefer `formatBytesGiB` from
+ * `@/lib/memory/format`, whose name says which unit it takes. This alias exists because a
+ * SECOND exported `formatMemoryGb` in lib/model-memory.ts took gigabytes and printed a
+ * different label, with the same name and signature. The divide was always by 1024^3, so
+ * every figure was a gibibyte labelled as a gigabyte, overstating each by 7.4% (#9570). */
export const formatMemoryGb = formatBytesGiB;
/** At most one note under the figures, most actionable first. */
@@ -82,13 +56,12 @@ export interface MemoryFitCapacity {
gpuCapacityGb: number;
/** GPU plus host RAM, the ceiling an offloaded load works against. 0 when unknown. */
totalCapacityGb: number;
- /** Host RAM alone. Bytes pinned OUTSIDE the GPU have to fit in this, and unused
- * VRAM cannot help them, so it is a separate question from the total. */
+ /** Host RAM alone. Bytes pinned OUTSIDE the GPU have to fit in this, and unused VRAM cannot
+ * help them, so it is a separate question from the total. */
systemRamCapacityGb: number;
/** VRAM free on the usable cards right now. Warns only. 0 when nothing was probed. */
freeGpuCapacityGb: number;
- /** Host RAM the machine can hand out right now, less the loader's reserve. Warns
- * only. 0 when unknown. */
+ /** Host RAM the machine can hand out right now, less the loader's reserve. Warns only. 0 when unknown. */
usableSystemRamGb: number;
/** GPU and host draw on the same memory, so an offloaded byte is not a freed one. */
singleMemoryPool: boolean;
@@ -117,33 +90,27 @@ export interface MemoryFitResult {
advisory: MemoryAdvisory | null;
}
-/**
- * Every verdict the row shows, plus the one note it prints.
- *
- * `_host_offload_shortfall_message` refuses a load whose offloaded weights exceed
- * psutil's AVAILABLE memory less a reserve, not the physical total, so a 70 GB host
- * share read as fitting a 128 GB box with 32 GB free. The free-memory verdicts here
- * WARN instead: the bytes a pending load reclaims are mostly the resident model's
- * own, which Studio unloads first, and cannot be attributed from here.
- */
+/** Every verdict the row shows, plus the one note it prints. `_host_offload_shortfall_message`
+ * refuses a load whose offloaded weights exceed psutil's AVAILABLE memory less a reserve,
+ * not the physical total, so a 70 GB host share read as fitting a 128 GB box with 32 GB
+ * free. The free-memory verdicts here WARN instead: the bytes a pending load reclaims are
+ * mostly the resident model's own, which Studio unloads first. */
export function resolveMemoryFit(
estimate: MemoryFitEstimate,
capacity: MemoryFitCapacity,
): MemoryFitResult {
const { singleMemoryPool } = capacity;
const rawGpuFit = classifyMemoryFit(estimate.gpuBytes, capacity.gpuCapacityGb);
- // One pool means the WHOLE load draws on that memory, so the pressure question goes
- // to the total rather than to a GPU share that is not a separate reservation. Asking
- // it of gpuBytes alone let a partly CPU-offloaded load on a Vulkan iGPU look
- // comfortable against free memory that has to hold all of it.
+ // One pool means the WHOLE load draws on that memory, so the pressure question goes to the
+ // total rather than a GPU share that is not a separate reservation. Asking it of gpuBytes
+ // alone let a partly CPU-offloaded load on a Vulkan iGPU look comfortable.
const freeGpuFit = classifyMemoryFit(
singleMemoryPool ? estimate.totalBytes : estimate.gpuBytes,
capacity.freeGpuCapacityGb,
);
const gpuPressured = freeGpuFit === "exceeds" || freeGpuFit === "tight";
// Guarded, not subtracted blind: a non-finite figure makes the difference NaN, which
- // `Math.max(0, ...)` propagates rather than clamps. 0 classifies the same ("unknown")
- // and is printable, which NaN is not.
+ // `Math.max(0, ...)` propagates rather than clamps. 0 classifies the same and is printable.
const hostShareBytes =
Number.isFinite(estimate.totalBytes) && Number.isFinite(estimate.gpuBytes)
? Math.max(0, estimate.totalBytes - estimate.gpuBytes)
@@ -155,9 +122,9 @@ export function resolveMemoryFit(
);
const hostPressured = usableHostFit === "exceeds" || usableHostFit === "tight";
const gpuFit = rawGpuFit === "fits" && gpuPressured ? "tight" : rawGpuFit;
- // The host share must fit host RAM on its own: unused VRAM cannot hold bytes pinned
- // outside the GPU, so the combined ceiling alone called a 70 GB CPU placement a fit
- // on a 24 GB card plus 64 GB of RAM. Skipped where the two are one pool.
+ // The host share must fit host RAM on its own: unused VRAM cannot hold bytes pinned outside
+ // the GPU, so the combined ceiling alone called a 70 GB CPU placement a fit on a 24 GB card
+ // plus 64 GB of RAM. Skipped where the two are one pool.
const hostShareFit: MemoryFitVerdict = singleMemoryPool
? "unknown"
: classifyMemoryFit(hostShareBytes, capacity.systemRamCapacityGb);
@@ -165,10 +132,9 @@ export function resolveMemoryFit(
classifyMemoryFit(estimate.totalBytes, capacity.totalCapacityGb),
hostShareFit,
);
- // Lower bound, not an estimate. Both routes here UNDER-count by a term that grows
- // with context: no attention dims, so the target cache is missing, or a drafter that
- // is a repository rather than a file on disk, so its cache is missing while its
- // weights are counted. The advisory still tells them apart -- different fixes.
+ // Lower bound, not an estimate. Both routes here UNDER-count by a term that grows with
+ // context: no attention dims, so the target cache is missing, or a drafter that is a
+ // repository rather than a file, so its cache is missing while its weights are counted.
const bounded =
!estimate.kvEstimable || estimate.drafterKvUnsized || estimate.adaptersUnsized;
return {
@@ -205,20 +171,11 @@ interface AdvisoryVerdicts {
hostPressured: boolean;
}
-/**
- * At most one note, most actionable first.
- *
- * An unsizable cache outranks any verdict drawn from the figures, since it says they
- * are incomplete. Branches on `kvEstimable`, not `bounded`: both make the figures a
- * floor, but only this one is about the header.
- *
- * The pool split used to gate the whole tail, which made the shared-pool pressure copy
- * dead code -- it sat inside the `singleMemoryPool === false` arm and re-tested
- * `singleMemoryPool` -- leaving a single-pool host one reachable note, "exceeds". An
- * Apple machine or a Vulkan iGPU never warned that a load fitting the machine did not
- * fit what was free. The pool question now decides the WORDING and which figures are
- * compared, not whether the branch exists.
- */
+/** At most one note, most actionable first. An unsizable cache outranks any verdict drawn from
+ * the figures, since it says they are incomplete. Branches on `kvEstimable`, not `bounded`:
+ * both make the figures a floor, but only this one is about the header. The pool split used
+ * to gate the whole tail, making the shared-pool pressure copy dead code, so a single-pool
+ * host had one reachable note. The pool question now decides the WORDING, not the branch. */
export function resolveMemoryAdvisory(
estimate: MemoryFitEstimate,
verdicts: AdvisoryVerdicts,
@@ -248,9 +205,8 @@ export function resolveMemoryAdvisory(
text: "More than this machine's memory. The GPU and the rest of the system share one pool here, so there is nothing to offload to.",
};
}
- // One pool, so one pressure question however it was measured: the GPU's free
- // reading and the host's available reading are two views of the same bytes, and
- // either seeing pressure is the same news.
+ // One pool, so one pressure question however it was measured: the GPU's free reading and the
+ // host's available reading are two views of the same bytes.
if (verdicts.hostPressured || verdicts.gpuPressured) {
return {
tone: "muted",
@@ -259,10 +215,9 @@ export function resolveMemoryAdvisory(
}
return null;
}
- // Discrete memory, so the two verdicts are separate questions and the aggregate one
- // is asked FIRST. Reading gpuFit alone offered spilling to system RAM as the remedy
- // for a load that does not fit in GPU and RAM combined, which is advice to do
- // something that cannot work.
+ // Discrete memory, so the two verdicts are separate questions and the aggregate one is asked
+ // FIRST. Reading gpuFit alone offered spilling to system RAM as the remedy for a load that
+ // does not fit in GPU and RAM combined.
if (verdicts.hostShareFit === "exceeds") {
return {
tone: "warn",
@@ -299,22 +254,12 @@ export function resolveMemoryAdvisory(
/** What `resolveKvNote` joins its items with, and what `glueNoteItems` splits on. */
export const NOTE_SEPARATOR = " · ";
-/**
- * A separated caption, breakable only between its items.
- *
- * A caption like "f16 · 262,144 tokens · 4 slots" does not fit the panel once the
- * window is narrow enough to shrink it, and left to itself the browser breaks at the
- * last space that fits -- which put "slots" alone on a line under "... tokens · 4".
- * Gluing each item together with U+00A0 leaves exactly one break opportunity per
- * bullet, and gluing the bullet to the item that FOLLOWS it means the break lands
- * before the bullet rather than orphaning it at the end of the previous line.
- *
- * A note with NO separator is returned untouched. It has no items to keep apart, so
- * gluing it would buy nothing and cost every break opportunity it had: the Weights and
- * Draft cache notes are ordinary prose ("256 of 257 layers on GPU"), and glued they
- * became a single unbreakable run that overflows the caption column rather than
- * wrapping inside it, which is worse than the orphan this function exists to prevent.
- */
+/** A separated caption, breakable only between its items. A caption like "f16 - 262,144 tokens
+ * - 4 slots" does not fit a narrow panel, and the browser breaks at the last space that
+ * fits, orphaning "slots". Gluing each item with U+00A0 leaves one break opportunity per
+ * bullet, and gluing the bullet to the item that FOLLOWS it puts the break before it. A note
+ * with NO separator is returned untouched: it is ordinary prose, and gluing it made a single
+ * unbreakable run that overflows the caption column rather than wrapping. */
export function glueNoteItems(note: string): string {
const items = note.split(NOTE_SEPARATOR);
if (items.length < 2) {
@@ -337,8 +282,8 @@ export function resolveKvNote(estimate: {
}): string {
return [
estimate.cacheTypeKv ?? "f16",
- // Off the wire, so it is not trusted to be a number: `.toLocaleString()` on a
- // null throws, and one bad field must not take the whole panel down.
+ // Off the wire, so it is not trusted to be a number: `.toLocaleString()` on a null throws, and
+ // one bad field must not take the whole panel down.
`${Number.isFinite(estimate.nCtx) ? Math.max(0, estimate.nCtx).toLocaleString() : "0"} tokens`,
Number.isFinite(estimate.nParallel) && estimate.nParallel > 1
? `${estimate.nParallel} slots`
@@ -349,17 +294,11 @@ export function resolveKvNote(estimate: {
.join(" · ");
}
-/**
- * Where the draft cache actually sits, read from its own GPU share rather than from
- * kvOnGpu.
- *
- * kvOnGpu is the TARGET cache's placement, and the two are set by different flags:
- * `--no-kv-offload` moves the target, `--spec-draft-ngl 0` moves the drafter. Off the
- * target flag this line was wrong in both directions -- silent about a genuinely
- * host-resident draft cache, and claiming "host RAM" for one the same response had
- * just charged to gpu_bytes. Under MTP the term is split across both placements, so
- * there is a third case that no boolean could have expressed.
- */
+/** Where the draft cache actually sits, read from its own GPU share rather than kvOnGpu.
+ * kvOnGpu is the TARGET cache's placement and the two are set by different flags:
+ * `--no-kv-offload` moves the target, `--spec-draft-ngl 0` moves the drafter. Off the target
+ * flag this line was wrong in both directions. Under MTP the term is split across both
+ * placements, a third case no boolean could express. */
export function resolveDraftCacheNote(
drafterRuntimeGpuBytes: number,
drafterRuntimeBytes: number,
diff --git a/studio/frontend/src/features/model-picker/model-config/model-identity.ts b/studio/frontend/src/features/model-picker/model-config/model-identity.ts
index 4a8f9c580bd..c5e006d0958 100644
--- a/studio/frontend/src/features/model-picker/model-config/model-identity.ts
+++ b/studio/frontend/src/features/model-picker/model-config/model-identity.ts
@@ -76,8 +76,8 @@ export function ggufVariantFromStorageKey(key: string): string | null {
return separator >= 0 ? key.slice(separator + 2) : null;
}
-// Mirrors split_quant_suffix in studio/backend/utils/openai_auto_switch_settings.py.
-// The bpw modifier ("IQ4_XS-3.53bpw") is optional: the backend label helpers disagree.
+// Mirrors split_quant_suffix in openai_auto_switch_settings.py. The bpw modifier
+// ("IQ4_XS-3.53bpw") is optional: the backend label helpers disagree.
const BPW_SUFFIX = /-[0-9]+(?:\.[0-9]+)?bpw$/i;
// One source for the anchored test and the scan below. Mirrors _GGUF_QUANT_RE in gguf.py.
const QUANT_TOKEN_SOURCE =
@@ -102,9 +102,8 @@ function ggufStem(filename: string): string {
return withoutExtension.replace(GGUF_SPLIT_SUFFIX, "").trim();
}
-/**
-* Mirrors extract_quant_label in gguf.py, for a bare filename. The parent-directory pass
-* cannot fire on a basename, so this is the stem's quant token or the stem itself. */
+/** Mirrors extract_quant_label in gguf.py, for a bare filename. The parent-directory pass cannot
+ * fire on a basename, so this is the stem's quant token or the stem itself. */
export function ggufQuantLabel(filename: string): string {
const stem = ggufStem(filename);
let fallback: RegExpExecArray | null = null;
@@ -121,9 +120,8 @@ export function ggufQuantLabel(filename: string): string {
return stem || "gguf";
}
-/**
-* `[head, quant]` for a `head:QUANT` key, or null when the colon is not one. The suffix must
-* look like a real quant, so an ordinary colon in a POSIX filename and a drive letter are left alone. */
+/** `[head, quant]` for a `head:QUANT` key, or null when the colon is not one. The suffix must look
+ * like a real quant, so an ordinary colon in a POSIX filename and a drive letter are left alone. */
export function splitQuantSuffix(value: string): [string, string] | null {
const separator = value.lastIndexOf(":");
if (separator <= 0 || separator === value.length - 1) {
@@ -144,8 +142,8 @@ export function splitQuantSuffix(value: string): [string, string] | null {
if (!head.toLowerCase().endsWith(".gguf")) {
return null;
}
- // Exactly that label, as the backend requires: a colon is legal in a POSIX filename, so
- // reading the suffix as a variant folds two real files onto one lowercased key.
+ // Exactly that label, as the backend requires: a colon is legal in a POSIX filename, so reading
+ // the suffix as a variant folds two real files onto one lowercased key.
const filename = head.replace(BACKSLASHES, "/").split("/").pop() ?? head;
return tail.toLowerCase() === ggufQuantLabel(filename).toLowerCase()
? [head, tail]
diff --git a/studio/frontend/src/features/model-picker/model-config/per-model-config.ts b/studio/frontend/src/features/model-picker/model-config/per-model-config.ts
index ec0dd05cbeb..d1eaf8a37b5 100644
--- a/studio/frontend/src/features/model-picker/model-config/per-model-config.ts
+++ b/studio/frontend/src/features/model-picker/model-config/per-model-config.ts
@@ -24,14 +24,14 @@ export interface PerModelConfig {
mlxKvBits?: number | null;
speculativeType: string | null;
specDraftNMax: number | null;
- /** KV cache dtype for the DRAFT context (--spec-draft-type-k/-v), sized and
- * quantized independently of kvCacheDtype. Optional so older blobs parse. */
+ /** KV cache dtype for the DRAFT context, sized and quantized independently of kvCacheDtype.
+ * Optional so older blobs parse. */
specDraftCacheDtype?: string | null;
nParallel: number | null;
nBatch: number | null;
nUbatch: number | null;
- /** --load-mode; null lets the fit decide: `none` when the load fits in VRAM
- * (or VRAM plus host RAM), else no flag. Any value set here wins. */
+ /** --load-mode; null lets the fit decide (`none` when the load fits, else no flag).
+ * Any value set here wins. */
loadMode?: string | null;
/** --ctx-checkpoints; null follows the llama.cpp default (32). */
ctxCheckpoints?: number | null;
@@ -41,20 +41,13 @@ export interface PerModelConfig {
/** Load a vision GGUF without its mmproj, freeing the projector's VRAM. */
disableVision: boolean;
chatTemplateOverride: string | null;
- /**
- * Pass-through llama-server args, one argv token per entry, appended after
- * Unsloth's own flags.
- *
- * Three states, and the difference is load-bearing. `undefined` means this copy
- * never read the stored value, so a save must leave the server's alone: the
- * overrides route preserves the field when it is omitted, which is what kept
- * CLI-set flags alive while the panel had no control for them. `null` means the
- * user cleared the box, which has to be sent as an explicit `[]` or the clear is
- * silently dropped. A non-empty list is what to launch with.
- */
+ /** Pass-through llama-server argv tokens, appended after Unsloth's own flags. Three states:
+ * `undefined` means this copy never read the stored value, so a save leaves the server's
+ * alone (this is what kept CLI-set flags alive); `null` means the user cleared the box and
+ * must be sent as an explicit `[]`; a non-empty list is what to launch with. */
llamaExtraArgs?: string[] | null;
- // GPU Memory controls (per-model, GGUF-only), optional so older blobs still parse. null/absent
- // selectedGpuIds means automatic. --tensor-split is not remembered: it is bound to the GPU set.
+ // GPU Memory controls (per-model, GGUF-only), optional so older blobs parse. Absent or null
+ // selectedGpuIds means automatic. --tensor-split is not remembered: it follows the GPU set.
gpuMemoryMode?: "auto" | "manual";
gpuLayers?: number;
nCpuMoe?: number;
@@ -85,14 +78,14 @@ export const DEFAULT_PER_MODEL_CONFIG: PerModelConfig = {
export const N_PARALLEL_MIN = 1;
export const N_PARALLEL_MAX = 64;
-// Mirrors vram_budget_settings.py VRAM_FRACTION_MIN/MAX/DEFAULT as whole percent
-// (the slider works in integers). Server-wide, so these are only the bounds the
-// control clamps to; the value lives in /api/settings/vram-budget.
+// Mirrors vram_budget_settings.py VRAM_FRACTION_MIN/MAX/DEFAULT as whole percent (the slider is
+// integer). Server-wide, so these only bound the control; the value lives in
+// /api/settings/vram-budget.
export const VRAM_BUDGET_PERCENT_MIN = 80;
export const VRAM_BUDGET_PERCENT_MAX = 100;
export const VRAM_BUDGET_PERCENT_DEFAULT = 97;
// Tenths: a whole percent is ~245 MiB of context on a 24 GB card. Mirrors
-// VRAM_FRACTION_DECIMALS = 3 in vram_budget_settings.py.
+// VRAM_FRACTION_DECIMALS = 3.
export const VRAM_BUDGET_PERCENT_STEP = 0.1;
/** Percent for the slider; the fraction is rebuilt at the API boundary. */
@@ -101,16 +94,16 @@ export function vramFractionToPercent(fraction: number): number {
}
export function vramPercentToFraction(percent: number): number {
- // Three decimals, so 0.975 cannot come back as 0.9750000000000001 and read as
- // "changed" after a drag that ended where it began.
+ // Three decimals, so 0.975 cannot return as 0.9750000000000001 and read as "changed" after
+ // a drag that ended where it began.
return Math.round(percent * 10) / 1000;
}
// mirrors llama_server_args.py BATCH_MIN/MAX; null = follow the llama.cpp defaults (2048 / 512)
export const N_BATCH_MIN = 1;
export const N_BATCH_MAX = 65536;
-// llama.cpp's own --batch-size default (_DEFAULT_LLAMA_N_BATCH), which a blank control
-// runs at: it still caps the micro-batch, so advisories have to reckon with it.
+// llama.cpp's own --batch-size default, which a blank control runs at: it still caps the
+// micro-batch, so advisories have to reckon with it.
export const N_BATCH_LLAMA_DEFAULT = 2048;
export const MAX_SEQ_LENGTH_MIN = 128;
@@ -128,11 +121,9 @@ const NO_MLX_REASONS = new Set([
"detection_failed",
]);
-/** Whether MLX will serve this model, and so whether MLX-only settings apply.
- *
- * Every non-GGUF model loads through MLX on a working Mac stack, including plain
- * safetensors repos, so `!isGguf` alone would show these controls to CUDA users.
- */
+/** Whether MLX will serve this model, and so whether MLX-only settings apply. Every non-GGUF
+ * model loads through MLX on a working Mac stack, so `!isGguf` alone would show these
+ * controls to CUDA users. */
export function isServedByMlx(
isGguf: boolean,
deviceType: string | null | undefined,
@@ -273,14 +264,13 @@ export const KV_CACHE_DTYPES = [
"f32",
] as const;
-// Every width mx.quantize supports. By bit width, not a dtype name, hence separate
-// from KV_CACHE_DTYPES.
+// Every width mx.quantize supports. By bit width, not a dtype name, hence separate from KV_CACHE_DTYPES.
export const MLX_KV_BITS: readonly number[] = [8, 6, 5, 4, 3, 2];
const VALID_KV_CACHE_DTYPES = new Set<string>(KV_CACHE_DTYPES);
-// llama-server's --load-mode enum, in --help order. "auto" is the default: the UI
-// shows it, storage keeps null and the backend emits no flag, so the fit may pick
-// "none". Never sent verbatim; builds like b10360 reject "auto" as a value.
+// llama-server's --load-mode enum in --help order. "auto" is the default: the UI shows it,
+// storage keeps null and the backend emits no flag. Never sent verbatim; builds like
+// b10360 reject "auto" as a value.
export const LOAD_MODES = [
"auto",
"none",
@@ -292,14 +282,14 @@ export const LOAD_MODES = [
export const LOAD_MODE_DEFAULT = "auto";
const VALID_LOAD_MODES = new Set<string>(LOAD_MODES);
-// --ctx-checkpoints: per-slot snapshots of the sliding-window cache, so the count
-// is small. 0 disables them; the ceiling is a sanity bound, not an upstream one.
+// --ctx-checkpoints: per-slot sliding-window snapshots, so the count is small. 0 disables
+// them; the ceiling is a sanity bound, not an upstream one.
export const CTX_CHECKPOINTS_MIN = 0;
export const CTX_CHECKPOINTS_MAX = 256;
export const CTX_CHECKPOINTS_LLAMA_DEFAULT = 32;
-// --cache-ram in MiB: -1 is "no limit" and 0 disables the host prompt cache, so
-// the floor is -1. The 1 TiB ceiling fails a stray keystroke before the child does.
+// --cache-ram in MiB: -1 is "no limit" and 0 disables the host prompt cache, so the floor
+// is -1. The 1 TiB ceiling fails a stray keystroke before the child does.
export const CACHE_RAM_MIN = -1;
export const CACHE_RAM_MAX = 1024 * 1024;
export const CACHE_RAM_LLAMA_DEFAULT = 8192;
@@ -310,16 +300,14 @@ export {
SPECULATIVE_TYPES,
} from "@/lib/speculative-modes";
-/** Exported so cross-tab listeners can tell this key's storage event from the
- * dozens of others Studio writes. */
+/** Exported so cross-tab listeners can tell this key's storage event from the dozens of others Studio writes. */
export const PER_MODEL_CONFIG_STORAGE_KEY = "unsloth_model_configs";
const STORAGE_KEY = PER_MODEL_CONFIG_STORAGE_KEY;
const LEGACY_STORAGE_KEY = "unsloth_load_settings";
const LEGACY_MIGRATION_FLAG = "unsloth_model_configs_migrated";
-// v2 added nBatch / nUbatch, v3 llamaExtraArgs, v4 disableVision and v5 the
-// llama-server tuning group (loadMode / specDraftCacheDtype / ctxCheckpoints /
-// cacheRam); a client from before any of them would normalize the field it does
-// not know straight back out of the record.
+// would normalize the unknown field straight back out of the record.
+// v2 added nBatch/nUbatch, v3 llamaExtraArgs, v4 disableVision, v5 the llama-server tuning group
+// (loadMode / specDraftCacheDtype / ctxCheckpoints / cacheRam); a client from before any of them
const STORAGE_SCHEMA_VERSION = 5;
const PRE_SERVER_TUNING_SCHEMA_VERSION = 4;
const PRE_VISION_SCHEMA_VERSION = 3;
@@ -361,13 +349,9 @@ const STORED_CONFIG_FIELDS = new Set([
"selectedGpuIndexKind",
]);
-/**
- * Keep only a list of strings, preserving the three states above.
- *
- * Anything that is not an array is "not loaded" (`undefined`), never "cleared":
- * the wiping case is the one worth being careful about, since the flags it would
- * throw away are invisible in this panel until the row reads them.
- */
+/** Keep only a list of strings, preserving the three states above. Anything that is not an
+ * array is "not loaded" (`undefined`), never "cleared": the flags a wipe would throw
+ * away are invisible in this panel until the row reads them. */
function normalizeLlamaExtraArgs(value: unknown): string[] | null | undefined {
if (value === null) {
return null;
@@ -462,18 +446,14 @@ function canonicalizeSpeculativeType(value: string): string | null {
return null;
}
-/**
- * Canonicalize a stored --load-mode, or null to follow the llama.cpp default.
- *
- * "auto" folds to null like Speculative Decoding's: it IS the default, so storing
- * it would pin a value the build may redefine and read as an override everywhere.
- */
+/** Canonicalize a stored --load-mode, or null to follow the llama.cpp default. "auto" folds
+ * to null: it IS the default, so storing it would pin a value the build may redefine. */
export function canonicalizeLoadMode(value: unknown): string | null {
if (typeof value !== "string") {
return null;
}
- // Whitespace and case only: "mmap + mlock" is not a spelling llama-server
- // accepts, so it is refused rather than repaired.
+ // Whitespace and case only: "mmap + mlock" is not a spelling llama-server accepts, so it is
+ // refused rather than repaired.
const mode = value.trim().toLowerCase();
if (!mode || mode === LOAD_MODE_DEFAULT) {
return null;
@@ -563,8 +543,8 @@ function canUseStorage(): boolean {
return typeof window !== "undefined";
}
-// Whether Run Settings shows its advanced section is a standing preference, not per-model:
-// opening it once keeps it open for every model and quant. Closed until asked for.
+// Whether Run Settings shows its advanced section is a standing preference, not per-model.
+// Closed until asked for.
export const ADVANCED_SETTINGS_OPEN_KEY = "unsloth_model_advanced_settings";
function loadAdvancedSettingsOpen(): boolean | null {
@@ -579,23 +559,22 @@ function loadAdvancedSettingsOpen(): boolean | null {
}
}
-// Set only when a write is refused, so the switch keeps working in a browser with storage
-// disabled or full. Cleared by the next write that sticks. `stored` is what storage held at
-// the time, the one signal that tells a later write by someone else apart.
+// Set only when a write is refused, so the switch keeps working with storage disabled or
+// full. Cleared by the next write that sticks. `stored` is what storage held then, the
+// one signal that tells a later write by someone else apart.
let unpersisted: { open: boolean; stored: boolean | null } | null = null;
const advancedOpenListeners = new Set<() => void>();
-/** null until the switch is used, so an untouched panel is free to open the
-* section for a model that carries non-default advanced values.
-* Read straight from storage rather than cached: a write from another tab while every panel
-* was unmounted has no listener to catch it, and its storage event is not replayed on mount. */
+/** Null until the switch is used, so an untouched panel may open the section for a model with
+ * non-default advanced values. Read straight from storage, not cached: a write from
+ * another tab while every panel was unmounted has no listener and no replayed event. */
export function readAdvancedSettingsOpen(): boolean | null {
const stored = loadAdvancedSettingsOpen();
if (!unpersisted) {
return stored;
}
// Storage moved since the refused write, so a newer choice outranks the fallback. Checked on
- // read, not on the storage event, so it still holds for an event that landed while unmounted.
+ // read, not on the storage event, so it holds for an event that landed while unmounted.
if (stored !== unpersisted.stored) {
unpersisted = null;
return stored;
@@ -620,8 +599,8 @@ export function saveAdvancedSettingsOpen(open: boolean): void {
unpersisted = writeAdvancedSettingsOpen(open)
? null
: { open, stored: loadAdvancedSettingsOpen() };
- // Run Settings is mounted on several surfaces at once, and the sidebar copy stays mounted
- // while collapsed, so tell them all rather than let them keep a snapshot taken at mount.
+ // Run Settings is mounted on several surfaces at once and the sidebar copy stays mounted
+ // while collapsed, so tell them all rather than let them keep a mount-time snapshot.
for (const listener of [...advancedOpenListeners]) {
listener();
}
@@ -764,9 +743,8 @@ function legacyEntryToConfig(raw: Record<string, unknown>): PerModelConfig {
disableVision:
typeof raw.disableVision === "boolean" ? raw.disableVision : false,
chatTemplateOverride: null,
- // Absent, not null: a legacy blob predates the editor, and the server may well
- // hold flags set from the CLI. Reading that as "cleared" would wipe them on the
- // first save from this panel.
+ // Absent, not null: a legacy blob predates the editor and the server may hold CLI-set flags.
+ // Reading that as "cleared" would wipe them on the first save from this panel.
llamaExtraArgs: undefined,
// Carry legacy GPU Memory knobs; normalizeGpuFields drops anything malformed.
gpuMemoryMode:
@@ -828,7 +806,8 @@ function migrateLegacyLoadSettingsOnce(): void {
return;
}
const map = readMapRaw();
- // Snapshot existing entries so eviction protects them: importing old load settings must never discard a newer config.
+ // Snapshot existing entries so eviction protects them: importing old load settings must
+ // never discard a newer config.
const existingKeys = new Set(Object.keys(map));
const migratedKeys = mergeLegacyEntries(
map,
@@ -874,9 +853,8 @@ function readMap(): StoredMap {
return readMapRaw();
}
-/** Fires when any model's saved config changes, in this tab. The browser's own
- * `storage` event only reaches *other* tabs, so readers that need to react to
- * an edit made here (the picker's memory bar) have nothing else to listen to. */
+/** Fires when any model's saved config changes, in this tab. The browser's `storage` event
+ * only reaches OTHER tabs, so readers like the picker's memory bar have nothing else. */
export const PER_MODEL_CONFIG_UPDATED_EVENT =
"unsloth-per-model-config-updated";
@@ -890,8 +868,8 @@ function writeMap(map: StoredMap): boolean {
console.warn("Failed to persist per-model config:", err);
return false;
}
- // Best-effort: the write already landed, so a host that cannot dispatch
- // events must not make a saved config report back as unsaved.
+ // Best-effort: the write already landed, so a host that cannot dispatch events must not make
+ // a saved config report back as unsaved.
if (typeof window?.dispatchEvent === "function") {
window.dispatchEvent(new Event(PER_MODEL_CONFIG_UPDATED_EVENT));
}
@@ -930,8 +908,8 @@ function normalizeV1(partial: RawConfig): PerModelConfig {
Number.isFinite(partial.specDraftNMax)
? Math.max(1, Math.min(16, Math.round(partial.specDraftNMax)))
: null;
- // Tied to the mode like specDraftNMax: a dtype stored under a mode with no
- // separate drafter shows a row for a context that never exists.
+ // Tied to the mode like specDraftNMax: a dtype stored under a mode with no separate drafter
+ // shows a row for a context that never exists.
const specDraftCacheDtype =
speculativeType != null &&
SEPARATE_DRAFT_MODEL_SPEC_TYPES.has(speculativeType) &&
@@ -997,9 +975,8 @@ function normalizeV1(partial: RawConfig): PerModelConfig {
};
}
-/**
-* A config in the exact shape storage keeps it in: the UI carries sentinels storage does not
-* (Speculative Decoding "auto" canonicalizes to null), which would read as non-default. */
+/** A config in the exact shape storage keeps: the UI carries sentinels storage does not
+ * (Speculative Decoding "auto" canonicalizes to null), which would read as non-default. */
export function normalizePerModelConfig(raw: unknown): PerModelConfig {
return normalize(raw);
}
@@ -1019,15 +996,10 @@ function normalize(raw: unknown): PerModelConfig {
function toStoredConfig(config: PerModelConfig): StoredPerModelConfig {
const normalized = normalize(config);
- // Stamped with the OLDEST version that still understands every field present, so
- // a record an older client can safely rewrite is not needlessly locked away from
- // it. Only a record carrying a newer field is put out of that client's reach.
- // Only a TRUE disableVision needs the v4 stamp. The default is false, which
- // is what a pre-vision client reconstructs anyway, so a record that merely
- // carries the key at its default loses nothing by staying in that client's
- // reach -- and stamping every record v4 would put the whole store out of it.
- // The tuning group above it follows the same rule: only a record that actually
- // sets one of the four is put out of a pre-v5 client's reach.
+ // Stamped with the OLDEST version that still understands every field present, so a record an
+ // older client can safely rewrite stays in its reach. Only a TRUE disableVision needs v4:
+ // false is what a pre-vision client reconstructs anyway, and stamping every record v4
+ // would put the whole store out of reach. The tuning group follows the same rule.
const hasServerTuning =
normalized.loadMode != null ||
normalized.specDraftCacheDtype != null ||
@@ -1192,11 +1164,9 @@ export function isDefaultConfig(config: PerModelConfig): boolean {
config.nParallel == null &&
config.nBatch == null &&
config.nUbatch == null &&
- // The llama-server tuning group, for the same reason as the arguments below:
- // savePerModelConfig deletes an entry it judges default, so a config whose only
- // change was one of these was dropped on the way to storage while the settings
- // page reported that defaults were kept. Compared against null, not truth: 0
- // checkpoints and a 0 or -1 cache are values, not blanks.
+ // The tuning group, for the same reason as the arguments below: savePerModelConfig deletes
+ // an entry it judges default, so a config changing only these was dropped on the way to
+ // storage. Compared against null, not truth: 0 checkpoints and a 0 or -1 cache are values.
(config.specDraftCacheDtype ?? null) === null &&
(config.loadMode ?? null) === null &&
config.ctxCheckpoints == null &&
@@ -1206,14 +1176,15 @@ export function isDefaultConfig(config: PerModelConfig): boolean {
Boolean(config.disableVision) ===
Boolean(DEFAULT_PER_MODEL_CONFIG.disableVision) &&
(config.chatTemplateOverride ?? null) === null &&
- // Or a config whose only change is Extra Arguments reads as default, and
- // savePerModelConfig deletes the entry it was asked to remember.
+ // Or a config whose only change is Extra Arguments reads as default, and savePerModelConfig
+ // deletes the entry it was asked to remember.
(config.llamaExtraArgs == null || config.llamaExtraArgs.length === 0) &&
gpuFieldsAtDefault(config)
);
}
-// GPU knobs are "default" when mode is Auto with no explicit choice: gpuLayers < 0/absent, nCpuMoe 0/absent, selectedGpuIds null/absent.
+// GPU knobs are "default" when mode is Auto with no explicit choice: gpuLayers < 0 or absent,
+// nCpuMoe 0 or absent, selectedGpuIds null or absent.
function gpuFieldsAtDefault(config: PerModelConfig): boolean {
return (
(config.gpuMemoryMode ?? "auto") === "auto" &&
@@ -1227,10 +1198,9 @@ export function savePerModelConfig(
modelId: string,
ggufVariant: string | null | undefined,
config: PerModelConfig,
- /**
- * Receives models dropped to stay inside the storage budget. Eviction is silent and still
- * reports success, so without this their server overrides would keep applying with nothing
- * in the UI able to forget them. */
+ /** Receives models dropped to stay inside the storage budget. Eviction is silent and still
+ * reports success, so without this their server overrides would keep applying with
+ * nothing in the UI able to forget them. */
evicted?: { modelId: string; ggufVariant: string | null }[],
): boolean {
if (
@@ -1419,8 +1389,9 @@ export function adoptLegacyConfigKey(
const [key] = storageKeysForModelVariant(modelId, ggufVariant);
map[key] = toStoredConfig(legacy);
}
- // Only the key strings change length, and a repo id is normally shorter than the snapshot path.
- // If it is not and that tips the map past its byte cap, leave storage as it was: eviction is not undoable.
+ // Only the key strings change length, and a repo id is normally shorter than the snapshot
+ // path. If it is not and that tips the map past its byte cap, leave storage as it was:
+ // eviction is not undoable.
const bytesAfter = serializedMapSize(map);
if (
bytesAfter > bytesBefore &&
@@ -1458,15 +1429,11 @@ export function resolveInitialConfig(
return { config: { ...DEFAULT_PER_MODEL_CONFIG }, remembered: false };
}
-/**
-* Remembered settings for the identifier ``/api/inference/status`` reports as loaded.
-*
-* An API auto-switch hands the loader the concrete snapshot path (the resolver index only holds
-* paths), so ``model_identifier`` names that path while this model's settings are keyed by its
-* repo id. Reading the raw identifier alone reports the resident model as unremembered, blanking
-* a control it is running with, which the next save writes back over the saved record. Only a
-* namespaced collapse is adopted, per ``residentModelIdMatches``: an HF snapshot collapses onto
-* a repo id naming exactly one model, while other paths collapse onto a shareable stem. */
+/** Remembered settings for the identifier /api/inference/status reports as loaded. An API
+ * auto-switch hands the loader a concrete snapshot path while settings are keyed by repo
+ * id, so reading the raw identifier reports the resident model as unremembered and blanks
+ * a control it is running with. Only a namespaced collapse is adopted, per
+ * residentModelIdMatches. */
export function resolveResidentInitialConfig(
modelId: string,
ggufVariant?: string | null,
diff --git a/studio/frontend/src/features/video/api.ts b/studio/frontend/src/features/video/api.ts
index 57b70a0907f..5c5eb7f87d4 100644
--- a/studio/frontend/src/features/video/api.ts
+++ b/studio/frontend/src/features/video/api.ts
@@ -8,9 +8,10 @@ import type { DiffusionDownloadPlan } from "@/features/images/api";
import { apiUrl } from "@/lib/api-base";
import { readFastApiError } from "@/lib/format-fastapi-error";
-// One Advanced control's resolved value + provenance, same shape as the diffusion status. `value` is the engaged value
-// (string, null when off, or boolean), `requested` is what the caller asked for (null = left to the backend), `source` is
-// "auto" or "explicit", `status` says whether the ask survived, and `reason` is the tooltip.
+// One Advanced control's resolved value plus provenance, same shape as the diffusion status.
+// `value` is engaged, `requested` is what the caller asked for (null = left to the backend),
+// `source` is "auto" or "explicit", `status` says whether the ask survived, `reason` is the
+// tooltip.
export interface VideoResolvedControl {
value: string | boolean | null;
// Absent on backends predating the requested/actual split.
@@ -80,14 +81,16 @@ export interface VideoStatus {
h3_task?: string | null;
// Per-family generation defaults + shape constraints; null when unloaded.
defaults?: VideoGenerationDefaults | null;
- // Per-control provenance keyed by control name (memory_mode, speed_mode, attention_backend, transformer_cache), read by
- // the "Auto: X" badges. Null when nothing is loaded or the backend does not record it.
+ // Per-control provenance keyed by control name, read by the "Auto: X" badges. Null when
+ // nothing is loaded or the backend does not record it.
+ // The names are memory_mode, speed_mode, attention_backend and transformer_cache.
resolved?: Record<string, VideoResolvedControl> | null;
}
export interface VideoGenerateProgress {
active: boolean;
- // "queued" | "denoise" | "decode" | "export" | "completed" | "failed" | null; the terminal phases carry the background job's outcome.
+ // "queued" | "denoise" | "decode" | "export" | "completed" | "failed" | null; the terminal
+ // phases carry the background job's outcome.
phase?: string | null;
step: number;
total: number;
@@ -108,10 +111,12 @@ export interface VideoLoadProgress {
export interface VideoLoadRequest {
model_path: string;
- // Required for the gguf / single_file kinds, omitted for a full pipeline (a diffusers repo loaded via from_pretrained).
+ // Required for the gguf / single_file kinds, omitted for a full pipeline loaded via
+ // from_pretrained.
gguf_filename?: string;
- // How to load the model (omit to auto-detect from gguf_filename): "gguf", "single_file" (safetensors transformer) or
- // "pipeline" (a full diffusers repo). Non-GGUF kinds are restricted to unsloth/* or family bases.
+ // How to load the model (omit to auto-detect from gguf_filename): "gguf", "single_file"
+ // (safetensors transformer) or "pipeline". Non-GGUF kinds are restricted to unsloth/* or
+ // family bases.
model_kind?: "gguf" | "single_file" | "pipeline";
base_repo?: string;
family_override?: string;
@@ -133,11 +138,13 @@ export interface VideoLoadRequest {
| "aiter";
transformer_cache?: "off" | "fbcache";
transformer_cache_threshold?: number;
- // Dense DiT precision on full-pipeline loads (omit for the hardware ladder; "none" pins bf16). GGUF / single-file checkpoints carry their own.
+ // Dense DiT precision on full-pipeline loads (omit for the hardware ladder; "none" pins bf16).
+ // GGUF / single-file checkpoints carry their own.
transformer_quant?: "none" | "fp8" | "int8" | "nvfp4" | "mxfp8";
// Pipeline denoiser partition. GGUF filenames already identify theirs.
h3_task?: "fl2va" | "ref2va";
- // CUDA / ROCm physical indices this load may use; omit for automatic. Neither engine shards a checkpoint, so several cards resolve to the one with the most free VRAM.
+ // CUDA / ROCm physical indices this load may use; omit for automatic. Neither engine shards a
+ // checkpoint, so several cards resolve to the one with the most free VRAM.
gpu_ids?: number[];
// Text-encoder precision (omit to keep the dense bf16 encoder). Refused with a 409 when the host cannot run it.
text_encoder_quant?: "fp8" | "fp8_dynamic" | "int8" | "nvfp4";
@@ -157,9 +164,10 @@ export interface VideoReferenceVideo {
export interface VideoGenerateRequest {
prompt: string;
negative_prompt?: string;
- // Width/height/num_frames/fps default per loaded family, so they are optional. When sent they must match that family's
- // rules -- width/height one of status.defaults.resolution_presets, num_frames on the k*frame_step+1 lattice -- or the
- // backend answers 422 with the supported shapes (the same rules the video page's selects are built from).
+ // Width/height/num_frames/fps default per loaded family, so they are optional. When sent they
+ // must match that family's rules -- a resolution preset, and num_frames on the
+ // k*frame_step+1 lattice -- or the backend answers 422 with the supported shapes.
+ // Width/height must be one of status.defaults.resolution_presets.
width?: number;
height?: number;
num_frames?: number;
@@ -202,8 +210,8 @@ export interface GalleryVideo {
flow_shift?: number | null;
audio_flow_shift?: number | null;
model?: string | null;
- // The load-time BUILD, all ENGAGED values, so a clip's recipe still names the precision it ran at
- // once the model is unloaded. Absent on sidecars written before this existed.
+ // The load-time BUILD, all ENGAGED values, so a clip's recipe still names the precision it ran
+ // at once the model is unloaded. Absent on sidecars written before this existed.
model_kind?: string | null;
gguf_filename?: string | null;
transformer_quant?: string | null;
@@ -250,8 +258,8 @@ export async function getVideoGenerateProgress(): Promise<VideoGenerateProgress>
}
export async function loadVideoModel(body: VideoLoadRequest): Promise<VideoStatus> {
- // Announced so the indicator shows the load while the toast does, and settled
- // from load-progress because this POST only starts it. See images.
+ // Announced so the indicator shows the load while the toast does, and settled from
+ // load-progress because this POST only starts it. See images.
return withBackgroundLoadNotice(
"video",
body.model_path,
@@ -280,7 +288,8 @@ export async function getVideoDownloadPlan(
);
}
-/** Start a generation job. Returns as soon as the backend accepts it (a clip takes minutes and secure mode's tunnel caps responses near 100s); poll getVideoGenerateProgress for completion. */
+/** Start a generation job. Returns as soon as the backend accepts it (a clip takes minutes and
+ * secure mode's tunnel caps responses near 100s); poll getVideoGenerateProgress. */
export async function generateVideo(
body: VideoGenerateRequest,
): Promise<VideoGenerateResponse> {
@@ -293,7 +302,8 @@ export async function generateVideo(
);
}
-/** Request a cancel. Best-effort: the backend stops at the next step boundary and raises the cancelled sentinel, which the caller maps to a 409. */
+/** Request a cancel. Best-effort: the backend stops at the next step boundary and raises the
+ * cancelled sentinel, which the caller maps to a 409. */
export async function cancelVideoGeneration(): Promise<{ cancelled: boolean }> {
return parseJson(
await authFetch("/api/inference/video/generate/cancel", { method: "POST" }),
@@ -346,7 +356,10 @@ export async function clearVideoGallery(): Promise<void> {
if (!res.ok) throw new Error(await readFastApiError(res));
}
-/** A directly playable, range-capable URL for one gallery clip. Not the images-gallery blob treatment: an MP4 is tens to hundreds of MB, so res.blob() would download the whole clip before playback, defeat seeking and pin those bytes. The backend's file route streams ranges but is bearer-gated, so mint a short-lived signed link. */
+/** A directly playable, range-capable URL for one gallery clip. Not the images-gallery blob
+ * treatment: an MP4 is tens to hundreds of MB, so res.blob() would download the whole clip
+ * before playback, defeat seeking and pin those bytes. The backend's file route streams
+ * ranges but is bearer-gated, so mint a short-lived signed link. */
export async function fetchGalleryVideoSignedUrl(id: string): Promise<string> {
const res = await authFetch(
`/api/inference/video/gallery/${encodeURIComponent(id)}/signed-url`,
@@ -354,12 +367,13 @@ export async function fetchGalleryVideoSignedUrl(id: string): Promise<string> {
if (!res.ok) throw new Error(await readFastApiError(res));
const body = (await res.json()) as { url?: string };
if (!body.url) throw new Error("The server returned no video link.");
- // Absolute because consumers bypass authFetch, and a relative path under Tauri
- // resolves against the webview origin. No-op in the browser (empty apiBase).
+ // Absolute because consumers bypass authFetch, and a relative path under Tauri resolves
+ // against the webview origin. No-op in the browser (empty apiBase).
return apiUrl(body.url);
}
-/** Server-side transcode for the Download menu (WebM / GIF). The backend 501s with a readable message when the codec is unavailable. */
+/** Server-side transcode for the Download menu (WebM / GIF). The backend 501s with a readable
+ * message when the codec is unavailable. */
export async function fetchGalleryVideoExport(
id: string,
format: "webm" | "gif",
diff --git a/studio/frontend/src/features/video/keyframe-canvas.ts b/studio/frontend/src/features/video/keyframe-canvas.ts
index 7d9493a7ade..f3139a83ee4 100644
--- a/studio/frontend/src/features/video/keyframe-canvas.ts
+++ b/studio/frontend/src/features/video/keyframe-canvas.ts
@@ -6,10 +6,8 @@ import type { VideoGenerationDefaults } from "./api";
/** Sentinel for deriving resolution from the keyframe aspect ratio. */
export const MATCH_SOURCE_RESOLUTION = -1;
-/**
- * Preview the backend-owned canvas rule from status defaults.
- * Returns null when the family lacks a rule or the aspect ratio is unsupported.
- */
+/** Preview the backend-owned canvas rule from status defaults. Returns null when the family lacks a
+ * rule or the aspect ratio is unsupported. */
export function matchedCanvas(
aspectWidth: number,
aspectHeight: number,
diff --git a/studio/frontend/src/features/video/reference-budget.ts b/studio/frontend/src/features/video/reference-budget.ts
index 97b13b53382..183ad2317fa 100644
--- a/studio/frontend/src/features/video/reference-budget.ts
+++ b/studio/frontend/src/features/video/reference-budget.ts
@@ -17,25 +17,17 @@ export function hasReferenceCapacity(
export type ReferenceKind = "video" | "audio";
-/**
- * Room for the `data:<mime>;base64,` FileReader prepends.
- *
- * Reserved rather than measured, because the cap has to be known before the FileReader runs and
- * the MIME string is whatever the OS reported: `video/mp4` makes a 22 character prefix and the
- * long-winded ones (`video/x-matroska`, `audio/vnd.wave`) are half as long again. 256 covers any
- * of them and costs 192 bytes of the advertised limit, which does not move the rounded MB figure.
- */
+/** Room for the `data:<mime>;base64,` prefix FileReader prepends. Reserved rather than measured,
+ * because the cap has to be known before the FileReader runs and the MIME string is whatever the
+ * OS reported: `video/mp4` makes 22 characters and the long-winded ones half as long again. 256
+ * covers any of them and costs 192 bytes of the advertised limit. */
const DATA_URL_HEADER_BUDGET = 256;
-/**
- * The largest raw file of each kind whose data URL still fits the backend's cap.
- *
- * The caps in models/inference.py bound the STRING, not the file: 96 MiB for a reference video and
- * 32 MiB for its soundtrack. Base64 costs 4 characters per 3 bytes, so a plain three quarters is
- * off by the header, and a file at exactly that size was accepted here and then 422'd during
- * request validation. Floor to a multiple of 3 as well, so the encode is exactly (n / 3) * 4 with
- * no padding to account for.
- */
+/** The largest raw file of each kind whose data URL still fits the backend's cap. The caps in
+ * models/inference.py bound the STRING, not the file: 96 MiB for a reference video and 32 MiB
+ * for its soundtrack. Base64 costs 4 characters per 3 bytes, so a plain three quarters is off by
+ * the header and a file at exactly that size was accepted and then 422'd. Floor to a multiple of
+ * 3 as well, so the encode is exactly (n / 3) * 4 with no padding. */
function rawLimitFor(base64Cap: number): number {
return Math.floor(((base64Cap - DATA_URL_HEADER_BUDGET) * 3) / 4 / 3) * 3;
}
@@ -96,14 +88,10 @@ export function referenceFileRejection(
return null;
}
-/**
- * Read one reference file into the data URL the request carries.
- *
- * The size check happens BEFORE the FileReader exists, because a data URL costs roughly 2.33x the
- * file in renderer memory and it is built long before the backend's 422 can arrive. H3 reference
- * clips are 2 to 15 seconds by spec, so a 15 second 4K phone clip clears the cap routinely; chat
- * attachments, OpenDocument imports and the seed dialog all guard the same way.
- */
+/** Read one reference file into the data URL the request carries. The size check happens BEFORE the
+ * FileReader exists, because a data URL costs roughly 2.33x the file in renderer memory and is
+ * built long before the backend's 422 can arrive. H3 reference clips are 2 to 15 seconds by
+ * spec, so a 15 second 4K phone clip clears the cap routinely. */
export function readReferenceFile(
kind: ReferenceKind,
file: File | undefined | null,
diff --git a/studio/frontend/src/features/video/reference-image-crop.ts b/studio/frontend/src/features/video/reference-image-crop.ts
index 62548a62f6b..686d4acafdf 100644
--- a/studio/frontend/src/features/video/reference-image-crop.ts
+++ b/studio/frontend/src/features/video/reference-image-crop.ts
@@ -45,10 +45,10 @@ export interface CropRasterCanvas {
// Keep this aligned with VideoGenerateRequest.reference_images in models/inference.py.
export const MAX_REFERENCE_IMAGE_DATA_URL_LENGTH = 32 * 1024 * 1024;
-// fit_h3_reference_image never keeps more than a 2048px short edge, so a full-resolution
-// export just spends the 32 MiB cap and a blocking encode on pixels it discards: a 12MP
-// photo alone reaches ~52 MiB of PNG base64. The long edge is bounded too, so an extreme
-// aspect cannot stay huge just because its short edge fits.
+// fit_h3_reference_image never keeps more than a 2048px short edge, so a full-resolution export
+// just spends the 32 MiB cap and a blocking encode on pixels it discards: a 12MP photo alone
+// reaches ~52 MiB of PNG base64. The long edge is bounded too, so an extreme aspect cannot stay
+// huge just because its short edge fits.
export const MAX_REFERENCE_CROP_SHORT_EDGE = 2048;
export const MAX_REFERENCE_CROP_LONG_EDGE = 4096;
diff --git a/studio/frontend/src/features/video/reference-picker.tsx b/studio/frontend/src/features/video/reference-picker.tsx
index 155ee09df23..e0d54f97c34 100644
--- a/studio/frontend/src/features/video/reference-picker.tsx
+++ b/studio/frontend/src/features/video/reference-picker.tsx
@@ -33,13 +33,10 @@ export interface ReferenceMedia {
/** How long to wait for a browser to report a clip's duration before giving up on it. */
export const REFERENCE_DURATION_TIMEOUT_MS = 15_000;
-/** Read a clip's duration, resolving undefined when the browser cannot report one.
- *
- * An element firing neither loadedmetadata nor error would leave this pending forever, and
- * its picker slot with it, so the wait is bounded; callers already treat an unknown duration
- * as "no auto trim". The source stays a data URL because WebKit reports no metadata for an
- * object URL, and losing the duration costs more than the extra parse saves.
- */
+/** Read a clip's duration, resolving undefined when the browser cannot report one. An element
+ * firing neither loadedmetadata nor error would leave this pending forever, and its picker slot
+ * with it, so the wait is bounded; callers already treat an unknown duration as "no auto trim".
+ * The source stays a data URL because WebKit reports no metadata for an object URL. */
function readVideoDuration(dataUrl: string): Promise<number | undefined> {
return new Promise((resolve) => {
const media = document.createElement("video");
@@ -122,8 +119,8 @@ export function ReferenceMediaPicker({
[gate, kind, onChange],
);
- // Tauri suppresses the webview's own drop events, so the handlers below never
- // fire on the desktop app; this claims the OS drop for the button (#9036).
+ // Tauri suppresses the webview's own drop events, so the handlers below never fire on the desktop
+ // app; this claims the OS drop for the button (#9036).
const { ref: dropRef, dragging, dragHandlers } = useNativeFileDrop({
onFiles: (files) => void readFile(files[0]),
accept: REFERENCE_DROP_ACCEPT[kind],
diff --git a/studio/frontend/src/features/video/reference-trim.ts b/studio/frontend/src/features/video/reference-trim.ts
index 3c6a3876421..040ccbbbbca 100644
--- a/studio/frontend/src/features/video/reference-trim.ts
+++ b/studio/frontend/src/features/video/reference-trim.ts
@@ -34,8 +34,8 @@ export function referenceVideoTrimError(
end: number | null,
sourceDuration?: number,
): string | null {
- // No interval inside a too-short source can reach the minimum, so say so here instead of
- // letting the decoder refuse it later.
+ // No interval inside a too-short source can reach the minimum, so say so here instead of letting
+ // the decoder refuse it later.
if (
sourceDuration !== undefined &&
sourceDuration + DURATION_EPSILON < H3_REFERENCE_MIN_SECONDS
diff --git a/studio/frontend/src/features/video/video-page.tsx b/studio/frontend/src/features/video/video-page.tsx
index 9720c93192e..242fef9264c 100644
--- a/studio/frontend/src/features/video/video-page.tsx
+++ b/studio/frontend/src/features/video/video-page.tsx
@@ -176,10 +176,10 @@ import {
unloadVideoModel,
} from "./api";
-// Curated models come from the shared catalog: one canonical group per model with its artifacts as data (HunyuanVideo carries both repacks), and the load kind per artifact via loadSpecFor.
-// The picker renders groups with a format second level, which also surfaces LTX-2.3 in Recommended (its HF pipeline_tag is image-to-video).
-// Host-dependent, so it is built per render rather than once at module load: a Mac is offered
-// only the GGUF rows, and an accelerated host gets the speed qualifiers.
+// Curated models come from the shared catalog, one group per model with a format second level
+// (which also surfaces LTX-2.3 in Recommended). Host-dependent: a Mac gets only GGUF rows.
+// The format second level also surfaces LTX-2.3 in Recommended, since its HF pipeline_tag is
+// image-to-video. The load kind per artifact comes from loadSpecFor.
function useVideoModels(host: HostClass): ModelOption[] {
return useMemo(() => catalogToModelOptions(VIDEO_CATALOG, host), [host]);
}
@@ -193,9 +193,9 @@ const MODEL_DEFAULTS: Array<{ match: string; steps: number; guidance: number }>
// "distilled" before the generic "ltx": the distilled model runs at 8 steps, guidance 1.
{ match: "distilled", steps: 8, guidance: 1 },
{ match: "ltx", steps: 40, guidance: 4 },
- // Wan2.2 pipelines default to 50 steps at CFG 5.0 (verified in diffusers 0.39). The backend supplies the fps per family.
+ // Wan2.2 pipelines default to 50 steps at CFG 5.0 (diffusers 0.39). The backend supplies the fps per family.
{ match: "wan", steps: 50, guidance: 5 },
- // HunyuanVideo-1.5 runs 50 steps; guidance 6 matches the guider the repo ships (there is no pipeline kwarg).
+ // HunyuanVideo-1.5 runs 50 steps; guidance 6 matches the guider the repo ships.
{ match: "hunyuanvideo", steps: 50, guidance: 6 },
];
@@ -204,7 +204,7 @@ function defaultsFor(repoId: string): { steps: number; guidance: number } {
return MODEL_DEFAULTS.find((d) => id.includes(d.match)) ?? DEFAULT_GEN;
}
-// Resolution presets offered before a model is loaded. Once loaded, status.defaults.resolution_presets replaces these.
+// Resolution presets offered before a model is loaded; status.defaults.resolution_presets replaces these once loaded.
const FALLBACK_RESOLUTION_PRESETS: Array<[number, number]> = [
[768, 512],
[1216, 704],
@@ -217,24 +217,27 @@ const FALLBACK_FRAME_OFFSET = 1;
const FALLBACK_FPS = 24;
const FALLBACK_DURATION_TARGETS = [1, 2, 3, 5];
-// Module cache of the backend-persisted gallery, so a tab switch re-renders instantly. The srcById entries are short-lived
-// signed links, not object URLs: nothing is pinned in the webview, and the media element streams ranges as it plays.
+// Module cache of the backend-persisted gallery, so a tab switch re-renders instantly. The
+// srcById entries are short-lived signed links, not object URLs: nothing is pinned.
const galleryCache: {
videos: GalleryVideo[];
hasMore: boolean;
selectedId: string | null;
quant: string | null;
- // id -> the signed link and when it was minted. The link is short-lived and its signing secret is per-process, while this
- // cache survives navigation, so an entry has to be re-mintable or playback, seeking and Save would 401 until a reload.
+ // id -> the signed link and when it was minted. The link is short-lived and its signing secret
+ // is per-process while this cache survives navigation, so an entry has to be re-mintable or
+ // playback would 401 until a reload.
srcById: Map<string, { url: string; mintedAt: number }>;
- // Ids re-minted once after a media error already, so a clip that is broken for any other reason cannot spin in a mint/error loop.
+ // Ids re-minted once after a media error already, so a clip broken for any other reason cannot
+ // spin in a mint/error loop.
refreshed: Set<string>;
- // Ids with a mint in flight, so concurrent ensureSrc calls don't double-request.
+ // Ids with a mint in flight, so concurrent ensureSrc calls do not double-request.
inflight: Set<string>;
- // Ids deleted while their link was still being minted, so a reply landing after the delete is not cached. Clear-all bumps the epoch instead.
+ // Ids deleted while their link was still being minted, so a reply landing after the delete is
+ // not cached. Clear-all bumps the epoch instead.
deleted: Set<string>;
- /** Clips archived locally: a terminal progress response snapshotted before the archive cannot be
- * revoked, so the merges below must refuse it or the clip returns to the active strip. */
+ /** Clips archived locally: a terminal progress response snapshotted before the archive cannot
+ * be revoked, so the merges below must refuse it or the clip returns to the strip. */
archived: Set<string>;
epoch: number;
} = {
@@ -250,14 +253,14 @@ const galleryCache: {
epoch: 0,
};
-// Re-mint a cached link once it is this old, comfortably inside the backend's own expiry, so a long-lived tab keeps working.
+// Re-mint a cached link once it is this old, comfortably inside the backend's own expiry.
const VIDEO_LINK_REFRESH_MS = 6 * 60 * 60 * 1000;
// Videos loaded per infinite-scroll page.
const PAGE_SIZE = 50;
-// Passes a window resync may make before giving up. Each extra pass only happens when pagination
-// moved while it was fetching, which cannot repeat indefinitely without the user scrolling along.
+// Passes a window resync may make before giving up: each extra pass only happens when
+// pagination moved while it was fetching.
const RESYNC_MAX_ATTEMPTS = 3;
// Export filename, e.g. Unsloth_video_20260624-143005_123.mp4.
@@ -274,8 +277,8 @@ function exportFilename(video: GalleryVideo, format: VideoExportFormat = "mp4"):
}
// MP4 streams from its signed link to the chosen path: that link is cross-origin under Tauri,
-// where an anchor no longer saves, and a clip is too big to hold in memory on the way past.
-// WebM / GIF are transcoded by the backend on demand (501 when the codec is absent).
+// where an anchor no longer saves, and a clip is too big to hold in memory. WebM / GIF are
+// transcoded by the backend on demand.
async function downloadVideo(
src: string,
video: GalleryVideo,
@@ -312,14 +315,15 @@ function clipMeta(video: GalleryVideo): string {
function genStepLabel(p: VideoGenerateProgress): string {
if (p.phase === "decode") return "Decoding video and audio…";
if (p.phase === "export") return "Encoding video…";
- // Text encoding and the first-step warmup run before the first scheduler tick, so step 0 means "working, not denoising yet" -- up to a minute at 720p.
+ // Text encoding and the first-step warmup run before the first scheduler tick, so step 0 means
+ // "working, not denoising yet" - up to a minute at 720p.
if (p.step === 0) return "Preparing (text encoding + warmup)…";
const base = p.total > 0 ? `Denoising step ${p.step}/${p.total}` : "Denoising…";
const eta = p.eta_seconds != null ? formatEta(p.eta_seconds) : "";
return eta ? `${base} · ~${eta}` : base;
}
-// The chat tab's model-load toast styling, reused verbatim so the video load toast is visually identical.
+// The chat tab's model-load toast styling, reused verbatim so the video load toast is identical.
const LOAD_TOAST_CLASSNAMES = {
toast: "chat-model-load-toast items-center gap-2.5",
content: "gap-0.5 flex-1 min-w-0",
@@ -327,7 +331,8 @@ const LOAD_TOAST_CLASSNAMES = {
description: "mt-0 w-full",
} as const;
-// The download total for a video load can only be estimated from a companion base repo, so the toast shows a byte count until the total is known.
+// The download total for a video load can only be estimated from a companion base repo, so
+// the toast shows a byte count until the total is known.
function loadFraction(p: VideoLoadProgress): number | null {
if (!p.expected_bytes || p.expected_bytes <= 0) return null;
return Math.min(1, p.downloaded_bytes / p.expected_bytes);
@@ -359,10 +364,8 @@ function loadToastDescription(p: VideoLoadProgress) {
);
}
-// Toast args mirroring chat: persistent, closeable, content in `description`. Pass `id` to update in place.
-// `onCancel` adds chat's Cancel action, the one control that reaches a load already in flight: the model
-// selector's eject is hidden for exactly the span a first load runs (nothing is resident, so it has no
-// selection to eject), which left a multi-gigabyte pull with no way out.
+// Toast args mirroring chat; `id` updates in place. `onCancel` adds chat's Cancel, the one
+// control that reaches a load in flight: the selector's eject is hidden for that span.
function loadToastArgs(
p: VideoLoadProgress,
id?: string | number,
@@ -417,7 +420,7 @@ function SliderField({
);
}
-// Matches the field-label style used across Unsloth (export/chat settings).
+// Matches the field-label style used across Unsloth.
function Field({
label,
hint,
@@ -440,10 +443,9 @@ function Field({
);
}
-// The badge for one Advanced control: "Auto: X" when the backend decided it, "NVFP4 -> OFF" in a
-// warning tone when an EXPLICIT request was declined. The old rule rendered nothing for the second
-// case, which is how a clip could be labelled BF16 while telemetry confirmed NVFP4 (and the other
-// way round). Same markup and helpers as the images page ResolvedBadge.
+// The badge for one Advanced control: "Auto: X" when the backend decided, "NVFP4 -> OFF" in a
+// warning tone when an EXPLICIT request was declined. The old rule rendered nothing there,
+// so a clip could be labelled BF16 while telemetry confirmed NVFP4.
function ResolvedBadge({
status,
controlKey,
@@ -487,12 +489,9 @@ function BuildRow({ label, value, badge }: { label: string; value: string; badge
);
}
-/**
- * What the LOADED model is actually running, read from status (never from the request): the DiT and
- * text-encoder precision, the memory mode with its resolved offload behaviour, and the attention
- * backend. The Advanced selects above say what was ASKED for; this says what happened, and any
- * control whose request was declined carries its reason in the badge tooltip.
- */
+/** What the LOADED model is actually running, read from status and never from the request: DiT
+ * and text-encoder precision, memory mode with its offload behaviour, and the attention
+ * backend. A declined control carries its reason in the badge tooltip. */
function LoadedBuildSummary({ status }: { status: VideoStatus | null }) {
if (!status?.loaded) return null;
const offload = status.offload_policy ?? "none";
@@ -545,10 +544,8 @@ function LoadedBuildSummary({ status }: { status: VideoStatus | null }) {
);
}
-/**
- * Report a failed load. A refused precision is a long actionable sentence, so it becomes a toast
- * description under a short title rather than one unreadable line. Mirrors the images page.
- */
+/** Report a failed load. A refused precision is a long actionable sentence, so it becomes a
+ * toast description under a short title. Mirrors the images page. */
function reportLoadFailure(message: string | null | undefined, fallback: string): void {
const text = (message || "").trim();
if (text && isPrecisionRefusal(text)) {
@@ -680,7 +677,7 @@ function RecipePopover({
/>
) : null}
{/* The load-time build, all ENGAGED values, so a saved clip can never be labelled with a
- precision that did not run. Matches what the images Recipe already shows. */}
+ precision that did not run. */}
{video.gguf_filename ? (
<RecipeRow label="File" value={video.gguf_filename} mono />
) : null}
@@ -762,9 +759,8 @@ type VideoLoadOptions = {
h3Task?: H3Task;
};
/** A pick held back while the user chooses the H3 partition. It carries what the deferred
- * loadOrStage call would otherwise have been given inline, so the choice only adds `h3Task`:
- * `source` decides whether the pick is preflighted against the Hub download plan or loaded
- * straight off disk, and a dialog must not silently change that for the pick it is holding. */
+ * loadOrStage call would have been given inline, so the choice only adds `h3Task`; `source`
+ * decides whether the pick is preflighted against the Hub plan or loaded off disk. */
type PendingH3Load = {
repoId: string;
opts: VideoLoadOptions;
@@ -775,15 +771,9 @@ type PendingH3Load = {
const H3_BF16_REPO = "MiniMaxAI/MiniMax-H3";
/** Whether a pick is the H3 base pipeline, whose denoiser partition the user must choose.
- * Shared by both entry points: a chat-picker pick arrives as ?model= and reaches loadOrStage
- * without passing through handleModelSelect, so checking it in one place staged the default
- * fl2va partition, tens of GB, with no way to ask for References.
- *
- * An on-device copy counts. The same pipeline added as a directory reaches the generic
- * local-pipeline branch, which a Hub-id equality test never recognises, so an omitted h3_task
- * pinned it to fl2va and its transformer_ref partition was unreachable even with the weights
- * sitting on disk. Matched on the final path segment, the same way a local checkpoint's family
- * is read off its filename elsewhere. */
+ * Shared by both entry points, since a chat-picker pick reaches loadOrStage without passing
+ * through handleModelSelect. An on-device copy counts, and a Hub-id equality test never
+ * recognises one, so it is matched on the final path segment. */
function isH3PipelinePick(repoId: string, kind: VideoLoadOptions["kind"]): boolean {
if (kind !== "pipeline") return false;
const id = repoId.toLowerCase();
@@ -792,8 +782,8 @@ function isH3PipelinePick(repoId: string, kind: VideoLoadOptions["kind"]): boole
return leaf === H3_BF16_REPO.split("/")[1].toLowerCase();
}
-// What a pick optimistically replaced, so a load that never takes can put all of it back. The quant
-// label and the generation recipe move together at pick time, so they have to roll back together too.
+// What a pick optimistically replaced, so a load that never takes can put it all back. The
+// quant label and the recipe move together at pick time, so they roll back together.
type PickRevert = {
prev: string | null;
steps: number;
@@ -826,17 +816,10 @@ function VideoGate({ children }: { children: ReactNode }) {
);
}
-/**
- * Capability gate in front of the generator.
- *
- * The root guard never bounces /video: a chat-only host is both where the explanation below has
- * something to say (a CPU-only box) and where video works anyway (Apple Silicon whose only
- * problem is MLX). So the page answers for itself: spin while the answer is out, explain a no.
- *
- * That answer is /api/system/hardware's alone, which settles detection before replying. Waiting
- * on the chat-only verdict too would spin through an MLX self-heal /api/health holds it back for,
- * which cannot change a Metal answer.
- */
+/** Capability gate in front of the generator. The root guard never bounces /video: a chat-only
+ * host is both where the explanation has something to say and where video works anyway
+ * (Apple Silicon whose only problem is MLX), so the page answers for itself, from
+ * /api/system/hardware, which settles detection before replying. */
export function VideoPage({
active = true,
onInitialReady,
@@ -862,7 +845,7 @@ export function VideoPage({
}
// Only an authoritative "no" hides the generator; an older backend omits the field, which
- // arrives as null, and must keep the page it has always served.
+ // arrives as null and must keep the page it has always served.
if (hardware.videoSupported === false) {
return (
<VideoGate>
@@ -903,16 +886,16 @@ function VideoGenerator({
const [guidance, setGuidance] = useState(DEFAULT_GEN.guidance);
const modelSeeded = useRef(false);
const familySeeded = useRef(false);
- // Whether the user has taken the recipe since the pick that is still waiting for its status.
- // Both the seeding effects and the rollback below ask: a preset selected while the model
- // downloaded is newer than that pick, so neither its defaults nor its rollback may land on top.
+ // Whether the user has taken the recipe since the pick still waiting for its status: a preset
+ // selected while the model downloaded is newer, so neither its defaults nor its rollback
+ // may land on top.
const pickRecipeSuperseded = useRef<(() => boolean) | null>(null);
// Put back everything a pick optimistically applied. Setters are stable, so this never re-renders on its own.
const revertPick = useCallback((r: PickRevert) => {
setQuant(r.prev);
setPendingModelDefaults(null);
// Equality alone cannot tell "nobody touched this" from "the user chose the same number": a
- // preset selected after the pick owns these fields even where it matches what the pick applied.
+ // preset selected after the pick owns these fields.
if (!pickRecipeSuperseded.current?.()) {
setSteps((cur) => (cur === r.appliedSteps ? r.steps : cur));
setGuidance((cur) => (cur === r.appliedGuidance ? r.guidance : cur));
@@ -923,8 +906,8 @@ function VideoGenerator({
r.releaseRecipeClaim?.();
r.releaseRecipeClaim = undefined;
}, []);
- // The recipe a pick optimistically claimed, until status confirms it or a failed load reverts
- // it. Without this the Default preset would read as "modified" for the whole download.
+ // The recipe a pick optimistically claimed until status confirms it or a failed load reverts
+ // it; without this the Default preset reads as "modified" for the whole download.
const [pendingModelDefaults, setPendingModelDefaults] = useState<{
steps: number;
guidance: number;
@@ -963,18 +946,15 @@ function VideoGenerator({
const [durationIntentSeconds, setDurationIntentSeconds] = useState(
numFrames / FALLBACK_FPS,
);
- // Advanced options live in a right-docked panel, closed by default; a single fixed top-bar toggle opens it.
- // Sits inline under Seed; the open state is remembered across visits.
+ // Advanced options live in a right-docked panel, closed by default; the open state is remembered across visits.
const [advancedOpen, setAdvancedOpen] = usePersistedToggle(
"unsloth_video_advanced_open",
);
// Advanced (load-time) options; "auto"/"off" map to the backend defaults. "Reapply" reloads with new values.
const [memoryMode, setMemoryMode] = useState<"auto" | "fast" | "balanced" | "low_vram">("auto");
- // "auto", or the physical index to pin this load to. Only offered on a multi-card CUDA / ROCm host.
- // Persisted, unlike the selects around it: those are reseeded from the loaded build, and the
- // status carries the device a pipeline is on but not which card, so a refresh would reset this
- // one to Auto while the model stayed put and the next Reapply would move it to the default GPU.
- // A stored id is only a hint; the send path below still drops one whose card is no longer there.
+ // "auto", or the physical index to pin this load to; offered only on a multi-card CUDA/ROCm
+ // host. Persisted, unlike the selects around it: status carries the device a pipeline is on
+ // but not which card, so a refresh would reset it to Auto. A stored id is only a hint.
const [selectedGpu, setSelectedGpu] = usePersistedChoice(
"unsloth_video_gpu_choice",
"auto",
@@ -997,7 +977,8 @@ function VideoGenerator({
// Live per-step progress (phase / step / total + ETA) polled during generation.
const [genStep, setGenStep] = useState<VideoGenerateProgress | null>(null);
const genPollTimer = useRef<ReturnType<typeof setInterval> | null>(null);
- // visibilitychange handler active while a generation poll runs: background tabs clamp setInterval, so returning fires one immediate poll.
+ // visibilitychange handler active while a generation poll runs: background tabs clamp
+ // setInterval, so returning fires one immediate poll.
const genVisibilityListener = useRef<(() => void) | null>(null);
const [status, setStatus] = useState<VideoStatus | null>(null);
// Controlled so the body-portaled model selector force-closes when this page is mounted but off-tab.
@@ -1014,16 +995,17 @@ function VideoGenerator({
const [selectedId, setSelectedId] = useState<string | null>(() => galleryCache.selectedId);
const [clearConfirmOpen, setClearConfirmOpen] = useState(false);
const [clearingGallery, setClearingGallery] = useState(false);
- // The `active` gate below only HIDES the confirm, and Radix does not call onOpenChange for
- // a parent-forced close, so on this persistently mounted page the state would outlive the
- // route change and the confirm would be back on return. Reset it during render.
+ // The `active` gate below only HIDES the confirm and Radix does not call onOpenChange for a
+ // parent-forced close, so on this persistently mounted page the state would outlive the
+ // route change. Reset it during render.
if (!active && clearConfirmOpen) setClearConfirmOpen(false);
// Autoplay replays per selected clip (3 total plays, then pause). Reset on every selection change.
const playCountRef = useRef(0);
useEffect(() => {
playCountRef.current = 0;
}, [selectedId]);
- // Pause the preview when this page stops being visible: the keep-alive layout only hides it, and display:none does not pause a media element.
+ // Pause the preview when this page stops being visible: the keep-alive layout only hides it,
+ // and display:none does not pause a media element.
const previewRef = useRef<HTMLVideoElement | null>(null);
// The media element's own handlers fire while the page is hidden, so they read `active` through a ref.
const activeRef = useRef(active);
@@ -1034,29 +1016,29 @@ function VideoGenerator({
const [srcById, setSrcById] = useState<Record<string, string>>(() =>
Object.fromEntries([...galleryCache.srcById].map(([id, e]) => [id, e.url])),
);
- // Guards a "load more" so a fast scroll can't fire several at once.
+ // Guards a "load more" so a fast scroll cannot fire several at once.
const loadingMore = useRef(false);
// False once the page truly unmounts. The page stays mounted across tab switches, so a switch does NOT flip this.
const isMounted = useRef(true);
const pollTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
- // The persistent load toast's id, so each poll updates it in place (chat-style).
+ // The persistent load toast's id, so each poll updates it in place.
const loadToastId = useRef<string | number | null>(null);
// Last load-progress signature shown, so a tick that moved nothing skips the toast.
const lastLoadSig = useRef<string | null>(null);
- // The quant to restore if the current optimistic swap fails.
- // A pick also applies its own step/guidance recipe, so the rollback carries those too: a cancelled distilled
- // pick otherwise leaves its low-step, guidance-0 recipe applied to the model that is still resident.
+ // The quant to restore if the current optimistic swap fails. A pick also applies its own
+ // step/guidance recipe, so the rollback carries that too, or a cancelled distilled pick
+ // leaves its low-step recipe on the model still resident.
const quantRevert = useRef<PickRevert | null>(null);
- // Which quantRevert entry the live staged download belongs to. Staging does not set `busy`, so a second pick can overwrite
- // quantRevert while the first plan is still resolving; without this the dying first job reverts the newer pick's label.
+ // Which quantRevert entry the live staged download belongs to: staging does not set `busy`, so
+ // a second pick can overwrite quantRevert while the first plan resolves.
const stagedQuantRevert = useRef<PickRevert | null>(null);
// Bumped per Hub pick, so a plan that resolves after a newer pick can tell it has been superseded.
const pickSeq = useRef(0);
- // The Reapply target (and its canReapply flag) to restore if the optimistic swap fails: handleLoad overwrites lastLoad at
- // load start, and a load failing AFTER that leaves the previous model resident, so the poll rolls it back.
+ // The Reapply target (and its canReapply flag) to restore if the optimistic swap fails:
+ // handleLoad overwrites lastLoad at load start, and a later failure leaves the old model.
const lastLoadRevert = useRef<{ prev: typeof lastLoad.current; canReapply: boolean } | null>(null);
- // Which pick owns the page: resolving and staging are requests that do not set `busy`, so a pick can land on an awaiting
- // one. Lazy state, not a ref: a ref cannot be written during render.
+ // Which pick owns the page: resolving and staging do not set `busy`, so a pick can land on an
+ // awaiting one. Lazy state, not a ref, since a ref cannot be written during render.
const [pickGuard] = useState(createPickGuard);
const dismissLoadToast = useCallback(() => {
@@ -1064,42 +1046,33 @@ function VideoGenerator({
loadToastId.current = null;
}, []);
- // The load toast is built by handleLoad and the progress poll, both of which are defined above
- // handleCancelLoad (it needs handleUnload, which needs them). Route the action through a ref so
- // the toast keeps a stable onClick instead of dragging the whole load graph into its deps.
+ // The load toast is built by handleLoad and the progress poll, both defined above
+ // handleCancelLoad, so the action goes through a ref to keep a stable onClick.
const cancelLoadRef = useRef<() => void>(() => {});
const cancelLoadFromToast = useCallback(() => cancelLoadRef.current(), []);
- // Bumped by every cancel / eject (see dropResidentState). Requests that were already awaiting a
- // response when the cancel landed compare against it and discard their own result.
+ // Bumped by every cancel / eject (see dropResidentState): requests already awaiting a response
+ // compare against it and discard their own result.
const cancelSeq = useRef(0);
- // Bumped by every load start. The compensating unload below carries no identity, so it must not
- // fire once a newer load owns the page -- it would tear that one down instead.
+ // Bumped by every load start. The compensating unload below carries no identity, so it must
+ // not fire once a newer load owns the page.
const loadSeq = useRef(0);
- // The load currently in flight, if any, as a promise that settles only once handleLoad has run
- // to the end -- including the compensating unload it may issue. A cancel that lands before the
- // backend has registered the load has to wait for all of it: begin_load REFUSES a second load
- // while one is live ("a load is already in progress"), so a model picked in that window would be
- // rejected while the cancelled one kept going, and the compensating unload names no load, so one
- // still in flight would tear down whatever the user picked next. Holding busy shuts both.
+ // The load in flight, as a promise that settles only once handleLoad has run to the end,
+ // compensating unload included. begin_load REFUSES a second load while one is live, so a
+ // model picked in that window would be rejected while the cancelled one kept going.
const pendingStart = useRef<Promise<unknown> | null>(null);
// Set by restoreLoadTracking: handleLoad's compensating unload failed, so the load it was
- // cancelling is STILL running and its toast and poll are back up. handleUnload reads it to
- // report that the eject stopped nothing, rather than claiming success over a live load.
+ // cancelling is STILL running. handleUnload reads it to report that the eject did nothing.
const loadTrackingRestored = useRef(false);
- // Client-side state that only means anything while a model is resident: the
- // in-flight replacement load's tracking, and the Reapply target. Shared with
- // the indicator eject, which frees the runtime without going through the
- // page's own Unload.
+ // Client-side state that only means anything while a model is resident: the replacement
+ // load's tracking and the Reapply target. Shared with the indicator eject.
const dropResidentState = useCallback(() => {
- // Cancel, not release: a resolving pick or a staged download would load
- // back what was just ejected. In here rather than only in handleUnload, so
- // an eject driven from the loaded models card is covered by it too.
+ // Cancel, not release: a resolving pick or a staged download would load back what was just
+ // ejected. Here rather than in handleUnload, so the loaded-models card is covered too.
pickGuard.cancel();
- // Everything already in flight is now stale. Clearing the timer below stops the NEXT poll
- // tick, but not a poll or a start request currently awaiting its response, and those still
- // apply terminal state when they land. The counter is what they compare against.
+ // Everything in flight is now stale. Clearing the timer stops the NEXT poll tick but not a
+ // request awaiting its response; the counter is what those compare against.
cancelSeq.current += 1;
if (pollTimer.current) clearTimeout(pollTimer.current);
pollTimer.current = null;
@@ -1108,10 +1081,8 @@ function VideoGenerator({
// Leaving this set would let Reapply reload the model that was just freed.
lastLoad.current = null;
setCanReapply(false);
- // Stopping the poll above also stops its "the load was cancelled or evicted" branch, which is
- // what hands back a pick that never became resident. Do it here, exactly as that branch would:
- // an unreleased recipe claim leaves hydration parked behind a load that is never coming, and a
- // rollback left behind is one a later pick would inherit in place of its own.
+ // Stopping the poll also stops its "cancelled or evicted" branch, which is what hands back a
+ // pick that never became resident, so do it here exactly as that branch would.
if (quantRevert.current) {
revertPick(quantRevert.current);
quantRevert.current = null;
@@ -1132,7 +1103,7 @@ function VideoGenerator({
);
const selectedSrc = selected ? srcById[selected.id] : undefined;
- // The resolution presets + temporal lattice for the currently loaded family, or the fallbacks before anything is loaded.
+ // The resolution presets + temporal lattice for the loaded family, or the fallbacks before anything is loaded.
const resolutionPresets = useMemo<Array<[number, number]>>(() => {
const presets = status?.defaults?.resolution_presets;
if (presets && presets.length > 0) {
@@ -1169,7 +1140,6 @@ function VideoGenerator({
);
}, [resolutionPresets.length]);
- // ── keyframes ──────────────────────────────────────────────────────────────
const supportsKeyframes = status?.supports_keyframes === true;
// The keyframe the canvas follows: the first when there is one, else the last, matching the backend.
const canvasKeyframe = firstFrame ?? lastFrame;
@@ -1291,8 +1261,8 @@ function VideoGenerator({
setResolutionIntent([params.width, params.height]);
setDurationIntentSeconds(params.durationSeconds);
setNegativePrompt(params.negativePrompt);
- // Same rule restoreSettings follows: a negative prompt that is in effect has to be visible,
- // or the user generates against a setting the collapsed field is hiding.
+ // Same rule restoreSettings follows: a negative prompt in effect has to be visible, or the
+ // user generates against a setting the collapsed field is hiding.
if (params.negativePrompt) setNegativeOpen(true);
setResolutionIdx(resolutionIndex);
setNumFrames(durationFrames);
@@ -1340,8 +1310,8 @@ function VideoGenerator({
revert.commitRecipeClaim = claim.commit;
revert.releaseRecipeClaim = claim.release;
}
- // Baselined per pick, including a pick that inherits an earlier one's rollback: the question
- // is whether the user takes the form after THIS pick, not after the one it replaced.
+ // Baselined per pick, including one that inherits an earlier pick's rollback: the question is
+ // whether the user takes the form after THIS pick.
const claimedAt = videoFormClaimId();
pickRecipeSuperseded.current = () => videoFormClaimId() !== claimedAt;
const recommended = defaultsFor(repoId);
@@ -1354,8 +1324,8 @@ function VideoGenerator({
revert.appliedSteps = recommended.steps;
revert.appliedGuidance = recommended.guidance;
}
- // This explicit pick owns the first status confirmation. On failure, revertPick restores
- // both markers so a previously saved recipe can still outrank a merely discovered resident.
+ // This explicit pick owns the first status confirmation. On failure revertPick restores both
+ // markers, so a saved recipe can still outrank a merely discovered resident.
modelSeeded.current = true;
familySeeded.current = true;
},
@@ -1398,8 +1368,8 @@ function VideoGenerator({
const familyChanged = loadedFamily !== prevFamilyRef.current;
prevFamilyRef.current = loadedFamily;
// A newly loaded family brings its own default clip length; without this the pre-load fallback
- // sticks and every default run is a ~1s clip. Intent moves with it, so the recipe a preset is
- // compared against and the frame count generation sends never disagree.
+ // sticks and every default run is a ~1s clip. Intent moves with it, so the recipe and the
+ // frame count generation sends never disagree.
const applyFamilyDefault = shouldApplyModelDefaults(
familySeeded.current,
videoPresets.storedRecipe,
@@ -1434,9 +1404,9 @@ function VideoGenerator({
videoPresets.storedRecipe,
]);
- // Seed steps/guidance from the loaded model's backend defaults: on mount with a model already loaded only refreshStatus runs, so the
- // controls would stick at the pre-load DEFAULT_GEN and a base checkpoint wanting 40/4 generates a degraded clip. Keyed on the resolved
- // schedule, not the repo alone: a GGUF repo holds several variants, so another client swapping builds changes the defaults in place.
+ // Seed steps/guidance from the loaded model's backend defaults: on mount with a model already
+ // loaded only refreshStatus runs, so the controls would stick at the pre-load DEFAULT_GEN.
+ // Keyed on the resolved schedule, not the repo, since a GGUF repo holds several variants.
const loadedModelKey = status?.loaded
? `${status.repo_id ?? ""}|${defaultSteps ?? ""}|${defaultGuidance ?? ""}|${defaultFlowShift ?? ""}|${defaultAudioFlowShift ?? ""}`
: null;
@@ -1447,15 +1417,14 @@ function VideoGenerator({
if (modelChanged && loadedModelKey && defaultSteps != null && defaultGuidance != null) {
// Status is the authority now, so the recipe a pick claimed has served its purpose.
setPendingModelDefaults(null);
- // A stored recipe is the user's own choice, so it outranks the model's defaults on the first
- // seed. Every later model change still seeds, as picking a model always has.
+ // A stored recipe is the user's own choice, so it outranks the model's defaults on the first seed.
const applyDefaults = shouldApplyModelDefaults(
modelSeeded.current,
videoPresets.storedRecipe,
pickRecipeSuperseded.current?.() ?? false,
);
// This status IS the pending pick's confirmation, so the question is answered for good. Read
- // after the family effect above, which runs first on the same status and asks the same thing.
+ // after the family effect above, which runs first on the same status.
pickRecipeSuperseded.current = null;
modelSeeded.current = true;
if (!applyDefaults) return;
@@ -1475,11 +1444,10 @@ function VideoGenerator({
const canPickAudioFlowShift = status?.defaults?.supports_audio_flow_shift === true;
- // Reseed the Advanced selects from the LOADED build, so they stop being pure local request state.
- // An honored request re-selects itself; a declined one snaps to what actually engaged, so the
- // Precision dropdown can never go on advertising a scheme the loaded DiT is not running. Keyed on
- // the LOAD-TIME half of the record: the backend rewrites the transformer_cache entry at GENERATION
- // time, so serializing the whole record let a step-cache toggle discard a pending Advanced edit.
+ // Reseed the Advanced selects from the LOADED build, so a declined request snaps to what
+ // engaged and Precision never advertises a scheme the DiT is not running. Keyed on the
+ // LOAD-TIME half of the record: the backend rewrites transformer_cache at GENERATION time,
+ // so the whole record let a step-cache toggle discard a pending edit.
const resolvedKey = status?.loaded ? resolvedSeedKey(status.resolved) : null;
useEffect(() => {
const record = status?.loaded ? status.resolved : null;
@@ -1513,10 +1481,11 @@ function VideoGenerator({
const epochAtStart = galleryCache.epoch;
try {
const url = await fetchGalleryVideoSignedUrl(video.id);
- // The record can be deleted (or the gallery cleared) while the link is being minted; caching it then would strand an entry.
+ // The record can be deleted (or the gallery cleared) while the link is being minted; caching
+ // it then would strand an entry.
if (galleryCache.deleted.has(video.id) || galleryCache.epoch !== epochAtStart) return;
galleryCache.srcById.set(video.id, { url, mintedAt: Date.now() });
- // The URL is cached above either way; skip the state update after unmount (matches the other async callbacks in this file).
+ // The URL is cached above either way; skip the state update after unmount.
if (isMounted.current) setSrcById((prev) => ({ ...prev, [video.id]: url }));
} catch {
// Leave it without a src; the card shows a placeholder.
@@ -1525,7 +1494,8 @@ function VideoGenerator({
}
}, []);
- // A media error on a playing clip means its link died early (the server restarted, changing its signing secret). Re-mint once per clip per session.
+ // A media error on a playing clip means its link died early (the server restarted, changing
+ // its signing secret). Re-mint once per clip per session.
const remintSrc = useCallback(
(video: GalleryVideo) => {
if (galleryCache.refreshed.has(video.id)) return;
@@ -1536,8 +1506,9 @@ function VideoGenerator({
[ensureSrc],
);
- // A card's poster frame appears once its src lands and each src costs a request, so minting a full page up front would queue PAGE_SIZE
- // requests ahead of the clip being waited on. Mint as a card nears the viewport, observed from here (the tile is a Tooltip trigger), per page.
+ // A card's poster frame appears once its src lands and each src costs a request, so minting a
+ // full page up front would queue PAGE_SIZE requests ahead of the clip being waited on.
+ // Mint as a card nears the viewport, per page.
const stripRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const root = stripRef.current;
@@ -1552,8 +1523,8 @@ function VideoGenerator({
if (clip) void ensureSrc(clip);
}
},
- // rootMargin is added to the ROOT box only, so the root has to be the strip itself: a card past its right edge is clipped,
- // and a viewport-root margin would never reach it. The strip scrolls horizontally, so the sideways margin is the one that matters.
+ // rootMargin is added to the ROOT box only, so the root has to be the strip itself: a card
+ // past its right edge is clipped. The strip scrolls horizontally, so sideways is what counts.
{ root, rootMargin: "0px 600px" },
);
for (const card of root.querySelectorAll("[data-clip-id]")) io.observe(card);
@@ -1568,24 +1539,23 @@ function VideoGenerator({
})();
}, [selected, ensureSrc]);
- // Bumped by every LOCAL change to the strip: a pin, an archive, a delete, a merged generation.
- // A resync started before one of those holds a snapshot the server listing cannot reconcile with
- // what the user just did, so it drops it rather than applying a window they have moved off.
+ // Bumped by every LOCAL change to the strip. A resync started before one holds a snapshot the
+ // server listing cannot reconcile with what the user just did, so it drops it.
const stripEpoch = useRef(0);
- // Bumped by the window growing from the server instead: a load, an appended page. That is not a
- // conflict, it just means the resync sized itself against a smaller window, so it refetches.
+ // Bumped by the window growing from the server. Not a conflict: the resync merely sized itself
+ // against a smaller window, so it refetches.
const pageEpoch = useRef(0);
- // Only the most recently started resync may apply. Two restores in a row start two of them, and
- // the older snapshot arriving last would drop whatever the newer one had already shown.
+ // Only the most recently started resync may apply: two restores in a row would let the older
+ // snapshot arrive last and drop what the newer one showed.
const resyncSeq = useRef(0);
- // Shelf mutations in flight. The epoch is an EDGE, so a page starting after the bump and landing
- // before the row is dropped sees it hold still. A page is only trusted while this is zero.
+ // Shelf mutations in flight. The epoch is an EDGE, so a page starting after the bump and
+ // landing before the row is dropped sees it hold still.
const pendingShelfMutations = useRef(0);
const loadGallery = useCallback(async () => {
try {
// Fenced: this page renders from the module cache while the load runs, so its tiles are
- // actionable, and a pre-pin snapshot would undo the action with nothing to correct it.
+ // actionable and a pre-pin snapshot would undo the action.
const page = await fetchWhileStable(
() => stripEpoch.current,
() => getVideoGallery(0, PAGE_SIZE),
@@ -1601,7 +1571,7 @@ function VideoGenerator({
page.videos.forEach((video) => void ensureSrc(video));
}
} catch {
- // Best-effort: a failed gallery load shouldn't block the page.
+ // Best-effort: a failed gallery load should not block the page.
}
}, [ensureSrc]);
@@ -1609,8 +1579,8 @@ function VideoGenerator({
if (loadingMore.current || !galleryCache.hasMore) return;
loadingMore.current = true;
try {
- // Guarded on all three counters: an archive landing anywhere across this GET shortens the
- // shelf, and the clip that shifts over the page boundary is returned by no page at all.
+ // Guarded on all three counters: an archive landing across this GET shortens the shelf, and
+ // the clip that shifts over the page boundary is returned by no page at all.
const result = await fetchNextPage(
() => galleryCache.videos.length,
() => stripEpoch.current,
@@ -1638,7 +1608,8 @@ function VideoGenerator({
}
}, [ensureSrc]);
- // WebM/GIF go through a server-side transcode that can take seconds (and 501s when the codec is missing), so wrap the helper with toasts.
+ // WebM/GIF go through a server-side transcode that can take seconds (and 501s when the codec
+ // is missing), so wrap the helper with toasts.
const handleDownload = useCallback(
async (src: string, video: GalleryVideo, format: "mp4" | "webm" | "gif") => {
const toastId =
@@ -1661,8 +1632,7 @@ function VideoGenerator({
);
// Drop a clip from the strip. `discardLink` is for a real delete: the bytes are gone, so the
- // cached link must go and any mint in flight must throw its result away. An archived clip keeps
- // both, since the archived view plays the same file.
+ // cached link must go and any mint in flight must discard. An archived clip keeps both.
const dropFromStrip = useCallback((id: string, discardLink: boolean) => {
if (discardLink) {
galleryCache.srcById.delete(id);
@@ -1675,8 +1645,8 @@ function VideoGenerator({
});
}
stripEpoch.current += 1;
- // Read the list from the cache (kept in sync with state every render) rather than nesting a
- // setSelectedId inside a setVideos updater, which would run a side effect during dispatch.
+ // Read the list from the cache rather than nesting a setSelectedId inside a setVideos
+ // updater, which would run a side effect during dispatch.
const at = galleryCache.videos.findIndex((v) => v.id === id);
const next = removeGalleryItem(galleryCache.videos, id);
galleryCache.videos = next;
@@ -1686,8 +1656,8 @@ function VideoGenerator({
const handleDelete = useCallback(
async (id: string) => {
- // Held for the whole round trip: the server shortens the shelf when it processes this, and a
- // page read inside that window sees the shortened list at an offset nothing contradicts.
+ // Held for the whole round trip: the server shortens the shelf when it processes this, so a
+ // page read inside that window still sees a consistent offset.
stripEpoch.current += 1;
pendingShelfMutations.current += 1;
try {
@@ -1703,27 +1673,21 @@ function VideoGenerator({
[dropFromStrip],
);
- /**
- * Refetch the loaded window from offset 0.
- *
- * Unpinning can drop a clip past the end of the loaded window and promote a previously unloaded
- * one into it. The local reorder cannot know about the promoted clip, and the next loadMore
- * still pages from the unchanged length, so that clip would be skipped until a reload.
- */
+ /** Refetch the loaded window from offset 0. Unpinning can drop a clip past the end of the
+ * window and promote an unloaded one into it, which the local reorder cannot know about. */
const resyncWindow = useCallback(
async (count: number, stillFresh?: () => boolean) => {
const ticket = (resyncSeq.current += 1);
for (let attempt = 0; attempt < RESYNC_MAX_ATTEMPTS; attempt += 1) {
const paged = pageEpoch.current;
- // Sized against the live window, so a page appended while this ran is covered rather than
- // cut back off the bottom of the strip when the snapshot lands.
+ // Sized against the live window, so a page appended while this ran is covered rather than cut
+ // off the bottom of the strip.
const wanted = Math.max(count, galleryCache.videos.length, PAGE_SIZE);
const collected: GalleryVideo[] = [];
let more = false;
while (collected.length < wanted) {
- // The REMAINDER, not a whole page: a window of 51 (a page plus one new generation) would
- // otherwise ask for 100 and grow the strip to match, reading 49 recipes off disk and
- // rendering their tiles for a one-row shortfall.
+ // The REMAINDER, not a whole page: a window of 51 would otherwise ask for 100 and read 49
+ // recipes off disk for a one-row shortfall.
const page = await getVideoGallery(
collected.length,
Math.min(PAGE_SIZE, wanted - collected.length),
@@ -1732,12 +1696,12 @@ function VideoGenerator({
more = page.has_more;
if (!page.has_more || page.videos.length === 0) break;
}
- // Checked here, not by the caller: by the time this returns the window is already applied,
- // so a stale snapshot has to be dropped before it overwrites a newer local change.
+ // Checked here, not by the caller: by the time this returns the window is already applied, so
+ // a stale snapshot has to be dropped first.
if (stillFresh && !stillFresh()) return;
if (resyncSeq.current !== ticket) return;
- // Pagination moved under this pass. That is only server data, so cover it with another
- // pass instead of giving up: giving up is what left an unpin's promoted clip missing.
+ // Pagination moved under this pass. That is only server data, so cover it with another pass
+ // instead of giving up, which is what left an unpin's promoted clip missing.
if (pageEpoch.current !== paged) continue;
galleryCache.videos = collected;
galleryCache.hasMore = more;
@@ -1752,17 +1716,16 @@ function VideoGenerator({
[ensureSrc],
);
- // This page stays mounted across route changes, so a restore from the Settings archive would
- // otherwise not reach the strip until a full reload. Resync the window that is actually loaded:
- // loadGallery would cut it back to the first page and throw away everything scrolled to.
+ // This page stays mounted across route changes, so an archive restore would not reach the
+ // strip until a reload. Resync the loaded window: loadGallery would cut it to page one.
useEffect(
() =>
subscribeGalleryChanged("videos", () => {
- // Bumped FIRST: a restore changes the shelf, so reads already in flight must be discarded.
- // Capturing without advancing let them pass their own checks and land on the new window.
+ // Bumped FIRST: a restore changes the shelf, so reads already in flight must be discarded, or
+ // they pass their own checks and land on the new window.
stripEpoch.current += 1;
- // Fenced like the unpin resync: a generation or a new page landing while this GET runs
- // would otherwise be overwritten by a snapshot taken before it.
+ // Fenced like the unpin resync: a generation or a new page landing while this GET runs would
+ // be overwritten by a snapshot taken before it.
const epoch = stripEpoch.current;
void resyncWindow(
galleryCache.videos.length,
@@ -1772,20 +1735,19 @@ function VideoGenerator({
[loadGallery, resyncWindow],
);
- // The pin state each id was last CLICKED into, so a failing request can tell whether it is still
- // the current intent. Without it, a slow first click failing after a later click succeeded would
- // roll the strip back onto the state the user has since moved off.
+ // The pin state each id was last CLICKED into, so a failing request can tell whether it is
+ // still the current intent; without it a slow failure rolls back a later success.
const pinAttempt = useRef(new Map<string, number>());
const pinSeq = useRef(0);
const handleTogglePin = useCallback(
async (id: string, pinned: boolean) => {
const loadedCount = galleryCache.videos.length;
- // The pinned order as it stands BEFORE the click, so a failed unpin can put the clip back
- // where it was instead of at the front of the pins.
+ // The pinned order BEFORE the click, so a failed unpin can put the clip back where it was
+ // instead of at the front.
const orderBefore = pinnedOrder(galleryCache.videos);
- // A per-attempt token, not the target boolean: pin, unpin, pin stores true twice, so the FIRST
- // attempt's failure would roll back the THIRD attempt's pin and leave the two disagreeing.
+ // A per-attempt token, not the target boolean: pin, unpin, pin stores true twice, so the first
+ // attempt's failure would roll back the third attempt's pin.
const attempt = (pinSeq.current += 1);
pinAttempt.current.set(id, attempt);
stripEpoch.current += 1;
@@ -1797,21 +1759,19 @@ function VideoGenerator({
return next;
});
try {
- // One queue for the whole gallery, not one per clip. The server stamps `pinned_at` when
- // it runs the PATCH and orders pins by that stamp, so two requests in flight together can
- // be stamped in either order and the strip disagrees with the next load. Issuing them one
- // at a time makes the stamps follow the clicks.
+ // One queue for the whole gallery: the server stamps `pinned_at` when it runs the PATCH, so
+ // two requests in flight can be stamped in either order. One at a time follows the clicks.
await serializeById("video-pin", () => setGalleryVideoFlags(id, { pinned }));
} catch (err) {
toast.error(err instanceof Error ? err.message : "Failed to pin video");
- // Put the old order back rather than leave the strip lying about server state, but only
- // while this is still what the user last asked for.
+ // Put the old order back rather than leave the strip lying about server state, but only while
+ // this is still what the user last asked for.
if (pinAttempt.current.get(id) === attempt) {
pinAttempt.current.delete(id);
stripEpoch.current += 1;
setVideos((prev) => {
- // A failed pin simply goes back to unpinned; a failed unpin has to be restored to its
- // old position among the pins, which applyPin cannot do (it means "freshly pinned").
+ // A failed pin goes back to unpinned; a failed unpin has to be restored to its old position
+ // among the pins, which applyPin cannot do.
const next = pinned
? applyPin(prev, id, false)
: restorePinOrder(prev, id, orderBefore);
@@ -1823,12 +1783,10 @@ function VideoGenerator({
}
if (pinAttempt.current.get(id) !== attempt) return; // superseded by a later click
pinAttempt.current.delete(id);
- // Pinning keeps the same set in the window (it only moves an already-loaded clip to the
- // front), so only unpinning can open a gap.
+ // Pinning keeps the same set in the window, so only unpinning can open a gap.
if (!pinned && loadedCount > 0) {
try {
- // Fenced: a pin clicked while this GET is in flight would otherwise be overwritten by a
- // snapshot taken before it, leaving the strip unpinned while the server is pinned.
+ // Fenced: a pin clicked while this GET is in flight would be overwritten by a snapshot taken before it.
await resyncWindow(loadedCount, () => stripEpoch.current === epoch);
} catch {
// Best-effort: the strip is still usable, just possibly short one clip until a reload.
@@ -1840,8 +1798,8 @@ function VideoGenerator({
const handleArchive = useCallback(
async (id: string) => {
- // Held for the whole round trip: the server shortens the shelf when it processes this, and a
- // page read inside that window sees the shortened list at an offset nothing contradicts.
+ // Held for the whole round trip: the server shortens the shelf when it processes this, so a
+ // page read inside that window still sees a consistent offset.
stripEpoch.current += 1;
pendingShelfMutations.current += 1;
try {
@@ -1877,7 +1835,8 @@ function VideoGenerator({
await clearVideoGallery();
galleryCache.srcById.clear();
galleryCache.refreshed.clear();
- // Every mint in flight now belongs to a cleared gallery, so their links are discarded on arrival. The epoch covers unlisted ids too.
+ // Every mint in flight now belongs to a cleared gallery, so their links are discarded on
+ // arrival. The epoch covers unlisted ids too.
galleryCache.epoch += 1;
stripEpoch.current += 1;
galleryCache.videos = [];
@@ -1925,10 +1884,8 @@ function VideoGenerator({
[resolutionPresets, durationOptions, fps],
);
- // A status read started before an eject can answer after the one that
- // followed it, and this page has no periodic poll to correct it: the controls
- // would go on offering to generate against a runtime that is already free.
- // So every read takes a ticket and only the newest may write.
+ // A status read started before an eject can answer after the one that followed it, and this
+ // page has no periodic poll to correct it, so only the newest ticket may write.
const statusTicket = useRef(0);
const setStatusIfNewest = useCallback(
(ticket: number, next: VideoStatus) => {
@@ -1937,8 +1894,8 @@ function VideoGenerator({
[],
);
- // Answers with what it wrote, or null when the read failed or a newer one superseded it,
- // so a caller can act on what the server now says.
+ // Answers with what it wrote, or null when the read failed or a newer one superseded it, so a
+ // caller can act on what the server now says.
const refreshStatus = useCallback(async (): Promise<VideoStatus | null> => {
const ticket = ++statusTicket.current;
try {
@@ -1946,23 +1903,18 @@ function VideoGenerator({
setStatusIfNewest(ticket, next);
return ticket === statusTicket.current ? next : null;
} catch {
- // Status is best-effort; a failed poll shouldn't surface an error toast.
+ // Status is best-effort; a failed poll should not surface an error toast.
return null;
}
}, [setStatusIfNewest]);
- // A generation can be refused because the runtime went away under the page: an idle
- // auto-unload frees it server-side and the browser hears nothing, since the eject event
- // is raised by whoever clicked eject. Without a re-read here Generate stays enabled off
- // the stale flag and every retry 409s again, so the refusal is the news that the model
- // is gone. Also clears the state that only means anything while one is resident (the
- // Reapply target, a replacement load's tracking), as the indicator eject does.
+ // A generation can be refused because the runtime went away under the page: an idle auto-unload
+ // frees it server-side and the browser hears nothing, so the refusal is the news. Also
+ // clears the state that only means anything while one is resident.
const resyncAfterGenerateRefusal = useCallback(async () => {
// A model picked while this read is in flight makes the answer stale rather than wrong:
- // /video/status reports committed state, so it says loaded: false for the load that has
- // just started. Acting on it would dismiss that load's toast and poll, and -- while its
- // start request is still out -- the cancel it counts as sends the compensating unload
- // that tears it down. The load counter is the fence handleLoad already bumps.
+ // /video/status reports committed state, so it says loaded: false for the load that has just
+ // started, and acting on it would tear that load down. handleLoad's counter is the fence.
const startLoad = loadSeq.current;
const next = await refreshStatus();
if (!isMounted.current || next === null || next.loaded) return;
@@ -1979,7 +1931,7 @@ function VideoGenerator({
};
}, []);
- // Re-sync model status when the tab becomes active again: while off-tab the video model may have been evicted.
+ // Re-sync model status when the tab becomes active again: the video model may have been evicted while off-tab.
useEffect(() => {
if (!active) return;
if (initialReadySent.current) {
@@ -2008,25 +1960,15 @@ function VideoGenerator({
};
}, [active, ensureSrc, loadGallery, onInitialReady, refreshStatus]);
- // Ejected from the loaded models indicator, which does not run handleUnload:
- // without this the controls keep offering to generate on a freed runtime, and
- // Reapply still points at the model that was just ejected. The runtime is
- // already free, so this is handleUnload without the unload call.
+ // Ejected from the loaded models indicator, which does not run handleUnload: without this the
+ // controls keep offering to generate on a freed runtime. So: handleUnload minus the unload.
useEffect(
() =>
subscribeModelEjected("video", () => {
dropResidentState();
- // That eject cancelled the replacement load, and its progress poll is
- // the only thing that clears `busy` -- which dropResidentState has just
- // stopped. Leaving it set locks the page: the picker ignores every
- // choice while busy, and Unload is not offered once the status read
- // comes back empty, so only an app reload recovered. Narrowed to
- // "loading" so a generation in flight is left alone, as handleUnload's
- // own finally does.
- // ...but not while a load start is still in flight. begin_load refuses a second load
- // while one is registered, so a model picked in that window is rejected while the load
- // this eject was meant to cancel carries on, and handleLoad's compensating unload names
- // no load. Hold busy until the whole path settles, exactly as handleCancelLoad does.
+ // That eject cancelled the replacement load, and its progress poll is the only thing that
+ // clears `busy`, which dropResidentState just stopped; leaving it set locks the page.
+ // Narrowed to "loading" so a generation is left alone, and held until the start settles.
const pending = pendingStart.current;
if (pending) {
setBusy((prev) => (prev === "loading" ? "unloading" : prev));
@@ -2042,13 +1984,13 @@ function VideoGenerator({
[refreshStatus, dropResidentState],
);
- // Collapse the body-ported model selector when leaving the tab so returning to /video does not pop it back open unprompted.
+ // Collapse the body-ported model selector when leaving the tab so returning to /video does not pop it back open.
useEffect(() => {
if (active) return;
setSelectorOpen(false);
}, [active]);
- // Poll load-progress until the background load reaches "ready" or "error", updating the persistent toast in place each tick.
+ // Poll load-progress until the background load reaches "ready" or "error", updating the persistent toast in place.
const pollLoadProgress = useCallback(async () => {
// This tick's cancellation fence: clearing pollTimer stops the next tick, not the awaits below.
const seq = cancelSeq.current;
@@ -2060,10 +2002,8 @@ function VideoGenerator({
const ticket = ++statusTicket.current;
const loaded = await getVideoStatus();
if (seq !== cancelSeq.current) {
- // Cancelled while this read was in flight, so it describes a pipeline being torn down.
- // Drop it and refresh NOTHING: the unload's own response is authoritative and already
- // holds the newest ticket, and a status read issued from here would take a newer one
- // still and could re-report the model mid-teardown.
+ // Cancelled while this read was in flight, so it describes a pipeline being torn down. Drop
+ // it and refresh NOTHING: the unload's own response is authoritative.
return;
}
setStatusIfNewest(ticket, loaded);
@@ -2083,7 +2023,7 @@ function VideoGenerator({
revertPick(quantRevert.current);
quantRevert.current = null;
}
- // Same rollback for the Reapply target: the previous model is still resident, so point Reapply back at it.
+ // Same rollback for the Reapply target: the previous model is still resident.
if (lastLoadRevert.current) {
lastLoad.current = lastLoadRevert.current.prev;
setCanReapply(lastLoadRevert.current.canReapply);
@@ -2093,14 +2033,15 @@ function VideoGenerator({
return;
}
if (p.phase === null) {
- // No load in flight and nothing loaded: the load was cancelled or evicted. Terminal, else this loop spins forever.
+ // No load in flight and nothing loaded: the load was cancelled or evicted. Terminal, else this
+ // loop spins forever.
dismissLoadToast();
setBusy(null);
if (quantRevert.current) {
revertPick(quantRevert.current);
quantRevert.current = null;
}
- // Restore the Reapply target too, so it never lingers on the failed pick after a cancel or eviction.
+ // Restore the Reapply target too, so it never lingers on the failed pick.
if (lastLoadRevert.current) {
lastLoad.current = lastLoadRevert.current.prev;
setCanReapply(lastLoadRevert.current.canReapply);
@@ -2122,9 +2063,8 @@ function VideoGenerator({
}, [dismissLoadToast, refreshStatus, cancelLoadFromToast]);
// Put back what a teardown removed when the load it was tearing down is still running: the
- // unload failed, so the poll and the toast were stopped for nothing. refreshStatus cannot do
- // this -- a first load is not resident yet, so status has nothing to report -- and without it a
- // multi-gigabyte load continues with no progress and no way to cancel it a second time.
+ // unload failed, so the poll and toast were stopped for nothing. refreshStatus cannot do
+ // this, since a first load is not resident yet.
const restoreLoadTracking = useCallback(() => {
loadTrackingRestored.current = true;
setBusy("loading");
@@ -2143,8 +2083,8 @@ function VideoGenerator({
}
}, []);
- // Poll the backend's per-step progress so the bar tracks denoising and the encode phase, driving completion off the terminal
- // phase. A named poll body also serves the visibilitychange listener. Shared by handleGenerate and the mount-time resume.
+ // Poll the backend's per-step progress so the bar tracks denoising and the encode phase,
+ // driving completion off the terminal phase. Shared with the mount-time resume.
const startGenPoll = useCallback(() => {
stopGenPoll();
let pollInFlight = false;
@@ -2159,11 +2099,11 @@ function VideoGenerator({
setBusy(null);
setGenStep(null);
if (p.phase === "completed" && p.video) {
- // Merge the new clip and mint its link. Sorted, not prepended: a new clip is unpinned,
- // so the server puts it after the pinned group.
+ // Merge the new clip and mint its link. Sorted, not prepended: a new clip is unpinned, so the
+ // server puts it after the pinned group.
const clip = p.video;
- // Refused if archived while this poll was in flight: forgetting the backend record
- // cannot revoke a response already on the wire, and it still says archived: false.
+ // Refused if archived while this poll was in flight: forgetting the backend record cannot
+ // revoke a response already on the wire, and it still says archived: false.
if (!galleryCache.archived.has(clip.id) && !galleryCache.deleted.has(clip.id)) {
stripEpoch.current += 1;
setVideos((prev) =>
@@ -2219,7 +2159,8 @@ function VideoGenerator({
} catch {
// Resume is best-effort; a failed probe just leaves the idle view.
}
- // A generation also runs on a daemon thread, so a reload mid-denoise must re-enter the same poll loop rather than show an idle page.
+ // A generation also runs on a daemon thread, so a reload mid-denoise must re-enter the same
+ // poll loop rather than show an idle page.
try {
const g = await getVideoGenerateProgress();
if (g.active) {
@@ -2227,9 +2168,11 @@ function VideoGenerator({
setGenStep(g.phase === "queued" ? null : g);
startGenPoll();
} else if (g.phase === "completed" && g.video) {
- // The job finished while no page was mounted. The terminal record persists until the next job; merging here covers the race where it completed after the mount fetch.
+ // The job finished while no page was mounted: the terminal record persists until the next job,
+ // so merging here covers the race where it completed after the mount fetch.
const clip = g.video;
- // Deleted this session: the backend clears its terminal record on delete, but a client racing that must not merge a record whose file is gone.
+ // Deleted this session: the backend clears its terminal record on delete, but a client racing
+ // that must not merge a record whose file is gone.
if (!galleryCache.deleted.has(clip.id) && !galleryCache.archived.has(clip.id)) {
stripEpoch.current += 1;
setVideos((prev) =>
@@ -2238,7 +2181,8 @@ function VideoGenerator({
void ensureSrc(clip);
}
} else if (g.phase === "failed") {
- // The other terminal phase, kept only until the next job: without this a reload after a failed generation shows an idle page and loses the error.
+ // The other terminal phase, kept only until the next job: without this a reload after a failed
+ // generation shows an idle page and loses the error.
const msg = g.error || "Video generation failed";
if (!msg.toLowerCase().includes("cancelled")) toast.error(msg);
}
@@ -2286,7 +2230,7 @@ function VideoGenerator({
kind === "pipeline" && controls.transformerQuant !== "auto"
? controls.transformerQuant
: undefined,
- // Dropped when the chosen card is gone (a driver reset, an eGPU unplugged), so a stale pick loads automatically instead of 400ing.
+ // Dropped when the chosen card is gone, so a stale pick loads automatically instead of 400ing.
gpu_ids:
controls.selectedGpu !== "auto" &&
controls.gpuChoices.some((d) => String(d.index) === controls.selectedGpu)
@@ -2330,8 +2274,7 @@ function VideoGenerator({
): Promise<boolean> => {
if (pollTimer.current) clearTimeout(pollTimer.current);
// Read BEFORE the start request goes out: a Cancel pressed while it is in flight sends an
- // unload that can reach the backend first, find no load registered, and succeed without
- // stopping anything.
+ // unload that can reach the backend first, find no load registered, and stop nothing.
const startSeq = cancelSeq.current;
const startLoad = ++loadSeq.current;
// Published now and settled in the finally below, so a cancel waits for the WHOLE path.
@@ -2340,8 +2283,7 @@ function VideoGenerator({
settleLoad = resolve;
});
pendingStart.current = inFlight;
- // Every exit below goes through this: it settles the promise a cancel is waiting on and
- // releases the ref, so the page cannot stay busy on a load that has already finished.
+ // Every exit below goes through this: it settles the promise a cancel is waiting on and releases the ref.
const settle = (started: boolean): boolean => {
settleLoad();
if (pendingStart.current === inFlight) pendingStart.current = null;
@@ -2351,7 +2293,7 @@ function VideoGenerator({
dismissLoadToast();
lastLoadSig.current = null;
loadToastId.current = toast(null, loadToastArgs(IDLE_PROGRESS, undefined, cancelLoadFromToast));
- // Snapshot the prior Reapply target first: a load that fails to START leaves the previous model resident, so Reapply must keep pointing at it.
+ // Snapshot the prior Reapply target first: a load that fails to START leaves the previous model resident.
const prevLastLoad = lastLoad.current;
const prevCanReapply = canReapply;
const advanced = pinned ?? currentLoadAdvanced(opts.kind);
@@ -2372,8 +2314,8 @@ function VideoGenerator({
attention_backend: advanced.attention_backend,
transformer_cache: advanced.transformer_cache,
transformer_quant: advanced.transformer_quant,
- // Not an Advanced control: the partition is chosen per pick, so it stays on opts rather
- // than joining the pinned set.
+ // Not an Advanced control: the partition is chosen per pick, so it stays on opts rather than
+ // joining the pinned set.
h3_task: opts.h3Task,
gpu_ids: advanced.gpu_ids,
});
@@ -2389,19 +2331,15 @@ function VideoGenerator({
return settle(false);
}
if (startSeq !== cancelSeq.current) {
- // Cancelled during the start request. The unload it sent may have landed before this load
- // registered, in which case it stopped nothing and the model is loading right now with no
- // toast and no Cancel button. The load exists on the backend as of this line, so unload
- // once more -- that one cannot miss it. Unless a NEWER load has since taken the page:
- // this unload names nothing, so firing it then would cancel that load instead of this
- // one, and the newer start has already superseded this load's token on the backend.
+ // Cancelled during the start request: the unload it sent may have landed before this load
+ // registered, leaving it running with no toast and no Cancel. The load exists as of this
+ // line, so unload once more, unless a NEWER load has taken the page.
if (startLoad === loadSeq.current) {
try {
await unloadVideoModel();
} catch {
- // This request is the ONLY one that can still stop the load the first unload missed,
- // so a failure here is not best-effort: the load is running, untracked. Put the
- // tracking back exactly as a failed cancel does, so it stays visible and cancellable.
+ // This request is the ONLY one that can still stop the load the first unload missed, so a
+ // failure here is not best-effort: put the tracking back exactly as a failed cancel does.
restoreLoadTracking();
return settle(false);
}
@@ -2422,7 +2360,8 @@ function VideoGenerator({
],
);
- // Downloads go through the Hub download manager like every other model, sharing its panel, progress, cancel and preflight. Mirrors Images.
+ // Downloads go through the Hub download manager like every other model, sharing its panel,
+ // progress, cancel and preflight. Mirrors Images.
const pendingStagedLoad = useRef<{
repoId: string;
opts: VideoLoadOptions;
@@ -2432,14 +2371,12 @@ function VideoGenerator({
} | null>(null);
const handleLoadRef = useRef(handleLoad);
handleLoadRef.current = handleLoad;
- // A download finishing while this page is hidden must not evict the model the visible page loaded. The pick is held, not dropped.
+ // A download finishing while this page is hidden must not evict the model the visible page
+ // loaded. The pick is held, not dropped.
const stagedLoadDeferred = useRef(false);
// Both deferred paths run the load minutes after the pick was reported started, so both need
- // the same rollback: onReady when the page is active, and the effect below when the download
- // finished off-tab. The deferred load can still be REFUSED, by a training run or another load
- // claiming the slot while the download ran. Staging started no load, so nothing polls and the
- // poll's own rollback never runs; without this the selector keeps advertising a quant that was
- // never loaded. `owned` is read BEFORE the call, so a newer pick's label is left alone.
+ // the same rollback: a deferred load can still be REFUSED, and staging polls nothing.
+ // `owned` is read BEFORE the call, so a newer pick's label is left alone.
const runStagedLoad = useCallback(
(pending: NonNullable<typeof pendingStagedLoad.current>) => {
if (pendingStagedLoad.current === pending) pendingStagedLoad.current = null;
@@ -2468,13 +2405,12 @@ function VideoGenerator({
},
onCancelled: () => {
// Same rule as the images page: a plan that ends without every dependency on disk must not
- // leave an intent for a late completion or a deferred activation to act on.
+ // leave an intent for a late completion to act on.
pendingStagedLoad.current = null;
stagedLoadDeferred.current = false;
// No load started, so the poll that owns the after-start rollback never runs: put the
- // optimistic quant label back, or the selector describes the resident model with a
- // quant nothing ever loaded. Only for the pick that staged THIS job: a newer pick owns
- // the label from the moment it is made.
+ // optimistic quant label back, or the selector describes the resident model with a quant
+ // nothing loaded. Only for the pick that staged THIS job.
if (quantRevert.current && quantRevert.current === stagedQuantRevert.current) {
revertPick(quantRevert.current);
quantRevert.current = null;
@@ -2490,8 +2426,8 @@ function VideoGenerator({
if (pending) runStagedLoad(pending);
}, [active, runStagedLoad]);
- // Stage a not-yet-downloaded hub pick, else load it directly.
- // `token` lets an awaiting caller drop out: the plan below is a second window for a newer pick to take the page.
+ // Stage a not-yet-downloaded hub pick, else load it directly. `token` lets an awaiting caller
+ // drop out: the plan below is a second window for a newer pick to take the page.
const loadOrStage = useCallback(
async (
repoId: string,
@@ -2499,17 +2435,13 @@ function VideoGenerator({
source: ModelSelectorChangeMeta["source"] = "hub",
token?: number,
): Promise<boolean> => {
- // Every Hub pick needs the plan, not just an undownloaded one: a cached checkpoint can
- // still be missing its base repo's text encoder or VAE, and only the plan can see that.
- // The plan is cache-aware, so a fully cached pick comes back with no entries.
- // Staging never sets `busy`, so a second pick passes handleModelSelect's guard while this
- // plan is still in flight. Plans then resolve in response order, not pick order: without
- // this the older one restages over the newer queue, or loads the model the user left.
- // Bumped before the non-hub return too: a local pick must invalidate an in-flight hub plan.
+ // Every Hub pick needs the plan, not just an undownloaded one: a cached checkpoint can still
+ // be missing its base repo's text encoder or VAE. Staging never sets `busy`, so plans
+ // resolve in response order; bumped before the non-hub return too, so a local pick
+ // invalidates an in-flight hub plan.
const pick = ++pickSeq.current;
- // The previous pick's staged intent dies with it. A pick that stages nothing (fully cached,
- // local, no plan) never calls stage(), so the hook's queue keeps running the older job and
- // its onReady would load the model the user moved away from, evicting this one.
+ // The previous pick's staged intent dies with it: a pick that stages nothing never calls
+ // stage(), so the queue keeps the older job and its onReady loads the abandoned model.
pendingStagedLoad.current = null;
stagedLoadDeferred.current = false;
stagedQuantRevert.current = null;
@@ -2518,7 +2450,8 @@ function VideoGenerator({
if (source !== "hub") return handleLoadRef.current(repoId, opts);
const advanced = currentLoadAdvanced(opts.kind);
- // Read before the await: a pick made while the plan resolves replaces quantRevert, and this job must not revert it.
+ // Read before the await: a pick made while the plan resolves replaces quantRevert, and this
+ // job must not revert it.
const ownRevert = quantRevert.current;
// Read inside the try, acted on outside it, as on the images page.
let incompatible: string | null = null;
@@ -2527,24 +2460,25 @@ function VideoGenerator({
model_path: repoId,
gguf_filename: opts.filename,
model_kind: opts.kind,
- // Same token handleLoad sends: without it the metadata lookup fails on a gated base and the plan drops the companion entry, so the load pulls those files inline.
+ // Same token handleLoad sends: without it the metadata lookup fails on a gated base and the
+ // plan drops the companion entry.
hf_token: hfApiToken(getHfToken()),
// The route preflights the same values used by the eventual load.
transformer_quant: advanced.transformer_quant,
memory_mode: advanced.memory_mode,
- // And the partition, for the same reason: the two H3 denoisers are separate downloads,
- // so a plan asked without it stages the default fl2va weights for a References pick.
+ // And the partition, for the same reason: the two H3 denoisers are separate downloads, so a
+ // plan asked without it stages the default fl2va weights.
h3_task: opts.h3Task,
// The plan sizes its file set against the card the load will use, so it needs the pick.
gpu_ids: advanced.gpu_ids,
});
// Superseded. Report started so this pick's `.then` leaves the newer label alone.
if (pick !== pickSeq.current || !owns()) return true;
- // Same selection-time refusal the images page makes: the plan is the last point at which
- // an incompatible pairing can be caught before the download it would waste. No video
- // family declares one today (the check is the FLUX.2 GGUF/base size pairing, and the video
- // planner has no diffusers base to pair against), so this is the shared envelope's half of
- // the contract rather than a live path -- keep it, or a future one lands unguarded.
+ // Same selection-time refusal the images page makes: the plan is the last point at which an
+ // incompatible pairing can be caught before the download it would waste. No video family
+ // declares one today, so this is the shared envelope's half rather than a live path.
+ // The check is the FLUX.2 GGUF/base size pairing, and the video planner has no diffusers base to
+ // pair against, so this is the shared envelope's half of the contract rather than a live path.
incompatible = plan.incompatible_reason ?? null;
if (!incompatible && plan.entries.length > 0) {
pendingStagedLoad.current = {
@@ -2560,15 +2494,10 @@ function VideoGenerator({
files: e.files,
bytes: e.bytes,
ggufFilename: e.gguf_filename,
- // The entry carrying the picked checkpoint file, so the panel can label it without
- // guessing: filenames cannot tell the two apart once a checkpoint ships as
- // .safetensors like its companions do. Repo identity alone is not enough, because a
- // checkpoint that shares its repo with the companions and is already cached leaves an
- // entry of companion files only. A pipeline pick has no one file: the repo IS it.
- // The backend's own answer wins: a gated pipeline is staged from an ungated MIRROR,
- // so its entry no longer carries the id we picked and the id test below reads the
- // whole selected model as companion assets. `??`, not `||`: a planner that says false
- // is answering, and the fallback exists only for a backend too old to send the key.
+ // The entry carrying the picked checkpoint file, so the panel can label it without guessing:
+ // filenames cannot tell the two apart, and repo identity is not enough when a checkpoint
+ // shares its repo with cached companions. The backend's answer wins, since a gated pipeline
+ // is staged from an ungated MIRROR; nullish coalescing, since false is still an answer.
checkpoint:
e.checkpoint ??
(opts.filename
@@ -2592,8 +2521,8 @@ function VideoGenerator({
[stage, pickGuard, currentLoadAdvanced],
);
- // A GGUF pick can arrive with only a repo id (a pinned row, a curated artifact, a local GGUF directory). The backend
- // rejects a gguf load with no filename and a pipeline load of a GGUF repo, so name the file from the listing first.
+ // A GGUF pick can arrive with only a repo id. The backend rejects a gguf load with no filename
+ // and a pipeline load of a GGUF repo, so name the file from the listing first.
const loadGgufRepoPick = useCallback(
async (
repoId: string,
@@ -2636,9 +2565,8 @@ function VideoGenerator({
[applyVideoModelDefaults, loadOrStage, pickGuard, quant, revertPick],
);
- // A pick that is rejected after beginPick() has already retired the staged pick it replaced, so
- // nothing will load and nothing else will restore the label. Hand the resident state back here or
- // the selector keeps showing the abandoned pick's quant and recipe for good.
+ // A pick rejected after beginPick() has already retired the staged pick it replaced, so
+ // nothing will load and nothing else restores the label. Hand the resident state back here.
const abandonPick = useCallback(() => {
if (quantRevert.current) {
revertPick(quantRevert.current);
@@ -2650,11 +2578,8 @@ function VideoGenerator({
useEffect(() => {
if (!active) {
pickGuard.release();
- // Through the SAME ending as pressing Cancel. The pick that opened this dialog already
- // replaced the quant label, steps and guidance and parked their rollback in quantRevert,
- // and the load it was deferring never ran. Clearing the dialog alone leaves the controls
- // describing H3 over whatever model is still resident, and leaves a stale rollback for the
- // next pick to trip over.
+ // Through the SAME ending as pressing Cancel: the pick that opened this dialog already replaced
+ // the quant label, steps and guidance and parked their rollback, and its load never ran.
setPendingH3Load((pending) => {
if (pending) abandonPick();
return null;
@@ -2662,9 +2587,9 @@ function VideoGenerator({
}
}, [abandonPick, active, pickGuard]);
- // A diffusion model picked from the chat picker arrives as ?model= on this route. Load it once, then clear the params.
- // This route's own match, never `strict: false`: that resolves to the ROOT match, whose search is whatever route is live, and
- // /hub names its selection with the same param. `active` cannot fence that off, since it lags the matches by a render.
+ // A diffusion model picked from the chat picker arrives as ?model= on this route. This route's
+ // own match, never `strict: false`: that resolves to the ROOT match, whose search is
+ // whatever route is live, and /hub names its selection with the same param.
const routeSearch = useSearch({ from: "/video", shouldThrow: false });
const navigateSelf = useNavigate();
const handledRouteModel = useRef<string | null>(null);
@@ -2673,13 +2598,14 @@ function VideoGenerator({
if (!active) return;
if (!videoPresets.hydrated) return;
const wanted = routeSearch?.model;
- // Model AND quant, released once the query is gone: this page stays mounted, so a marker that outlived the query made re-picking a dead click.
+ // Model AND quant, released once the query is gone: this page stays mounted, so a marker that
+ // outlived the query made re-picking a dead click.
if (!wanted) {
handledRouteModel.current = null;
return;
}
- // `quant` is used verbatim as a filename; a label there (a hand-built link, an older producer) is resolved instead.
- // The two fields, not the object: `routeSearch` is rebuilt every render, so it would churn the deps.
+ // `quant` is used verbatim as a filename, so a label there is resolved instead. The two
+ // fields, not the object: `routeSearch` is rebuilt every render.
const routed = { quant: routeSearch?.quant, ggufQuant: routeSearch?.ggufQuant };
const routedFilename = routedGgufFilename(routed);
const routedLabel = routedGgufLabel(routed);
@@ -2689,8 +2615,8 @@ function VideoGenerator({
// This arrival owns the page like a direct pick, so a download staged by an earlier one cannot land on top.
const token = pickGuard.claim();
void navigateSelf({ to: "/video", search: {}, replace: true });
- // A label means a GGUF repo whatever the catalog says, and is not loadable, so resolve it instead of routing it as a
- // filename.
+ // A label means a GGUF repo whatever the catalog says, and is not loadable, so resolve it
+ // rather than routing it as a filename.
if (routedLabel) {
// Deferred, not inline: resolution is a request, and the load it fires owns the state a direct pick sets.
void Promise.resolve().then(() =>
@@ -2698,7 +2624,8 @@ function VideoGenerator({
);
return;
}
- // Same catalog lookup a direct pick makes: the chat picker can only forward a GGUF filename, so a curated single-file artifact would load as a pipeline and fail.
+ // Same catalog lookup a direct pick makes: the chat picker can only forward a GGUF filename,
+ // so a curated single-file artifact would load as a pipeline and fail.
const pick = diffusionRoutePick(
wanted,
routedFilename ?? undefined,
@@ -2710,7 +2637,7 @@ function VideoGenerator({
return;
}
// Match every direct picker branch: the routed intent owns both the visible build label and
- // the model-specific Default recipe, and a load that never becomes resident rolls both back.
+ // the Default recipe, and a load that never becomes resident rolls both back.
const revert: PickRevert = quantRevert.current ?? { prev: quant, steps, guidance };
quantRevert.current = revert;
setQuant(pick.opts.kind === "pipeline" ? null : (pick.opts.filename ?? null));
@@ -2750,8 +2677,8 @@ function VideoGenerator({
// The task dialog defers the load out of the branch that snapshotted the rollback, so the two
- // ways out of it carry that branch's two endings: choosing runs the load and reverts if it never
- // starts, cancelling abandons the pick outright.
+ // ways out carry that branch's two endings: choosing runs the load and reverts if it never
+ // starts, cancelling abandons the pick.
const chooseH3Task = useCallback(
(task: H3Task) => {
const pending = pendingH3Load;
@@ -2782,8 +2709,8 @@ function VideoGenerator({
// Reload the current model with the current advanced options.
const handleReapply = useCallback(() => {
- // Status is authoritative when another client replaced the resident model. The ref remains
- // the fallback while this page's own load is committing and status has not caught up yet.
+ // Status is authoritative when another client replaced the resident model; the ref remains the
+ // fallback while this page's own load is committing.
const l = lastLoad.current;
if (l) {
void handleLoad(l.repoId, {
@@ -2794,11 +2721,10 @@ function VideoGenerator({
}
}, [handleLoad]);
- // The chat picker emits (modelId, quant + filename) for a GGUF, or just (modelId) for a curated pipeline pick.
- // Every pick supersedes the one before it, whichever route it takes. A staged download outlives
- // its pick, and the direct-local branches call handleLoad rather than loadOrStage, so clearing
- // only inside loadOrStage left the old job's onReady free to load the abandoned model over the
- // one just chosen. Bumping the sequence here also invalidates any plan still in flight.
+ // The chat picker emits (modelId, quant + filename) for a GGUF, or just (modelId) for a
+ // curated pipeline pick. Every pick supersedes the one before it: a staged download outlives
+ // its pick, so clearing only inside loadOrStage left the old job free to load the abandoned
+ // model. This also invalidates any plan still in flight.
const beginPick = useCallback(() => {
pickSeq.current += 1;
pendingStagedLoad.current = null;
@@ -2811,23 +2737,20 @@ function VideoGenerator({
// Ignore picks while a load/generation/unload is in flight.
if (busy !== null) return;
beginPick();
- // This pick owns the page now, so one still awaiting a listing or a plan drops out. Before any branch: staging never
- // sets `busy`, so any pick can land on an awaiting one.
+ // This pick owns the page now, so one still awaiting a listing or a plan drops out. Before any
+ // branch, since staging never sets `busy`.
const token = pickGuard.claim();
// Curated non-GGUF model: load as a full pipeline.
const spec = loadSpecFor(id, VIDEO_CATALOG);
if (spec && spec.kind !== "gguf") {
- // Carried forward when one is already pending: a superseded staged pick left its
- // optimistic quant and recipe in state, so snapshotting now would record THAT and
- // restore a model which never loaded. The live entry already holds the resident one.
- // Registers its own rollback like every other branch. Leaving the previous pick's entry in
- // place would let that older staged download, on cancelling, revert to state from before it
- // -- over a selection this pick already replaced -- and leave this one with no rollback.
+ // Carried forward when one is already pending: a superseded staged pick left its optimistic
+ // quant and recipe in state, so snapshotting now would record THAT and restore a model that
+ // never loaded. Leaving the old entry would also let that download revert this pick.
const revert: PickRevert = quantRevert.current ?? { prev: quant, steps, guidance };
quantRevert.current = revert;
setQuant(null);
- // The distilled variant lives in the checkpoint name, not the repo id, so include the filename when seeding defaults.
- // Without it these distilled entries fall through to the generic LTX 40-step/CFG-4 defaults instead of the 8-step schedule.
+ // The distilled variant lives in the checkpoint name, not the repo id, so include the filename
+ // when seeding defaults, or these fall through to the generic LTX 40-step/CFG-4 values.
applyVideoModelDefaults(spec.filename ? `${id}/${spec.filename}` : id);
if (isH3PipelinePick(id, spec.kind)) {
setPendingH3Load({
@@ -2851,7 +2774,8 @@ function VideoGenerator({
});
return;
}
- // GGUF quant pick from the variant expander. Optimistic for picker feedback, reverted if the load fails to START; the poll owns the after-start revert.
+ // GGUF quant pick from the variant expander. Optimistic for picker feedback, reverted if the
+ // load fails to START; the poll owns the after-start revert.
if (meta.ggufVariant && meta.ggufFilename) {
const revert: PickRevert = quantRevert.current ?? { prev: quant, steps, guidance };
quantRevert.current = revert;
@@ -2879,8 +2803,8 @@ function VideoGenerator({
const filename = slash >= 0 ? norm.slice(slash + 1) : norm;
const dir = slash >= 0 ? norm.slice(0, slash) : ".";
if (!filename.toLowerCase().endsWith(".gguf")) {
- // A repo id or local directory, not a file. The listing names its .gguf and the label picks between siblings; a
- // local pick passes its directory so the listing reads that path, not a hub repo.
+ // A repo id or local directory, not a file: the listing names its .gguf and the label picks
+ // between siblings, and a local pick passes its directory so the listing reads that path.
void loadGgufRepoPick(
id,
meta.ggufVariant ?? null,
@@ -2901,7 +2825,8 @@ function VideoGenerator({
});
return;
}
- // A direct local .safetensors pick must load via from_single_file: the pipeline route rejects a bare file, and only after evicting the resident model.
+ // A direct local .safetensors pick must load via from_single_file: the pipeline route rejects
+ // a bare file, and only after evicting the resident model.
if (meta.source === "local" && id.toLowerCase().endsWith(".safetensors")) {
const norm = id.replace(/\\/g, "/");
const slash = norm.lastIndexOf("/");
@@ -2919,8 +2844,8 @@ function VideoGenerator({
});
return;
}
- // A GGUF repo with no filename: these used to fall through to the pipeline branch below, which the backend rejects
- // for a single-file GGUF repo.
+ // A GGUF repo with no filename: these used to fall through to the pipeline branch below, which
+ // the backend rejects for a single-file GGUF repo.
if (spec?.kind === "gguf" || meta.ggufVariant) {
// An artifact that names its file short-circuits the listing; otherwise the label is the hint.
void loadGgufRepoPick(
@@ -2931,20 +2856,21 @@ function VideoGenerator({
);
return;
}
- // Otherwise treat it as a full diffusers repo. The backend gates loads to unsloth/* repos, the family bases, or on-device paths.
+ // Otherwise treat it as a full diffusers repo. The backend gates loads to unsloth/* repos, the
+ // family bases, or on-device paths.
if (meta.source !== "local" && !id.toLowerCase().startsWith("unsloth/")) {
toast.error("Only unsloth or on-device video models can be loaded here");
abandonPick();
return;
}
// Its own rollback, like every other branch: leaving the previous pick's entry live lets an
- // older staged download revert over a selection this pick already replaced.
+ // older staged download revert over a selection this pick replaced.
const revert: PickRevert = quantRevert.current ?? { prev: quant, steps, guidance };
quantRevert.current = revert;
setQuant(null);
applyVideoModelDefaults(id);
// The on-device copy of the H3 pipeline lands here rather than in the curated branch, and
- // it needs the same partition question: without it the load silently takes fl2va.
+ // needs the same partition question: without it the load silently takes fl2va.
if (isH3PipelinePick(id, "pipeline")) {
setPendingH3Load({
repoId: id,
@@ -2983,14 +2909,11 @@ function VideoGenerator({
try {
setStatusIfNewest(++statusTicket.current, await unloadVideoModel());
setQuant(null);
- // Hold the page until any load start still in flight has run to its END, compensating
- // unload and all. The selector's eject routes straight here, so without the fence an eject
- // landing before the start registered returned success and cleared busy: the user picks
- // another model, the backend refuses it ("a load is already in progress") because the older
- // start won the race, and that older handler -- seeing the newer loadSeq -- skips its
- // compensating unload and returns without restarting its poll, leaving a multi-gigabyte
- // load running with no toast and no cancel control. Same fence handleCancelLoad and the
- // external-eject listener take.
+ // Hold the page until any load start still in flight has run to its END, compensating unload
+ // and all. Without the fence an eject landing before the start registered returned success
+ // and cleared busy, so the next pick was refused while the older load carried on.
+ // That older handler, seeing the newer loadSeq, skips its compensating unload and returns without
+ // restarting its poll, leaving a multi-gigabyte load running with no toast and no cancel control.
const pending = pendingStart.current;
if (pending) {
try {
@@ -2999,38 +2922,35 @@ function VideoGenerator({
// Its own handler reports the failure; this only waits for the window to close.
}
}
- // The wait above can end with the tracking RESTORED: handleLoad's compensating unload
- // failed, so the load is still running. This eject stopped nothing, so do not report
- // success -- the caller would toast "stopped loading" over a live load.
+ // The wait above can end with the tracking RESTORED: handleLoad's compensating unload failed,
+ // so the load is still running and this eject stopped nothing.
return !loadTrackingRestored.current;
} catch (err) {
toast.error(err instanceof Error ? err.message : "Failed to unload model");
void refreshStatus();
return false;
} finally {
- // Not an unconditional clear. A restore during the wait above put the page back to
- // "loading" deliberately; wiping it hides the toast's Cancel and the "Cancel load"
- // button and re-enables the picker over a load that is still running.
+ // Not an unconditional clear: a restore during the wait put the page back to "loading"
+ // deliberately, and wiping it hides the Cancel controls over a load still running.
setBusy((prev) => (prev === "unloading" ? null : prev));
}
}, [refreshStatus, dropResidentState]);
- // Cancelling a load IS the unload: it sets the running load's cancel event, bumps the load token so the
- // worker can never commit, and drops the load marker. What it leaves behind is only cache: bytes already
- // fetched stay in the HF cache, so loading the same model again resumes instead of restarting, and no
- // half-built pipeline survives (the worker's commit is token-gated and unload clears the GPU state).
+ // Cancelling a load IS the unload: it sets the load's cancel event, bumps the load token so
+ // the worker can never commit, and drops the load marker. What it leaves is only cache, so
+ // loading the same model again resumes.
const handleCancelLoad = useCallback(async () => {
const wasLoading = busy === "loading";
if (await handleUnload()) {
- // handleUnload holds the page for the whole pending-start path before it returns, so by
- // here the window the backend's "a load is already in progress" refusal lives in is shut.
+ // handleUnload holds the page for the whole pending-start path, so by here the window for the
+ // backend's "a load is already in progress" refusal is shut.
toast.info("Stopped loading the model", {
description: "Anything already downloaded stays cached, so loading it again resumes.",
});
return;
}
- // Already restored inside handleUnload (its own compensating-unload failure), so the toast
- // and the poll are up: a second restore would raise a duplicate toast and a second poll loop.
+ // Already restored inside handleUnload, so the toast and poll are up: a second restore would
+ // raise a duplicate toast and a second poll loop.
if (!wasLoading || loadTrackingRestored.current) return;
// The unload failed, so the load is still running and its tracking was torn down for nothing.
restoreLoadTracking();
@@ -3071,7 +2991,7 @@ function VideoGenerator({
return;
}
}
- // Resolve a base seed up front: with a random one we still pick a concrete seed now so the recipe records it.
+ // Resolve a base seed up front: even a random one is pinned now so the recipe records it.
let resolvedSeed: number | undefined;
if (seed.trim()) {
const n = Number(seed);
@@ -3090,8 +3010,8 @@ function VideoGenerator({
setBusy("generating");
setGenStep(null);
- // The POST only STARTS the job and returns at once (a clip takes minutes, and the secure-mode tunnel caps responses near 100s).
- // A synchronous rejection still surfaces here; everything after acceptance arrives via the poll.
+ // The POST only STARTS the job and returns at once (a clip takes minutes, and the secure-mode
+ // tunnel caps responses near 100s). A synchronous rejection still surfaces here.
try {
await generateVideo({
prompt: prompt.trim(),
@@ -3144,12 +3064,12 @@ function VideoGenerator({
toast.error(err instanceof Error ? err.message : "Video generation failed");
setBusy(null);
setGenStep(null);
- // The refusal can be "No video model is loaded": re-read rather than leave Generate
- // enabled against a runtime that is already free.
+ // The refusal can be "No video model is loaded": re-read rather than leave Generate enabled
+ // against a runtime that is already free.
void resyncAfterGenerateRefusal();
return;
}
- // Track live progress + the terminal outcome via the shared poll loop (also used by the mount-time resume).
+ // Track live progress + the terminal outcome via the shared poll loop.
startGenPoll();
}, [
prompt,
@@ -3210,7 +3130,8 @@ function VideoGenerator({
["max", "Max"],
]}
/>
- {/* The dense transformer_quant fast path only engages on a full-pipeline load, so gate the control and otherwise show why it is unavailable. */}
+ {/* The dense transformer_quant fast path engages only on a full-pipeline load, so gate the
+ control and otherwise show why it is unavailable. */}
{!status?.loaded || status.model_kind === "pipeline" ? (
<AdvancedSelect
label="Precision"
@@ -3299,8 +3220,8 @@ function VideoGenerator({
);
return (
- // The chat-style layout gives this page no outer top inset, so clear the custom
- // titlebar here (34px on win/linux, 0 under macOS's native one) as chat does.
+ // The chat-style layout gives this page no outer top inset, so clear the custom titlebar here as chat does.
+ // 34px on win/linux, 0 under macOS's native one.
<div className="diffusion-surface flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden pt-[var(--studio-content-top-inset,0px)]">
<AlertDialog
open={active && clearConfirmOpen}
@@ -3337,8 +3258,8 @@ function VideoGenerator({
if (!open) cancelH3TaskChoice();
}}
>
- {/* Squarer than the shared dialog's rounded-4xl: at this width the default reads as a
- lozenge rather than a panel. The option cards step down from it so the nesting holds. */}
+ {/* Squarer than the shared dialog's rounded-4xl: at this width the default reads as a lozenge
+ rather than a panel. */}
<DialogContent className="max-w-lg rounded-2xl">
<DialogHeader>
<DialogTitle>Choose how MiniMax H3 should generate</DialogTitle>
@@ -3377,7 +3298,8 @@ function VideoGenerator({
</div>
</DialogContent>
</Dialog>
- {/* Top: the model selector, sitting clear of the sidebar and level with the controls column below. Load progress shows in a toast. */}
+ {/* Top: the model selector, clear of the sidebar and level with the controls column. Load
+ progress shows in a toast. */}
<div className="@container pointer-events-none relative z-40 flex h-[48px] shrink-0 items-start justify-between pl-[var(--studio-media-header-left-inset,1.5rem)] pr-2 pt-[var(--studio-chat-header-padding-top,11px)]">
{/* min-w-0: without it a long resident model name pushes the Images link off a phone screen. */}
<div className="pointer-events-auto flex min-w-0 items-center gap-3">
@@ -3396,10 +3318,9 @@ function VideoGenerator({
open={active && selectorOpen}
onOpenChange={(o) => setSelectorOpen(active && o)}
/>
- {/* The load's own cancel, beside the selector rather than inside it: the selector's eject needs a
- resident model, so it is hidden for exactly the span a first load runs. A real button, not the
- trigger's aria-hidden eject hit area, so it is reachable by keyboard and a screen reader. Says
- "load", never "download": the download manager's own Cancel stops a staged pull, a different job. */}
+ {/* The load's own cancel, beside the selector rather than inside it: the selector's eject needs
+ a resident model, so it is hidden for exactly the span a first load runs. A real button,
+ so it is keyboard reachable. Says "load", never "download": that Cancel stops another job. */}
{busy === "loading" && (
<Tooltip>
<TooltipTrigger asChild={true}>
@@ -3417,7 +3338,7 @@ function VideoGenerator({
<TooltipContent>Stop loading this model</TooltipContent>
</Tooltip>
)}
- {/* Loaded-model status line: family / kind / offload / speed, as the images page surfaces on load. Hidden until a model is resident. */}
+ {/* Loaded-model status line: family / kind / offload / speed. Hidden until a model is resident. */}
{status?.loaded && (
<div className="hidden min-w-0 items-center gap-3 text-ui-11 @min-[720px]:flex">
{status.family && <StatusChip label="Family" value={status.family} />}
@@ -3436,11 +3357,10 @@ function VideoGenerator({
</div>
</div>
- {/* Controls rail + preview canvas, as on the Images tabs: no cards, a rule the full page
- height. Full width, so the preview grows with the window.
- Gutters match Images, so both pages' content starts at the same 40px. */}
- {/* overflow-x-hidden: an unset overflow-x computes to auto beside overflow-y-auto,
- letting a wide row pan the page sideways on a phone. */}
+ {/* Controls rail and preview canvas, as on the Images tabs. Gutters match Images, so both
+ pages' content starts at the same 40px. */}
+ {/* overflow-x-hidden: an unset overflow-x computes to auto beside overflow-y-auto, letting a
+ wide row pan the page sideways on a phone. */}
<div className="flex min-h-0 w-full min-w-0 flex-1 flex-col overflow-y-auto overflow-x-hidden pl-2 pr-5 pt-9 sm:pr-8 md:flex-row md:overflow-hidden">
{/* Widened by the pl-8 so the controls keep their old width. */}
<div className="flex w-full shrink-0 flex-col border-b border-border/60 pl-8 md:w-[400px] md:overflow-hidden md:border-r md:border-b-0">
@@ -3453,8 +3373,7 @@ function VideoGenerator({
settingsFadeClass,
)}
>
- {/* Names the pane, as the Images column does. Same shape there, so
- the two pages stay level. */}
+ {/* Names the pane, as the Images column does. Same shape there, so the two pages stay level. */}
<div className="mb-2 flex items-start justify-between gap-3">
<div className="min-w-0 grid gap-1.5">
<h2 className="flex items-center gap-2 font-heading text-xl font-medium leading-none text-foreground">
@@ -3992,7 +3911,8 @@ function VideoGenerator({
<div className="hover-scrollbar relative flex flex-1 items-center justify-center overflow-auto p-6">
{selected && selectedSrc ? (
<>
- {/* The first video element in the app. autoPlay + muted + playsInline so it plays inline without a gesture; controls let the user scrub. onEnded replays up to 3 total plays, and the counter resets per selection. */}
+ {/* autoPlay + muted + playsInline so it plays inline without a gesture; controls let the user
+ scrub. onEnded replays up to 3 total plays, reset per selection. */}
<video
key={selected.id}
ref={previewRef}
@@ -4062,7 +3982,7 @@ function VideoGenerator({
</div>
</>
) : selected ? (
- // The selected record's link has not landed yet -- spin in place.
+ // The selected record's link has not landed yet; spin in place.
<div className="flex flex-col items-center gap-3 text-muted-foreground">
<Spinner className="size-8" />
<p className="text-sm">Loading…</p>
@@ -4079,7 +3999,8 @@ function VideoGenerator({
</div>
)}
- {/* Live generation progress: a per-step bar with the phase label + ETA, centered when there is nothing else to show. */}
+ {/* Live generation progress: a per-step bar with the phase label and ETA, centered when there
+ is nothing else to show. */}
{busy === "generating" && (
<div
className={cn(
@@ -4114,15 +4035,16 @@ function VideoGenerator({
if (el.scrollWidth - el.scrollLeft - el.clientWidth < 400) void loadMore();
}}
>
- {/* In-progress generation: a placeholder tile at the front so past clips stay visible and browsable while the new one renders. */}
+ {/* In-progress generation: a placeholder tile at the front so past clips stay browsable while
+ the new one renders. */}
{busy === "generating" && (
<div className="flex size-16 shrink-0 animate-pulse items-center justify-center rounded-[10px] bg-muted/50 ring-2 ring-primary/30">
<Spinner className="size-5 text-muted-foreground" />
</div>
)}
- {/* The card is a wrapper, not a button: the actions menu has to be the select
- button's SIBLING, since a button inside a button is invalid and would swallow
- its own clicks. data-clip-id rides the wrapper so the observer still sees it. */}
+ {/* The card is a wrapper, not a button: the actions menu must be the select button's SIBLING,
+ since a button inside a button is invalid. data-clip-id rides the wrapper so the observer
+ still sees it. */}
{videos.map((video) => (
<div
key={video.id}
@@ -4137,7 +4059,7 @@ function VideoGenerator({
className="relative flex size-full flex-col justify-end overflow-hidden rounded-[10px] bg-muted/40 outline-none ring-1 ring-transparent transition-shadow hover:ring-border focus-visible:ring-2 focus-visible:ring-ring"
>
{srcById[video.id] ? (
- // Muted, preload="metadata" so the first frame renders as a poster without playing every card at once.
+ // Muted, preload="metadata" so the first frame renders as a poster without playing every card.
<video
src={srcById[video.id]}
muted
@@ -4151,7 +4073,8 @@ function VideoGenerator({
<Spinner className="size-4 text-muted-foreground" />
</span>
)}
- {/* A terse caption strip so cards read at a glance. Left/bottom padding clears the rounded corner and the selection border. */}
+ {/* A terse caption strip so cards read at a glance. Left/bottom padding clears the rounded
+ corner and the selection border. */}
<span className="relative z-10 truncate bg-gradient-to-t from-black/70 to-transparent px-2 pb-1 pt-2 text-left text-ui-9 font-medium leading-none text-white">
{clipMeta(video)}
</span>