Class: Zuno::ToolDefinition

Inherits:
Struct
  • Object
show all
Defined in:
lib/zuno.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:, input_schema:, execute_proc:) ⇒ ToolDefinition

Returns a new instance of ToolDefinition.



127
128
129
130
131
132
133
134
# File 'lib/zuno.rb', line 127

def initialize(name:, description:, input_schema:, execute_proc:)
  super(
    name: name.to_s,
    description: description.to_s,
    input_schema: input_schema.is_a?(Hash) ? input_schema : {},
    execute_proc: execute_proc
  )
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description

Returns:

  • the current value of description



126
127
128
# File 'lib/zuno.rb', line 126

def description
  @description
end

#execute_procObject

Returns the value of attribute execute_proc

Returns:

  • the current value of execute_proc



126
127
128
# File 'lib/zuno.rb', line 126

def execute_proc
  @execute_proc
end

#input_schemaObject

Returns the value of attribute input_schema

Returns:

  • the current value of input_schema



126
127
128
# File 'lib/zuno.rb', line 126

def input_schema
  @input_schema
end

#nameObject

Returns the value of attribute name

Returns:

  • the current value of name



126
127
128
# File 'lib/zuno.rb', line 126

def name
  @name
end

Instance Method Details

#as_provider_toolObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/zuno.rb', line 136

def as_provider_tool
  {
    type: "function",
    function: {
      name: name,
      description: description,
      parameters: input_schema
    }
  }
end

#execute(arguments) ⇒ Object

Raises:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/zuno.rb', line 147

def execute(arguments)
  raise ToolError, "Tool '#{name}' is missing an execute block" unless execute_proc.respond_to?(:call)

  symbolized_args = arguments.each_with_object({}) { |(key, value), acc| acc[key.to_sym] = value }
  parameter_kinds = execute_proc.parameters.map(&:first)
  accepts_keywords = parameter_kinds.any? { |kind| [:keyreq, :key, :keyrest].include?(kind) }
  accepts_positionals = parameter_kinds.any? { |kind| [:req, :opt, :rest].include?(kind) }

  if accepts_keywords && !accepts_positionals
    execute_proc.call(**symbolized_args)
  elsif parameter_kinds.empty?
    execute_proc.call
  else
    execute_proc.call(arguments)
  end
end