ExAgent.Source (ExAgent v0.3.0)

Copy Markdown View Source

Pure helpers for reasoning about attachment sources.

Infers MIME types from a path, a URL, or raw bytes, and maps a MIME type to the coarse modality providers gate on.

This module knows nothing about providers and makes no decisions about how an attachment is delivered (inline, uploaded, or referenced by URL) - that is the responsibility of each provider's service module.

Summary

Functions

Infers a MIME type from an attachment source.

Maps a MIME type to the modality providers declare support for.

Types

modality()

@type modality() :: :text | :image | :audio | :video | :document

source()

@type source() :: {:path, String.t()} | {:url, String.t()} | {:data, binary()}

Functions

infer_mime(arg)

@spec infer_mime(source()) :: {:ok, String.t()} | {:error, String.t()}

Infers a MIME type from an attachment source.

Paths and URLs are resolved by file extension (query strings and fragments are ignored); raw binaries are resolved by magic bytes. Returns an error naming :mime_type when the type cannot be determined, since passing it explicitly is always the fix.

Examples

iex> ExAgent.Source.infer_mime({:url, "https://cdn.example.com/a.png?sig=x"})
{:ok, "image/png"}

iex> ExAgent.Source.infer_mime({:data, <<0x89, "PNG", 0x0D, 0x0A, 0x1A, 0x0A>>})
{:ok, "image/png"}

iex> ExAgent.Source.infer_mime({:path, "/tmp/mystery"})
{:error, "could not infer :mime_type from path \"/tmp/mystery\"; pass :mime_type explicitly"}

modality(arg1)

@spec modality(String.t()) :: modality()

Maps a MIME type to the modality providers declare support for.

Anything that is not image, audio, or video is treated as a document.

Examples

iex> ExAgent.Source.modality("video/mp4")
:video

iex> ExAgent.Source.modality("text/csv")
:document