Files
Epicure/apps/web/lib/ai/features/generate-meal-plan.ts
T
2026-07-01 11:10:37 +02:00

87 lines
4.0 KiB
TypeScript

import { generateObject } from "ai";
import { z } from "zod";
import { resolveModel, type AiConfig } from "../factory";
const MealPlanSchema = z.object({
entries: z.array(z.object({
day: z.enum(["mon", "tue", "wed", "thu", "fri", "sat", "sun"]),
mealType: z.enum(["breakfast", "lunch", "dinner"]),
recipe: z.object({
title: z.string().max(150),
description: z.string().max(300),
ingredients: z.array(z.object({
rawName: z.string(),
quantity: z.number().optional(),
unit: z.string().optional(),
})).max(20),
steps: z.array(z.object({
instruction: z.string(),
})).max(12),
prepMins: z.number().int().min(0).max(180).optional(),
cookMins: z.number().int().min(0).max(360).optional(),
difficulty: z.enum(["easy", "medium", "hard"]).optional(),
}),
servings: z.number().int().min(1).max(20),
})),
});
export type GeneratedMealPlan = z.infer<typeof MealPlanSchema>;
const LANG: Record<string, string> = { en: "English", fr: "French" };
export async function generateMealPlan(
options: {
dietaryPrefs?: string;
servings?: number;
pantryItems?: string[];
days?: Array<"mon" | "tue" | "wed" | "thu" | "fri" | "sat" | "sun">;
pantryMode?: boolean;
difficulty?: "easy" | "medium" | "hard";
},
config?: AiConfig & { userContext?: string },
locale?: string
): Promise<GeneratedMealPlan> {
const model = resolveModel(config);
const lang = LANG[locale ?? "en"] ?? "English";
const servings = options.servings ?? 2;
const days = options.days ?? ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
const pantryMode = options.pantryMode ?? false;
const difficulty = options.difficulty;
const dietaryClause = options.dietaryPrefs?.trim()
? `Dietary requirements: ${options.dietaryPrefs.trim()}.`
: "";
const difficultyClause = difficulty
? `All recipes must be ${difficulty} difficulty: ${{ easy: "simple techniques, few steps, everyday ingredients", medium: "moderate skill, standard techniques", hard: "advanced techniques, multiple components" }[difficulty]}.`
: "";
const pantryClause =
options.pantryItems && options.pantryItems.length > 0
? `Available pantry ingredients to use: ${options.pantryItems.slice(0, 20).join(", ")}.`
: "";
const systemPrompt = pantryMode
? `You are a professional nutritionist and chef. Generate a meal plan that maximizes use of the provided pantry items. Prefer meals using multiple pantry ingredients. Minimize additional shopping required. Vary cuisines and cooking methods where possible. Make meals achievable for home cooks. For ingredients: quantity must be a number only (e.g. 0.25, 1.5, 2), unit is a separate string (e.g. 'cup', 'tbsp', 'g', 'ml'). Never combine quantity and unit. Respond in ${lang}.`
: `You are a professional nutritionist and chef. Generate balanced, practical, and delicious weekly meal plans. Vary cuisines and cooking methods throughout the week. Make meals achievable for home cooks. For ingredients: quantity must be a number only (e.g. 0.25, 1.5, 2), unit is a separate string (e.g. 'cup', 'tbsp', 'g', 'ml'). Never combine quantity and unit. Respond in ${lang}.`;
const systemPromptWithContext = systemPrompt + (config?.userContext ? `\n\nUser preferences and context:\n${config.userContext}` : "");
const { object } = await generateObject({
model,
schema: MealPlanSchema,
system: systemPromptWithContext,
prompt: [
`Generate a meal plan for ${days.length} day(s): ${days.join(", ")}.`,
`Include breakfast, lunch, and dinner for each day.`,
`Plan for ${servings} servings per meal.`,
dietaryClause,
difficultyClause,
pantryClause,
pantryMode
? "Prioritize using the listed pantry ingredients across as many meals as possible. Minimize ingredients that need to be purchased."
: "Ensure nutritional balance across the week. Vary ingredients and cooking styles.",
].filter(Boolean).join(" "),
});
return object;
}