Module: LLM::Function::Registry

Included in:
LLM::Function, Tool
Defined in:
lib/llm/function/registry.rb

Overview

The LLM::Function::Registry module provides shared registry behavior for functions and tools. Tool.registry stores LLM::Tool subclasses, including dynamically created MCP tool subclasses, while registry stores the functions derived from those tools.

The registry overwrites older tool definitions with newer ones when they share the same tool name. In practice, tool identity is resolved by name, and LLMs generally do not allow two tools with the same name.

Functions defined with LLM.function are not added to the function registry, since they may be closures bound to local state. Each registry decides how entries are keyed via #registry_key.

Instance Method Summary collapse

Instance Method Details

#lockObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



102
103
104
# File 'lib/llm/function/registry.rb', line 102

def lock(&)
  @__monitor.synchronize(&)
end

#find_by_name(name) ⇒ LLM::Function, ...

Finds a registered entry by name.

Parameters:

  • name (String)

Returns:



40
41
42
43
44
45
46
# File 'lib/llm/function/registry.rb', line 40

def find_by_name(name)
  lock do
    @__names[name.to_s] ||= @__registry.each_value.find do
      tool_name(_1).to_s == name.to_s
    end
  end
end

#clear_registry!void

This method returns an undefined value.

Clears the registry.



51
52
53
54
55
56
57
# File 'lib/llm/function/registry.rb', line 51

def clear_registry!
  lock do
    @__registry.clear
    @__names.clear
    nil
  end
end

#register(entry) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers an entry.

Parameters:



63
64
65
66
67
68
# File 'lib/llm/function/registry.rb', line 63

def register(entry)
  lock do
    @__registry[registry_key(entry)] = entry
    @__names[tool_name(entry).to_s] = entry if tool_name(entry)
  end
end

#unregister(entry) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Unregisters an entry.

Parameters:



74
75
76
77
78
79
80
# File 'lib/llm/function/registry.rb', line 74

def unregister(entry)
  lock do
    @__registry.delete(registry_key(entry))
    @__registry.delete(entry)
    @__names.delete(tool_name(entry).to_s) if tool_name(entry)
  end
end

#registry_key(entry) ⇒ Class<LLM::Tool>, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the storage key for an entry.

Parameters:

Returns:



87
88
89
# File 'lib/llm/function/registry.rb', line 87

def registry_key(entry)
  tool_name(entry) ? entry.name : entry
end

#tool_name(entry) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the tool name, or nil for tools that are not fully initialized.

Parameters:

Returns:

  • (String, nil)


96
97
98
# File 'lib/llm/function/registry.rb', line 96

def tool_name(entry)
  entry.respond_to?(:name) ? entry.name : nil
end

#registryArray<LLM::Function, LLM::Tool>

Returns all registered entries.



30
31
32
33
34
# File 'lib/llm/function/registry.rb', line 30

def registry
  lock do
    @__registry.values
  end
end