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>
This commit is contained in:
Roland 2026-09-10 16:09:50 -06:00
parent b4e3b33994
commit d8bd36219e
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
10 changed files with 593 additions and 86 deletions

View file

@ -53,15 +53,21 @@ def test_format_file_group_video():
assert result['id'] == 42
assert result['kind'] == 'video'
assert result['link'] == '/videos/42'
assert result['captions_link'] == '/api/ai/videos/42/captions'
assert result['channel'] == 'Homesteading'
assert result['duration'] == 4021
assert result['duration'] == '1:07:01'
assert result['published'] == '2024-01-02'
assert result['headline'] == 'pressure <b>canning</b>'
assert result['tags'] == ['food']
# Listing descriptions are truncated.
assert len(result['description']) == lib.LISTING_DESCRIPTION_LENGTH + 1
# Empty fields are omitted.
assert 'author' not in result and 'url' not in result
# Listings leave out what the model can derive or does not need to choose an item.
assert 'captions_link' not in result and 'size' not in result and 'mimetype' not in result
# Details add them back.
detail = lib.format_file_group(fg, description_length=lib.DETAIL_DESCRIPTION_LENGTH, detail=True)
assert detail['size'] == 1000 and detail['mimetype'] == 'video/mp4'
assert len(detail['description']) == 500
def test_format_file_group_archive():
@ -69,7 +75,7 @@ def test_format_file_group_archive():
result = lib.format_file_group(fg)
assert result['kind'] == 'archive'
assert result['link'] == '/archives/8'
assert result['text_link'] == '/api/ai/archives/8/text'
assert 'text_link' not in result
# Listings omit the source URL so small models present the WROLPi link instead.
assert 'url' not in result
assert lib.format_file_group(fg, include_url=True)['url'] == 'https://example.com/a'
@ -109,6 +115,17 @@ def test_html_to_text():
assert 'evil' not in text and 'menu' not in text and '.x{}' not in text
def test_format_duration_and_date():
assert lib.format_duration(59) == '0:59'
assert lib.format_duration(692) == '11:32'
assert lib.format_duration(4021) == '1:07:01'
assert lib.format_duration(None) is None and lib.format_duration('x') is None
import datetime
assert lib.format_date(datetime.datetime(2024, 1, 2, 13, 4, 5)) == '2024-01-02'
assert lib.format_date('2024-01-02T13:04:05+00:00') == '2024-01-02'
assert lib.format_date(None) is None
def test_format_caption_chunks():
chunks = [dict(start_seconds=0, text='hello'), dict(start_seconds=3671, text='goodbye')]
text = lib.format_caption_chunks(chunks)