Class: AgentC::TestHelpers::DummyChat

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_c/test_helpers.rb

Overview

DummyChat that maps input_text => output_text for testing Use this with Session.new() by passing it as a chat_provider or record parameter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(responses: {}, prompts: [], tools: [], cached_prompts: [], workspace_dir: nil, record: nil, session: nil, **_options) ⇒ DummyChat

Returns a new instance of DummyChat.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/agent_c/test_helpers.rb', line 26

def initialize(
  responses: {},
  prompts: [],
  tools: [],
  cached_prompts: [],
  workspace_dir: nil,
  record: nil,
  session: nil,
  **_options
)
  @responses = responses
  @id = "test-chat-#{rand(1000)}"
  @messages_history = []
  @prompts_received = prompts
  @tools_received = tools
  @on_end_message_blocks = []
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/agent_c/test_helpers.rb', line 24

def id
  @id
end

#invocationsObject (readonly)

Returns the value of attribute invocations.



24
25
26
# File 'lib/agent_c/test_helpers.rb', line 24

def invocations
  @invocations
end

#messages_historyObject (readonly)

Returns the value of attribute messages_history.



24
25
26
# File 'lib/agent_c/test_helpers.rb', line 24

def messages_history
  @messages_history
end

#prompts_receivedObject (readonly)

Returns the value of attribute prompts_received.



24
25
26
# File 'lib/agent_c/test_helpers.rb', line 24

def prompts_received
  @prompts_received
end

#tools_receivedObject (readonly)

Returns the value of attribute tools_received.



24
25
26
# File 'lib/agent_c/test_helpers.rb', line 24

def tools_received
  @tools_received
end

Instance Method Details

#ask(input_text) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/agent_c/test_helpers.rb', line 44

def ask(input_text)
  # Try to find a matching response
  _, output = (
    @responses.find do |key, value|
      (key.is_a?(Regexp) && input_text.match?(key)) ||
        (key.is_a?(Proc) && key.call(input_text)) ||
        (key == input_text)
    end
  )

  output_text = (
    if output.respond_to?(:call)
      output.call
    else
      output
    end
  )

  raise "No response configured for: #{input_text.inspect}" if output_text.nil?

  # Create a mock message with the input
  user_message = OpenStruct.new(
    role: :user,
    content: input_text,
    to_llm: OpenStruct.new(to_h: { role: :user, content: input_text })
  )

  # Create a mock response message
  response_message = OpenStruct.new(
    role: :assistant,
    content: output_text,
    to_llm: OpenStruct.new(to_h: { role: :assistant, content: output_text })
  )

  @messages_history << user_message
  @messages_history << response_message

  # Call all on_end_message hooks
  @on_end_message_blocks.each { |block| block.call(response_message) }

  response_message
end

#get(input_text, schema: nil, **options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/agent_c/test_helpers.rb', line 87

def get(input_text, schema: nil, **options)
  # Similar to ask, but returns parsed JSON as a Hash for structured responses
  response_message = ask(input_text)

  json_schema = schema&.to_json_schema&.fetch(:schema)

  # Parse the response content as JSON
  begin
    result = JSON.parse(response_message.content)
    if json_schema.nil? || JSON::Validator.validate(json_schema, result)
      result
    else
      raise "Failed to get valid response"
    end
  rescue JSON::ParserError
    # If not valid JSON, wrap in a hash
    { "result" => response_message.content }
  end
end

#messagesObject



107
108
109
# File 'lib/agent_c/test_helpers.rb', line 107

def messages(...)
  @messages_history
end

#on_end_message(&block) ⇒ Object



120
121
122
123
# File 'lib/agent_c/test_helpers.rb', line 120

def on_end_message(&block)
  @on_end_message_blocks << block
  self
end

#on_new_message(&block) ⇒ Object



116
117
118
# File 'lib/agent_c/test_helpers.rb', line 116

def on_new_message(&block)
  self
end

#on_tool_call(&block) ⇒ Object



125
126
127
# File 'lib/agent_c/test_helpers.rb', line 125

def on_tool_call(&block)
  self
end

#on_tool_result(&block) ⇒ Object



129
130
131
# File 'lib/agent_c/test_helpers.rb', line 129

def on_tool_result(&block)
  self
end

#with_tools(*tools) ⇒ Object



111
112
113
114
# File 'lib/agent_c/test_helpers.rb', line 111

def with_tools(*tools)
  @tools_received = tools.flatten
  self
end