Module: OllamaChat::Tools

Defined in:
lib/ollama_chat/tools.rb

Overview

A module that provides tool registration and management for OllamaChat.

The Tools module serves as a registry for available tools that can be invoked during chat conversations. It maintains a collection of registered tools and provides methods for registering new tools and accessing the complete set of available tools for use in chat interactions.

Defined Under Namespace

Modules: Concern Classes: RunTests

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registered ⇒ Hash

The registered attribute reader

Returns:

  • (Hash) —

    the registered tools hash containing all available tools (as class or instance).



15
16
17
# File 'lib/ollama_chat/tools.rb', line 15

def registered
  @registered
end

Class Method Details

.register(tool) ⇒ OllamaChat::Tools

Registers a tool class in the registry.

The class (not an instance) is stored. Instantiation is deferred to registered_tool(name, chat:) at request time, so each invocation gets a fresh instance bound to the current session.

Parameters:

  • tool (Class) —

    the tool class to register

Returns:

Raises:

  • (ArgumentError) —

    if the tool has no register_name or is already registered



66
67
68
69
70
71
72
73
# File 'lib/ollama_chat/tools.rb', line 66

def register(tool)
  name = tool.register_name.to_s
  name.present? or raise ArgumentError, 'tool needs a name'
  registered.key?(name) and
    raise ArgumentError, 'tool %s already registered' % name
  registered[name] = tool
  self
end

.registered?(register_name) ⇒ TrueClass, FalseClass

Checks if a tool with the given name is registered.

Parameters:

  • register_name (String, #to_s) —

    the name of the tool to check

Returns:

  • (TrueClass, FalseClass) —

    true if the tool is registered, false otherwise



81
82
83
# File 'lib/ollama_chat/tools.rb', line 81

def registered?(register_name)
  registered.key?(register_name.to_s)
end

.registered_class(name) ⇒ Class

Returns the tool class for a registered tool name.

Parameters:

  • name (String, #to_s) —

    the registered tool name

Returns:

  • (Class) —

    the tool class

Raises:

  • (ArgumentError) —

    if no tool with the given name is registered



22
23
24
25
26
27
28
29
30
31
# File 'lib/ollama_chat/tools.rb', line 22

def registered_class(name)
  name = name.to_s
  registered.key?(name) or raise ArgumentError, 'tool %s not registered'
  tool = registered[name]
  if tool.is_a?(Class)
    tool
  else
    tool.class
  end
end

.registered_tool(name, chat:) ⇒ OllamaChat::Tools::Concern?

Instantiates a registered tool, binding it to the given chat session.

The registry stores tool classes (not instances). This method produces a fresh instance with the chat reference injected via Concern#initialize(chat), making self.chat available for config access and feedback during execution.

Parameters:

  • name (String, #to_s) —

    the registered tool name

  • chat (OllamaChat::Chat) —

    the chat session to bind

Returns:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ollama_chat/tools.rb', line 44

def registered_tool(name, chat:)
  name = name.to_s
  case tool = registered[name]
  when NilClass
    nil
  when Class
    tool.new(chat)
  else
    tool
  end
end