Class: Agentic::Extension::ProtocolHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/extension/protocol_handler.rb

Overview

The ProtocolHandler standardizes connections to external systems through consistent interface definitions. It enables integration with various APIs, data sources, and services while providing a uniform access pattern regardless of the underlying protocol or system specifics.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ProtocolHandler

Initialize a new ProtocolHandler

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :logger (Logger)

    Custom logger instance

  • :default_headers (Hash)

    Default headers for all protocol requests



15
16
17
18
19
# File 'lib/agentic/extension/protocol_handler.rb', line 15

def initialize(options = {})
  @logger = options[:logger] || Agentic.logger
  @default_headers = options[:default_headers] || {}
  @protocols = {}
end

Instance Method Details

#list_protocolsArray<Symbol>

List all registered protocols

Returns:

  • (Array<Symbol>)

    List of registered protocol names



100
101
102
# File 'lib/agentic/extension/protocol_handler.rb', line 100

def list_protocols
  @protocols.keys
end

#protocol_config(protocol_name) ⇒ Hash?

Get protocol configuration

Parameters:

  • protocol_name (Symbol)

    The protocol to get configuration for

Returns:

  • (Hash, nil)

    The protocol configuration or nil if not registered



71
72
73
74
75
# File 'lib/agentic/extension/protocol_handler.rb', line 71

def protocol_config(protocol_name)
  return nil unless @protocols.key?(protocol_name)

  @protocols[protocol_name][:config]
end

#protocol_registered?(protocol_name) ⇒ Boolean

Check if a protocol is registered

Parameters:

  • protocol_name (Symbol)

    The protocol to check

Returns:

  • (Boolean)

    True if the protocol is registered



93
94
95
# File 'lib/agentic/extension/protocol_handler.rb', line 93

def protocol_registered?(protocol_name)
  @protocols.key?(protocol_name)
end

#register_protocol(protocol_name, implementation, config = {}) ⇒ Boolean

Register a protocol implementation

Parameters:

  • protocol_name (Symbol)

    The name of the protocol (e.g., :http, :websocket, :grpc)

  • implementation (Object)

    The protocol implementation

  • config (Hash) (defaults to: {})

    Protocol-specific configuration

Returns:

  • (Boolean)

    True if registration was successful



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/agentic/extension/protocol_handler.rb', line 27

def register_protocol(protocol_name, implementation, config = {})
  if !valid_protocol?(implementation)
    @logger.error("Protocol implementation for '#{protocol_name}' is invalid")
    return false
  end

  @protocols[protocol_name] = {
    implementation: implementation,
    config: config
  }

  @logger.info("Protocol '#{protocol_name}' registered successfully")
  true
end

#send_request(protocol_name, endpoint, options = {}) ⇒ Hash?

Send a request using a registered protocol

Parameters:

  • protocol_name (Symbol)

    The protocol to use

  • endpoint (String)

    The endpoint to send the request to

  • options (Hash) (defaults to: {})

    Request options including :method, :headers, :body, etc.

Returns:

  • (Hash, nil)

    The response or nil if the protocol is not registered



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/agentic/extension/protocol_handler.rb', line 48

def send_request(protocol_name, endpoint, options = {})
  unless @protocols.key?(protocol_name)
    @logger.error("Protocol '#{protocol_name}' is not registered")
    return nil
  end

  protocol = @protocols[protocol_name]
  options[:headers] = @default_headers.merge(options[:headers] || {})

  begin
    response = protocol[:implementation].send_request(endpoint, options.merge(protocol[:config]))
    @logger.debug("Request sent using '#{protocol_name}' protocol to '#{endpoint}'")
    response
  rescue => e
    @logger.error("Failed to send request using '#{protocol_name}' protocol: #{e.message}")
    nil
  end
end

#update_protocol_config(protocol_name, config) ⇒ Boolean

Update protocol configuration

Parameters:

  • protocol_name (Symbol)

    The protocol to update

  • config (Hash)

    The new configuration to merge

Returns:

  • (Boolean)

    True if update was successful



82
83
84
85
86
87
# File 'lib/agentic/extension/protocol_handler.rb', line 82

def update_protocol_config(protocol_name, config)
  return false unless @protocols.key?(protocol_name)

  @protocols[protocol_name][:config].merge!(config)
  true
end