Class: Omnibot::Testing::FakeChat

Inherits:
Object
  • Object
show all
Defined in:
lib/omnibot/testing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_class: nil) ⇒ FakeChat

Returns a new instance of FakeChat.



51
52
53
54
55
56
57
# File 'lib/omnibot/testing.rb', line 51

def initialize(agent_class: nil)
  @agent_class = agent_class
  @messages = []
  @tools = []
  @before_tool_call_hooks = []
  @tool_results = []
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



49
50
51
# File 'lib/omnibot/testing.rb', line 49

def messages
  @messages
end

#tool_resultsObject (readonly)

Returns the value of attribute tool_results.



49
50
51
# File 'lib/omnibot/testing.rb', line 49

def tool_results
  @tool_results
end

Instance Method Details

#add_message(role:, content:) ⇒ Object



70
71
72
73
# File 'lib/omnibot/testing.rb', line 70

def add_message(role:, content:)
  @messages << { role: role, content: content }
  self
end

#ask(message, &stream_block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/omnibot/testing.rb', line 85

def ask(message, &stream_block)
  @messages << { role: :user, content: message }
  script = Testing.script_for(@agent_class)

  while (step = script.shift)
    kind, a, b = step
    case kind
    when :tool
      tool_call = FakeToolCall.new(a, b)
      @before_tool_call_hooks.each { |h| h.call(tool_call) }
      tool = @tools.find { |t| t.name.to_s == a }
      raise Omnibot::Error, "FakeChat: no tool #{a.inspect} attached" unless tool
      @tool_results << tool.execute(**b)
    when :reply
      return emit_reply(a, stream_block)
    end
  end

  emit_reply("(fake) #{message}", stream_block)
end

#before_tool_call(&blk) ⇒ Object



75
76
77
78
# File 'lib/omnibot/testing.rb', line 75

def before_tool_call(&blk)
  @before_tool_call_hooks << blk
  self
end

#with_instructions(text) ⇒ Object



59
60
61
62
# File 'lib/omnibot/testing.rb', line 59

def with_instructions(text, **)
  @messages << { role: :system, content: text }
  self
end

#with_schema(schema) ⇒ Object



80
81
82
83
# File 'lib/omnibot/testing.rb', line 80

def with_schema(schema)
  @schema = schema
  self
end

#with_tools(*tools, replace: false) ⇒ Object



64
65
66
67
68
# File 'lib/omnibot/testing.rb', line 64

def with_tools(*tools, replace: false)
  @tools = [] if replace
  @tools.concat(tools)
  self
end