Files
ketopath/packages/shared/src/planner/matchmaking.ts
T
lucianoandClaude Opus 4.7 d17c5ef869 feat: §6 Gruppo B residuo — pannello macros, esclusioni granulari, achievement
- Plan: pannello macros laterale "oggi" con consumati/pianificati vs target (kcal/P/F/C). Sticky su lg, in-flow su mobile. Profile/derived ora include kcalTarget e i macro target (BMR aggiustato per condizioni e diet history).
- Preferenze: nuova sezione "Ingredienti specifici da evitare" con ricerca debounced sul catalogo (cap 50). Schema Preferences.bannedIngredientIds + matchmaking che scarta ricette contenenti ingredient bandito (test unit).
- Achievement: tabella Achievement (userId+key unique), 4 traguardi iniziali (first_weigh_in, first_plan, first_fast_complete, ten_meals_consumed). Service idempotente eval+persist agganciato a 4 trigger (weight POST, plan POST, fast PATCH→COMPLETED, slot consumed). Push notification best-effort all'unlock. Pannello badge nel profilo.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 15:45:30 +02:00

240 lines
8.9 KiB
TypeScript

// PRD §9.4 — algoritmo di matchmaking pasti.
// Implementazione pura senza dipendenze: filtra ricette per esclusioni, fase
// e categoria pasto; calcola un punteggio di distanza dai macros target;
// penalizza ricette consumate di recente; bilancia sui macros già accumulati.
// Non importa nulla da @ketopath/db perché l'algoritmo deve essere
// utilizzabile anche con fixture in test e con feature-flag indipendenti
// dalla persistenza.
export interface RecipeCandidate {
id: string;
name: string;
category: 'COLAZIONE' | 'PRANZO' | 'SPUNTINO' | 'CENA';
kcal: number;
proteinG: number;
fatG: number;
netCarbG: number;
exclusionTags: ReadonlyArray<string>;
// PRD §6 — ingredient ids della ricetta. Usati per applicare le esclusioni
// granulari per singolo ingrediente (`bannedIngredientIds`).
ingredientIds?: ReadonlyArray<string>;
phases: ReadonlyArray<1 | 2 | 3>;
prepMinutes?: number;
}
export interface MacroTargets {
kcal: number;
proteinG: number;
fatG: number;
netCarbG: number;
}
export interface MatchOptions {
candidates: ReadonlyArray<RecipeCandidate>;
meal: RecipeCandidate['category'];
phase: 1 | 2 | 3;
excludedTags: ReadonlyArray<string>;
// PRD §6 — ids di Ingredient che l'utente ha bandito espressamente.
// Una ricetta è scartata se contiene almeno uno di questi ingredients.
bannedIngredientIds?: ReadonlyArray<string>;
recentlyConsumedIds?: ReadonlyArray<string>;
// Macros già consumati nello stesso giorno; usati per bilanciare le scelte
// successive (la cena tiene conto di colazione/pranzo/spuntino).
consumedSoFar?: MacroTargets;
dailyTarget: MacroTargets;
// Quote del pasto sul totale giornaliero (sommano a 1.0).
mealShare: { COLAZIONE: number; PRANZO: number; SPUNTINO: number; CENA: number };
topN?: number; // default 5
// Tempo massimo accettabile in cucina per pasto (in minuti). Se la ricetta
// supera la soglia, lo score viene penalizzato proporzionalmente.
// Soft cap, non hard filter: lo SPUNTINO tipicamente è < 5 min comunque,
// mentre la CENA può tollerare elaborazioni più lunghe.
maxPrepMinutes?: number;
}
export interface MatchResult extends RecipeCandidate {
score: number;
}
/**
* Restituisce le `topN` ricette più adatte, ordinate dalla migliore
* (score più basso) alla peggiore. Lo score combina:
* - distanza euclidea dai macros target del pasto (pesato sul giorno residuo)
* - penalità additiva se la ricetta è stata consumata negli ultimi N giorni
*/
export function matchMeals(opts: MatchOptions): MatchResult[] {
const consumed = opts.consumedSoFar ?? { kcal: 0, proteinG: 0, fatG: 0, netCarbG: 0 };
const remainingTarget: MacroTargets = {
kcal: Math.max(0, opts.dailyTarget.kcal - consumed.kcal),
proteinG: Math.max(0, opts.dailyTarget.proteinG - consumed.proteinG),
fatG: Math.max(0, opts.dailyTarget.fatG - consumed.fatG),
netCarbG: Math.max(0, opts.dailyTarget.netCarbG - consumed.netCarbG),
};
const share = opts.mealShare[opts.meal];
const mealTarget: MacroTargets = {
kcal: remainingTarget.kcal * share,
proteinG: remainingTarget.proteinG * share,
fatG: remainingTarget.fatG * share,
netCarbG: remainingTarget.netCarbG * share,
};
const recentSet = new Set(opts.recentlyConsumedIds ?? []);
const exclusionSet = new Set(opts.excludedTags);
const bannedSet = new Set(opts.bannedIngredientIds ?? []);
const results: MatchResult[] = [];
for (const r of opts.candidates) {
if (r.category !== opts.meal) continue;
if (!r.phases.includes(opts.phase)) continue;
if (r.exclusionTags.some((tag) => exclusionSet.has(tag))) continue;
if (bannedSet.size > 0 && r.ingredientIds && r.ingredientIds.some((id) => bannedSet.has(id)))
continue;
const dKcal = (r.kcal - mealTarget.kcal) / Math.max(1, mealTarget.kcal);
const dPro = (r.proteinG - mealTarget.proteinG) / Math.max(1, mealTarget.proteinG);
const dFat = (r.fatG - mealTarget.fatG) / Math.max(1, mealTarget.fatG);
const dCarb = (r.netCarbG - mealTarget.netCarbG) / Math.max(1, mealTarget.netCarbG);
let score = Math.sqrt(dKcal * dKcal + dPro * dPro + dFat * dFat + dCarb * dCarb);
if (recentSet.has(r.id)) score += 1.5;
// Penalty proporzionale per ricette oltre il tempo cucina dichiarato.
// 0.05 punti per ogni 5 min di sforamento — cumulativo ma morbido.
if (opts.maxPrepMinutes != null && r.prepMinutes != null) {
const overflow = Math.max(0, r.prepMinutes - opts.maxPrepMinutes);
score += overflow * 0.01;
}
results.push({ ...r, score });
}
results.sort((a, b) => a.score - b.score);
return results.slice(0, opts.topN ?? 5);
}
export type MealShare = MatchOptions['mealShare'];
// PRD §5.1 — soglia di tempo cucina per livello dichiarato.
// Soglia ≠ filtro stretto: oltre la soglia il match-score viene penalizzato.
export function maxPrepMinutesFor(level: 'LOW' | 'MEDIUM' | 'HIGH' | null | undefined): number {
switch (level) {
case 'LOW':
return 15;
case 'HIGH':
return 60;
case 'MEDIUM':
case null:
case undefined:
default:
return 30;
}
}
// Default share per pasto (somma 1.0). Può essere sovrascritta dall'utente.
export const DEFAULT_MEAL_SHARE: MealShare = {
COLAZIONE: 0.25,
PRANZO: 0.35,
SPUNTINO: 0.1,
CENA: 0.3,
};
export type FastingProtocolKey =
| 'FOURTEEN_TEN'
| 'SIXTEEN_EIGHT'
| 'EIGHTEEN_SIX'
| 'TWENTY_FOUR'
| 'ESE_24'
| 'FIVE_TWO';
/**
* Piano del singolo giorno per un certo protocollo IF.
* - `share`: quote per pasto (sommano a `kcalMultiplier`, non a 1, perché un
* giorno di digiuno completo ha tutto a 0).
* - `kcalMultiplier`: moltiplicatore sul TDEE/kcalTarget del giorno.
* - 1.0 → giorno normale
* - 0.0 → giorno di digiuno completo (il piano salta il giorno intero)
* - 0.25 → giorno "fasting day" del 5:2 (~500 kcal su 2000)
*/
export interface ProtocolDayPlan {
share: MealShare;
kcalMultiplier: number;
}
const ALL_ZERO: MealShare = { COLAZIONE: 0, PRANZO: 0, SPUNTINO: 0, CENA: 0 };
// PRD §5.6 + §9.4 — finestra alimentare per protocollo, declinata per giorno
// della settimana. `dayOfWeek` è Mon-anchored: 0=Lunedì … 6=Domenica.
//
// 14:10 → 10h, tutti i pasti, ogni giorno.
// 16:8 → 8h, niente colazione (brunch + cena + spuntino), ogni giorno.
// 18:6 → 6h, niente colazione, pranzo grande + cena + spuntino piccolo.
// 20:4 → 4h, un solo pasto sostanzioso a cena + spuntino opzionale.
// ESE 24h → mercoledì digiuno completo (kcalMultiplier=0), altri giorni
// default. Variante "una volta a settimana" del classico Eat-Stop-Eat.
// 5:2 → lunedì + giovedì giorni "magri" (~500 kcal, un solo pasto a cena),
// gli altri 5 giorni default.
export function protocolPlanForDay(
protocol: FastingProtocolKey | null | undefined,
dayOfWeek: number,
): ProtocolDayPlan {
switch (protocol) {
case 'SIXTEEN_EIGHT':
return {
share: { COLAZIONE: 0, PRANZO: 0.5, SPUNTINO: 0.1, CENA: 0.4 },
kcalMultiplier: 1,
};
case 'EIGHTEEN_SIX':
return {
share: { COLAZIONE: 0, PRANZO: 0.55, SPUNTINO: 0.05, CENA: 0.4 },
kcalMultiplier: 1,
};
case 'TWENTY_FOUR':
return {
share: { COLAZIONE: 0, PRANZO: 0, SPUNTINO: 0.1, CENA: 0.9 },
kcalMultiplier: 1,
};
case 'ESE_24':
// Mercoledì (dayOfWeek === 2) digiuno completo, altrimenti normale.
return dayOfWeek === 2
? { share: ALL_ZERO, kcalMultiplier: 0 }
: { share: DEFAULT_MEAL_SHARE, kcalMultiplier: 1 };
case 'FIVE_TWO':
// Lunedì (0) e giovedì (3) "fasting days": un solo pasto a cena con
// kcal complessive ridotte (~25% di TDEE, target classico ~500 kcal).
// Nota: la share somma comunque a 1 (è la quota del *giorno ridotto*),
// mentre la riduzione totale viene applicata via kcalMultiplier.
return dayOfWeek === 0 || dayOfWeek === 3
? {
share: { COLAZIONE: 0, PRANZO: 0, SPUNTINO: 0, CENA: 1 },
kcalMultiplier: 0.25,
}
: { share: DEFAULT_MEAL_SHARE, kcalMultiplier: 1 };
case 'FOURTEEN_TEN':
case null:
case undefined:
default:
return { share: DEFAULT_MEAL_SHARE, kcalMultiplier: 1 };
}
}
// PRD §5.1 — Quote per pasto in base alla frequenza dichiarata dall'utente.
// Usato solo in assenza di un fastingProtocol attivo (che ha precedenza).
//
// 1 pasto → solo cena (OMAD light)
// 2 pasti → pranzo + cena
// 3 pasti → colazione + pranzo + cena, niente spuntino
// 4 pasti → default (con spuntino)
export function mealShareForFrequency(mealsPerDay: number | null | undefined): MealShare {
switch (mealsPerDay) {
case 1:
return { COLAZIONE: 0, PRANZO: 0, SPUNTINO: 0, CENA: 1 };
case 2:
return { COLAZIONE: 0, PRANZO: 0.5, SPUNTINO: 0, CENA: 0.5 };
case 3:
return { COLAZIONE: 0.3, PRANZO: 0.4, SPUNTINO: 0, CENA: 0.3 };
case 4:
default:
return DEFAULT_MEAL_SHARE;
}
}