mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-30 16:39:33 +01:00
feat(local-model): a library of models, with one in use
The local model window could hold one model. A second install was refused (409 already-installed), and a model a Remove kept could never be freed from the window again. The store can now hold several models, and exactly one of them is the model in use: the one the endpoint record lists, the runtime is started for and a session may be launched on. Server - LocalModelStateFile.library lists every model that passed the check (id, label, context, digest, size, coding agent). The service writes the entry when verify passes; the reducer carries it and remove drops it. An installed file of an earlier build counts its one model. - POST /api/local-model/install is allowed from installed: it adds a model, and the earlier one stays. A replaced job no longer deletes a model of the library, only an unchecked or partial download. - POST /api/local-model/use switches to a checked model: a job of the two steps verify and connect. The stored digest is compared before anything is loaded (model-changed otherwise). Nothing is downloaded. - DELETE /api/local-model/models deletes one model by its name, through the runtime's own delete, never by a path, never the model in use. - Adding and switching answer 409 sessions-open while a live session uses the endpoint. Until such a job ends installed, a session start is refused with the new code job-unfinished. - The view carries library (the state's entries joined with what the running runtime lists: ready, changed, missing, unverified) and store (the size of the blob folder). Both are cached and refreshed in the background, never read on the one-second poll. - With nothing installed, remove is offered while the store still holds models an earlier Remove kept. Window - The installed screen lists the models on this Mac with the context each was checked with, its state and its size on disk, with Use and Delete per row (Delete asks once, in the row) and Add a model. - In the chooser a model that is on this Mac already says so and is switched to without a download or a confirm screen. - A paused or failed job that has a checked model beside it shows the way back to that model. Every route is admin only in multi-user mode, like the existing ones. Not run on a Mac: the tests use fakes and the fake runtime, and the listing, store size and delete were checked against a real Ollama 0.13.0 on Linux, not the pinned 0.40.15 build for macOS. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
f62c1f7e2f
commit
70dc42b248
20 files changed
+4088
-201
No files matched your search
+687
-74
File diff suppressed because it is too large.
Load diff
@@ -808,6 +808,45 @@ export async function removePartialModelBlobs(paths?: LocalModelPaths): Promise<
|
||||
return freed;
|
||||
}
|
||||
|
||||
/** How many files of the blob folder are counted at most: a store holds a few dozen, and a walk must end. */
|
||||
const STORE_MEASURE_MAX_FILES = 20_000;
|
||||
|
||||
/**
|
||||
* What the model store takes on disk: the sizes of the regular files directly in
|
||||
* `<models>/blobs`, summed. That folder is flat (one file per layer, plus the partial files of
|
||||
* a download), so this is the store's real disk use, shared layers counted once, an
|
||||
* interrupted download included. The manifests beside it are a few kilobytes and are left out.
|
||||
*
|
||||
* One `readdir` and one `lstat` per file, at most `STORE_MEASURE_MAX_FILES` of them. Nothing is
|
||||
* followed: a `root`, a model store or a blob folder that is a symbolic link counts as empty,
|
||||
* and so does a link in the place of a file. Never outside `LocalModelPaths.root`. Resolves 0
|
||||
* when there is no store. Never throws.
|
||||
*/
|
||||
export async function measureModelStore(paths?: LocalModelPaths): Promise<number> {
|
||||
let total = 0;
|
||||
try {
|
||||
const resolved = paths ?? localModelPaths(OLLAMA_RUNTIME_PIN.version);
|
||||
const blobs = join(resolved.modelsDir, 'blobs');
|
||||
if (!isInsideLocalModelRoot(blobs, resolved.root)) return 0;
|
||||
for (const folder of [resolved.root, resolved.modelsDir, blobs]) {
|
||||
const info = await lstat(folder);
|
||||
if (info.isSymbolicLink() || !info.isDirectory()) return 0;
|
||||
}
|
||||
const names = await readdir(blobs);
|
||||
for (const name of names.slice(0, STORE_MEASURE_MAX_FILES)) {
|
||||
try {
|
||||
const info = await lstat(join(blobs, name));
|
||||
if (info.isFile()) total += info.size;
|
||||
} catch {
|
||||
// Gone between the listing and the look: a download or a delete is under way.
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// No model store yet.
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// ---------- Port ----------
|
||||
|
||||
/** Whether nothing holds `port` on 127.0.0.1, decided by binding it and letting go again. */
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
type LocalModelErrorCode,
|
||||
type LocalModelInstallState,
|
||||
type LocalModelInstallStep,
|
||||
type LocalModelLibraryEntry,
|
||||
type LocalModelStateFile,
|
||||
} from '../types.js';
|
||||
import { OLLAMA_RUNTIME_PIN } from './runtime.js';
|
||||
@@ -153,6 +154,41 @@ function parseConnection(value: unknown): LocalModelStateFile['connection'] {
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/** How many checked models a state file may list: far more than one Mac holds, and a bound on what a damaged file can cost. */
|
||||
const MAX_LIBRARY_ENTRIES = 64;
|
||||
|
||||
/**
|
||||
* The checked models of a file. An entry without its id, label, context or digest is dropped
|
||||
* (it could never be switched to), and so is a second entry for an id. `undefined` for a file
|
||||
* that has no list, which is what an earlier build wrote.
|
||||
*/
|
||||
function parseLibrary(value: unknown): LocalModelLibraryEntry[] | undefined {
|
||||
if (!Array.isArray(value)) return undefined;
|
||||
const entries: LocalModelLibraryEntry[] = [];
|
||||
for (const item of value) {
|
||||
if (entries.length >= MAX_LIBRARY_ENTRIES) break;
|
||||
if (!isRecord(item)) continue;
|
||||
const id = text(item.id, 200);
|
||||
const label = text(item.label, 200);
|
||||
const context = count(item.context);
|
||||
const digest = text(item.digest, 200);
|
||||
if (!id || !label || !context || !digest || entries.some((entry) => entry.id === id)) continue;
|
||||
const bytes = count(item.bytes);
|
||||
const cli = parseCli(item.cli);
|
||||
const verifiedAt = text(item.verifiedAt, 64);
|
||||
entries.push({
|
||||
id,
|
||||
label,
|
||||
context,
|
||||
digest,
|
||||
...(bytes ? { bytes } : {}),
|
||||
...(cli ? { cli } : {}),
|
||||
...(verifiedAt ? { verifiedAt } : {}),
|
||||
});
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* The state a file's text stands for. Pure and total: text that is not a JSON object of
|
||||
* `schema: 1` with a known `state` is `IDLE_LOCAL_MODEL_STATE`. Members it does not know are
|
||||
@@ -180,6 +216,7 @@ export function parseLocalModelState(raw: string): LocalModelStateFile {
|
||||
const connection = parseConnection(parsed.connection);
|
||||
const updatedAt = text(parsed.updatedAt, 64);
|
||||
const replacedModelId = text(parsed.replacedModelId, 200);
|
||||
const library = parseLibrary(parsed.library);
|
||||
if (!model || !cli) return IDLE_LOCAL_MODEL_STATE;
|
||||
|
||||
if (state === 'installed') {
|
||||
@@ -192,6 +229,7 @@ export function parseLocalModelState(raw: string): LocalModelStateFile {
|
||||
cli,
|
||||
runtime,
|
||||
...(loaded ? { loaded } : {}),
|
||||
...(library ? { library } : {}),
|
||||
...(connection ? { connection } : {}),
|
||||
...(updatedAt ? { updatedAt } : {}),
|
||||
};
|
||||
@@ -215,6 +253,7 @@ export function parseLocalModelState(raw: string): LocalModelStateFile {
|
||||
...(runtime ? { runtime } : {}),
|
||||
...(progress ? { progress } : {}),
|
||||
...(loaded ? { loaded } : {}),
|
||||
...(library ? { library } : {}),
|
||||
...(connection ? { connection } : {}),
|
||||
...(replacedModelId ? { replacedModelId } : {}),
|
||||
...(updatedAt ? { updatedAt } : {}),
|
||||
|
||||
Reference in new issue
Block a user