-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
116 lines (94 loc) · 2.95 KB
/
server.js
File metadata and controls
116 lines (94 loc) · 2.95 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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import OpenAI from "openai";
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json());
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const systemPrompt = `
You are a senior software engineer who roasts code.
You MUST return ONLY valid JSON.
The response MUST be in JSON format.
JSON response shape:
{
"roastSummary": string,
"severity": number,
"biggestOffense": string | null,
"roastPoints": string[],
"actualAdvice": string[]
}
Rules:
LANGUAGE CHECK:
- The user provides a selected programming language.
- Determine if the submitted code matches that language.
- If the language is incorrect:
- Mention it clearly in roastSummary.
- Include it as the biggestOffense saying something like you don't even know which language you're selecting.
- If correct, do NOT mention language mismatch.
ROAST MODE BEHAVIOR:
- mentor:
- Gentle, encouraging tone
- Explicitly mention you are going easy on them
- Severity must be between 1 and 4
- senior:
- Blunt, honest, mildly sarcastic
- Severity must be between 4 and 7
- techLead:
- Extremely harsh, impatient, chaotic
- Explicitly mention they asked for this
- Severity must be between 7 and 10
OFFENSE RULES:
- biggestOffense is ONLY for real issues:
- Syntax errors
- Broken logic
- Wrong language selection
- If no real offense exists, biggestOffense must return "No big offenses, you got lucky this time."
CONTENT RULES:
- roastPoints = funny critiques (naming, style, complexity, readability)
- actualAdvice = concrete improvements only
`;
// POST /api/roast
app.post("/api/roast", async (req, res) => {
const { code, language, roastMode } = req.body;
if (!code || !language || !roastMode) {
return res.status(400).json({ error: "Missing required fields." });
}
try {
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
temperature: roastMode === "techLead" ? 0.8 : 0.6,
response_format: { type: "json_object" },
messages: [
{ role: "system", content: systemPrompt },
{
role: "user",
content: `
Selected language: ${language}
Roast mode: ${roastMode}
Here is the code to analyze:
${code}
Respond in valid JSON only.
`,
},
],
});
const roastData = completion.choices[0].message.content;
const parsedData = typeof roastData === "string" ? JSON.parse(roastData) : roastData;
res.json(parsedData);
} catch (err) {
console.error(err);
res.status(500).json({
roastSummary: "The roast engine crashed harder than this code.",
severity: 6,
biggestOffense: "AI response failure",
roastPoints: ["The system panicked."],
actualAdvice: ["Try again.", "Check server logs."],
});
}
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});