Let buyers ask the assistant about a listing

Node · Node · intermediate · modification

Adds the "Ask about this item" endpoint for listing pages on Stallhaus. The new `src/assistant/listingQa.js` holds the assistant: a fixed `SYSTEM_PROMPT` that keeps the model on the listing in front of it and away from promises the seller never made, a `buildMessages` helper that gives the model the listing's title and description together with the buyer's question, and `answerListingQuestion`, which makes one call to the pinned model and wraps provider errors in `AssistantUnavailableError`. `src/routes/listings.js` gets `POST /:id/questions`: it reuses the existing id parsing and published-listing lookup, validates the question (1 to 1,000 characters, not blank) before any model call, and maps `AssistantUnavailableError` to a 502. `src/assistant/listingQa.test.js` runs `answerListingQuestion` against a stubbed client (the request it sends, a `null` content, a provider error).

Node 22, Express 4.21, CommonJS JavaScript; `openai` 7.47.0 (Node SDK, chat completions). Existing, unchanged: `src/app.js` uses `express.json({ limit: '16kb' })`, mounts `app.use('/api/listings', listingsRouter)` behind the app-wide per-IP rate limiter, and ends with an error handler that answers body-parser errors with their 4xx status and anything else with `500 { error: 'internal' }` after logging it. `src/openai.js` exports the shared `openai` client (`new OpenAI()`, key from `OPENAI_API_KEY`, the SDK's default retries); `src/logger.js` exports the app's pino `logger`. `src/db/listings.js` exports `listings.findPublished(id)`, which resolves to `{ id, title, description, priceCents, currency }` for a published listing and `null` for an unknown or unpublished id. Any registered seller writes and edits their listings' title (up to 120 characters) and description (up to 5,000 characters) in the seller dashboard, and edits go live without review. Tests run with `node --test`. The listing page's "Ask about this item" box ships separately.

Requirements

Files touched

Review this PR

Node practice