Class: Terret::LLM::FakeAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/terret/llm.rb

Overview

Deterministic scripted adapter for tests, demos, and record/replay. Script: array of hashes { text:, tool_calls: [ToolCall, ...], usage: }.

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ FakeAdapter

Returns a new instance of FakeAdapter.



144
145
146
# File 'lib/terret/llm.rb', line 144

def initialize(script)
  @script = script.dup
end

Instance Method Details

#stream(_request) {|Usage.new(**step[:usage])| ... } ⇒ Object

Yields:

  • (Usage.new(**step[:usage]))


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/terret/llm.rb', line 148

def stream(_request)
  step = @script.shift or raise "FakeAdapter script exhausted"
  parts = []
  if (t = step[:text])
    t.chars.each_slice(8) { |chunk| yield TextDelta.new(text: chunk.join) }
    parts << Text.new(text: t)
  end
  calls = Array(step[:tool_calls])
  calls.each do |tc|
    yield ToolCallEnd.new(tool_call: tc)
    parts << tc
  end
  yield Usage.new(**step[:usage]) if step[:usage]
  yield MessageStop.new(stop_reason: calls.empty? ? :end_turn : :tool_use)
  Message.new(role: :assistant, parts: parts)
end