diff --git a/bun.lockb b/bun.lockb index 7f7a2d4..b15147a 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/config.js b/config.js new file mode 100644 index 0000000..e69de29 diff --git a/electron/ConfigHelper.ts b/electron/ConfigHelper.ts index ba0d67c..5e02af2 100644 --- a/electron/ConfigHelper.ts +++ b/electron/ConfigHelper.ts @@ -7,6 +7,7 @@ import { OpenAI } from "openai" interface Config { apiKey: string; + apiProvider: "openai" | "gemini"; // Added provider selection extractionModel: string; solutionModel: string; debuggingModel: string; @@ -18,9 +19,10 @@ export class ConfigHelper extends EventEmitter { private configPath: string; private defaultConfig: Config = { apiKey: "", - extractionModel: "gpt-4o", - solutionModel: "gpt-4o", - debuggingModel: "gpt-4o", + apiProvider: "gemini", // Default to Gemini + extractionModel: "gemini-2.0-flash", // Default to Flash for faster responses + solutionModel: "gemini-2.0-flash", + debuggingModel: "gemini-2.0-flash", language: "python", opacity: 1.0 }; @@ -53,21 +55,27 @@ export class ConfigHelper extends EventEmitter { } } - /** - * Load the configuration from disk - */ /** * Validate and sanitize model selection to ensure only allowed models are used */ - private sanitizeModelSelection(model: string): string { - // Only allow gpt-4o and gpt-4o-mini - const allowedModels = ['gpt-4o', 'gpt-4o-mini']; - if (!allowedModels.includes(model)) { - // Default to gpt-4o if an invalid model is specified - console.warn(`Invalid model specified: ${model}. Using default model: gpt-4o`); - return 'gpt-4o'; + private sanitizeModelSelection(model: string, provider: "openai" | "gemini"): string { + if (provider === "openai") { + // Only allow gpt-4o and gpt-4o-mini for OpenAI + const allowedModels = ['gpt-4o', 'gpt-4o-mini']; + if (!allowedModels.includes(model)) { + console.warn(`Invalid OpenAI model specified: ${model}. Using default model: gpt-4o`); + return 'gpt-4o'; + } + return model; + } else { + // Only allow gemini-1.5-pro and gemini-2.0-flash for Gemini + const allowedModels = ['gemini-1.5-pro', 'gemini-2.0-flash']; + if (!allowedModels.includes(model)) { + console.warn(`Invalid Gemini model specified: ${model}. Using default model: gemini-2.0-flash`); + return 'gemini-2.0-flash'; // Changed default to flash + } + return model; } - return model; } public loadConfig(): Config { @@ -76,15 +84,20 @@ export class ConfigHelper extends EventEmitter { const configData = fs.readFileSync(this.configPath, 'utf8'); const config = JSON.parse(configData); + // Ensure apiProvider is a valid value + if (config.apiProvider !== "openai" && config.apiProvider !== "gemini") { + config.apiProvider = "gemini"; // Default to Gemini if invalid + } + // Sanitize model selections to ensure only allowed models are used if (config.extractionModel) { - config.extractionModel = this.sanitizeModelSelection(config.extractionModel); + config.extractionModel = this.sanitizeModelSelection(config.extractionModel, config.apiProvider); } if (config.solutionModel) { - config.solutionModel = this.sanitizeModelSelection(config.solutionModel); + config.solutionModel = this.sanitizeModelSelection(config.solutionModel, config.apiProvider); } if (config.debuggingModel) { - config.debuggingModel = this.sanitizeModelSelection(config.debuggingModel); + config.debuggingModel = this.sanitizeModelSelection(config.debuggingModel, config.apiProvider); } return { @@ -125,26 +138,55 @@ export class ConfigHelper extends EventEmitter { public updateConfig(updates: Partial): Config { try { const currentConfig = this.loadConfig(); + let provider = updates.apiProvider || currentConfig.apiProvider; + + // Auto-detect provider based on API key format if a new key is provided + if (updates.apiKey && !updates.apiProvider) { + // If API key starts with "sk-", it's likely an OpenAI key + if (updates.apiKey.trim().startsWith('sk-')) { + provider = "openai"; + console.log("Auto-detected OpenAI API key format"); + } else { + provider = "gemini"; + console.log("Using Gemini API key format (default)"); + } + + // Update the provider in the updates object + updates.apiProvider = provider; + } + + // If provider is changing, reset models to the default for that provider + if (updates.apiProvider && updates.apiProvider !== currentConfig.apiProvider) { + if (updates.apiProvider === "openai") { + updates.extractionModel = "gpt-4o"; + updates.solutionModel = "gpt-4o"; + updates.debuggingModel = "gpt-4o"; + } else { + updates.extractionModel = "gemini-2.0-flash"; + updates.solutionModel = "gemini-2.0-flash"; + updates.debuggingModel = "gemini-2.0-flash"; + } + } // Sanitize model selections in the updates if (updates.extractionModel) { - updates.extractionModel = this.sanitizeModelSelection(updates.extractionModel); + updates.extractionModel = this.sanitizeModelSelection(updates.extractionModel, provider); } if (updates.solutionModel) { - updates.solutionModel = this.sanitizeModelSelection(updates.solutionModel); + updates.solutionModel = this.sanitizeModelSelection(updates.solutionModel, provider); } if (updates.debuggingModel) { - updates.debuggingModel = this.sanitizeModelSelection(updates.debuggingModel); + updates.debuggingModel = this.sanitizeModelSelection(updates.debuggingModel, provider); } const newConfig = { ...currentConfig, ...updates }; this.saveConfig(newConfig); // Only emit update event for changes other than opacity - // This prevents re-initializing the OpenAI client when only opacity changes - if (updates.apiKey !== undefined || updates.extractionModel !== undefined || - updates.solutionModel !== undefined || updates.debuggingModel !== undefined || - updates.language !== undefined) { + // This prevents re-initializing the AI client when only opacity changes + if (updates.apiKey !== undefined || updates.apiProvider !== undefined || + updates.extractionModel !== undefined || updates.solutionModel !== undefined || + updates.debuggingModel !== undefined || updates.language !== undefined) { this.emit('config-updated', newConfig); } @@ -166,9 +208,25 @@ export class ConfigHelper extends EventEmitter { /** * Validate the API key format */ - public isValidApiKeyFormat(apiKey: string): boolean { - // Basic format validation for OpenAI API keys - return /^sk-[a-zA-Z0-9]{32,}$/.test(apiKey.trim()); + public isValidApiKeyFormat(apiKey: string, provider?: "openai" | "gemini"): boolean { + // If provider is not specified, attempt to auto-detect + if (!provider) { + if (apiKey.trim().startsWith('sk-')) { + provider = "openai"; + } else { + provider = "gemini"; + } + } + + if (provider === "openai") { + // Basic format validation for OpenAI API keys + return /^sk-[a-zA-Z0-9]{32,}$/.test(apiKey.trim()); + } else if (provider === "gemini") { + // Basic format validation for Gemini API keys (usually alphanumeric with no specific prefix) + return apiKey.trim().length >= 10; // Assuming Gemini keys are at least 10 chars + } + + return false; } /** @@ -204,24 +262,48 @@ export class ConfigHelper extends EventEmitter { } /** - * Test API key with OpenAI + * Test API key with the selected provider + */ + public async testApiKey(apiKey: string, provider?: "openai" | "gemini"): Promise<{valid: boolean, error?: string}> { + // Auto-detect provider based on key format if not specified + if (!provider) { + if (apiKey.trim().startsWith('sk-')) { + provider = "openai"; + console.log("Auto-detected OpenAI API key format for testing"); + } else { + provider = "gemini"; + console.log("Using Gemini API key format for testing (default)"); + } + } + + if (provider === "openai") { + return this.testOpenAIKey(apiKey); + } else if (provider === "gemini") { + return this.testGeminiKey(apiKey); + } + + return { valid: false, error: "Unknown API provider" }; + } + + /** + * Test OpenAI API key */ - public async testApiKey(apiKey: string): Promise<{valid: boolean, error?: string}> { + private async testOpenAIKey(apiKey: string): Promise<{valid: boolean, error?: string}> { try { const openai = new OpenAI({ apiKey }); // Make a simple API call to test the key await openai.models.list(); return { valid: true }; } catch (error: any) { - console.error('API key test failed:', error); + console.error('OpenAI API key test failed:', error); // Determine the specific error type for better error messages - let errorMessage = 'Unknown error validating API key'; + let errorMessage = 'Unknown error validating OpenAI API key'; if (error.status === 401) { - errorMessage = 'Invalid API key. Please check your key and try again.'; + errorMessage = 'Invalid API key. Please check your OpenAI key and try again.'; } else if (error.status === 429) { - errorMessage = 'Rate limit exceeded. Your API key has reached its request limit or has insufficient quota.'; + errorMessage = 'Rate limit exceeded. Your OpenAI API key has reached its request limit or has insufficient quota.'; } else if (error.status === 500) { errorMessage = 'OpenAI server error. Please try again later.'; } else if (error.message) { @@ -231,6 +313,31 @@ export class ConfigHelper extends EventEmitter { return { valid: false, error: errorMessage }; } } + + /** + * Test Gemini API key + * Note: This is a simplified implementation since we don't have the actual Gemini client + */ + private async testGeminiKey(apiKey: string): Promise<{valid: boolean, error?: string}> { + try { + // For now, we'll just do a basic check to ensure the key exists and has valid format + // In production, you would connect to the Gemini API and validate the key + if (apiKey && apiKey.trim().length >= 20) { + // Here you would actually validate the key with a Gemini API call + return { valid: true }; + } + return { valid: false, error: 'Invalid Gemini API key format.' }; + } catch (error: any) { + console.error('Gemini API key test failed:', error); + let errorMessage = 'Unknown error validating Gemini API key'; + + if (error.message) { + errorMessage = `Error: ${error.message}`; + } + + return { valid: false, error: errorMessage }; + } + } } // Export a singleton instance diff --git a/electron/ProcessingHelper.ts b/electron/ProcessingHelper.ts index dd6e3b6..bfc5568 100644 --- a/electron/ProcessingHelper.ts +++ b/electron/ProcessingHelper.ts @@ -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 OpenAI client - this.initializeOpenAIClient(); + // Initialize AI client based on config + this.initializeAIClient(); - // Listen for config changes to re-initialize the OpenAI client + // Listen for config changes to re-initialize the AI client configHelper.on('config-updated', () => { - this.initializeOpenAIClient(); + this.initializeAIClient(); }); } /** - * Initialize or reinitialize the OpenAI client with current config + * Initialize or reinitialize the AI client with current config */ - private initializeOpenAIClient(): void { + private initializeAIClient(): 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) + validScreenshots.map((s) => s.path) ) const result = await this.processExtraScreenshotsHelper( - screenshots, + validScreenshots, 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 @@ -309,57 +444,131 @@ export class ProcessingHelper { progress: 20 }); } + + let problemInfo; - 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}` } - })) - ] + 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." + }; + } } - ]; - // 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 - }); + // 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 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); - - // Update the user on progress - if (mainWindow) { - mainWindow.webContents.send("processing-status", { - message: "Problem analyzed successfully. Preparing to generate solution...", - progress: 40 - }); + // 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." + }; } - } 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." - }; + } 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) { + 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 - }); - - const responseContent = solutionResponse.choices[0].message.content; + 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", + 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, + 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 - // 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 - }); +### 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) } } diff --git a/electron/ScreenshotHelper.ts b/electron/ScreenshotHelper.ts index aee6818..3e82ed4 100644 --- a/electron/ScreenshotHelper.ts +++ b/electron/ScreenshotHelper.ts @@ -7,6 +7,7 @@ import { v4 as uuidv4 } from "uuid" import { execFile } from "child_process" import { promisify } from "util" import screenshot from "screenshot-desktop" +import os from "os" const execFileAsync = promisify(execFile) @@ -17,6 +18,7 @@ export class ScreenshotHelper { private readonly screenshotDir: string private readonly extraScreenshotDir: string + private readonly tempDir: string private view: "queue" | "solutions" | "debug" = "queue" @@ -29,19 +31,59 @@ export class ScreenshotHelper { app.getPath("userData"), "extra_screenshots" ) + this.tempDir = path.join(app.getPath("temp"), "interview-coder-screenshots") // Create directories if they don't exist - if (!fs.existsSync(this.screenshotDir)) { - fs.mkdirSync(this.screenshotDir, { recursive: true }) - } - if (!fs.existsSync(this.extraScreenshotDir)) { - fs.mkdirSync(this.extraScreenshotDir, { recursive: true }) - } + this.ensureDirectoriesExist(); + + // Load any existing screenshots from disk + this.loadExistingScreenshots(); + } + + private ensureDirectoriesExist(): void { + const directories = [this.screenshotDir, this.extraScreenshotDir, this.tempDir]; - // Ensure temp directory exists - const tempDir = app.getPath("temp") - if (!fs.existsSync(tempDir)) { - fs.mkdirSync(tempDir, { recursive: true }) + for (const dir of directories) { + if (!fs.existsSync(dir)) { + try { + fs.mkdirSync(dir, { recursive: true }); + console.log(`Created directory: ${dir}`); + } catch (err) { + console.error(`Error creating directory ${dir}:`, err); + } + } + } + } + + private loadExistingScreenshots(): void { + try { + // Load main queue screenshots + if (fs.existsSync(this.screenshotDir)) { + const files = fs.readdirSync(this.screenshotDir) + .filter(file => file.endsWith('.png')) + .map(file => path.join(this.screenshotDir, file)) + .sort((a, b) => { + return fs.statSync(a).mtime.getTime() - fs.statSync(b).mtime.getTime(); + }); + + this.screenshotQueue = files.slice(0, this.MAX_SCREENSHOTS); + console.log(`Loaded ${this.screenshotQueue.length} existing screenshots`); + } + + // Load extra queue screenshots + if (fs.existsSync(this.extraScreenshotDir)) { + const files = fs.readdirSync(this.extraScreenshotDir) + .filter(file => file.endsWith('.png')) + .map(file => path.join(this.extraScreenshotDir, file)) + .sort((a, b) => { + return fs.statSync(a).mtime.getTime() - fs.statSync(b).mtime.getTime(); + }); + + this.extraScreenshotQueue = files.slice(0, this.MAX_SCREENSHOTS); + console.log(`Loaded ${this.extraScreenshotQueue.length} existing extra screenshots`); + } + } catch (err) { + console.error("Error loading existing screenshots:", err); } } @@ -94,18 +136,114 @@ export class ScreenshotHelper { private async captureScreenshot(): Promise { try { - // Ensure temp directory exists - const tempDir = app.getPath("temp") - if (!fs.existsSync(tempDir)) { - fs.mkdirSync(tempDir, { recursive: true }) - } + console.log("Starting screenshot capture..."); + + // For Windows, try multiple methods + if (process.platform === 'win32') { + return await this.captureWindowsScreenshot(); + } - // Use screenshot-desktop for cross-platform support + // For macOS and Linux, use buffer directly + console.log("Taking screenshot on non-Windows platform"); const buffer = await screenshot({ format: 'png' }); + console.log(`Screenshot captured successfully, size: ${buffer.length} bytes`); return buffer; } catch (error) { console.error("Error capturing screenshot:", error); - throw error; + throw new Error(`Failed to capture screenshot: ${error.message}`); + } + } + + /** + * Windows-specific screenshot capture with multiple fallback mechanisms + */ + private async captureWindowsScreenshot(): Promise { + console.log("Attempting Windows screenshot with multiple methods"); + + // Method 1: Try screenshot-desktop with filename first + try { + const tempFile = path.join(this.tempDir, `temp-${uuidv4()}.png`); + console.log(`Taking Windows screenshot to temp file (Method 1): ${tempFile}`); + + await screenshot({ filename: tempFile }); + + if (fs.existsSync(tempFile)) { + const buffer = await fs.promises.readFile(tempFile); + console.log(`Method 1 successful, screenshot size: ${buffer.length} bytes`); + + // Cleanup temp file + try { + await fs.promises.unlink(tempFile); + } catch (cleanupErr) { + console.warn("Failed to clean up temp file:", cleanupErr); + } + + return buffer; + } else { + console.log("Method 1 failed: File not created"); + throw new Error("Screenshot file not created"); + } + } catch (error) { + console.warn("Windows screenshot Method 1 failed:", error); + + // Method 2: Try using PowerShell + try { + console.log("Attempting Windows screenshot with PowerShell (Method 2)"); + const tempFile = path.join(this.tempDir, `ps-temp-${uuidv4()}.png`); + + // PowerShell command to take screenshot using .NET classes + const psScript = ` + Add-Type -AssemblyName System.Windows.Forms,System.Drawing + $screens = [System.Windows.Forms.Screen]::AllScreens + $top = ($screens | ForEach-Object {$_.Bounds.Top} | Measure-Object -Minimum).Minimum + $left = ($screens | ForEach-Object {$_.Bounds.Left} | Measure-Object -Minimum).Minimum + $width = ($screens | ForEach-Object {$_.Bounds.Right} | Measure-Object -Maximum).Maximum + $height = ($screens | ForEach-Object {$_.Bounds.Bottom} | Measure-Object -Maximum).Maximum + $bounds = [System.Drawing.Rectangle]::FromLTRB($left, $top, $width, $height) + $bmp = New-Object System.Drawing.Bitmap $bounds.Width, $bounds.Height + $graphics = [System.Drawing.Graphics]::FromImage($bmp) + $graphics.CopyFromScreen($bounds.Left, $bounds.Top, 0, 0, $bounds.Size) + $bmp.Save('${tempFile.replace(/\\/g, '\\\\')}', [System.Drawing.Imaging.ImageFormat]::Png) + $graphics.Dispose() + $bmp.Dispose() + `; + + // Execute PowerShell + await execFileAsync('powershell', [ + '-NoProfile', + '-ExecutionPolicy', 'Bypass', + '-Command', psScript + ]); + + // Check if file exists and read it + if (fs.existsSync(tempFile)) { + const buffer = await fs.promises.readFile(tempFile); + console.log(`Method 2 successful, screenshot size: ${buffer.length} bytes`); + + // Cleanup + try { + await fs.promises.unlink(tempFile); + } catch (err) { + console.warn("Failed to clean up PowerShell temp file:", err); + } + + return buffer; + } else { + throw new Error("PowerShell screenshot file not created"); + } + } catch (psError) { + console.warn("Windows PowerShell screenshot failed:", psError); + + // Method 3: Last resort - create a tiny placeholder image + console.log("All screenshot methods failed, creating placeholder image"); + + // Create a 1x1 transparent PNG as fallback + const fallbackBuffer = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64'); + console.log("Created placeholder image as fallback"); + + // Show the error but return a valid buffer so the app doesn't crash + throw new Error("Could not capture screenshot with any method. Please check your Windows security settings and try again."); + } } } @@ -115,12 +253,19 @@ export class ScreenshotHelper { ): Promise { console.log("Taking screenshot in view:", this.view) hideMainWindow() - await new Promise((resolve) => setTimeout(resolve, 100)) + + // Increased delay for window hiding on Windows + const hideDelay = process.platform === 'win32' ? 500 : 300; + await new Promise((resolve) => setTimeout(resolve, hideDelay)) let screenshotPath = "" try { // Get screenshot buffer using cross-platform method const screenshotBuffer = await this.captureScreenshot(); + + if (!screenshotBuffer || screenshotBuffer.length === 0) { + throw new Error("Screenshot capture returned empty buffer"); + } // Save and manage the screenshot based on current view if (this.view === "queue") { @@ -167,7 +312,8 @@ export class ScreenshotHelper { console.error("Screenshot error:", error) throw error } finally { - await new Promise((resolve) => setTimeout(resolve, 50)) + // Increased delay for showing window again + await new Promise((resolve) => setTimeout(resolve, 200)) showMainWindow() } @@ -176,11 +322,16 @@ export class ScreenshotHelper { public async getImagePreview(filepath: string): Promise { try { + if (!fs.existsSync(filepath)) { + console.error(`Image file not found: ${filepath}`); + return ''; + } + const data = await fs.promises.readFile(filepath) return `data:image/png;base64,${data.toString("base64")}` } catch (error) { console.error("Error reading image:", error) - throw error + return '' } } @@ -188,7 +339,10 @@ export class ScreenshotHelper { path: string ): Promise<{ success: boolean; error?: string }> { try { - await fs.promises.unlink(path) + if (fs.existsSync(path)) { + await fs.promises.unlink(path) + } + if (this.view === "queue") { this.screenshotQueue = this.screenshotQueue.filter( (filePath) => filePath !== path @@ -208,13 +362,15 @@ export class ScreenshotHelper { public clearExtraScreenshotQueue(): void { // Clear extraScreenshotQueue this.extraScreenshotQueue.forEach((screenshotPath) => { - fs.unlink(screenshotPath, (err) => { - if (err) - console.error( - `Error deleting extra screenshot at ${screenshotPath}:`, - err - ) - }) + if (fs.existsSync(screenshotPath)) { + fs.unlink(screenshotPath, (err) => { + if (err) + console.error( + `Error deleting extra screenshot at ${screenshotPath}:`, + err + ) + }) + } }) this.extraScreenshotQueue = [] } diff --git a/invisible_launcher.vbs b/invisible_launcher.vbs new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index 832cd8b..1f593c8 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,9 @@ "start": "cross-env NODE_ENV=development concurrently \"tsc -p tsconfig.electron.json\" \"vite\" \"wait-on -t 30000 http://localhost:54321 && electron ./dist-electron/main.js\"", "build": "cross-env NODE_ENV=production rimraf dist dist-electron && vite build && tsc -p tsconfig.electron.json", "run-prod": "cross-env NODE_ENV=production electron ./dist-electron/main.js", - "package": "yarn build && electron-builder build", - "package-mac": "yarn build && electron-builder build --mac", - "package-win": "yarn build && electron-builder build --win" + "package": "npm run build && electron-builder build", + "package-mac": "npm run build && electron-builder build --mac", + "package-win": "npm run build && electron-builder build --win" }, "build": { "appId": "com.chunginlee.interviewcoder", @@ -121,6 +121,7 @@ "@radix-ui/react-label": "^2.1.0", "@radix-ui/react-slot": "^1.1.0", "@radix-ui/react-toast": "^1.2.2", + "@supabase/supabase-js": "^2.49.4", "@tanstack/react-query": "^5.64.0", "axios": "^1.7.7", "class-variance-authority": "^0.7.1", @@ -186,4 +187,4 @@ "last 1 safari version" ] } -} +} \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index a448082..f2dd348 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -37,7 +37,7 @@ function App() { open: false, title: "", description: "", - variant: "neutral" as const + variant: "neutral" as "neutral" | "success" | "error" }) const [credits, setCredits] = useState(999) // Unlimited credits const [currentLanguage, setCurrentLanguage] = useState("python") diff --git a/src/components/Settings/SettingsDialog.tsx b/src/components/Settings/SettingsDialog.tsx index be685c2..9f0229b 100644 --- a/src/components/Settings/SettingsDialog.tsx +++ b/src/components/Settings/SettingsDialog.tsx @@ -13,7 +13,9 @@ import { Button } from "../ui/button"; import { Settings } from "lucide-react"; import { useToast } from "../../contexts/toast"; -type OpenAIModel = { +type APIProvider = "openai" | "gemini"; + +type AIModel = { id: string; name: string; description: string; @@ -23,7 +25,8 @@ type ModelCategory = { key: 'extractionModel' | 'solutionModel' | 'debuggingModel'; title: string; description: string; - models: OpenAIModel[]; + openaiModels: AIModel[]; + geminiModels: AIModel[]; }; // Define available models for each category @@ -32,7 +35,7 @@ const modelCategories: ModelCategory[] = [ key: 'extractionModel', title: 'Problem Extraction', description: 'Model used to analyze screenshots and extract problem details', - models: [ + openaiModels: [ { id: "gpt-4o", name: "gpt-4o", @@ -43,13 +46,25 @@ const modelCategories: ModelCategory[] = [ name: "gpt-4o-mini", description: "Faster, more cost-effective option" } + ], + geminiModels: [ + { + id: "gemini-1.5-pro", + name: "Gemini 1.5 Pro", + description: "Best overall performance for problem extraction" + }, + { + id: "gemini-2.0-flash", + name: "Gemini 2.0 Flash", + description: "Faster, more cost-effective option" + } ] }, { key: 'solutionModel', title: 'Solution Generation', description: 'Model used to generate coding solutions', - models: [ + openaiModels: [ { id: "gpt-4o", name: "gpt-4o", @@ -60,13 +75,25 @@ const modelCategories: ModelCategory[] = [ name: "gpt-4o-mini", description: "Faster, more cost-effective option" } + ], + geminiModels: [ + { + id: "gemini-1.5-pro", + name: "Gemini 1.5 Pro", + description: "Strong overall performance for coding tasks" + }, + { + id: "gemini-2.0-flash", + name: "Gemini 2.0 Flash", + description: "Faster, more cost-effective option" + } ] }, { key: 'debuggingModel', title: 'Debugging', description: 'Model used to debug and improve solutions', - models: [ + openaiModels: [ { id: "gpt-4o", name: "gpt-4o", @@ -77,6 +104,18 @@ const modelCategories: ModelCategory[] = [ name: "gpt-4o-mini", description: "Faster, more cost-effective option" } + ], + geminiModels: [ + { + id: "gemini-1.5-pro", + name: "Gemini 1.5 Pro", + description: "Best for analyzing code and error messages" + }, + { + id: "gemini-2.0-flash", + name: "Gemini 2.0 Flash", + description: "Faster, more cost-effective option" + } ] } ]; @@ -89,6 +128,7 @@ interface SettingsDialogProps { export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDialogProps) { const [open, setOpen] = useState(externalOpen || false); const [apiKey, setApiKey] = useState(""); + const [apiProvider, setApiProvider] = useState("openai"); const [extractionModel, setExtractionModel] = useState("gpt-4o"); const [solutionModel, setSolutionModel] = useState("gpt-4o"); const [debuggingModel, setDebuggingModel] = useState("gpt-4o"); @@ -115,15 +155,24 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia useEffect(() => { if (open) { setIsLoading(true); + interface Config { + apiKey?: string; + apiProvider?: APIProvider; + extractionModel?: string; + solutionModel?: string; + debuggingModel?: string; + } + window.electronAPI .getConfig() - .then((config) => { + .then((config: Config) => { setApiKey(config.apiKey || ""); + setApiProvider(config.apiProvider || "openai"); setExtractionModel(config.extractionModel || "gpt-4o"); setSolutionModel(config.solutionModel || "gpt-4o"); setDebuggingModel(config.debuggingModel || "gpt-4o"); }) - .catch((error) => { + .catch((error: unknown) => { console.error("Failed to load config:", error); showToast("Error", "Failed to load settings", "error"); }) @@ -133,11 +182,28 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia } }, [open, showToast]); + // Handle API provider change + const handleProviderChange = (provider: APIProvider) => { + setApiProvider(provider); + + // Reset models to defaults when changing provider + if (provider === "openai") { + setExtractionModel("gpt-4o"); + setSolutionModel("gpt-4o"); + setDebuggingModel("gpt-4o"); + } else { + setExtractionModel("gemini-1.5-pro"); + setSolutionModel("gemini-1.5-pro"); + setDebuggingModel("gemini-1.5-pro"); + } + }; + const handleSave = async () => { setIsLoading(true); try { const result = await window.electronAPI.updateConfig({ apiKey, + apiProvider, extractionModel, solutionModel, debuggingModel, @@ -173,7 +239,6 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia return ( - {/* Removed unnecessary dialog trigger - using the icon in QueueCommands instead */} - OpenAI API Settings + > + + API Settings - Configure your OpenAI API key and model preferences. You'll need your own OpenAI API key to use this application. + Configure your API key and model preferences. You'll need your own API key to use this application.
+ {/* API Provider Selection */} +
+ +
+
handleProviderChange("openai")} + > +
+
+
+

OpenAI

+

GPT-4o models

+
+
+
+
handleProviderChange("gemini")} + > +
+
+
+

Gemini

+

Gemini 1.5 models

+
+
+
+
+
+
setApiKey(e.target.value)} - placeholder="sk-..." + placeholder={apiProvider === "openai" ? "sk-..." : "Enter your Gemini API key"} className="bg-black/50 border-white/10 text-white" /> {apiKey && ( @@ -218,19 +331,35 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia

)}

- Your API key is stored locally and never sent to any server except OpenAI + Your API key is stored locally and never sent to any server except {apiProvider === "openai" ? "OpenAI" : "Google"}

Don't have an API key?

-

1. Create an account at -

-

2. Go to section -

-

3. Create a new secret key and paste it here

+ {apiProvider === "openai" ? ( + <> +

1. Create an account at +

+

2. Go to section +

+

3. Create a new secret key and paste it here

+ + ) : ( + <> +

1. Create an account at +

+

2. Go to the section +

+

3. Create a new API key and paste it here

+ + )}
@@ -283,54 +412,59 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia Select which models to use for each stage of the process

- {modelCategories.map((category) => ( -
- -

{category.description}

- -
- {category.models.map((m) => { - // Determine which state to use based on category key - const currentValue = - category.key === 'extractionModel' ? extractionModel : - category.key === 'solutionModel' ? solutionModel : - debuggingModel; - - // Determine which setter function to use - const setValue = - category.key === 'extractionModel' ? setExtractionModel : - category.key === 'solutionModel' ? setSolutionModel : - setDebuggingModel; + {modelCategories.map((category) => { + // Get the appropriate model list based on selected provider + const models = apiProvider === "openai" ? category.openaiModels : category.geminiModels; + + return ( +
+ +

{category.description}

+ +
+ {models.map((m) => { + // Determine which state to use based on category key + const currentValue = + category.key === 'extractionModel' ? extractionModel : + category.key === 'solutionModel' ? solutionModel : + debuggingModel; - return ( -
setValue(m.id)} - > -
-
-
-

{m.name}

-

{m.description}

+ // Determine which setter function to use + const setValue = + category.key === 'extractionModel' ? setExtractionModel : + category.key === 'solutionModel' ? setSolutionModel : + setDebuggingModel; + + return ( +
setValue(m.id)} + > +
+
+
+

{m.name}

+

{m.description}

+
-
- ); - })} + ); + })} +
-
- ))} + ); + })}
diff --git a/stealth-run.bat b/stealth-run.bat index 927a980..591b195 100644 --- a/stealth-run.bat +++ b/stealth-run.bat @@ -44,4 +44,4 @@ echo. echo If you encounter any issues: echo 1. Make sure you've installed dependencies with 'npm install' echo 2. Press Ctrl+B multiple times to toggle visibility -echo 3. Check Task Manager to verify the app is running +echo 3. Check Task Manager to verify the app is running \ No newline at end of file