@ -4,14 +4,38 @@ import path from "node:path"
import { ScreenshotHelper } from "./ScreenshotHelper"
import { ScreenshotHelper } from "./ScreenshotHelper"
import { IProcessingHelperDeps } from "./main"
import { IProcessingHelperDeps } from "./main"
import * as axios from "axios"
import * as axios from "axios"
import { app , BrowserWindow } from "electron"
import { app , BrowserWindow , dialog } from "electron"
import { OpenAI } from "openai"
import { OpenAI } from "openai"
import { configHelper } from "./ConfigHelper"
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 {
export class ProcessingHelper {
private deps : IProcessingHelperDeps
private deps : IProcessingHelperDeps
private screenshotHelper : ScreenshotHelper
private screenshotHelper : ScreenshotHelper
private openaiClient : OpenAI | null = null
private openaiClient : OpenAI | null = null
private geminiApiKey : string | null = null
// AbortControllers for API requests
// AbortControllers for API requests
private currentProcessingAbortController : AbortController | null = null
private currentProcessingAbortController : AbortController | null = null
@ -21,35 +45,51 @@ export class ProcessingHelper {
this . deps = deps
this . deps = deps
this . screenshotHelper = deps . getScreenshotHelper ( )
this . screenshotHelper = deps . getScreenshotHelper ( )
// Initialize Open AI client
// Initialize AI client based on config
this . initialize Open AIClient( ) ;
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' , ( ) = > {
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 {
try {
const config = configHelper . loadConfig ( ) ;
const config = configHelper . loadConfig ( ) ;
if ( config . apiKey ) {
this . openaiClient = new OpenAI ( {
if ( config . apiProvider === "openai" ) {
apiKey : config.apiKey ,
if ( config . apiKey ) {
timeout : 60000 , // 60 second timeout
this . openaiClient = new OpenAI ( {
maxRetries : 2 // Retry up to 2 times
apiKey : config.apiKey ,
} ) ;
timeout : 60000 , // 60 second timeout
console . log ( "OpenAI client initialized successfully" ) ;
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 {
} else {
// Gemini client initialization
this . openaiClient = null ;
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 ) {
} catch ( error ) {
console . error ( "Failed to initialize OpenAI client:" , error ) ;
console . error ( "Failed to initialize AI client:", error ) ;
this . openaiClient = null ;
this . openaiClient = null ;
this . geminiApiKey = null ;
}
}
}
}
@ -124,9 +164,11 @@ export class ProcessingHelper {
const mainWindow = this . deps . getMainWindow ( )
const mainWindow = this . deps . getMainWindow ( )
if ( ! mainWindow ) return
if ( ! mainWindow ) return
// First verify we have a valid OpenAI client
const config = configHelper . loadConfig ( ) ;
if ( ! this . openaiClient ) {
this . initializeOpenAIClient ( ) ;
// First verify we have a valid AI client
if ( config . apiProvider === "openai" && ! this . openaiClient ) {
this . initializeAIClient ( ) ;
if ( ! this . openaiClient ) {
if ( ! this . openaiClient ) {
console . error ( "OpenAI client not initialized" ) ;
console . error ( "OpenAI client not initialized" ) ;
@ -135,6 +177,16 @@ export class ProcessingHelper {
) ;
) ;
return ;
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 ( )
const view = this . deps . getView ( )
@ -144,9 +196,38 @@ export class ProcessingHelper {
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . INITIAL_START )
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . INITIAL_START )
const screenshotQueue = this . screenshotHelper . getScreenshotQueue ( )
const screenshotQueue = this . screenshotHelper . getScreenshotQueue ( )
console . log ( "Processing main queue screenshots:" , screenshotQueue )
console . log ( "Processing main queue screenshots:" , screenshotQueue )
if ( screenshotQueue . length === 0 ) {
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS )
// Check if the queue is empty
return
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 {
try {
@ -155,18 +236,32 @@ export class ProcessingHelper {
const { signal } = this . currentProcessingAbortController
const { signal } = this . currentProcessingAbortController
const screenshots = await Promise . all (
const screenshots = await Promise . all (
screenshotQueue . map ( async ( path ) = > ( {
existingScreenshots . map ( async ( path ) = > {
path ,
try {
preview : await this . screenshotHelper . getImagePreview ( path ) ,
return {
data : fs.readFileSync ( path ) . toString ( 'base64' )
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 ) {
if ( ! result . success ) {
console . log ( "Processing failed:" , result . error )
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 (
mainWindow . webContents . send (
this . deps . PROCESSING_EVENTS . API_KEY_INVALID
this . deps . PROCESSING_EVENTS . API_KEY_INVALID
)
)
@ -217,10 +312,39 @@ export class ProcessingHelper {
const extraScreenshotQueue =
const extraScreenshotQueue =
this . screenshotHelper . getExtraScreenshotQueue ( )
this . screenshotHelper . getExtraScreenshotQueue ( )
console . log ( "Processing extra queue screenshots:" , extraScreenshotQueue )
console . log ( "Processing extra queue screenshots:" , extraScreenshotQueue )
if ( extraScreenshotQueue . length === 0 ) {
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS )
// Check if the extra queue is empty
return
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 )
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . DEBUG_START )
// Initialize AbortController
// Initialize AbortController
@ -228,23 +352,46 @@ export class ProcessingHelper {
const { signal } = this . currentExtraProcessingAbortController
const { signal } = this . currentExtraProcessingAbortController
try {
try {
// Get all screenshots (both main and extra) for processing
const allPaths = [
. . . this . screenshotHelper . getScreenshotQueue ( ) ,
. . . existingExtraScreenshots
] ;
const screenshots = await Promise . all (
const screenshots = await Promise . all (
[
allPaths . map ( async ( path ) = > {
. . . this . screenshotHelper . getScreenshotQueue ( ) ,
try {
. . . extraScreenshotQueue
if ( ! fs . existsSync ( path ) ) {
] . map ( async ( path ) = > ( {
console . warn ( ` Screenshot file does not exist: ${ path } ` ) ;
path ,
return null ;
preview : await this . screenshotHelper . getImagePreview ( path ) ,
}
data : fs.readFileSync ( path ) . toString ( 'base64' )
} ) )
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 (
console . log (
"Combined screenshots for processing:" ,
"Combined screenshots for processing:" ,
screenshots . map ( ( s ) = > s . path )
validS creenshots. map ( ( s ) = > s . path )
)
)
const result = await this . processExtraScreenshotsHelper (
const result = await this . processExtraScreenshotsHelper (
screenshots ,
validS creenshots,
signal
signal
)
)
@ -287,19 +434,7 @@ export class ProcessingHelper {
const language = await this . getLanguage ( ) ;
const language = await this . getLanguage ( ) ;
const mainWindow = this . deps . getMainWindow ( ) ;
const mainWindow = this . deps . getMainWindow ( ) ;
// Verify OpenAI client
// Step 1: Extract problem info using AI Vision API (OpenAI or Gemini)
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
const imageDataList = screenshots . map ( screenshot = > screenshot . data ) ;
const imageDataList = screenshots . map ( screenshot = > screenshot . data ) ;
// Update the user on progress
// Update the user on progress
@ -310,56 +445,130 @@ export class ProcessingHelper {
} ) ;
} ) ;
}
}
const messages = [
let problemInfo ;
{
role : "system" as const ,
if ( config . apiProvider === "openai" ) {
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."
// Verify OpenAI client
} ,
if ( ! this . openaiClient ) {
{
this . initializeAIClient ( ) ; // Try to reinitialize
role : "user" as const ,
content : [
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 ,
role : "user" ,
text : ` Extract the coding problem details from these screenshots. Return in JSON format. Preferred coding language we gonna use for this problem is ${ language } . `
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 = > ( {
{ signal }
type : "image_url" as const ,
) ;
image_url : { url : ` data:image/png;base64, ${ data } ` }
} ) )
]
}
] ;
// Send to OpenAI Vision API
const responseData = response . data as GeminiResponse ;
const extractionResponse = await this . openaiClient . chat . completions . create ( {
model : config.extractionModel || "gpt-4o" ,
messages : messages ,
max_tokens : 4000 ,
temperature : 0.2
} ) ;
// Parse the response to get structured problem info
if ( ! responseData . candidates || responseData . candidates . length === 0 ) {
let problemInfo ;
throw new Error ( "Empty response from Gemini API" ) ;
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 ) ;
// Update the user on progress
const responseText = responseData . candidates [ 0 ] . content . parts [ 0 ] . text ;
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
// Handle when Gemini might wrap the JSON in markdown code blocks
message : "Problem analyzed successfully. Preparing to generate solution..." ,
const jsonText = responseText . replace ( /```json|```/g , '' ) . trim ( ) ;
progress : 40
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 ) ;
// Update the user on progress
return {
if ( mainWindow ) {
success : false ,
mainWindow . webContents . send ( "processing-status" , {
error : "Failed to parse problem information. Please try again or use clearer screenshots."
message : "Problem analyzed successfully. Preparing to generate solution..." ,
} ;
progress : 40
} ) ;
}
}
// Store problem info in AppState
// Store problem info in AppState
@ -443,13 +652,6 @@ export class ProcessingHelper {
throw new Error ( "No problem info available" ) ;
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
// Update progress status
if ( mainWindow ) {
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
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 .
Your solution should be efficient , well - commented , and handle edge cases .
` ;
` ;
// Send to OpenAI API
let responseContent ;
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
} ) ;
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
// Extract parts from the response
const codeMatch = responseContent . match ( /```(?:\w+)?\s*([\s\S]*?)```/ ) ;
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
// 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 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 ;
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 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." ;
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 ) ;
const timeMatch = responseContent . match ( timeComplexityPattern ) ;
if ( timeMatch && timeMatch [ 1 ] ) {
if ( timeMatch && timeMatch [ 1 ] ) {
timeComplexity = timeMatch [ 1 ] . trim ( ) ;
timeComplexity = timeMatch [ 1 ] . trim ( ) ;
// Ensure the response includes actual Big O notation and a dash
if ( ! timeComplexity . match ( /O\([^)]+\)/i ) ) {
if ( ! timeComplexity . match ( /O\([^)]+\)/i ) ) {
timeComplexity = ` O(n) - ${ timeComplexity } ` ;
timeComplexity = ` O(n) - ${ timeComplexity } ` ;
} else if ( ! timeComplexity . includes ( '-' ) && ! timeComplexity . includes ( 'because' ) ) {
} else if ( ! timeComplexity . includes ( '-' ) && ! timeComplexity . includes ( 'because' ) ) {
// Add a dash if there isn't one and no 'because'
const notationMatch = timeComplexity . match ( /O\([^)]+\)/i ) ;
const notationMatch = timeComplexity . match ( /O\([^)]+\)/i ) ;
if ( notationMatch ) {
if ( notationMatch ) {
const notation = notationMatch [ 0 ] ;
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 ) ;
const spaceMatch = responseContent . match ( spaceComplexityPattern ) ;
if ( spaceMatch && spaceMatch [ 1 ] ) {
if ( spaceMatch && spaceMatch [ 1 ] ) {
spaceComplexity = spaceMatch [ 1 ] . trim ( ) ;
spaceComplexity = spaceMatch [ 1 ] . trim ( ) ;
// Ensure the response includes actual Big O notation and a dash
if ( ! spaceComplexity . match ( /O\([^)]+\)/i ) ) {
if ( ! spaceComplexity . match ( /O\([^)]+\)/i ) ) {
spaceComplexity = ` O(n) - ${ spaceComplexity } ` ;
spaceComplexity = ` O(n) - ${ spaceComplexity } ` ;
} else if ( ! spaceComplexity . includes ( '-' ) && ! spaceComplexity . includes ( 'because' ) ) {
} else if ( ! spaceComplexity . includes ( '-' ) && ! spaceComplexity . includes ( 'because' ) ) {
// Add a dash if there isn't one and no 'because'
const notationMatch = spaceComplexity . match ( /O\([^)]+\)/i ) ;
const notationMatch = spaceComplexity . match ( /O\([^)]+\)/i ) ;
if ( notationMatch ) {
if ( notationMatch ) {
const notation = notationMatch [ 0 ] ;
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 = {
const formattedResponse = {
code : code ,
code : code ,
thoughts : thoughts.length > 0 ? thoughts : [ "Solution approach based on efficiency and readability" ] ,
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 } ;
return { success : true , data : formattedResponse } ;
} catch ( error : any ) {
} 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 ) {
if ( error ? . response ? . status === 401 ) {
return {
return {
success : false ,
success : false ,
@ -610,13 +871,6 @@ Your solution should be efficient, well-commented, and handle edge cases.
throw new Error ( "No problem info available" ) ;
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
// Update progress status
if ( mainWindow ) {
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
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
// Prepare the images for the API call
const imageDataList = screenshots . map ( screenshot = > screenshot . data ) ;
const imageDataList = screenshots . map ( screenshot = > screenshot . data ) ;
const messages = [
{
let debugContent ;
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.
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 ) :
Your response MUST follow this exact structure with these section headers ( use # # # for headers ) :
# # # Issues Identified
# # # 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
- Summary bullet points of the most important takeaways
If you include code examples , use proper markdown code blocks with language specification ( e . g . \ ` \` \` java). `
If you include code examples , use proper markdown code blocks with language specification ( e . g . \ ` \` \` java). `
} ,
} ,
{
{
role : "user" as const ,
role : "user" as const ,
content : [
content : [
{
{
type : "text" as const ,
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:
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
1 . What issues you found in my code
2 . Specific improvements and corrections
2 . Specific improvements and corrections
3 . Any optimizations that would make the solution better
3 . Any optimizations that would make the solution better
4 . A clear explanation of the changes needed `
4 . A clear explanation of the changes needed `
} ,
} ,
. . . imageDataList . map ( data = > ( {
. . . imageDataList . map ( data = > ( {
type : "image_url" as const ,
type : "image_url" as const ,
image_url : { url : ` data:image/png;base64, ${ data } ` }
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
const debugResponse = await this . openaiClient . chat . completions . create ( {
if ( mainWindow ) {
model : config.debuggingModel || "gpt-4o" ,
mainWindow . webContents . send ( "processing-status" , {
messages : messages ,
message : "Analyzing code and generating debug feedback..." ,
m ax_tokens: 4000 ,
progress : 60
temperature: 0.2
} ) ;
} ) ;
}
// Send to OpenAI Vision API
debugContent = debugResponse . choices [ 0 ] . message . content ;
const debugResponse = await this . openaiClient . chat . completions . create ( {
} else {
model : config.debuggingModel || "gpt-4o" ,
if ( ! this . geminiApiKey ) {
messages : messages ,
return {
max_tokens : 4000 ,
success : false ,
temperature : 0.2
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 ) {
if ( mainWindow ) {
mainWindow . webContents . send ( "processing-status" , {
mainWindow . webContents . send ( "processing-status" , {
message : "Debug analysis complete" ,
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" ;
let extractedCode = "// Debug mode - see analysis below" ;
const codeMatch = debugContent . match ( /```(?:[a-zA-Z]+)?([\s\S]*?)```/ ) ;
const codeMatch = debugContent . match ( /```(?:[a-zA-Z]+)?([\s\S]*?)```/ ) ;
if ( codeMatch && codeMatch [ 1 ] ) {
if ( codeMatch && codeMatch [ 1 ] ) {
extractedCode = codeMatch [ 1 ] . trim ( ) ;
extractedCode = codeMatch [ 1 ] . trim ( ) ;
}
}
// Format the debug content for better display - add clear section headings
let formattedDebugContent = debugContent ;
let formattedDebugContent = debugContent ;
// Ensure proper section formatting
if ( ! debugContent . includes ( '# ' ) && ! debugContent . includes ( '## ' ) ) {
if ( ! debugContent . includes ( '# ' ) && ! debugContent . includes ( '## ' ) ) {
formattedDebugContent = debugContent
formattedDebugContent = debugContent
. replace ( /issues identified|problems found|bugs found/i , '## Issues Identified' )
. 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' ) ;
. replace ( /explanation|detailed analysis/i , '## Explanation' ) ;
}
}
// Try to extract bullet points for "thoughts"
const bulletPoints = formattedDebugContent . match ( /(?:^|\n)[ ]*(?:[-*•]|\d+\.)[ ]+([^\n]+)/g ) ;
const bulletPoints = formattedDebugContent . match ( /(?:^|\n)[ ]*(?:[-*•]|\d+\.)[ ]+([^\n]+)/g ) ;
const thoughts = bulletPoints
const thoughts = bulletPoints
? bulletPoints . map ( point = > point . replace ( /^[ ]*(?:[-*•]|\d+\.)[ ]+/ , '' ) . trim ( ) ) . slice ( 0 , 5 )
? bulletPoints . map ( point = > point . replace ( /^[ ]*(?:[-*•]|\d+\.)[ ]+/ , '' ) . trim ( ) ) . slice ( 0 , 5 )
: [ "Debug analysis based on your screenshots" ] ;
: [ "Debug analysis based on your screenshots" ] ;
// Return the debug assistance in the format expected by the app
const response = {
const response = {
code : extractedCode ,
code : extractedCode ,
debug_analysis : formattedDebugContent ,
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 } ;
return { success : true , data : response } ;
} catch ( error : any ) {
} 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 ) ;
console . error ( "Debug processing error:" , error ) ;
return { success : false , error : error.message || "Failed to process debug request" } ;
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
wasCancelled = true
}
}
// Reset hasDebugged flag
this . deps . setHasDebugged ( false )
this . deps . setHasDebugged ( false )
// Clear any pending state
this . deps . setProblemInfo ( null )
this . deps . setProblemInfo ( null )
const mainWindow = this . deps . getMainWindow ( )
const mainWindow = this . deps . getMainWindow ( )
if ( wasCancelled && mainWindow && ! mainWindow . isDestroyed ( ) ) {
if ( wasCancelled && mainWindow && ! mainWindow . isDestroyed ( ) ) {
// Send a clear message that processing was cancelled
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS )
mainWindow . webContents . send ( this . deps . PROCESSING_EVENTS . NO_SCREENSHOTS )
}
}
}
}