Class: A2A::PluginManager

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/plugin_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = A2A.config) ⇒ PluginManager

Initialize plugin manager

Parameters:



15
16
17
18
19
20
# File 'lib/a2a/plugin_manager.rb', line 15

def initialize(config = A2A.config)
  @config = config
  @logger = config.logger
  @auto_load_plugins = []
  @plugin_configs = {}
end

Instance Attribute Details

#configObject (readonly, private)

Returns the value of attribute config.



157
158
159
# File 'lib/a2a/plugin_manager.rb', line 157

def config
  @config
end

#loggerObject (readonly, private)

Returns the value of attribute logger.



157
158
159
# File 'lib/a2a/plugin_manager.rb', line 157

def logger
  @logger
end

Instance Method Details

#auth_pluginsArray<A2A::Plugin::AuthPlugin>

Get authentication plugins

Returns:



74
75
76
# File 'lib/a2a/plugin_manager.rb', line 74

def auth_plugins
  A2A::Plugin.loaded_plugins(type: :auth)
end

#build_middleware_chainArray<Proc>

Create middleware chain from plugins

Returns:

  • (Array<Proc>)

    Middleware chain



133
134
135
136
137
# File 'lib/a2a/plugin_manager.rb', line 133

def build_middleware_chain
  middleware_plugins.map do |plugin|
    proc { |request, next_middleware| plugin.call(request, next_middleware) }
  end
end

#configure_plugins(plugins) ⇒ Object

Configure auto-loading plugins

Parameters:

  • plugins (Hash<Symbol, Hash>)

    Plugin configurations



24
25
26
27
28
29
30
# File 'lib/a2a/plugin_manager.rb', line 24

def configure_plugins(plugins)
  @plugin_configs = plugins

  plugins.each do |name, plugin_config|
    @auto_load_plugins << name if plugin_config[:auto_load]
  end
end

#execute_request_hooks(event, request) ⇒ Hash

Execute request hooks

Parameters:

  • event (Symbol)

    Hook event

  • request (Hash)

    Request data

Returns:

  • (Hash)

    Modified request



108
109
110
111
112
113
114
115
116
117
# File 'lib/a2a/plugin_manager.rb', line 108

def execute_request_hooks(event, request)
  results = A2A::Plugin.execute_hooks(event, request)

  # Apply modifications from hooks
  results.each do |result|
    request.merge!(result) if result.is_a?(Hash)
  end

  request
end

#execute_response_hooks(event, response, request = nil) ⇒ Object

Execute response hooks

Parameters:

  • event (Symbol)

    Hook event

  • response (Object)

    Response data

  • request (Hash) (defaults to: nil)

    Original request

Returns:

  • (Object)

    Modified response



124
125
126
127
128
129
# File 'lib/a2a/plugin_manager.rb', line 124

def execute_response_hooks(event, response, request = nil)
  results = A2A::Plugin.execute_hooks(event, response, request)

  # Return last non-nil result or original response
  results.reverse.find { |result| !result.nil? } || response
end

#find_auth_strategy(auth_name) ⇒ A2A::Plugin::AuthPlugin?

Find auth plugin by name

Parameters:

  • auth_name (String)

    Authentication strategy name

Returns:



97
98
99
100
101
102
# File 'lib/a2a/plugin_manager.rb', line 97

def find_auth_strategy(auth_name)
  auth_plugins.find do |plugin|
    plugin.respond_to?(:strategy_name) &&
      plugin.strategy_name == auth_name
  end
end

#find_transport(transport_name) ⇒ A2A::Plugin::TransportPlugin?

Find transport plugin by name

Parameters:

  • transport_name (String)

    Transport protocol name

Returns:



87
88
89
90
91
92
# File 'lib/a2a/plugin_manager.rb', line 87

def find_transport(transport_name)
  transport_plugins.find do |plugin|
    plugin.respond_to?(:transport_name) &&
      plugin.transport_name == transport_name
  end
end

#load_all_pluginsObject

Load all configured plugins



33
34
35
36
37
# File 'lib/a2a/plugin_manager.rb', line 33

def load_all_plugins
  @auto_load_plugins.each do |plugin_name|
    load_plugin(plugin_name)
  end
end

#load_plugin(name, **config) ⇒ Object

Load a specific plugin

Parameters:

  • name (Symbol)

    Plugin name

  • **config (Hash)

    Plugin configuration override



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/a2a/plugin_manager.rb', line 42

def load_plugin(name, **config)
  plugin_config = @plugin_configs[name] || {}
  merged_config = plugin_config.merge(config)

  # Load dependencies first
  if A2A::Plugin.registry[name]
    dependencies = A2A::Plugin.registry[name][:class].dependencies
    dependencies.each do |dep|
      load_plugin(dep) unless A2A::Plugin.loaded?(dep)
    end
  end

  A2A::Plugin.load(name, **merged_config)
rescue A2A::Errors::PluginError => e
  @logger&.error("Failed to load plugin #{name}: #{e.message}")
  raise
end

#middleware_pluginsArray<A2A::Plugin::MiddlewarePlugin>

Get middleware plugins

Returns:



80
81
82
# File 'lib/a2a/plugin_manager.rb', line 80

def middleware_plugins
  A2A::Plugin.loaded_plugins(type: :middleware)
end

#statusHash

Get plugin status

Returns:

  • (Hash)

    Plugin status information



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/a2a/plugin_manager.rb', line 141

def status
  {
    loaded_plugins: A2A::Plugin.loaded_plugins.size,
    registered_plugins: A2A::Plugin.registry.size,
    plugins_by_type: {
      transport: transport_plugins.size,
      auth: auth_plugins.size,
      middleware: middleware_plugins.size
    },
    auto_load_plugins: @auto_load_plugins,
    plugin_list: A2A::Plugin.list
  }
end

#transport_pluginsArray<A2A::Plugin::TransportPlugin>

Get transport plugins

Returns:



68
69
70
# File 'lib/a2a/plugin_manager.rb', line 68

def transport_plugins
  A2A::Plugin.loaded_plugins(type: :transport)
end

#unload_plugin(name) ⇒ Object

Unload a plugin

Parameters:

  • name (Symbol)

    Plugin name



62
63
64
# File 'lib/a2a/plugin_manager.rb', line 62

def unload_plugin(name)
  A2A::Plugin.unload(name)
end