# `Octoroom.Agent.Processor`
[🔗](https://github.com/samrat/octoroom/blob/v0.1.0/agents/lib/octoroom/agent/processor.ex#L1)

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

# `ctx`

```elixir
@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`

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

# `effect`

```elixir
@type effect() ::
  {:push, struct(), direction()}
  | {:async, key :: term(), (emit :: (term() -&gt; :ok) -&gt; term())}
  | {:async, key :: term(), (emit :: (term() -&gt; :ok) -&gt; 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`

```elixir
@type state() :: term()
```

# `handle_async`

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

# `handle_frame`

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

# `handle_info`

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

# `handle_timer`

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

# `handle_update`
*optional* 

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

# `init`

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

# `terminate`

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

# `init`

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

# `send_to`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
