feat: flexible meal planning wizard — plan 1 meal, 1 day, several days, or a week

Backend:
- migration 005: expand menu_items.meal_type CHECK to all 6 types (second_breakfast, afternoon_snack, snack)
- ai/types.go: add Days and MealTypes to MenuRequest for partial generation
- openai/menu.go: parametrize GenerateMenu — use requested meal types and day count; add caloric fractions for all 6 meal types
- menu/repository.go: add UpsertItemsInTx for partial upsert (preserves existing slots); fix meal_type sort order in GetByWeek
- menu/handler.go: add dates+meal_types path to POST /ai/generate-menu; extract fetchImages/saveRecipes helpers; returns {"plans":[...]} for dates mode; backward-compatible with week mode

Client:
- PlanMenuSheet: bottom sheet with 4 planning horizon options
- PlanDatePickerSheet: adaptive sheet with date strip (single day/meal) or custom CalendarRangePicker (multi-day/week); sliding 7-day window for week mode
- menu_service.dart: add generateForDates
- menu_provider.dart: add PlanMenuService (generates + invalidates week providers), lastPlannedDateProvider
- home_screen.dart: add _PlanMenuButton card below quick actions; opens planning wizard
- l10n: 16 new keys for planning UI across all 12 languages

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-22 12:10:52 +02:00
parent 5096df2102
commit 9580bff54e
35 changed files with 2025 additions and 136 deletions

View File

@@ -51,7 +51,7 @@ type NutritionInfo struct {
Approximate bool `json:"approximate"`
}
// MenuRequest contains parameters for weekly menu generation.
// MenuRequest contains parameters for menu generation.
type MenuRequest struct {
UserGoal string
DailyCalories int
@@ -59,6 +59,12 @@ type MenuRequest struct {
CuisinePrefs []string
AvailableProducts []string
Lang string // ISO 639-1 target language code, e.g. "en", "ru"
// Days specifies which day-of-week slots (1=Monday…7=Sunday) to generate.
// When nil, all 7 days are generated.
Days []int
// MealTypes restricts generation to the listed meal types.
// When nil, defaults to ["breakfast","lunch","dinner"].
MealTypes []string
}
// DayPlan is the AI-generated plan for a single day.

View File

@@ -8,72 +8,91 @@ import (
"github.com/food-ai/backend/internal/adapters/ai"
)
// GenerateMenu produces a 7-day × 3-meal plan by issuing three parallel
// GenerateRecipes calls (one per meal type). This avoids token-limit errors
// that arise from requesting 21 full recipes in a single prompt.
// caloricFractions maps each meal type to its share of the daily calorie budget.
var caloricFractions = map[string]float64{
"breakfast": 0.25,
"second_breakfast": 0.10,
"lunch": 0.35,
"afternoon_snack": 0.10,
"dinner": 0.20,
"snack": 0.10,
}
// defaultMealTypes is used when MenuRequest.MealTypes is nil.
var defaultMealTypes = []string{"breakfast", "lunch", "dinner"}
// GenerateMenu produces a meal plan by issuing one parallel GenerateRecipes call per meal
// type. The number of recipes per type equals the number of days requested.
func (c *Client) GenerateMenu(ctx context.Context, req ai.MenuRequest) ([]ai.DayPlan, error) {
type mealSlot struct {
mealType string
fraction float64 // share of daily calories
mealTypes := req.MealTypes
if len(mealTypes) == 0 {
mealTypes = defaultMealTypes
}
slots := []mealSlot{
{"breakfast", 0.25},
{"lunch", 0.40},
{"dinner", 0.35},
days := req.Days
if len(days) == 0 {
days = make([]int, 7)
for dayIndex := range days {
days[dayIndex] = dayIndex + 1
}
}
recipeCount := len(days)
type mealResult struct {
recipes []ai.Recipe
err error
}
results := make([]mealResult, len(slots))
results := make([]mealResult, len(mealTypes))
var wg sync.WaitGroup
for i, slot := range slots {
for slotIndex, mealType := range mealTypes {
wg.Add(1)
go func(idx int, mealType string, fraction float64) {
go func(idx int, mealTypeName string) {
defer wg.Done()
// Scale daily calories to what this meal should contribute.
mealCal := int(float64(req.DailyCalories) * fraction)
r, err := c.GenerateRecipes(ctx, ai.RecipeRequest{
fraction, ok := caloricFractions[mealTypeName]
if !ok {
fraction = 0.15
}
mealCalories := int(float64(req.DailyCalories) * fraction)
recipes, generateError := c.GenerateRecipes(ctx, ai.RecipeRequest{
UserGoal: req.UserGoal,
DailyCalories: mealCal * 3, // prompt divides by 3 internally
DailyCalories: mealCalories * recipeCount,
Restrictions: req.Restrictions,
CuisinePrefs: req.CuisinePrefs,
Count: 7,
Count: recipeCount,
AvailableProducts: req.AvailableProducts,
Lang: req.Lang,
})
results[idx] = mealResult{r, err}
}(i, slot.mealType, slot.fraction)
results[idx] = mealResult{recipes, generateError}
}(slotIndex, mealType)
}
wg.Wait()
for i, res := range results {
if res.err != nil {
return nil, fmt.Errorf("generate %s: %w", slots[i].mealType, res.err)
for slotIndex, result := range results {
if result.err != nil {
return nil, fmt.Errorf("generate %s: %w", mealTypes[slotIndex], result.err)
}
if len(res.recipes) == 0 {
return nil, fmt.Errorf("no %s recipes returned", slots[i].mealType)
if len(result.recipes) == 0 {
return nil, fmt.Errorf("no %s recipes returned", mealTypes[slotIndex])
}
// Pad to exactly 7 by repeating the last recipe.
for len(results[i].recipes) < 7 {
results[i].recipes = append(results[i].recipes, results[i].recipes[len(results[i].recipes)-1])
// Pad to exactly recipeCount by repeating the last recipe.
for len(results[slotIndex].recipes) < recipeCount {
last := results[slotIndex].recipes[len(results[slotIndex].recipes)-1]
results[slotIndex].recipes = append(results[slotIndex].recipes, last)
}
}
days := make([]ai.DayPlan, 7)
for day := range 7 {
days[day] = ai.DayPlan{
Day: day + 1,
Meals: []ai.MealEntry{
{MealType: slots[0].mealType, Recipe: results[0].recipes[day]},
{MealType: slots[1].mealType, Recipe: results[1].recipes[day]},
{MealType: slots[2].mealType, Recipe: results[2].recipes[day]},
},
dayPlans := make([]ai.DayPlan, recipeCount)
for dayIndex, dayOfWeek := range days {
meals := make([]ai.MealEntry, len(mealTypes))
for slotIndex, mealType := range mealTypes {
meals[slotIndex] = ai.MealEntry{
MealType: mealType,
Recipe: results[slotIndex].recipes[dayIndex],
}
}
dayPlans[dayIndex] = ai.DayPlan{Day: dayOfWeek, Meals: meals}
}
return days, nil
return dayPlans, nil
}