fix(skiva-1): spara accept, bildref, lokal träningsbank, lärande-loop-dok
- confirm-loop sparar nu även action=accept som positivt exempel i ai_corrections - imageS3Key sparas vid image_training-samtycke, annars null - ai_corrections.proposal lagrar det specifika AI-förslaget per item - BUILD_TRAINING_SAMPLE bankar lokalt till ai_training_bank (ej externt runTask) - Jobbet kastar aldrig i AAMOS_MODE=gemini - Migration 0020: ai_corrections.image_s3_key/proposal + ai_training_bank - docs/28-lärande-loop.md: datakontrakt, samtycke, retention, GDPR-radering - Tester: accept + bildref (ja/nej) + lokal bank i gemini-läge - REQUIRE_REAL=1 är AI-fokuserat i gemini-läge; staging mail/S3 får vara mock
This commit is contained in:
@@ -129,17 +129,12 @@ export async function scanRoutes(app: FastifyInstance) {
|
||||
input.storageLocationId ?? (await defaultLocation(app, householdId, job.scanType));
|
||||
|
||||
const created: string[] = [];
|
||||
const proposals = extractProposals(job);
|
||||
for (const item of input.items) {
|
||||
if (item.action === "reject") {
|
||||
await recordCorrection(app, job, item.tempId ?? null, { action: "reject" });
|
||||
continue;
|
||||
}
|
||||
if (item.action === "edit" || item.action === "add") {
|
||||
await recordCorrection(app, job, item.tempId ?? null, {
|
||||
action: item.action,
|
||||
corrected: { name: item.displayName, quantity: item.quantity, unit: item.unit },
|
||||
});
|
||||
}
|
||||
const proposal = findProposal(proposals, item.tempId);
|
||||
await recordCorrection(app, job, item, proposal);
|
||||
if (item.action === "reject") continue;
|
||||
|
||||
const locationId = item.storageLocationId ?? fallbackLocation;
|
||||
if (!locationId)
|
||||
throw errors.badRequest("storageLocationId saknas och ingen standardplats finns.");
|
||||
@@ -334,6 +329,39 @@ async function defaultLocation(app: FastifyInstance, householdId: string, scanTy
|
||||
return loc?.id ?? null;
|
||||
}
|
||||
|
||||
type ProposalItem = {
|
||||
tempId?: string;
|
||||
detectedName?: string;
|
||||
canonicalIngredientId?: string | null;
|
||||
brand?: string | null;
|
||||
estimatedQuantity?: number | null;
|
||||
unit?: string | null;
|
||||
bestBeforeDate?: string | null;
|
||||
confidence?: number;
|
||||
requiresConfirmation?: boolean;
|
||||
};
|
||||
|
||||
function extractProposals(job: { result: unknown }): ProposalItem[] {
|
||||
const result = job.result as Record<string, unknown> | null;
|
||||
if (!result || !Array.isArray(result.items)) return [];
|
||||
return result.items.map((it, idx) => ({
|
||||
tempId: String(it.tempId ?? idx),
|
||||
detectedName: String(it.detectedName ?? ""),
|
||||
canonicalIngredientId: it.canonicalIngredientId ?? null,
|
||||
brand: it.brand ?? null,
|
||||
estimatedQuantity: it.estimatedQuantity ?? null,
|
||||
unit: it.unit ?? null,
|
||||
bestBeforeDate: it.bestBeforeDate ?? null,
|
||||
confidence: typeof it.confidence === "number" ? it.confidence : null,
|
||||
requiresConfirmation: typeof it.requiresConfirmation === "boolean" ? it.requiresConfirmation : null,
|
||||
}));
|
||||
}
|
||||
|
||||
function findProposal(proposals: ProposalItem[], tempId: string | undefined): ProposalItem | null {
|
||||
if (tempId == null) return null;
|
||||
return proposals.find((p) => p.tempId === tempId) ?? null;
|
||||
}
|
||||
|
||||
async function recordCorrection(
|
||||
app: FastifyInstance,
|
||||
job: {
|
||||
@@ -341,33 +369,62 @@ async function recordCorrection(
|
||||
userId: string;
|
||||
jobType: string;
|
||||
result: unknown;
|
||||
s3Keys: string[];
|
||||
modelVersion: string | null;
|
||||
promptVersion: string | null;
|
||||
},
|
||||
tempId: string | null,
|
||||
correction: Record<string, unknown>,
|
||||
item: {
|
||||
tempId?: string;
|
||||
action: "accept" | "edit" | "reject" | "add";
|
||||
displayName: string;
|
||||
quantity: number;
|
||||
unit: string;
|
||||
brand?: string;
|
||||
canonicalIngredientId?: string;
|
||||
bestBeforeDate?: string;
|
||||
useByDate?: string;
|
||||
},
|
||||
proposal: ProposalItem | null,
|
||||
) {
|
||||
const consents = await app.db
|
||||
.select()
|
||||
.from(schema.userConsents)
|
||||
.where(eq(schema.userConsents.userId, job.userId));
|
||||
const snapshot = Object.fromEntries(consents.map((c) => [c.kind, c.status]));
|
||||
const hasImageConsent = snapshot.image_training === "granted";
|
||||
|
||||
const userCorrection: Record<string, unknown> = { action: item.action };
|
||||
if (item.action !== "reject") {
|
||||
userCorrection.corrected = {
|
||||
displayName: item.displayName,
|
||||
canonicalIngredientId: item.canonicalIngredientId ?? null,
|
||||
brand: item.brand ?? null,
|
||||
quantity: item.quantity,
|
||||
unit: item.unit,
|
||||
bestBeforeDate: item.bestBeforeDate ?? null,
|
||||
useByDate: item.useByDate ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
await app.db.insert(schema.aiCorrections).values({
|
||||
scanJobId: job.id,
|
||||
userId: job.userId,
|
||||
taskType: job.jobType,
|
||||
aiOutput: { tempId, raw: job.result },
|
||||
userCorrection: correction,
|
||||
aiOutput: { raw: job.result },
|
||||
proposal: proposal,
|
||||
userCorrection,
|
||||
imageS3Key: hasImageConsent && job.s3Keys.length > 0 ? job.s3Keys[0] : null,
|
||||
modelVersion: job.modelVersion,
|
||||
promptVersion: job.promptVersion,
|
||||
consentSnapshot: snapshot,
|
||||
});
|
||||
|
||||
await emitEvent(app.db, {
|
||||
type: "AI_CORRECTED",
|
||||
payload: {
|
||||
scanJobId: job.id,
|
||||
taskType: job.jobType,
|
||||
field: String(correction.action ?? "unknown"),
|
||||
field: item.action,
|
||||
},
|
||||
userId: job.userId,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user