Class: Zuno::ToolDefinition
- Inherits:
-
Struct
- Object
- Struct
- Zuno::ToolDefinition
- Defined in:
- lib/zuno.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#execute_proc ⇒ Object
Returns the value of attribute execute_proc.
-
#input_schema ⇒ Object
Returns the value of attribute input_schema.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #as_provider_tool ⇒ Object
- #execute(arguments) ⇒ Object
-
#initialize(name:, description:, input_schema:, execute_proc:) ⇒ ToolDefinition
constructor
A new instance of ToolDefinition.
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
#description ⇒ Object
Returns the value of attribute description
126 127 128 |
# File 'lib/zuno.rb', line 126 def description @description end |
#execute_proc ⇒ Object
Returns the value of attribute execute_proc
126 127 128 |
# File 'lib/zuno.rb', line 126 def execute_proc @execute_proc end |
#input_schema ⇒ Object
Returns the value of attribute input_schema
126 127 128 |
# File 'lib/zuno.rb', line 126 def input_schema @input_schema end |
#name ⇒ Object
Returns the value of attribute name
126 127 128 |
# File 'lib/zuno.rb', line 126 def name @name end |
Instance Method Details
#as_provider_tool ⇒ Object
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
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 |