-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
265 lines (231 loc) 路 8.79 KB
/
Copy pathserver.ts
File metadata and controls
265 lines (231 loc) 路 8.79 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import express from "express";
import path from "path";
import { createServer as createViteServer } from "vite";
import { GoogleGenAI, Type } from "@google/genai";
import dotenv from "dotenv";
dotenv.config();
const app = express();
const PORT = 3000;
app.use(express.json({ limit: "10mb" }));
// Initialize Gemini Client
const getGeminiClient = () => {
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
console.warn("Warning: GEMINI_API_KEY environment variable is missing.");
}
return new GoogleGenAI({
apiKey: apiKey || "",
httpOptions: {
headers: {
"User-Agent": "aistudio-build",
},
},
});
};
// Clean helper for parsing AI JSON response
function cleanAndParseJSON(text: string) {
try {
let cleaned = text.trim();
if (cleaned.startsWith("```json")) {
cleaned = cleaned.replace(/^```json\s*/i, "").replace(/\s*```$/, "");
} else if (cleaned.startsWith("```")) {
cleaned = cleaned.replace(/^```\s*/, "").replace(/\s*```$/, "");
}
return JSON.parse(cleaned);
} catch (err) {
console.error("Failed to parse JSON response:", text);
throw new Error("Invalid JSON structure returned by AI model.");
}
}
// 1. Generate Interview Questions API
app.post("/api/generate-questions", async (req, res) => {
try {
const { role, experience = "Fresher", question_count = 5 } = req.body;
if (!role) {
return res.status(400).json({ error: "Job role is required." });
}
const ai = getGeminiClient();
const prompt = `You are an expert technical and HR interviewer conducting a realistic interview.
Target Role: "${role}"
Target Experience Level: "${experience}"
Number of questions required: ${question_count}
Generate exactly ${question_count} distinct, high-quality, relevant interview questions ranging from technical fundamentals, practical real-world scenarios, problem-solving, to behavioral questions appropriate for a ${experience} in ${role}.
Return JSON ONLY formatted according to the schema.`;
const response = await ai.models.generateContent({
model: "gemini-3.6-flash",
contents: prompt,
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.OBJECT,
properties: {
questions: {
type: Type.ARRAY,
items: {
type: Type.OBJECT,
properties: {
id: { type: Type.INTEGER },
question: { type: Type.STRING },
category: { type: Type.STRING, description: "e.g., Technical, Scenario, Behavioral, Architecture" },
difficulty: { type: Type.STRING, description: "Easy, Medium, or Hard" },
expectedKeyConcepts: {
type: Type.ARRAY,
items: { type: Type.STRING },
description: "3-4 key concepts expected in a strong answer"
}
},
required: ["id", "question", "category", "difficulty"]
}
}
},
required: ["questions"]
}
}
});
const parsed = cleanAndParseJSON(response.text || "{}");
res.json(parsed);
} catch (error: any) {
console.error("Error generating questions:", error);
res.status(500).json({
error: "Failed to generate questions using AI.",
details: error.message || String(error)
});
}
});
// 2. Evaluate Candidate Answer API
app.post("/api/evaluate-answer", async (req, res) => {
try {
const { role, question, answer, experience = "Fresher" } = req.body;
if (!role || !question) {
return res.status(400).json({ error: "Role and question are required." });
}
const userAns = answer && answer.trim() ? answer.trim() : "(No answer submitted / skipped)";
const ai = getGeminiClient();
const prompt = `You are a professional technical interviewer evaluating a candidate's answer.
Role: ${role}
Candidate Experience Level: ${experience}
Question: "${question}"
Candidate's Answer: "${userAns}"
Evaluate the candidate's answer carefully for:
1. Technical Correctness & Accuracy
2. Depth of Understanding & Completeness
3. Communication Clarity & Structure (e.g., STAR framework alignment if applicable)
4. Confidence & Key Industry Terminology
Assign a score from 0 to 10 (integer or 1 decimal place like 8.5).
Provide detailed, supportive, and actionable feedback.
Return JSON ONLY according to the specified schema.`;
const response = await ai.models.generateContent({
model: "gemini-3.6-flash",
contents: prompt,
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.OBJECT,
properties: {
score: { type: Type.NUMBER, description: "Score out of 10" },
feedback: { type: Type.STRING, description: "Detailed constructive analysis" },
strength: { type: Type.STRING, description: "What the candidate did well" },
weakness: { type: Type.STRING, description: "Missing elements or areas needing clarity" },
improvement: { type: Type.STRING, description: "Actionable tips to improve this answer" },
idealAnswerKeyPoints: {
type: Type.ARRAY,
items: { type: Type.STRING },
description: "Key bullet points an ideal candidate answer should include"
}
},
required: ["score", "feedback", "strength", "weakness", "improvement"]
}
}
});
const parsed = cleanAndParseJSON(response.text || "{}");
res.json(parsed);
} catch (error: any) {
console.error("Error evaluating answer:", error);
res.status(500).json({
error: "Failed to evaluate answer using AI.",
details: error.message || String(error)
});
}
});
// 3. Generate Final Report API
app.post("/api/generate-report", async (req, res) => {
try {
const { role, experience = "Fresher", session = [] } = req.body;
if (!role || !Array.isArray(session) || session.length === 0) {
return res.status(400).json({ error: "Session data with answered questions is required." });
}
const ai = getGeminiClient();
const prompt = `You are an executive hiring panel evaluating a candidate's entire interview session.
Role: ${role}
Experience Level: ${experience}
Complete Interview Transcript & Question Scores:
${JSON.stringify(session, null, 2)}
Analyze the overall performance across all questions.
Provide a comprehensive final evaluation containing:
- Overall percentage score (0-100)
- Detailed list of top strengths
- Detailed list of key weaknesses/gaps
- Clear hiring recommendation (e.g. "Strong Hire", "Hire - Low Risk", "Needs Practice / Borderline", "Not Recommended Yet")
- Specific technical/interview topics to study & improve
- Overall candidate confidence level ("High", "Moderate", or "Needs Development")
- Executive summary paragraph for the candidate
Return JSON ONLY according to schema.`;
const response = await ai.models.generateContent({
model: "gemini-3.6-flash",
contents: prompt,
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.OBJECT,
properties: {
overallScore: { type: Type.INTEGER, description: "Percentage score 0 to 100" },
strengths: {
type: Type.ARRAY,
items: { type: Type.STRING }
},
weaknesses: {
type: Type.ARRAY,
items: { type: Type.STRING }
},
recommendation: { type: Type.STRING },
topicsToImprove: {
type: Type.ARRAY,
items: { type: Type.STRING }
},
confidence: { type: Type.STRING },
summary: { type: Type.STRING }
},
required: ["overallScore", "strengths", "weaknesses", "recommendation", "topicsToImprove", "confidence", "summary"]
}
}
});
const parsed = cleanAndParseJSON(response.text || "{}");
res.json(parsed);
} catch (error: any) {
console.error("Error generating report:", error);
res.status(500).json({
error: "Failed to generate report using AI.",
details: error.message || String(error)
});
}
});
// Vite Middleware for Dev & Static serving for Production
async function startServer() {
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Interview Agent AI Server running on http://0.0.0.0:${PORT}`);
});
}
startServer();