# `AgentSessionManager.Rendering.Sink`
[🔗](https://github.com/nshkrdotcom/agent_session_manager/blob/v0.8.0/lib/agent_session_manager/rendering/sink.ex#L1)

Behaviour for output destinations that receive rendered text from a renderer.

A sink writes rendered output to a specific destination: terminal, file, JSON log,
callback function, etc. Sinks receive iodata and decide how to write it.

## Implementing a Sink

    defmodule MySink do
      @behaviour AgentSessionManager.Rendering.Sink

      @impl true
      def init(opts), do: {:ok, %{device: opts[:device] || :stdio}}

      @impl true
      def write(iodata, state) do
        IO.write(state.device, iodata)
        {:ok, state}
      end

      @impl true
      def write_event(_event, _iodata, state), do: {:ok, state}

      @impl true
      def flush(state), do: {:ok, state}

      @impl true
      def close(state), do: :ok
    end

## Rendered vs Raw Output

Sinks receive output through two channels:

- `write/2` — Rendered text from the renderer. Used for human-readable output.
- `write_event/3` — The raw event plus rendered text. Used by sinks that need
  the structured event data (e.g. JSONL loggers that serialize events, not text).

# `opts`

```elixir
@type opts() :: keyword()
```

# `state`

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

# `close`

```elixir
@callback close(state()) :: :ok
```

Close the sink and release resources.

# `flush`

```elixir
@callback flush(state()) :: {:ok, state()}
```

Flush any buffered output.

# `init`

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

Initialize sink state from options.

# `write`

```elixir
@callback write(iodata(), state()) :: {:ok, state()} | {:error, term(), state()}
```

Write rendered iodata to the output destination.

# `write_event`

```elixir
@callback write_event(event :: map(), iodata(), state()) ::
  {:ok, state()} | {:error, term(), state()}
```

Write the raw event alongside its rendered form. Called for every event.

Sinks that only care about rendered text can delegate to `write/2` or no-op.
Sinks that need structured data (JSONL, callback) use the event directly.

---

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