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>
This commit is contained in:
Roland 2026-08-25 21:47:50 -06:00
parent 485a9703eb
commit f86058ee8a
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
10 changed files with 872 additions and 6 deletions

View file

@ -1,5 +1,5 @@
import dataclasses
from typing import Optional
from typing import List, Optional
@dataclasses.dataclass
@ -7,8 +7,8 @@ class AISearchRequest:
search_str: Optional[str] = None
limit: int = 5
offset: int = 0
mimetypes: Optional[list] = None
tag_names: Optional[list] = None
mimetypes: Optional[List[str]] = None
tag_names: Optional[List[str]] = None
@dataclasses.dataclass
@ -17,7 +17,7 @@ class AIVideoSearchRequest:
limit: int = 5
offset: int = 0
channel_id: Optional[int] = None
tag_names: Optional[list] = None
tag_names: Optional[List[str]] = None
@dataclasses.dataclass
@ -26,7 +26,7 @@ class AIArchiveSearchRequest:
limit: int = 5
offset: int = 0
domain: Optional[str] = None
tag_names: Optional[list] = None
tag_names: Optional[List[str]] = None
@dataclasses.dataclass
@ -38,7 +38,7 @@ class AIDocSearchRequest:
mimetype: Optional[str] = None
limit: int = 5
offset: int = 0
tag_names: Optional[list] = None
tag_names: Optional[List[str]] = None
@dataclasses.dataclass