Class: A2A::Protocol::CapabilityRegistry

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

Overview

Manages a registry of capabilities

The capability registry allows for registration, discovery, and dynamic updates of agent capabilities.

Instance Method Summary collapse

Constructor Details

#initializeCapabilityRegistry

Returns a new instance of CapabilityRegistry.



348
349
350
351
# File 'lib/a2a/protocol/capability.rb', line 348

def initialize
  @capabilities = {}
  @listeners = []
end

Instance Method Details

#add_listener(&listener) ⇒ Object

Add a listener for capability changes

Parameters:

  • listener (Proc)

    The listener proc that receives (event, capability)



456
457
458
# File 'lib/a2a/protocol/capability.rb', line 456

def add_listener(&listener)
  @listeners << listener
end

#allArray<Capability>

Get all registered capabilities

Returns:



390
391
392
# File 'lib/a2a/protocol/capability.rb', line 390

def all
  @capabilities.values
end

#clearObject

Clear all capabilities



446
447
448
449
450
# File 'lib/a2a/protocol/capability.rb', line 446

def clear
  old_capabilities = @capabilities.values
  @capabilities.clear
  old_capabilities.each { |cap| notify_listeners(:unregistered, cap) }
end

#countInteger

Get the number of registered capabilities

Returns:

  • (Integer)

    The count



440
441
442
# File 'lib/a2a/protocol/capability.rb', line 440

def count
  @capabilities.size
end

#find_by_method(pattern) ⇒ Array<Capability>

Find capabilities by method pattern

Parameters:

  • pattern (String, Regexp)

    The method pattern to match

Returns:



408
409
410
411
412
413
414
415
416
# File 'lib/a2a/protocol/capability.rb', line 408

def find_by_method(pattern)
  if pattern.is_a?(String)
    @capabilities.values.select { |cap| cap.method == pattern }
  elsif pattern.is_a?(Regexp)
    @capabilities.values.select { |cap| cap.method.match?(pattern) }
  else
    raise ArgumentError, "pattern must be a String or Regexp"
  end
end

#find_by_security(scheme) ⇒ Array<Capability>

Find capabilities requiring specific security

Parameters:

  • scheme (String)

    The security scheme

Returns:

  • (Array<Capability>)

    Capabilities requiring the scheme



423
424
425
# File 'lib/a2a/protocol/capability.rb', line 423

def find_by_security(scheme)
  @capabilities.values.select { |cap| cap.requires_security?(scheme) }
end

#find_by_tag(tag) ⇒ Array<Capability>

Find capabilities by tag

Parameters:

  • tag (String)

    The tag to search for

Returns:

  • (Array<Capability>)

    Capabilities with the tag



399
400
401
# File 'lib/a2a/protocol/capability.rb', line 399

def find_by_tag(tag)
  @capabilities.values.select { |cap| cap.has_tag?(tag) }
end

#get(name) ⇒ Capability?

Get a capability by name

Parameters:

  • name (String)

    The capability name

Returns:

  • (Capability, nil)

    The capability or nil if not found



382
383
384
# File 'lib/a2a/protocol/capability.rb', line 382

def get(name)
  @capabilities[name]
end

#notify_listeners(event, capability) ⇒ Object (private)

Notify all listeners of a capability event

Parameters:

  • event (Symbol)

    The event type (:registered, :unregistered)

  • capability (Capability)

    The capability involved



486
487
488
489
490
491
492
493
# File 'lib/a2a/protocol/capability.rb', line 486

def notify_listeners(event, capability)
  @listeners.each do |listener|
    listener.call(event, capability)
  rescue StandardError => e
    # Log error but don't let listener errors break the registry
    warn "Capability registry listener error: #{e.message}"
  end
end

#register(capability) ⇒ Capability

Register a capability

Parameters:

  • capability (Capability)

    The capability to register

Returns:

Raises:

  • (ArgumentError)


358
359
360
361
362
363
364
# File 'lib/a2a/protocol/capability.rb', line 358

def register(capability)
  raise ArgumentError, "capability must be a Capability instance" unless capability.is_a?(Capability)

  @capabilities[capability.name] = capability
  notify_listeners(:registered, capability)
  capability
end

#registered?(name) ⇒ Boolean

Check if a capability is registered

Parameters:

  • name (String)

    The capability name

Returns:

  • (Boolean)

    True if registered



432
433
434
# File 'lib/a2a/protocol/capability.rb', line 432

def registered?(name)
  @capabilities.key?(name)
end

#remove_listener(listener) ⇒ Object

Remove a listener

Parameters:

  • listener (Proc)

    The listener to remove



464
465
466
# File 'lib/a2a/protocol/capability.rb', line 464

def remove_listener(listener)
  @listeners.delete(listener)
end

#to_hHash

Convert registry to hash representation

Returns:

  • (Hash)

    The registry as a hash



472
473
474
475
476
477
# File 'lib/a2a/protocol/capability.rb', line 472

def to_h
  {
    capabilities: @capabilities.transform_values(&:to_h),
    count: count
  }
end

#unregister(name) ⇒ Capability?

Unregister a capability

Parameters:

  • name (String)

    The capability name to unregister

Returns:

  • (Capability, nil)

    The unregistered capability or nil if not found



371
372
373
374
375
# File 'lib/a2a/protocol/capability.rb', line 371

def unregister(name)
  capability = @capabilities.delete(name)
  notify_listeners(:unregistered, capability) if capability
  capability
end