Class: Riffer::Agent::Context
- Inherits:
-
Object
- Object
- Riffer::Agent::Context
- Defined in:
- lib/riffer/agent/context.rb
Overview
Typed value object wrapping the runtime context Hash held by a Riffer::Agent. Exposes typed skills, token_usage, mcp_progressive_tools, and discovered_tools accessors while preserving #[] / #dig for caller-provided keys.
Constant Summary collapse
- RESERVED_KEYS =
[:skills, :token_usage, :mcp_progressive_tools, :discovered_tools].freeze
Instance Method Summary collapse
-
#[](key) ⇒ Object
Hash-style read, preserved so tools can pull caller-provided keys via
context[:agent]. -
#dig(*keys) ⇒ Object
– : (*Symbol) -> untyped.
-
#discover_tools(tools) ⇒ Object
Accumulates newly discovered MCP tool classes, deduplicating by name.
-
#discovered_tools ⇒ Object
MCP tool classes discovered during progressive search.
-
#discovered_tools=(value) ⇒ Object
Sets the discovered tools array.
-
#initialize(data = {}) ⇒ Context
constructor
Builds a new context.
-
#mcp_progressive_tools ⇒ Object
Auth-wrapped MCP tool classes for progressive discovery, or
nil. -
#mcp_progressive_tools=(value) ⇒ Object
Sets progressive MCP tools.
-
#skills ⇒ Object
The agent’s resolved
Riffer::Skills::Context, ornilwhen skills are not configured. -
#skills=(value) ⇒ Object
Sets the resolved skills context.
-
#to_h ⇒ Object
Returns a copy of the underlying Hash; mutating it does not affect this context.
-
#token_usage ⇒ Object
The cumulative
Riffer::Providers::TokenUsageacross every Run on this agent, ornilbefore the first response is recorded. -
#token_usage=(value) ⇒ Object
Sets the cumulative token usage.
Constructor Details
#initialize(data = {}) ⇒ Context
Builds a new context. The caller Hash is duped so later caller mutations don’t leak in. Raises Riffer::ArgumentError if it contains a reserved key. – : (?Hash[Symbol, untyped]) -> void
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/riffer/agent/context.rb', line 16 def initialize(data = {}) reserved = data.keys & RESERVED_KEYS if reserved.any? raise Riffer::ArgumentError, "Reserved keys cannot be passed in context: #{reserved.join(", ")}" end @data = data.dup @data[:skills] = nil @data[:token_usage] = nil @data[:mcp_progressive_tools] = nil @data[:discovered_tools] = nil end |
Instance Method Details
#[](key) ⇒ Object
Hash-style read, preserved so tools can pull caller-provided keys via context[:agent]. – : (Symbol) -> untyped
76 77 78 |
# File 'lib/riffer/agent/context.rb', line 76 def [](key) @data[key] end |
#dig(*keys) ⇒ Object
– : (*Symbol) -> untyped
136 137 138 |
# File 'lib/riffer/agent/context.rb', line 136 def dig(*keys) @data.dig(*keys) end |
#discover_tools(tools) ⇒ Object
129 130 131 132 |
# File 'lib/riffer/agent/context.rb', line 129 def discover_tools(tools) existing = @data[:discovered_tools] || [] @data[:discovered_tools] = (existing + tools).uniq(&:name) end |
#discovered_tools ⇒ Object
MCP tool classes discovered during progressive search. Accumulates across generate calls and is merged into the active tool list on every LLM call. – : () -> Array?
106 107 108 |
# File 'lib/riffer/agent/context.rb', line 106 def discovered_tools @data[:discovered_tools] end |
#discovered_tools=(value) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/riffer/agent/context.rb', line 113 def discovered_tools=(value) valid = value.nil? || ( value.is_a?(Array) && value.all? { |tool| tool.is_a?(Class) && tool < Riffer::Tool } ) unless valid raise Riffer::ArgumentError, "discovered_tools must be an Array of Riffer::Tool subclasses or nil, got #{value.class}" end @data[:discovered_tools] = value end |
#mcp_progressive_tools ⇒ Object
Auth-wrapped MCP tool classes for progressive discovery, or nil. – : () -> Array?
83 84 85 |
# File 'lib/riffer/agent/context.rb', line 83 def mcp_progressive_tools @data[:mcp_progressive_tools] end |
#mcp_progressive_tools=(value) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/riffer/agent/context.rb', line 90 def mcp_progressive_tools=(value) valid = value.nil? || ( value.is_a?(Array) && value.all? { |tool| tool.is_a?(Class) && tool < Riffer::Tool } ) unless valid raise Riffer::ArgumentError, "mcp_progressive_tools must be an Array of Riffer::Tool subclasses or nil, got #{value.class}" end @data[:mcp_progressive_tools] = value end |
#skills ⇒ Object
The agent’s resolved Riffer::Skills::Context, or nil when skills are not configured.
– : () -> Riffer::Skills::Context?
35 36 37 |
# File 'lib/riffer/agent/context.rb', line 35 def skills @data[:skills] end |
#skills=(value) ⇒ Object
Sets the resolved skills context. Raises Riffer::ArgumentError on an invalid value. – : (Riffer::Skills::Context?) -> Riffer::Skills::Context?
43 44 45 46 47 48 49 |
# File 'lib/riffer/agent/context.rb', line 43 def skills=(value) unless value.nil? || value.is_a?(Riffer::Skills::Context) raise Riffer::ArgumentError, "skills must be a Riffer::Skills::Context or nil, got #{value.class}" end @data[:skills] = value end |
#to_h ⇒ Object
Returns a copy of the underlying Hash; mutating it does not affect this context. – : () -> Hash[Symbol, untyped]
144 145 146 |
# File 'lib/riffer/agent/context.rb', line 144 def to_h @data.dup end |
#token_usage ⇒ Object
The cumulative Riffer::Providers::TokenUsage across every Run on this agent, or nil before the first response is recorded.
– : () -> Riffer::Providers::TokenUsage?
56 57 58 |
# File 'lib/riffer/agent/context.rb', line 56 def token_usage @data[:token_usage] end |
#token_usage=(value) ⇒ Object
Sets the cumulative token usage. Raises Riffer::ArgumentError on an invalid value. – : (Riffer::Providers::TokenUsage?) -> Riffer::Providers::TokenUsage?
64 65 66 67 68 69 70 |
# File 'lib/riffer/agent/context.rb', line 64 def token_usage=(value) unless value.nil? || value.is_a?(Riffer::Providers::TokenUsage) raise Riffer::ArgumentError, "token_usage must be a Riffer::Providers::TokenUsage or nil, got #{value.class}" end @data[:token_usage] = value end |