Files
local-cal/src/app/api/ai-event/route.ts

60 lines
1.7 KiB
TypeScript

import { NextResponse } from "next/server";
export async function POST(request: Request) {
const { prompt } = await request.json();
const systemPrompt = `
You are an assistant that converts natural language into an ARRAY of calendar events.
TypeScript type:
{
id?: string,
title: string,
description?: string,
location?: string,
url?: string,
start: string, // ISO datetime
end?: string,
allDay?: boolean,
recurrenceRule?: string // valid iCal RRULE string like FREQ=WEEKLY;BYDAY=MO;INTERVAL=1
}[]
Rules:
- If the user describes multiple events in one prompt, return multiple objects (one per event).
- Always return a valid JSON array of objects, even if there's only one event.
- Today is ${new Date().toLocaleString()}.
- If no time is given, assume allDay event.
- If no end time is given (and event is not allDay), default to 1 hour after start.
- If multiple events are described, return multiple.
- If recurrence is implied (e.g. "every Monday", "daily for 10 days", "monthly on the 15th"), generate a recurrenceRule.
- 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);
return NextResponse.json(parsed);
} catch (e) {
return NextResponse.json(
{ error: "Failed to parse AI output", raw: data },
{ status: 500 },
);
}
}