Class: RailsAi::Agents::AgentTeam

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai/agents/agent_team.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, agents:, strategy: :round_robin, manager:) ⇒ AgentTeam

Returns a new instance of AgentTeam.



8
9
10
11
12
13
14
15
16
17
# File 'lib/rails_ai/agents/agent_team.rb', line 8

def initialize(name:, agents:, strategy: :round_robin, manager:)
  @name = name
  @agents = Array(agents)
  @strategy = strategy
  @manager = manager
  @created_at = Time.now
  @current_agent_index = 0
  @team_memory = {}
  @collaboration_history = []
end

Instance Attribute Details

#agentsObject (readonly)

Returns the value of attribute agents.



6
7
8
# File 'lib/rails_ai/agents/agent_team.rb', line 6

def agents
  @agents
end

#created_atObject (readonly)

Returns the value of attribute created_at.



6
7
8
# File 'lib/rails_ai/agents/agent_team.rb', line 6

def created_at
  @created_at
end

#managerObject (readonly)

Returns the value of attribute manager.



6
7
8
# File 'lib/rails_ai/agents/agent_team.rb', line 6

def manager
  @manager
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/rails_ai/agents/agent_team.rb', line 6

def name
  @name
end

#strategyObject (readonly)

Returns the value of attribute strategy.



6
7
8
# File 'lib/rails_ai/agents/agent_team.rb', line 6

def strategy
  @strategy
end

Instance Method Details

#assign_task(task) ⇒ Object

Team task assignment



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

def assign_task(task)
  case @strategy
  when :round_robin
    assign_round_robin(task)
  when :capability_based
    assign_capability_based(task)
  when :load_balanced
    assign_load_balanced(task)
  when :collaborative
    assign_collaborative(task)
  else
    assign_round_robin(task)
  end
end

#collaborate_on_task(task) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_ai/agents/agent_team.rb', line 35

def collaborate_on_task(task)
  return false unless @strategy == :collaborative

  collaboration = {
    id: SecureRandom.uuid,
    task: task,
    agents: @agents.map(&:name),
    started_at: Time.now,
    status: :in_progress,
    contributions: {}
  }

  # Each agent contributes to the task
  @agents.each do |agent|
    begin
      contribution = agent.collaborate_with(agent, task, context: build_team_context)
      collaboration[:contributions][agent.name] = contribution
    rescue => e
      defined?(Rails) && Rails.logger && Rails.logger.error("Agent #{agent.name} collaboration failed: #{e.message}")
      collaboration[:contributions][agent.name] = { error: e.message }
    end
  end

  collaboration[:completed_at] = Time.now
  collaboration[:status] = :completed
  @collaboration_history << collaboration

  defined?(Rails) && Rails.logger && Rails.logger.info("Team #{@name} completed collaborative task: #{task[:description]}")
  collaboration
end

#learn_from_experienceObject

Team learning



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rails_ai/agents/agent_team.rb', line 138

def learn_from_experience
  recent_collaborations = @collaboration_history.last(10)
  return if recent_collaborations.empty?

  insights = analyze_collaboration_patterns(recent_collaborations)
  
  # Share insights with all agents
  @agents.each do |agent|
    agent.remember("team_insights_#{Time.now.to_i}", insights, importance: :high)
  end

  defined?(Rails) && Rails.logger && Rails.logger.info("Team #{@name} learned from recent collaborations")
  insights
end

#share_knowledge(agent_name, knowledge) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rails_ai/agents/agent_team.rb', line 93

def share_knowledge(agent_name, knowledge)
  @team_memory["knowledge_#{Time.now.to_i}"] = {
    shared_by: agent_name,
    knowledge: knowledge,
    shared_at: Time.now
  }

  # Broadcast knowledge to other agents
  @manager.broadcast_message(agent_name, {
    type: :knowledge_share,
    knowledge: knowledge,
    shared_by: agent_name
  }, exclude: [agent_name])

  defined?(Rails) && Rails.logger && Rails.logger.info("Agent #{agent_name} shared knowledge with team #{@name}")
end

#team_healthObject



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rails_ai/agents/agent_team.rb', line 124

def team_health
  agent_health = @agents.map do |agent|
    { name: agent.name, health: agent.health_check }
  end

  {
    overall_health: agent_health.all? { |ah| ah[:health].values.all? },
    agent_health: agent_health,
    memory_usage: calculate_team_memory_usage,
    collaboration_success_rate: calculate_collaboration_success_rate
  }
end

#team_meeting(agenda) ⇒ Object

Team communication



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails_ai/agents/agent_team.rb', line 67

def team_meeting(agenda)
  meeting = {
    id: SecureRandom.uuid,
    agenda: agenda,
    participants: @agents.map(&:name),
    started_at: Time.now,
    discussions: {}
  }

  # Each agent shares their perspective
  @agents.each do |agent|
    perspective = agent.think("Team meeting agenda: #{agenda}. Share your perspective and insights.", 
                            context: build_team_context)
    meeting[:discussions][agent.name] = perspective
  end

  meeting[:ended_at] = Time.now
  meeting[:duration] = meeting[:ended_at] - meeting[:started_at]

  # Store meeting results in team memory
  @team_memory["meeting_#{meeting[:id]}"] = meeting

  defined?(Rails) && Rails.logger && Rails.logger.info("Team #{@name} meeting completed: #{agenda}")
  meeting
end

#team_statusObject

Team monitoring



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rails_ai/agents/agent_team.rb', line 111

def team_status
  {
    name: @name,
    strategy: @strategy,
    agent_count: @agents.length,
    active_agents: @agents.count { |a| a.state == :active },
    total_tasks: @agents.sum { |a| a.active_tasks.length },
    team_memory_size: @team_memory.length,
    collaboration_count: @collaboration_history.length,
    created_at: @created_at
  }
end