@ -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 . apiKey ) {
this . openaiClient = new OpenAI ( {
apiKey : config.apiKey ,
timeout : 60000 , // 60 second timeout
maxRetries : 2 // Retry up to 2 times
} ) ;
console . log ( "OpenAI client initialized successfully" ) ;
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 ;
console . warn ( "No API key available, OpenAI client not initialized" ) ;
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 ) = > ( {
path ,
preview : await this . screenshotHelper . getImagePreview ( path ) ,
data : fs.readFileSync ( path ) . toString ( 'base64' )
} ) )
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 {
// Get all screenshots (both main and extra) for processing
const allPaths = [
. . . this . screenshotHelper . getScreenshotQueue ( ) ,
. . . existingExtraScreenshots
] ;
const screenshots = await Promise . all (
[
. . . this . screenshotHelper . getScreenshotQueue ( ) ,
. . . extraScreenshotQueue
] . map ( async ( path ) = > ( {
path ,
preview : await this . screenshotHelper . getImagePreview ( path ) ,
data : fs.readFileSync ( path ) . toString ( 'base64' )
} ) )
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,19 +434,7 @@ export class ProcessingHelper {
const language = await this . getLanguage ( ) ;
const mainWindow = this . deps . getMainWindow ( ) ;
// Verify OpenAI client
if ( ! this . openaiClient ) {
this . initializeOpenAIClient ( ) ; // Try to reinitialize
if ( ! this . openaiClient ) {
return {
success : false ,
error : "OpenAI API key not configured or invalid. Please check your settings."
} ;
}
}
// Step 1: Extract problem info using OpenAI Vision API
// Step 1: Extract problem info using AI Vision API (OpenAI or Gemini)
const imageDataList = screenshots . map ( screenshot = > screenshot . data ) ;
// Update the user on progress
@ -310,56 +445,130 @@ export class ProcessingHelper {
} ) ;
}
const messages = [
{
role : "system" as const ,
content : "You are a coding challenge interpreter. Analyze the screenshot 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."
} ,
{
role : "user" as const ,
content : [
let problemInfo ;
if ( config . apiProvider === "openai" ) {
// Verify OpenAI client
if ( ! this . openaiClient ) {
this . initializeAIClient ( ) ; // Try to reinitialize
if ( ! this . openaiClient ) {
return {
success : false ,
error : "OpenAI API key not configured or invalid. Please check your settings."
} ;
}
}
// Use OpenAI for processing
const messages = [
{
role : "system" as const ,
content : "You are a coding challenge interpreter. Analyze the screenshot 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."
} ,
{
role : "user" as const ,
content : [
{
type : "text" as const ,
text : ` Extract the coding problem details from these screenshots. Return in JSON format. Preferred coding language we gonna use for this problem is ${ language } . `
} ,
. . . imageDataList . map ( data = > ( {
type : "image_url" as const ,
image_url : { url : ` data:image/png;base64, ${ data } ` }
} ) )
]
}
] ;
// Send to OpenAI Vision API
const extractionResponse = await this . openaiClient . chat . completions . create ( {
model : config.extractionModel || "gpt-4o" ,
messages : messages ,
max_tokens : 4000 ,
temperature : 0.2
} ) ;
// 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 [ ] = [
{
type : "text" as const ,
text : ` Extract the coding problem details from these screenshots. Return in JSON format. Preferred coding language we gonna use for this problem is ${ language } . `
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
}
} ,
. . . imageDataList . map ( data = > ( {
type : "image_url" as const ,
image_url : { url : ` data:image/png;base64, ${ data } ` }
} ) )
]
}
] ;
{ signal }
) ;
// Send to OpenAI Vision API
const extractionResponse = await this . openaiClient . chat . completions . create ( {
model : config.extractionModel || "gpt-4o" ,
messages : messages ,
max_tokens : 4000 ,
temperature : 0.2
} ) ;
const responseData = response . data as GeminiResponse ;
// Parse the response to get structured problem info
let problemInfo ;
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 ) ;
if ( ! responseData . candidates || responseData . candidates . length === 0 ) {
throw new Error ( "Empty response from Gemini API" ) ;
}
// Update the user on progress
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Problem analyzed successfully. Preparing to generate solution..." ,
progress : 40
} ) ;
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."
} ;
}
} 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."
} ;
}
// Update the user on progress
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Problem analyzed successfully. Preparing to generate solution..." ,
progress : 40
} ) ;
}
// Store problem info in AppState
@ -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,18 +689,79 @@ For complexity explanations, please be thorough. For example: "Time complexity:
Your solution should be efficient , well - commented , and handle edge cases .
` ;
// Send to OpenAI API
const solutionResponse = await this . openaiClient . chat . completions . create ( {
model : config.solutionModel || "gpt-4o" , // Using selected model for code generation
messages : [
{ role : "system" , content : "You are an expert coding interview assistant. Provide clear, optimal solutions with detailed explanations." } ,
{ role : "user" , content : promptText }
] ,
max_tokens : 4000 ,
temperature : 0.2
} ) ;
let responseContent ;
const responseContent = solutionResponse . choices [ 0 ] . message . content ;
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" ,
messages : [
{ role : "system" , content : "You are an expert coding interview assistant. Provide clear, optimal solutions with detailed explanations." } ,
{ role : "user" , content : promptText }
] ,
max_tokens : 4000 ,
temperature : 0.2
} ) ;
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,10 +881,21 @@ 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 ) ;
const messages = [
{
role : "system" as const ,
content : ` 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.
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 ,
content : ` 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.
Your response MUST follow this exact structure with these section headers ( use # # # for headers ) :
# # # Issues Identified
@ -649,43 +914,124 @@ Here provide a clear explanation of why the changes are needed
- Summary bullet points of the most important takeaways
If you include code examples , use proper markdown code blocks with language specification ( e . g . \ ` \` \` java). `
} ,
{
role : "user" as const ,
content : [
{
type : "text" as const ,
text : ` I'm solving this coding problem: " ${ problemInfo . problem_statement } " in ${ language } . I need help with debugging or improving my solution. Here are screenshots of my code, the errors or test cases. Please provide a detailed analysis with:
} ,
{
role : "user" as const ,
content : [
{
type : "text" as const ,
text : ` I'm solving this coding problem: " ${ problemInfo . problem_statement } " in ${ language } . I need help with debugging or improving my solution. Here are screenshots of my code, the errors or test cases. Please provide a detailed analysis with:
1 . What issues you found in my code
2 . Specific improvements and corrections
3 . Any optimizations that would make the solution better
4 . A clear explanation of the changes needed `
} ,
. . . imageDataList . map ( data = > ( {
type : "image_url" as const ,
image_url : { url : ` data:image/png;base64, ${ data } ` }
} ) )
]
} ,
. . . imageDataList . map ( data = > ( {
type : "image_url" as const ,
image_url : { url : ` data:image/png;base64, ${ data } ` }
} ) )
]
}
] ;
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Analyzing code and generating debug feedback..." ,
progress : 60
} ) ;
}
] ;
// Update progress
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
message : "Analyzing code and generating debug feedback..." ,
progress : 60
const debugResponse = await this . openaiClient . chat . completions . create ( {
model : config.debuggingModel || "gpt-4o" ,
messages : messages ,
m ax_tokens: 4000 ,
temperature: 0.2
} ) ;
}
// Send to OpenAI Vision API
const debugResponse = await this . openaiClient . chat . completions . create ( {
model : config.debuggingModel || "gpt-4o" ,
messages : messages ,
max_tokens : 4000 ,
temperature : 0.2
} ) ;
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."
} ;
}
}
// Update final progress
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 )
}
}