Class: Phronomy::Tool::AgentTool

Inherits:
Base
  • Object
show all
Defined in:
lib/phronomy/tool/agent_tool.rb

Overview

Wraps a Phronomy::Agent::Base subclass as a callable tool so that a parent ReactAgent (or any agent that supports tools) can delegate sub-tasks to a fully-capable agent.

Use AgentTool.from_agent to generate a concrete tool class. The generated class is anonymous; assign it to a constant when you need a stable name.

Examples:

Wrap an existing agent

SummarizerTool = Phronomy::Tool::AgentTool.from_agent(
  SummarizerAgent,
  tool_name:   "summarize",
  description: "Summarizes a long text and returns a brief summary"
)

class OrchestratorAgent < Phronomy::Agent::ReactAgent
  model "openai/gpt-4o-mini"
  instructions "You are an orchestrator that delegates to specialist agents."
  tools SummarizerTool
end

Class Method Summary collapse

Methods inherited from Base

#call, #execute, #name, on_error, on_schema_error, param, param_enums, #params_schema, requires_approval, #requires_approval?, retry_on, retry_policies, scope, tool_name

Class Method Details

.from_agent(agent_class, tool_name: nil, description: nil) ⇒ Class

Generates a Phronomy::Tool::AgentTool subclass that delegates #execute to an instance of +agent_class+.

Parameters:

  • agent_class (Class)

    a Phronomy::Agent::Base subclass

  • tool_name (String, nil) (defaults to: nil)

    function name exposed to the LLM; defaults to a snake_case derivation of the agent class name

  • description (String, nil) (defaults to: nil)

    description exposed to the LLM; defaults to "Delegates to "

Returns:

  • (Class)

    an anonymous Phronomy::Tool::AgentTool subclass

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/phronomy/tool/agent_tool.rb', line 38

def from_agent(agent_class, tool_name: nil, description: nil)
  raise ArgumentError, "agent_class must be a Class" unless agent_class.is_a?(Class)

  klass = Class.new(self)

  effective_name = tool_name || derive_name(agent_class)
  effective_desc = description || "Delegates to #{agent_class.name || "an agent"}"

  klass.tool_name(effective_name)
  klass.description(effective_desc)

  klass.define_method(:execute) do |input:|
    result = agent_class.new.invoke(input)
    result[:output].to_s
  end

  klass
end