A small Next.js app that helps people ask product-building questions while you iterate on a fictional commerce platform (Chica). Each answer is generated with OpenRouter, then graded and reviewed (with at most one automatic revision). Everything is stored in PostgreSQL via Prisma.
- Chat UI at
/chat— matcha + strawberry themed layout, light / dark toggle (stored inlocalStorageasproduct_ai_theme), question in / answer out, status badges (approved, needs revision, pending review, etc.). - Session-scoped threads — the browser keeps one active conversation id in
sessionStorage(product_ai_assistant_conversation_id); New chat starts a fresh conversation. The assistant still answers each question from the same system prompts (no cross-session memory in the model). - Persistence — conversations, messages, and evaluation payloads in Postgres.
- Pipeline — answer model → grader (JSON scores) → reviewer (JSON decision) → optional single revision.
- Evaluator switch — see OpenRouter vs Braintrust below.
- Rate limits — sliding window on
POST /api/ask(in-memory; configurable via env). - Eval dataset — curated Chica questions:
evals/product-assistant-eval-set.json, runnable withnpm run eval:chica.
- Next.js (App Router), TypeScript, Tailwind CSS
- PostgreSQL + Prisma ORM
- OpenRouter for LLM calls
- Optional Braintrust SDK for eval logging
| What | Where |
|---|---|
| Switch mode | .env.local: EVALUATOR_PROVIDER=multi (default), braintrust, langsmith (stub), or single (stub). Restart npm run dev. |
| Factory that reads that env | lib/evaluator.ts — getEvaluator() returns MultiModelEvaluator, BraintrustEvaluator, LangSmithEvaluator (placeholder), or SingleModelEvaluator (placeholder). |
| OpenRouter HTTP calls (answer, grader, reviewer, revision) | lib/openrouter.ts — retries, model fallback lists from OPENROUTER_*_MODELS env vars. |
| Grader + reviewer prompts | lib/prompts.ts |
Braintrust logging (only when braintrust mode + BRAINTRUST_API_KEY set) |
lib/braintrustLog.ts — initLogger + traced span ProductAssistant_pipeline_eval. |
| Pipeline order | lib/pipeline.ts — calls getEvaluator() after the draft answer exists. |
Summary: OpenRouter always powers the LLM calls for this MVP. Braintrust adds observability (logs/scores to your Braintrust project); it does not replace OpenRouter unless you build a different integration later.
- Node.js 20+ recommended
- A PostgreSQL database (e.g. Neon)
- An OpenRouter API key
-
Clone and install
npm install
-
Environment
Copy
.env.exampleto.env.localand fill in at least:DATABASE_URL— Postgres connection stringOPENROUTER_API_KEY— from OpenRouter
Optional:
EVALUATOR_PROVIDER=braintrustandBRAINTRUST_API_KEY— log evals to Braintrust (docs)BRAINTRUST_PROJECT_NAME— defaults toProduct AI AssistantOPENROUTER_*_MODELS— comma-separated model ids (see.env.example)RATE_LIMIT_MAX,RATE_LIMIT_WINDOW_MS, orRATE_LIMIT_DISABLED=1for local stress tests
-
Database schema
From the project root (loads
.env.localviadotenv-cli):npm run db:push
For migration-based workflows:
npm run db:migrate
-
Run locally
npm run dev
| Command | Purpose |
|---|---|
npm run dev |
Development server |
npm run build |
prisma generate + production Next build |
npm run start |
Run production build |
npm run lint |
ESLint |
npm run db:push |
Push Prisma schema to DB (uses .env.local) |
npm run db:migrate |
Create/apply migrations (uses .env.local) |
npm run eval:chica |
POST every Chica eval question to a running app (see below) |
With the dev server already running (npm run dev with valid .env.local):
npm run eval:chicaOptional env:
EVAL_BASE_URL— defaulthttp://localhost:3000EVAL_DELAY_MS— pause between requests (default600)
The script reads evals/product-assistant-eval-set.json and prints each row’s HTTP outcome. Exit code is non-zero if any request fails.
| Method | Path | Description |
|---|---|---|
POST |
/api/ask |
Body: { "question": string, "conversationId"?: uuid } — rate-limited; max question length 8000 chars |
GET |
/api/history |
Query ?conversationId=<uuid> (required for data) — returns at most one conversation and its messages. Missing or invalid id → { "conversations": [] } (avoids listing everyone’s chats). |
GET |
/api/metrics |
Aggregate evaluation scores from the DB |
POST /api/askuses an in-memory sliding window per client IP (x-forwarded-for→ first hop, elsex-real-ip, elseunknown).- Defaults: 15 requests per 60s per IP (
RATE_LIMIT_MAX,RATE_LIMIT_WINDOW_MS). - 429 responses include
Retry-Afterand a JSON body witherror: "rate_limited". - On serverless with many cold instances, each instance has its own counter; for strict global limits use Redis (e.g. Upstash) later.
- Disable for local abuse tests:
RATE_LIMIT_DISABLED=1in.env.local.
GitHub Actions workflow .github/workflows/ci.yml runs npm ci, npm run lint, and npm run build on pushes/PRs to main/master.
app/— pages and API routescomponents/—AppHeader,ThemeToggle,ChatWindow,AskBox,MessageListlib/—openrouter,pipeline,evaluator,prompts,retry,rateLimit,db,braintrustLogscripts/—run-chica-evals.mjsprisma/— schema and migrationstypes/— Zod-backed evaluation shapes + evaluator typesevals/— Chica eval question set (JSON)
- Never commit
.envor.env.local(they are gitignored). - Rotate keys if they were ever pasted into chat, tickets, or screenshots.
GET /api/historyonly returns a conversation when you pass a validconversationId; it does not enumerate all rows. For true multi-tenant isolation, still add auth and server-side checks that the caller may access that id.
- Deploy — Vercel (or similar): set env vars, run migrations against prod DB.
- Auth / sessions — per-user conversations and history.
- Redis-based rate limits — shared limiter across serverless instances.
- LangSmith SDK —
LangSmithEvaluatoris a stub today; fork contributors can replace it with real LangChain/LangSmith calls.
Chica is a fictional classroom platform name used for eval prompts and demos.