Commit Graph
2 Commits
Author SHA1 Message Date
Jorem Juárez c001550e4f Optimize and harden the AI consultant
A) Token optimization:
- Prompt caching: the stable block (system instructions + full menu) is now a
  single cached system text block with cache_control:ephemeral and goes first;
  the variable diner question stays in messages, after the cache breakpoint, so
  the cached prefix is byte-identical between queries of the same restaurant/locale
- Compact context: only name, price, short description (<=120 chars), diet tags
  and allergens; macros are omitted (rarely change a recommendation). Inactive
  dishes still skipped

B) Hardening:
- Validate existence: each recommended dish must match a real active menu name
  (accent/case/space-insensitive, both locales); non-existent dishes are dropped
- Fewer than 3 valid is fine — return what's valid, never pad with non-fitting dishes
- Robust parsing: reuse the import's tolerant parser (extractJson +
  repairTruncatedJson, now shared in lib/json.ts) so truncated/prose replies don't 502
- max_tokens 700 -> 900 and system prompt asks for one-sentence reasons

Refactor: repairTruncatedJson + parseLenientJson moved into lib/json.ts and shared
by import.ts and ai-consult. No behavior change to import.

Verified: build passes; existence filter drops fake dishes, recovers truncated+prose
replies, and returns <3 without padding.
2026-06-15 15:08:56 -06:00
Jorem Juárez 112e95dc28 Add AI smart menu import (Cargar menú)
Import a menu from PDF, photo, Excel/CSV, web link, or pasted text and turn it
into draft sections + products with Claude.

- Product.aiEstimated JSON flags AI-inferred macros/allergens (migration 0002)
- POST /api/[slug]/import: resolves the source (vision via Sonnet for PDF/photo,
  Haiku for text/CSV/XLSX/web), extracts structured JSON for review only
- POST /api/[slug]/import/apply: writes to the draft in add or replace mode,
  preserving aiEstimated flags; replace wipes the draft first
- Editor: 'Cargar menú' button + glass ImportModal (method -> review -> add/replace),
  'Estimado por IA — verifica' badges, verification disclaimer; ProductEditor shows
  the badge and clears the flag when the value is edited
- Reuses anthropic.ts, upload/guard, shared extractJson (moved to lib/json.ts)
- i18n labels (es/en); adds xlsx dependency for spreadsheet parsing

Verified: build passes; migration applies; apply add/replace persist correctly
with aiEstimated; extract validation + auth guard behave as expected.
2026-06-13 20:43:06 -06:00