Module: Agents::Helpers::MessageExtractor

Defined in:
lib/agents/helpers/message_extractor.rb

Constant Summary collapse

AUTHORING_AGENT_IVAR =

RubyLLM::Message has no metadata/extension API, so agent ownership is stored as an SDK-namespaced ivar on the message object until it is extracted.

Caveat: RubyLLM::Message#instance_variables is overridden to hide :@raw but does not hide this ivar, so it will appear in instance_variables listings. This is harmless for our own code path (we read it via instance_variable_get and serialize through extract_messages, not Marshal), but external introspection of a message’s ivars will see :@agents_authoring_agent.

:@agents_authoring_agent

Class Method Summary collapse

Class Method Details

.assign_agent_name(message, agent_name) ⇒ Object



33
34
35
36
37
# File 'lib/agents/helpers/message_extractor.rb', line 33

def assign_agent_name(message, agent_name)
  return unless message && agent_name

  message.instance_variable_set(AUTHORING_AGENT_IVAR, agent_name)
end

.attributed_agent_name_for(message) ⇒ Object



39
40
41
42
43
# File 'lib/agents/helpers/message_extractor.rb', line 39

def attributed_agent_name_for(message)
  return unless message&.instance_variable_defined?(AUTHORING_AGENT_IVAR)

  message.instance_variable_get(AUTHORING_AGENT_IVAR)
end

.content_empty?(content) ⇒ Boolean

Check if content is considered empty (handles both String and Hash content)

Parameters:

  • content (String, Hash, nil)

    The content to check

Returns:

  • (Boolean)

    true if content is empty, false otherwise



49
50
51
52
53
54
55
56
57
58
# File 'lib/agents/helpers/message_extractor.rb', line 49

def content_empty?(content)
  case content
  when String
    content.strip.empty?
  when Hash
    content.empty?
  else
    content.nil?
  end
end

.extract_messages(chat, current_agent) ⇒ Array<Hash>

Extract messages from a chat object for conversation history persistence

Parameters:

  • chat (Object)

    Chat object that responds to :messages

  • current_agent (Agent)

    The agent currently handling the conversation

Returns:

  • (Array<Hash>)

    Array of message hashes suitable for persistence



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/agents/helpers/message_extractor.rb', line 65

def extract_messages(chat, current_agent)
  return [] unless chat.respond_to?(:messages)

  chat.messages.filter_map do |msg|
    case msg.role
    when :user, :assistant
      extract_user_or_assistant_message(msg, current_agent)
    when :tool
      extract_tool_message(msg)
    end
  end
end