ai: add the /api/ai blueprint, the read-only LLM tool catalog
Lean, flat, LLM-shaped read-only endpoints over the library: global,
video, archive, and doc search; paged captions, archive text, and zim
entries; collections, inventories, and a media-directory text reader.
Links are server-computed; big content is paged, never truncated.
This ports the formatting layer from wrolpi_mcp (untouched for now)
into tested code. First phase of ai.md.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-25 21:03:11 -06:00
|
|
|
import dataclasses
|
ai: the /api/chat agent loop — SSE streaming, modes, tool execution
Tool definitions are generated in-process from the /api/ai blueprint's
own OpenAPI operations (operationId = tool name, description = tool
description, dataclass bodies + declared parameters = one flat JSON
schema) — the spec IS the catalog, nothing to drift. Manage endpoints
never become tools. A mode is a server-enforced system prompt + tool
allowlist (Help / Research / System per ai.md); the executor can only
reach allowlisted /api/ai endpoints, path parameters are encoded and
traversal-rejected so a crafted argument cannot escape an endpoint.
POST /api/chat streams SSE (status/token/tool_call/tool_result/done/
error), starts llama-server on demand, validates tool args server-side
with instructive errors the model can retry on, caps tool results with
a paging hint, and bounds the loop by ai.yaml max_tool_calls plus a
hard timeout. Caddy passes the stream unbuffered (flush_interval -1)
in both deployments.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-25 21:47:50 -06:00
|
|
|
from typing import List, Optional
|
ai: add the /api/ai blueprint, the read-only LLM tool catalog
Lean, flat, LLM-shaped read-only endpoints over the library: global,
video, archive, and doc search; paged captions, archive text, and zim
entries; collections, inventories, and a media-directory text reader.
Links are server-computed; big content is paged, never truncated.
This ports the formatting layer from wrolpi_mcp (untouched for now)
into tested code. First phase of ai.md.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-25 21:03:11 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class AIZimSearchRequest:
|
|
|
|
|
search_str: Optional[str] = None
|
|
|
|
|
zim_id: Optional[int] = None
|
|
|
|
|
limit: int = 5
|
|
|
|
|
offset: int = 0
|
|
|
|
|
|
|
|
|
|
|
2026-08-25 21:13:59 -06:00
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class AIHelpSearchRequest:
|
|
|
|
|
search_str: Optional[str] = None
|
|
|
|
|
limit: int = 5
|
|
|
|
|
|
|
|
|
|
|
2026-08-25 21:21:34 -06:00
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class AIManageSettingsRequest:
|
|
|
|
|
enabled: Optional[bool] = None
|
|
|
|
|
active_model: Optional[str] = None
|
|
|
|
|
idle_unload_minutes: Optional[int] = None
|
|
|
|
|
context_size: Optional[int] = None
|
|
|
|
|
|
|
|
|
|
|
ai: add the /api/ai blueprint, the read-only LLM tool catalog
Lean, flat, LLM-shaped read-only endpoints over the library: global,
video, archive, and doc search; paged captions, archive text, and zim
entries; collections, inventories, and a media-directory text reader.
Links are server-computed; big content is paged, never truncated.
This ports the formatting layer from wrolpi_mcp (untouched for now)
into tested code. First phase of ai.md.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-25 21:03:11 -06:00
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class AISearchResponse:
|
|
|
|
|
results: list = dataclasses.field(default_factory=list)
|
|
|
|
|
total: int = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class AIPagedTextResponse:
|
|
|
|
|
content: str = ''
|
|
|
|
|
next_offset: Optional[int] = None
|
|
|
|
|
total_chars: int = 0
|
2026-09-10 15:32:27 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class AIListFilesResponse:
|
|
|
|
|
path: str = ''
|
|
|
|
|
directories: list = dataclasses.field(default_factory=list)
|
|
|
|
|
files: list = dataclasses.field(default_factory=list)
|
|
|
|
|
total: int = 0
|
|
|
|
|
next_offset: Optional[int] = None
|
ai: tags, downloads, map, and search-preview tools
Five new read-only /api/ai endpoints, each with a matching wrolpi_mcp
proxy tool: list_tags (tags with what they are applied to, plus recent
names), search_suggestions (matching channels/domains/authors/subjects
and file/Zim result estimates), list_downloads (queue summary and lean
recurring/one-time entries with relative destinations; status filter;
errors keep their tail, which holds the exception message),
get_map_overview (map files, search-index state, subscriptions, pins
with map links), and search_places (place search with map links and
optional proximity ranking).
Research mode allowlists the library-facing four. Nothing here can
start, stop, or retry a download.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-10 15:48:11 -06:00
|
|
|
|
|
|
|
|
|
ai: kind-generic search_files / get_file / read_content
Small models carry every tool definition on every request, and the
per-kind trios (four searches, three getters, three readers) cost a
quarter of an 8k context while giving the model near-duplicate choices.
Three generic endpoints replace them:
- POST files/search: one search over videos, archives, and docs with an
optional kind; channel (name or id), domain, author, subject each
imply their kind and are ignored for other kinds. When the term also
names channels/domains/authors, they are returned as `matches` so the
model can narrow without a second tool. Channels resolve by name, so
the library map already in the prompt is enough: no list call first.
- GET files/{id}: detail by id whatever the kind; videos now report
has_captions/has_comments and the full description, archives keep
their snapshot history.
- GET files/{id}/content?part=text|comments: captions, page text, or
comments, paged; a wrong part for the kind says what the file is.
Inventories take an optional slug on GET /inventories (one tool lists
and fetches). Listings are slimmed: no derivable captions/text links,
no size/mimetype, duration as H:MM:SS, published as a date. The
library map gains the tag names.
wrolpi_mcp keeps every existing tool name; the wrappers now call the
generic routes, and search_files/get_file/read_content are exposed too.
The old per-kind endpoints remain until the modes switch over.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-10 16:09:50 -06:00
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class AIFileSearchRequest:
|
|
|
|
|
search_str: Optional[str] = None
|
|
|
|
|
kind: Optional[str] = None
|
|
|
|
|
channel: Optional[str] = None
|
|
|
|
|
domain: Optional[str] = None
|
|
|
|
|
author: Optional[str] = None
|
|
|
|
|
subject: Optional[str] = None
|
|
|
|
|
tag_names: Optional[List[str]] = None
|
|
|
|
|
limit: int = 5
|
|
|
|
|
offset: int = 0
|