You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
interview-coder/electron/ProcessingHelper.ts

1606 lines
60 KiB

// ProcessingHelper.ts
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { spawn } from "node:child_process"
import { ScreenshotHelper } from "./ScreenshotHelper"
import { IProcessingHelperDeps } from "./main"
import * as axios from "axios"
import { app, BrowserWindow, dialog } from "electron"
import { OpenAI } from "openai"
import { configHelper } from "./ConfigHelper"
import Anthropic from '@anthropic-ai/sdk';
// 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;
}>;
}
interface AnthropicMessage {
role: 'user' | 'assistant';
content: Array<{
type: 'text' | 'image';
text?: string;
source?: {
type: 'base64';
media_type: string;
data: string;
};
}>;
}
export class ProcessingHelper {
private deps: IProcessingHelperDeps
private screenshotHelper: ScreenshotHelper
private openaiClient: OpenAI | null = null
private geminiApiKey: string | null = null
private anthropicClient: Anthropic | null = null
// AbortControllers for API requests
private currentProcessingAbortController: AbortController | null = null
private currentExtraProcessingAbortController: AbortController | null = null
private currentFollowUpAbortController: AbortController | null = null
constructor(deps: IProcessingHelperDeps) {
this.deps = deps
this.screenshotHelper = deps.getScreenshotHelper()
// Initialize AI client based on config
this.initializeAIClient();
// Listen for config changes to re-initialize the AI client
configHelper.on('config-updated', () => {
this.initializeAIClient();
});
}
/**
* Initialize or reinitialize the AI client with current config
*/
private initializeAIClient(): void {
try {
const config = configHelper.loadConfig();
if (config.apiProvider === "openai") {
if (config.apiKey) {
this.openaiClient = new OpenAI({
apiKey: config.apiKey,
timeout: 60000, // 60 second timeout
maxRetries: 2 // Retry up to 2 times
});
this.geminiApiKey = null;
this.anthropicClient = null;
console.log("OpenAI client initialized successfully");
} else {
this.openaiClient = null;
this.geminiApiKey = null;
this.anthropicClient = null;
console.warn("No API key available, OpenAI client not initialized");
}
} else if (config.apiProvider === "gemini"){
// Gemini client initialization
this.openaiClient = null;
this.anthropicClient = null;
if (config.apiKey) {
this.geminiApiKey = config.apiKey;
console.log("Gemini API key set successfully");
} else {
this.openaiClient = null;
this.geminiApiKey = null;
this.anthropicClient = null;
console.warn("No API key available, Gemini client not initialized");
}
} else if (config.apiProvider === "anthropic") {
// Reset other clients
this.openaiClient = null;
this.geminiApiKey = null;
if (config.apiKey) {
this.anthropicClient = new Anthropic({
apiKey: config.apiKey,
timeout: 60000,
maxRetries: 2
});
console.log("Anthropic client initialized successfully");
} else {
this.openaiClient = null;
this.geminiApiKey = null;
this.anthropicClient = null;
console.warn("No API key available, Anthropic client not initialized");
}
}
} catch (error) {
console.error("Failed to initialize AI client:", error);
this.openaiClient = null;
this.geminiApiKey = null;
this.anthropicClient = null;
}
}
private async waitForInitialization(
mainWindow: BrowserWindow
): Promise<void> {
let attempts = 0
const maxAttempts = 50 // 5 seconds total
while (attempts < maxAttempts) {
const isInitialized = await mainWindow.webContents.executeJavaScript(
"window.__IS_INITIALIZED__"
)
if (isInitialized) return
await new Promise((resolve) => setTimeout(resolve, 100))
attempts++
}
throw new Error("App failed to initialize after 5 seconds")
}
private async getCredits(): Promise<number> {
const mainWindow = this.deps.getMainWindow()
if (!mainWindow) return 999 // Unlimited credits in this version
try {
await this.waitForInitialization(mainWindow)
return 999 // Always return sufficient credits to work
} catch (error) {
console.error("Error getting credits:", error)
return 999 // Unlimited credits as fallback
}
}
private async getLanguage(): Promise<string> {
try {
// Get language from config
const config = configHelper.loadConfig();
if (config.language) {
return config.language;
}
// Fallback to window variable if config doesn't have language
const mainWindow = this.deps.getMainWindow()
if (mainWindow) {
try {
await this.waitForInitialization(mainWindow)
const language = await mainWindow.webContents.executeJavaScript(
"window.__LANGUAGE__"
)
if (
typeof language === "string" &&
language !== undefined &&
language !== null
) {
return language;
}
} catch (err) {
console.warn("Could not get language from window", err);
}
}
// Default fallback
return "python";
} catch (error) {
console.error("Error getting language:", error)
return "python"
}
}
public async processScreenshots(): Promise<void> {
const mainWindow = this.deps.getMainWindow()
if (!mainWindow) return
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");
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.API_KEY_INVALID
);
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;
}
} else if (config.apiProvider === "anthropic" && !this.anthropicClient) {
// Add check for Anthropic client
this.initializeAIClient();
if (!this.anthropicClient) {
console.error("Anthropic client not initialized");
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.API_KEY_INVALID
);
return;
}
}
const view = this.deps.getView()
console.log("Processing screenshots in view:", view)
if (view === "queue") {
mainWindow.webContents.send(this.deps.PROCESSING_EVENTS.INITIAL_START)
const screenshotQueue = this.screenshotHelper.getScreenshotQueue()
console.log("Processing main queue screenshots:", screenshotQueue)
// 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);
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);
return;
}
try {
// Initialize AbortController
this.currentProcessingAbortController = new AbortController()
const { signal } = this.currentProcessingAbortController
const screenshots = await Promise.all(
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;
}
})
)
// 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") || result.error?.includes("Gemini")) {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.API_KEY_INVALID
)
} else {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.INITIAL_SOLUTION_ERROR,
result.error
)
}
// Reset view back to queue on error
console.log("Resetting view to queue due to error")
this.deps.setView("queue")
return
}
// Only set view to solutions if processing succeeded
console.log("Setting view to solutions after successful processing")
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.SOLUTION_SUCCESS,
result.data
)
this.deps.setView("solutions")
} catch (error: any) {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.INITIAL_SOLUTION_ERROR,
error
)
console.error("Processing error:", error)
if (axios.isCancel(error)) {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.INITIAL_SOLUTION_ERROR,
"Processing was canceled by the user."
)
} else {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.INITIAL_SOLUTION_ERROR,
error.message || "Server error. Please try again."
)
}
// Reset view back to queue on error
console.log("Resetting view to queue due to error")
this.deps.setView("queue")
} finally {
this.currentProcessingAbortController = null
}
} else {
// view == 'solutions'
const extraScreenshotQueue =
this.screenshotHelper.getExtraScreenshotQueue()
console.log("Processing extra queue screenshots:", extraScreenshotQueue)
// 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);
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);
return;
}
mainWindow.webContents.send(this.deps.PROCESSING_EVENTS.DEBUG_START)
// Initialize AbortController
this.currentExtraProcessingAbortController = new AbortController()
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(
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:",
validScreenshots.map((s) => s.path)
)
const result = await this.processExtraScreenshotsHelper(
validScreenshots,
signal
)
if (result.success) {
this.deps.setHasDebugged(true)
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.DEBUG_SUCCESS,
result.data
)
} else {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.DEBUG_ERROR,
result.error
)
}
} catch (error: any) {
if (axios.isCancel(error)) {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.DEBUG_ERROR,
"Extra processing was canceled by the user."
)
} else {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.DEBUG_ERROR,
error.message
)
}
} finally {
this.currentExtraProcessingAbortController = null
}
}
}
private async processScreenshotsHelper(
screenshots: Array<{ path: string; data: string }>,
signal: AbortSignal
) {
try {
const config = configHelper.loadConfig();
const language = await this.getLanguage();
const mainWindow = this.deps.getMainWindow();
// Step 1: Extract problem info using AI Vision API (OpenAI or Gemini)
const imageDataList = screenshots.map(screenshot => screenshot.data);
// Update the user on progress
if (mainWindow) {
mainWindow.webContents.send("processing-status", {
message: "Analyzing problem from screenshots...",
progress: 20
});
}
let problemInfo;
if (config.apiProvider === "openai") {
// Verify OpenAI client
if (!this.openaiClient) {
this.initializeAIClient(); // Try to reinitialize
if (!this.openaiClient) {
return {
success: false,
error: "OpenAI API key not configured or invalid. Please check your settings."
};
}
}
// Use OpenAI for processing
const messages = [
{
role: "system" as const,
content: "You are a coding challenge interpreter. Analyze the screenshot of the coding problem and extract all relevant information. Return the information in JSON format with these fields: problem_statement, constraints, example_input, example_output, problem_type. Set problem_type to 'system_design' if the screenshots ask you to design a system, product or architecture rather than write code, otherwise 'coding'. Use exactly these keys and no others. For a system design question, put the design question itself in problem_statement, word for word. Example or possible questions that a page says might be asked count as design questions; if several are listed, include all of them, numbered. Put surrounding guidance such as tips or evaluation criteria in constraints. 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-6.1-sol",
messages: messages,
max_completion_tokens: 16000
});
// Parse the response
try {
const responseText = extractionResponse.choices[0].message.content;
// Handle when OpenAI might wrap the JSON in markdown code blocks
const jsonText = responseText.replace(/```json|```/g, '').trim();
problemInfo = JSON.parse(jsonText);
} catch (error) {
console.error("Error parsing OpenAI response:", error);
return {
success: false,
error: "Failed to parse problem information. Please try again or use clearer screenshots."
};
}
} else if (config.apiProvider === "gemini") {
// 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, problem_type. Set problem_type to 'system_design' if the screenshots ask you to design a system, product or architecture rather than write code, otherwise 'coding'. Use exactly these keys and no others. For a system design question, put the design question itself in problem_statement, word for word. Example or possible questions that a page says might be asked count as design questions; if several are listed, include all of them, numbered. Put surrounding guidance such as tips or evaluation criteria in constraints. 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-3.8-flash"}:generateContent?key=${this.geminiApiKey}`,
{
contents: geminiMessages,
generationConfig: {
maxOutputTokens: 16000
}
},
{ 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.map(part => part.text ?? "").join("");
// 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."
};
}
} else if (config.apiProvider === "anthropic" || config.apiProvider === "claude-code") {
if (config.apiProvider === "anthropic" && !this.anthropicClient) {
return {
success: false,
error: "Anthropic API key not configured. Please check your settings."
};
}
try {
const messages = [
{
role: "user" as const,
content: [
{
type: "text" as const,
text: `Extract the coding problem details from these screenshots. Return in JSON format with these fields: problem_statement, constraints, example_input, example_output, problem_type. Set problem_type to 'system_design' if the screenshots ask you to design a system, product or architecture rather than write code, otherwise 'coding'. Use exactly these keys and no others. For a system design question, put the design question itself in problem_statement, word for word. Example or possible questions that a page says might be asked count as design questions; if several are listed, include all of them, numbered. Put surrounding guidance such as tips or evaluation criteria in constraints. Preferred coding language is ${language}. Return only the JSON object, with no other text.`
},
...imageDataList.map(data => ({
type: "image" as const,
source: {
type: "base64" as const,
media_type: "image/png" as const,
data: data
}
}))
]
}
];
const responseText = config.apiProvider === "claude-code"
? await this.runClaudeCode(config.extractionModel || "opus", messages, signal)
: await this.createAnthropicText(config.extractionModel || "claude-opus-5-5", messages);
// Claude sometimes wraps the JSON in a sentence or a code fence; keep only the object
const jsonText = responseText.slice(responseText.indexOf("{"), responseText.lastIndexOf("}") + 1);
problemInfo = JSON.parse(jsonText);
} catch (error: any) {
console.error("Error using Anthropic API:", error);
if (config.apiProvider === "claude-code") {
return { success: false, error: `Claude Code: ${error.message}` };
}
// Add specific handling for Claude's limitations
if (error.status === 429) {
return {
success: false,
error: "Claude API rate limit exceeded. Please wait a few minutes before trying again."
};
} else if (error.status === 413 || (error.message && error.message.includes("token"))) {
return {
success: false,
error: "Your screenshots contain too much information for Claude to process. Switch to OpenAI or Gemini in settings which can handle larger inputs."
};
}
return {
success: false,
error: "Failed to process with Anthropic 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
this.deps.setProblemInfo(problemInfo);
// Send first success event
if (mainWindow) {
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.PROBLEM_EXTRACTED,
problemInfo
);
// Generate solutions after successful extraction
const solutionsResult = await this.generateSolutionsHelper(signal);
if (solutionsResult.success) {
// Clear any existing extra screenshots before transitioning to solutions view
this.screenshotHelper.clearExtraScreenshotQueue();
// Final progress update
mainWindow.webContents.send("processing-status", {
message: "Solution generated successfully",
progress: 100
});
mainWindow.webContents.send(
this.deps.PROCESSING_EVENTS.SOLUTION_SUCCESS,
solutionsResult.data
);
return { success: true, data: solutionsResult.data };
} else {
throw new Error(
solutionsResult.error || "Failed to generate solutions"
);
}
}
return { success: false, error: "Failed to process screenshots" };
} catch (error: any) {
// If the request was cancelled, don't retry
if (axios.isCancel(error)) {
return {
success: false,
error: "Processing was canceled by the user."
};
}
// 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."
};
} else if (error?.response?.status === 500) {
return {
success: false,
error: "OpenAI server error. Please try again later."
};
}
console.error("API Error Details:", error);
return {
success: false,
error: error.message || "Failed to process screenshots. Please try again."
};
}
}
private async generateSolutionsHelper(signal: AbortSignal) {
try {
const problemInfo = this.deps.getProblemInfo();
const language = await this.getLanguage();
const config = configHelper.loadConfig();
const mainWindow = this.deps.getMainWindow();
if (!problemInfo) {
throw new Error("No problem info available");
}
// Update progress status
if (mainWindow) {
mainWindow.webContents.send("processing-status", {
message: "Creating optimal solution with detailed explanations...",
progress: 60
});
}
// System design questions get a written design instead of code and complexity
const isSystemDesign = problemInfo.problem_type === "system_design";
// Create prompt for solution generation
const promptText = isSystemDesign ? `
Answer the following system design interview question the way a strong candidate would, following the Hello Interview delivery framework.
QUESTION:
${problemInfo.problem_statement}
ADDITIONAL CONTEXT:
${problemInfo.constraints || "None provided."}
If several design questions are listed, answer the first one and name the others in one line at the very end.
Use exactly these markdown sections, in this order:
## Functional Requirements
(the top 3 core requirements as "Users should be able to..." statements, then a short "Below the line (out of scope)" list)
## Non-Functional Requirements
(the 3 to 5 that matter most as "The system should..." statements, quantified where possible, such as "search latency under 500ms" or "availability over consistency", then a short "Below the line" list; include a scale estimate only where it changes the design)
## Core Entities
(a bulleted list of entity names with a few words each, not a full schema)
## API
(REST endpoints covering each functional requirement, such as "GET /businesses?query&lat&long&category -> Business[]"; take the user from the auth token, never from the request body)
## High-Level Design
(build it up one functional requirement at a time, with a ### subsection per requirement saying which components serve it and how a request flows through them; finish with one small ASCII diagram of the complete design, listing next to each database only the fields that matter)
## Deep Dives
(the 3 or 4 problems an interviewer would probe to meet the non-functional requirements, each as a ### heading phrased as a question; under each, give a Bad, Good and Great solution in a line or two apiece saying what each gets wrong, then explain the Great one in more depth)
## Key Decisions
(one bullet per decision, in the form "Decision: chosen option over the alternative, because of the trade-off")
Only for a data-processing system such as a web crawler, add a ## Data Flow section after API listing the steps from input to output.
Keep the high-level design simple and save the complexity for the deep dives. Always write the full answer: cover every section above completely, with no length limit, and never shorten a section, skip one or replace it with a summary. Use short bullets wherever prose isn't needed.
` : `
Generate a detailed solution for the following coding problem:
PROBLEM STATEMENT:
${problemInfo.problem_statement}
CONSTRAINTS:
${problemInfo.constraints || "No specific constraints provided."}
EXAMPLE INPUT:
${problemInfo.example_input || "No example input provided."}
EXAMPLE OUTPUT:
${problemInfo.example_output || "No example output provided."}
LANGUAGE: ${language}
I need the response in the following format:
1. Code: A clean, optimized implementation in ${language}
2. Your Thoughts: A list of key insights and reasoning behind your approach
3. Time complexity: O(X) with a detailed explanation (at least 2 sentences)
4. Space complexity: O(X) with a detailed explanation (at least 2 sentences)
For complexity explanations, please be thorough. For example: "Time complexity: O(n) because we iterate through the array only once. This is optimal as we need to examine each element at least once to find the solution." or "Space complexity: O(n) because in the worst case, we store all elements in the hashmap. The additional space scales linearly with the input size."
Your solution should be efficient, well-commented, and handle edge cases.
`;
let responseContent;
if (config.apiProvider === "openai") {
// OpenAI processing
if (!this.openaiClient) {
return {
success: false,
error: "OpenAI API key not configured. Please check your settings."
};
}
// Send to OpenAI API
const solutionResponse = await this.openaiClient.chat.completions.create({
model: config.solutionModel || "gpt-6.1-sol",
messages: [
{ role: "system", content: "You are an expert coding interview assistant. Provide clear, optimal solutions with detailed explanations." },
{ role: "user", content: promptText }
],
max_completion_tokens: 16000
});
responseContent = solutionResponse.choices[0].message.content;
} else if (config.apiProvider === "gemini") {
// 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-3.8-flash"}:generateContent?key=${this.geminiApiKey}`,
{
contents: geminiMessages,
generationConfig: {
maxOutputTokens: 16000
}
},
{ 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.map(part => part.text ?? "").join("");
} 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."
};
}
} else if (config.apiProvider === "anthropic" || config.apiProvider === "claude-code") {
// Anthropic processing
if (config.apiProvider === "anthropic" && !this.anthropicClient) {
return {
success: false,
error: "Anthropic API key not configured. Please check your settings."
};
}
try {
const messages = [
{
role: "user" as const,
content: [
{
type: "text" as const,
text: `You are an expert coding interview assistant. Provide a clear, optimal solution with detailed explanations for this problem:\n\n${promptText}`
}
]
}
];
// Send to Anthropic API
responseContent = config.apiProvider === "claude-code"
? await this.runClaudeCode(config.solutionModel || "opus", messages, signal)
: await this.createAnthropicText(config.solutionModel || "claude-opus-5-5", messages);
} catch (error: any) {
console.error("Error using Anthropic API for solution:", error);
if (config.apiProvider === "claude-code") {
return { success: false, error: `Claude Code: ${error.message}` };
}
// Add specific handling for Claude's limitations
if (error.status === 429) {
return {
success: false,
error: "Claude API rate limit exceeded. Please wait a few minutes before trying again."
};
} else if (error.status === 413 || (error.message && error.message.includes("token"))) {
return {
success: false,
error: "Your screenshots contain too much information for Claude to process. Switch to OpenAI or Gemini in settings which can handle larger inputs."
};
}
return {
success: false,
error: "Failed to generate solution with Anthropic API. Please check your API key or try again later."
};
}
}
if (isSystemDesign) {
// The whole write-up is the design; its Key Decisions bullets fill the thoughts list
const decisionsMatch = responseContent?.match(/##\s*Key Decisions\s*\n([\s\S]*?)(?=\n##\s|$)/i);
const decisions = decisionsMatch
? decisionsMatch[1].split("\n")
.filter((line) => /^\s*(?:[-*•]|\d+\.)\s+/.test(line))
.map((line) => line.replace(/^\s*(?:[-*•]|\d+\.)\s+/, "").trim())
: [];
return {
success: true,
data: {
code: responseContent,
thoughts: decisions.length > 0 ? decisions : ["See the design below"],
time_complexity: "",
space_complexity: ""
}
};
}
// Extract parts from the response
const codeMatch = responseContent.match(/```(?:\w+)?\s*([\s\S]*?)```/);
const code = codeMatch ? codeMatch[1].trim() : responseContent;
// Extract thoughts, looking for bullet points or numbered lists
const thoughtsRegex = /(?:Thoughts:|Key Insights:|Reasoning:|Approach:)([\s\S]*?)(?:Time complexity:|$)/i;
const thoughtsMatch = responseContent.match(thoughtsRegex);
let thoughts: string[] = [];
if (thoughtsMatch && thoughtsMatch[1]) {
// Extract bullet points or numbered items
const bulletPoints = thoughtsMatch[1].match(/(?:^|\n)\s*(?:[-*•]|\d+\.)\s*(.*)/g);
if (bulletPoints) {
thoughts = bulletPoints.map(point =>
point.replace(/^\s*(?:[-*•]|\d+\.)\s*/, '').trim()
).filter(Boolean);
} else {
// If no bullet points found, split by newlines and filter empty lines
thoughts = thoughtsMatch[1].split('\n')
.map((line) => line.trim())
.filter(Boolean);
}
}
// Extract complexity information
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.";
const timeMatch = responseContent.match(timeComplexityPattern);
if (timeMatch && timeMatch[1]) {
timeComplexity = timeMatch[1].trim();
if (!timeComplexity.match(/O\([^)]+\)/i)) {
timeComplexity = `O(n) - ${timeComplexity}`;
} else if (!timeComplexity.includes('-') && !timeComplexity.includes('because')) {
const notationMatch = timeComplexity.match(/O\([^)]+\)/i);
if (notationMatch) {
const notation = notationMatch[0];
const rest = timeComplexity.replace(notation, '').trim();
timeComplexity = `${notation} - ${rest}`;
}
}
}
const spaceMatch = responseContent.match(spaceComplexityPattern);
if (spaceMatch && spaceMatch[1]) {
spaceComplexity = spaceMatch[1].trim();
if (!spaceComplexity.match(/O\([^)]+\)/i)) {
spaceComplexity = `O(n) - ${spaceComplexity}`;
} else if (!spaceComplexity.includes('-') && !spaceComplexity.includes('because')) {
const notationMatch = spaceComplexity.match(/O\([^)]+\)/i);
if (notationMatch) {
const notation = notationMatch[0];
const rest = spaceComplexity.replace(notation, '').trim();
spaceComplexity = `${notation} - ${rest}`;
}
}
}
const formattedResponse = {
code: code,
thoughts: thoughts.length > 0 ? thoughts : ["Solution approach based on efficiency and readability"],
time_complexity: timeComplexity,
space_complexity: spaceComplexity
};
return { success: true, data: formattedResponse };
} catch (error: any) {
if (axios.isCancel(error)) {
return {
success: false,
error: "Processing was canceled by the user."
};
}
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("Solution generation error:", error);
return { success: false, error: error.message || "Failed to generate solution" };
}
}
private async processExtraScreenshotsHelper(
screenshots: Array<{ path: string; data: string }>,
signal: AbortSignal
) {
try {
const problemInfo = this.deps.getProblemInfo();
const language = await this.getLanguage();
const config = configHelper.loadConfig();
const mainWindow = this.deps.getMainWindow();
if (!problemInfo) {
throw new Error("No problem info available");
}
// Update progress status
if (mainWindow) {
mainWindow.webContents.send("processing-status", {
message: "Processing debug screenshots...",
progress: 30
});
}
// Prepare the images for the API call
const imageDataList = screenshots.map(screenshot => screenshot.data);
let debugContent;
if (config.apiProvider === "openai") {
if (!this.openaiClient) {
return {
success: false,
error: "OpenAI API key not configured. Please check your settings."
};
}
const messages = [
{
role: "system" as const,
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
- 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).`
},
{
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}` }
}))
]
}
];
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-6.1-sol",
messages: messages,
max_completion_tokens: 16000
});
debugContent = debugResponse.choices[0].message.content;
} else if (config.apiProvider === "gemini") {
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-3.8-flash"}:generateContent?key=${this.geminiApiKey}`,
{
contents: geminiMessages,
generationConfig: {
maxOutputTokens: 16000
}
},
{ 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.map(part => part.text ?? "").join("");
} 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."
};
}
} else if (config.apiProvider === "anthropic" || config.apiProvider === "claude-code") {
if (config.apiProvider === "anthropic" && !this.anthropicClient) {
return {
success: false,
error: "Anthropic 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.
`;
const messages = [
{
role: "user" as const,
content: [
{
type: "text" as const,
text: debugPrompt
},
...imageDataList.map(data => ({
type: "image" as const,
source: {
type: "base64" as const,
media_type: "image/png" as const,
data: data
}
}))
]
}
];
if (mainWindow) {
mainWindow.webContents.send("processing-status", {
message: "Analyzing code and generating debug feedback with Claude...",
progress: 60
});
}
debugContent = config.apiProvider === "claude-code"
? await this.runClaudeCode(config.debuggingModel || "opus", messages, signal)
: await this.createAnthropicText(config.debuggingModel || "claude-opus-5-5", messages);
} catch (error: any) {
console.error("Error using Anthropic API for debugging:", error);
if (config.apiProvider === "claude-code") {
return { success: false, error: `Claude Code: ${error.message}` };
}
// Add specific handling for Claude's limitations
if (error.status === 429) {
return {
success: false,
error: "Claude API rate limit exceeded. Please wait a few minutes before trying again."
};
} else if (error.status === 413 || (error.message && error.message.includes("token"))) {
return {
success: false,
error: "Your screenshots contain too much information for Claude to process. Switch to OpenAI or Gemini in settings which can handle larger inputs."
};
}
return {
success: false,
error: "Failed to process debug request with Anthropic API. Please check your API key or try again later."
};
}
}
if (mainWindow) {
mainWindow.webContents.send("processing-status", {
message: "Debug analysis complete",
progress: 100
});
}
let extractedCode = "// Debug mode - see analysis below";
const codeMatch = debugContent.match(/```(?:[a-zA-Z]+)?([\s\S]*?)```/);
if (codeMatch && codeMatch[1]) {
extractedCode = codeMatch[1].trim();
}
let formattedDebugContent = debugContent;
if (!debugContent.includes('# ') && !debugContent.includes('## ')) {
formattedDebugContent = debugContent
.replace(/issues identified|problems found|bugs found/i, '## Issues Identified')
.replace(/code improvements|improvements|suggested changes/i, '## Code Improvements')
.replace(/optimizations|performance improvements/i, '## Optimizations')
.replace(/explanation|detailed analysis/i, '## Explanation');
}
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"];
const response = {
code: extractedCode,
debug_analysis: formattedDebugContent,
thoughts: thoughts,
time_complexity: "N/A - Debug mode",
space_complexity: "N/A - Debug mode"
};
return { success: true, data: response };
} catch (error: any) {
console.error("Debug processing error:", error);
return { success: false, error: error.message || "Failed to process debug request" };
}
}
/**
* Answer a follow-up question about a system design answer. The question, the design
* and the earlier follow-ups go in as a single prompt, so every provider (including the
* Claude Code CLI, which takes one turn per call) sees the whole conversation.
*/
public async answerFollowUp(request: {
design: string
history: Array<{ question: string; answer: string }>
question: string
}): Promise<{ success: boolean; data?: string; error?: string }> {
const problemInfo = this.deps.getProblemInfo();
const config = configHelper.loadConfig();
const earlierFollowUps = request.history
.map((turn) => `Q: ${turn.question}\nA: ${turn.answer}`)
.join("\n\n");
const promptText = `
You are an expert system design interview assistant. You answered the system design question below; now answer a follow-up question about your design.
QUESTION:
${problemInfo?.problem_statement || "Not available."}
ADDITIONAL CONTEXT:
${problemInfo?.constraints || "None provided."}
YOUR DESIGN:
${request.design}
${earlierFollowUps ? `\nEARLIER FOLLOW-UPS:\n${earlierFollowUps}\n` : ""}
FOLLOW-UP QUESTION:
${request.question}
Always give the full write-up: answer the follow-up completely and in depth, with no length limit, building on the design above rather than repeating it. If it changes a requirement or adds a constraint, walk through everything in the design that changes and why. Use markdown with short bullets wherever prose isn't needed, and add an ASCII diagram if it helps.
`;
this.currentFollowUpAbortController = new AbortController();
const { signal } = this.currentFollowUpAbortController;
try {
let answer: string | null | undefined;
if (config.apiProvider === "openai") {
if (!this.openaiClient) {
return { success: false, error: "OpenAI API key not configured. Please check your settings." };
}
const response = await this.openaiClient.chat.completions.create({
model: config.solutionModel || "gpt-6.1-sol",
messages: [{ role: "user", content: promptText }],
max_completion_tokens: 16000
}, { signal });
answer = response.choices[0].message.content;
} else if (config.apiProvider === "gemini") {
if (!this.geminiApiKey) {
return { success: false, error: "Gemini API key not configured. Please check your settings." };
}
const response = await axios.default.post(
`https://generativelanguage.googleapis.com/v1beta/models/${config.solutionModel || "gemini-3.8-flash"}:generateContent?key=${this.geminiApiKey}`,
{
contents: [{ role: "user", parts: [{ text: promptText }] }],
generationConfig: {
maxOutputTokens: 16000
}
},
{ signal }
);
const responseData = response.data as GeminiResponse;
answer = responseData.candidates?.[0]?.content?.parts?.map(part => part.text ?? "").join("");
} else if (config.apiProvider === "anthropic" || config.apiProvider === "claude-code") {
if (config.apiProvider === "anthropic" && !this.anthropicClient) {
return { success: false, error: "Anthropic API key not configured. Please check your settings." };
}
const messages = [
{
role: "user" as const,
content: [{ type: "text" as const, text: promptText }]
}
];
answer = config.apiProvider === "claude-code"
? await this.runClaudeCode(config.solutionModel || "opus", messages, signal)
: await this.createAnthropicText(config.solutionModel || "claude-opus-5-5", messages, signal);
}
if (!answer?.trim()) {
return { success: false, error: "The model returned an empty answer. Please try again." };
}
return { success: true, data: answer.trim() };
} catch (error: any) {
console.error("Error answering follow-up:", error);
if (signal.aborted) {
return { success: false, error: "The follow-up was canceled." };
}
return {
success: false,
error: config.apiProvider === "claude-code"
? `Claude Code: ${error.message}`
: error.message || "Failed to answer the follow-up. Please try again."
};
} finally {
// A reset may already have replaced this controller with a newer request's
if (this.currentFollowUpAbortController?.signal === signal) {
this.currentFollowUpAbortController = null;
}
}
}
public cancelOngoingRequests(): void {
let wasCancelled = false
if (this.currentFollowUpAbortController) {
this.currentFollowUpAbortController.abort()
this.currentFollowUpAbortController = null
}
if (this.currentProcessingAbortController) {
this.currentProcessingAbortController.abort()
this.currentProcessingAbortController = null
wasCancelled = true
}
if (this.currentExtraProcessingAbortController) {
this.currentExtraProcessingAbortController.abort()
this.currentExtraProcessingAbortController = null
wasCancelled = true
}
this.deps.setHasDebugged(false)
this.deps.setProblemInfo(null)
const mainWindow = this.deps.getMainWindow()
if (wasCancelled && mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send(this.deps.PROCESSING_EVENTS.NO_SCREENSHOTS)
}
}
/**
* Send a request to Claude and return the text of its reply.
* Current Claude models think before answering (thinking tokens count toward
* max_tokens) and reject a custom temperature. Opus and Sonnet requests opt into
* server-side refusal fallbacks; Haiku has none.
*/
private async createAnthropicText(
model: string,
messages: Anthropic.Beta.Messages.BetaMessageParam[],
signal?: AbortSignal
): Promise<string> {
if (!this.anthropicClient) {
throw new Error("Anthropic client not initialized");
}
// SDK 0.39 predates the fallbacks parameter, hence the cast below
const fallbackParams = model.startsWith("claude-haiku")
? {}
: { betas: ["server-side-fallback-2026-07-01"], fallbacks: "default" };
const response = await this.anthropicClient.beta.messages.create({
model,
max_tokens: 16000,
messages,
...fallbackParams
} as Anthropic.Beta.Messages.MessageCreateParamsNonStreaming, { signal });
if ((response.stop_reason as string) === "refusal") {
throw new Error("Claude declined to answer this request.");
}
return response.content
.filter((block): block is Anthropic.Beta.Messages.BetaTextBlock => block.type === "text")
.map((block) => block.text)
.join("");
}
/**
* Send a request through the local Claude Code CLI, so it counts against the user's
* Claude subscription instead of API billing. Messages go in as stream-json, which lets
* screenshots travel inline, with every tool disabled and the user's own Claude Code
* customizations (CLAUDE.md, hooks, MCP servers) switched off by --safe-mode.
*/
private runClaudeCode(
model: string,
messages: Anthropic.Beta.Messages.BetaMessageParam[],
signal?: AbortSignal
): Promise<string> {
// Drop the variables a parent Claude Code session sets (a nested session refuses to
// start) and any API key, so the CLI signs in with the subscription
const env: NodeJS.ProcessEnv = {};
for (const [name, value] of Object.entries(process.env)) {
if (!/^(CLAUDECODE$|CLAUDE_CODE_|CLAUDE_PID$|CLAUDE_EFFORT$|ANTHROPIC_API_KEY$)/.test(name)) {
env[name] = value;
}
}
// Apps opened from Finder get a minimal PATH, so add the usual install locations
env.PATH = [env.PATH, path.join(os.homedir(), ".local", "bin"), "/opt/homebrew/bin", "/usr/local/bin"]
.filter(Boolean)
.join(path.delimiter);
const args = [
"-p",
"--input-format", "stream-json",
"--output-format", "stream-json",
"--verbose",
"--model", model,
// Matches the API's default for Opus; Claude Code's own default runs far longer per answer
"--effort", "medium",
"--tools", "",
"--safe-mode",
"--no-session-persistence",
"--system-prompt", "You are an expert coding interview assistant. Follow the user's instructions exactly and answer directly."
];
return new Promise((resolve, reject) => {
const child = spawn("claude", args, {
cwd: app.getPath("temp"),
env,
signal,
timeout: 5 * 60 * 1000
});
let stdout = "";
let stderr = "";
child.stdout.on("data", (chunk) => { stdout += chunk; });
child.stderr.on("data", (chunk) => { stderr += chunk; });
// A failed spawn also fails the stdin write; the process "error" event reports it
child.stdin.on("error", () => {});
child.on("error", (error: NodeJS.ErrnoException) => {
reject(error.code === "ENOENT"
? new Error("Claude Code isn't installed or isn't on your PATH. Install it from claude.com/claude-code and sign in with your Claude account.")
: error);
});
child.on("close", (code) => {
// The final stream-json event is the result: { type: "result", is_error, result }
let result: { is_error?: boolean; result?: string } | undefined;
for (const line of stdout.split("\n")) {
try {
const event = JSON.parse(line);
if (event.type === "result") result = event;
} catch {
// Not a JSON event line
}
}
if (result && !result.is_error && typeof result.result === "string") {
resolve(result.result);
} else {
reject(new Error(result?.result || stderr.trim() || `exited with code ${code} without a result`));
}
});
child.stdin.end(
messages.map((message) => JSON.stringify({ type: "user", message })).join("\n") + "\n"
);
});
}
}