module

MJ::Arcana::VoiceService

The voice service — ported out of the arcana server.

Arcana supplies the bus; it should not also own every provider integration, or it becomes a monolith with a bus inside it. The TTS provider code stays in arcana-ai (a library); what moves here is the EXPOSURE of it as a service.

Ported with one change worth having: arcana only ever exposed OpenAI, while arcana-ai has had a complete ElevenLabs provider sitting unused. Both are offered here, chosen per call.

Constants

BUSY_BACKOFF = [0.5, 1.0, 2.0, 4.0, 8.0].map(&.seconds)
ELEVENLABS_MODELS = (::Arcana::AI::TTS::ElevenLabs::MODELS + ["eleven_v3"]).uniq

arcana-ai's list stops at v2.5. Any model name passes through to ElevenLabs as given, but callers read this list to see what's there, so v3 has to be on it.

MIN_LEFT = 3.seconds

The fastest line measured came back in 0.6 s (Flash); a v3 sentence takes 3-5 s.

MUSIC_ENDPOINT = "https://api.elevenlabs.io/v1/music"

--- music ---------------------------------------------------------------------------

Eleven Music, called directly: arcana-ai has no music provider. It sings, but it takes no voice id, so a song is in a voice's manner, not its voice.

MUSIC_FORMAT = "mp3_44100_128"
MUSIC_MODEL = "music_v1"
MUSIC_REFUSALS = ["bad_prompt", "bad_composition_plan"]

A plan or prompt it won't take comes back as one of these, often with a suggestion.

MUSIC_SCHEMA = JSON.parse("{\n \"type\":\"object\",\n \"description\":\"A song from Eleven Music, sung, from a composition plan or a prompt. It takes no voice id. Returns base64 audio unless output_path is given. Needs ELEVENLABS_API_KEY.\",\n \"properties\":{\n \"composition_plan\":{\"type\":\"object\",\"description\":\"ElevenLabs' plan: positive_global_styles, negative_global_styles, and sections, each {section_name, positive_local_styles, negative_local_styles, duration_ms, lines}. The sections set the length.\"},\n \"prompt\":{\"type\":\"string\",\"description\":\"A text prompt instead of a plan.\"},\n \"music_length_ms\":{\"type\":\"integer\",\"description\":\"With a prompt only: the length in ms.\"},\n \"model\":{\"type\":\"string\",\"description\":\"Default music_v1.\"},\n \"format\":{\"type\":\"string\",\"description\":\"ElevenLabs' output_format, default mp3_44100_128.\"},\n \"output_path\":{\"type\":\"string\",\"description\":\"Write the audio 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 song, default 600. A song still queued when less than 3 s of it is left comes back expired, unmade and unbilled.\"}\n }\n }")
MUSIC_WAIT = 600.seconds
OPENAI_VOICES = ["alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"] of ::String
READ_TIMEOUT = 110.seconds

How long to wait for the provider to send something. OpenAI's gpt-4o-mini-tts sends its events only once the clip is done, which took 65-81 s for a 4-5 s clip on 2026-10-21, past arcana-ai's old fixed 60 s. 110 s answers inside the 120 s Overlore waits, rather than after it has given up.

REFUSAL_MARKERS = ["content_policy", "content policy", "moderation", "rejected", "flagged", "safety"]
SPEAK_SCHEMA = JSON.parse("{\n \"type\":\"object\",\n \"required\":[\"text\"],\n \"description\":\"Synthesize speech. Returns base64 audio unless output_path is given — bus services are filesystem-isolated, so base64 is the portable answer.\",\n \"properties\":{\n \"text\":{\"type\":\"string\",\"description\":\"What to say.\"},\n \"provider\":{\"type\":\"string\",\"enum\":[\"openai\",\"elevenlabs\"],\"description\":\"Default openai. elevenlabs needs ELEVENLABS_API_KEY.\"},\n \"voice\":{\"type\":\"string\",\"description\":\"openai: alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, verse. elevenlabs: a voice id.\"},\n \"model\":{\"type\":\"string\",\"description\":\"Override the provider's default model.\"},\n \"format\":{\"type\":\"string\",\"description\":\"mp3, wav, aac, flac, opus, pcm (default opus).\"},\n \"instructions\":{\"type\":\"string\",\"description\":\"Style/persona direction. OpenAI only.\"},\n \"speed\":{\"type\":\"number\",\"description\":\"OpenAI 0.25-4.0; ElevenLabs 0.7-1.2, which eleven_v3 ignores.\"},\n \"previous_text\":{\"type\":\"string\",\"description\":\"Preceding line, for prosody continuity. ElevenLabs only, and not eleven_v3, which refuses it with a 400.\"},\n \"next_text\":{\"type\":\"string\",\"description\":\"Following line, for prosody continuity. ElevenLabs only.\"},\n \"output_path\":{\"type\":\"string\",\"description\":\"Write the audio 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 answer, default 120. A line still queued when less than 3 s of it is left comes back expired, unmade and unbilled.\"}\n }\n }")
SPEAK_WAIT = 120.seconds

--- the queue ------------------------------------------------------------------------

A request waits in its lane until there's room, so a burst queues here instead of being refused there. Two rules keep the queue honest.

  1. What nobody waits for isn't made. The caller gives up after its own wait (Overlore: 120 s for a line), but its request stays queued, and once made it is billed all the same. So a request whose caller has less than MIN_LEFT of its wait left when its turn comes is answered expired, unmade and unbilled. The wait runs from the envelope's timestamp, the sender's clock: fine on one host.
  2. ElevenLabs at its limit anyway is waited out. The lanes keep mj under the plan's limit, so a 429 concurrent_limit_exceeded means someone else on the account (the website, another program) has lines running. ElevenLabs refuses those at once, unbilled; mj tries again after a pause while the caller still waits, then answers busy.
VOICES_SCHEMA = JSON.parse("{\"type\":\"object\",\"properties\":{}}")

Class methods

at_limit?(ex : ::Arcana::AI::APIError) : Bool
Source
available
Source
busy(provider : String, reason : String) : JSON::Any
Source
deadline(data : JSON::Any, sent_at : Time, default : Time::Span) : Time
Source
expired(provider : String, sent_at : Time, deadline : Time) : JSON::Any | Nil
Source
handle_music(data : JSON::Any, sent_at : Time = Time.utc) : JSON::Any
Source
handle_speak(data : JSON::Any, sent_at : Time = Time.utc) : JSON::Any
Source
handle_voices
Source
refusal?(message : String) : Bool
Source
register(ts : ::Arcana::Toolset, limits = VoiceLanes.limits(nil))
Source
with_room(deadline : Time, &)

Runs the provider call, trying again while ElevenLabs is at its limit and the caller still has time; raises Busy when it doesn't.

Source

Nested types