Class: OllamaAgent::Plugins::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/plugins/registry.rb

Overview

Central plugin registry.

A plugin is any object (module, class, or struct) that can register extensions at one or more extension points.

Extension points:

:tools            — Array<Tools::Base subclass instances>
:prompts          — Array<Hash { name:, content: }>
:policies         — Array<Proc(tool, args, ctx) → nil | String>
:providers        — Array<Providers::Base instances>
:postprocessors   — Array<Proc(response) → response>
:memory_adapters  — Array<Memory::Base-like objects>
:command_handlers — Array<Hash { slash_command:, handler: Proc }>

Examples:

Register a plugin inline

OllamaAgent::Plugins::Registry.register(:my_plugin) do |r|
  r.extend(:tools, MyTool.new)
  r.extend(:prompts, name: "code_review", content: File.read("prompts/review.md"))
end

Constant Summary collapse

EXTENSION_POINTS =
%i[
  tools prompts policies providers
  postprocessors memory_adapters command_handlers
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



48
49
50
# File 'lib/ollama_agent/plugins/registry.rb', line 48

def initialize
  reset!
end

Class Method Details

.all_command_handlersObject



43
# File 'lib/ollama_agent/plugins/registry.rb', line 43

def all_command_handlers                 = instance.extensions_for(:command_handlers)

.all_policiesObject



40
# File 'lib/ollama_agent/plugins/registry.rb', line 40

def all_policies                         = instance.extensions_for(:policies)

.all_postprocessorsObject



42
# File 'lib/ollama_agent/plugins/registry.rb', line 42

def all_postprocessors                   = instance.extensions_for(:postprocessors)

.all_promptsObject



39
# File 'lib/ollama_agent/plugins/registry.rb', line 39

def all_prompts                          = instance.extensions_for(:prompts)

.all_providersObject



41
# File 'lib/ollama_agent/plugins/registry.rb', line 41

def all_providers                        = instance.extensions_for(:providers)

.all_toolsObject



38
# File 'lib/ollama_agent/plugins/registry.rb', line 38

def all_tools                            = instance.extensions_for(:tools)

.extensions_for(point) ⇒ Object



37
# File 'lib/ollama_agent/plugins/registry.rb', line 37

def extensions_for(point)                = instance.extensions_for(point)

.instanceObject



31
32
33
# File 'lib/ollama_agent/plugins/registry.rb', line 31

def instance
  @instance ||= new
end

.plugin_namesObject



44
# File 'lib/ollama_agent/plugins/registry.rb', line 44

def plugin_names                         = instance.plugin_names

.register(name, plugin = nil) ⇒ Object

Delegate class methods to the singleton.



36
# File 'lib/ollama_agent/plugins/registry.rb', line 36

def register(name, plugin = nil, &) = instance.register(name, plugin, &)

.reset!Object



45
# File 'lib/ollama_agent/plugins/registry.rb', line 45

def reset!                               = instance.reset!

Instance Method Details

#add_command(slash_command:, &handler) ⇒ Object



88
89
90
91
# File 'lib/ollama_agent/plugins/registry.rb', line 88

def add_command(slash_command:,
                &handler)
  extend(:command_handlers, { slash_command: slash_command, handler: handler })
end

#add_policy(&block) ⇒ Object



85
# File 'lib/ollama_agent/plugins/registry.rb', line 85

def add_policy(&block)     = extend(:policies, block)

#add_prompt(hash) ⇒ Object



84
# File 'lib/ollama_agent/plugins/registry.rb', line 84

def add_prompt(hash)       = extend(:prompts, hash)

#add_provider(provider) ⇒ Object



86
# File 'lib/ollama_agent/plugins/registry.rb', line 86

def add_provider(provider) = extend(:providers, provider)

#add_tool(tool) ⇒ Object

Shorthand helpers



83
# File 'lib/ollama_agent/plugins/registry.rb', line 83

def add_tool(tool)         = extend(:tools, tool)

#extend(point, handler) ⇒ Object

Add an extension at a specific extension point.

Parameters:

  • point (Symbol)

    one of EXTENSION_POINTS

  • handler (Object)

    see per-point documentation above



72
73
74
75
76
77
78
79
80
# File 'lib/ollama_agent/plugins/registry.rb', line 72

def extend(point, handler)
  point = point.to_sym
  unless EXTENSION_POINTS.include?(point)
    raise ArgumentError,
          "Unknown extension point: #{point}. Valid: #{EXTENSION_POINTS.join(", ")}"
  end

  @extensions[point] << handler
end

#extensions_for(point) ⇒ Object



93
94
95
# File 'lib/ollama_agent/plugins/registry.rb', line 93

def extensions_for(point)
  @extensions[point.to_sym].dup
end

#plugin_namesObject



97
98
99
# File 'lib/ollama_agent/plugins/registry.rb', line 97

def plugin_names
  @plugins.keys
end

#register(name, plugin = nil, &block) ⇒ Object

Register a plugin by name.

Parameters:

  • name (Symbol, String)

    unique plugin identifier

  • plugin (Object, nil) (defaults to: nil)

    plugin object (must respond to #register(registry))

  • block (Proc)

    alternative: inline registration block

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ollama_agent/plugins/registry.rb', line 56

def register(name, plugin = nil, &block)
  name = name.to_sym
  raise ArgumentError, "Plugin #{name} is already registered" if @plugins.key?(name)

  @plugins[name] = plugin

  if block
    block.call(self)
  elsif plugin.respond_to?(:register)
    plugin.register(self)
  end
end

#reset!Object



101
102
103
104
# File 'lib/ollama_agent/plugins/registry.rb', line 101

def reset!
  @plugins    = {}
  @extensions = Hash.new { |h, k| h[k] = [] }
end