2154512e54
Recipe cards, the recipe detail page, and meal planner still rendered raw
"{n} servings"/"{n}m cook"/"{n} srv" text and untranslated difficulty labels
outside the earlier i18n pass. Also wires the user's locale into AI features
that previously always answered in English regardless of app language:
recipe chat, ingredient substitution, recipe ideas, and generate-from-idea.
The recipe-language picker in the AI generate dialog now defaults to the
user's locale instead of always defaulting to English.
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { requireSession } from "@/lib/api-auth";
|
|
import { checkAndIncrementTierLimit, TierLimitError } from "@/lib/tiers";
|
|
import { applyRateLimit } from "@/lib/rate-limit";
|
|
import { getDefaultProviderWithKey } from "@/lib/ai/resolve-user-key";
|
|
import { substituteIngredient } from "@/lib/ai/features/substitute-ingredient";
|
|
|
|
const Schema = z.object({
|
|
ingredient: z.string().min(1).max(200),
|
|
recipeTitle: z.string().max(200).optional(),
|
|
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" }, { status: 400 });
|
|
|
|
const rateLimitRes = await applyRateLimit(`ai:substitute:${session!.user.id}`, 10, 60);
|
|
if (rateLimitRes) return rateLimitRes;
|
|
|
|
try {
|
|
await checkAndIncrementTierLimit(session!.user.id, session!.user.tier as "free" | "pro", "aiCall");
|
|
} catch (err) {
|
|
if (err instanceof TierLimitError) {
|
|
return NextResponse.json({ error: "AI call limit reached for your tier" }, { status: 403 });
|
|
}
|
|
throw err;
|
|
}
|
|
|
|
const context = parsed.data.recipeTitle
|
|
? `recipe "${parsed.data.recipeTitle}"`
|
|
: "a general recipe";
|
|
|
|
const aiConfig = parsed.data.provider
|
|
? { provider: parsed.data.provider, model: parsed.data.model }
|
|
: await getDefaultProviderWithKey(session!.user.id);
|
|
|
|
const locale = (session!.user as { locale?: string }).locale ?? "en";
|
|
|
|
const substitutions = await substituteIngredient(
|
|
parsed.data.ingredient,
|
|
context,
|
|
aiConfig,
|
|
locale
|
|
);
|
|
|
|
return NextResponse.json({ substitutions });
|
|
}
|