Class: PromptBuilder::ToolRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_builder/tool_registry.rb

Overview

Registry for tool definitions and their handler callables.

Instance Method Summary collapse

Constructor Details

#initializeToolRegistry

Create a new ToolRegistry.



7
8
9
10
# File 'lib/prompt_builder/tool_registry.rb', line 7

def initialize
  @definitions = {}
  @handlers = {}
end

Instance Method Details

#definition_for(name) ⇒ Tools::Definition?

Look up a definition by name.

Parameters:

  • name (String)

    the tool name

Returns:



51
52
53
# File 'lib/prompt_builder/tool_registry.rb', line 51

def definition_for(name)
  @definitions[name]
end

#definitionsArray<Tools::Definition>

Return all tool definitions.

Returns:



58
59
60
# File 'lib/prompt_builder/tool_registry.rb', line 58

def definitions
  @definitions.values
end

#handler_for(name) ⇒ #call?

Look up a handler by name.

Parameters:

  • name (String)

    the tool name

Returns:

  • (#call, nil)

    the handler, or nil if not found



43
44
45
# File 'lib/prompt_builder/tool_registry.rb', line 43

def handler_for(name)
  @handlers[name]
end

#invoke(name, arguments) ⇒ Object

Invoke a tool by name with the given arguments.

Parameters:

  • name (String)

    the tool name

  • arguments (Hash)

    the parsed arguments

Returns:

  • (Object)

    the raw tool handler return value

Raises:



68
69
70
71
72
73
# File 'lib/prompt_builder/tool_registry.rb', line 68

def invoke(name, arguments)
  handler = handler_for(name)
  raise ToolNotFoundError, "No handler registered for tool: #{name.inspect}" unless handler

  handler.call(arguments)
end

#register(name, description: nil, parameters: nil, strict: false, callable: nil, **extra) {|Hash| ... } ⇒ Tools::Definition

Register a tool with its definition and handler.

Parameters:

  • name (String, Symbol)

    the tool name

  • description (String, nil) (defaults to: nil)

    the tool description

  • parameters (Hash, nil) (defaults to: nil)

    the JSON Schema for parameters

  • strict (Boolean) (defaults to: false)

    whether strict mode is enabled

  • callable (#call, nil) (defaults to: nil)

    a callable handler (alternative to block)

  • extra (Hash)

    provider-specific extra keyword arguments (e.g. cache_control)

Yields:

  • (Hash)

    the parsed arguments when the tool is invoked

Yield Returns:

  • (Object)

    the tool output (String, Hash, Array, or any object)

Returns:

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prompt_builder/tool_registry.rb', line 23

def register(name, description: nil, parameters: nil, strict: false, callable: nil, **extra, &handler)
  name = name.to_s
  raise ArgumentError.new("Tool name is required") if name.empty?

  definition = Tools::Definition.new(
    name: name,
    description: description,
    parameters: parameters,
    strict: strict,
    **extra
  )
  @definitions[name] = definition
  @handlers[name] = callable || handler
  definition
end