Class: AIA::SessionTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/aia/session_tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSessionTracker

Returns a new instance of SessionTracker.



12
13
14
15
16
17
# File 'lib/aia/session_tracker.rb', line 12

def initialize
  @turn_count = 0
  @total_cost = 0.0
  @total_tokens = 0
  @turns = []
end

Instance Attribute Details

#total_costObject (readonly)

Returns the value of attribute total_cost.



10
11
12
# File 'lib/aia/session_tracker.rb', line 10

def total_cost
  @total_cost
end

#total_tokensObject (readonly)

Returns the value of attribute total_tokens.



10
11
12
# File 'lib/aia/session_tracker.rb', line 10

def total_tokens
  @total_tokens
end

#turn_countObject (readonly)

Returns the value of attribute turn_count.



10
11
12
# File 'lib/aia/session_tracker.rb', line 10

def turn_count
  @turn_count
end

#turnsObject (readonly)

Returns the value of attribute turns.



10
11
12
# File 'lib/aia/session_tracker.rb', line 10

def turns
  @turns
end

Instance Method Details

#record_model_switch(from:, to:, reason: "user_request") ⇒ Object

Record a model switch event.

Parameters:

  • from (String)

    previous model

  • to (String)

    new model

  • reason (String) (defaults to: "user_request")

    reason for the switch



59
60
61
62
63
64
65
66
67
# File 'lib/aia/session_tracker.rb', line 59

def record_model_switch(from:, to:, reason: "user_request")
  @turns << {
    type: :model_switch,
    from: from,
    to: to,
    reason: reason,
    timestamp: Time.now
  }
end

#record_turn(model:, input:, result:, decisions: nil, elapsed: nil) ⇒ Object

Record a completed turn.

For network results (SimpleFlow::Result), records one entry per robot so the /cost directive can show per-model breakdowns.

Parameters:

  • model (String)

    model used (ignored for network results)

  • input (String)

    user input

  • result

    the LLM response

  • decisions (Hash, nil) (defaults to: nil)

    routing decisions for this turn

  • elapsed (Float, nil) (defaults to: nil)

    seconds the model took to respond



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aia/session_tracker.rb', line 29

def record_turn(model:, input:, result:, decisions: nil, elapsed: nil)
  if defined?(SimpleFlow::Result) && result.is_a?(SimpleFlow::Result)
    record_network_turn(input: input, flow_result: result, decisions: decisions, elapsed: elapsed)
    return
  end

  @turn_count += 1

  metrics = extract_metrics(result)
  @total_cost += metrics[:cost]
  @total_tokens += metrics[:tokens]

  @turns << {
    model: model,
    input_length: input.to_s.length,
    input_tokens: metrics[:input_tokens],
    output_tokens: metrics[:output_tokens],
    tokens: metrics[:tokens],
    cost: metrics[:cost],
    elapsed: elapsed || 0,
    decisions: decisions&.to_h,
    timestamp: Time.now
  }
end

#record_user_feedback(satisfied:) ⇒ Object

Record user feedback on the last response.

Parameters:

  • satisfied (Boolean)

    whether the user was satisfied



72
73
74
75
# File 'lib/aia/session_tracker.rb', line 72

def record_user_feedback(satisfied:)
  return if @turns.empty?
  @turns.last[:user_satisfied] = satisfied
end

#reset!Object

Reset all tracked data.



89
90
91
92
93
94
# File 'lib/aia/session_tracker.rb', line 89

def reset!
  @turn_count = 0
  @total_cost = 0.0
  @total_tokens = 0
  @turns.clear
end

#to_factsHash

Export session stats as a hash for KBS fact assertion.

Returns:

  • (Hash)

    session statistics



80
81
82
83
84
85
86
# File 'lib/aia/session_tracker.rb', line 80

def to_facts
  {
    turn_count: @turn_count,
    total_cost: @total_cost,
    total_tokens: @total_tokens
  }
end