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
- `POST /api/listings/:id/questions` takes `{ "question": string }` and answers `200 { "answer": string }`, an answer about that one listing based on its title and description. `question` must be a string that is not only whitespace, with a JavaScript `length` of 1 to 1,000; anything else (missing, another type, blank, longer) answers `400 { "error": "invalid_question" }` without calling the model. The checks run in this order: the id format (404 below), then the question (400), then the listing lookup (404), so an unknown listing with an invalid question answers 400.
- A listing id that is not a positive integer of up to 10 digits, an unknown id, or a listing that is not published answers `404 { "error": "not_found" }`, exactly like `GET /api/listings/:id`, which this PR leaves unchanged.
- The system prompt is the module constant `SYSTEM_PROMPT` and is sent unchanged in every model call. The listing's title and description are written by the seller and the question by the buyer: both are untrusted content and enter the call only as `user` message content, never as part of the `system` message.
- One chat-completions call per question to the dated snapshot `gpt-4.1-mini-2025-04-14`. A provider failure (any error from the SDK call) answers `502 { "error": "assistant_unavailable" }` and is logged as a warning; a `null` message content is returned as `""`. A listing-store failure goes to the app's error handler (`next(err)`), as on `GET /api/listings/:id`.
Files touched
- src/routes/listings.js
- src/assistant/listingQa.js
- src/assistant/listingQa.test.js