Files
Pascal LinxweilerandClaude Opus 5.5 0e0445a418 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>
2026-11-11 21:52:28 +01:00

254 lines
12 KiB
Markdown

---
name: kaname-models
description: Find, download, verify, repair and tag local LLMs (Hugging Face models, GGUF quants) on the user's Kaname server. Use when the user asks to get, download, add, check, fix or tag a model, asks which models are available locally, or how much disk space is left for models.
required_environment_variables:
- name: KANAME_URL
prompt: Kaname base URL, e.g. https://models.example.com (no trailing slash)
- name: KANAME_TOKEN
prompt: Kaname API token (kn_…), from Settings → API tokens
---
# Kaname: the user's local model library
Kaname downloads models from Hugging Face onto the user's server, pinned to an exact commit. With
this skill you can:
- see what is already downloaded, and how much disk space is left
- look up a Hugging Face repo (its commit, quants and sizes)
- request a download, and follow it until it's done
- verify a model's files, and repair the ones that are missing or broken
- add and remove tags on models
- cancel jobs that **you** requested
You **cannot** delete models or files, update a model to a newer commit, change settings, or touch
jobs someone else started. When the user needs one of those, tell them to do it in Kaname's web UI.
## Setup
You need two environment variables:
- `KANAME_URL`: e.g. `https://models.example.com` (no trailing slash)
- `KANAME_TOKEN`: the API token (`kn_…`). **Never print it, log it or put it in a URL.**
The examples use `kaname`, a two-line wrapper around `curl` that adds the token and JSON headers.
Install it once:
```bash
mkdir -p ~/.local/bin && curl -fsSL https://git.linxweiler.xyz/pascallinxweiler/Kaname/raw/branch/main/docs/agent/kaname -o ~/.local/bin/kaname && chmod +x ~/.local/bin/kaname
```
If `kaname` isn't available, write the full command every time instead:
`curl -sS -H "Authorization: Bearer $KANAME_TOKEN" -H "Content-Type: application/json" …`.
Every command may run in a fresh shell, so don't rely on a shell function or alias.
Every answer is JSON. Errors look like `{"error": {"code": "…", "message": "…"}}`, and the message
says what to do next. Read it before you do anything else.
## 0. Check who you are and your limits
```bash
kaname "$KANAME_URL/api/v1"
```
This returns your token name, your **scopes** (what you're allowed to do) and the **guardrails**:
| Field | Default | Meaning |
| --- | --- | --- |
| `approvalMode` | `approve` | Your downloads and repairs wait until the user approves them. That's normal, not an error. |
| `maxRequestSize` | 60 GB | One request may download at most this much. |
| `minFreeSize` | 100 GB | This much must stay free on disk after your download. |
| `maxOpenAgentJobs` | 2 | At most this many agent downloads may be pending or running at once. |
| `allowedAuthors` | `[]` (any) | If not empty, new downloads must come from these Hugging Face authors. |
| Scope | Lets you |
| --- | --- |
| `models:read` | look up repos, list models and tags, read jobs and disk space |
| `downloads:create` | request downloads and repairs, cancel your own downloads |
| `models:verify` | start verify and deep verify, cancel your own verify jobs |
| `tags:write` | add and remove tags on models |
If a call answers `403 forbidden`, you lack the scope. Tell the user which one is missing; don't
try another way around it.
## 1. Is the model already here?
Always check before you download.
```bash
kaname "$KANAME_URL/api/v1/models?q=qwen3%208b"
kaname "$KANAME_URL/api/v1/models?author=unsloth&quant=Q4_K_M&status=ready"
```
Filters: `q` (every word must match the repo, author, family, quant or tags), `author`, `family`,
`quant`, `tag`, `status`, `requested_by`. You can repeat a filter or separate values with commas
(OR within a filter, AND across filters).
The `status` values are `pending`, `downloading`, `ready`, `incomplete`, `failed`, `deleting`,
`pending_approval`, `update_available`, `repo_missing` and `commit_missing`.
If the model is there with `status: "ready"`, you're done. Don't request it again. For details on
one model (its files and their state, tags, latest job):
```bash
kaname "$KANAME_URL/api/v1/models/<modelId>"
```
`source: "local"` means a folder the user put there by hand, with no Hugging Face repo. Kaname
can't download into a local model or repair it, but it can verify it and tag it.
## 2. Look up the repo on Hugging Face
```bash
kaname "$KANAME_URL/api/v1/hub/lookup?repo=unsloth/Qwen3-8B-GGUF"
```
`repo` can also be a pasted Hugging Face URL. From the answer, note:
- `sha`: the exact commit. You need it to request the download.
- `quants`: one entry per GGUF quant, with `size`. Pick one that fits what the user asked for and
is no bigger than `maxRequestSize`. If the user didn't say, `Q4_K_M` is a sensible default.
- `inLibrary`: not `null` if the repo is already in Kaname (see step 1).
- `gated`: if it isn't `false`, the download only works when the user's Hugging Face account has
accepted the model's terms.
Check the free space before a big download:
```bash
kaname "$KANAME_URL/api/v1/disk" # availableForRequests = what one request may still use
```
## 3. Request the download
```bash
kaname -X POST "$KANAME_URL/api/v1/downloads" \
-d '{"repoId":"unsloth/Qwen3-8B-GGUF","sha":"<sha from the lookup>","quant":"Q4_K_M"}'
```
- `quant` downloads that quant's weight files (every split part), plus the companion files the user
picked in Kaname's settings (chat template, vision projector, …).
- For repos without GGUF quants (safetensors), send `"include": ["*.safetensors", "*.json"]` instead
of `quant`. Send exactly one of the two.
- Optional: `"tags": ["coding"]`. Run `GET /api/v1/tags` first and reuse the tags that already
exist.
Kaname answers with one of these:
| Answer | Meaning | What you do |
| --- | --- | --- |
| `201`, `state: "pending_approval"` | Waits for the user. | Tell the user what you requested (repo, quant, size) and that they need to approve it in Kaname. **Stop there. Don't retry and don't poll.** |
| `201`, `state: "queued"` | Starts on its own. | Follow it (step 4). |
| `200`, `created: false` | Exactly this was already requested. | You get the existing job back. Don't request it again. |
If the repo is already in the library at the same commit, the request adds only the missing files
(for example another quant). A repo that's in the library at a different commit is refused: only
the user can update it.
## 4. Follow a job
```bash
kaname -m 960 "$KANAME_URL/api/v1/jobs/<jobId>?wait=900"
```
With `?wait=900`, Kaname holds the request open until the job is finished, or up to 900 seconds.
One such call replaces a whole polling loop. Keep curl's `-m` a bit above the wait. Each call costs
you context, so make few of them:
| `state` | What you do |
| --- | --- |
| `pending_approval` | Don't poll. Tell the user it waits for their approval and stop. Check again only when the user says they approved it (or, if they ask for it, once later from a scheduled task). |
| `queued`, `downloading`, `verifying` | One call with `?wait=900`. If it comes back with `timedOut: true`, tell the user the progress and the ETA (`progress.etaSeconds`) instead of waiting again and again. |
| `completed` | Tell the user it's ready. For a verify job, also read `verify` (see step 5). |
| `cancelled` | The user rejected or cancelled it. Don't ask again unless they tell you to. |
| `failed` | Report `error.message` to the user. Don't retry in a loop. |
| `interrupted` | Kaname restarted. Tell the user; they can resume it. |
Add `&log=20` only when you need to explain a failure (the last lines of the download log).
## 5. Verify and repair
Run a verify when the user asks whether a model is intact, or after something went wrong:
```bash
kaname -X POST "$KANAME_URL/api/v1/models/<modelId>/verify" # presence and size, takes seconds
kaname -X POST "$KANAME_URL/api/v1/models/<modelId>/verify" -d '{"deep":true}' # hashes every byte: slow, but thorough
```
A verify starts right away; it never needs approval. Follow it like any other job. When it has
finished, the job has a `verify` block:
```json
"verify": { "ok": false, "missing": ["…gguf"], "sizeMismatch": [], "hashMismatch": [], "next": "POST /api/v1/models/…/repair …" }
```
If `ok` is `false`, repair it. That downloads only the files that are missing or broken:
```bash
kaname -X POST "$KANAME_URL/api/v1/models/<modelId>/repair"
```
A repair counts as a download: same guardrails, and by default it waits for the user's approval
just like step 3. If nothing is broken, Kaname answers `409 nothing_to_repair`. Then don't repair;
run a deep verify if you suspect damage. Deep verify and repair aren't possible for local models.
## 6. Tags
```bash
kaname "$KANAME_URL/api/v1/tags" # existing tags, with how many models use each
kaname -X POST "$KANAME_URL/api/v1/models/<modelId>/tags" -d '{"name":"coding"}' # add (creates the tag if it's new)
kaname -X DELETE "$KANAME_URL/api/v1/models/<modelId>/tags/coding" # remove (by name, case-insensitive)
```
Reuse existing tags instead of inventing near-duplicates (`code` vs `coding`). Removing a tag from a
model doesn't delete the tag itself. Tag only what the user asked for, or what clearly fits their
existing scheme.
## 7. Cancel your own job
```bash
kaname -X POST "$KANAME_URL/api/v1/jobs/<jobId>/cancel"
```
This works only for jobs your token requested:
- A job that's still `pending_approval` is withdrawn. If it was a brand-new model, the model leaves
the library again (`removedModel: true`).
- A queued or running job is stopped. Files that are already downloaded stay.
For anything else you get `403 not_own_job`: tell the user they can cancel it in Kaname.
## When a request is refused
| `error.code` | What to do |
| --- | --- |
| `too_large` | Pick a smaller quant, or fewer files, that fits `maxRequestSize`. |
| `disk_space` | Pick a smaller quant, or tell the user the disk is full. |
| `too_many_jobs` | Your earlier downloads are still pending or running. Tell the user and stop. Don't wait in a loop. |
| `author_not_allowed` | Find the model from an allowed author, or ask the user. |
| `already_exists` | The repo is in the library at another commit; the user updates it in Kaname. |
| `busy` | That model already has a job pending or running. Tell the user and stop. |
| `nothing_to_repair` | Every file is present. Run a deep verify first if you suspect damage. |
| `local_model` | A local folder: nothing to download, and no hashes to compare against. Use a plain verify. |
| `not_own_job` | Only the user can cancel this job. |
| `gated_no_access`, `gated_no_token` | The user must accept the model's terms on Hugging Face (and Kaname needs a token). |
| `disk_unknown` | Kaname's worker is offline. Tell the user and stop. |
| `rate_limited` | Hugging Face is rate limiting. Try once more later, not in a loop. |
| `unauthorized`, `forbidden` | Your token is wrong or revoked, or it lacks a scope. Tell the user. |
| `invalid_request`, `invalid_json` | Fix the request as the message says. |
Never work around a guardrail, for example by splitting one big download into several requests, or
by repairing a model to fetch files it never had. Everything you do is listed under your token's
name in Kaname, refusals included.
## A complete example
> User: "Get me Qwen3 8B, something that fits in about 6 GB."
1. `kaname "$KANAME_URL/api/v1/models?q=qwen3%208b"`: nothing ready yet.
2. `kaname "$KANAME_URL/api/v1/hub/lookup?repo=unsloth/Qwen3-8B-GGUF"`: `Q4_K_M` is 5.0 GB; note `sha`.
3. `kaname -X POST "$KANAME_URL/api/v1/downloads" -d '{"repoId":"unsloth/Qwen3-8B-GGUF","sha":"…","quant":"Q4_K_M"}'`
answers `pending_approval`.
4. Tell the user: "I've requested unsloth/Qwen3-8B-GGUF Q4_K_M (5.0 GB). It's waiting for your
approval in Kaname." Then stop.
5. When the user says it's approved: `kaname -m 960 "$KANAME_URL/api/v1/jobs/<jobId>?wait=900"`, then
report that it's done, or the progress and ETA.