Class: Phronomy::Agent::ToolDefinitionSet

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/agent/tool_definition_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime_tools:, definitions:) ⇒ ToolDefinitionSet

Returns a new instance of ToolDefinitionSet.



48
49
50
51
52
# File 'lib/phronomy/agent/tool_definition_set.rb', line 48

def initialize(runtime_tools:, definitions:)
  @runtime_tools = runtime_tools
  @definitions = Immutable.copy(definitions)
  freeze
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



6
7
8
# File 'lib/phronomy/agent/tool_definition_set.rb', line 6

def definitions
  @definitions
end

#runtime_toolsObject (readonly)

Returns the value of attribute runtime_tools.



6
7
8
# File 'lib/phronomy/agent/tool_definition_set.rb', line 6

def runtime_tools
  @runtime_tools
end

Class Method Details

.build(agent) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/phronomy/agent/tool_definition_set.rb', line 8

def self.build(agent)
  runtime_tools = (agent.class.tools + agent.send(:_handoff_tools)).freeze
  definitions = runtime_tools.map do |tool_class|
    prepared = agent.send(:prepare_tool_class, tool_class)
    tool = prepared.is_a?(Class) ? prepared.new : prepared
    {
      "name" => tool.name.to_s,
      "description" => tool.description.to_s,
      "parameters_schema" => normalize(
        tool.respond_to?(:parameters_schema) ? tool.parameters_schema : {}
      ),
      "provider_options" => normalize(
        tool.respond_to?(:provider_options) ? tool.provider_options : {}
      )
    }
  end.freeze
  new(runtime_tools: runtime_tools, definitions: definitions)
end

.normalize(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/phronomy/agent/tool_definition_set.rb', line 27

def self.normalize(value)
  case value
  when Hash
    value.to_h { |key, child| [key.to_s, normalize(child)] }
  when Array
    value.map { |child| normalize(child) }
  when Symbol
    value.to_s
  when String, Integer, Float, TrueClass, FalseClass, NilClass
    value
  else
    if value.respond_to?(:to_json_schema)
      normalize(value.to_json_schema)
    elsif value.respond_to?(:to_h)
      normalize(value.to_h)
    else
      raise ArgumentError, "unsupported tool definition value: #{value.class}"
    end
  end
end