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.
Class Method Summary collapse
- .extended(klass) ⇒ Object private
Instance Method Summary collapse
-
#clear_registry! ⇒ void
Clears the registry.
-
#find_by_name(name) ⇒ LLM::Function, ...
Finds a registered entry by name.
- #lock ⇒ Object private
-
#register(entry) ⇒ Object
private
Registers an entry.
-
#registry ⇒ Array<LLM::Function, LLM::Tool>
Returns all registered entries.
-
#registry_key(entry) ⇒ Class<LLM::Tool>, ...
private
Returns the storage key for an entry.
-
#tool_name(entry) ⇒ String?
private
Returns the tool name, or nil for tools that are not fully initialized.
-
#unregister(entry) ⇒ Object
private
Unregisters an entry.
Class Method Details
.extended(klass) ⇒ 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.
21 22 23 24 25 |
# File 'lib/llm/function/registry.rb', line 21 def self.extended(klass) klass.instance_variable_set(:@__registry, {}) klass.instance_variable_set(:@__names, {}) klass.instance_variable_set(:@__monitor, Monitor.new) end |
Instance Method Details
#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 |
#find_by_name(name) ⇒ LLM::Function, ...
Finds a registered entry by name.
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 |
#lock ⇒ 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.
102 103 104 |
# File 'lib/llm/function/registry.rb', line 102 def lock(&) @__monitor.synchronize(&) 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.
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 |
#registry ⇒ Array<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 |
#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.
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.
96 97 98 |
# File 'lib/llm/function/registry.rb', line 96 def tool_name(entry) entry.respond_to?(:name) ? entry.name : nil 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.
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 |