Class: Riffer::Tracing::StreamRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/tracing/stream_recorder.rb,
sig/generated/riffer/tracing/stream_recorder.rbs

Overview

Wraps a stream yielder to observe terminal events for span stamping while forwarding every event downstream untouched.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yielder, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) ⇒ StreamRecorder

-- : (Enumerator::Yielder, ?clock: ^() -> Float) -> void

Parameters:

  • (Enumerator::Yielder)
  • clock: (^() -> Float) (defaults to: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })


25
26
27
28
29
30
# File 'lib/riffer/tracing/stream_recorder.rb', line 25

def initialize(yielder, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
  @yielder = yielder
  @clock = clock
  @started_at = clock.call
  @tool_calls = [] #: Array[Riffer::Messages::Assistant::ToolCall]
end

Instance Attribute Details

#contentString? (readonly)

: String?

Returns:

  • (String, nil)


19
20
21
# File 'lib/riffer/tracing/stream_recorder.rb', line 19

def content
  @content
end

#finish_reasonSymbol? (readonly)

: Symbol?

Returns:

  • (Symbol, nil)


15
16
17
# File 'lib/riffer/tracing/stream_recorder.rb', line 15

def finish_reason
  @finish_reason
end

#raw_finish_reasonString? (readonly)

: String?

Returns:

  • (String, nil)


17
18
19
# File 'lib/riffer/tracing/stream_recorder.rb', line 17

def raw_finish_reason
  @raw_finish_reason
end

#time_to_first_chunkFloat? (readonly)

: Float?

Returns:

  • (Float, nil)


13
14
15
# File 'lib/riffer/tracing/stream_recorder.rb', line 13

def time_to_first_chunk
  @time_to_first_chunk
end

#token_usageRiffer::Providers::TokenUsage? (readonly)

Returns the value of attribute token_usage.



11
12
13
# File 'lib/riffer/tracing/stream_recorder.rb', line 11

def token_usage
  @token_usage
end

#tool_callsArray[Riffer::Messages::Assistant::ToolCall] (readonly)

: Array



21
22
23
# File 'lib/riffer/tracing/stream_recorder.rb', line 21

def tool_calls
  @tool_calls
end

Instance Method Details

#<<(event) ⇒ self

-- : (Riffer::StreamEvents::Base) -> self

Parameters:

Returns:

  • (self)


34
35
36
37
38
39
# File 'lib/riffer/tracing/stream_recorder.rb', line 34

def <<(event)
  @time_to_first_chunk ||= @clock.call - @started_at
  record(event)
  @yielder << event
  self
end

#record(event) ⇒ void

This method returns an undefined value.

-- : (Riffer::StreamEvents::Base) -> void



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/riffer/tracing/stream_recorder.rb', line 45

def record(event)
  case event
  when Riffer::StreamEvents::TextDone
    @content = event.content
  when Riffer::StreamEvents::ToolCallDone
    @tool_calls << Riffer::Messages::Assistant::ToolCall.new(call_id: event.call_id, name: event.name, arguments: event.arguments)
  when Riffer::StreamEvents::TokenUsageDone
    @token_usage = event.token_usage
  when Riffer::StreamEvents::FinishReasonDone
    @finish_reason = event.finish_reason
    @raw_finish_reason = event.raw_finish_reason
  end
end