Octoroom.Agent.Worker (octoroom_agents v0.1.0)

Copy Markdown View Source

Runs an agent as an Octoroom worker: one process per agent, hearing any number of people. Usually started by Octoroom.Agent.agent/3 from an Octoroom.Agent module:

def workers(%{participants: []}), do: []

def workers(room) do
  [agent("assistant", MyApp.Assistant, hears: Enum.map(room.participants, &mic/1))]
end

It can also be added with worker/3 and the args below:

worker("assistant", Octoroom.Agent.Worker,
  inputs: Enum.map(room.participants, &mic/1),
  outputs: ["assistant"],
  args: %{name: "Assistant", instructions: "You help a team on a call. Be brief."}
)

Many speakers, one conversation

Each input gets its own front end, so each person has their own VAD and turn detection and nothing is mixed. The speaker is known from the input a turn came in on, not from the voice, so it assumes one person per mic. Their finished turns go into one back end, labelled with the speaker's name: the LLM sees [Priya] can you add Acme to the sheet. People joining and leaving add and drop front ends without restarting the agent. Two pipelines (see Octoroom.Agent.Pipeline.Spec.default/1):

  • cascaded (default): each front end streams to speech-to-text, and turns arrive as text.
  • half-cascaded (Spec.default(stt: false)): each front end records whole turns as audio (Smart Turn decides when a turn ends), and the LLM hears them, after a [speaker] label. A transcriber turns each into text in the background, for the history and the transcript.

Deciding to answer

Every turn goes into the conversation. Whether it also starts a reply depends on :respond:

  • :always - every turn. The default with one input.
  • :addressed - turns that say the agent's :name (as a word, any case; text turns only), and follow-ups: a turn from the person it last answered within :follow_up_ms after it stops talking, or before that reply is done. With the decision gate, the model can still turn a follow-up down (see below). The default with several inputs, or with the decision gate. Half-cascaded turns are audio, so the name never matches and only follow-ups reply: use gate: :decision there.
  • :never - none.

With gate: :decision, each front end also asks a decision model about each turn's audio (Octoroom.Agent.Processors.Decisions): who the speaker is talking to, and whether an assistant could answer. When neither the name nor a follow-up settles it, :thresholds do:

  • the assistant's probability is at least reply_above (0.7): reply;
  • at least wait_above (0.35), or it's aimed at the group or nobody and answerable at least answer_above (0.7): wait wait_ms (2.5 s), and reply if nobody starts talking in the meantime;
  • otherwise: listen.

A follow-up gets the same reading, except that waiting counts as replying: it replies unless the model says listen. So the person it just answered can carry on without the name, but turning to someone else ("Bob, can you share your screen?") doesn't get an answer.

A turn with no decision (the call failed or took too long) falls back to the name and follow-ups.

Only the person the agent is answering can interrupt it (with one input and :always, that's always them). Two people talking over it don't cut it off.

Changing front-end stages

An Octoroom.Agent.Frames.UpdateStage that leaves the back end, from either end, goes to every front end, and to each one that starts later (with the latest options per stage). That's how a back-end stage changes the decision model's questions as a conversation moves on.

It hears at 16 kHz mono and speaks at 24 kHz mono on its first output.

Gemini Live and OpenAI Live

With Octoroom.Agent.Pipeline.Spec.gemini_live/1 (Octoroom.Agent.Processors.GeminiLive) or Octoroom.Agent.Pipeline.Spec.openai_live/1 (Octoroom.Agent.Processors.OpenAILive) as the pipeline, one speech-to-speech model does everything. It has one audio input, so there are no front ends: the inputs are mixed (Octoroom.Agent.Audio.Mixer) and the back end hears a 20 ms chunk of the mix at a time. The model decides when to answer and when it's been interrupted, so :respond, :gate and follow-ups don't apply, and anyone can interrupt it. What it hears has no speakers: it tells voices apart as best it can, the conversation's user items have no speaker, and tools get speaker: nil (except for typed turns, which are sent as [speaker] text). It needs a voice, and GOOGLE_API_KEY (Gemini Live) or OPENAI_API_KEY (OpenAI Live).

The model doesn't know when its audio has played, and generates faster than real time. So when it hears someone start a turn while its reply is still playing, the agent interrupts that reply itself, and the model thinks the whole reply was heard. There are no :transcription, :decision, :waiting or :follow_up events, and say/3 asks the model to say the words, which it may not do exactly.

Gemini Live keeps its session through dropped connections (the server closes each after 10 minutes or so) and through restarts: it saves the session's latest resumption handle (save:), and a restarted agent resumes with it. Gemini keeps a session for 2 hours after it ends. If the handle is older than that, or Gemini rejects it (or on OpenAI Live, which starts a new session on restart), the new session's instructions end with the conversation so far (the last 50 items), so the model still knows what was said, but not how it sounded. Either way, it doesn't greet again.

Args (a map or keyword list)

Octoroom.Agent.agent/3 builds these from an Octoroom.Agent module: :agent (the module) and :args (its ctx.args), whose callbacks fill in :instructions, :tools, :initial_text and the pipelines. Given directly:

  • :front_end, :back_end - the pipeline specs, both needed (see Octoroom.Agent.Pipeline.Spec.default/1). Each front end has its own host for tasks and timers. front_end: nil mixes the inputs into the back end, as for Gemini Live. A :tts stage must produce 24 kHz and an :stt stage hear 16 kHz. With gate: :decision, a front end with no :decisions stage gets one at its end, with :decisions as its options: they must name a :model.
  • :name (default "Assistant"), :respond, :follow_up_ms (default 10 s), :gate (:name, the default, or :decision), :thresholds, :decisions (options for the decision stage) - see above
  • :instructions, :initial_text, :tools - read by the context and tools stages
  • :tool_context - the map tool handlers receive (room_id, agent_id, and speaker, the participant whose turn started the reply, are added)
  • :voice - false runs no text-to-speech

It also takes {:octoroom_agent, :say, text}, {:octoroom_agent, :prompt, text} and {:octoroom_agent, :interrupt} messages (see Octoroom.Agent.say/3 and the others).

Speakers are named from participants' join/2 metadata (:name), or their id. Provider keys come from the environment: SONIOX_API_KEY, INWORLD_API_KEY, and whatever ReqLLM reads for the LLM (GOOGLE_API_KEY for Gemini).

Events

The worker emits, to the room's scope: :room subscribers (Octoroom.subscribe/2):

  • {:agent, {:conversation, item}} - a finished item (see Octoroom.Agent.ConversationEvents); user items carry speaker and source (the input track). A spoken reply comes twice with the same message_id: "playing", then "completed", or "interrupted" with only what was heard.
  • {:agent, {:transcription, %{status, text, source, speaker}}} - cascaded only: partial and final transcripts of each input, for captions. Half-cascaded sends {:agent, {:transcription, %{status: "final", text}}} per turn from the transcriber, with no source or speaker and usually after the reply has started; the matching :conversation item has both.
  • {:agent, {:follow_up, %{source, speaker, ms} | nil}} - with :addressed, when a reply finishes: for the next ms (the rest of the reply's audio plus :follow_up_ms), speaker can carry on without saying the name. nil when a new reply starts.

  • {:agent, {:decision, %{source, speaker, text, outcome, why, decision}}}
    • per turn: outcome is :reply, :wait or :listen, why says which rule decided (:name, :follow_up, :addressed, :maybe_addressed, :open_question, :not_addressed, :always, :never), and decision is the model's answers or nil
  • {:agent, {:waiting, %{source, speaker, ms} | nil}} - a turn is held for ms to see if anyone else answers; nil when that's settled

  • {:agent, {:latency, %{transcript, llm, audio}}} - per reply, ms from speech_end (below) to the turn reaching the back end (transcript: the final transcript when cascaded, the recorded turn when half-cascaded), the first LLM text, and the first audio handed to the outlet
  • {:agent, event} - the back end's other notifications, such as {:status, "Thinking"}

speech_end is when that speaker's VAD reported they stopped, minus its min_silence_ms. What the browser adds after the outlet isn't included.

Clients

Participants subscribed to its first output (its voice, or its data output with voice: false) see it in their client (see Octoroom.Worker's presence: and notify:; in the browser, agents/priv/static/octoroom_agents.mjs):

  • presence: attributes name and description (from agents/2), and state: "listening", "thinking" (a reply started), "speaking" (its audio is playing; until the last of it has played), or "waiting" (holding a turn, see above)
  • "transcript" events: each conversation item as it changes, message_id, sequence, role, text, status, interrupted, and speaker and source for people
  • a "transcript_snapshot" (%{items: [...]}, the last :snapshot_items, default 50) to each participant as it subscribes
  • "error" events: %{stage, fatal}

Decisions, waiting and latency stay server-side, as emits.

A client's %{"text" => text} message (room.send/2) is a turn from that participant that it always answers, whatever :respond and the gate say: typing to it is addressing it. It interrupts a reply as speaking would. Other payloads are ignored.