S5: onboarding → minne + smaksignaler (user_stated)

This commit is contained in:
Sven (AAMOS AI)
2026-08-11 04:43:43 +07:00
parent 1e18146665
commit f86a37dd33
26 changed files with 10812 additions and 2 deletions
+2
View File
@@ -281,6 +281,7 @@ export async function computeMemoryImpact(options: MemoryImpactOptions): Promise
id: schema.tasteSignals.id,
userId: schema.tasteSignals.userId,
axis: schema.tasteSignals.axis,
target: schema.tasteSignals.target,
direction: schema.tasteSignals.direction,
strength: schema.tasteSignals.strength,
origin: schema.tasteSignals.origin,
@@ -291,6 +292,7 @@ export async function computeMemoryImpact(options: MemoryImpactOptions): Promise
.where(eq(schema.tasteSignals.userId, userId));
const tasteSignals: TasteSignal[] = tasteRows.map((t) => ({
...t,
target: t.target ?? undefined,
refRecipeId: t.refRecipeId ?? undefined,
createdAt: t.createdAt.toISOString(),
}));
+156
View File
@@ -0,0 +1,156 @@
import type { Database } from "@app/database";
import { schema } from "@app/database";
import type { Allergen, Cuisine, GoalType } from "@app/shared-types";
/**
* Spegla onboarding-angivna preferenser i användarens synliga minne och
* smaksignaler (S5). Allt skrivs som `user_stated` / `verifiedByUser=true`.
*
* - memory_items: upsert på (userId, key).
* - taste_signals: upsert på (userId, axis, target).
*
* Idempotent upprepade anrop skapar inga dubletter.
*/
const ONBOARDING_ORIGIN = "user_stated" as const;
const ONBOARDING_CONFIDENCE = 1;
const TASTE_STRENGTH = 0.9;
export interface OnboardingMemoryInput {
primaryGoal?: GoalType | null;
goals?: GoalType[];
allergens?: Allergen[];
favoriteCuisines?: Cuisine[];
avoidIngredientIds?: string[];
spiceLevelMax?: number | null;
}
interface MemoryEntry {
key: string;
summarySv: string;
value: unknown;
}
export async function syncOnboardingMemory(
db: Database,
userId: string,
prefs: OnboardingMemoryInput,
): Promise<void> {
const memoryEntries: MemoryEntry[] = [];
if (prefs.primaryGoal) {
memoryEntries.push({
key: `goal:primary:${prefs.primaryGoal}`,
summarySv: `Primärt mål: ${prefs.primaryGoal}`,
value: { primaryGoal: prefs.primaryGoal },
});
}
for (const goal of prefs.goals ?? []) {
memoryEntries.push({
key: `goal:${goal}`,
summarySv: `Mål: ${goal}`,
value: { goal },
});
}
for (const allergen of prefs.allergens ?? []) {
memoryEntries.push({
key: `allergen:${allergen}`,
summarySv: `Allergi: ${allergen}`,
value: { allergen },
});
}
for (const cuisine of prefs.favoriteCuisines ?? []) {
memoryEntries.push({
key: `favorite-cuisine:${cuisine}`,
summarySv: `Favoritkök: ${cuisine}`,
value: { favoriteCuisine: cuisine },
});
}
for (const ingredientId of prefs.avoidIngredientIds ?? []) {
memoryEntries.push({
key: `avoid-ingredient:${ingredientId}`,
summarySv: `Undviker ingrediens: ${ingredientId}`,
value: { avoidIngredientId: ingredientId },
});
}
if (prefs.spiceLevelMax != null) {
memoryEntries.push({
key: "spice-level-max",
summarySv: `Max styrka: ${prefs.spiceLevelMax}`,
value: { spiceLevelMax: prefs.spiceLevelMax },
});
}
for (const entry of memoryEntries) {
await db
.insert(schema.memoryItems)
.values({
userId,
kind: "structured_fact",
key: entry.key,
summarySv: entry.summarySv,
value: entry.value,
origin: ONBOARDING_ORIGIN,
confidence: ONBOARDING_CONFIDENCE,
verifiedByUser: true,
})
.onConflictDoUpdate({
target: [schema.memoryItems.userId, schema.memoryItems.key],
set: {
summarySv: entry.summarySv,
value: entry.value,
origin: ONBOARDING_ORIGIN,
confidence: ONBOARDING_CONFIDENCE,
verifiedByUser: true,
updatedAt: new Date(),
},
});
}
const tasteEntries: Array<{
axis: "cuisine" | "ingredient_avoid";
target: string;
direction: number;
}> = [];
for (const cuisine of prefs.favoriteCuisines ?? []) {
tasteEntries.push({ axis: "cuisine", target: cuisine, direction: 1 });
}
for (const ingredientId of prefs.avoidIngredientIds ?? []) {
tasteEntries.push({
axis: "ingredient_avoid",
target: ingredientId,
direction: -1,
});
}
for (const signal of tasteEntries) {
await db
.insert(schema.tasteSignals)
.values({
userId,
axis: signal.axis,
target: signal.target,
direction: signal.direction,
strength: TASTE_STRENGTH,
origin: ONBOARDING_ORIGIN,
})
.onConflictDoUpdate({
target: [
schema.tasteSignals.userId,
schema.tasteSignals.axis,
schema.tasteSignals.target,
],
set: {
direction: signal.direction,
strength: TASTE_STRENGTH,
origin: ONBOARDING_ORIGIN,
},
});
}
}
+4
View File
@@ -12,6 +12,7 @@ import { computeDailyTargets, DEFAULT_TARGETS } from "@app/nutrition-engine";
import { errors, parse } from "../lib/errors.js";
import { audit, generateInviteCode, getActiveHouseholdId } from "../lib/helpers.js";
import { loadEntitlementsWithToken } from "../lib/entitlements.js";
import { syncOnboardingMemory } from "../lib/onboardingMemory.js";
/** Profil, preferenser, samtycken, dagsmål, GDPR-export/-radering (spec §6, §56). */
export async function meRoutes(app: FastifyInstance) {
@@ -120,6 +121,9 @@ export async function meRoutes(app: FastifyInstance) {
target: schema.userPreferences.userId,
set: { ...input.preferences, updatedAt: new Date() },
});
// S5: spegla uttryckligen angivna preferenser i minne + smaksignaler.
await syncOnboardingMemory(app.db, req.userId, input.preferences);
}
let householdId: string | null = await getActiveHouseholdId(app.db, req.userId);
+11
View File
@@ -12,6 +12,7 @@ import {
import { t } from "../lib/i18n.js";
import { KNOWN_FLAGS } from "@app/feature-flags";
import { syncOnboardingMemory } from "../lib/onboardingMemory.js";
/**
* Progressive onboarding (FAS 1b): 3-layer flow.
@@ -227,6 +228,16 @@ export async function onboardingRoutes(app: FastifyInstance) {
});
}
// S5: spegla alla uttryckligen angivna preferenser i minne + smaksignaler.
const [completedPrefs] = await app.db
.select()
.from(schema.userPreferences)
.where(eq(schema.userPreferences.userId, req.userId))
.limit(1);
if (completedPrefs) {
await syncOnboardingMemory(app.db, req.userId, completedPrefs);
}
const [user] = await app.db
.update(schema.users)
.set({
+2
View File
@@ -276,6 +276,7 @@ export async function recommendationRoutes(app: FastifyInstance) {
id: schema.tasteSignals.id,
userId: schema.tasteSignals.userId,
axis: schema.tasteSignals.axis,
target: schema.tasteSignals.target,
direction: schema.tasteSignals.direction,
strength: schema.tasteSignals.strength,
origin: schema.tasteSignals.origin,
@@ -286,6 +287,7 @@ export async function recommendationRoutes(app: FastifyInstance) {
.where(eq(schema.tasteSignals.userId, req.userId));
tasteSignals = tasteRows.map((t) => ({
...t,
target: t.target ?? undefined,
refRecipeId: t.refRecipeId ?? undefined,
createdAt: t.createdAt.toISOString(),
}));