@@ -10,6 +10,11 @@ All commands should be run from the project root directory:
1010 - **Build**: `npm run build` - Builds the production-ready Next.js application
1111 - **Start Production**: `npm run start` - Starts the production server (requires build first)
1212 - **Lint**: `npm run lint` - Runs ESLint to check code quality
13+- **Format**: `npm run format` - Formats all files with Prettier
14+- **Format Check**: `npm run format:check` - Checks code formatting without making changes
15+- **DB Generate**: `npm run db:generate` - Generates database migrations using Drizzle Kit
16+- **DB Migrate**: `npm run db:migrate` - Runs database migrations
17+- **DB Studio**: `npm run db:studio` - Opens Drizzle Studio for database management
1318
1419 ## Dependencies
1520
@@ -25,6 +30,8 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
2530 - **ollama**: ^0.6.3 - Official Ollama npm package for model interaction
2631 - **autoprefixer**: ^10.4.17 - CSS post-processor
2732 - **postcss**: ^8.4.30 - CSS transformation tool
33+- **drizzle-orm**: ^0.45.1 - TypeScript ORM for SQL databases
34+- **better-sqlite3**: ^12.5.0 - Fast SQLite3 database driver
2835
2936 ### Dev Dependencies
3037
@@ -33,6 +40,10 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
3340 - **@types/react-dom**: ^18.2.19 - TypeScript types for React DOM
3441 - **eslint**: ^8.56.0 - JavaScript/TypeScript linter
3542 - **eslint-config-next**: ^13.5.6 - Next.js ESLint configuration
43+- **prettier**: ^3.2.5 - Code formatter
44+- **prettier-plugin-tailwindcss**: ^0.5.11 - Prettier plugin for sorting Tailwind CSS classes
45+- **drizzle-kit**: ^0.31.8 - Drizzle ORM migration and introspection tool
46+- **@types/better-sqlite3**: ^7.6.13 - TypeScript types for better-sqlite3
3647
3748 ## Code Style Guidelines
3849
@@ -84,8 +95,10 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
8495 - Set default text colors on topmost container to cascade: `text-slate-800 dark:text-slate-200`
8596
8697 7. **Code Formatting**:
98+ - Use Prettier for code formatting: `npm run format` to format all files
99+ - Prettier configuration is in `.prettierrc` (2 spaces, semicolons, double quotes, 100 char width)
100+ - Prettier automatically sorts Tailwind CSS classes via `prettier-plugin-tailwindcss`
87101 - Use consistent indentation (2 spaces)
88- - Use single quotes for strings in some contexts, double quotes in others (maintain existing file style)
89102 - Add trailing commas in multi-line objects/arrays
90103 - Use semicolons
91104
@@ -120,8 +133,17 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
120133 │ │ └── icons/
121134 │ │ └── PaperPlaneIcon.tsx # Reusable icon components
122135 │ └── lib/
136+│ ├── db/
137+│ │ ├── index.ts # Database connection and setup
138+│ │ └── schema.ts # Drizzle ORM schema definitions
123139 │ ├── duckduckgo.ts # DuckDuckGo API wrapper
124-│ └── ollama.ts # Ollama API wrapper
140+│ └── ollama.ts # Ollama API wrapper
141+├── drizzle/ # Generated migration files
142+│ ├── meta/ # Migration metadata
143+│ └── *.sql # SQL migration files
144+├── data/ # Database files (created at runtime)
145+│ └── database.db # SQLite database file
146+├── drizzle.config.ts # Drizzle Kit configuration
125147 ├── package.json
126148 ├── tsconfig.json
127149 └── tailwind.config.js
@@ -155,6 +177,22 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
155177 - Interface: `{ title: string; snippet: string; url: string }`
156178 - Located in: `src/lib/duckduckgo.ts`
157179
180+### Database Integration (Drizzle ORM)
181+
182+- **Database**: SQLite using better-sqlite3
183+- **Location**: `data/database.db` (created automatically)
184+- **Schema**: Defined in `src/lib/db/schema.ts`
185+- **Connection**: Exported from `src/lib/db/index.ts` as `db`
186+- **Tables**:
187+ - `threads`: Stores conversation threads with id, title, createdAt, updatedAt
188+ - `messages`: Stores messages with id, threadId, role, content, createdAt
189+- **Relations**: Messages belong to threads (one-to-many)
190+- **Types**: Exported types `Thread`, `NewThread`, `Message`, `NewMessage`
191+- **Configuration**: `drizzle.config.ts` defines schema path and database location
192+- **Migrations**: Generated in `drizzle/` directory using `npm run db:generate`
193+- **Foreign Keys**: Enabled automatically in database connection
194+- **Usage**: Import `db` from `src/lib/db/index.ts` to query the database
195+
158196 ### Chat API Flow
159197
160198 1. Client sends POST request to `/api/chat` with `{ message: string }`
@@ -168,8 +206,9 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
168206 1. Start development server: `npm run dev`
169207 2. Make code changes
170208 3. Test in browser (typically `http://localhost:3000`)
171-4. Run linter: `npm run lint`
172-5. Build for production: `npm run build`
209+4. Format code: `npm run format`
210+5. Run linter: `npm run lint`
211+6. Build for production: `npm run build`
173212
174213 ## Important Constraints
175214