Class: Google::ADK::BaseAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/google/adk/agents/base_agent.rb

Direct Known Subclasses

LlmAgent, LoopAgent, ParallelAgent, SequentialAgent

Constant Summary collapse

AGENT_NAME_REGEX =
/^[a-zA-Z][a-zA-Z0-9_-]*$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description: nil, sub_agents: [], before_agent_callback: nil, after_agent_callback: nil) ⇒ BaseAgent

Initialize a new BaseAgent

Parameters:

  • name (String)

    Unique name for the agent

  • description (String) (defaults to: nil)

    Description of the agent’s capabilities (optional)

  • sub_agents (Array<BaseAgent>) (defaults to: [])

    Child agents (optional)

  • before_agent_callback (Proc) (defaults to: nil)

    Callback before agent execution (optional)

  • after_agent_callback (Proc) (defaults to: nil)

    Callback after agent execution (optional)

Raises:

  • (ArgumentError)

    If name is not provided

  • (ConfigurationError)

    If name format is invalid or sub-agent names are not unique



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/google/adk/agents/base_agent.rb', line 20

def initialize(name:, description: nil, sub_agents: [], before_agent_callback: nil, after_agent_callback: nil)
  raise ArgumentError, "name is required" if name.nil?

  validate_agent_name!(name)

  @name = name
  @description = description
  @parent_agent = nil
  @sub_agents = []
  @before_agent_callback = before_agent_callback
  @after_agent_callback = after_agent_callback

  # Set up sub-agents with validation
  self.sub_agents = sub_agents
end

Instance Attribute Details

#after_agent_callbackObject (readonly)

Returns the value of attribute after_agent_callback.



8
9
10
# File 'lib/google/adk/agents/base_agent.rb', line 8

def after_agent_callback
  @after_agent_callback
end

#before_agent_callbackObject (readonly)

Returns the value of attribute before_agent_callback.



8
9
10
# File 'lib/google/adk/agents/base_agent.rb', line 8

def before_agent_callback
  @before_agent_callback
end

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/google/adk/agents/base_agent.rb', line 8

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/google/adk/agents/base_agent.rb', line 8

def name
  @name
end

#parent_agentObject (readonly)

Returns the value of attribute parent_agent.



8
9
10
# File 'lib/google/adk/agents/base_agent.rb', line 8

def parent_agent
  @parent_agent
end

#sub_agentsObject

Returns the value of attribute sub_agents.



8
9
10
# File 'lib/google/adk/agents/base_agent.rb', line 8

def sub_agents
  @sub_agents
end

Class Method Details

.from_config(config) ⇒ BaseAgent

Create an agent from configuration

Parameters:

  • config (Hash)

    Configuration hash

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/google/adk/agents/base_agent.rb', line 99

def self.from_config(config)
  config = config.transform_keys(&:to_sym)

  # Recursively create sub-agents if present
  sub_agents = if config[:sub_agents]
                 config[:sub_agents].map { |sub_config| from_config(sub_config) }
               else
                 []
               end

  new(
    name: config[:name],
    description: config[:description],
    sub_agents: sub_agents,
    before_agent_callback: config[:before_agent_callback],
    after_agent_callback: config[:after_agent_callback]
  )
end

Instance Method Details

#clone(**attributes) ⇒ BaseAgent

Create a copy of the agent with optional updates

Parameters:

  • attributes (Hash)

    Attributes to update in the clone

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/google/adk/agents/base_agent.rb', line 55

def clone(**attributes)
  # Deep clone sub_agents if present
  cloned_sub_agents = if attributes.key?(:sub_agents)
                        attributes[:sub_agents]
                      else
                        @sub_agents.map(&:clone)
                      end

  self.class.new(
    name: attributes.fetch(:name, @name),
    description: attributes.fetch(:description, @description),
    sub_agents: cloned_sub_agents,
    before_agent_callback: attributes.fetch(:before_agent_callback, @before_agent_callback),
    after_agent_callback: attributes.fetch(:after_agent_callback, @after_agent_callback)
  )
end

#find_agent(agent_name) ⇒ BaseAgent?

Find an agent by name in the agent tree

Parameters:

  • agent_name (String)

    Name of the agent to find

Returns:

  • (BaseAgent, nil)

    The found agent or nil



76
77
78
79
80
81
82
83
84
85
# File 'lib/google/adk/agents/base_agent.rb', line 76

def find_agent(agent_name)
  return self if @name == agent_name

  @sub_agents.each do |sub_agent|
    found = sub_agent.find_agent(agent_name)
    return found if found
  end

  nil
end

#find_sub_agent(agent_name) ⇒ BaseAgent?

Find a direct sub-agent by name

Parameters:

  • agent_name (String)

    Name of the sub-agent to find

Returns:

  • (BaseAgent, nil)

    The found sub-agent or nil



91
92
93
# File 'lib/google/adk/agents/base_agent.rb', line 91

def find_sub_agent(agent_name)
  @sub_agents.find { |agent| agent.name == agent_name }
end

#run_async(message) ⇒ Object

Run the agent asynchronously with a text message

Parameters:

  • message (String)

    The input message

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



40
41
42
# File 'lib/google/adk/agents/base_agent.rb', line 40

def run_async(message)
  raise NotImplementedError, "Subclasses must implement #run_async"
end

#run_liveObject

Run the agent in live mode (video/audio)

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



47
48
49
# File 'lib/google/adk/agents/base_agent.rb', line 47

def run_live
  raise NotImplementedError, "Subclasses must implement #run_live"
end