class RubyLLM::AgUI::Mapper

Projects one RubyLLM 2.x chat turn onto AG-UI 1.0 events (FR-03).

The mapper is the only place that decides which AG-UI event a RubyLLM signal becomes. It is a state machine rather than a set of converters, because the protocol’s rules are about state: a text message must be open before its content arrives, a tool call must be closed before its result, consecutive steps must not overlap, and everything must be closed before the run’s terminal event.

It maps what RubyLLM actually reported and nothing else. It never invents an event to fill a gap in its own output, and it never re-synthesises tool arguments that RubyLLM delivered as a stream: the deltas go out as received, in order.

Every method returns the events to write, in order. The mapper writes nothing itself, so the caller owns the transport and can advance the run record’s sequence with the same list.

mapper = RubyLLM::AgUI::Mapper.new(thread_id: "t1", run_id: "r1")

chat.before_tool_call { |tool_call| write(mapper.before_tool_call(tool_call)) }
chat.after_message    { |message|   write(mapper.message(message)) }
chat.complete         { |chunk|     write(mapper.chunk(chunk)) }

write(mapper.close)  # before the run's terminal event