Added geminiapi for free users (#24)

* Added geminiapi for free users

* Updated package.json and stealth-run.bat back to npm from bun to align with readme

---------

Co-authored-by: Ornithopter-pilot <greeneub@gmail.com>
main
Chiragbisht 2 years ago committed by GitHub
parent 2a0d022bbc
commit cce347d97f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

Binary file not shown.

@ -7,6 +7,7 @@ import { OpenAI } from "openai"
interface Config { interface Config {
apiKey: string; apiKey: string;
apiProvider: "openai" | "gemini"; // Added provider selection
extractionModel: string; extractionModel: string;
solutionModel: string; solutionModel: string;
debuggingModel: string; debuggingModel: string;
@ -18,9 +19,10 @@ export class ConfigHelper extends EventEmitter {
private configPath: string; private configPath: string;
private defaultConfig: Config = { private defaultConfig: Config = {
apiKey: "", apiKey: "",
extractionModel: "gpt-4o", apiProvider: "gemini", // Default to Gemini
solutionModel: "gpt-4o", extractionModel: "gemini-2.0-flash", // Default to Flash for faster responses
debuggingModel: "gpt-4o", solutionModel: "gemini-2.0-flash",
debuggingModel: "gemini-2.0-flash",
language: "python", language: "python",
opacity: 1.0 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 * Validate and sanitize model selection to ensure only allowed models are used
*/ */
private sanitizeModelSelection(model: string): string { private sanitizeModelSelection(model: string, provider: "openai" | "gemini"): string {
// Only allow gpt-4o and gpt-4o-mini if (provider === "openai") {
// Only allow gpt-4o and gpt-4o-mini for OpenAI
const allowedModels = ['gpt-4o', 'gpt-4o-mini']; const allowedModels = ['gpt-4o', 'gpt-4o-mini'];
if (!allowedModels.includes(model)) { if (!allowedModels.includes(model)) {
// Default to gpt-4o if an invalid model is specified console.warn(`Invalid OpenAI model specified: ${model}. Using default model: gpt-4o`);
console.warn(`Invalid model specified: ${model}. Using default model: gpt-4o`);
return 'gpt-4o'; return 'gpt-4o';
} }
return model; 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;
}
} }
public loadConfig(): Config { public loadConfig(): Config {
@ -76,15 +84,20 @@ export class ConfigHelper extends EventEmitter {
const configData = fs.readFileSync(this.configPath, 'utf8'); const configData = fs.readFileSync(this.configPath, 'utf8');
const config = JSON.parse(configData); 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 // Sanitize model selections to ensure only allowed models are used
if (config.extractionModel) { if (config.extractionModel) {
config.extractionModel = this.sanitizeModelSelection(config.extractionModel); config.extractionModel = this.sanitizeModelSelection(config.extractionModel, config.apiProvider);
} }
if (config.solutionModel) { if (config.solutionModel) {
config.solutionModel = this.sanitizeModelSelection(config.solutionModel); config.solutionModel = this.sanitizeModelSelection(config.solutionModel, config.apiProvider);
} }
if (config.debuggingModel) { if (config.debuggingModel) {
config.debuggingModel = this.sanitizeModelSelection(config.debuggingModel); config.debuggingModel = this.sanitizeModelSelection(config.debuggingModel, config.apiProvider);
} }
return { return {
@ -125,26 +138,55 @@ export class ConfigHelper extends EventEmitter {
public updateConfig(updates: Partial<Config>): Config { public updateConfig(updates: Partial<Config>): Config {
try { try {
const currentConfig = this.loadConfig(); 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 // Sanitize model selections in the updates
if (updates.extractionModel) { if (updates.extractionModel) {
updates.extractionModel = this.sanitizeModelSelection(updates.extractionModel); updates.extractionModel = this.sanitizeModelSelection(updates.extractionModel, provider);
} }
if (updates.solutionModel) { if (updates.solutionModel) {
updates.solutionModel = this.sanitizeModelSelection(updates.solutionModel); updates.solutionModel = this.sanitizeModelSelection(updates.solutionModel, provider);
} }
if (updates.debuggingModel) { if (updates.debuggingModel) {
updates.debuggingModel = this.sanitizeModelSelection(updates.debuggingModel); updates.debuggingModel = this.sanitizeModelSelection(updates.debuggingModel, provider);
} }
const newConfig = { ...currentConfig, ...updates }; const newConfig = { ...currentConfig, ...updates };
this.saveConfig(newConfig); this.saveConfig(newConfig);
// Only emit update event for changes other than opacity // Only emit update event for changes other than opacity
// This prevents re-initializing the OpenAI client when only opacity changes // This prevents re-initializing the AI client when only opacity changes
if (updates.apiKey !== undefined || updates.extractionModel !== undefined || if (updates.apiKey !== undefined || updates.apiProvider !== undefined ||
updates.solutionModel !== undefined || updates.debuggingModel !== undefined || updates.extractionModel !== undefined || updates.solutionModel !== undefined ||
updates.language !== undefined) { updates.debuggingModel !== undefined || updates.language !== undefined) {
this.emit('config-updated', newConfig); this.emit('config-updated', newConfig);
} }
@ -166,9 +208,25 @@ export class ConfigHelper extends EventEmitter {
/** /**
* Validate the API key format * Validate the API key format
*/ */
public isValidApiKeyFormat(apiKey: string): boolean { 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 // Basic format validation for OpenAI API keys
return /^sk-[a-zA-Z0-9]{32,}$/.test(apiKey.trim()); 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): Promise<{valid: boolean, error?: string}> { 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
*/
private async testOpenAIKey(apiKey: string): Promise<{valid: boolean, error?: string}> {
try { try {
const openai = new OpenAI({ apiKey }); const openai = new OpenAI({ apiKey });
// Make a simple API call to test the key // Make a simple API call to test the key
await openai.models.list(); await openai.models.list();
return { valid: true }; return { valid: true };
} catch (error: any) { } 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 // 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) { 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) { } 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) { } else if (error.status === 500) {
errorMessage = 'OpenAI server error. Please try again later.'; errorMessage = 'OpenAI server error. Please try again later.';
} else if (error.message) { } else if (error.message) {
@ -231,6 +313,31 @@ export class ConfigHelper extends EventEmitter {
return { valid: false, error: errorMessage }; 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 // Export a singleton instance

@ -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 OpenAI client // Initialize AI client based on config
this.initializeOpenAIClient(); 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', () => { 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 { try {
const config = configHelper.loadConfig(); const config = configHelper.loadConfig();
if (config.apiProvider === "openai") {
if (config.apiKey) { if (config.apiKey) {
this.openaiClient = new OpenAI({ this.openaiClient = new OpenAI({
apiKey: config.apiKey, apiKey: config.apiKey,
timeout: 60000, // 60 second timeout timeout: 60000, // 60 second timeout
maxRetries: 2 // Retry up to 2 times maxRetries: 2 // Retry up to 2 times
}); });
this.geminiApiKey = null;
console.log("OpenAI client initialized successfully"); console.log("OpenAI client initialized successfully");
} else { } else {
this.openaiClient = null; this.openaiClient = null;
this.geminiApiKey = null;
console.warn("No API key available, OpenAI client not initialized"); console.warn("No API key available, OpenAI client not initialized");
} }
} else {
// Gemini client initialization
this.openaiClient = null;
if (config.apiKey) {
this.geminiApiKey = config.apiKey;
console.log("Gemini API key set successfully");
} else {
this.geminiApiKey = null;
console.warn("No API key available, Gemini client not initialized");
}
}
} catch (error) { } 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) => {
try {
return {
path, path,
preview: await this.screenshotHelper.getImagePreview(path), preview: await this.screenshotHelper.getImagePreview(path),
data: fs.readFileSync(path).toString('base64') 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 {
const screenshots = await Promise.all( // Get all screenshots (both main and extra) for processing
[ const allPaths = [
...this.screenshotHelper.getScreenshotQueue(), ...this.screenshotHelper.getScreenshotQueue(),
...extraScreenshotQueue ...existingExtraScreenshots
].map(async (path) => ({ ];
const screenshots = await Promise.all(
allPaths.map(async (path) => {
try {
if (!fs.existsSync(path)) {
console.warn(`Screenshot file does not exist: ${path}`);
return null;
}
return {
path, path,
preview: await this.screenshotHelper.getImagePreview(path), preview: await this.screenshotHelper.getImagePreview(path),
data: fs.readFileSync(path).toString('base64') 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) validScreenshots.map((s) => s.path)
) )
const result = await this.processExtraScreenshotsHelper( const result = await this.processExtraScreenshotsHelper(
screenshots, validScreenshots,
signal signal
) )
@ -287,9 +434,23 @@ export class ProcessingHelper {
const language = await this.getLanguage(); const language = await this.getLanguage();
const mainWindow = this.deps.getMainWindow(); const mainWindow = this.deps.getMainWindow();
// Step 1: Extract problem info using AI Vision API (OpenAI or Gemini)
const imageDataList = screenshots.map(screenshot => screenshot.data);
// Update the user on progress
if (mainWindow) {
mainWindow.webContents.send("processing-status", {
message: "Analyzing problem from screenshots...",
progress: 20
});
}
let problemInfo;
if (config.apiProvider === "openai") {
// Verify OpenAI client // Verify OpenAI client
if (!this.openaiClient) { if (!this.openaiClient) {
this.initializeOpenAIClient(); // Try to reinitialize this.initializeAIClient(); // Try to reinitialize
if (!this.openaiClient) { if (!this.openaiClient) {
return { return {
@ -299,17 +460,7 @@ export class ProcessingHelper {
} }
} }
// Step 1: Extract problem info using OpenAI Vision API // Use OpenAI for processing
const imageDataList = screenshots.map(screenshot => screenshot.data);
// Update the user on progress
if (mainWindow) {
mainWindow.webContents.send("processing-status", {
message: "Analyzing problem from screenshots...",
progress: 20
});
}
const messages = [ const messages = [
{ {
role: "system" as const, role: "system" as const,
@ -338,13 +489,79 @@ export class ProcessingHelper {
temperature: 0.2 temperature: 0.2
}); });
// Parse the response to get structured problem info // Parse the response
let problemInfo;
try { try {
const responseText = extractionResponse.choices[0].message.content; const responseText = extractionResponse.choices[0].message.content;
// Handle when OpenAI might wrap the JSON in markdown code blocks // Handle when OpenAI might wrap the JSON in markdown code blocks
const jsonText = responseText.replace(/```json|```/g, '').trim(); const jsonText = responseText.replace(/```json|```/g, '').trim();
problemInfo = JSON.parse(jsonText); problemInfo = JSON.parse(jsonText);
} catch (error) {
console.error("Error parsing OpenAI response:", error);
return {
success: false,
error: "Failed to parse problem information. Please try again or use clearer screenshots."
};
}
} else {
// Use Gemini API
if (!this.geminiApiKey) {
return {
success: false,
error: "Gemini API key not configured. Please check your settings."
};
}
try {
// Create Gemini message structure
const geminiMessages: GeminiMessage[] = [
{
role: "user",
parts: [
{
text: `You are a coding challenge interpreter. Analyze the screenshots of the coding problem and extract all relevant information. Return the information in JSON format with these fields: problem_statement, constraints, example_input, example_output. Just return the structured JSON without any other text. Preferred coding language we gonna use for this problem is ${language}.`
},
...imageDataList.map(data => ({
inlineData: {
mimeType: "image/png",
data: data
}
}))
]
}
];
// Make API request to Gemini
const response = await axios.default.post(
`https://generativelanguage.googleapis.com/v1beta/models/${config.extractionModel || "gemini-2.0-flash"}:generateContent?key=${this.geminiApiKey}`,
{
contents: geminiMessages,
generationConfig: {
temperature: 0.2,
maxOutputTokens: 4000
}
},
{ signal }
);
const responseData = response.data as GeminiResponse;
if (!responseData.candidates || responseData.candidates.length === 0) {
throw new Error("Empty response from Gemini API");
}
const responseText = responseData.candidates[0].content.parts[0].text;
// Handle when Gemini might wrap the JSON in markdown code blocks
const jsonText = responseText.replace(/```json|```/g, '').trim();
problemInfo = JSON.parse(jsonText);
} catch (error) {
console.error("Error using Gemini API:", error);
return {
success: false,
error: "Failed to process with Gemini API. Please check your API key or try again later."
};
}
}
// Update the user on progress // Update the user on progress
if (mainWindow) { if (mainWindow) {
@ -353,14 +570,6 @@ export class ProcessingHelper {
progress: 40 progress: 40
}); });
} }
} catch (error) {
console.error("Error parsing problem extraction response:", error);
console.log("Raw response:", extractionResponse.choices[0].message.content);
return {
success: false,
error: "Failed to parse problem information. Please try again or use clearer screenshots."
};
}
// Store problem info in AppState // Store problem info in AppState
this.deps.setProblemInfo(problemInfo); this.deps.setProblemInfo(problemInfo);
@ -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,9 +689,20 @@ 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.
`; `;
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 // Send to OpenAI API
const solutionResponse = await this.openaiClient.chat.completions.create({ const solutionResponse = await this.openaiClient.chat.completions.create({
model: config.solutionModel || "gpt-4o", // Using selected model for code generation model: config.solutionModel || "gpt-4o",
messages: [ messages: [
{ role: "system", content: "You are an expert coding interview assistant. Provide clear, optimal solutions with detailed explanations." }, { role: "system", content: "You are an expert coding interview assistant. Provide clear, optimal solutions with detailed explanations." },
{ role: "user", content: promptText } { role: "user", content: promptText }
@ -498,7 +711,57 @@ Your solution should be efficient, well-commented, and handle edge cases.
temperature: 0.2 temperature: 0.2
}); });
const responseContent = solutionResponse.choices[0].message.content; responseContent = solutionResponse.choices[0].message.content;
} else {
// Gemini processing
if (!this.geminiApiKey) {
return {
success: false,
error: "Gemini API key not configured. Please check your settings."
};
}
try {
// Create Gemini message structure
const geminiMessages = [
{
role: "user",
parts: [
{
text: `You are an expert coding interview assistant. Provide a clear, optimal solution with detailed explanations for this problem:\n\n${promptText}`
}
]
}
];
// Make API request to Gemini
const response = await axios.default.post(
`https://generativelanguage.googleapis.com/v1beta/models/${config.solutionModel || "gemini-2.0-flash"}:generateContent?key=${this.geminiApiKey}`,
{
contents: geminiMessages,
generationConfig: {
temperature: 0.2,
maxOutputTokens: 4000
}
},
{ signal }
);
const responseData = response.data as GeminiResponse;
if (!responseData.candidates || responseData.candidates.length === 0) {
throw new Error("Empty response from Gemini API");
}
responseContent = responseData.candidates[0].content.parts[0].text;
} catch (error) {
console.error("Error using Gemini API for solution:", error);
return {
success: false,
error: "Failed to generate solution with Gemini API. Please check your API key or try again later."
};
}
}
// Extract parts from the response // 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,6 +881,17 @@ 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);
let debugContent;
if (config.apiProvider === "openai") {
if (!this.openaiClient) {
return {
success: false,
error: "OpenAI API key not configured. Please check your settings."
};
}
const messages = [ const messages = [
{ {
role: "system" as const, role: "system" as const,
@ -669,7 +934,6 @@ If you include code examples, use proper markdown code blocks with language spec
} }
]; ];
// Update progress
if (mainWindow) { if (mainWindow) {
mainWindow.webContents.send("processing-status", { mainWindow.webContents.send("processing-status", {
message: "Analyzing code and generating debug feedback...", message: "Analyzing code and generating debug feedback...",
@ -677,7 +941,6 @@ If you include code examples, use proper markdown code blocks with language spec
}); });
} }
// Send to OpenAI Vision API
const debugResponse = await this.openaiClient.chat.completions.create({ const debugResponse = await this.openaiClient.chat.completions.create({
model: config.debuggingModel || "gpt-4o", model: config.debuggingModel || "gpt-4o",
messages: messages, messages: messages,
@ -685,7 +948,90 @@ If you include code examples, use proper markdown code blocks with language spec
temperature: 0.2 temperature: 0.2
}); });
// Update final progress debugContent = debugResponse.choices[0].message.content;
} else {
if (!this.geminiApiKey) {
return {
success: false,
error: "Gemini API key not configured. Please check your settings."
};
}
try {
const debugPrompt = `
You are a coding interview assistant helping debug and improve solutions. Analyze these screenshots which include either error messages, incorrect outputs, or test cases, and provide detailed debugging help.
I'm solving this coding problem: "${problemInfo.problem_statement}" in ${language}. I need help with debugging or improving my solution.
YOUR RESPONSE MUST FOLLOW THIS EXACT STRUCTURE WITH THESE SECTION HEADERS:
### Issues Identified
- List each issue as a bullet point with clear explanation
### Specific Improvements and Corrections
- List specific code changes needed as bullet points
### Optimizations
- List any performance optimizations if applicable
### Explanation of Changes Needed
Here provide a clear explanation of why the changes are needed
### Key Points
- Summary bullet points of the most important takeaways
If you include code examples, use proper markdown code blocks with language specification (e.g. \`\`\`java).
`;
const geminiMessages = [
{
role: "user",
parts: [
{ text: debugPrompt },
...imageDataList.map(data => ({
inlineData: {
mimeType: "image/png",
data: data
}
}))
]
}
];
if (mainWindow) {
mainWindow.webContents.send("processing-status", {
message: "Analyzing code and generating debug feedback with Gemini...",
progress: 60
});
}
const response = await axios.default.post(
`https://generativelanguage.googleapis.com/v1beta/models/${config.debuggingModel || "gemini-2.0-flash"}:generateContent?key=${this.geminiApiKey}`,
{
contents: geminiMessages,
generationConfig: {
temperature: 0.2,
maxOutputTokens: 4000
}
},
{ signal }
);
const responseData = response.data as GeminiResponse;
if (!responseData.candidates || responseData.candidates.length === 0) {
throw new Error("Empty response from Gemini API");
}
debugContent = responseData.candidates[0].content.parts[0].text;
} catch (error) {
console.error("Error using Gemini API for debugging:", error);
return {
success: false,
error: "Failed to process debug request with Gemini API. Please check your API key or try again later."
};
}
}
if (mainWindow) { 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)
} }
} }

@ -7,6 +7,7 @@ import { v4 as uuidv4 } from "uuid"
import { execFile } from "child_process" import { execFile } from "child_process"
import { promisify } from "util" import { promisify } from "util"
import screenshot from "screenshot-desktop" import screenshot from "screenshot-desktop"
import os from "os"
const execFileAsync = promisify(execFile) const execFileAsync = promisify(execFile)
@ -17,6 +18,7 @@ export class ScreenshotHelper {
private readonly screenshotDir: string private readonly screenshotDir: string
private readonly extraScreenshotDir: string private readonly extraScreenshotDir: string
private readonly tempDir: string
private view: "queue" | "solutions" | "debug" = "queue" private view: "queue" | "solutions" | "debug" = "queue"
@ -29,19 +31,59 @@ export class ScreenshotHelper {
app.getPath("userData"), app.getPath("userData"),
"extra_screenshots" "extra_screenshots"
) )
this.tempDir = path.join(app.getPath("temp"), "interview-coder-screenshots")
// Create directories if they don't exist // Create directories if they don't exist
if (!fs.existsSync(this.screenshotDir)) { this.ensureDirectoriesExist();
fs.mkdirSync(this.screenshotDir, { recursive: true })
// Load any existing screenshots from disk
this.loadExistingScreenshots();
}
private ensureDirectoriesExist(): void {
const directories = [this.screenshotDir, this.extraScreenshotDir, this.tempDir];
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);
}
}
} }
if (!fs.existsSync(this.extraScreenshotDir)) {
fs.mkdirSync(this.extraScreenshotDir, { recursive: true })
} }
// Ensure temp directory exists private loadExistingScreenshots(): void {
const tempDir = app.getPath("temp") try {
if (!fs.existsSync(tempDir)) { // Load main queue screenshots
fs.mkdirSync(tempDir, { recursive: true }) 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<Buffer> { private async captureScreenshot(): Promise<Buffer> {
try { try {
// Ensure temp directory exists console.log("Starting screenshot capture...");
const tempDir = app.getPath("temp")
if (!fs.existsSync(tempDir)) { // For Windows, try multiple methods
fs.mkdirSync(tempDir, { recursive: true }) 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' }); const buffer = await screenshot({ format: 'png' });
console.log(`Screenshot captured successfully, size: ${buffer.length} bytes`);
return buffer; return buffer;
} catch (error) { } catch (error) {
console.error("Error capturing screenshot:", 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<Buffer> {
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,13 +253,20 @@ export class ScreenshotHelper {
): Promise<string> { ): Promise<string> {
console.log("Taking screenshot in view:", this.view) console.log("Taking screenshot in view:", this.view)
hideMainWindow() 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 = "" let screenshotPath = ""
try { try {
// Get screenshot buffer using cross-platform method // Get screenshot buffer using cross-platform method
const screenshotBuffer = await this.captureScreenshot(); 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 // Save and manage the screenshot based on current view
if (this.view === "queue") { if (this.view === "queue") {
screenshotPath = path.join(this.screenshotDir, `${uuidv4()}.png`) screenshotPath = path.join(this.screenshotDir, `${uuidv4()}.png`)
@ -167,7 +312,8 @@ export class ScreenshotHelper {
console.error("Screenshot error:", error) console.error("Screenshot error:", error)
throw error throw error
} finally { } finally {
await new Promise((resolve) => setTimeout(resolve, 50)) // Increased delay for showing window again
await new Promise((resolve) => setTimeout(resolve, 200))
showMainWindow() showMainWindow()
} }
@ -176,11 +322,16 @@ export class ScreenshotHelper {
public async getImagePreview(filepath: string): Promise<string> { public async getImagePreview(filepath: string): Promise<string> {
try { try {
if (!fs.existsSync(filepath)) {
console.error(`Image file not found: ${filepath}`);
return '';
}
const data = await fs.promises.readFile(filepath) const data = await fs.promises.readFile(filepath)
return `data:image/png;base64,${data.toString("base64")}` return `data:image/png;base64,${data.toString("base64")}`
} catch (error) { } catch (error) {
console.error("Error reading image:", error) console.error("Error reading image:", error)
throw error return ''
} }
} }
@ -188,7 +339,10 @@ export class ScreenshotHelper {
path: string path: string
): Promise<{ success: boolean; error?: string }> { ): Promise<{ success: boolean; error?: string }> {
try { try {
if (fs.existsSync(path)) {
await fs.promises.unlink(path) await fs.promises.unlink(path)
}
if (this.view === "queue") { if (this.view === "queue") {
this.screenshotQueue = this.screenshotQueue.filter( this.screenshotQueue = this.screenshotQueue.filter(
(filePath) => filePath !== path (filePath) => filePath !== path
@ -208,6 +362,7 @@ export class ScreenshotHelper {
public clearExtraScreenshotQueue(): void { public clearExtraScreenshotQueue(): void {
// Clear extraScreenshotQueue // Clear extraScreenshotQueue
this.extraScreenshotQueue.forEach((screenshotPath) => { this.extraScreenshotQueue.forEach((screenshotPath) => {
if (fs.existsSync(screenshotPath)) {
fs.unlink(screenshotPath, (err) => { fs.unlink(screenshotPath, (err) => {
if (err) if (err)
console.error( console.error(
@ -215,6 +370,7 @@ export class ScreenshotHelper {
err err
) )
}) })
}
}) })
this.extraScreenshotQueue = [] this.extraScreenshotQueue = []
} }

@ -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\"", "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", "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", "run-prod": "cross-env NODE_ENV=production electron ./dist-electron/main.js",
"package": "yarn build && electron-builder build", "package": "npm run build && electron-builder build",
"package-mac": "yarn build && electron-builder build --mac", "package-mac": "npm run build && electron-builder build --mac",
"package-win": "yarn build && electron-builder build --win" "package-win": "npm run build && electron-builder build --win"
}, },
"build": { "build": {
"appId": "com.chunginlee.interviewcoder", "appId": "com.chunginlee.interviewcoder",
@ -121,6 +121,7 @@
"@radix-ui/react-label": "^2.1.0", "@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-slot": "^1.1.0", "@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-toast": "^1.2.2", "@radix-ui/react-toast": "^1.2.2",
"@supabase/supabase-js": "^2.49.4",
"@tanstack/react-query": "^5.64.0", "@tanstack/react-query": "^5.64.0",
"axios": "^1.7.7", "axios": "^1.7.7",
"class-variance-authority": "^0.7.1", "class-variance-authority": "^0.7.1",

@ -37,7 +37,7 @@ function App() {
open: false, open: false,
title: "", title: "",
description: "", description: "",
variant: "neutral" as const variant: "neutral" as "neutral" | "success" | "error"
}) })
const [credits, setCredits] = useState<number>(999) // Unlimited credits const [credits, setCredits] = useState<number>(999) // Unlimited credits
const [currentLanguage, setCurrentLanguage] = useState<string>("python") const [currentLanguage, setCurrentLanguage] = useState<string>("python")

@ -13,7 +13,9 @@ import { Button } from "../ui/button";
import { Settings } from "lucide-react"; import { Settings } from "lucide-react";
import { useToast } from "../../contexts/toast"; import { useToast } from "../../contexts/toast";
type OpenAIModel = { type APIProvider = "openai" | "gemini";
type AIModel = {
id: string; id: string;
name: string; name: string;
description: string; description: string;
@ -23,7 +25,8 @@ type ModelCategory = {
key: 'extractionModel' | 'solutionModel' | 'debuggingModel'; key: 'extractionModel' | 'solutionModel' | 'debuggingModel';
title: string; title: string;
description: string; description: string;
models: OpenAIModel[]; openaiModels: AIModel[];
geminiModels: AIModel[];
}; };
// Define available models for each category // Define available models for each category
@ -32,7 +35,7 @@ const modelCategories: ModelCategory[] = [
key: 'extractionModel', key: 'extractionModel',
title: 'Problem Extraction', title: 'Problem Extraction',
description: 'Model used to analyze screenshots and extract problem details', description: 'Model used to analyze screenshots and extract problem details',
models: [ openaiModels: [
{ {
id: "gpt-4o", id: "gpt-4o",
name: "gpt-4o", name: "gpt-4o",
@ -43,13 +46,25 @@ const modelCategories: ModelCategory[] = [
name: "gpt-4o-mini", name: "gpt-4o-mini",
description: "Faster, more cost-effective option" 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', key: 'solutionModel',
title: 'Solution Generation', title: 'Solution Generation',
description: 'Model used to generate coding solutions', description: 'Model used to generate coding solutions',
models: [ openaiModels: [
{ {
id: "gpt-4o", id: "gpt-4o",
name: "gpt-4o", name: "gpt-4o",
@ -60,13 +75,25 @@ const modelCategories: ModelCategory[] = [
name: "gpt-4o-mini", name: "gpt-4o-mini",
description: "Faster, more cost-effective option" 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', key: 'debuggingModel',
title: 'Debugging', title: 'Debugging',
description: 'Model used to debug and improve solutions', description: 'Model used to debug and improve solutions',
models: [ openaiModels: [
{ {
id: "gpt-4o", id: "gpt-4o",
name: "gpt-4o", name: "gpt-4o",
@ -77,6 +104,18 @@ const modelCategories: ModelCategory[] = [
name: "gpt-4o-mini", name: "gpt-4o-mini",
description: "Faster, more cost-effective option" 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) { export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDialogProps) {
const [open, setOpen] = useState(externalOpen || false); const [open, setOpen] = useState(externalOpen || false);
const [apiKey, setApiKey] = useState(""); const [apiKey, setApiKey] = useState("");
const [apiProvider, setApiProvider] = useState<APIProvider>("openai");
const [extractionModel, setExtractionModel] = useState("gpt-4o"); const [extractionModel, setExtractionModel] = useState("gpt-4o");
const [solutionModel, setSolutionModel] = useState("gpt-4o"); const [solutionModel, setSolutionModel] = useState("gpt-4o");
const [debuggingModel, setDebuggingModel] = useState("gpt-4o"); const [debuggingModel, setDebuggingModel] = useState("gpt-4o");
@ -115,15 +155,24 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
useEffect(() => { useEffect(() => {
if (open) { if (open) {
setIsLoading(true); setIsLoading(true);
interface Config {
apiKey?: string;
apiProvider?: APIProvider;
extractionModel?: string;
solutionModel?: string;
debuggingModel?: string;
}
window.electronAPI window.electronAPI
.getConfig() .getConfig()
.then((config) => { .then((config: Config) => {
setApiKey(config.apiKey || ""); setApiKey(config.apiKey || "");
setApiProvider(config.apiProvider || "openai");
setExtractionModel(config.extractionModel || "gpt-4o"); setExtractionModel(config.extractionModel || "gpt-4o");
setSolutionModel(config.solutionModel || "gpt-4o"); setSolutionModel(config.solutionModel || "gpt-4o");
setDebuggingModel(config.debuggingModel || "gpt-4o"); setDebuggingModel(config.debuggingModel || "gpt-4o");
}) })
.catch((error) => { .catch((error: unknown) => {
console.error("Failed to load config:", error); console.error("Failed to load config:", error);
showToast("Error", "Failed to load settings", "error"); showToast("Error", "Failed to load settings", "error");
}) })
@ -133,11 +182,28 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
} }
}, [open, showToast]); }, [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 () => { const handleSave = async () => {
setIsLoading(true); setIsLoading(true);
try { try {
const result = await window.electronAPI.updateConfig({ const result = await window.electronAPI.updateConfig({
apiKey, apiKey,
apiProvider,
extractionModel, extractionModel,
solutionModel, solutionModel,
debuggingModel, debuggingModel,
@ -173,7 +239,6 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
return ( return (
<Dialog open={open} onOpenChange={handleOpenChange}> <Dialog open={open} onOpenChange={handleOpenChange}>
{/* Removed unnecessary dialog trigger - using the icon in QueueCommands instead */}
<DialogContent <DialogContent
className="sm:max-w-md bg-black border border-white/10 text-white settings-dialog" className="sm:max-w-md bg-black border border-white/10 text-white settings-dialog"
style={{ style={{
@ -193,23 +258,71 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
animation: 'fadeIn 0.25s ease forwards', animation: 'fadeIn 0.25s ease forwards',
opacity: 0.98 opacity: 0.98
}} }}
> <DialogHeader> >
<DialogTitle>OpenAI API Settings</DialogTitle> <DialogHeader>
<DialogTitle>API Settings</DialogTitle>
<DialogDescription className="text-white/70"> <DialogDescription className="text-white/70">
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.
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<div className="space-y-4 py-4"> <div className="space-y-4 py-4">
{/* API Provider Selection */}
<div className="space-y-2">
<label className="text-sm font-medium text-white">API Provider</label>
<div className="flex gap-2">
<div
className={`flex-1 p-2 rounded-lg cursor-pointer transition-colors ${
apiProvider === "openai"
? "bg-white/10 border border-white/20"
: "bg-black/30 border border-white/5 hover:bg-white/5"
}`}
onClick={() => handleProviderChange("openai")}
>
<div className="flex items-center gap-2">
<div
className={`w-3 h-3 rounded-full ${
apiProvider === "openai" ? "bg-white" : "bg-white/20"
}`}
/>
<div className="flex flex-col">
<p className="font-medium text-white text-sm">OpenAI</p>
<p className="text-xs text-white/60">GPT-4o models</p>
</div>
</div>
</div>
<div
className={`flex-1 p-2 rounded-lg cursor-pointer transition-colors ${
apiProvider === "gemini"
? "bg-white/10 border border-white/20"
: "bg-black/30 border border-white/5 hover:bg-white/5"
}`}
onClick={() => handleProviderChange("gemini")}
>
<div className="flex items-center gap-2">
<div
className={`w-3 h-3 rounded-full ${
apiProvider === "gemini" ? "bg-white" : "bg-white/20"
}`}
/>
<div className="flex flex-col">
<p className="font-medium text-white text-sm">Gemini</p>
<p className="text-xs text-white/60">Gemini 1.5 models</p>
</div>
</div>
</div>
</div>
</div>
<div className="space-y-2"> <div className="space-y-2">
<label className="text-sm font-medium text-white" htmlFor="apiKey"> <label className="text-sm font-medium text-white" htmlFor="apiKey">
OpenAI API Key {apiProvider === "openai" ? "OpenAI API Key" : "Gemini API Key"}
</label> </label>
<Input <Input
id="apiKey" id="apiKey"
type="password" type="password"
value={apiKey} value={apiKey}
onChange={(e) => setApiKey(e.target.value)} onChange={(e) => setApiKey(e.target.value)}
placeholder="sk-..." placeholder={apiProvider === "openai" ? "sk-..." : "Enter your Gemini API key"}
className="bg-black/50 border-white/10 text-white" className="bg-black/50 border-white/10 text-white"
/> />
{apiKey && ( {apiKey && (
@ -218,10 +331,12 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
</p> </p>
)} )}
<p className="text-xs text-white/50"> <p className="text-xs text-white/50">
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"}
</p> </p>
<div className="mt-2 p-2 rounded-md bg-white/5 border border-white/10"> <div className="mt-2 p-2 rounded-md bg-white/5 border border-white/10">
<p className="text-xs text-white/80 mb-1">Don't have an API key?</p> <p className="text-xs text-white/80 mb-1">Don't have an API key?</p>
{apiProvider === "openai" ? (
<>
<p className="text-xs text-white/60 mb-1">1. Create an account at <button <p className="text-xs text-white/60 mb-1">1. Create an account at <button
onClick={() => openExternalLink('https://platform.openai.com/signup')} onClick={() => openExternalLink('https://platform.openai.com/signup')}
className="text-blue-400 hover:underline cursor-pointer">OpenAI</button> className="text-blue-400 hover:underline cursor-pointer">OpenAI</button>
@ -231,6 +346,20 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
className="text-blue-400 hover:underline cursor-pointer">API Keys</button> section className="text-blue-400 hover:underline cursor-pointer">API Keys</button> section
</p> </p>
<p className="text-xs text-white/60">3. Create a new secret key and paste it here</p> <p className="text-xs text-white/60">3. Create a new secret key and paste it here</p>
</>
) : (
<>
<p className="text-xs text-white/60 mb-1">1. Create an account at <button
onClick={() => openExternalLink('https://aistudio.google.com/')}
className="text-blue-400 hover:underline cursor-pointer">Google AI Studio</button>
</p>
<p className="text-xs text-white/60 mb-1">2. Go to the <button
onClick={() => openExternalLink('https://aistudio.google.com/app/apikey')}
className="text-blue-400 hover:underline cursor-pointer">API Keys</button> section
</p>
<p className="text-xs text-white/60">3. Create a new API key and paste it here</p>
</>
)}
</div> </div>
</div> </div>
@ -283,7 +412,11 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
Select which models to use for each stage of the process Select which models to use for each stage of the process
</p> </p>
{modelCategories.map((category) => ( {modelCategories.map((category) => {
// Get the appropriate model list based on selected provider
const models = apiProvider === "openai" ? category.openaiModels : category.geminiModels;
return (
<div key={category.key} className="mb-4"> <div key={category.key} className="mb-4">
<label className="text-sm font-medium text-white mb-1 block"> <label className="text-sm font-medium text-white mb-1 block">
{category.title} {category.title}
@ -291,7 +424,7 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
<p className="text-xs text-white/60 mb-2">{category.description}</p> <p className="text-xs text-white/60 mb-2">{category.description}</p>
<div className="space-y-2"> <div className="space-y-2">
{category.models.map((m) => { {models.map((m) => {
// Determine which state to use based on category key // Determine which state to use based on category key
const currentValue = const currentValue =
category.key === 'extractionModel' ? extractionModel : category.key === 'extractionModel' ? extractionModel :
@ -330,7 +463,8 @@ export function SettingsDialog({ open: externalOpen, onOpenChange }: SettingsDia
})} })}
</div> </div>
</div> </div>
))} );
})}
</div> </div>
</div> </div>
<DialogFooter className="flex justify-between sm:justify-between"> <DialogFooter className="flex justify-between sm:justify-between">

Loading…
Cancel
Save