API for agents: tokens, /api/v1 for downloads, verify, repair, tags
Brings back the token API that 0.15 removed with deployments, for models only, ported onto the current code (local models, lean client bundles, MiB-rounded disk space): - Scoped bearer tokens (models:read, downloads:create, models:verify, tags:write), stored as SHA-256, managed in Settings -> API tokens. Only /api/v1 accepts them; the rest stays OIDC-only. - /api/v1: Hub lookup, library with the search filters, download requests through the add-model/queue path (idempotent), repair of missing or broken files, verify / deep verify, tags, job status with ?wait=, cancelling or withdrawing the token's own jobs, disk space. - Guardrails for downloads and repairs: approval (default ask), max size per request, minimum free space incl. reserved bytes, agent jobs at once, author allowlist (new downloads only). Approve/reject in the UI, "requested by" badges and filter, activity log per token. - Migration 0027 adds api_tokens, agent_activity and requested_by columns on models and jobs; job state pending_approval. - docs/api.md, docs/agent/SKILL.md and the kaname curl helper; DB-backed API tests. The login gate test now expects /api/v1 to be left to token auth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7d1a8fb85a
commit
0e0445a418
63 files changed
+5448
-42
No files matched your search
@@ -2,6 +2,7 @@ import "server-only";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { settings, workerState } from "@/lib/db/schema";
|
||||
import { APPROVAL_MODES, type Guardrails, parseGuardrails } from "@/lib/agent-guardrails";
|
||||
import { type Backdrop, BACKDROP_KEY, parseBackdrop } from "@/lib/backdrop";
|
||||
import { isLabelColor } from "@/lib/colors";
|
||||
import { FILE_ROLE_ORDER, FILE_ROLES, type FileRole, type FileRoleStyles } from "@/lib/file-roles";
|
||||
@@ -157,3 +158,22 @@ export async function saveBackdrop(input: unknown): Promise<Backdrop | null> {
|
||||
await write(BACKDROP_KEY, backdrop);
|
||||
return backdrop;
|
||||
}
|
||||
|
||||
const GUARDRAILS_KEY = "agent.guardrails";
|
||||
|
||||
/** Limits for downloads requested through the API (Settings → API tokens). */
|
||||
export async function getGuardrails(): Promise<Guardrails> {
|
||||
return parseGuardrails(await read<unknown>(GUARDRAILS_KEY));
|
||||
}
|
||||
|
||||
export async function saveGuardrails(input: Partial<Guardrails>): Promise<Guardrails> {
|
||||
const next = { ...(await getGuardrails()), ...input };
|
||||
if (!APPROVAL_MODES.includes(next.approvalMode)) throw new SettingsError(`Approval mode must be one of: ${APPROVAL_MODES.join(", ")}.`);
|
||||
if (!(next.maxRequestBytes >= 1_000_000)) throw new SettingsError("Max size per request must be at least 0.001 GB.");
|
||||
if (!(next.minFreeBytes >= 0)) throw new SettingsError("Minimum free space can't be negative.");
|
||||
if (!Number.isInteger(next.maxOpenJobs) || next.maxOpenJobs < 1 || next.maxOpenJobs > 50) throw new SettingsError("Max agent jobs at once must be 1–50.");
|
||||
const parsed = parseGuardrails(next);
|
||||
if (parsed.allowedAuthors.length !== next.allowedAuthors.length) throw new SettingsError("The author allowlist contains an invalid name.");
|
||||
await write(GUARDRAILS_KEY, parsed);
|
||||
return parsed;
|
||||
}
|
||||
Reference in new issue
Block a user