- Rewrite receipt OCR prompt: completes truncated names, preserves fat% and flavour attributes, extracts weight/volume from line, infers typical package sizes for solid goods with quantity_confidence field - Add quantity_confidence to RecognizedItem, EnrichedItem, and ProductJobResultItem; propagate through item enricher and worker - Replace per-item create loop with single POST /user-products/batch call from RecognitionConfirmScreen - Rebuild RecognitionConfirmScreen: amber qty border for low quantity_confidence, tappable product name → catalog picker, sort items by confidence, full L10n (no hardcoded strings) - Add timestamps (HH:mm / d MMM HH:mm) to recent scan chips - Show close-app hint on ProductJobWatchScreen (queued + processing) - Refresh recentProductJobsProvider on watch screen init so new job appears without a manual pull-to-refresh - App-level WidgetsBindingObserver refreshes product and dish job lists on resume, fixing stale lists after background/foreground transitions - Add 9 new L10n keys across all 12 locales Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
100 lines
3.4 KiB
Go
100 lines
3.4 KiB
Go
package recognition
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
|
|
"github.com/food-ai/backend/internal/adapters/ai"
|
|
"github.com/food-ai/backend/internal/domain/product"
|
|
)
|
|
|
|
// itemEnricher matches recognized items against the product catalog,
|
|
// triggering AI classification for unknown items.
|
|
// Extracted from Handler so both the HTTP handler and the product worker pool can use it.
|
|
type itemEnricher struct {
|
|
recognizer Recognizer
|
|
productRepo ProductRepository
|
|
}
|
|
|
|
func newItemEnricher(recognizer Recognizer, productRepo ProductRepository) *itemEnricher {
|
|
return &itemEnricher{recognizer: recognizer, productRepo: productRepo}
|
|
}
|
|
|
|
// enrich matches each recognized item against the product catalog.
|
|
// Items without a match trigger a classification call and upsert into the DB.
|
|
func (enricher *itemEnricher) enrich(enrichContext context.Context, items []ai.RecognizedItem) []EnrichedItem {
|
|
result := make([]EnrichedItem, 0, len(items))
|
|
for _, item := range items {
|
|
enriched := EnrichedItem{
|
|
Name: item.Name,
|
|
Quantity: item.Quantity,
|
|
Unit: item.Unit,
|
|
Category: item.Category,
|
|
Confidence: item.Confidence,
|
|
QuantityConfidence: item.QuantityConfidence,
|
|
StorageDays: 7, // sensible default
|
|
}
|
|
|
|
catalogProduct, matchError := enricher.productRepo.FuzzyMatch(enrichContext, item.Name)
|
|
if matchError != nil {
|
|
slog.WarnContext(enrichContext, "fuzzy match product", "name", item.Name, "err", matchError)
|
|
}
|
|
|
|
if catalogProduct != nil {
|
|
productID := catalogProduct.ID
|
|
enriched.MappingID = &productID
|
|
if catalogProduct.DefaultUnit != nil {
|
|
enriched.Unit = *catalogProduct.DefaultUnit
|
|
}
|
|
if catalogProduct.StorageDays != nil {
|
|
enriched.StorageDays = *catalogProduct.StorageDays
|
|
}
|
|
if catalogProduct.Category != nil {
|
|
enriched.Category = *catalogProduct.Category
|
|
}
|
|
} else {
|
|
classification, classifyError := enricher.recognizer.ClassifyIngredient(enrichContext, item.Name)
|
|
if classifyError != nil {
|
|
slog.WarnContext(enrichContext, "classify unknown product", "name", item.Name, "err", classifyError)
|
|
} else {
|
|
saved := enricher.saveClassification(enrichContext, classification)
|
|
if saved != nil {
|
|
savedID := saved.ID
|
|
enriched.MappingID = &savedID
|
|
}
|
|
enriched.Category = classification.Category
|
|
enriched.Unit = classification.DefaultUnit
|
|
enriched.StorageDays = classification.StorageDays
|
|
}
|
|
}
|
|
result = append(result, enriched)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// saveClassification upserts an AI-produced classification into the product catalog.
|
|
func (enricher *itemEnricher) saveClassification(enrichContext context.Context, classification *ai.IngredientClassification) *product.Product {
|
|
if classification == nil || classification.CanonicalName == "" {
|
|
return nil
|
|
}
|
|
|
|
catalogProduct := &product.Product{
|
|
CanonicalName: classification.CanonicalName,
|
|
Category: strPtr(classification.Category),
|
|
DefaultUnit: strPtr(classification.DefaultUnit),
|
|
CaloriesPer100g: classification.CaloriesPer100g,
|
|
ProteinPer100g: classification.ProteinPer100g,
|
|
FatPer100g: classification.FatPer100g,
|
|
CarbsPer100g: classification.CarbsPer100g,
|
|
StorageDays: intPtr(classification.StorageDays),
|
|
}
|
|
|
|
saved, upsertError := enricher.productRepo.Upsert(enrichContext, catalogProduct)
|
|
if upsertError != nil {
|
|
slog.WarnContext(enrichContext, "upsert classified product", "name", classification.CanonicalName, "err", upsertError)
|
|
return nil
|
|
}
|
|
|
|
return saved
|
|
}
|