Local models: Reload button, scans keep their files in step
Files tab → Reload asks the worker for a scan and waits until it has finished. Every scan now syncs local models' file lists with their folders (new, resized, removed files; README) and settles the status. Skipped while a job runs, and for a folder that is gone or too big. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f862717158
commit
80d71a1bc9
9 files changed
+218
-15
No files matched your search
@@ -13,6 +13,7 @@ import { useRecommended } from "@/hooks/use-recommended";
|
||||
import { groupSplitFiles } from "@/lib/file-groups";
|
||||
import { formatBytes, percent, shortSha } from "@/lib/format";
|
||||
import { quantGroup } from "@/lib/selection";
|
||||
import { ReloadButton } from "./reload-button";
|
||||
|
||||
type Candidate = { path: string; size: number };
|
||||
|
||||
@@ -32,6 +33,7 @@ export function FilesHeader({
|
||||
freeBytes,
|
||||
busy,
|
||||
hubMissing,
|
||||
local = false,
|
||||
}: {
|
||||
modelId: string;
|
||||
sha: string;
|
||||
@@ -47,6 +49,8 @@ export function FilesHeader({
|
||||
busy: boolean;
|
||||
/** The repo or pinned commit is gone from the Hub; nothing more can be downloaded. */
|
||||
hubMissing: boolean;
|
||||
/** A local model: "Reload" reads the folder again instead of "Download more". */
|
||||
local?: boolean;
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const [open, setOpen] = useState(false);
|
||||
@@ -128,7 +132,8 @@ export function FilesHeader({
|
||||
<span className="text-muted-foreground">
|
||||
{localCount} of {selectedCount} on disk · {formatBytes(localBytes)}
|
||||
</span>
|
||||
{candidates.length > 0 && !hubMissing && (
|
||||
{local && <ReloadButton />}
|
||||
{!local && candidates.length > 0 && !hubMissing && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant={open ? "outline" : "default"}
|
||||
|
||||
@@ -165,6 +165,7 @@ export function FilesTab({
|
||||
freeBytes={freeBytes}
|
||||
busy={busy}
|
||||
hubMissing={hubMissing}
|
||||
local={local}
|
||||
/>
|
||||
|
||||
<ul className="divide-y">
|
||||
|
||||
@@ -66,7 +66,7 @@ export function LocalModelCardSection({ markdown, localPath }: { markdown: strin
|
||||
return (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
This folder has no README.md. Add one to <code className="font-mono">{localPath}</code> and it shows up here after the
|
||||
next scan of the models folder.
|
||||
next scan of the models folder, or right away with Reload on the Files tab.
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import { RefreshCwIcon } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
/** The worker picks a scan request up with its next heartbeat (every 5 s); a big models folder takes longer. */
|
||||
const TIMEOUT_MS = 90_000;
|
||||
const POLL_MS = 1_500;
|
||||
|
||||
/**
|
||||
* Local models: read the folder again (files and README). Asks the worker for a scan of the models
|
||||
* folder and waits until it has finished, then reloads the page.
|
||||
*/
|
||||
export function ReloadButton() {
|
||||
const router = useRouter();
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
async function reload() {
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await fetch("/api/local-folders/scan", { method: "POST" });
|
||||
const body = await res.json().catch(() => ({}));
|
||||
if (!res.ok || typeof body.requestedAt !== "number") {
|
||||
toast.error(body.error?.message ?? `Could not start a scan (${res.status})`);
|
||||
return;
|
||||
}
|
||||
const deadline = Date.now() + TIMEOUT_MS;
|
||||
while (Date.now() < deadline) {
|
||||
await new Promise((r) => setTimeout(r, POLL_MS));
|
||||
const state = await fetch("/api/local-folders/scan", { cache: "no-store" })
|
||||
.then((r) => (r.ok ? r.json() : null))
|
||||
.catch(() => null);
|
||||
if (state?.lastScanAt != null && state.lastScanAt >= body.requestedAt) {
|
||||
router.refresh();
|
||||
return;
|
||||
}
|
||||
}
|
||||
toast.error("The worker hasn't scanned the folder yet. Is it running?");
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Button size="sm" variant="outline" onClick={() => void reload()} disabled={busy} title="Read the folder again: files and README.md">
|
||||
<RefreshCwIcon className={cn(busy && "animate-spin")} />
|
||||
{busy ? "Reloading…" : "Reload"}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
@@ -1,9 +1,16 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withAuth } from "@/lib/api-auth";
|
||||
import { requestFolderScan } from "@/lib/local-folders";
|
||||
import { getScanState, requestFolderScan } from "@/lib/local-folders";
|
||||
|
||||
/** Ask the worker to rescan the models folder for importable folders. */
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/** Ask the worker to rescan the models folder (importable folders, and local models' files). */
|
||||
export const POST = withAuth(async () => {
|
||||
await requestFolderScan();
|
||||
return new NextResponse(null, { status: 202 });
|
||||
const requestedAt = await requestFolderScan();
|
||||
return NextResponse.json({ requestedAt: requestedAt.getTime() }, { status: 202 });
|
||||
});
|
||||
|
||||
/** When the worker last finished a scan, so a client can wait for the one it asked for. */
|
||||
export const GET = withAuth(async () => {
|
||||
return NextResponse.json(await getScanState());
|
||||
});
|
||||
Reference in new issue
Block a user