Class: AgUi::Terminals::RubyLLM::TurnEmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/ag_ui/terminals/ruby_llm.rb

Overview

Emits the turn's reasoning + text phases lazily: nothing opens until its first non-empty delta (tool-call-only turns produce no empty bubbles), and reasoning closes as soon as text begins — providers stream thinking strictly before the answer. Turns a provider's chunk stream into the AG-UI text/reasoning vocabulary, emitted through the env's hooks: a reasoning phase opens lazily and closes before any text, and the text phase starts on its first non-empty delta.

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TurnEmitter

Returns a new instance of TurnEmitter.



97
98
99
100
101
102
103
# File 'lib/ag_ui/terminals/ruby_llm.rb', line 97

def initialize(env)
  @env = env
  @text_id = SecureRandom.uuid
  @reasoning_id = SecureRandom.uuid
  @text_started = false
  @reasoning_open = false
end

Instance Method Details

#finishObject



126
127
128
129
130
131
# File 'lib/ag_ui/terminals/ruby_llm.rb', line 126

def finish
  close_reasoning
  if @text_started
    @env.emit(:text_message_end, { message_id: @text_id })
  end
end

#text_delta(text) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/ag_ui/terminals/ruby_llm.rb', line 115

def text_delta(text)
  unless text.empty?
    close_reasoning
    unless @text_started
      @env.emit(:text_message_start, { message_id: @text_id })
      @text_started = true
    end
    @env.emit(:text_message_content, { message_id: @text_id, delta: text })
  end
end

#thinking_delta(text) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/ag_ui/terminals/ruby_llm.rb', line 105

def thinking_delta(text)
  unless text.empty?
    open_reasoning
    @env.emit(
      :reasoning_message_content,
      { message_id: @reasoning_id, delta: text },
    )
  end
end