chore(interviews): route AI suggestions through n8n webhook

This commit is contained in:
Gabriel Ramos 2026-06-10 17:07:45 -04:00
parent b1c2f7fbd2
commit cc9087630c

View file

@ -2,11 +2,6 @@ import { NextRequest, NextResponse } from "next/server";
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
try { try {
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
return NextResponse.json({ error: "Missing GEMINI_API_KEY environment variable" }, { status: 500 });
}
const body = await request.json(); const body = await request.json();
const { const {
candidateName, candidateName,
@ -23,63 +18,55 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ error: "Missing required parameters" }, { status: 400 }); return NextResponse.json({ error: "Missing required parameters" }, { status: 400 });
} }
const formattedComments = (commentHistory || []) const webhookUrl = process.env.NEXT_PUBLIC_N8N_WEBHOOK_URL;
.map((c: { author?: string; text?: string; timestamp?: string }) => const baseUrl = webhookUrl
`[${c.timestamp ? new Date(c.timestamp).toLocaleString() : ""}] ${c.author || "Agent"}: ${c.text}` ? webhookUrl.replace(/\/evaluate-candidate$/, "")
) : "https://n8n.gaboggamer.online/webhook";
.join("\n");
const prompt = `You are an AI recruitment co-pilot. Suggest the next step for this candidate in their interview process. const targetUrl = `${baseUrl}/suggest-next-steps`;
Candidate Name: ${candidateName} const response = await fetch(targetUrl, {
Vacancy: ${jobTitle}
Current Interview Stage: ${currentStage}
Candidate Summary: ${candidateSummary || "None provided"}
Candidate Skills: ${JSON.stringify(candidateSkills || [])}
Job Requirements: ${jobRequirements || "None provided"}
Interview Comments History:
${formattedComments || "No comments yet"}
Provide your suggestion for the next steps.
Requirements:
1. MUST be extremely brief and concise (max 3-4 bullet points).
2. MUST focus on actionable suggestions based on their current stage, comment history, and candidate profile.
3. Respond in ${lang === "es" ? "Spanish" : "English"}.
4. Use standard Markdown formatting. Keep it professional.
Do not include any pre-text or post-text. Return only the markdown content.`;
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${apiKey}`,
{
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
contents: [{ candidateName,
parts: [{ text: prompt }] jobTitle,
}] currentStage,
candidateSummary,
candidateSkills,
jobRequirements,
commentHistory,
lang,
}), }),
} });
);
if (!response.ok) { if (!response.ok) {
const errText = await response.text(); const errText = await response.text();
return NextResponse.json({ error: `Gemini API error: ${response.status} - ${errText}` }, { status: 500 }); return NextResponse.json(
{ error: `n8n suggestion error: ${response.status} - ${errText}` },
{ status: 500 }
);
} }
const data = await response.json(); const data = await response.json();
const textContent = data.candidates?.[0]?.content?.parts?.[0]?.text;
if (!textContent) { // Support either direct suggestion string, or wrapped inside an object/array
return NextResponse.json({ error: "Failed to generate suggestion from Gemini" }, { status: 500 }); let result = Array.isArray(data) ? data[0] : data;
if (result && result.json) {
result = result.json;
}
const suggestionText = typeof result === "string" ? result : result.suggestion;
if (!suggestionText) {
return NextResponse.json({ error: "Invalid response structure from n8n suggestion" }, { status: 500 });
} }
return NextResponse.json({ return NextResponse.json({
success: true, success: true,
suggestion: textContent.trim(), suggestion: suggestionText.trim(),
}); });
} catch (error: unknown) { } catch (error: unknown) {