Class: Legion::Extensions::Agentic::Inference::Prediction::Helpers::PredictionStore

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePredictionStore

Returns a new instance of PredictionStore.



14
15
16
17
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 14

def initialize
  @predictions = {}
  @outcomes = []
end

Instance Attribute Details

#outcomesObject (readonly)

Returns the value of attribute outcomes.



12
13
14
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 12

def outcomes
  @outcomes
end

#predictionsObject (readonly)

Returns the value of attribute predictions.



12
13
14
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 12

def predictions
  @predictions
end

Instance Method Details

#accuracy(window: 100) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 49

def accuracy(window: 100)
  recent = @outcomes.last(window)
  return 0.0 if recent.empty?

  correct = recent.count { |o| o[:outcome] == :correct }
  correct.to_f / recent.size
end

#countObject



57
58
59
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 57

def count
  @predictions.size
end

#get(prediction_id) ⇒ Object



28
29
30
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 28

def get(prediction_id)
  @predictions[prediction_id]
end

#pendingObject



45
46
47
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 45

def pending
  @predictions.values.select { |p| p[:status] == :pending }
end

#recently_resolved(limit: 20) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 65

def recently_resolved(limit: 20)
  @outcomes.last(limit).filter_map do |entry|
    pred = @predictions[entry[:prediction_id]]
    next unless pred

    {
      prediction_id:  entry[:prediction_id],
      domain:         pred[:mode],
      outcome_domain: pred.dig(:context, :outcome_domain) || pred[:mode],
      outcome:        entry[:outcome],
      accurate:       entry[:outcome] == :correct,
      confidence:     pred[:confidence],
      resolved_at:    entry[:at]
    }
  end
end

#resolve(prediction_id, outcome:, actual: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 32

def resolve(prediction_id, outcome:, actual: nil)
  pred = @predictions[prediction_id]
  return nil unless pred

  pred[:status] = outcome # :correct, :incorrect, :partial, :expired
  pred[:resolved_at] = Time.now.utc
  pred[:actual] = actual

  @outcomes << { prediction_id: prediction_id, outcome: outcome, at: Time.now.utc }
  @outcomes.shift while @outcomes.size > 500
  pred
end

#resolved_countObject



61
62
63
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 61

def resolved_count
  @predictions.values.count { |p| p[:status] != :pending }
end

#store(prediction) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 19

def store(prediction)
  id = prediction[:prediction_id] || SecureRandom.uuid
  prediction[:prediction_id] = id
  prediction[:created_at] ||= Time.now.utc
  prediction[:status] ||= :pending
  @predictions[id] = prediction
  id
end