Octoroom.Agent.Processor behaviour (octoroom_agents v0.1.0)

Copy Markdown View Source

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} - run fun.(emit) in a task owned by the runner. Optional meta (a map) is merged into the task's [:octoroom_agent, :processor, :async, ...] telemetry metadata. Each emit.(message) arrives as handle_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) after ms. Interruptions don't cancel timers.
  • {:notify, event} - send event to the session owner
  • {:error, reason, fatal?} - push a Octoroom.Agent.Frames.Error upstream

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

ctx()

@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.

direction()

@type direction() :: :downstream | :upstream

effect()

@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()}

state()

@type state() :: term()

Callbacks

handle_async(key, message, state)

@callback handle_async(key :: term(), message :: term(), state()) :: {[effect()], state()}

handle_frame(frame, direction, state)

@callback handle_frame(frame :: struct(), direction(), state()) :: {[effect()], state()}

handle_info(message, state)

@callback handle_info(message :: term(), state()) :: {[effect()], state()}

handle_timer(key, state)

@callback handle_timer(key :: term(), state()) :: {[effect()], state()}

handle_update(opts, state)

(optional)
@callback handle_update(opts :: keyword(), state()) :: {[effect()], state()}

init(opts, ctx)

@callback init(opts :: keyword(), ctx()) :: {:ok, state()} | {:error, term()}

terminate(reason, state)

@callback terminate(reason :: term(), state()) :: term()

Functions

init(module, opts, ctx)

Initializes module, whether it is a plain processor or a Octoroom.Agent.Processor.StateMachine.

send_to(arg, message)

Sends message from another process (a socket, for example) to the processor whose ctx.reply_to this is. It arrives as handle_info/2.