-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
99 lines (85 loc) · 2.57 KB
/
Copy pathserver.ts
File metadata and controls
99 lines (85 loc) · 2.57 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
import express from "express";
import path from "path";
import { createServer as createViteServer } from "vite";
import { GoogleGenAI, Type } from "@google/genai";
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY,
httpOptions: {
headers: {
"User-Agent": "aistudio-build",
},
},
});
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
app.post("/api/analyze", async (req, res) => {
try {
const { code, output, error } = req.body;
const prompt = `You are an expert Python tutor.
Analyze the following Python code written by a student, and its execution result.
Student's Code:
\`\`\`python
${code}
\`\`\`
Execution Output (stdout):
\`\`\`
${output}
\`\`\`
Execution Error (stderr):
\`\`\`
${error || "None"}
\`\`\`
Determine if the code is correct or wrong.
Explain any syntax or logic errors clearly and constructively to the student.
If the code is correct but can be improved, provide a tip.`;
const response = await ai.models.generateContent({
model: "gemini-3.5-flash",
contents: prompt,
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.OBJECT,
properties: {
status: {
type: Type.STRING,
description: "Either 'Correct', 'Incorrect', or 'Error'",
},
feedback: {
type: Type.STRING,
description: "Markdown formatted feedback explaining the logic and syntax, telling the student why it is correct or wrong.",
},
tip: {
type: Type.STRING,
description: "Optional tip for improvement if the code is correct, or empty string.",
}
},
required: ["status", "feedback", "tip"],
},
},
});
res.json(JSON.parse(response.text || "{}"));
} catch (err: any) {
console.error(err);
res.status(500).json({ error: "Failed to analyze code." });
}
});
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(`Server running on http://localhost:${PORT}`);
});
}
startServer();