Media Boundarydoc/{zh,en}/design/plugins/media-boundary.md

Native Media at the Model Boundary

Status: built 2026-12-03 as slice B of the media harness review of 2026-12-03 (PR #195; findings M-04, M-05, M-11 and M-14; P100, ADR 0018). Verified natively on macOS arm64 only;

Status: built 2026-12-03 as slice B of the media harness review of 2026-12-03 (PR #195; findings M-04, M-05, M-11 and M-14; P100, ADR 0018). Verified natively on macOS arm64 only; Linux and Windows are cross-compiled, not run. HEIC is tested with photos made by macOS sips; AVIF goes through the same ffmpeg path untested. Slice C (finding M-06) added a parsed file's pictures, section 9.

1. What it does

A file reaches a model in one of two forms, decided at the model boundary (prepareParts): natively, when the model declares its kind (an image, a PDF, audio, video), or as the rendering of its parse. Before this slice a native file went byte for byte, and one over 10 MiB (images) or 20 MiB (other files) became an empty "too large to show" note. Now a native file is first made to fit the model:

  • ◆Images (attachments and the media tool's results alike) are turned upright by their EXIF orientation (all eight values), have their metadata left out (EXIF, XMP, GPS, ICC, comments; by encoding them again), are scaled to the model's longest side and byte limit, and are converted to PNG or JPEG when the model does not take their format (HEIC, HEIF, AVIF, TIFF, BMP, WebP when not taken, SVG). An image that needs none of this goes byte for byte as it is, so a screenshot keeps its exact pixels.
  • ◆SVG is drawn to PNG by a native Go rasterizer (pkg/media/svg) and sent with its source text beside it. The read tool, which shows an SVG as the text it is, adds on its first page the file as a visual image part (Part.Visual): a model that sees images gets the drawing beside the text read gave, without the source again; a text-only model gets only that text.
  • ◆A file over the model's size that cannot be made smaller (a PDF, audio, video) reaches the model as its parsed reading, after a note that names the native limit it exceeded. An image over the size is scaled instead, so a 15 MB photo is seen.
  • ◆Audio in a format the model's API does not take (Ogg/Opus, M4A, FLAC, AIFF, CAF for Chat Completions' input_audio, which takes WAV and MP3) is converted by ffmpeg first; without ffmpeg it reaches the model as its reading, with a note naming what is missing.
  • ◆How each file was shown is in the run context (shown of each attachment in <orq:context>), in the run trace and in orquelo run show.

2. The contract: plugin.MediaLimits

A model plugin declares what it takes beyond its input kinds in ModelInfo.Media (pkg/plugin/medialimits.go). A zero field means the default; the Host has no vendor branch, only these values.

FieldMeaningDefault
image_formatsImage media types sent as they arePNG, JPEG, GIF, WebP
image_max_sideLongest side in pixels2048
image_max_bytesBytes of one image as sent, before base643.75 MiB (5 MiB in base64, the strictest vendor's limit)
audio_formatsAudio media types the API takes; other audio is converted to the first of them ffmpeg makes (WAV, MP3)any audio as it is
file_max_bytesBytes of a PDF, audio or video file sent as it is20 MiB
file_max_pagesPages of a PDF sent as it is (Feishu #423: providers read a page as about 2,000 tokens; 482 pages took 94% of a 1M window). A PDF whose pages the reader cannot count goes as it is only when its size could not hold more (about 1 KiB a page); a larger one goes as its reading (2026-12-05, review #264 H22: it counted as 0 pages and went whole)50, one part of its reading

The openai plugin reports the same names from its configuration (image_formats, image_max_side, image_max_bytes, audio_formats, file_max_bytes, file_max_pages) and defaults audio_formats to audio/wav and audio/mpeg, the two its input_audio form takes.

3. Call chain and owners

text
Runtime (Agent) ── RunContext: FileShower.FilesShown(model, attachments) ──► <orq:context> attachments[].shown
       │
       └─ Caller.Chat ─► prepareParts (orchestrator, internal/host/engine/content.go)
                            └─ show (shown.go): per file part
                                 ├─ a range, or a kind the model does not take, or C5 ─► parsed rendering (parsing.Files)
                                 └─ natives.fit (native.go) ─► imageprep.Preparer.Prepare (pkg/media/imageprep)
                                                              │   └─ svg.Render (pkg/media/svg); Decoder: ffmpeg.Image (HEIC, AVIF)
                                                              ├─ ffmpeg.Audio (audio the model does not take)
                                                              └─ size check (PDF, video, audio)
                                 └─ file.shown fact (once per run, file, model and form) ─► trace item span, run show
  • ◆prepareParts is the orchestrator: it decides, per part, between native and parsed, and records the decision. It holds no vendor knowledge.
  • ◆imageprep is the image provider: orientation, metadata, scaling, encoding, SVG. It knows no artifact or model; its HEIC/AVIF decoder is injected (Decoder), so the package has no dependency on ffmpeg and no new module dependency (only golang.org/x/image).
  • ◆natives keeps what it made, by artifact and limits (at most 128 MiB, oldest out first), because a request is prepared on every step with every file of the conversation; at most two images or recordings are decoded at once. A failure that may pass (ffmpeg missing, a timeout) is not kept.
  • ◆internal/ffmpeg runs ffmpeg for HEIC/AVIF decoding (-frames:v 1, so the photo is turned by its irot/imir and not turned again by EXIF) and audio conversion (mono, 16 kHz WAV or 24 kHz 64 kb/s MP3, metadata left out).

4. Images in detail

  1. SVG: drawn (section 5), then encoded as PNG; its source goes beside the picture as <orq:file … shown="source">, at most media.inline_bytes of it.
  2. HEIC, HEIF, AVIF: decoded by the injected decoder to an upright PNG; without it (no ffmpeg) the image is media.unavailable and falls back to its reading.
  3. Others: the header is read; a header that claims more pixels than its bytes could hold, or more than 100 megapixels, is media.too_large, refused before any pixel is held. A file the model takes, upright (orientation 1 or none), within side and bytes and without metadata goes as it is. Otherwise it is decoded (an animated GIF's first frame), scaled with a bilinear filter to the longest side, turned upright, and encoded: a JPEG, HEIC or AVIF photo as JPEG (quality 85), anything else as PNG; a PNG over the byte limit is tried as JPEG on white, and a JPEG still over it is made smaller by a quarter at a time. A model that names only one of PNG and JPEG gets that one.
  4. The result is native (its own format and size, perhaps turned or cleaned), native-scaled (smaller) or native-converted (another format). The model gets a short note of what was done beside the image, as in [image "photo.jpg" (artifact af_…) was prepared for you: turned upright (EXIF orientation 6); metadata removed; scaled from 4000×3000 to 1536×2048], then the file's properties as before.

5. The SVG rasterizer

pkg/media/svg draws, on white, what icons, diagrams and charts mostly use: path (every command, arcs as curves), rect (rounded too), circle, ellipse, line, polyline, polygon, g, nested svg, use and symbol; fills and strokes (round joins, butt caps) with colours by name, hex, rgb()/rgba() and currentColor, opacities, a gradient as the mean of its stops; transform (matrix, translate, scale, rotate, skew), viewBox with xMidYMid meet, units and percentages; simple CSS (<style> rules by element or class, style attributes); text and tspan in the Go fonts, anchored as text-anchor says, drawn level. Left out: filters, masks, clipping, patterns, markers, dashes, embedded images, rotated text, and even-odd fills (drawn as non-zero). Letters the Go fonts lack (Chinese, Japanese) show as boxes; the source text beside the picture carries them. Bounds: 8 MiB of file, 50,000 elements, 2 million outline points, use eight deep; nothing is fetched. A drawing's longest side is between 512 pixels and the model's side.

6. Oversized files and audio

  • ◆A PDF, audio or video file larger than file_max_bytes reaches the model as its parsed reading (outline and first part for a large one) after [file "report.pdf" (…) could not be given to you as it is: media.too_large: 26215434 bytes, over the 20971520 bytes this model takes as they are. Its reading follows instead.]. A PDF of more than file_max_pages pages does the same, whatever its size: … media.too_large: 482 pages, over the 50 pages this model takes as they are. ….
  • ◆Audio the model does not take is converted to the first format of audio_formats that ffmpeg makes; a conversion larger than file_max_bytes, or no ffmpeg, gives the reading with the note (… media.unavailable: ffmpeg is not installed …).
  • ◆The model parser's own call (media.models, purpose media) gets its file natively or not at all, as before: a file that cannot be made to fit is refused there, and the parser falls back to the reading without that model.

7. shown: the context, the trace and run show

The value is one of native, native-scaled, native-converted, parsed, outline (outline and first part) or failed (pkg/plugin.FileShower, pkg/media.ShownParsed and the like).

  • ◆Context. At run start the Agent asks the Host (FilesShown, an optional Caller interface) how each attachment reaches its model and writes it as shown in <orq:context>, e.g. {"name":"photo.jpg","mime":"image/jpeg","size":3815220,"path":"…","shown":"native-scaled"}. The Host decides with the same code as prepareParts, and keeps what it prepared, so the first step finds the images ready and the parses cached. A run that later goes parsed (C5) or falls back to another model keeps the value of its start.
  • ◆Trace. Each file sent records a file.shown fact once per run, file, model and form: {artifact, name, model, shown, detail, ok, dur_ms}. The trace shows it as an item span named by the file, lasting its preparation, with shown, model and detail attributes; failed is an error span.
  • ◆run show lists them: file af_9 "photo.jpg" to kimi: native-scaled (turned upright (EXIF orientation 6); metadata removed; scaled from 4000×3000 to 1536×2048).

8. Verification

Unit tests: all eight orientations (corner colours of a four-colour picture), GPS gone after preparation, a clean image byte for byte, scaling to side and bytes, BMP, TIFF and WebP converted, HEIC through the decoder without turning twice, SVG pixel checks (size, shapes, paths, holes, transforms, use, joins, opacity, CSS, gradients, text), pixel bombs refused, ffmpeg's Opus → WAV and MP3, the trace span and run show, shown in <orq:context>. One engine test drives the model boundary with a model recording what it got (macOS arm64, ffmpeg 9.1):

FileSent asshown
12-megapixel JPEG stored sideways with GPSJPEG 1536×2048, 54 KB, upright, no EXIFnative-scaled
15 MB noise JPEG, 3200×2600JPEG 2048×1664, 1.7 MBnative-scaled
SVGPNG 512×256 and its sourcenative-converted
25 MB PDF to a PDF modelits reading, with the noteparsed
Opus voice message to a WAV/MP3 modelWAV, 32 KBnative-converted
HEIC 640×480 (sips)JPEG 640×480native-converted

Without ffmpeg the voice message reaches the model as its reading with the note naming ffmpeg. Not verified: AVIF photos, live vendor calls with prepared images (the owner's veruqelo cases run on Kimi), Linux and Windows.

9. A parsed file's pictures

Built 2026-12-03 as slice C of the same review (finding M-06). A model that declares image gets, after the rendering of a file given as its parse, the pictures of the figures and frames that rendering shows; a text-only model gets the text alone, as before.

  • ◆Where pictures come from. A parser marks a figure or a video frame whose picture it can give again with media.Block.Ref, and implements plugin.Figurer. docx and pptx: pictures embedded in word/media or ppt/media and drawn as pictures (not a background or a shape filled with one), in the text as [figure N: description] where they are drawn, those in a table after it; EMF and WMF are named without a picture. pdf: image XObjects a page draws over at least 2% of it, at least 48 pixels a side, not stencil masks, at most 16 a page, after the page's text; none on a scanned page, which OCR reads. video: each frame of the timeline, taken again by its time with ffmpeg, since the frames read for their text are not kept. Not built: regions a PDF page draws as vector paths (a chart drawn with lines), which need a page renderer; pictures of XLSX, ODF, EPUB and web pages.
  • ◆The rendering lists them (media.RenderView, media.Figure): only those of the blocks it shows, numbered as it numbers them (figure N, frame N), in each view.
  • ◆The boundary attaches them (internal/host/engine/figures.go). parsed has parsing.Files.Figure ask the parser that read the file for the picture, under the parse deadline (Service.Figure), prepares it like any image (imageprep: upright, metadata left out, scaled, converted) and keeps it with the other prepared files. At most media.MaxPictures (4) pictures of parsed files go with one model step, the latest message's files first. A note follows the file's text, e.g. [Pictures of "report.docx" (artifact af_…): figure 1, figure 2 follow as images, in this order. left out, as at most 4 pictures go with a step: figure 5 (page 2), figure 6 (page 2); see them with the media tool and this file's id, e.g. pages="2".]; a frame left out points to time, and a picture that could not be given is named with why.
  • ◆Pages for pictures. A document without pages that holds more than four pictures is numbered in pages of at most four (media.Paginate), so each can be asked for with pages. A page of a PDF or a slide with more than four pictures gives its first four when read alone.
  • ◆No pictures for a text-only model, the model parser's own call, or a step retried with every file parsed after a provider refused a file (C5). A refused picture of a parsed file counts as a refused file: the step is prepared afresh from its original request, with every file parsed, so no text goes twice.
  • ◆Too small to show. A picture under media.MinFigureSide (48 pixels) on a side as sent is not attached, an icon or a 1×1 placeholder that shows nothing and that some providers refuse; the note names it with its size.
  • ◆The trace. The file's file.shown fact lists each picture with its shown (native, native-scaled, native-converted), those left out and those that failed, e.g. pictures: figure 1 native, figure 2 native-scaled, left out: figure 5 (page 2). The file's own shown stays parsed or outline.

Verified by unit tests and with a built binary and a fake model that sees images (a DOCX with two pictures, a short video); not with a live vendor.