Class: Bugsage::AiAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsage/ai_analyzer.rb

Constant Summary collapse

SYSTEM_PROMPT =
<<~PROMPT
  You are BugSage, a Ruby on Rails debugging assistant.
  Refine the provided rule-based analysis using the exception details and request context.
  Respond with JSON only using this schema:
  {
    "root_cause": "string",
    "fixes": ["string", "string"],
    "confidence": 0,
    "notes": "string",
    "code_patch": {
      "action": "replace_lines | delete_lines | insert_before | no_change",
      "start_line": 0,
      "end_line": 0,
      "replacement": "string"
    }
  }
  Keep fixes concrete and actionable. Confidence must be an integer from 0 to 100.

  You receive numbered source code from the real file. Rules for code_patch:
  - Read the FULL source context before suggesting changes.
  - Use absolute line numbers from the numbered source (the line marked with >>).
  - NEVER duplicate code that already exists in the file.
  - Change ONLY the lines required to fix the error.
  - To remove a line (e.g. stray raise, debug code), use delete_lines with empty replacement.
  - To replace lines, use replace_lines with start_line/end_line covering only affected lines.
  - If the file already contains the correct fix, use no_change.
  - Do not repeat existing assignments, finds, or renders.
  - replacement must be only the new Ruby code for the affected lines, without markdown fences.
PROMPT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Bugsage.configuration, client: nil) ⇒ AiAnalyzer

Returns a new instance of AiAnalyzer.



41
42
43
44
# File 'lib/bugsage/ai_analyzer.rb', line 41

def initialize(config: Bugsage.configuration, client: nil)
  @config = config
  @client = client
end

Class Method Details

.enhance(suggestion, exception, context = {}, config: Bugsage.configuration, client: nil) ⇒ Object



37
38
39
# File 'lib/bugsage/ai_analyzer.rb', line 37

def self.enhance(suggestion, exception, context = {}, config: Bugsage.configuration, client: nil)
  new(config: config, client: client).enhance(suggestion, exception, context)
end

Instance Method Details

#enhance(suggestion, exception, context) ⇒ Object

Returns [suggestion, ai_error_message]



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bugsage/ai_analyzer.rb', line 47

def enhance(suggestion, exception, context)
  return [suggestion, nil] unless @config.ai_configured?

  response = client.complete(
    system_prompt: SYSTEM_PROMPT,
    user_prompt: build_prompt(suggestion, exception, context)
  )
  [merge_suggestion(suggestion, parse_response(response, suggestion: suggestion)), nil]
rescue StandardError => e
  log_failure(e)
  [suggestion, e.message]
end