Class: Aidp::Temporal::Activities::RunAgentActivity

Inherits:
BaseActivity
  • Object
show all
Defined in:
lib/aidp/temporal/activities/run_agent_activity.rb

Overview

Activity that executes an AI agent iteration Wraps the existing AIDP agent execution with Temporal durability

Instance Method Summary collapse

Methods inherited from BaseActivity

#activity_context, #cancellation_requested?, #check_cancellation!, #heartbeat

Instance Method Details

#execute(input) ⇒ Object



11
12
13
14
15
16
17
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
79
80
81
82
83
84
85
# File 'lib/aidp/temporal/activities/run_agent_activity.rb', line 11

def execute(input)
  with_activity_context do
    project_dir = input[:project_dir]
    step_name = input[:step_name]
    iteration = input[:iteration]
    injected_instructions = input[:injected_instructions] || []
    escalate = input[:escalate] || false

    log_activity("executing_agent",
      project_dir: project_dir,
      step_name: step_name,
      iteration: iteration,
      instructions_count: injected_instructions.length,
      escalate: escalate)

    # Load configuration
    config = load_config(project_dir)
    provider_manager = create_provider_manager(project_dir, config)

    # Get prompt content
    prompt_manager = Aidp::Execute::PromptManager.new(project_dir, config: config)
    prompt_content = prompt_manager.read

    unless prompt_content
      return error_result("No PROMPT.md found")
    end

    # Inject any queued instructions
    if injected_instructions.any?
      prompt_content = inject_instructions(prompt_content, injected_instructions)
      prompt_manager.write(prompt_content)
    end

    # Select model based on escalation status
    model_selector = create_model_selector(config, escalate: escalate)
    provider, model = model_selector.select

    log_activity("agent_model_selected",
      provider: provider,
      model: model,
      escalate: escalate)

    # Periodic heartbeat during agent execution
    heartbeat_thread = start_heartbeat_thread(iteration: iteration)

    begin
      # Execute agent
      result = execute_agent(
        project_dir: project_dir,
        provider_manager: provider_manager,
        provider: provider,
        model: model,
        prompt_content: prompt_content
      )

      check_cancellation!

      if result[:success]
        success_result(
          result: result[:output],
          provider: provider,
          model: model,
          iteration: iteration
        )
      else
        error_result(result[:error] || "Agent execution failed",
          provider: provider,
          model: model,
          iteration: iteration)
      end
    ensure
      heartbeat_thread&.kill
    end
  end
end