chore(interviews): route AI suggestions through n8n webhook
This commit is contained in:
parent
b1c2f7fbd2
commit
cc9087630c
1 changed files with 36 additions and 49 deletions
|
|
@ -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}
|
method: "POST",
|
||||||
Current Interview Stage: ${currentStage}
|
headers: {
|
||||||
Candidate Summary: ${candidateSummary || "None provided"}
|
"Content-Type": "application/json",
|
||||||
Candidate Skills: ${JSON.stringify(candidateSkills || [])}
|
},
|
||||||
Job Requirements: ${jobRequirements || "None provided"}
|
body: JSON.stringify({
|
||||||
|
candidateName,
|
||||||
Interview Comments History:
|
jobTitle,
|
||||||
${formattedComments || "No comments yet"}
|
currentStage,
|
||||||
|
candidateSummary,
|
||||||
Provide your suggestion for the next steps.
|
candidateSkills,
|
||||||
Requirements:
|
jobRequirements,
|
||||||
1. MUST be extremely brief and concise (max 3-4 bullet points).
|
commentHistory,
|
||||||
2. MUST focus on actionable suggestions based on their current stage, comment history, and candidate profile.
|
lang,
|
||||||
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",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
contents: [{
|
|
||||||
parts: [{ text: prompt }]
|
|
||||||
}]
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
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) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue