c31ab8771a
Widens the tier enum from free/pro to free/pro/team and every "free" | "pro" cast that assumed exactly two tiers (~30 call sites: every AI route's withAiQuota/checkAndIncrementTierLimit call, admin user/invite management, upload quota checks, OpenAPI schemas). Team sits above Pro with genuinely unlimited recipes/public-recipes (the -1 sentinel, which Pro doesn't actually use — Pro uses large finite numbers instead) and a higher AI-call/storage cap. Seeded via db:seed, editable afterward from Admin > Tiers. role (user/moderator/admin — permissions) and tier (free/pro/team — billing limits) stay separate concepts, as they already were; this does not touch role-based permissions. Requires migration 0043 to run against a live DB — not applied in this sandbox (no Docker here); run `pnpm db:migrate` then `pnpm db:seed`. v0.44.0
57 lines
2.4 KiB
TypeScript
57 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { requireSessionOrApiKey } from "@/lib/api-auth";
|
|
import { applyRateLimit } from "@/lib/rate-limit";
|
|
import { withAiQuota, resolveAiConfigOrError } from "@/lib/ai/ai-error";
|
|
import { regenerateRecipe } from "@/lib/ai/features/regenerate-recipe";
|
|
import { withUserKey } from "@/lib/ai/resolve-user-key";
|
|
|
|
const Schema = z.object({
|
|
title: z.string().min(1).max(200),
|
|
description: z.string().max(2000).optional(),
|
|
baseServings: z.number().int().min(1).max(100),
|
|
difficulty: z.enum(["easy", "medium", "hard"]).optional(),
|
|
ingredients: z.array(z.object({
|
|
rawName: z.string().min(1).max(200),
|
|
quantity: z.union([z.string(), z.number()]).optional(),
|
|
unit: z.string().max(50).optional(),
|
|
})).max(100),
|
|
steps: z.array(z.object({ instruction: z.string().min(1).max(2000) })).max(100),
|
|
instruction: z.string().min(1).max(500),
|
|
language: z.string().max(10).default("en"),
|
|
provider: z.enum(["openai", "anthropic", "openrouter", "ollama"]).optional(),
|
|
model: z.string().optional(),
|
|
});
|
|
|
|
// No recipeId, no DB access — this is a stateless AI transform over whatever
|
|
// draft the editor currently holds (including unsaved edits), not the saved
|
|
// row. The caller merges the result into their own in-progress form state.
|
|
export async function POST(req: NextRequest) {
|
|
const { session, response } = await requireSessionOrApiKey(req);
|
|
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;
|
|
|
|
const configResult = await resolveAiConfigOrError(() =>
|
|
withUserKey(session!.user.id, { provider: parsed.data.provider, model: parsed.data.model })
|
|
);
|
|
if (!configResult.ok) return configResult.response;
|
|
const aiConfig = configResult.data;
|
|
|
|
const { instruction, language, ...current } = parsed.data;
|
|
|
|
const result = await withAiQuota(session!.user.id, session!.user.tier as "free" | "pro" | "team", () =>
|
|
regenerateRecipe(current, instruction, { ...aiConfig, language }), { skipQuota: aiConfig.isByok }
|
|
);
|
|
if (!result.ok) return result.response;
|
|
|
|
return NextResponse.json(result.data);
|
|
}
|