Class: Aidp::Providers::Opencode

Inherits:
Base
  • Object
show all
Includes:
DebugMixin
Defined in:
lib/aidp/providers/opencode.rb

Constant Summary

Constants included from DebugMixin

DebugMixin::DEBUG_BASIC, DebugMixin::DEBUG_OFF, DebugMixin::DEBUG_VERBOSE

Constants inherited from Base

Base::ACTIVITY_STATES, Base::DEFAULT_STUCK_TIMEOUT

Instance Attribute Summary

Attributes inherited from Base

#activity_state, #last_activity_time, #start_time, #step_name, #stuck_timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DebugMixin

#debug_basic?, #debug_command, #debug_enabled?, #debug_error, #debug_execute_command, #debug_level, #debug_log, #debug_logger, #debug_provider, #debug_step, #debug_timing, #debug_verbose?, included, shared_logger

Methods inherited from Base

#activity_summary, #available?, #execution_time, #harness_config, #harness_health_status, #harness_healthy?, #harness_metrics, #harness_mode?, #initialize, #record_harness_request, #send_with_harness, #set_harness_context, #set_job_context, #stuck?, #supports_activity_monitoring?, #time_since_last_activity, #update_activity_state

Constructor Details

This class inherits a constructor from Aidp::Providers::Base

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/aidp/providers/opencode.rb', line 13

def self.available?
  !!Aidp::Util.which("opencode")
end

Instance Method Details

#nameObject



17
18
19
# File 'lib/aidp/providers/opencode.rb', line 17

def name
  "opencode"
end

#send(prompt:, session: nil) ⇒ Object



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
# File 'lib/aidp/providers/opencode.rb', line 21

def send(prompt:, session: nil)
  raise "opencode not available" unless self.class.available?

  # Smart timeout calculation
  timeout_seconds = calculate_timeout

  debug_provider("opencode", "Starting execution", {timeout: timeout_seconds})
  debug_log("📝 Sending prompt to opencode (length: #{prompt.length})", level: :info)

  # Check if prompt is too large and warn
  if prompt.length > 3000
    debug_log("⚠️  Large prompt detected (#{prompt.length} chars) - this may cause rate limiting", level: :warn)
  end

  # Set up activity monitoring
  setup_activity_monitoring("opencode", method(:activity_callback))
  record_activity("Starting opencode execution")

  # Start activity display thread with timeout
  activity_display_thread = Thread.new do
    start_time = Time.now
    loop do
      sleep 0.5 # Update every 500ms to reduce spam
      elapsed = Time.now - start_time

      # Break if we've been running too long or state changed
      break if elapsed > timeout_seconds || @activity_state == :completed || @activity_state == :failed

      print_activity_status(elapsed)
    end
  end

  begin
    # Use debug_execute_command for better debugging
    # opencode run command with prompt and model
    model = ENV["OPENCODE_MODEL"] || "github-copilot/claude-3.5-sonnet"
    result = debug_execute_command("opencode", args: ["run", "-m", model, prompt], timeout: timeout_seconds)

    # Log the results
    debug_command("opencode", args: ["run", "-m", model, prompt], input: nil, output: result.out, error: result.err, exit_code: result.exit_status)

    # Stop activity display
    activity_display_thread.kill if activity_display_thread.alive?
    activity_display_thread.join(0.1) # Give it 100ms to finish
    clear_activity_status

    if result.exit_status == 0
      mark_completed
      result.out
    else
      mark_failed("opencode failed with exit code #{result.exit_status}")
      debug_error(StandardError.new("opencode failed"), {exit_code: result.exit_status, stderr: result.err})
      raise "opencode failed with exit code #{result.exit_status}: #{result.err}"
    end
  rescue => e
    # Stop activity display
    activity_display_thread.kill if activity_display_thread.alive?
    activity_display_thread.join(0.1) # Give it 100ms to finish
    clear_activity_status
    mark_failed("opencode execution failed: #{e.message}")
    debug_error(e, {provider: "opencode", prompt_length: prompt.length})
    raise
  end
end