module

MJ::Arcana::CameraService

The camera service: pick a camera, hand it a prompt and a reference, get a picture.

The point is that the caller supplies intent and nothing else. Geometry rules, per-model parameter spelling, which models silently discard a reference, and what a moderation refusal looks like all live in the registry rather than in every caller.

Constants

CAMERAS_SCHEMA = JSON.parse("{\"type\":\"object\",\"properties\":{}}")
OVERLOAD_STATUSES = [408, 429, 500, 502, 503, 504]

The provider's own refusal text is long, carries a support URL and a request id, and buries the one part a caller can act on. Pull the violated category out when OpenAI names it — a caller retrying automatically needs to know whether it tripped self-harm or violence, not read prose. A third outcome, distinct from both success and refusal: Runware's queue beat us.

Runware enforces no hard rate limit — it runs a shared queue, so heavy traffic shows up as latency, and capacity exhaustion arrives as 429 or, as measured here, 504. The client already retries these with backoff; this is what is left when the retries are used up. It is unbilled, it is transient, and the caller's move is to wait and try again — which is a different instruction from a refusal (reword it) and from an error (stop).

REFUSAL_MARKERS = ["invalidProviderContent", "content moderation", "Responsible AI", "flagged and rejected", "could not generate the image", "rejected by the safety system", "safety_violations", "content_policy_violation", "safety system"]

A provider's content filter refusing is NOT an error: it is unbilled, it is probabilistic (Nano refuses the same prompt it accepted minutes earlier), and the caller's right response is a retry or a tamer prompt — not a failure. It gets its own outcome so callers can tell the two apart. Each provider words it differently, and the error CODE is no help: OpenAI returns the same providerBadRequest for a refusal as for a bad parameter, so matching on the code would misfile real errors as refusals. Match the safety language instead. These were all collected from live refusals; don't add one speculatively.

SHOOT_SCHEMA = JSON.parse("{\n \"type\":\"object\",\n \"required\":[\"prompt\"],\n \"description\":\"Draw a picture with the named camera. If a reference image is supplied the subject is carried into a NEW pose rather than reproduced — that is what the reference is for. Returns base64 unless output_path is given; bus services do not share a filesystem, so base64 is the portable answer.\",\n \"properties\":{\n \"camera\":{\"type\":\"string\",\"description\":\"Camera id — call `cameras` for the list. Default 'klein' (cheapest that holds a character).\"},\n \"prompt\":{\"type\":\"string\",\"description\":\"What to draw. If you pass a reference, say 'in the same style' rather than describing the style: an over-specified style fights the reference image.\"},\n \"reference_base64\":{\"type\":\"string\",\"description\":\"One reference image as base64. The subject to carry into the new picture.\"},\n \"references_base64\":{\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"SEVERAL references, base64. Order is preserved exactly as given; whether providers weight the first most heavily is NOT verified here, so do not rely on it. Use for a recurring character plus the scene to edit, or a room plus its neighbours. Measured: klein, kontext, dflare, dnano and dlite all accept at least 4.\"},\n \"reference_url\":{\"type\":\"string\",\"description\":\"One reference fetched by URL (http/https) — keeps image bytes off the bus entirely. A loopback URL like http://127.0.0.1:PORT/media/KEY works because the service fetches it here; it is NOT handed to the provider, which could not reach it.\"},\n \"references_url\":{\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"SEVERAL references by URL, order preserved. Same fetching as reference_url. Max 32MB each, http/https only, up to 2 redirects.\"},\n \"width\":{\"type\":\"integer\",\"description\":\"Desired width; snapped to the camera's nearest supported aspect. Default 1024.\"},\n \"height\":{\"type\":\"integer\",\"description\":\"Desired height; snapped. Default 1024.\"},\n \"format\":{\"type\":\"string\",\"enum\":[\"webp\",\"png\",\"jpg\",\"xcf\"],\"description\":\"Default webp: ~26x smaller on the wire than png for the same picture, and unlike jpg it keeps hard ink edges clean. Note Runware returns LOSSY webp with NO alpha channel (verified: a VP8 chunk, no VP8X) — it is the right default for serve-and-display, but do not plan on transparency or re-key a webp. Use png for lossless and for anything with an alpha channel; jpg only for photographic subjects (its ringing lands on the black outlines comic art is made of). xcf is GIMP's TILED format — far bigger, but two revisions of the same picture share ~98% of their chunks under content-defined chunking, so it is the right choice for art that will be edited repeatedly and stored in a deduplicating store. Adds ~1.4s for the GIMP conversion.\"},\n \"view\":{\"description\":\"Where the camera stands, as a unit vector {x,y,z}, or the readable form {side:'front'|'back',yaw:-1..1,pitch:-1..1} (quarter turns). Rendered into prompt words — these models take a prompt, not a camera matrix, so this is AS ASKED and not exact. Echoed back.\"},\n \"view_roll\":{\"type\":\"number\",\"description\":\"Image turned in its own plane, quarter turns, -2..2.\"},\n \"light\":{\"description\":\"Where the light comes FROM, in the picture's frame, same shapes as `view`. Also as-asked.\"},\n \"output_path\":{\"type\":\"string\",\"description\":\"Write the image here instead of returning base64: a new file inside ~/.local/share/mj/out (a bare name goes there). Never overwrites.\"},\n \"wait_seconds\":{\"type\":\"number\",\"description\":\"How long you will wait for the picture. A shot whose turn comes with less than the camera's typical time left comes back expired, unmade and unbilled, and no retry starts that couldn't finish in time. Without it, a shot waits as long as it takes.\"}\n }\n }")

Class methods

expired(cam : Camera, sent_at : Time, start_by : Time | Nil) : JSON::Any | Nil
Source
handle_cameras
Source
handle_shoot(transports : Hash(String, Transport), data : JSON::Any, sent_at : Time = Time.utc) : JSON::Any
Source
overloaded?(message : String) : Bool
Source
refusal?(message : String) : Bool
Source
refusal_categories(message : String) : Array(String)
Source
register(ts : ::Arcana::Toolset, rw : RunwareClient)

Transports are passed as a map so a camera is served by whichever one it names. Today there is exactly one; see transport.cr for why recording that still matters.

Source
register(ts : ::Arcana::Toolset, transports : Hash(String, Transport))
Source