Module: Agents::Helpers::MessageExtractor

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

Class Method Summary collapse

Class Method Details

.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



27
28
29
30
31
32
33
34
35
36
# File 'lib/agents/helpers/message_extractor.rb', line 27

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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/agents/helpers/message_extractor.rb', line 43

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