Class: Ace::Sim::Molecules::StageExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/sim/molecules/stage_executor.rb

Defined Under Namespace

Classes: CommandRunner

Instance Method Summary collapse

Constructor Details

#initialize(command_runner: nil) ⇒ StageExecutor

Returns a new instance of StageExecutor.



22
23
24
# File 'lib/ace/sim/molecules/stage_executor.rb', line 22

def initialize(command_runner: nil)
  @command_runner = command_runner || CommandRunner.new
end

Instance Method Details

#execute(step:, provider:, iteration:, step_dir:, step_bundle_path:, input_source_path:) ⇒ Object



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
# File 'lib/ace/sim/molecules/stage_executor.rb', line 26

def execute(step:, provider:, iteration:, step_dir:, step_bundle_path:, input_source_path:)
  input_path = File.join(step_dir, "input.md")
  bundle_path = File.join(step_dir, "user.bundle.md")
  prompt_path = File.join(step_dir, "user.prompt.md")
  output_path = File.join(step_dir, "output.md")

  FileUtils.cp(input_source_path, input_path)
  FileUtils.cp(step_bundle_path, bundle_path)

  bundle_result = command_runner.call(["ace-bundle", bundle_path, "--output", prompt_path])
  return failure(step, provider, iteration, output_path, "ace-bundle failed", bundle_result) unless bundle_result[:success]

  llm_result = command_runner.call(["ace-llm", provider, "--prompt", prompt_path, "--output", output_path])
  return failure(step, provider, iteration, output_path, "ace-llm failed", llm_result) unless llm_result[:success]

  unless valid_output?(output_path)
    return {
      "step" => step,
      "provider" => provider,
      "iteration" => iteration,
      "status" => "failed",
      "input_path" => input_path,
      "bundle_path" => bundle_path,
      "prompt_path" => prompt_path,
      "output_path" => output_path,
      "error" => "Step output missing or empty: #{output_path}"
    }
  end

  {
    "step" => step,
    "provider" => provider,
    "iteration" => iteration,
    "status" => "ok",
    "input_path" => input_path,
    "bundle_path" => bundle_path,
    "prompt_path" => prompt_path,
    "output_path" => output_path
  }
rescue => e
  {
    "step" => step,
    "provider" => provider,
    "iteration" => iteration,
    "status" => "failed",
    "input_path" => input_path,
    "bundle_path" => bundle_path,
    "prompt_path" => prompt_path,
    "output_path" => output_path,
    "error" => e.message
  }
end