YuHeng (玉衡)

A local nutrition tracking app powered by AI

View the Project on GitHub formaxcn/yuheng

Recognition Queue & Guided Retry

Implementation Status: Completed & Stable

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.

Core Architecture

The recognition system is split between a persistent client-side store and a persistent server-side queue managed by PostgreSQL.

1. Client-Side Store (lib/recognition-store.ts)

2. Server-Side Queue (lib/queue.ts)

3. Recognition Flow

  1. Initiation: User uploads an image on /add or via Home page.
  2. Immediate Redirection: The app calls POST /api/nutrition/recognize, which enqueues a job in pg-boss and returns a taskId.
  3. Background Processing: The QueueManager worker picks up the job, calls the AI provider, and updates the task status in the recognition_tasks table.
  4. Polling: The client-side RecognitionStore polls for completion.
  5. Completion: Once finished, a “View Results” button appears in the UI.

Guided Retry Mechanism

A key refinement of the recognition system is the Guided Retry feature, which allows users to improve AI accuracy through natural language feedback.

How it works

Database Integration (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