Class: Collavre::AiAgentService
- Inherits:
-
Object
- Object
- Collavre::AiAgentService
- Defined in:
- app/services/collavre/ai_agent_service.rb
Overview
Orchestrates AI agent execution for a task. Delegates specific concerns to specialized service objects:
-
AgentLifecycleManager: agent status, cancellation, heartbeats
-
ResponseStreamer: streaming content updates
-
ResponseFinalizer: comment finalization, review workflow
-
A2aDispatcher: agent-to-agent event dispatch
Constant Summary collapse
- CANCEL_CHECK_INTERVAL =
Compatibility alias for constants moved to AgentLifecycleManager
AiAgent::AgentLifecycleManager::CANCEL_CHECK_INTERVAL
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(task) ⇒ AiAgentService
constructor
A new instance of AiAgentService.
Constructor Details
#initialize(task) ⇒ AiAgentService
Returns a new instance of AiAgentService.
12 13 14 15 16 |
# File 'app/services/collavre/ai_agent_service.rb', line 12 def initialize(task) @task = task @agent = task.agent @context = task.trigger_event_payload end |
Instance Method Details
#call ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/services/collavre/ai_agent_service.rb', line 18 def call Current.set(user: @agent) do log_action("start", { message: "Starting agent execution" }) # Build context and messages @original_comment = find_original_comment = log_action("prompt_generated", { messages: [:messages] }) # Prepare rendering context and prompts @creative = find_creative rendering_context = prepare_rendering_context system_prompt = render_system_prompt(rendering_context) # Resolve session context (decides what to send) resolved = resolve_session_context(, system_prompt) # Create placeholder comment if needed @reply_comment = create_reply_comment_if_needed # Initialize lifecycle manager @lifecycle_manager = AiAgent::AgentLifecycleManager.new( task: @task, agent: @agent, creative: @creative ) # Initialize response streamer @streamer = AiAgent::ResponseStreamer.new( reply_comment: @reply_comment, creative: @creative ) @lifecycle_manager.broadcast_status("thinking") # Execute AI chat with streaming @client = build_ai_client(resolved[:system_prompt]) stream_response(@client, resolved) log_action("completion", { response: @streamer.content }) # Finalize and dispatch (skip A2A for review-flow updates) finalized_comment = finalize_response dispatch_a2a(finalized_comment) unless @finalizer&.review_flow @lifecycle_manager.broadcast_status("idle") @streamer.content end rescue ApprovalPendingError => e summary = generate_approval_summary(e) AiAgent::ApprovalHandler.new( task: @task, agent: @agent, context: @context, creative: @creative, reply_comment: @reply_comment ).handle(e, summary: summary) raise rescue CancelledError handle_cancelled abort_openclaw_if_needed raise end |