@ -4,14 +4,38 @@ import path from "node:path"
import { ScreenshotHelper } from "./ScreenshotHelper"
import { IProcessingHelperDeps } from "./main"
import * as axios from "axios"
import { app , BrowserWindow } from "electron"
import { app , BrowserWindow , dialog } from "electron"
import { OpenAI } from "openai"
import { configHelper } from "./ConfigHelper"
// Interface for Gemini API requests
interface GeminiMessage {
role : string ;
parts : Array < {
text? : string ;
inlineData ? : {
mimeType : string ;
data : string ;
}
} > ;
}
interface GeminiResponse {
candidates : Array < {
content : {
parts : Array < {
text : string ;
} > ;
} ;
finishReason : string ;
} > ;
}
export class ProcessingHelper {
private deps : IProcessingHelperDeps
private screenshotHelper : ScreenshotHelper
private openaiClient : OpenAI | null = null
private geminiApiKey : string | null = null
// AbortControllers for API requests
private currentProcessingAbortController : AbortController | null = null
@ -21,35 +45,51 @@ export class ProcessingHelper {
this . deps = deps
this . screenshotHelper = deps . getScreenshotHelper ( )
// Initialize Open AI client
this . initialize Open AIClient( ) ;
// Initialize AI client based on config
this . initialize AIClient( ) ;
// Listen for config changes to re-initialize the Open AI client
// Listen for config changes to re-initialize the AI client
configHelper . on ( 'config-updated' , ( ) = > {
this . initialize Open AIClient( ) ;
this . initialize AIClient( ) ;
} ) ;
}
/ * *
* Initialize or reinitialize the Open AI client with current config
* Initialize or reinitialize the AI client with current config
* /
private initialize Open AIClient( ) : void {
private initialize AIClient( ) : void {
try {
const config = configHelper . loadConfig ( ) ;
if ( config . apiProvider === "openai" ) {
if ( config . apiKey ) {
this . openaiClient = new OpenAI ( {
apiKey : config.apiKey ,
timeout : 60000 , // 60 second timeout
maxRetries : 2 // Retry up to 2 times
} ) ;
this . geminiApiKey = null ;
console . log ( "OpenAI client initialized successfully" ) ;
} else {
this . openaiClient = null ;
this . geminiApiKey = null ;
console . warn ( "No API key available, OpenAI client not initialized" ) ;
}
} else {
// Gemini client initialization
this . openaiClient = null ;
if ( config . apiKey ) {
this . geminiApiKey = config . apiKey ;
console . log ( "Gemini API key set successfully" ) ;
} else {
this . geminiApiKey = null ;
console . warn ( "No API key available, Gemini client not initialized" ) ;
}
}
} catch ( error ) {
console . error ( "Failed to initialize OpenAI client:" , error ) ;
console . error ( "Failed to initialize AI client:", error ) ;
this . openaiClient = null ;
this . geminiApiKey = null ;
}
}
@ -124,9 +164,11 @@ export class ProcessingHelper {
const mainWindow = this . deps . getMainWindow ( )
if ( ! mainWindow ) return
// First verify we have a valid OpenAI client
if ( ! this . openaiClient ) {
this . initializeOpenAIClient ( ) ;
const config = configHelper . loadConfig ( ) ;
// First verify we have a valid AI client
if ( config . apiProvider === "openai" && ! this . openaiClient ) {
this . initializeAIClient ( ) ;
if ( ! this . openaiClient ) {
console . error ( "OpenAI client not initialized" ) ;
@ -135,6 +177,16 @@ export class ProcessingHelper {
) ;
return ;
}
} else if ( config . apiProvider === "gemini" && ! this . geminiApiKey ) {
this . initializeAIClient ( ) ;
if ( ! this . geminiApiKey ) {
console . error ( "Gemini API key not initialized" ) ;
mainWindow . webContents . send (
this . deps . PROCESSING_EVENTS . API_KEY_INVALID
) ;
return ;
}
}
const view = this . deps . getView ( )
@ -144,9 +196,38 @@ export class ProcessingHelper {
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . INITIAL_START )
const screenshotQueue = this . screenshotHelper . getScreenshotQueue ( )
console . log ( "Processing main queue screenshots:" , screenshotQueue )
if ( screenshotQueue . length === 0 ) {
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS )
return
// Check if the queue is empty
if ( ! screenshotQueue || screenshotQueue . length === 0 ) {
console . log ( "No screenshots found in queue" ) ;
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS ) ;
// Show dialog if no screenshots
dialog . showMessageBox ( mainWindow , {
type : 'info' ,
title : 'No Screenshots Detected' ,
message : 'No screenshots were found to process.' ,
detail : 'Please take a screenshot first using Ctrl+H (or Cmd+H on Mac). Make sure your screenshot contains the coding problem you want to solve.' ,
buttons : [ 'OK' ]
} ) ;
return ;
}
// Check that files actually exist
const existingScreenshots = screenshotQueue . filter ( path = > fs . existsSync ( path ) ) ;
if ( existingScreenshots . length === 0 ) {
console . log ( "Screenshot files don't exist on disk" ) ;
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS ) ;
// Show error dialog
dialog . showMessageBox ( mainWindow , {
type : 'warning' ,
title : 'Screenshot Files Missing' ,
message : 'The screenshot files were not found on disk.' ,
detail : 'Try taking a new screenshot with Ctrl+H (or Cmd+H on Mac).' ,
buttons : [ 'OK' ]
} ) ;
return ;
}
try {
@ -155,18 +236,32 @@ export class ProcessingHelper {
const { signal } = this . currentProcessingAbortController
const screenshots = await Promise . all (
screenshotQueue . map ( async ( path ) = > ( {
existingScreenshots . map ( async ( path ) = > {
try {
return {
path ,
preview : await this . screenshotHelper . getImagePreview ( path ) ,
data : fs.readFileSync ( path ) . toString ( 'base64' )
} ) )
} ;
} catch ( err ) {
console . error ( ` Error reading screenshot ${ path } : ` , err ) ;
return null ;
}
} )
)
const result = await this . processScreenshotsHelper ( screenshots , signal )
// Filter out any nulls from failed screenshots
const validScreenshots = screenshots . filter ( Boolean ) ;
if ( validScreenshots . length === 0 ) {
throw new Error ( "Failed to load screenshot data" ) ;
}
const result = await this . processScreenshotsHelper ( validScreenshots , signal )
if ( ! result . success ) {
console . log ( "Processing failed:" , result . error )
if ( result . error ? . includes ( "API Key" ) || result . error ? . includes ( "OpenAI" ) ) {
if ( result . error ? . includes ( "API Key" ) || result . error ? . includes ( "OpenAI" ) || result . error ? . includes ( "Gemini" ) ) {
mainWindow . webContents . send (
this . deps . PROCESSING_EVENTS . API_KEY_INVALID
)
@ -217,10 +312,39 @@ export class ProcessingHelper {
const extraScreenshotQueue =
this . screenshotHelper . getExtraScreenshotQueue ( )
console . log ( "Processing extra queue screenshots:" , extraScreenshotQueue )
if ( extraScreenshotQueue . length === 0 ) {
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS )
return
// Check if the extra queue is empty
if ( ! extraScreenshotQueue || extraScreenshotQueue . length === 0 ) {
console . log ( "No extra screenshots found in queue" ) ;
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS ) ;
// Show dialog if no screenshots
dialog . showMessageBox ( mainWindow , {
type : 'info' ,
title : 'No Debug Screenshots' ,
message : 'No screenshots were found for debugging.' ,
detail : 'Please take screenshots of your code/errors with Ctrl+H before debugging.' ,
buttons : [ 'OK' ]
} ) ;
return ;
}
// Check that files actually exist
const existingExtraScreenshots = extraScreenshotQueue . filter ( path = > fs . existsSync ( path ) ) ;
if ( existingExtraScreenshots . length === 0 ) {
console . log ( "Extra screenshot files don't exist on disk" ) ;
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS ) ;
dialog . showMessageBox ( mainWindow , {
type : 'warning' ,
title : 'Screenshot Files Missing' ,
message : 'The debug screenshot files were not found.' ,
detail : 'Try taking a new screenshot with Ctrl+H (or Cmd+H on Mac).' ,
buttons : [ 'OK' ]
} ) ;
return ;
}
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . DEBUG_START )
// Initialize AbortController
@ -228,23 +352,46 @@ export class ProcessingHelper {
const { signal } = this . currentExtraProcessingAbortController
try {
const screenshots = await Promise . all (
[
// Get all screenshots (both main and extra) for processing
const allPaths = [
. . . this . screenshotHelper . getScreenshotQueue ( ) ,
. . . extraScreenshotQueue
] . map ( async ( path ) = > ( {
. . . existingExtraScreenshots
] ;
const screenshots = await Promise . all (
allPaths . map ( async ( path ) = > {
try {
if ( ! fs . existsSync ( path ) ) {
console . warn ( ` Screenshot file does not exist: ${ path } ` ) ;
return null ;
}
return {
path ,
preview : await this . screenshotHelper . getImagePreview ( path ) ,
data : fs.readFileSync ( path ) . toString ( 'base64' )
} ) )
} ;
} catch ( err ) {
console . error ( ` Error reading screenshot ${ path } : ` , err ) ;
return null ;
}
} )
)
// Filter out any nulls from failed screenshots
const validScreenshots = screenshots . filter ( Boolean ) ;
if ( validScreenshots . length === 0 ) {
throw new Error ( "Failed to load screenshot data for debugging" ) ;
}
console . log (
"Combined screenshots for processing:" ,
screenshots . map ( ( s ) = > s . path )
validS creenshots. map ( ( s ) = > s . path )
)
const result = await this . processExtraScreenshotsHelper (
screenshots ,
validS creenshots,
signal
)
@ -287,9 +434,23 @@ export class ProcessingHelper {
const language = await this . getLanguage ( ) ;
const mainWindow = this . deps . getMainWindow ( ) ;
// Step 1: Extract problem info using AI Vision API (OpenAI or Gemini)
const imageDataList = screenshots . map ( screenshot = > screenshot . data ) ;
// Update the user on progress
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Analyzing problem from screenshots..." ,
progress : 20
} ) ;
}
let problemInfo ;
if ( config . apiProvider === "openai" ) {
// Verify OpenAI client
if ( ! this . openaiClient ) {
this . initializeOpenAIClient ( ) ; // Try to reinitialize
this . initialize AIClient( ) ; // Try to reinitialize
if ( ! this . openaiClient ) {
return {
@ -299,17 +460,7 @@ export class ProcessingHelper {
}
}
// Step 1: Extract problem info using OpenAI Vision API
const imageDataList = screenshots . map ( screenshot = > screenshot . data ) ;
// Update the user on progress
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Analyzing problem from screenshots..." ,
progress : 20
} ) ;
}
// Use OpenAI for processing
const messages = [
{
role : "system" as const ,
@ -338,13 +489,79 @@ export class ProcessingHelper {
temperature : 0.2
} ) ;
// Parse the response to get structured problem info
let problemInfo ;
// Parse the response
try {
const responseText = extractionResponse . choices [ 0 ] . message . content ;
// Handle when OpenAI might wrap the JSON in markdown code blocks
const jsonText = responseText . replace ( /```json|```/g , '' ) . trim ( ) ;
problemInfo = JSON . parse ( jsonText ) ;
} catch ( error ) {
console . error ( "Error parsing OpenAI response:" , error ) ;
return {
success : false ,
error : "Failed to parse problem information. Please try again or use clearer screenshots."
} ;
}
} else {
// Use Gemini API
if ( ! this . geminiApiKey ) {
return {
success : false ,
error : "Gemini API key not configured. Please check your settings."
} ;
}
try {
// Create Gemini message structure
const geminiMessages : GeminiMessage [ ] = [
{
role : "user" ,
parts : [
{
text : ` You are a coding challenge interpreter. Analyze the screenshots of the coding problem and extract all relevant information. Return the information in JSON format with these fields: problem_statement, constraints, example_input, example_output. Just return the structured JSON without any other text. Preferred coding language we gonna use for this problem is ${ language } . `
} ,
. . . imageDataList . map ( data = > ( {
inlineData : {
mimeType : "image/png" ,
data : data
}
} ) )
]
}
] ;
// Make API request to Gemini
const response = await axios . default . post (
` https://generativelanguage.googleapis.com/v1beta/models/ ${ config . extractionModel || "gemini-2.0-flash" } :generateContent?key= ${ this . geminiApiKey } ` ,
{
contents : geminiMessages ,
generationConfig : {
temperature : 0.2 ,
maxOutputTokens : 4000
}
} ,
{ signal }
) ;
const responseData = response . data as GeminiResponse ;
if ( ! responseData . candidates || responseData . candidates . length === 0 ) {
throw new Error ( "Empty response from Gemini API" ) ;
}
const responseText = responseData . candidates [ 0 ] . content . parts [ 0 ] . text ;
// Handle when Gemini might wrap the JSON in markdown code blocks
const jsonText = responseText . replace ( /```json|```/g , '' ) . trim ( ) ;
problemInfo = JSON . parse ( jsonText ) ;
} catch ( error ) {
console . error ( "Error using Gemini API:" , error ) ;
return {
success : false ,
error : "Failed to process with Gemini API. Please check your API key or try again later."
} ;
}
}
// Update the user on progress
if ( mainWindow ) {
@ -353,14 +570,6 @@ export class ProcessingHelper {
progress : 40
} ) ;
}
} catch ( error ) {
console . error ( "Error parsing problem extraction response:" , error ) ;
console . log ( "Raw response:" , extractionResponse . choices [ 0 ] . message . content ) ;
return {
success : false ,
error : "Failed to parse problem information. Please try again or use clearer screenshots."
} ;
}
// Store problem info in AppState
this . deps . setProblemInfo ( problemInfo ) ;
@ -443,13 +652,6 @@ export class ProcessingHelper {
throw new Error ( "No problem info available" ) ;
}
if ( ! this . openaiClient ) {
return {
success : false ,
error : "OpenAI API key not configured. Please check your settings."
} ;
}
// Update progress status
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
@ -487,9 +689,20 @@ For complexity explanations, please be thorough. For example: "Time complexity:
Your solution should be efficient , well - commented , and handle edge cases .
` ;
let responseContent ;
if ( config . apiProvider === "openai" ) {
// OpenAI processing
if ( ! this . openaiClient ) {
return {
success : false ,
error : "OpenAI API key not configured. Please check your settings."
} ;
}
// Send to OpenAI API
const solutionResponse = await this . openaiClient . chat . completions . create ( {
model : config.solutionModel || "gpt-4o" , // Using selected model for code generation
model : config.solutionModel || "gpt-4o" ,
messages : [
{ role : "system" , content : "You are an expert coding interview assistant. Provide clear, optimal solutions with detailed explanations." } ,
{ role : "user" , content : promptText }
@ -498,7 +711,57 @@ Your solution should be efficient, well-commented, and handle edge cases.
temperature : 0.2
} ) ;
const responseContent = solutionResponse . choices [ 0 ] . message . content ;
responseContent = solutionResponse . choices [ 0 ] . message . content ;
} else {
// Gemini processing
if ( ! this . geminiApiKey ) {
return {
success : false ,
error : "Gemini API key not configured. Please check your settings."
} ;
}
try {
// Create Gemini message structure
const geminiMessages = [
{
role : "user" ,
parts : [
{
text : ` You are an expert coding interview assistant. Provide a clear, optimal solution with detailed explanations for this problem: \ n \ n ${ promptText } `
}
]
}
] ;
// Make API request to Gemini
const response = await axios . default . post (
` https://generativelanguage.googleapis.com/v1beta/models/ ${ config . solutionModel || "gemini-2.0-flash" } :generateContent?key= ${ this . geminiApiKey } ` ,
{
contents : geminiMessages ,
generationConfig : {
temperature : 0.2 ,
maxOutputTokens : 4000
}
} ,
{ signal }
) ;
const responseData = response . data as GeminiResponse ;
if ( ! responseData . candidates || responseData . candidates . length === 0 ) {
throw new Error ( "Empty response from Gemini API" ) ;
}
responseContent = responseData . candidates [ 0 ] . content . parts [ 0 ] . text ;
} catch ( error ) {
console . error ( "Error using Gemini API for solution:" , error ) ;
return {
success : false ,
error : "Failed to generate solution with Gemini API. Please check your API key or try again later."
} ;
}
}
// Extract parts from the response
const codeMatch = responseContent . match ( /```(?:\w+)?\s*([\s\S]*?)```/ ) ;
@ -525,22 +788,18 @@ Your solution should be efficient, well-commented, and handle edge cases.
}
// Extract complexity information
// Use more flexible patterns to find complexity sections
const timeComplexityPattern = /Time complexity:?\s*([^\n]+(?:\n[^\n]+)*?)(?=\n\s*(?:Space complexity|$))/i ;
const spaceComplexityPattern = /Space complexity:?\s*([^\n]+(?:\n[^\n]+)*?)(?=\n\s*(?:[A-Z]|$))/i ;
let timeComplexity = "O(n) - Linear time complexity because we only iterate through the array once. Each element is processed exactly one time, and the hashmap lookups are O(1) operations." ;
let spaceComplexity = "O(n) - Linear space complexity because we store elements in the hashmap. In the worst case, we might need to store all elements before finding the solution pair." ;
// Extract time complexity with better matching
const timeMatch = responseContent . match ( timeComplexityPattern ) ;
if ( timeMatch && timeMatch [ 1 ] ) {
timeComplexity = timeMatch [ 1 ] . trim ( ) ;
// Ensure the response includes actual Big O notation and a dash
if ( ! timeComplexity . match ( /O\([^)]+\)/i ) ) {
timeComplexity = ` O(n) - ${ timeComplexity } ` ;
} else if ( ! timeComplexity . includes ( '-' ) && ! timeComplexity . includes ( 'because' ) ) {
// Add a dash if there isn't one and no 'because'
const notationMatch = timeComplexity . match ( /O\([^)]+\)/i ) ;
if ( notationMatch ) {
const notation = notationMatch [ 0 ] ;
@ -550,15 +809,12 @@ Your solution should be efficient, well-commented, and handle edge cases.
}
}
// Extract space complexity with better matching
const spaceMatch = responseContent . match ( spaceComplexityPattern ) ;
if ( spaceMatch && spaceMatch [ 1 ] ) {
spaceComplexity = spaceMatch [ 1 ] . trim ( ) ;
// Ensure the response includes actual Big O notation and a dash
if ( ! spaceComplexity . match ( /O\([^)]+\)/i ) ) {
spaceComplexity = ` O(n) - ${ spaceComplexity } ` ;
} else if ( ! spaceComplexity . includes ( '-' ) && ! spaceComplexity . includes ( 'because' ) ) {
// Add a dash if there isn't one and no 'because'
const notationMatch = spaceComplexity . match ( /O\([^)]+\)/i ) ;
if ( notationMatch ) {
const notation = notationMatch [ 0 ] ;
@ -568,7 +824,6 @@ Your solution should be efficient, well-commented, and handle edge cases.
}
}
// Construct the formatted response
const formattedResponse = {
code : code ,
thoughts : thoughts.length > 0 ? thoughts : [ "Solution approach based on efficiency and readability" ] ,
@ -578,7 +833,13 @@ Your solution should be efficient, well-commented, and handle edge cases.
return { success : true , data : formattedResponse } ;
} catch ( error : any ) {
// Handle OpenAI API errors specifically
if ( axios . isCancel ( error ) ) {
return {
success : false ,
error : "Processing was canceled by the user."
} ;
}
if ( error ? . response ? . status === 401 ) {
return {
success : false ,
@ -610,13 +871,6 @@ Your solution should be efficient, well-commented, and handle edge cases.
throw new Error ( "No problem info available" ) ;
}
if ( ! this . openaiClient ) {
return {
success : false ,
error : "OpenAI API key not configured. Please check your settings."
} ;
}
// Update progress status
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
@ -627,6 +881,17 @@ Your solution should be efficient, well-commented, and handle edge cases.
// Prepare the images for the API call
const imageDataList = screenshots . map ( screenshot = > screenshot . data ) ;
let debugContent ;
if ( config . apiProvider === "openai" ) {
if ( ! this . openaiClient ) {
return {
success : false ,
error : "OpenAI API key not configured. Please check your settings."
} ;
}
const messages = [
{
role : "system" as const ,
@ -669,7 +934,6 @@ If you include code examples, use proper markdown code blocks with language spec
}
] ;
// Update progress
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Analyzing code and generating debug feedback..." ,
@ -677,7 +941,6 @@ If you include code examples, use proper markdown code blocks with language spec
} ) ;
}
// Send to OpenAI Vision API
const debugResponse = await this . openaiClient . chat . completions . create ( {
model : config.debuggingModel || "gpt-4o" ,
messages : messages ,
@ -685,7 +948,90 @@ If you include code examples, use proper markdown code blocks with language spec
temperature : 0.2
} ) ;
// Update final progress
debugContent = debugResponse . choices [ 0 ] . message . content ;
} else {
if ( ! this . geminiApiKey ) {
return {
success : false ,
error : "Gemini API key not configured. Please check your settings."
} ;
}
try {
const debugPrompt = `
You are a coding interview assistant helping debug and improve solutions . Analyze these screenshots which include either error messages , incorrect outputs , or test cases , and provide detailed debugging help .
I ' m solving this coding problem : "${problemInfo.problem_statement}" in $ { language } . I need help with debugging or improving my solution .
YOUR RESPONSE MUST FOLLOW THIS EXACT STRUCTURE WITH THESE SECTION HEADERS :
# # # Issues Identified
- List each issue as a bullet point with clear explanation
# # # Specific Improvements and Corrections
- List specific code changes needed as bullet points
# # # Optimizations
- List any performance optimizations if applicable
# # # Explanation of Changes Needed
Here provide a clear explanation of why the changes are needed
# # # Key Points
- Summary bullet points of the most important takeaways
If you include code examples , use proper markdown code blocks with language specification ( e . g . \ ` \` \` java).
` ;
const geminiMessages = [
{
role : "user" ,
parts : [
{ text : debugPrompt } ,
. . . imageDataList . map ( data = > ( {
inlineData : {
mimeType : "image/png" ,
data : data
}
} ) )
]
}
] ;
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Analyzing code and generating debug feedback with Gemini..." ,
progress : 60
} ) ;
}
const response = await axios . default . post (
` https://generativelanguage.googleapis.com/v1beta/models/ ${ config . debuggingModel || "gemini-2.0-flash" } :generateContent?key= ${ this . geminiApiKey } ` ,
{
contents : geminiMessages ,
generationConfig : {
temperature : 0.2 ,
maxOutputTokens : 4000
}
} ,
{ signal }
) ;
const responseData = response . data as GeminiResponse ;
if ( ! responseData . candidates || responseData . candidates . length === 0 ) {
throw new Error ( "Empty response from Gemini API" ) ;
}
debugContent = responseData . candidates [ 0 ] . content . parts [ 0 ] . text ;
} catch ( error ) {
console . error ( "Error using Gemini API for debugging:" , error ) ;
return {
success : false ,
error : "Failed to process debug request with Gemini API. Please check your API key or try again later."
} ;
}
}
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Debug analysis complete" ,
@ -693,20 +1039,14 @@ If you include code examples, use proper markdown code blocks with language spec
} ) ;
}
// Extract and format the debug response
const debugContent = debugResponse . choices [ 0 ] . message . content ;
// Extract code if there's code block in the response
let extractedCode = "// Debug mode - see analysis below" ;
const codeMatch = debugContent . match ( /```(?:[a-zA-Z]+)?([\s\S]*?)```/ ) ;
if ( codeMatch && codeMatch [ 1 ] ) {
extractedCode = codeMatch [ 1 ] . trim ( ) ;
}
// Format the debug content for better display - add clear section headings
let formattedDebugContent = debugContent ;
// Ensure proper section formatting
if ( ! debugContent . includes ( '# ' ) && ! debugContent . includes ( '## ' ) ) {
formattedDebugContent = debugContent
. replace ( /issues identified|problems found|bugs found/i , '## Issues Identified' )
@ -715,13 +1055,11 @@ If you include code examples, use proper markdown code blocks with language spec
. replace ( /explanation|detailed analysis/i , '## Explanation' ) ;
}
// Try to extract bullet points for "thoughts"
const bulletPoints = formattedDebugContent . match ( /(?:^|\n)[ ]*(?:[-*•]|\d+\.)[ ]+([^\n]+)/g ) ;
const thoughts = bulletPoints
? bulletPoints . map ( point = > point . replace ( /^[ ]*(?:[-*•]|\d+\.)[ ]+/ , '' ) . trim ( ) ) . slice ( 0 , 5 )
: [ "Debug analysis based on your screenshots" ] ;
// Return the debug assistance in the format expected by the app
const response = {
code : extractedCode ,
debug_analysis : formattedDebugContent ,
@ -732,19 +1070,6 @@ If you include code examples, use proper markdown code blocks with language spec
return { success : true , data : response } ;
} catch ( error : any ) {
// Handle OpenAI API errors specifically
if ( error ? . response ? . status === 401 ) {
return {
success : false ,
error : "Invalid OpenAI API key. Please check your settings."
} ;
} else if ( error ? . response ? . status === 429 ) {
return {
success : false ,
error : "OpenAI API rate limit exceeded or insufficient credits. Please try again later."
} ;
}
console . error ( "Debug processing error:" , error ) ;
return { success : false , error : error.message || "Failed to process debug request" } ;
}
@ -765,15 +1090,12 @@ If you include code examples, use proper markdown code blocks with language spec
wasCancelled = true
}
// Reset hasDebugged flag
this . deps . setHasDebugged ( false )
// Clear any pending state
this . deps . setProblemInfo ( null )
const mainWindow = this . deps . getMainWindow ( )
if ( wasCancelled && mainWindow && ! mainWindow . isDestroyed ( ) ) {
// Send a clear message that processing was cancelled
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS )
}
}