ExAgent.Media (ExAgent v3.0.0)

View Source

Buffered media operations backed by stock ReqLLM. Each result carries qualified usage. Audio and OCR inputs are tagged bytes with a MIME type. Text input is UTF-8. Host-provided media options and credentials use :req_llm.

Stability — Candidate.

All calls accept :max_input_bytes (1 MiB), :max_output_bytes (4 MiB), :max_items (64 segments, images or pages), and :timeout (30,000 ms). Input limits apply before dispatch. Output limits apply to decoded results and to their UTF-8 representation in a tool result store. Provider responses can allocate memory before stock ReqLLM hands them to ExAgent.

Use :rate_limiter to share RPM and concurrency with other participants. A matching TPM bucket also needs :estimated_tokens, a positive host reservation retained for the quota window. Usage represents separate evidence and never reports the reservation as measured tokens. Media tools use the run's limiter when present. HTTP 429 cools down the selected bucket using a positive provider hint or one second by default.

Use :spend_budget to reserve cents before a stock request. Media tools inherit the run's shared account, which takes precedence over a tool's configured account. Set :price_per_unit_cents to estimate the cost from observed audio input milliseconds, input graphemes, output images or output pages, respectively. An explicit zero rate requires an observed quantity. A charge with no complete price keeps its reservation.

Each operation makes one stock request with retries disabled. Tool execution uses the run's existing permission, deadline, and continuation boundaries. A lost response or HTTP 5xx leaves the effect unconfirmed. Admitted calls emit [:exagent, :media, :request, :start | :stop] events. Pass :observability for native OpenTelemetry or inherit it in tools.

Summary

Functions

Read a media tool's stored JSON result in the originating run's scope. The version 1 envelope has "operation" and "result". Audio and inline image bytes inside the result are Base64. Store TTLs and capacity apply after the reference has entered history.

Generate images from a text prompt. A stock image URL is returned as a URL.

Extract markdown and indexed text pages from a tagged PDF or image payload.

Prepare user prompt parts explicitly for a target without audio input. Inline audio file parts become transcript text in the same positions. A capable target keeps its audio parts. The host owns the returned usage and passes the prepared prompt to the run. The run and its resumes do not execute the preprocessor.

Synthesize speech using the model and voice selected by the host. Buffered RIFF/WAVE results with open length fields are finalized before returning the audio.

Build only the media tools supplied as options. Each operation takes a keyword list with :model and the same options as the direct call. Tool names are media_transcribe, media_speak, media_generate_image and media_ocr. Set :requires_approval on the collection to require the ordinary tool approval flow for every operation.

Transcribe a tagged audio payload, for example Media.transcribe("openai:whisper-1", {:binary, bytes, "audio/wav"}).

Types

file()

@type file() :: {:binary | :base64, binary(), String.t()}

operation()

@type operation() :: :transcribe | :speak | :generate_image | :ocr

prompt()

Functions

fetch(store, run_id, ref)

@spec fetch(
  ExAgent.Tool.ResultStore.t(),
  ExAgent.Tool.ResultStore.scope(),
  ExAgent.Tool.ResultStore.ref_id()
) :: {:ok, map()} | {:error, :expired | :unavailable | :invalid_media_reference}

Read a media tool's stored JSON result in the originating run's scope. The version 1 envelope has "operation" and "result". Audio and inline image bytes inside the result are Base64. Store TTLs and capacity apply after the reference has entered history.

generate_image(model, prompt, opts \\ [])

@spec generate_image(ReqLLM.model_input(), String.t(), keyword()) ::
  {:ok, ExAgent.Media.Images.t()} | {:error, ExAgent.Media.Error.t()}

Generate images from a text prompt. A stock image URL is returned as a URL.

ocr(model, document, opts \\ [])

@spec ocr(ReqLLM.model_input(), file(), keyword()) ::
  {:ok, ExAgent.Media.OCR.t()} | {:error, ExAgent.Media.Error.t()}

Extract markdown and indexed text pages from a tagged PDF or image payload.

preprocess_audio(target, prompt, model, opts \\ [])

@spec preprocess_audio(
  ExAgent.Model.model(),
  prompt(),
  ReqLLM.model_input(),
  keyword()
) ::
  {:ok, prompt(), ExAgent.Message.Usage.t()} | {:error, ExAgent.Media.Error.t()}

Prepare user prompt parts explicitly for a target without audio input. Inline audio file parts become transcript text in the same positions. A capable target keeps its audio parts. The host owns the returned usage and passes the prepared prompt to the run. The run and its resumes do not execute the preprocessor.

The options are those of transcribe/3 plus :max_clips (4). All clips share the call deadline and combined transcript output limit. If a later clip fails, its error usage also contains prior measurements.

speak(model, text, opts \\ [])

@spec speak(ReqLLM.model_input(), String.t(), keyword()) ::
  {:ok, ExAgent.Media.Speech.t()} | {:error, ExAgent.Media.Error.t()}

Synthesize speech using the model and voice selected by the host. Buffered RIFF/WAVE results with open length fields are finalized before returning the audio.

tools(opts)

@spec tools(keyword()) :: [ExAgent.Tool.t()]

Build only the media tools supplied as options. Each operation takes a keyword list with :model and the same options as the direct call. Tool names are media_transcribe, media_speak, media_generate_image and media_ocr. Set :requires_approval on the collection to require the ordinary tool approval flow for every operation.

Supply :result_store as a Tool.ResultStore tuple. Transcription and OCR also require :resolve_input, a host function receiving the run context and the opaque input reference. It returns {:ok, tagged_bytes} or an error. Model arguments cannot pick a file path, URL, model, or provider options. The host resolver authorizes access to each input.

A successful tool return contains "ref" and "bytes". Fetch the stored result with fetch/3 and the originating run ID. The ordinary tool ledger owns accounting. For durable runs, keep tool configuration and the result store bound to the same application continuation identity.

transcribe(model, audio, opts \\ [])

@spec transcribe(ReqLLM.model_input(), file(), keyword()) ::
  {:ok, ExAgent.Media.Transcription.t()} | {:error, ExAgent.Media.Error.t()}

Transcribe a tagged audio payload, for example Media.transcribe("openai:whisper-1", {:binary, bytes, "audio/wav"}).