feat(plugins): file viewers — pages by type, fetchFile, inline previews #40

Closed
gabrielvidal wants to merge 0 commits from file-viewers into main
pull from: file-viewers
Owner

Summary

The hub side of "file viewers as plugins" (form viewers-for-pdf-media-and-3d-files_jl74lk): a generic contract so separate plugins can show file types the explorer can't (pdf, media, 3D, html, code, office, archives, fonts). A viewer is its own full plugin page, picked by the file's extension. It reads the bytes through a new fetchFile bridge. A message naming such a file gets an inline preview card under it that opens the same viewer. Guests get viewers the way they get file-explorer (managed, defaultForGuests). What a guest can read is still decided by /api/host-file/raw.

Key changes

  • V1, pages by file type: ui.pages[].fileTypes (1..32 ^[a-z0-9]{1,10}$, valid only on an opens: ["host-file"] page; written only when set). pluginOpenHref(…, "host-file", {path}) picks the first active page whose fileTypes has the path's extension (fileExt: lowercased, :line suffix ignored). Otherwise it picks the first untyped host-file page; folders always go there. File chips, /file/*, a frame's open() and preview hrefs all follow this. Every frame's init now has fileOpeners: {ext: route}, and a typed page is never openers["host-file"]. fileIcon gains icons for 3D, archive, font and audio files.
  • V2, aiAgentPlugin.fetchFile(url) → Promise<Blob>: any hub media/file path (isHubMediaPath), any content type, ≤ 100 MB (MAX_FILE_BYTES), sent with cookies. mediaUrl is unchanged.
  • V3, ui.filePreviews[] ({id, label, html, fileTypes, height, vis?}, ≤ 4 per plugin) in plugins/manifest.py. /api/plugins[/ui] lists them as ui.filePreviews (without html), and /api/plugins/{id}/ui/preview/{previewId} serves the document. PluginFilePreviews.tsx draws them under assistant messages:
    • files are found the way file chips find them (messageHostPaths: inline code, running text, link targets; never inside fenced code), one card per distinct file, at most 4 per message;
    • the first active plugin per extension wins, and the caller must be able to open the file;
    • each card mounts once it is visible; init is {preview, path, name, ext, href, sessionId, conversationId}; resize(0) collapses it;
    • cards sit behind Rich widgets and the preview's own vis.
  • aiAgentPlugin.navigate(path, {replace: true}), so a page that forwards a file to its viewer on load doesn't trap Back.
  • Mock: a pdf-viewer page + preview; the kitchen-sink thread names notes/brief.pdf.
  • Docs: docs/plugins.md → "File viewers: pages by type, fetchFile, previews (since 2026-11-28)", written for the eight viewer-plugin runs.

Key decisions

  • No CSP / sandbox widening was needed. On a live hub, a sandboxed frame (allow-scripts allow-popups allow-popups-to-escape-sandbox, opaque origin) ran all of these with the flags as they are, so nothing was widened:
    • blob: URLs in <img>, <video> and <audio>;
    • FontFace(buffer) and an @font-face from a blob: URL;
    • a blob: Worker, including pdf.js's worker built from its CDN source fetched as text;
    • WebGL2;
    • a jsDelivr <script integrity crossorigin> and an ESM import() from jsDelivr.
  • The first preview per extension is chosen in the app, not deduped server-side the way fences are, because one plugin can list several types.
  • With no generic page active, a viewer still links its own types; usePluginOpener now says "has" when any page opens the kind.

Changelog

  • Added: plugins can be viewers for file types — a PDF, a 3D model, a video named in a conversation opens in its viewer plugin
  • Added: a preview card under an agent's message that names a file a viewer plugin previews

Test notes

  • Backend: test_plugin_file_viewers.py (new: normalisation, refusals, fingerprint unchanged without the keys, descriptors, the preview document, the capability line). Every test_plugin*.py, test_authz_matrix, test_hostfiles and test_csp pass (bwrap tmpfs /data). No route was added, so openapi.json, src/generated and matrix.golden.txt didn't need regenerating.
  • Frontend: fileViewers.test.ts covers opener resolution, fileOpeners and fetchFile limits. PluginFilePreviews.test.tsx covers detection, the per-message rules and mount-on-visible. Full vitest: 1123 pass; the one failure is the known FreshContext CacheExpiredNotice. tsc is clean.
  • Live hub (port 8140, built frontend/dist, a throwaway fileviewer-demo plugin + file-explorer 1.1.0) driven with puppeteer:
    • the chip and the card's Open lead to the typed page with ?path=;
    • fetchFile returned the pdf's bytes;
    • pdf.js rendered the pdf, and every capability check above passed;
    • the preview card appeared under the message, desktop and phone;
    • the explorer sent a .pdf row to the viewer, while a README stayed in the explorer and Back skipped the handover;
    • a guest's fetchFile worked inside their project and got 404 outside it.
  • Deploy from the Homelab after merge; merge the two together with ai-agent-plugins file-viewers-explorer (file-explorer 1.1.0). Nothing was deployed from the EVOX2.

🤖 Generated with Claude Code

Screenshots

conversation-preview-desktop.png
conversation-preview-phone.png
viewer-desktop.png
viewer-phone.png
guest-refused.png

## Summary The hub side of "file viewers as plugins" (form `viewers-for-pdf-media-and-3d-files_jl74lk`): a generic contract so separate plugins can show file types the explorer can't (pdf, media, 3D, html, code, office, archives, fonts). A viewer is its own full plugin page, picked by the file's extension. It reads the bytes through a new `fetchFile` bridge. A message naming such a file gets an inline preview card under it that opens the same viewer. Guests get viewers the way they get file-explorer (managed, `defaultForGuests`). What a guest can read is still decided by `/api/host-file/raw`. ## Key changes - **V1, pages by file type**: `ui.pages[].fileTypes` (1..32 `^[a-z0-9]{1,10}$`, valid only on an `opens: ["host-file"]` page; written only when set). `pluginOpenHref(…, "host-file", {path})` picks the first active page whose `fileTypes` has the path's extension (`fileExt`: lowercased, `:line` suffix ignored). Otherwise it picks the first untyped `host-file` page; folders always go there. File chips, `/file/*`, a frame's `open()` and preview `href`s all follow this. Every frame's `init` now has `fileOpeners: {ext: route}`, and a typed page is never `openers["host-file"]`. `fileIcon` gains icons for 3D, archive, font and audio files. - **V2, `aiAgentPlugin.fetchFile(url)`** → `Promise<Blob>`: any hub media/file path (`isHubMediaPath`), any content type, ≤ 100 MB (`MAX_FILE_BYTES`), sent with cookies. `mediaUrl` is unchanged. - **V3, `ui.filePreviews[]`** (`{id, label, html, fileTypes, height, vis?}`, ≤ 4 per plugin) in `plugins/manifest.py`. `/api/plugins[/ui]` lists them as `ui.filePreviews` (without `html`), and `/api/plugins/{id}/ui/preview/{previewId}` serves the document. `PluginFilePreviews.tsx` draws them under assistant messages: - files are found the way file chips find them (`messageHostPaths`: inline code, running text, link targets; never inside fenced code), one card per distinct file, at most 4 per message; - the first active plugin per extension wins, and the caller must be able to open the file; - each card mounts once it is visible; init is `{preview, path, name, ext, href, sessionId, conversationId}`; `resize(0)` collapses it; - cards sit behind *Rich widgets* and the preview's own `vis`. - `aiAgentPlugin.navigate(path, {replace: true})`, so a page that forwards a file to its viewer on load doesn't trap Back. - Mock: a `pdf-viewer` page + preview; the kitchen-sink thread names `notes/brief.pdf`. - Docs: `docs/plugins.md` → "File viewers: pages by type, fetchFile, previews (since 2026-11-28)", written for the eight viewer-plugin runs. ## Key decisions - **No CSP / sandbox widening was needed.** On a live hub, a sandboxed frame (`allow-scripts allow-popups allow-popups-to-escape-sandbox`, opaque origin) ran all of these with the flags as they are, so nothing was widened: - `blob:` URLs in `<img>`, `<video>` and `<audio>`; - `FontFace(buffer)` and an `@font-face` from a `blob:` URL; - a `blob:` Worker, including pdf.js's worker built from its CDN source fetched as text; - WebGL2; - a jsDelivr `<script integrity crossorigin>` and an ESM `import()` from jsDelivr. - The first preview **per extension** is chosen in the app, not deduped server-side the way fences are, because one plugin can list several types. - With no generic page active, a viewer still links its own types; `usePluginOpener` now says "has" when any page opens the kind. ## Changelog - Added: plugins can be viewers for file types — a PDF, a 3D model, a video named in a conversation opens in its viewer plugin - Added: a preview card under an agent's message that names a file a viewer plugin previews ## Test notes - Backend: `test_plugin_file_viewers.py` (new: normalisation, refusals, fingerprint unchanged without the keys, descriptors, the preview document, the capability line). Every `test_plugin*.py`, `test_authz_matrix`, `test_hostfiles` and `test_csp` pass (bwrap tmpfs `/data`). No route was added, so `openapi.json`, `src/generated` and `matrix.golden.txt` didn't need regenerating. - Frontend: `fileViewers.test.ts` covers opener resolution, `fileOpeners` and `fetchFile` limits. `PluginFilePreviews.test.tsx` covers detection, the per-message rules and mount-on-visible. Full vitest: 1123 pass; the one failure is the known `FreshContext` CacheExpiredNotice. `tsc` is clean. - Live hub (port 8140, built `frontend/dist`, a throwaway `fileviewer-demo` plugin + file-explorer 1.1.0) driven with puppeteer: - the chip and the card's Open lead to the typed page with `?path=`; - `fetchFile` returned the pdf's bytes; - pdf.js rendered the pdf, and every capability check above passed; - the preview card appeared under the message, desktop and phone; - the explorer sent a `.pdf` row to the viewer, while a README stayed in the explorer and Back skipped the handover; - a guest's `fetchFile` worked inside their project and got **404** outside it. - **Deploy from the Homelab after merge; merge the two together** with ai-agent-plugins `file-viewers-explorer` (file-explorer 1.1.0). Nothing was deployed from the EVOX2. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Screenshots ![conversation-preview-desktop.png](https://git.gabvdl.xyz/attachments/1a6f381f-1c4f-43c4-a689-ca45eac52546) ![conversation-preview-phone.png](https://git.gabvdl.xyz/attachments/0878d80a-4614-4e40-8ba1-1210d8694f56) ![viewer-desktop.png](https://git.gabvdl.xyz/attachments/9612d7df-e6b3-48f1-a9f9-9f43a3da7e5b) ![viewer-phone.png](https://git.gabvdl.xyz/attachments/8869a54d-e36c-474a-a3b6-0b0bb3b5007e) ![guest-refused.png](https://git.gabvdl.xyz/attachments/6620da87-ee7f-4500-9265-c42ee685c280)
gabrielvidal self-assigned this 2026-11-28 19:23:51 +01:00
gabrielvidal added 1 commit 2026-11-28 19:23:51 +01:00
ui.pages[].fileTypes (a viewer page picked by extension; host-file links,
file chips, /file/* and open() follow it; init.fileOpeners), the
aiAgentPlugin.fetchFile bridge (any hub file path, any type, <= 100 MB) and
ui.filePreviews (a card under an agent's message naming a previewable host
file, mounted once visible). navigate(path, {replace}). Docs: plugins.md →
File viewers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Owner
Merged into `main` as 1b3f5246 by the auto-ship batch https://ai-agent.lab.gabvdl.xyz/conversation/cadefe6f-0940-4688-bc56-1306d9265430.
gabrielvidal closed this pull request 2026-11-28 21:10:25 +01:00

Pull request closed

Please reopen this pull request to perform a merge.
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: gabrielvidal/ai-agent#40