Class: DecisionAgent::Agent
- Inherits:
-
Object
- Object
- DecisionAgent::Agent
- Defined in:
- lib/decision_agent/agent.rb
Overview
Agent runs multiple evaluators over a context, scores their evaluations, and returns a single Decision with the chosen outcome and confidence.
Class Attribute Summary collapse
-
.hash_cache ⇒ Object
readonly
Returns the value of attribute hash_cache.
-
.hash_cache_max_size ⇒ Object
readonly
Returns the value of attribute hash_cache_max_size.
-
.hash_cache_mutex ⇒ Object
readonly
Returns the value of attribute hash_cache_mutex.
Instance Attribute Summary collapse
-
#audit_adapter ⇒ Object
readonly
Returns the value of attribute audit_adapter.
-
#evaluators ⇒ Object
readonly
Returns the value of attribute evaluators.
-
#scoring_strategy ⇒ Object
readonly
Returns the value of attribute scoring_strategy.
Instance Method Summary collapse
-
#decide(context:, feedback: {}) ⇒ Decision
Runs all evaluators on the context, scores results, and returns a single decision.
-
#initialize(evaluators:, scoring_strategy: nil, audit_adapter: nil, validate_evaluations: nil) ⇒ Agent
constructor
A new instance of Agent.
Constructor Details
#initialize(evaluators:, scoring_strategy: nil, audit_adapter: nil, validate_evaluations: nil) ⇒ Agent
Returns a new instance of Agent.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/decision_agent/agent.rb', line 28 def initialize(evaluators:, scoring_strategy: nil, audit_adapter: nil, validate_evaluations: nil) @evaluators = Array(evaluators) @scoring_strategy = scoring_strategy || Scoring::WeightedAverage.new @audit_adapter = audit_adapter || Audit::NullAdapter.new # Default to validating in development, skip in production for performance @validate_evaluations = validate_evaluations.nil? ? (ENV["RAILS_ENV"] != "production") : validate_evaluations validate_configuration! # Freeze instance variables for thread-safety @evaluators.freeze end |
Class Attribute Details
.hash_cache ⇒ Object (readonly)
Returns the value of attribute hash_cache.
21 22 23 |
# File 'lib/decision_agent/agent.rb', line 21 def hash_cache @hash_cache end |
.hash_cache_max_size ⇒ Object (readonly)
Returns the value of attribute hash_cache_max_size.
21 22 23 |
# File 'lib/decision_agent/agent.rb', line 21 def hash_cache_max_size @hash_cache_max_size end |
.hash_cache_mutex ⇒ Object (readonly)
Returns the value of attribute hash_cache_mutex.
21 22 23 |
# File 'lib/decision_agent/agent.rb', line 21 def hash_cache_mutex @hash_cache_mutex end |
Instance Attribute Details
#audit_adapter ⇒ Object (readonly)
Returns the value of attribute audit_adapter.
11 12 13 |
# File 'lib/decision_agent/agent.rb', line 11 def audit_adapter @audit_adapter end |
#evaluators ⇒ Object (readonly)
Returns the value of attribute evaluators.
11 12 13 |
# File 'lib/decision_agent/agent.rb', line 11 def evaluators @evaluators end |
#scoring_strategy ⇒ Object (readonly)
Returns the value of attribute scoring_strategy.
11 12 13 |
# File 'lib/decision_agent/agent.rb', line 11 def scoring_strategy @scoring_strategy end |
Instance Method Details
#decide(context:, feedback: {}) ⇒ Decision
Runs all evaluators on the context, scores results, and returns a single decision.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/decision_agent/agent.rb', line 47 def decide(context:, feedback: {}) ctx = context.is_a?(Context) ? context : Context.new(context) evaluations = collect_evaluations(ctx, feedback) raise NoEvaluationsError if evaluations.empty? # Validate all evaluations for correctness and thread-safety (optional for performance) EvaluationValidator.validate_all!(evaluations) if @validate_evaluations scored_result = @scoring_strategy.score(evaluations) decision_value = scored_result[:decision] confidence_value = scored_result[:confidence] explanations = build_explanations(evaluations, decision_value, confidence_value) audit_payload = build_audit_payload( context: ctx, evaluations: evaluations, decision: decision_value, confidence: confidence_value, feedback: feedback ) decision = Decision.new( decision: decision_value, confidence: confidence_value, explanations: explanations, evaluations: evaluations, audit_payload: audit_payload ) @audit_adapter.record(decision, ctx) decision end |