Class: Riffer::Agent Abstract

Inherits:
Object
  • Object
show all
Extended by:
Helpers::Validations
Includes:
Messages::Converter
Defined in:
lib/riffer/agent.rb

Overview

This class is abstract.

Riffer::Agent is the base class for all agents in the Riffer framework.

Provides orchestration for LLM calls, tool use, and message management.

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Messages::Converter

#convert_to_message_object

Constructor Details

#initializevoid

Initializes a new agent

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/riffer/agent.rb', line 63

def initialize
  @messages = []
  @model_string = self.class.model
  @instructions_text = self.class.instructions

  provider_name, model_name = @model_string.split("/", 2)

  raise Riffer::ArgumentError, "Invalid model string: #{@model_string}" unless [provider_name, model_name].all? { |part| part.is_a?(String) && !part.strip.empty? }

  @provider_name = provider_name
  @model_name = model_name
end

Instance Attribute Details

#messagesArray<Riffer::Messages::Base> (readonly)

The message history for the agent

Returns:



58
59
60
# File 'lib/riffer/agent.rb', line 58

def messages
  @messages
end

Class Method Details

.allArray<Class>

Returns all agent subclasses

Returns:

  • (Array<Class>)

    all agent subclasses



51
52
53
# File 'lib/riffer/agent.rb', line 51

def all
  subclasses
end

.find(identifier) ⇒ Class?

Finds an agent class by identifier

Parameters:

  • identifier (String)

    the identifier to search for

Returns:

  • (Class, nil)

    the agent class, or nil if not found



45
46
47
# File 'lib/riffer/agent.rb', line 45

def find(identifier)
  subclasses.find { |agent_class| agent_class.identifier == identifier.to_s }
end

.identifier(value = nil) ⇒ String

Gets or sets the agent identifier

Parameters:

  • value (String, nil) (defaults to: nil)

    the identifier to set, or nil to get

Returns:

  • (String)

    the agent identifier



19
20
21
22
# File 'lib/riffer/agent.rb', line 19

def identifier(value = nil)
  return @identifier if value.nil?
  @identifier = value.to_s
end

.instructions(instructions_text = nil) ⇒ String

Gets or sets the agent instructions

Parameters:

  • instructions_text (String, nil) (defaults to: nil)

    the instructions to set, or nil to get

Returns:

  • (String)

    the agent instructions



36
37
38
39
40
# File 'lib/riffer/agent.rb', line 36

def instructions(instructions_text = nil)
  return @instructions if instructions_text.nil?
  validate_is_string!(instructions_text, "instructions")
  @instructions = instructions_text
end

.model(model_string = nil) ⇒ String

Gets or sets the model string (e.g., “openai/gpt-4”)

Parameters:

  • model_string (String, nil) (defaults to: nil)

    the model string to set, or nil to get

Returns:

  • (String)

    the model string



27
28
29
30
31
# File 'lib/riffer/agent.rb', line 27

def model(model_string = nil)
  return @model if model_string.nil?
  validate_is_string!(model_string, "model")
  @model = model_string
end

Instance Method Details

#generate(prompt_or_messages) ⇒ String

Generates a response from the agent

Parameters:

Returns:

  • (String)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/riffer/agent.rb', line 79

def generate(prompt_or_messages)
  initialize_messages(prompt_or_messages)

  loop do
    response = call_llm
    @messages << response

    break unless has_tool_calls?(response)

    execute_tool_calls(response)
  end

  extract_final_response
end

#stream(prompt_or_messages) ⇒ Enumerator

Streams a response from the agent

Parameters:

Returns:

  • (Enumerator)

    an enumerator yielding stream events



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/riffer/agent.rb', line 97

def stream(prompt_or_messages)
  initialize_messages(prompt_or_messages)

  Enumerator.new do |yielder|
    accumulated_content = ""

    call_llm_stream.each do |event|
      yielder << event

      case event
      when Riffer::StreamEvents::TextDelta
        accumulated_content += event.content
      when Riffer::StreamEvents::TextDone
        accumulated_content = event.content
      end
    end

    response = Riffer::Messages::Assistant.new(accumulated_content)
    @messages << response
  end
end