Module: RubyCoded::Chat::State::MessageAssistant
- Included in:
- RubyCoded::Chat::State
- Defined in:
- lib/ruby_coded/chat/state/message_assistant.rb
Overview
Operations on the last assistant message: streaming, reset, and error handling.
Instance Method Summary collapse
-
#ensure_last_is_assistant! ⇒ Object
Ensures the last message is :assistant so streaming chunks land in the right place after tool call/result messages.
- #fail_last_assistant(error, friendly_message: nil) ⇒ Object
- #last_assistant_empty? ⇒ Boolean
- #reset_last_assistant_content ⇒ Object
-
#streaming_append(text) ⇒ Object
Single-mutex operation combining ensure_last_is_assistant! + append.
Instance Method Details
#ensure_last_is_assistant! ⇒ Object
Ensures the last message is :assistant so streaming chunks land in the right place after tool call/result messages.
10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby_coded/chat/state/message_assistant.rb', line 10 def ensure_last_is_assistant! @mutex.synchronize do return if !@messages.empty? && @messages.last[:role] == :assistant @messages << (:assistant) @message_generation += 1 @dirty = true end end |
#fail_last_assistant(error, friendly_message: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruby_coded/chat/state/message_assistant.rb', line 52 def fail_last_assistant(error, friendly_message: nil) @mutex.synchronize do return if @messages.empty? last = @messages.last return unless last[:role] == :assistant (last, || "[Error] #{error.class}: #{error.}") @message_generation += 1 @dirty = true end end |
#last_assistant_empty? ⇒ Boolean
30 31 32 33 34 35 36 37 |
# File 'lib/ruby_coded/chat/state/message_assistant.rb', line 30 def last_assistant_empty? @mutex.synchronize do return true if @messages.empty? last = @messages.last last[:role] == :assistant && last[:content].strip.empty? end end |
#reset_last_assistant_content ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruby_coded/chat/state/message_assistant.rb', line 39 def reset_last_assistant_content @mutex.synchronize do return if @messages.empty? last = @messages.last return unless last[:role] == :assistant last[:content] = String.new @message_generation += 1 @dirty = true end end |
#streaming_append(text) ⇒ Object
Single-mutex operation combining ensure_last_is_assistant! + append.
21 22 23 24 25 26 27 28 |
# File 'lib/ruby_coded/chat/state/message_assistant.rb', line 21 def streaming_append(text) @mutex.synchronize do @messages << (:assistant) if @messages.empty? || @messages.last[:role] != :assistant @messages.last[:content] << text.to_s @message_generation += 1 @dirty = true end end |