Class: OmniAgent::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/omni_agent/tool.rb,
lib/omni_agent/tool/schema_builder.rb

Defined Under Namespace

Classes: SchemaBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTool

Returns a new instance of Tool.



165
166
167
# File 'lib/omni_agent/tool.rb', line 165

def initialize
  @context = {}
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



163
164
165
# File 'lib/omni_agent/tool.rb', line 163

def context
  @context
end

Class Method Details

.configured_tagsObject



20
21
22
# File 'lib/omni_agent/tool.rb', line 20

def configured_tags
  tags
end

.description(text = nil) ⇒ Object



4
5
6
7
# File 'lib/omni_agent/tool.rb', line 4

def description(text = nil)
  @description = text if text
  @description || "No description provided."
end

.input(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/omni_agent/tool.rb', line 24

def input(&block)
  if block_given?
    builder = SchemaBuilder.new
    builder.instance_eval(&block)

    @properties = builder.properties
    @required = builder.required_fields
    @validators = builder.validators
    @polymorphics = builder.polymorphics
  end
end

.json_schemaObject



36
37
38
39
40
41
42
43
# File 'lib/omni_agent/tool.rb', line 36

def json_schema
  {
    type: "object",
    properties: @properties || {},
    required: @required || [],
    additionalProperties: false
  }
end

.metadata(options = nil) ⇒ Object



9
10
11
12
# File 'lib/omni_agent/tool.rb', line 9

def (options = nil)
  @metadata = options if options
  @metadata || {}
end

.parse_arguments(arguments_hash) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/omni_agent/tool.rb', line 45

def parse_arguments(arguments_hash)
  kwargs = arguments_hash.transform_keys(&:to_sym)
  valid_keys = (@properties || {}).keys.map(&:to_sym)
  filtered = kwargs.slice(*valid_keys)

  validate_required!(filtered)
  validate_enums!(filtered)
  validate_constraints!(filtered)
  run_custom_validators!(filtered)
  resolve_polymorphics!(filtered)

  filtered
end

.stops_generation(value = true) ⇒ Object



59
60
61
# File 'lib/omni_agent/tool.rb', line 59

def stops_generation(value = true)
  @stops_generation = !!value
end

.stops_generation?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/omni_agent/tool.rb', line 63

def stops_generation?
  @stops_generation == true
end

.tags(*tag_names) ⇒ Object



14
15
16
17
18
# File 'lib/omni_agent/tool.rb', line 14

def tags(*tag_names)
  return @tags || [] if tag_names.empty?

  @tags = (tags + normalize_tags(tag_names)).uniq
end

Instance Method Details

#execute(**args) ⇒ Object

Raises:

  • (NotImplementedError)


182
183
184
# File 'lib/omni_agent/tool.rb', line 182

def execute(**args)
  raise NotImplementedError, "#{self.class.name} must implement #execute"
end

#invoke(arguments_hash) ⇒ Object



169
170
171
172
# File 'lib/omni_agent/tool.rb', line 169

def invoke(arguments_hash)
  filtered_kwargs = self.class.parse_arguments(arguments_hash)
  execute(**filtered_kwargs)
end

#stop_generation!Object



174
175
176
# File 'lib/omni_agent/tool.rb', line 174

def stop_generation!
  @stops_generation_instance = true
end

#stops_generation?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/omni_agent/tool.rb', line 178

def stops_generation?
  @stops_generation_instance == true
end