Media Generation Plugin
Lets users ask for images (and, in a later release, short videos) in chat: diagrams, mock-ups, illustrations. Codumentor orchestrates; the external tool generates. Codumentor turns the conversation into a prompt, calls the service the administrator chose, stores the result in the conversation's workspace and records who asked for what. It does not ship or host a generative model.
Output quality, content policy and the rights to the output belong to the selected provider and to your contract with it. Codumentor does not grant or change them. Every result is labelled with the provider and model that produced it.
Enterprise edition only. Load user_secrets (per-user keys) and workspace_files (result delivery) alongside it.
Status
This release generates and edits images: the generate_image and edit_image tools, the mask editor on the result card, the openai_images adapter (OpenAI Images API or a compatible gateway), technical-user keys and per-user keys (byo_key), the result card with its provenance label, audit records, per-user rate limits, provider probing with effective capabilities, budgets with media spend reported next to LLM spend, and the settings panel. Video follows in a later release.
With no providers configured, the plugin registers no tools and no UI.
Configuration
plugins:
- module: codumentor.plugins.media_generation
class: MediaGenerationPlugin
args:
providers:
corp-images: # id shown to users and admins
adapter: fake # registry key of the provider adapter
mode: technical_user # or byo_key
api_key: ${env:IMAGES_API_KEY} # a reference, never a pasted key
base_url: https://gateway.corp.example/v1 # optional proxy / regional endpoint
models: [fake-image-1]
allow_operations: [image.generate, image.edit] # narrows, never widens
allowed_egress: [gateway.corp.example]
limits: { per_user_per_hour: 20, max_count: 4 }
pricing: # required per model when budgets are set
currency: USD
per_image: { fake-image-1: 0.04 }
per_size: { fake-image-1: { 1024x1024: 0.08 } } # optional overrides
args: {} # adapter-specific options
default_provider: { image: corp-images }
probe_ttl_seconds: 600
budgets:
per_user_daily: { amount: 5.00, currency: USD }
instance_monthly: { amount: 500.00, currency: USD }
retention_days: 90
store_prompts_in_audit: false
target_agents: main
| Key | Default | Meaning |
|---|---|---|
enabled | true | Turn the plugin off without removing its config. |
providers | {} | Named provider entries. Empty means the plugin contributes nothing. |
providers.<id>.adapter | required | Which adapter talks to the service. Available: openai_images, fake. |
providers.<id>.mode | technical_user | technical_user: one operator key shared by every user. byo_key: each user's own key only (see Keys); api_key is never used for it. |
providers.<id>.api_key | none | ${env:VAR}. |
providers.<id>.models | required | Models users may select (at least one). |
providers.<id>.allow_operations | all the adapter declares | Any of image.generate, image.edit, image.variation, video.generate, video.from_image. |
providers.<id>.allowed_egress | [] | Hosts besides base_url the adapter may download results from. |
providers.<id>.limits | none | per_user_per_hour, per_provider_per_hour, max_count, max_video_seconds. |
providers.<id>.pricing | none | Price table: currency, per_image (model to price per image), optional per_size (model to size to price). The cost when the provider reports none. Required for every model when budgets are set, and its currency must match theirs. |
default_provider | none | Provider id per media kind (image, video); must name a configured provider. |
budgets | none | per_user_daily and instance_monthly spending caps (amount, currency; both in one currency). |
probe_ttl_seconds | 600 | How long a provider probe is trusted before it is re-run (min 10). |
retention_days | unset | Reserved; the plugin does not run an output-retention sweep. Files follow workspace retention (by default, 30 days of workspace inactivity for isolated workspaces with .workspace_meta). |
store_prompts_in_audit | false | Audit records carry a prompt hash; set true to store the full prompt. |
inputs | {max_bytes: 20000000, max_pixels: 40000000} | Caps on an edit's source image and mask: file size in bytes (min 1024) and width × height. |
output_dir | media | Workspace folder generated files are written to (relative, no ..). |
target_agents | main | Which agents get the generation tools. An unrecognized value falls back to main. |
Unknown keys are rejected, including inside a provider entry and its args.
Keys
Keys are referenced, never pasted into YAML: ${env:...} for a technical-user key. Keys never appear in prompts, tool results, logs, audit records or probe output.
Per-user keys (byo_key). Load User Secrets. Each user sets their own key in the plugin's settings under "Your API keys" (one row per byo_key provider: key set or not, a password field, Test, Clear). It is stored encrypted for that user under media_generation/<provider id>/api_key and never shown again. A byo_key provider runs on that key only: without one, the provider offers that user nothing (the agent gets no tool for it, and a call says where to set a key), and the operator api_key is never used. Test probes with the user's key; the result is kept for that user only and decides their effective capabilities. If the provider rejects the key, the user is told to update it in "Your API keys", and the provider offers them nothing until the key changes. Admins cannot probe a byo_key provider (it has no shared key).
What leaves the instance
The prompt, any source images and the requested parameters go to the provider at base_url. Point base_url at a regional or self-hosted endpoint to keep data in a region.
| Adapter | Sent to base_url | Downloaded from | Not sent |
|---|---|---|---|
openai_images | prompt, model, size, count, quality / output_format if set; for an edit the source image and mask (metadata stripped, see Editing); the key in the Authorization header | result URLs, only from the base_url host or an allowed_egress host | conversation history, workspace files, user identity |
fake | nothing (runs in-process) | nothing | everything |
The OpenAI Images API has no per-request "do not train" or retention option. Data controls are set on the OpenAI organisation or in your gateway's contract.
Probes and status
Each provider is probed (a cheap authenticated call, for openai_images GET <base_url>/models) at startup in the background, after a config reload that changes it, from the settings panel's Test button, and when the last result is older than probe_ttl_seconds. A provider's status is:
| Status | Meaning |
|---|---|
ok | The last probe succeeded; its operations are offered. |
degraded | The probe failed (service down, quota, none of the configured models offered by the key, ...). Nothing is offered until a probe succeeds. |
unauthenticated | No usable key, or the service rejected it. Nothing is offered. |
pending | Not probed yet (the first request waits for the probe). |
What a user is offered is one computation, the effective capabilities: what the adapter declares for the model, narrowed by allow_operations and limits.max_count, by the last probe (status and the models the key offers), and by the user's permissions. It decides whether the agent gets generate_image on that user's turn and what the settings panel and GET /ui/plugins/media_generation/capabilities show. If a provider answers a call with "unsupported", that operation is dropped from its capabilities until an admin presses Test. Probe errors are redacted and shown to admins only; keys never appear.
Budgets and spend
With budgets set, each request reserves its estimated cost before the provider is called. The estimate uses the price table, after count and size have been bounded. A request that would exceed the user's daily or the instance's monthly budget is refused with a plain-text message, and the provider is not called. After the call the reservation is settled with the provider-reported cost when the provider reports one in the budget currency (the OpenAI Images API does not), otherwise with the estimate. A failed call releases it, and a reservation never settled (a crashed worker) stops counting after 15 minutes. A settle that fails is retried once; if it fails again the reservation keeps counting at its estimate and the server log records an error. If the images are generated but cannot be saved to the workspace, the spend stays counted and reported, and the request is audited as media.failed with kind storage. Days and months are UTC.
The ledger is a SQLite file in the plugin data directory (media_generation.sqlite3, WAL), shared by every worker process. If it cannot be read or written, generation is refused (fail closed). Settled USD spend is reported like any tool usage: it counts toward the conversation's cost, the run's ledger and Provider Usage, under provider media_generation:<provider id> with provenance provider_reported_charge or estimate. Usage reporting is USD only: media costs in another currency are budgeted (and shown in the settings panel) but do not appear in Provider Usage or the conversation's cost.
Settings panel
The plugin's settings show one row per provider with its status, effective operations and model, and the budget use (your day; for admins also the instance's month and each provider's share). Admins (admin:config) also see probe errors and a Test button that re-probes the provider. Below them, "Your API keys" lists the byo_key providers for the caller's own key. The same data is served under /ui/plugins/media_generation/: GET capabilities (with byo, the caller's key rows, and default_provider.edit, the provider an edit uses for the caller), POST probe/{provider id} (admin), PUT / DELETE byo/{provider id}/key and POST byo/{provider id}/test (any signed-in user, always on their own key; responses never carry it), POST edit-mask (uploads the painted mask, below) and GET budget.
Using it
The agent gets generate_image(prompt, size?, count?, provider?) on a user's turn when image.generate is effective for that user on some provider (see Probes and status).
- Permission: RBAC permission
media_generation:image, granted by default to theuserandpower_userroles; editing also needsmedia_generation:edit(same roles).media_generation:video(power_useronly) is defined for a later release. Remove the permission from a role to deny it. A local run without a user store is not checked. - Limits:
limits.per_user_per_hourper user and provider,limits.per_provider_per_hourfor everyone, both counted in images (acount: 3request uses 3) and per process. A request is refused when either window has less room thancount; a refused request, or one that fails for lack of a key, uses up nothing.countis capped bylimits.max_countand by the model (DALL·E 3: 1). An unsupportedsizemaps to the nearest supported one, and the result says so. - Retries: a provider rate limit is retried once after its
Retry-After(up to 30 s). Timeouts and 5xx are retried twice with backoff. Auth, quota, content-policy and invalid-request errors are not retried. - Errors: refusals and failures come back as plain text. A content-policy refusal is passed on as-is and not worked around.
- Outputs: each image goes to
<output_dir>/<timestamp>-<prompt-slug>-<id>.pngat the root of the conversation workspace (on a local install,repos_dir, which defaults to the config file's folder). Beside it,<name>.media.jsonrecords provider id, adapter, model, request ids, timestamp, user and conversation, the prompt's SHA-256, size and any content credentials. Withstore_prompts_in_audit: trueit records the full prompt and the provider's revised prompt instead. Only PNG, JPEG, WebP and GIF bytes are stored; anything else is refused. - In chat: the web transcript shows a card with the preview (served from the workspace, never from the provider), the label "Generated by
· " ("Edited by" for an edit), Download, Open in Files, and Edit when you can edit (see below). Surfaces without cards get the file paths in the tool result. There is no Regenerate button: ask the agent again. - Audit:
media.requested,media.completed,media.failedandmedia.denied(reasonpermission,rate,capabilityorbudget) with provider, adapter, model, request id, latency and error kind. The prompt is stored as a SHA-256 unlessstore_prompts_in_auditis set. Keys and images are never stored. A config reload that adds, removes or changes a provider recordsmedia.provider_changedwith the provider id, the change and the changed field names (never values), and actorconfig_reload(the reload does not say which admin made it).
Editing images
The agent gets edit_image(prompt, source, mask?, size?, count?, provider?) on a user's turn when image.edit is effective for that user on some provider and they have media_generation:edit. source and mask are workspace paths.
- Inputs: a workspace file the user can read: no URLs, no absolute paths, no
.., no symlink out of the workspace. The type comes from the bytes: PNG, JPEG or WebP only. Each file is held toinputs.max_bytesandinputs.max_pixels. Before upload, metadata is removed without re-encoding (the pixels are untouched): for PNG every chunk except the image and colour chunks (tEXt,zTXt,iTXt,eXIf,tIME, ... go), for JPEG theAPP1–APP15segments (EXIF, XMP, IPTC, ...) except an ICC profile and an Adobe colour segment, comments and anything after the image end (a JFIF header stays), for WebP theEXIFandXMPchunks. The source file in the workspace is not changed. The result says which metadata was removed. - Mask: a PNG of the source's size, transparent where the image should change. If the provider cannot take a mask, a request with one is refused; without a mask the provider edits the whole image.
- Provider: editing needs
image.editon the provider used now, whoever generated the source. - Outputs: like generated images; the sidecar also records
operation: image.edit,source_pathsandmask_path. Budgets, rate limits, retries and audit apply as for generation. - Edit on the card: shown only when
image.editis effective for you on the provider an edit would use; otherwise the card has no editor at all. Edit opens a mask editor under the image: Brush paints the area to change, Eraser takes paint back, a size slider, Clear, and an instruction. Send edit request starts a normal chat turn without replacing your composer draft. The agent receives the selected source, your instruction and the painted mask path, if any. It can gather context or ask a question before callingedit_image. The authenticatedPOST /ui/plugins/media_generation/edit-maskonly validates and saves a painted mask as<output_dir>/masks/<timestamp>-mask-<id>.png; it does not send your instruction to the provider. The result appears as a new card outside the Focus mode work fold.
The openai_images adapter
providers:
corp-images:
adapter: openai_images
api_key: ${env:OPENAI_IMAGES_API_KEY}
base_url: https://api.openai.com/v1 # default; or your gateway
models: [gpt-image-1] # or dall-e-3, dall-e-2
allowed_egress: [] # extra hosts result URLs may use
limits: { per_user_per_hour: 20 }
args: { quality: high, timeout_s: 120 }
Calls POST <base_url>/images/generations, and for an edit POST <base_url>/images/edits (multipart: the source as image[] for gpt-image models, image for DALL·E 2, and the optional mask). Probe: GET <base_url>/models. Results come back as base64 or as a URL. A URL is downloaded server-side, only over https (http only when base_url is http) and only from the base_url host or an allowed_egress host, without following redirects. The key is sent only when scheme, host and port all match base_url; allowed_egress hosts never get it. A URL on any other host fails with an error naming it. Error messages are redacted (keys, key-shaped strings, data URLs) before they reach logs, audit or chat.
| Arg | Default | Meaning |
|---|---|---|
timeout_s | 120 | Per-request timeout (max 600). |
sizes | per model | Override the offered sizes (e.g. for a gateway model). |
quality | none | Sent as quality (low/medium/high, or hd for DALL·E 3). |
output_format | png | gpt-image models: png, jpeg or webp. |
max_download_bytes | 50 MiB | Cap on one downloaded result. |
image.generate on every model; image.edit with mask on gpt-image models and DALL·E 2, not on DALL·E 3.
The fake provider
A deterministic provider for tests and demos. It returns solid-colour PNGs (same request, same image) and supports image.generate and image.edit. Its args:
| Arg | Default | Meaning |
|---|---|---|
operations | [image.generate, image.edit] | Declared operations; drop image.edit to see the no-edit behaviour. |
models | [fake-image-1] | Accepted models. |
sizes | [256x256, 512x512, 1024x1024] | Accepted sizes. |
max_count | 4 | Images per request. |
supports_mask | true | Whether image.edit takes a mask; false shows the mask refusal. |
fail_with | none | Make every call fail with this error kind (auth, quota, rate_limited, content_policy, invalid_request, unavailable, timeout, unsupported). |
fail_probe | true | Whether fail_with also fails the probe; false makes only calls fail. |
retry_after | none | Seconds reported with a rate_limited failure. |
cost_per_image / currency | 0.0 / USD | Reported cost. |
report_cost | true | false reports no cost, like an API without prices (the price table applies). |
See also
- User Secrets
- Workspace Files
- Design:
doc/core-dev/design/media-generation.md