Orca's layout in the app's own look (form plugins-page-rework_rhce3j): a category sidebar with counts (a chip row on a phone), search, All / Installed / Available, a sort (Installed first, Name, Recently updated — per person), and cover cards or compact rows (the grid / list toggle is kept). - New manifest field `category` (backend/plugins/categories.py), strict: an app picks dev, data, agents, chat, scheduling, media or other (the default); a provider / harness / translation sits under its kind's shelf. Not in the approval fingerprint. Every row of GET /api/plugins carries the resolved category; a record from before the field reads its source's plugin.json. - Translations works as soon as the translation kind exists; an empty category is hidden. - ?cat= / ?show= addresses; the old ?tab= / ?kind= links still land. - Docs: docs/plugins.md → "Categories and the Plugins page", CLAUDE.md. Deploy this before installing ai-agent-plugins' plugin-categories branch: an older core refuses the unknown `category` key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
3.7 KiB
Python
106 lines
3.7 KiB
Python
"""A plugin's ``category``: the shelf Settings → Plugins files it under.
|
|
|
|
The kinds are categories of their own (a model provider is always under
|
|
*Providers*, a harness under *Harnesses*, a translation under
|
|
*Translations*); an app plugin picks one of the app shelves, else lands in
|
|
``other``. ``plugin.json``'s ``category`` is checked strictly
|
|
(:func:`parse_category`); the rows of ``GET /api/plugins`` carry the resolved
|
|
id (:func:`category_of`), which the page groups and counts by.
|
|
|
|
Keep :data:`CATEGORIES` in step with ``frontend/src/business/plugins/
|
|
categories.ts`` (the labels and icons live there).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import pathlib
|
|
import threading
|
|
from typing import Any
|
|
|
|
# Each kind's own category. A kind added later only needs its line here.
|
|
KIND_CATEGORY: dict[str, str] = {
|
|
"provider": "providers",
|
|
"harness": "harnesses",
|
|
"translation": "translations",
|
|
}
|
|
|
|
# The shelves an app plugin may pick; ``other`` is the default.
|
|
APP_CATEGORIES: tuple[str, ...] = ("dev", "data", "agents", "chat", "scheduling", "media", "other")
|
|
DEFAULT_CATEGORY = "other"
|
|
|
|
CATEGORIES: tuple[str, ...] = APP_CATEGORIES + tuple(KIND_CATEGORY.values())
|
|
|
|
|
|
def _kind(raw_kind: Any) -> str:
|
|
return raw_kind if isinstance(raw_kind, str) and raw_kind else "app"
|
|
|
|
|
|
def default_for(kind: Any) -> str:
|
|
return KIND_CATEGORY.get(_kind(kind), DEFAULT_CATEGORY)
|
|
|
|
|
|
def parse_category(raw: dict[str, Any], errors: list[str]) -> str:
|
|
"""``raw["category"]`` checked against the plugin's ``kind``: one of
|
|
:data:`CATEGORIES`; a typed plugin's must be its kind's (or absent), an
|
|
app may not claim a kind's shelf. Absent → the kind's default."""
|
|
kind = _kind(raw.get("kind"))
|
|
want = KIND_CATEGORY.get(kind)
|
|
v = raw.get("category")
|
|
if v is None:
|
|
return want or DEFAULT_CATEGORY
|
|
if not isinstance(v, str) or v not in CATEGORIES:
|
|
errors.append(f"category must be one of {', '.join(CATEGORIES)} (got {v!r})")
|
|
return want or DEFAULT_CATEGORY
|
|
if want and v != want:
|
|
errors.append(f"category of a {kind} plugin must be '{want}' (got {v!r})")
|
|
return want
|
|
if not want and v in KIND_CATEGORY.values():
|
|
errors.append(f"category '{v}' is for {v} plugins; an app picks one of {', '.join(APP_CATEGORIES)}")
|
|
return DEFAULT_CATEGORY
|
|
return v
|
|
|
|
|
|
_cache: dict[str, tuple[float, str]] = {}
|
|
_cache_lock = threading.Lock()
|
|
|
|
|
|
def _from_source(src: str | None, kind: Any) -> str | None:
|
|
"""The category a source folder's ``plugin.json`` declares now — for a
|
|
record installed (or promoted) before manifests had one. Cached on the
|
|
file's mtime; a missing or broken file gives None."""
|
|
if not src:
|
|
return None
|
|
mf = pathlib.Path(src) / "plugin.json"
|
|
try:
|
|
mtime = mf.stat().st_mtime
|
|
except OSError:
|
|
return None
|
|
with _cache_lock:
|
|
hit = _cache.get(str(mf))
|
|
if hit and hit[0] == mtime:
|
|
return hit[1]
|
|
try:
|
|
raw = json.loads(mf.read_text(encoding="utf-8"))
|
|
except (OSError, ValueError):
|
|
return None
|
|
if not isinstance(raw, dict):
|
|
return None
|
|
raw = {"kind": kind, "category": raw.get("category")}
|
|
cat = parse_category(raw, [])
|
|
with _cache_lock:
|
|
_cache[str(mf)] = (mtime, cat)
|
|
return cat
|
|
|
|
|
|
def category_of(mf: dict[str, Any] | None, src: str | None = None) -> str:
|
|
"""A record's category: its manifest's, else what its source folder says
|
|
today (validated manifests from before the field), else its kind's
|
|
default."""
|
|
mf = mf or {}
|
|
kind = mf.get("kind") or "app"
|
|
cat = mf.get("category")
|
|
if isinstance(cat, str) and cat in CATEGORIES:
|
|
return cat
|
|
return _from_source(src, kind) or default_for(kind)
|