Skip to content

Add lookup_barcode tool - #75

Closed
paultristanwagner wants to merge 2 commits into
rwestergren:mainfrom
paultristanwagner:barcode-lookup
Closed

paultristanwagner wants to merge 2 commits into
rwestergren:mainfrom
paultristanwagner:barcode-lookup

Conversation

@paultristanwagner

Copy link
Copy Markdown
Contributor

All code in this PR was written by an AI agent (Claude Opus 5.5) working with me; I directed it and tested the results on my own account. Feel free to take, change or drop any part of it.

Part of #73.

Adds a lookup_barcode tool: find foods by the barcode on their
package, the same lookup the app's scanner does. Barcodes can already be
attached to custom foods (#69); this is the read side.

The scanner calls POST /api/v2/barcode:

{"query": "4006381333931", "auth": {…}, "config": {"call_version": 2}}

A known code answers {"foodId": <first match>, "foods": [<full food objects, as get_food returns them>]}; an unknown one answers
{"result": "SUCCESS"} with no foods. Verified against the live API:

  • the server ignores leading zeros: UPC-A 049000050103 and
    0049000050103 both find foods stored as "49000050103"
  • it doesn't validate the code: 0000000000000 returns 19 unrelated
    restaurant foods whose stored barcode is "0"

Changes:

  • client: lookup_barcode(barcode) validates the code with
    normalize_barcodes (so a misread digit fails the check digit instead of
    matching nothing), refuses an all-zero code, and returns only the foods
    whose barcodes actually contain it (compared without leading zeros).
  • server: lookup_barcode tool, returning each food's id, name,
    source, default measure, measures and barcodes, ready for
    add_food_entry.
  • README: tool table row.

https://claude.ai/code/session_01MCJFDo71qoSqTRtcGfkpXJ

The app's scanner looks foods up with /api/v2/barcode, which returns full
food objects. The server matches loosely (leading zeros ignored, and an
all-zero code returns foods stored with the placeholder barcode "0"), so
the code is validated and only foods that carry it are returned.

Claude-Session: https://claude.ai/code/session_01MCJFDo71qoSqTRtcGfkpXJ
@paultristanwagner paultristanwagner mentioned this pull request Oct 16, 2026
8 of 9 tasks
@rwestergren

Copy link
Copy Markdown
Owner

I'm curious how agents are supposed to use this one - are you taking a picture and the agent is scanning it? Typing the barcode to the agent?

@jorgebaptista

Copy link
Copy Markdown
Contributor

On the question above, from real use: the agent reads the barcode from a photo. I log food from ChatGPT by sending a photo of the package, and the model reads the digits printed under the bars. For regional supermarket own-brand products, search_foods often returns unrelated results or several near-duplicates. Today that happened with two Portuguese store-brand items. A barcode lookup would give the exact food_id directly. The check-digit validation here matters for that path: a misread digit is rejected instead of matching the wrong food. I'm running this PR locally on top of v0.2.7, together with #93 and #76.

@rwestergren

Copy link
Copy Markdown
Owner

On the question above, from real use: the agent reads the barcode from a photo. I log food from ChatGPT by sending a photo of the package, and the model reads the digits printed under the bars. For regional supermarket own-brand products, search_foods often returns unrelated results or several near-duplicates. Today that happened with two Portuguese store-brand items. A barcode lookup would give the exact food_id directly. The check-digit validation here matters for that path: a misread digit is rejected instead of matching the wrong food. I'm running this PR locally on top of v0.2.7, together with #93 and #76.

Why aren't you just using the barcode scanner in the Cronometer for this use-case? I don't understand the need to pass a photo through an LLM.

@jorgebaptista

Copy link
Copy Markdown
Contributor

Fair question. The scanner covers logging one item yourself in the app. My case is logging done in the conversation: I send ChatGPT photos of a meal and its packages in one message, and it estimates portions, splits ingredients, logs them and records its assumptions, without me opening Cronometer. When a barcode is visible in one of those photos, lookup_barcode gives the exact product instead of a fuzzy search; otherwise the agent falls back to search_foods. Totally understand if it doesn't fit the project's direction, though. Thanks for considering it either way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants