A local nutrition tracking app powered by AI
YuHeng implements a sophisticated client-side queue for image recognition tasks. This allows users to upload multiple photos in quick succession and handle recognition results asychronously.
The recognition system is split between a persistent client-side store and a stateless backend processing API.
lib/recognition-store.ts)localStorage (yuheng_recognition_queue). This ensures that pending analysis tasks survive page reloads or accidental tab closures.useSyncExternalStore for high-performance, reactive UI updates./api/nutrition/tasks/[id] endpoint for every non-terminal task./add.POST /api/nutrition/recognize, gets a taskId, saves the Base64 image + taskId to the store, and immediately redirects the user to the Home page.RecognitionStore starts polling. The UI on the Home page (RecognitionQueue component) displays the task’s progress.completed, and a “View Results” button appears.A key refinement of the recognition system is the Guided Retry feature, which allows users to improve AI accuracy through natural language feedback.
if (userPrompt) {
promptText += `\n\nUSER ADDITIONAL INSTRUCTIONS: ${userPrompt}\nPlease prioritize these instructions while maintaining the overall output format.`;
}
recognition_tasks table)While images are stored on the client, the task metadata is tracked in PostgreSQL to support server-side asynchronous processing.
| Column | Type | Description |
|---|---|---|
id |
UUID | Unique task identifier |
status |
STRING | pending, processing, completed, failed |
result |
JSON | The recognized list of dishes |
error |
STRING | Error message if analysis failed |
created_at |
TIMESTAMP | Task creation time |
updated_at |
TIMESTAMP | Last status update |