style(api): standardize API route file formatting
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/auth";
|
||||
import { headers } from "next/headers";
|
||||
import { openRouterClient } from "@/lib/openrouter-client";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await auth.api.getSession({
|
||||
headers: await headers(),
|
||||
});
|
||||
|
||||
|
||||
if (!session?.user) {
|
||||
return NextResponse.json(
|
||||
{ error: "Authentication required" },
|
||||
{ status: 401 }
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -20,13 +21,13 @@ export async function POST(request: Request) {
|
||||
if (!prompt || typeof prompt !== "string" || prompt.trim().length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Prompt is required and must be a non-empty string" },
|
||||
{ status: 400 }
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
if (prompt.length > 2000) {
|
||||
return NextResponse.json(
|
||||
{ error: "Prompt must be less than 2000 characters" },
|
||||
{ status: 400 }
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -57,29 +58,23 @@ Rules:
|
||||
- Output ONLY valid JSON (no prose).
|
||||
`;
|
||||
|
||||
const res = await fetch("https://openrouter.ai/api/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "openai/gpt-4.1-nano",
|
||||
messages: [
|
||||
{ role: "system", content: systemPrompt },
|
||||
{ role: "user", content: prompt },
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
try {
|
||||
const content = data.choices[0].message.content;
|
||||
const parsed = JSON.parse(content);
|
||||
const result = openRouterClient.callModel({
|
||||
model: "openai/gpt-5.4-mini",
|
||||
instructions: systemPrompt,
|
||||
input: prompt,
|
||||
});
|
||||
|
||||
const text = await result.getText();
|
||||
const parsed = JSON.parse(text);
|
||||
return NextResponse.json(parsed);
|
||||
} catch {
|
||||
} catch (error) {
|
||||
console.error("AI Event Creation Error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to parse AI output", raw: data },
|
||||
{
|
||||
error: "Failed to parse AI output",
|
||||
raw: error instanceof Error ? error.message : error,
|
||||
},
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user