Class: Soka::AgentTool

Inherits:
Object
  • Object
show all
Includes:
Soka::AgentTools::ParamsValidator
Defined in:
lib/soka/agent_tool.rb

Overview

Base class for agent tools with parameter validation

Defined Under Namespace

Classes: ParamsDefinition

Constant Summary collapse

TYPE_MAPPING =
{
  'String' => 'string',
  'Integer' => 'integer', 'Fixnum' => 'integer', 'Bignum' => 'integer',
  'Float' => 'number', 'Numeric' => 'number',
  'TrueClass' => 'boolean', 'FalseClass' => 'boolean', 'Boolean' => 'boolean',
  'Array' => 'array',
  'Hash' => 'object', 'Object' => 'object'
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.descriptionObject (readonly)

Returns the value of attribute description.



51
52
53
# File 'lib/soka/agent_tool.rb', line 51

def description
  @description
end

.params_definitionObject (readonly)

Returns the value of attribute params_definition.



51
52
53
# File 'lib/soka/agent_tool.rb', line 51

def params_definition
  @params_definition
end

Class Method Details

.add_optional_params(schema) ⇒ Object



102
103
104
105
106
# File 'lib/soka/agent_tool.rb', line 102

def add_optional_params(schema)
  params_definition.optional_params.each do |name, config|
    schema[:properties][name.to_s] = build_property_schema(name, config)
  end
end

.add_parameters_to_schema(schema) ⇒ Object



90
91
92
93
# File 'lib/soka/agent_tool.rb', line 90

def add_parameters_to_schema(schema)
  add_required_params(schema)
  add_optional_params(schema)
end

.add_required_params(schema) ⇒ Object



95
96
97
98
99
100
# File 'lib/soka/agent_tool.rb', line 95

def add_required_params(schema)
  params_definition.required_params.each do |name, config|
    schema[:properties][name.to_s] = build_property_schema(name, config)
    schema[:required] << name.to_s
  end
end

.base_schemaObject



82
83
84
85
86
87
88
# File 'lib/soka/agent_tool.rb', line 82

def base_schema
  {
    type: 'object',
    properties: {},
    required: []
  }
end

.desc(description) ⇒ Object



53
54
55
# File 'lib/soka/agent_tool.rb', line 53

def desc(description)
  @description = description
end

.parameters_schemaObject



76
77
78
79
80
# File 'lib/soka/agent_tool.rb', line 76

def parameters_schema
  schema = base_schema
  add_parameters_to_schema(schema) if params_definition
  schema
end

.paramsObject



57
58
59
60
# File 'lib/soka/agent_tool.rb', line 57

def params(&)
  @params_definition = ParamsDefinition.new
  @params_definition.instance_eval(&) if block_given?
end

.to_hObject



68
69
70
71
72
73
74
# File 'lib/soka/agent_tool.rb', line 68

def to_h
  {
    name: tool_name,
    description: description || 'No description provided',
    parameters: parameters_schema
  }
end

.tool_nameObject



62
63
64
65
66
# File 'lib/soka/agent_tool.rb', line 62

def tool_name
  return 'anonymous' if name.nil?

  name.split('::').last.gsub(/Tool$/, '').downcase
end

Instance Method Details

#call(**params) ⇒ Object

Raises:

  • (NotImplementedError)


125
126
127
# File 'lib/soka/agent_tool.rb', line 125

def call(**params)
  raise NotImplementedError, "#{self.class} must implement #call method"
end

#execute(**params) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/soka/agent_tool.rb', line 129

def execute(**params)
  validate_params!(params)
  call(**params)
rescue ToolError
  raise # Re-raise ToolError without wrapping
rescue StandardError => e
  raise ToolError, "Error executing #{self.class.tool_name}: #{e.message}"
end