Class: Collavre::Orchestration::AgentOrchestrator

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/orchestration/agent_orchestrator.rb

Overview

AgentOrchestrator is the single entry point for AI agent routing and scheduling. It coordinates the following components:

  • Matcher: determines which agents are qualified to respond

  • Arbiter: selects which agents will actually respond (floor control)

  • Scheduler: decides when to execute (resource management)

Usage:

AgentOrchestrator.dispatch("comment_created", context)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_name:, context:) ⇒ AgentOrchestrator

Returns a new instance of AgentOrchestrator.



83
84
85
86
87
88
# File 'app/services/collavre/orchestration/agent_orchestrator.rb', line 83

def initialize(event_name:, context:)
  @event_name = event_name
  # Build context ONCE here - no more duplicate builds
  @context = SystemEvents::ContextBuilder.new(context).build
  @context["event_name"] = event_name
end

Class Method Details

.dequeue_next_for_topic(topic_id, creative_id = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/collavre/orchestration/agent_orchestrator.rb', line 19

def self.dequeue_next_for_topic(topic_id, creative_id = nil)
  task = Task.queued_for_topic(topic_id, creative_id).first
  return unless task

  updated = Task.where(id: task.id, status: "queued").update_all(status: "pending")
  if updated > 0
    task.reload
    cleanup_waiting_notices!(task)
    refresh_deferred_context!(task)

    if task.status == "cancelled"
      # refresh_deferred_context! cancelled this task (no eligible comment),
      # try the next queued task for this topic.
      dequeue_next_for_topic(topic_id, creative_id)
    else
      AiAgentJob.perform_later(task)
    end
  end
end

.dispatch(event_name, context) ⇒ Object



15
16
17
# File 'app/services/collavre/orchestration/agent_orchestrator.rb', line 15

def self.dispatch(event_name, context)
  new(event_name: event_name, context: context).dispatch
end

Instance Method Details

#dispatchObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/services/collavre/orchestration/agent_orchestrator.rb', line 90

def dispatch
  # Step 1: Find qualified agents (Matcher)
  candidates = matcher.match
  return [] if candidates.empty?

  # Step 2: Select responders (Arbiter) - with policy-based floor control
  selected = arbiter.select(candidates)
  return [] if selected.empty?

  # Step 3: Schedule execution (Scheduler) - Phase 3
  # For now, immediate execution
  decisions = scheduler.schedule(selected)

  # Step 4: Enqueue jobs
  enqueue_jobs(decisions)
end