d9d58fd01a
Provider factory (OpenAI/Anthropic/OpenRouter/Ollama). generateObject for all outputs. Features: recipe generation, photo-to-recipe vision, variations, drink/meal pairings, ingredient substitution, recipe adaptation, nutrition analysis, URL import, meal plan gen. User BYOK keys (AES-256-GCM). Per-use-case model preferences (text/vision/mealPlan). Site-level key override via admin settings.
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { requireSession } from "@/lib/api-auth";
|
|
import { applyRateLimit } from "@/lib/rate-limit";
|
|
import { checkTierLimit, incrementUsage } from "@/lib/tiers";
|
|
import { importFromUrl } from "@/lib/ai/features/import-url";
|
|
|
|
const Schema = z.object({
|
|
url: z.string().url(),
|
|
provider: z.enum(["openai", "anthropic", "openrouter", "ollama"]).optional(),
|
|
model: z.string().optional(),
|
|
});
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const { session, response } = await requireSession();
|
|
if (response) return response;
|
|
|
|
const body = await req.json() as unknown;
|
|
const parsed = Schema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: "Validation error", issues: parsed.error.issues }, { status: 400 });
|
|
}
|
|
|
|
const limited = await applyRateLimit(`rl:ai:${session!.user.id}`, 10, 60);
|
|
if (limited) return limited;
|
|
|
|
await checkTierLimit(session!.user.id, session!.user.tier as "free" | "pro", "aiCall");
|
|
|
|
const recipe = await importFromUrl(parsed.data.url, {
|
|
provider: parsed.data.provider,
|
|
model: parsed.data.model,
|
|
});
|
|
|
|
await incrementUsage(session!.user.id, "aiCall");
|
|
|
|
return NextResponse.json(recipe);
|
|
}
|