# Class LittleGhost::AGUI::Adapter

Documentation version: Edge

Canonical HTML: https://littleghostai.org/docs/LittleGhost/AGUI/Adapter.html

Adapter turns a LittleGhost stream into AG-UI event hashes. It lets a Ruby
agent drive compatible chat interfaces without changing the agent itself.

    events = CustomerSupportAgent.stream_ask("Where is my order?")
    adapter = LittleGhost::AGUI::Adapter.new
    adapter.stream(events, thread_id: "thread-1", run_id: "run-1").each do |event|
      websocket.write(JSON.generate(event))
    end

The adapter has no state between #stream calls, so one instance can translate
independent runs.

Progress comes from the Run's native `:agent_stream` events. By
default, only the top-level Agent's progress reaches the interface: its
[AgentStreamSource](../AgentStreamSource.md) has `agent_path`
`/root` and an empty `assembly_path`. A composite run's final
response arrives in `RUN_FINISHED` independently of participant progress.
Unwrapped progress and wrapped invocation usage or terminal events are
ignored; run lifecycle and aggregate usage remain separate.

### Choose what the interface receives

Pass a `source_filter` callable to select a participant whose progress is
intended for the destination. The callable receives an AgentStreamSource and
replaces the default root selection. Choose it in trusted application code,
not from request or model values.

    adapter = LittleGhost::AGUI::Adapter.new(source_filter: lambda { |source|
      step = source.assembly_path.first
      source.agent_path == "/root" && source.assembly_path.length == 1 &&
        step.assembly_id == "response_workflow" && step.participant == "response"
    })

Provider plaintext reasoning becomes AG-UI reasoning events. Tool arguments
and results, invocation metadata, subagent events, trace context, and selected
error text also pass through without redaction. Authorize and filter the
complete stream before transport, and send it only to an interface intended to
display that data. Encrypted reasoning and provider continuity artifacts are
never exposed here.

## Inheritance

`LittleGhost::AGUI::Adapter < Object`

## Class methods

<a id="method-c-new"></a>
### `.new`

```ruby
.new(source_filter: nil)
```

Selects which Agent's progress reaches the interface.

`source_filter` is a callable receiving an AgentStreamSource and returning a
truthy value to include its progress. Omit it to include only a root Agent
outside any composite assembly. A shared adapter may call the filter
concurrently for independent streams, so keep its captured state safe for
concurrent use. Filter exceptions propagate to the caller of #stream. Raises
ArgumentError when `source_filter` is not callable.

## Instance methods

<a id="method-i-stream"></a>
### `#stream`

```ruby
#stream(events, thread_id:, run_id:)
```

Lazily translates a Run's `events` into AG-UI hashes for one interface run.

Returns an Enumerator. Selected Agent progress arrives as it is produced;
`RUN_FINISHED` carries the run's final response in
`result[:response]`.
