# RD Curve AI - Copilot Instructions
You are assisting with "RD Curve AI", a Next.js 16 application for video/image compression researchers to manage datasets and plot Rate-Distortion (RD) curves.
## Tech Stack & Key Libraries
- **Framework:** Next.js 16 (App Router), React 19
- **Database:** PostgreSQL, Drizzle ORM (`lib/schema.ts`, `lib/db.ts`)
- **Auth:** Better-Auth (`lib/auth.ts`, `lib/auth-client.ts`)
- **UI:** Tailwind CSS, shadcn/ui, Recharts (`components/rd-chart.tsx`)
- **Validation:** Zod (`lib/types.ts`)
- **AI:** OpenAI (for data parsing)
## Architecture Overview
### Data Model (`lib/schema.ts`)
The core hierarchy is **Dataset -> Method -> DataPoint**.
- **Dataset:** A collection of methods (e.g., "Kodak Dataset").
- **Method:** A specific algorithm (e.g., "JPEG", "HEVC").
- **DataPoint:** A single result with `bpp` (x-axis) and dynamic `metrics` (y-axis) stored in a `jsonb` column.
- **Metric:** Custom definitions for y-axis values (e.g., "PSNR", "MS-SSIM") with a `mapping_function` string for display transformation.
### Authentication Pattern
- **Server-side:** Use `auth.api.getSession({ headers: await headers() })` in API routes/Server Components.
- **Client-side:** Use `authClient` hooks (e.g., `useSession`).
- **Protection:** Always check `if (!session)` in API routes before sensitive operations.
### API Route Pattern (`app/api/**`)
Follow this pattern for Route Handlers:
1. **Auth Check:** Verify session immediately.
2. **Validation:** Parse request body with Zod schemas from `lib/types.ts`.
3. **DB Operation:** Use Drizzle to query/mutate.
4. **Response:** Return `NextResponse.json`.
## Critical Conventions
### Database & Drizzle
- **Schema:** Define all tables in `lib/schema.ts`.
- **IDs:** Use `uuid` with `.defaultRandom()`.
- **Relations:** Use `drizzle-orm` relations for cleaner queries.
- **JSONB:** Use the `metrics` jsonb column in `data_point` for flexible metric storage.
### Frontend Components
- **UI:** Use `components/ui` (shadcn) for primitives, install new ones with `npx shadcn@latest add <component>`.
- **Charts:** Use `Recharts` in `components/rd-chart.tsx`. Handle the `mapping_function` logic (likely via `lib/safe-eval.ts`) when rendering axes.
- **Icons:** Use `lucide-react`.
- **MCP:** Use `context7` mcp for documentation if needed. Use `shadcn` mcp for UI components. Use `lucide-icons` mcp for searching icons.
### Configuration
- **Env Vars:** Access via `lib/config.ts` (e.g., `CONFIG.OPENAI_API_KEY`), NOT `process.env` directly.
- **Types:** Keep shared Zod schemas and TS interfaces in `lib/types.ts`.
### Language
- All API responses and frontend text must be in English.
## Developer Workflows
- **Migrations:** Run `pnpm db:generate` and `pnpm db:migrate` when changing schema.
- **AI Parsing:** The parsing logic (converting text to JSON) should handle unstructured input and map it to the `data_point` structure.
再次使用AI完成前后端应用——RD Curve AI
再次使用AI完成前后端应用——RD Curve AI
年初的时候有尝试使用大语言模型完成一个前后端项目,详见“记一次使用AI完成一整个前后端项目”。
那时的观点是很难用,即使已经有了现成的其他语言的代码作为参考,依然难以复现,很大程度上还是需要人工参与到代码的编写中(尤其是对一些编译问题和运行时bug)。
但今年 agent 的快速进步也推动了 Vibe Coding 的快速发展,以及我自己对 Next.js 框架的逐渐熟悉,似乎这件事也变得更加简单了。
作为一个压缩领域的研究者,我一直苦恼于每次画新的 RD 曲线图都要从过往的实验数据中找到旧方法的实验结果,并编写 matplotlib 代码绘制。这几天终于忍不住想要搞一个管理相关实验数据并进行可视化的应用——RD Curve AI,也欢迎大家试用并给我一些改进建议。
图表
BD-Rate
数据导入
评价指标
这次 Vibe Coding 实际上仅用了一天时间就完成了,个人认为还是很顺利的,大致流程如下:
最后想要说明的是,Vibe Coding 依然无法帮助小白完成一个较为复杂的系统任务,人在其中虽然不一定要完全读懂 AI 写的代码,但一定要对系统框架有一个较为全面的掌握。这里引用一条暴论:”AI(几乎)不能帮你完成你并不会的任务“。我对 Next.js 框架的熟悉就是这一年自学并在 AI 的帮助下完成一些小项目或者改进一些开源项目中逐渐成长起来的,当有了这些经验积累后,AI 才能变成一个提升效率的优秀工具。