A stage in a pipeline. See docs/pipelines.md.
Processors don't send messages or start processes for their work. Every callback returns a list of effects, and the runner executes them in order:
{:push, frame, :downstream | :upstream}- send a frame to the next or previous stage{:async, key, fun}or{:async, key, fun, meta}- runfun.(emit)in a task owned by the runner. Optionalmeta(a map) is merged into the task's[:octoroom_agent, :processor, :async, ...]telemetry metadata. Eachemit.(message)arrives ashandle_async(key, message, state); the return value arrives as{:done, result}and a crash as{:failed, reason}, so emitted messages must not have those shapes (wrap them). Starting a key that is running replaces the old task. An interruption kills the task, unless it was started while handling an uninterruptible frame.{:cancel, key}- kill a running task{:timer, key, ms}/{:cancel_timer, key}-handle_timer(key, state)afterms. Interruptions don't cancel timers.{:notify, event}- sendeventto the session owner{:error, reason, fatal?}- push aOctoroom.Agent.Frames.Errorupstream
A processor whose options can change while it runs implements the
optional handle_update/2. An Octoroom.Agent.Frames.UpdateStage frame
naming the stage arrives there with the new options, instead of at
handle_frame/3. It returns effects like any callback; one that can't
take an option returns an {:error, ...} effect.
use Octoroom.Agent.Processor defines every callback but
handle_update/2: frames pass through
unchanged and other messages are ignored, so a processor only needs to
match what it cares about.
defmodule MyApp.Shout do
use Octoroom.Agent.Processor
@impl true
def handle_frame(%Octoroom.Agent.Frames.LLMText{} = frame, :downstream, state) do
{[{:push, %{frame | text: String.upcase(frame.text)}, :downstream}], state}
end
def handle_frame(frame, direction, state), do: {[{:push, frame, direction}], state}
end
Summary
Types
Given to init/2: the stage's :name and :index, :reply_to when the
runner runs in a process (see send_to/2), plus whatever the runner adds.
Functions
Initializes module, whether it is a plain processor or a
Octoroom.Agent.Processor.StateMachine.
Sends message from another process (a socket, for example) to the
processor whose ctx.reply_to this is. It arrives as handle_info/2.
Types
@type ctx() :: map()
Given to init/2: the stage's :name and :index, :reply_to when the
runner runs in a process (see send_to/2), plus whatever the runner adds.
@type direction() :: :downstream | :upstream
@type effect() :: {:push, struct(), direction()} | {:async, key :: term(), (emit :: (term() -> :ok) -> term())} | {:async, key :: term(), (emit :: (term() -> :ok) -> term()), meta :: map()} | {:cancel, key :: term()} | {:timer, key :: term(), ms :: non_neg_integer()} | {:cancel_timer, key :: term()} | {:notify, event :: term()} | {:error, reason :: term(), fatal? :: boolean()}
@type state() :: term()
Callbacks
Functions
Initializes module, whether it is a plain processor or a
Octoroom.Agent.Processor.StateMachine.
Sends message from another process (a socket, for example) to the
processor whose ctx.reply_to this is. It arrives as handle_info/2.