v0.3 step 5: lean shopping list — claude on-demand foods + game strip
Two changes:
1. foods catalog grows organically. Switch the canonical seed from the
noisy USDA dump (2462 rows of "'s, classic chicken noodle soup")
to the Sonnet-curated cut (229 clean rows). search_food() is now
exact + case-insensitive — Mealie's parser already canonicalizes
food names household-side, so cauldron just needs to look them up
verbatim. On miss, the /list view calls forge.fetch_food_info() to
ask Sonnet for {density_g_per_ml, default_unit_class, common_size_g,
category}, persists the row with source='claude', and the household's
actual kitchen catalog builds itself out as Abby uses it.
Killer case verified end-to-end: "2 cups + 50g + 1.25 lb rice"
collapses to a single "2.25 lb rice" line on the shopping list once
rice has a density row.
2. Game system stripped from /plan. Scoreboard panel, streak banner,
"first to lock takes the week" / "🏆 you locked this one in" copy
all gone. award_pick_points calls in /api/plan/generate +
/api/plan/regenerate stopped firing. household_scoreboard /
household_streak DB methods kept as dead code; cauldron_pick_points
table left in place — non-destructive, easy to revive later if
gamification comes back. Goal: get the base flow (pick → plan →
list) working for Abby first, layer features on after.
This commit is contained in:
parent
36aba73f66
commit
d649b99aef
6 changed files with 2444 additions and 100 deletions
2063
cauldron/data/foods_seed.json
Normal file
2063
cauldron/data/foods_seed.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,20 +1,22 @@
|
|||
"""Foods catalog — canonical food rows + the seed loader.
|
||||
|
||||
Phase A step 1 (v0.3): seed cauldron_foods from USDA SR Legacy via the
|
||||
JSON file at cauldron/data/foods_seed_usda.json. Idempotent — running
|
||||
multiple times is fine, INSERT IGNORE on the unique canonical_name key.
|
||||
Seed = cauldron/data/foods_seed.json (Sonnet-curated, ~229 clean rows
|
||||
with proper densities and unit classes). The raw USDA dump still lives
|
||||
at foods_seed_usda.json as a reference; we don't load it directly.
|
||||
|
||||
Phase A step 2 (next commit): aggregator engine reads these rows + the
|
||||
per-household cauldron_food_mapping to group recipe ingredients.
|
||||
|
||||
Phase A step 3 (later): claude-curated cleanup of the USDA seed (better
|
||||
names, missing common foods, count-based foods like 'egg' / 'onion').
|
||||
Lookup is exact + case-insensitive. Mealie's parser already
|
||||
canonicalizes food names household-side via its own alias system, so
|
||||
the food.name we get from Mealie is consistent across recipes. When a
|
||||
Mealie food name has no match in cauldron_foods, server.py's
|
||||
ensure_food() calls clawdforge to fetch density+unit_class+common_size_g
|
||||
for that exact name, persists it with source='claude', and the
|
||||
household's catalog grows organically.
|
||||
"""
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
SEED_PATH = Path(__file__).parent / "data" / "foods_seed_usda.json"
|
||||
SEED_PATH = Path(__file__).parent / "data" / "foods_seed.json"
|
||||
|
||||
|
||||
def seed_count(db) -> int:
|
||||
|
|
@ -53,13 +55,15 @@ def _load_seed_file(db, path: Path) -> int:
|
|||
"""
|
||||
INSERT IGNORE INTO cauldron_foods
|
||||
(canonical_name, category, density_g_per_ml,
|
||||
default_unit_class, usda_fdc_id, usda_description, source)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, 'usda')
|
||||
common_size_g, default_unit_class, usda_fdc_id,
|
||||
usda_description, source)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, 'usda')
|
||||
""",
|
||||
(
|
||||
entry["canonical_name"][:255],
|
||||
entry.get("category"),
|
||||
entry.get("density_g_per_ml"),
|
||||
entry.get("common_size_g"),
|
||||
entry.get("default_unit_class") or "mass",
|
||||
entry.get("usda_fdc_id"),
|
||||
(entry.get("usda_description") or "")[:500],
|
||||
|
|
@ -72,25 +76,65 @@ def _load_seed_file(db, path: Path) -> int:
|
|||
return inserted
|
||||
|
||||
|
||||
def search_food(db, name: str, *, limit: int = 5) -> list[dict]:
|
||||
"""Best-effort canonical food lookup by name (used by aggregator + UI)."""
|
||||
def search_food(db, name: str, *, limit: int = 1) -> list[dict]:
|
||||
"""Case-insensitive exact match. Returns [] on miss.
|
||||
|
||||
`limit` kept for backwards-compat with callers; only ever returns 0 or 1.
|
||||
"""
|
||||
name_clean = (name or "").strip().lower()
|
||||
if not name_clean:
|
||||
return []
|
||||
with db.conn() as c, c.cursor() as cur:
|
||||
cur.execute(
|
||||
"""SELECT id, canonical_name, category, density_g_per_ml,
|
||||
default_unit_class, common_size_g, source
|
||||
FROM cauldron_foods
|
||||
WHERE LOWER(canonical_name) = %s
|
||||
LIMIT 1""",
|
||||
(name_clean,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return [dict(row)] if row else []
|
||||
|
||||
|
||||
def upsert_claude_food(
|
||||
db,
|
||||
*,
|
||||
canonical_name: str,
|
||||
density_g_per_ml: float | None,
|
||||
default_unit_class: str,
|
||||
common_size_g: float | None,
|
||||
category: str | None = None,
|
||||
) -> dict:
|
||||
"""Insert (or update if a row already exists) a canonical food row from
|
||||
a clawdforge response. Returns the row as a dict. Idempotent."""
|
||||
name = canonical_name.strip().lower()[:255]
|
||||
cls = (default_unit_class or "mass").lower()
|
||||
if cls not in ("mass", "volume", "count", "mixed"):
|
||||
cls = "mass"
|
||||
with db.conn() as c, c.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, canonical_name, category, density_g_per_ml,
|
||||
default_unit_class, common_size_g
|
||||
FROM cauldron_foods
|
||||
WHERE canonical_name LIKE %s OR canonical_name LIKE %s
|
||||
ORDER BY
|
||||
(CASE WHEN canonical_name = %s THEN 0
|
||||
WHEN canonical_name LIKE %s THEN 1
|
||||
ELSE 2 END),
|
||||
CHAR_LENGTH(canonical_name)
|
||||
LIMIT %s
|
||||
INSERT INTO cauldron_foods
|
||||
(canonical_name, category, density_g_per_ml,
|
||||
common_size_g, default_unit_class, source)
|
||||
VALUES (%s, %s, %s, %s, %s, 'claude')
|
||||
ON DUPLICATE KEY UPDATE
|
||||
category=COALESCE(VALUES(category), category),
|
||||
density_g_per_ml=COALESCE(VALUES(density_g_per_ml), density_g_per_ml),
|
||||
common_size_g=COALESCE(VALUES(common_size_g), common_size_g),
|
||||
default_unit_class=VALUES(default_unit_class),
|
||||
source='claude'
|
||||
""",
|
||||
(f"{name}%", f"%{name}%", name, f"{name}%", limit),
|
||||
(name, category, density_g_per_ml, common_size_g, cls),
|
||||
)
|
||||
return [dict(r) for r in cur.fetchall()]
|
||||
cur.execute(
|
||||
"""SELECT id, canonical_name, category, density_g_per_ml,
|
||||
default_unit_class, common_size_g, source
|
||||
FROM cauldron_foods WHERE LOWER(canonical_name)=%s LIMIT 1""",
|
||||
(name,),
|
||||
)
|
||||
return dict(cur.fetchone())
|
||||
|
||||
|
||||
def get_food(db, food_id: int) -> dict | None:
|
||||
|
|
|
|||
|
|
@ -215,6 +215,73 @@ class Forge:
|
|||
)
|
||||
|
||||
|
||||
def fetch_food_info(self, name: str, *, model: str | None = None) -> dict:
|
||||
"""Ask Sonnet for density + unit class + common size of a single
|
||||
food. Returns a dict shaped like:
|
||||
|
||||
{"density_g_per_ml": 1.04 | null,
|
||||
"default_unit_class": "mass"|"volume"|"count",
|
||||
"common_size_g": 150.0 | null,
|
||||
"category": "produce"|"dairy"|... | null}
|
||||
|
||||
density_g_per_ml is null when the food doesn't sensibly convert
|
||||
between mass and volume (e.g., whole onions, eggs — these are
|
||||
count-style). common_size_g lets the aggregator handle "1 onion"
|
||||
as a count → mass conversion. Cheap call, cached forever once
|
||||
persisted to cauldron_foods.
|
||||
"""
|
||||
prompt = (
|
||||
f"Give nutritional/cooking metadata for the food: {name!r}.\n\n"
|
||||
"Output JSON ONLY, no prose: "
|
||||
'{"density_g_per_ml": float|null, '
|
||||
'"default_unit_class": "mass"|"volume"|"count", '
|
||||
'"common_size_g": float|null, '
|
||||
'"category": "produce"|"dairy"|"meat"|"grain"|"baking"|"pantry"'
|
||||
'|"spice"|"oil"|"beverage"|"other"|null}\n\n'
|
||||
"Rules:\n"
|
||||
"- density_g_per_ml: typical packed/cooking density. Null if "
|
||||
"the food is count-based (whole onions, eggs).\n"
|
||||
"- default_unit_class: how this food is most often measured "
|
||||
"(salt=mass; milk=volume; egg=count).\n"
|
||||
"- common_size_g: the typical mass of one whole unit (1 onion "
|
||||
"≈ 150g; 1 egg ≈ 50g). Null if the food isn't naturally counted.\n"
|
||||
"- category: best single fit; null if uncertain.\n"
|
||||
)
|
||||
result = self.run(prompt, model=model or "sonnet", timeout_secs=60)
|
||||
return _extract_food_info(result)
|
||||
|
||||
|
||||
def _extract_food_info(forge_result: dict) -> dict:
|
||||
"""Normalize clawdforge wrapper → food info dict. Defensive on shapes."""
|
||||
if not isinstance(forge_result, dict):
|
||||
raise ForgeError("forge result not a dict")
|
||||
inner = forge_result.get("result", forge_result)
|
||||
if isinstance(inner, str):
|
||||
inner = _parse_json_blob(inner)
|
||||
if not isinstance(inner, dict):
|
||||
raise ForgeError(f"forge result not a dict: {str(inner)[:200]}")
|
||||
|
||||
cls = (inner.get("default_unit_class") or "mass").strip().lower()
|
||||
if cls not in ("mass", "volume", "count", "mixed"):
|
||||
cls = "mass"
|
||||
|
||||
def _f(v):
|
||||
if v is None:
|
||||
return None
|
||||
try:
|
||||
x = float(v)
|
||||
return x if x > 0 else None
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
return {
|
||||
"density_g_per_ml": _f(inner.get("density_g_per_ml")),
|
||||
"default_unit_class": cls,
|
||||
"common_size_g": _f(inner.get("common_size_g")),
|
||||
"category": (inner.get("category") or None) and str(inner["category"])[:64],
|
||||
}
|
||||
|
||||
|
||||
def _extract_plan_slots(forge_result: dict):
|
||||
"""clawdforge wraps its return; the JSON we asked for can sit in a few
|
||||
different shapes. Normalize aggressively."""
|
||||
|
|
|
|||
|
|
@ -501,8 +501,6 @@ def create_app() -> Flask:
|
|||
|
||||
plan = db.get_or_create_plan(hid, this_monday)
|
||||
db.enrich_plan_with_slots(plan)
|
||||
scoreboard = db.household_scoreboard(hid)
|
||||
streak = db.household_streak(hid)
|
||||
pick_count = len(db.list_household_pick_slugs(hid))
|
||||
|
||||
# Resolve display names for any subs we render — locked_by + every
|
||||
|
|
@ -523,8 +521,6 @@ def create_app() -> Flask:
|
|||
locked_by_display=locked_by_display,
|
||||
generated_by_display=generated_by_display,
|
||||
sub_display=sub_display,
|
||||
scoreboard=scoreboard,
|
||||
streak=streak,
|
||||
current_user_sub=u["sub"],
|
||||
pick_count=pick_count,
|
||||
active="plan",
|
||||
|
|
@ -611,15 +607,6 @@ def create_app() -> Flask:
|
|||
"plan": _plan_payload(plan),
|
||||
}), 409
|
||||
|
||||
# Award 1pt per pick that landed (one row per (sub, pick_used) per
|
||||
# plan, deduped client-side: a pick with two pickers gives both 1pt).
|
||||
for s in slots:
|
||||
for sub in s.get("picker_subs") or []:
|
||||
try:
|
||||
db.award_pick_points(hid, plan["id"], sub, 1, "pick_used")
|
||||
except Exception as exc:
|
||||
app.logger.warning("award_pick_points failed for %s: %s", sub, exc)
|
||||
|
||||
plan = db.mark_plan_generated(plan["id"], u["sub"])
|
||||
db.enrich_plan_with_slots(plan)
|
||||
return jsonify({"ok": True, "plan": _plan_payload(plan)})
|
||||
|
|
@ -678,12 +665,6 @@ def create_app() -> Flask:
|
|||
return jsonify({"error": "forge_failed", "detail": str(e)}), 502
|
||||
|
||||
db.save_plan_slots(plan["id"], slots)
|
||||
for s in slots:
|
||||
for sub in s.get("picker_subs") or []:
|
||||
try:
|
||||
db.award_pick_points(hid, plan["id"], sub, 1, "pick_used")
|
||||
except Exception as exc:
|
||||
app.logger.warning("award_pick_points failed for %s: %s", sub, exc)
|
||||
plan = db.mark_plan_generated(plan["id"], u["sub"])
|
||||
db.enrich_plan_with_slots(plan)
|
||||
return jsonify({"ok": True, "plan": _plan_payload(plan)})
|
||||
|
|
@ -739,9 +720,39 @@ def create_app() -> Flask:
|
|||
original_text=ri.get("display") or _ing_render(qty, unit, food_name, note),
|
||||
))
|
||||
|
||||
# foods_lookup: exact match → clawdforge fallback that persists.
|
||||
# Per-request cache so the same food in multiple recipes isn't
|
||||
# re-queried within one /list render.
|
||||
lookup_cache: dict[str, dict | None] = {}
|
||||
|
||||
def foods_lookup(name: str):
|
||||
hits = foods.search_food(db, name, limit=1)
|
||||
return hits[0] if hits else None
|
||||
key = (name or "").strip().lower()
|
||||
if not key:
|
||||
return None
|
||||
if key in lookup_cache:
|
||||
return lookup_cache[key]
|
||||
hits = foods.search_food(db, key, limit=1)
|
||||
if hits:
|
||||
lookup_cache[key] = hits[0]
|
||||
return hits[0]
|
||||
# Miss — ask clawdforge, persist, return. On any failure, cache
|
||||
# None for this request so we don't spam the model.
|
||||
try:
|
||||
info = forge.fetch_food_info(key)
|
||||
row = foods.upsert_claude_food(
|
||||
db,
|
||||
canonical_name=key,
|
||||
density_g_per_ml=info.get("density_g_per_ml"),
|
||||
default_unit_class=info.get("default_unit_class") or "mass",
|
||||
common_size_g=info.get("common_size_g"),
|
||||
category=info.get("category"),
|
||||
)
|
||||
lookup_cache[key] = row
|
||||
return row
|
||||
except Exception as exc:
|
||||
app.logger.warning("forge.fetch_food_info(%r) failed: %s", key, exc)
|
||||
lookup_cache[key] = None
|
||||
return None
|
||||
|
||||
lines = aggregator.aggregate(raw_ings, foods_lookup)
|
||||
return render_template(
|
||||
|
|
|
|||
|
|
@ -93,9 +93,9 @@
|
|||
· {{ plan.locked_at.strftime('%a %-I:%M %p') }}
|
||||
{% elif plan.slots %}
|
||||
generated{% if generated_by_display %} by <span style="color: var(--green-bright);">{{ generated_by_display }}</span>{% endif %}.
|
||||
lock when ready — first to lock takes the week.
|
||||
lock it in when the household has agreed.
|
||||
{% else %}
|
||||
{{ pick_count }} pinned in the pool. summon the planner to build a 7-day plan, then race to lock.
|
||||
{{ pick_count }} pinned in the pool. summon the planner to build a 7-day plan.
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -115,15 +115,12 @@
|
|||
|
||||
{% if plan.locked_at %}
|
||||
<p>this week's plan is locked. it'll archive Sunday night and a fresh week opens Monday.</p>
|
||||
{% if plan.locked_reason == 'user' and plan.locked_by_sub == current_user_sub %}
|
||||
<p class="muted">🏆 you locked this one in.</p>
|
||||
{% endif %}
|
||||
{% elif not plan.slots %}
|
||||
<p>no plan yet. summon claude to build one from the {{ pick_count }} pinned pick{{ '' if pick_count == 1 else 's' }} + the rest of the grimoire.</p>
|
||||
<button class="gen-cta" type="button" id="gen-btn" onclick="generatePlan(this)">🪄 generate this week's plan</button>
|
||||
<div class="gen-meta" id="gen-meta">sonnet · ~30s</div>
|
||||
{% else %}
|
||||
<p>plan's set. lock it in to claim the week — first to lock wins.</p>
|
||||
<p>plan's set.</p>
|
||||
<div class="btn-row">
|
||||
<button class="btn btn-purple" type="button" onclick="lockPlan(this)">🔒 lock this week</button>
|
||||
{% if plan.generated_by_sub == current_user_sub %}
|
||||
|
|
@ -159,53 +156,6 @@
|
|||
</section>
|
||||
{% endif %}
|
||||
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>scoreboard</h2>
|
||||
<span class="ctx">picks landed · weeks won</span>
|
||||
</div>
|
||||
|
||||
{% if streak and streak.count >= 2 %}
|
||||
<p style="font-size: 1.05em; margin: .2em 0 1em 0;">
|
||||
🔥 <strong style="color: var(--green-bright);">{{ streak.display_name }}</strong>
|
||||
on a <strong>{{ streak.count }}-week</strong> run.
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
{% if scoreboard and scoreboard|selectattr("points")|list or scoreboard and scoreboard|selectattr("wins")|list %}
|
||||
<table style="width: 100%; border-collapse: collapse; font-size: .95em;">
|
||||
<thead>
|
||||
<tr style="border-bottom: 1px solid var(--line);">
|
||||
<th style="text-align: left; color: var(--purple); font-size: 11px; letter-spacing: .15em; text-transform: uppercase; font-family: var(--mono); font-weight: 600; padding: 6px 0;">member</th>
|
||||
<th style="text-align: right; color: var(--purple); font-size: 11px; letter-spacing: .15em; text-transform: uppercase; font-family: var(--mono); font-weight: 600; padding: 6px 0;">pts</th>
|
||||
<th style="text-align: right; color: var(--purple); font-size: 11px; letter-spacing: .15em; text-transform: uppercase; font-family: var(--mono); font-weight: 600; padding: 6px 0;">wins</th>
|
||||
<th style="text-align: right; color: var(--purple); font-size: 11px; letter-spacing: .15em; text-transform: uppercase; font-family: var(--mono); font-weight: 600; padding: 6px 0;">last</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for s in scoreboard %}
|
||||
<tr style="border-bottom: 1px solid var(--line-soft);">
|
||||
<td style="padding: 8px 0;">
|
||||
{{ loop.index }}.
|
||||
<strong style="color: {% if loop.first and (s.points or s.wins) %}var(--green-bright){% else %}var(--bone){% endif %};">
|
||||
{{ s.display_name or s.email.split('@')[0] }}
|
||||
</strong>
|
||||
{% if s.sub == current_user_sub %}<span class="muted">· you</span>{% endif %}
|
||||
</td>
|
||||
<td style="text-align: right; padding: 8px 0; font-family: var(--mono); color: var(--bone);">{{ s.points or 0 }}</td>
|
||||
<td style="text-align: right; padding: 8px 0; font-family: var(--mono); color: var(--bone);">{{ s.wins or 0 }}</td>
|
||||
<td style="text-align: right; padding: 8px 0; font-family: var(--mono); color: var(--muted); font-size: .85em;">
|
||||
{{ s.last_win.strftime('%b %-d') if s.last_win else '—' }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="muted">nobody's locked or generated yet. be the first.</p>
|
||||
{% endif %}
|
||||
</section>
|
||||
|
||||
<script>
|
||||
async function lockPlan(btn) {
|
||||
btn.disabled = true; btn.textContent = '…';
|
||||
|
|
@ -243,7 +193,7 @@ async function generatePlan(btn) {
|
|||
}
|
||||
|
||||
async function rerollPlan(btn) {
|
||||
if (!confirm('re-roll the week? slots + points reset.')) return;
|
||||
if (!confirm('re-roll the week? slots will be replaced.')) return;
|
||||
btn.disabled = true; btn.textContent = '…';
|
||||
try {
|
||||
const r = await fetch('/api/plan/regenerate', { method: 'POST' });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue