Class: Envoy::Testing::FakeLLM

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

Overview

Deterministic stand-in for Envoy::LLM. Persists Envoy rows exactly as the real path would and replays a script of text: / args: directives.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conversation:) ⇒ FakeLLM

Returns a new instance of FakeLLM.



8
9
10
11
# File 'lib/envoy/testing/fake_llm.rb', line 8

def initialize(conversation:)
  @conversation = conversation
  @script = []
end

Instance Attribute Details

#scriptObject

Returns the value of attribute script.



6
7
8
# File 'lib/envoy/testing/fake_llm.rb', line 6

def script
  @script
end

Instance Method Details

#run(content:, tools:, instructions:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/envoy/testing/fake_llm.rb', line 13

def run(content:, tools:, instructions:)
  @conversation.messages.create!(role: "user", content: content)
  assistant = @conversation.messages.create!(role: "assistant", content: nil)

  @script.each do |directive|
    if directive[:text]
      # Accumulate like real streaming: multiple text deltas build one message.
      assistant.update!(content: assistant.content.to_s + directive[:text])
      yield [ :delta, directive[:text] ] if block_given?
    else
      tool = tools.find { |t| t.name == directive[:tool].to_s }
      raise "no tool #{directive[:tool]}" unless tool

      value = tool.execute(**directive[:args])
      tc = assistant.tool_calls.create!(
        tool_call_id: "fake-#{assistant.tool_calls.count + 1}",
        name: tool.name, arguments: directive[:args].stringify_keys,
        status: tool.last_status.to_s
      )
      @conversation.messages.create!(
        role: "tool", tool_call_id: tc.id, content: value
      )
      yield [ :tool, tc ] if block_given?
    end
  end
end