Class: Zephira::Tools

Inherits:
Object
  • Object
show all
Defined in:
lib/zephira/tools.rb,
lib/zephira/tools/shell.rb,
lib/zephira/tools/base_tool.rb,
lib/zephira/tools/read_file.rb,
lib/zephira/tools/web_search.rb,
lib/zephira/tools/code_search.rb,
lib/zephira/tools/delete_file.rb,
lib/zephira/tools/memory_list.rb,
lib/zephira/tools/memory_read.rb,
lib/zephira/tools/update_file.rb,
lib/zephira/tools/http_request.rb,
lib/zephira/tools/memory_store.rb,
lib/zephira/tools/memory_write.rb,
lib/zephira/tools/memory_delete.rb,
lib/zephira/tools/list_directory.rb

Defined Under Namespace

Classes: BaseTool, CodeSearch, DeleteFile, HttpRequest, ListDirectory, MemoryDelete, MemoryList, MemoryRead, MemoryStore, MemoryWrite, ReadFile, Shell, ToolExecutionError, ToolNotFoundError, ToolResultError, UpdateFile, WebSearch

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(paths:) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/zephira/tools.rb', line 9

def self.load(paths:)
  paths.each do |path|
    Dir.glob(File.join(path, "**", "*.rb")).each do |file|
      require File.expand_path(file)
    end
  end
  new
end

Instance Method Details

#constantsObject



18
19
20
21
22
# File 'lib/zephira/tools.rb', line 18

def constants
  @constants ||= ::Zephira::Tools.constants(false)
    .map { |const_sym| ::Zephira::Tools.const_get(const_sym) }
    .select { |const| const.is_a?(Class) && const < ::Zephira::Tools::BaseTool }
end

#find!(name) ⇒ Object

Raises:



52
53
54
55
56
# File 'lib/zephira/tools.rb', line 52

def find!(name)
  tool = constants.find { |candidate| candidate.name == name }
  raise ToolNotFoundError, "Tool not found: #{name}" if tool.nil?
  tool
end

#read_only?(name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/zephira/tools.rb', line 47

def read_only?(name)
  tool = constants.find { |candidate| candidate.name == name }
  tool && tool.respond_to?(:read_only?) && tool.read_only?
end

#run(name:, args:, agent:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zephira/tools.rb', line 34

def run(name:, args:, agent:)
  tool = find!(name)

  result = begin
    tool.run(args: args, agent: agent)
  rescue => exception
    raise ToolExecutionError, "Encountered an error when executing tool #{name}: #{exception.message}"
  end

  validate_result(result)
  result
end

#to_hObject



24
25
26
27
28
29
30
31
32
# File 'lib/zephira/tools.rb', line 24

def to_h
  constants.map do |tool|
    {
      name: tool.name,
      description: tool.description,
      parameters: tool.parameters
    }
  end
end