Class: Kreator::ToolRegistry
- Inherits:
-
Object
- Object
- Kreator::ToolRegistry
- Includes:
- Enumerable
- Defined in:
- lib/kreator/tool_registry.rb
Defined Under Namespace
Classes: Error
Class Method Summary collapse
Instance Method Summary collapse
- #call(tool_call, context:, signal: nil) ⇒ Object
- #each ⇒ Object
- #fetch(name) ⇒ Object
-
#initialize(tools = []) ⇒ ToolRegistry
constructor
A new instance of ToolRegistry.
- #names ⇒ Object
- #register(tool) ⇒ Object
- #select_names(names) ⇒ Object
Constructor Details
#initialize(tools = []) ⇒ ToolRegistry
Returns a new instance of ToolRegistry.
11 12 13 14 |
# File 'lib/kreator/tool_registry.rb', line 11 def initialize(tools = []) @tools = {} tools.each { |tool| register(tool) } end |
Class Method Details
.default(bash_timeout: ToolContext::DEFAULT_BASH_TIMEOUT) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/kreator/tool_registry.rb', line 16 def self.default(bash_timeout: ToolContext::DEFAULT_BASH_TIMEOUT) new( [ Tools::Read.new, Tools::Grep.new, Tools::Find.new, Tools::Ls.new, Tools::Edit.new, Tools::Write.new, Tools::Bash.new(default_timeout: bash_timeout), Tools::Agent.new ] ) end |
Instance Method Details
#call(tool_call, context:, signal: nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/kreator/tool_registry.rb', line 55 def call(tool_call, context:, signal: nil) context.ensure_not_cancelled!(signal) tool = fetch(tool_call.name) args = tool_call.arguments || {} validate!(tool, args) result = tool.call(args: args, context: context, signal: signal) context.ensure_not_cancelled!(signal) normalize_result(result, tool_call, tool) rescue StandardError => e structured_error = error_payload(e) ToolResult.new( tool_call_id: tool_call.id, name: tool_call.name, content: "#{structured_error.fetch('code')}: #{e.}", status: "error", error: structured_error ) end |
#each ⇒ Object
39 40 41 |
# File 'lib/kreator/tool_registry.rb', line 39 def each(&) @tools.values.each(&) end |
#fetch(name) ⇒ Object
47 48 49 |
# File 'lib/kreator/tool_registry.rb', line 47 def fetch(name) @tools.fetch(name.to_s) { raise Error, "unknown tool: #{name}" } end |
#names ⇒ Object
43 44 45 |
# File 'lib/kreator/tool_registry.rb', line 43 def names @tools.keys end |
#register(tool) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/kreator/tool_registry.rb', line 31 def register(tool) raise ArgumentError, "tool name is required" if tool.name.to_s.empty? raise Error, "duplicate tool: #{tool.name}" if @tools.key?(tool.name) @tools[tool.name] = tool self end |
#select_names(names) ⇒ Object
51 52 53 |
# File 'lib/kreator/tool_registry.rb', line 51 def select_names(names) self.class.new(Array(names).map { |name| fetch(name) }) end |