Class: Mistri::Providers::Fake

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/providers/fake.rb

Overview

A scriptable provider: it streams each scripted turn as a well-formed event sequence and returns the assembled assistant message, so hosts test agent behavior hermetically while exercising real streaming semantics.

provider = Mistri::Providers::Fake.new(turns: [
{ text: "Hello!" },
{ tool_calls: [{ name: "search", arguments: { "q" => "ruby" } }] },
])

A turn may combine :thinking, :text, and :tool_calls, or carry :error to stream a failed turn. :stop_reason overrides the inferred reason.

Constant Summary collapse

MODEL =
"fake-1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(turns: [], chunk_size: 12) ⇒ Fake

Returns a new instance of Fake.



27
28
29
30
31
# File 'lib/mistri/providers/fake.rb', line 27

def initialize(turns: [], chunk_size: 12)
  @turns = turns.map { |turn| turn.transform_keys(&:to_sym) }
  @chunk_size = [chunk_size, 1].max
  @requests = []
end

Instance Attribute Details

#requestsObject (readonly)

Every #stream call is recorded here, so a test can assert what the agent actually sent.



23
24
25
# File 'lib/mistri/providers/fake.rb', line 23

def requests
  @requests
end

Instance Method Details

#modelObject



25
# File 'lib/mistri/providers/fake.rb', line 25

def model = MODEL

#stream(messages: [], **options, &emit) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mistri/providers/fake.rb', line 33

def stream(messages: [], **options, &emit)
  # Snapshot the array: the loop appends replies to it in place.
  @requests << { messages: messages.dup, options: }
  turn = @turns.shift
  raise ConfigurationError, "fake provider has no scripted turns left" unless turn

  blocks = []
  emit_event(emit, :start, blocks)
  return finish_error(turn, blocks, emit) if turn[:error]

  stream_block(:thinking, turn[:thinking], blocks, emit) if turn[:thinking]
  stream_block(:text, turn[:text], blocks, emit) if turn[:text]
  Array(turn[:tool_calls]).each_with_index do |call, position|
    stream_tool_call(call, position, blocks, emit)
  end
  finish(turn, blocks, emit)
end