-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
109 lines (90 loc) · 4.16 KB
/
Copy pathbackground.js
File metadata and controls
109 lines (90 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Service worker (MV3). Owns the network call to Groq so the request
// runs with the extension's own permissions rather than the page's CSP.
const GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions";
const SYSTEM_PROMPT = `You are a rigorous, highly accurate coding-interview reviewer. Given a candidate's solution to a problem, respond with ONLY a single valid JSON object. Do not wrap the JSON in markdown code blocks (such as \`\`\`json) and do not output any explanation or conversational text before or after the JSON.
The JSON object must have EXACTLY this structure:
{
"time_complexity": "O(...) - explain briefly based on actual loops/recursions in the code",
"space_complexity": "O(...) - explain briefly specifying the exact data structures and stack frame overhead",
"optimal_time_complexity": "O(...) - best theoretical time complexity for this problem with a brief justification",
"optimal_space_complexity": "O(...) - best theoretical space complexity for this problem with a brief justification",
"correctness_notes": "Note on correctness/edge cases. If correct, state: 'The code is correct and handles standard edge cases.'",
"suggestions": ["short actionable improvement 1", "short actionable improvement 2"],
"cleaner_approach": "Description of the optimal algorithm or 'Current approach is already optimal'"
}
CRITICAL RULES FOR ACCURACY AND TRUTHFULNESS:
1. Base the time and space complexity strictly on the actual code provided, not the general algorithm for the problem.
2. Account for built-in language operations (e.g. slicing, array shifting, sorting, string operations) in complexity.
3. For space complexity, include auxiliary space and recursion stack space.
4. Perform a silent step-by-step verification of correctness. Do not hallucinate bugs. If the code is correct, confirm it truthfully.
5. If the code is incomplete or invalid, explain the error in correctness_notes and estimate complexity for what is there.`;
async function callGroq({ apiKey, model, code, language, problemTitle, problemDescription }) {
const userContent = `Problem: ${problemTitle || "Unknown"}
Problem description (may be truncated):
${(problemDescription || "Not available").slice(0, 3000)}
Language: ${language || "unknown"}
Candidate's code:
\`\`\`${language || ""}
${code}
\`\`\`
Analyze this exact code.`;
const response = await fetch(GROQ_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: model || "llama-3.3-70b-versatile",
temperature: 0.1,
max_tokens: 1500,
response_format: { type: "json_object" },
messages: [
{ role: "system", content: SYSTEM_PROMPT },
{ role: "user", content: userContent },
],
}),
});
if (!response.ok) {
const text = await response.text().catch(() => "");
throw new Error(`Groq API error ${response.status}: ${text.slice(0, 300)}`);
}
const data = await response.json();
const raw = data?.choices?.[0]?.message?.content;
if (!raw) throw new Error("Groq returned an empty response.");
try {
return JSON.parse(raw);
} catch (e) {
throw new Error("Could not parse the model's response as JSON. Try again.");
}
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type !== "ANALYZE_CODE") return false;
(async () => {
try {
const { groqApiKey, groqModel } = await chrome.storage.sync.get([
"groqApiKey",
"groqModel",
]);
if (!groqApiKey) {
sendResponse({
ok: false,
error: "No Groq API key set. Click the extension icon to add one.",
});
return;
}
const result = await callGroq({
apiKey: groqApiKey,
model: groqModel,
code: message.code,
language: message.language,
problemTitle: message.problemTitle,
problemDescription: message.problemDescription,
});
sendResponse({ ok: true, result });
} catch (err) {
sendResponse({ ok: false, error: err.message || String(err) });
}
})();
return true; // keep the message channel open for the async response
});