Class: RailsAi::Agents::Collaboration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task:, agents:, manager:) ⇒ Collaboration

Returns a new instance of Collaboration.



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

def initialize(task:, agents:, manager:)
  @id = SecureRandom.uuid
  @task = task
  @agents = Array(agents)
  @manager = manager
  @status = :pending
  @created_at = Time.now
  @contributions = {}
  @workflow = []
  @current_phase = 0
  @phases = build_workflow_phases
end

Instance Attribute Details

#agentsObject (readonly)

Returns the value of attribute agents.



6
7
8
# File 'lib/rails_ai/agents/collaboration.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/collaboration.rb', line 6

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#managerObject (readonly)

Returns the value of attribute manager.



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

def manager
  @manager
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#taskObject (readonly)

Returns the value of attribute task.



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

def task
  @task
end

Instance Method Details

#add_contribution(agent_name, contribution) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails_ai/agents/collaboration.rb', line 32

def add_contribution(agent_name, contribution)
  @contributions[agent_name] = {
    content: contribution,
    timestamp: Time.now,
    phase: @current_phase
  }
  
  defined?(Rails) && Rails.logger && Rails.logger.info("Agent #{agent_name} contributed to collaboration #{@id}")
  
  # Check if current phase is complete
  if phase_complete?
    advance_to_next_phase
  end
end

#complete!(result = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rails_ai/agents/collaboration.rb', line 96

def complete!(result = nil)
  @status = :completed
  @completed_at = Time.now
  @result = result
  
  defined?(Rails) && Rails.logger && Rails.logger.info("Collaboration #{@id} completed in #{duration&.round(2)} seconds")
  
  # Notify all participating agents
  @agents.each do |agent|
    @manager.send_message("collaboration_system", agent.name, {
      type: :collaboration_completed,
      collaboration_id: @id,
      result: result
    })
  end
  
  self
end

#current_phase_infoObject



59
60
61
62
63
# File 'lib/rails_ai/agents/collaboration.rb', line 59

def current_phase_info
  return nil if @current_phase >= @phases.length
  
  @phases[@current_phase]
end

#durationObject



73
74
75
76
77
78
# File 'lib/rails_ai/agents/collaboration.rb', line 73

def duration
  return nil unless @started_at
  
  end_time = @completed_at || Time.now
  end_time - @started_at
end

#fail!(error) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rails_ai/agents/collaboration.rb', line 115

def fail!(error)
  @status = :failed
  @failed_at = Time.now
  @error = error
  
  defined?(Rails) && Rails.logger && Rails.logger.error("Collaboration #{@id} failed: #{error}")
  
  # Notify all participating agents
  @agents.each do |agent|
    @manager.send_message("collaboration_system", agent.name, {
      type: :collaboration_failed,
      collaboration_id: @id,
      error: error
    })
  end
  
  self
end

#get_contributions(agent_name = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rails_ai/agents/collaboration.rb', line 47

def get_contributions(agent_name = nil)
  if agent_name
    @contributions[agent_name]
  else
    @contributions
  end
end

#get_phase_contributions(phase) ⇒ Object



55
56
57
# File 'lib/rails_ai/agents/collaboration.rb', line 55

def get_phase_contributions(phase)
  @contributions.select { |_, contrib| contrib[:phase] == phase }
end

#is_complete?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rails_ai/agents/collaboration.rb', line 65

def is_complete?
  @status == :completed
end

#is_failed?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/rails_ai/agents/collaboration.rb', line 69

def is_failed?
  @status == :failed
end

#start!Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_ai/agents/collaboration.rb', line 21

def start!
  @status = :in_progress
  @started_at = Time.now
  
  defined?(Rails) && Rails.logger && Rails.logger.info("Collaboration #{@id} started with #{@agents.length} agents")
  
  # Begin the collaboration workflow
  execute_workflow_phase
  self
end

#summaryObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails_ai/agents/collaboration.rb', line 80

def summary
  {
    id: @id,
    task: @task,
    agents: @agents.map(&:name),
    status: @status,
    current_phase: @current_phase,
    total_phases: @phases.length,
    contributions_count: @contributions.length,
    duration: duration,
    created_at: @created_at,
    started_at: @started_at,
    completed_at: @completed_at
  }
end