Class: Phronomy::Agent::Base
- Inherits:
-
Object
- Object
- Phronomy::Agent::Base
- Includes:
- Concerns::BeforeCompletion, Concerns::Guardrailable, Concerns::Retryable, Concerns::Suspendable, Runnable
- Defined in:
- lib/phronomy/agent/base.rb
Overview
Base class for all Phronomy agents.
Subclass this to create a conversational agent powered by an LLM. DSL class methods configure the model, instructions, tools, memory, and retry behaviour. Instance methods handle invocation.
Direct Known Subclasses
Instance Attribute Summary
Attributes included from Concerns::BeforeCompletion
Class Method Summary collapse
- ._on_compact_callback ⇒ Proc?
- ._on_compaction_trigger_callback ⇒ Proc?
- ._on_trim_callback ⇒ Proc?
-
.cache_instructions(enabled = nil) ⇒ Object
When enabled, attaches Anthropic prompt-cache markers to the system message so that the fixed instructions are served from cache on subsequent turns, reducing input-token costs.
-
.context_overhead(val = nil) ⇒ Object
Tokens reserved for the system prompt + tool definitions overhead.
-
.context_window(val = nil) ⇒ Object
Overrides the context window size used for token budget calculations.
-
.instructions(text = nil) { ... } ⇒ String, ...
Sets or reads the system instructions for this agent.
-
.max_iterations(val = nil) ⇒ Integer
Sets or reads the maximum number of LLM call cycles for ReAct agents.
-
.max_output_tokens(val = nil) ⇒ Object
Tokens to reserve for the model's output.
-
.model(name = nil) ⇒ String?
Sets or reads the LLM model identifier for this agent.
-
.on_compact {|ctx| ... } ⇒ Object
Registers a callback that performs the actual compaction when the +on_compaction_trigger+ callback fires.
-
.on_compaction_trigger {|ctx| ... } ⇒ Boolean
Registers a callback that decides whether compaction should run.
-
.on_trim {|ctx| ... } ⇒ Object
Registers a callback that is invoked before every LLM call so the application can remove stale or irrelevant messages from the conversation history.
-
.provider(name = nil) ⇒ Symbol?
Sets or reads the LLM provider for this agent.
-
.static_knowledge(*sources) ⇒ Object
Registers one or more static knowledge sources on the agent class.
-
.static_knowledge_sources ⇒ Array<Phronomy::KnowledgeSource::Base>
Returns the registered static knowledge sources.
-
.temperature(val = nil) ⇒ Float?
Sets or reads the sampling temperature sent to the LLM.
-
.tool_aliases ⇒ Hash{Class => String}
Returns the alias map registered via the hash form of .tools.
-
.tools(*args) ⇒ Object
Registers tool classes for this agent.
Instance Method Summary collapse
-
#_add_handoff_tool(tool_class) ⇒ self
Registers an anonymous handoff tool class on this agent instance.
-
#_handoff_tools ⇒ Array<Class>
Returns handoff tool classes registered on this instance by Runner.
-
#context_version_cache ⇒ Object
private
Returns the Context::ContextVersionCache for the current thread.
-
#invoke(input, messages: [], thread_id: nil, config: {}) ⇒ Hash
Invokes the agent with the given input and returns a result Hash.
-
#stream(input, messages: [], thread_id: nil, config: {}) {|Phronomy::Agent::StreamEvent| ... } ⇒ Hash
Streaming version of #invoke.
Methods included from Concerns::Suspendable
#on_approval_required, #resume
Methods included from Concerns::BeforeCompletion
Methods included from Concerns::Guardrailable
#add_input_guardrail, #add_output_guardrail
Methods included from Concerns::Retryable
Methods included from Runnable
Class Method Details
._on_compact_callback ⇒ Proc?
261 262 263 |
# File 'lib/phronomy/agent/base.rb', line 261 def _on_compact_callback @on_compact_callback end |
._on_compaction_trigger_callback ⇒ Proc?
239 240 241 |
# File 'lib/phronomy/agent/base.rb', line 239 def _on_compaction_trigger_callback @on_compaction_trigger_callback end |
._on_trim_callback ⇒ Proc?
216 217 218 |
# File 'lib/phronomy/agent/base.rb', line 216 def _on_trim_callback @on_trim_callback end |
.cache_instructions(enabled = nil) ⇒ Object
When enabled, attaches Anthropic prompt-cache markers to the system message so that the fixed instructions are served from cache on subsequent turns, reducing input-token costs.
Only has an effect when the agent also declares provider :anthropic.
The cache_control field is provider-specific (the format differs
between Anthropic direct, Bedrock, etc.), so the agent must explicitly
declare its provider via the DSL rather than having it inferred from
the model name.
280 281 282 283 284 285 286 |
# File 'lib/phronomy/agent/base.rb', line 280 def cache_instructions(enabled = nil) if enabled.nil? @cache_instructions else @cache_instructions = enabled end end |
.context_overhead(val = nil) ⇒ Object
Tokens reserved for the system prompt + tool definitions overhead. Subtract this from the context window before computing the memory budget.
327 328 329 330 331 332 333 |
# File 'lib/phronomy/agent/base.rb', line 327 def context_overhead(val = nil) if val.nil? @context_overhead || 0 else @context_overhead = val.to_i end end |
.context_window(val = nil) ⇒ Object
Overrides the context window size used for token budget calculations. When set, this value takes precedence over the RubyLLM model registry, which is useful for locally-hosted models (e.g. LM Studio) where the actually-loaded context length may differ from the catalogue value.
312 313 314 315 316 317 318 |
# File 'lib/phronomy/agent/base.rb', line 312 def context_window(val = nil) if val.nil? @context_window else @context_window = val.to_i end end |
.instructions(text = nil) { ... } ⇒ String, ...
Sets or reads the system instructions for this agent. Accepts a String, a PromptTemplate, or a block (Proc). When used as a reader (no argument, no block), returns the stored value.
73 74 75 76 77 78 79 80 |
# File 'lib/phronomy/agent/base.rb', line 73 def instructions(text = nil, &block) if text || block_given? @instructions = text || block else return @instructions if instance_variable_defined?(:@instructions) superclass.respond_to?(:instructions) ? superclass.instructions : nil end end |
.max_iterations(val = nil) ⇒ Integer
Sets or reads the maximum number of LLM call cycles for ReAct agents. Each tool call and follow-up counts as one iteration. Defaults to 10.
168 169 170 171 172 173 174 |
# File 'lib/phronomy/agent/base.rb', line 168 def max_iterations(val = nil) if val @max_iterations = val else @max_iterations || 10 end end |
.max_output_tokens(val = nil) ⇒ Object
Tokens to reserve for the model's output. When nil, the model's max_output_tokens from the registry is used.
295 296 297 298 299 300 301 |
# File 'lib/phronomy/agent/base.rb', line 295 def max_output_tokens(val = nil) if val.nil? @max_output_tokens else @max_output_tokens = val.to_i end end |
.model(name = nil) ⇒ String?
Sets or reads the LLM model identifier for this agent. When called without an argument, returns the stored model or the global default from Phronomy.configuration.
50 51 52 53 54 55 56 |
# File 'lib/phronomy/agent/base.rb', line 50 def model(name = nil) if name @model = name else @model || Phronomy.configuration.default_model end end |
.on_compact {|ctx| ... } ⇒ Object
Registers a callback that performs the actual compaction when the +on_compaction_trigger+ callback fires. The block receives a Context::CompactionContext and should call +ctx.compact+ to specify which messages to summarise.
256 257 258 |
# File 'lib/phronomy/agent/base.rb', line 256 def on_compact(&block) @on_compact_callback = block end |
.on_compaction_trigger {|ctx| ... } ⇒ Boolean
Registers a callback that decides whether compaction should run. Evaluated before every LLM call (after on_trim). If the block returns truthy AND an +on_compact+ callback is also registered, the compact pipeline is executed.
The block receives a read-only Context::TriggerContext.
234 235 236 |
# File 'lib/phronomy/agent/base.rb', line 234 def on_compaction_trigger(&block) @on_compaction_trigger_callback = block end |
.on_trim {|ctx| ... } ⇒ Object
Registers a callback that is invoked before every LLM call so the application can remove stale or irrelevant messages from the conversation history.
The block receives a Context::TrimContext and may call +ctx.remove(seqs)+ to drop messages by seq number. Changes affect only the current invocation; the underlying memory store is unchanged.
211 212 213 |
# File 'lib/phronomy/agent/base.rb', line 211 def on_trim(&block) @on_trim_callback = block end |
.provider(name = nil) ⇒ Symbol?
Sets or reads the LLM provider for this agent. Required when using a model not registered in RubyLLM's model registry (e.g. locally-hosted models via LM Studio or Ollama).
133 134 135 136 137 138 139 140 |
# File 'lib/phronomy/agent/base.rb', line 133 def provider(name = nil) if name @provider = name else return @provider if instance_variable_defined?(:@provider) superclass.respond_to?(:provider) ? superclass.provider : nil end end |
.static_knowledge(*sources) ⇒ Object
Registers one or more static knowledge sources on the agent class. Static sources are fetched once per agent instance and their content is cached in ContextVersionCache keyed by a fingerprint of the instruction text + source content. The cache is invalidated automatically when the fingerprint changes (e.g. because a source was updated).
187 188 189 |
# File 'lib/phronomy/agent/base.rb', line 187 def static_knowledge(*sources) @static_knowledge_sources = sources.flatten end |
.static_knowledge_sources ⇒ Array<Phronomy::KnowledgeSource::Base>
Returns the registered static knowledge sources.
193 194 195 |
# File 'lib/phronomy/agent/base.rb', line 193 def static_knowledge_sources @static_knowledge_sources || [] end |
.temperature(val = nil) ⇒ Float?
Sets or reads the sampling temperature sent to the LLM. When nil, the provider's default is used.
151 152 153 154 155 156 157 |
# File 'lib/phronomy/agent/base.rb', line 151 def temperature(val = nil) if val @temperature = val else @temperature end end |
.tool_aliases ⇒ Hash{Class => String}
Returns the alias map registered via the hash form of .tools.
118 119 120 |
# File 'lib/phronomy/agent/base.rb', line 118 def tool_aliases @tool_aliases ||= {} end |
.tools(*args) ⇒ Object
Registers tool classes for this agent.
Accepts either a splat of classes (backward-compatible) or a Hash mapping each class to an explicit alias name (String) or nil (use tool's own name). The alias form is useful when two tools share the same auto-generated name (e.g. two SearchTool classes from different modules).
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/phronomy/agent/base.rb', line 98 def tools(*args) if args.empty? if instance_variable_defined?(:@tools) return @tools end return superclass.respond_to?(:tools) ? superclass.tools : [] end if args.length == 1 && args.first.is_a?(Hash) hash = args.first @tools = hash.keys @tool_aliases = hash.transform_values { |v| v&.to_s }.reject { |_, v| v.nil? } else @tools = args @tool_aliases = {} end end |
Instance Method Details
#_add_handoff_tool(tool_class) ⇒ self
Registers an anonymous handoff tool class on this agent instance. Called by Runner during construction when routes are configured.
340 341 342 343 344 |
# File 'lib/phronomy/agent/base.rb', line 340 def _add_handoff_tool(tool_class) @_handoff_tools ||= [] @_handoff_tools << tool_class self end |
#_handoff_tools ⇒ Array<Class>
Returns handoff tool classes registered on this instance by Runner.
348 349 350 |
# File 'lib/phronomy/agent/base.rb', line 348 def _handoff_tools @_handoff_tools || [] end |
#context_version_cache ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the Context::ContextVersionCache for the current thread.
415 416 417 |
# File 'lib/phronomy/agent/base.rb', line 415 def context_version_cache (Thread.current[:phronomy_context_version_caches] ||= {})[object_id] end |
#invoke(input, messages: [], thread_id: nil, config: {}) ⇒ Hash
Invokes the agent with the given input and returns a result Hash. Applies the retry policy configured via retry_policy when transient errors occur. GuardrailError is never retried.
384 385 386 |
# File 'lib/phronomy/agent/base.rb', line 384 def invoke(input, messages: [], thread_id: nil, config: {}) _invoke_impl(input, messages: , thread_id: thread_id, config: config) end |
#stream(input, messages: [], thread_id: nil, config: {}) {|Phronomy::Agent::StreamEvent| ... } ⇒ Hash
Streaming version of #invoke. Yields StreamEvent objects as they are produced by the underlying LLM.
Events emitted (in order): :token — each content delta from the LLM :tool_call — when the LLM requests a tool (ReactAgent subclasses only) :tool_result — after a tool completes (ReactAgent subclasses only) :done — final event carrying output, messages, and usage :error — if an unrecoverable error occurs
404 405 406 407 408 409 410 411 |
# File 'lib/phronomy/agent/base.rb', line 404 def stream(input, messages: [], thread_id: nil, config: {}, &block) return invoke(input, messages: , thread_id: thread_id, config: config) unless block _stream_impl(input, messages: , thread_id: thread_id, config: config, &block) rescue => e block&.call(StreamEvent.new(type: :error, payload: {error: e})) raise end |