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, ...] }.

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ FakeAdapter

Returns a new instance of FakeAdapter.



57
58
59
# File 'lib/terret/llm.rb', line 57

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

Instance Method Details

#stream(_request) {|MessageStop.new(stop_reason: calls.empty? ? :end_turn : :tool_use)| ... } ⇒ Object

Yields:

  • (MessageStop.new(stop_reason: calls.empty? ? :end_turn : :tool_use))


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/terret/llm.rb', line 61

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 MessageStop.new(stop_reason: calls.empty? ? :end_turn : :tool_use)
  Message.new(role: :assistant, parts: parts)
end