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.



28
29
30
31
32
33
34
35
36
37
# File 'lib/mistri/providers/fake.rb', line 28

def initialize(turns: [], chunk_size: 12)
  @turns = turns.map { |turn| turn.transform_keys(&:to_sym) }
  @prices_usage = @turns.all? do |turn|
    !turn[:usage] || turn[:usage].cost.known?
  end
  @chunk_size = [chunk_size, 1].max
  @requests = []
  @tool_call_prefix = SecureRandom.uuid
  @tool_call_sequence = 0
end

Instance Attribute Details

#requestsObject (readonly)

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



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

def requests
  @requests
end

Instance Method Details

#modelObject



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

def model = MODEL

#prices_usage?Boolean

Returns:

  • (Boolean)


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

def prices_usage? = @prices_usage

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

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mistri/providers/fake.rb', line 41

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