ai integration

This commit is contained in:
2025-08-15 00:44:26 -04:00
parent 238d3cfbfe
commit 3ee7be9110
3 changed files with 199 additions and 67 deletions

View File

@@ -0,0 +1,46 @@
import { NextResponse } from "next/server";
import type { CalendarEvent } from "@/lib/types";
export async function POST(request: Request) {
try {
const { events } = await request.json();
if (!events || !Array.isArray(events)) {
return NextResponse.json(
{ error: "Invalid events array" },
{ status: 400 },
);
}
const res = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`, // Server-side only
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "@preset/i-cal-editor-summarize", // FREE model
messages: [
{
role: "system",
content: `You summarize a list of events in natural language. Include date, time, and title. Be concise.`,
},
{ role: "user", content: JSON.stringify(events) },
],
temperature: 0.4,
// max_tokens: 300,
}),
});
const data = await res.json();
const summary =
data?.choices?.[0]?.message?.content || "No summary generated.";
return NextResponse.json({ summary });
} catch (error) {
console.error(error);
return NextResponse.json(
{ error: "Failed to summarize events" },
{ status: 500 },
);
}
}