Module: Agent99::AgentLifecycle

Defined in:
lib/agent99/agent_lifecycle.rb

Overview

lib/agent99/agent_lifecycle.rb

Instance Method Summary collapse

Instance Method Details

#finiObject

Performs cleanup operations when the agent is shutting down.



77
78
79
80
81
82
83
84
85
# File 'lib/agent99/agent_lifecycle.rb', line 77

def fini
  if id
    queue_name = id
    withdraw
    @message_client&.delete_queue(queue_name)
  else
    logger.warn('fini called with a nil id')
  end
end

#initialize(registry_client: Agent99::RegistryClient.new, message_client: Agent99::AmqpMessageClient.new, logger: Logger.new($stdout)) ⇒ Object

Initializes a new AI agent with the given configuration.

Parameters:

  • registry_client (Agent99::RegistryClient) (defaults to: Agent99::RegistryClient.new)

    The client for agent registration

  • message_client (Agent99::AmqpMessageClient) (defaults to: Agent99::AmqpMessageClient.new)

    The client for message handling

  • logger (Logger) (defaults to: Logger.new($stdout))

    The logger instance for the agent



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/agent99/agent_lifecycle.rb', line 11

def initialize(registry_client: Agent99::RegistryClient.new,
               message_client: Agent99::AmqpMessageClient.new,
               logger: Logger.new($stdout))
  @agents           = {}
  @payload          = nil
  @name             = self.class.name
  @capabilities     = capabilities
  @id               = nil
  @registry_client  = registry_client
  @message_client   = message_client
  @logger           = logger

  validate_info_keys

  @registry_client.logger = logger
  register(info)

  @queue = message_client.setup(agent_id: id, logger:)

  init if respond_to?(:init)

  setup_signal_handlers
end

#register(agent_info) ⇒ Object

Registers the agent with the registry service.

Raises:

  • (StandardError)

    If registration fails



60
61
62
63
64
65
# File 'lib/agent99/agent_lifecycle.rb', line 60

def register(agent_info)
  @id = registry_client.register(info: agent_info)
  logger.info "Registered Agent #{name} with ID: #{id}"
rescue StandardError => e
  handle_error("Error during registration", e)
end

#validate_info_keysObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/agent99/agent_lifecycle.rb', line 36

def validate_info_keys
  required_keys = [:name, :capabilities]
  if respond_to? :info
    missing_keys = required_keys - info.keys
    unless missing_keys.empty?
      logger.error <<~MESSAGE
        This agent's info method is missing 
        #{1 == missing_keys.size ? 'a required key' : 'some required keys'}: 
        #{missing_keys}
      MESSAGE
      .split("\n").join
      exit(1)
    end
  else
    logger.error "An agent must implement the info method"
    exit(1)
  end
end

#withdrawObject

Withdraws the agent from the registry service.



69
70
71
72
# File 'lib/agent99/agent_lifecycle.rb', line 69

def withdraw
  registry_client.withdraw(@id) if @id
  @id = nil
end