class Riffer::Guardrails::Runner
Executes guardrails sequentially and manages the processing pipeline.
The runner processes guardrails in order, passing the output of each to the next. If any guardrail blocks, execution stops and a tripwire is returned.
runner = Runner.new(guardrail_configs, phase: :before, context: tool_context) data, tripwire, modifications = runner.run(messages)
Attributes
The context passed to guardrails.
The guardrail configs to execute.
The execution phase (:before or :after).
Public Class Methods
Source
# File lib/riffer/guardrails/runner.rb, line 29 def initialize(guardrail_configs, phase:, context: nil) @guardrail_configs = guardrail_configs @phase = phase @context = context end
Creates a new runner.
+guardrail_configs+ - configs with :class and :options keys. +phase+ - :before or :after. +context+ - optional context to pass to guardrails.
: (Array[Hash[Symbol, untyped]], phase: Symbol, ?context: untyped) -> void
Public Instance Methods
Source
# File lib/riffer/guardrails/runner.rb, line 44 def run(data, messages: nil) current_data = data modifications = [] #: Array[Riffer::Guardrails::Modification] guardrail_configs.each do |config| guardrail = instantiate_guardrail(config) result = execute_guardrail(guardrail, current_data, messages: messages) if result.block? tripwire = Riffer::Guardrails::Tripwire.new( reason: result.data, guardrail: guardrail.class, phase: phase, metadata: result.metadata ) return [current_data, tripwire, modifications] end if result.transform? modifications << Riffer::Guardrails::Modification.new( guardrail: guardrail.class, phase: phase, message_indices: detect_changed_indices(current_data, result.data) ) end current_data = result.data end [current_data, nil, modifications] end
Runs the guardrails sequentially.
For before phase, data should be an array of messages. For after phase, data should be a response and messages must be provided.
+data+ - the data to process (messages for before, response for after). +messages+ - the conversation messages (required for after phase).
: (untyped, ?messages: Array?) -> [untyped, Riffer::Guardrails::Tripwire?, Array]