This file provides guidance to Claude Code and other AI coding agents when working with code in this repository. Keep this file aligned with AGENTS.md and .cursor/rules/.
SPEC.md is the source of truth for this application's architecture.
Before implementing features or making architectural changes:
- Consult
SPEC.mdto ensure alignment with documented product behavior, architecture, database schema, API contracts, AI provider behavior, credit/billing rules, and MCP support. - Identify existing behavior that must be preserved before editing.
- Do not remove or simplify existing features unless the user explicitly asks for that.
- If a change would deviate from the spec, flag it for discussion before editing.
- After implementing significant behavior changes, update
SPEC.mdto keep it current.
Key sections to reference:
- Section 3: Database Schema (before schema changes)
- Section 5: AI Provider Integration (before adding providers)
- Section 7: Credit & Billing System (before modifying billing)
- Section 9: API Reference (before adding/modifying endpoints)
High-risk areas require extra care: authentication, anonymous usage limits, subscriptions and credits, chat persistence, message streaming, model/provider routing, API key handling, MCP tools, file upload/readers, and admin flows.
pnpm dev # Start development server with turbopack
pnpm build # Build production bundle with turbopack
pnpm start # Start production server
pnpm lint # Run ESLintpnpm test # Run Playwright tests (local config)
pnpm test:ui # Run tests with UI
pnpm test:debug # Run tests in debug mode
pnpm test:headed # Run tests in headed mode
pnpm test:anonymous # Run anonymous user testspnpm db:generate # Generate Drizzle schema
pnpm db:migrate # Run database migrations
pnpm db:push # Push schema to database
pnpm db:studio # Open Drizzle Studiopnpm pricing:analysis # Analyze OpenRouter pricing with tsx- Framework: Next.js 15 with App Router and Turbopack
- Database: PostgreSQL with Drizzle ORM
- Authentication: Better Auth with Google OAuth and anonymous users
- Payments: Polar integration for credit system
- AI: Multiple providers (OpenRouter, Anthropic, OpenAI, Groq, X AI, Requesty)
- UI: Tailwind CSS with shadcn/ui components
- Testing: Playwright for E2E testing
- Better Auth handles both Google OAuth and anonymous users
- Anonymous users get 10 messages/day, Google users get 20
- Credit system overrides daily limits for paid users
- User linking from anonymous to authenticated accounts
- Multiple AI providers with unified interface in
ai/providers.ts - Model Context Protocol (MCP) support for external tools
- Dynamic API key management with runtime overrides
- Credit validation prevents negative balance
- Main schema in
lib/db/schema.tswith Drizzle ORM - Better Auth schema integrated with custom user tables
- Chat/message storage with JSON parts for flexibility
- Polar usage events tracking
- Polar integration for billing and subscription management
- Credit-based usage with real-time validation
- Automatic customer creation and management
- Usage tracking for billing accuracy
app/- Next.js App Router pages and API routescomponents/- React components (shadcn/ui based)lib/- Shared utilities, database, and authenticationhooks/- Custom React hooksai/- AI provider configurations and utilities
app/api/auth/- Authentication endpointsapp/api/chat/- Chat functionalityapp/api/credits/- Credit managementapp/api/polar/- Polar webhooks and integration
drizzle/- Database migrations and schema snapshotslib/db/- Database configuration and schema definitions
tests/- Playwright E2E testsscripts/- Development and analysis scripts.cursor/rules/- Cursor IDE workflow rules
lib/auth.ts- Better Auth configuration with Google OAuthauth-schema.ts- Auth schema definitionslib/auth-client.ts- Client-side auth utilities
lib/db/schema.ts- Main database schema with Drizzledrizzle.config.ts- Drizzle configuration
ai/providers.ts- AI provider configurationslib/chat-store.ts- Chat state managementlib/openrouter-utils.ts- OpenRouter utilities
components/chat.tsx- Main chat interfacecomponents/message.tsx- Message displaycomponents/model-picker.tsx- AI model selectioncomponents/settings-sheet.tsx- Unified settings panel with horizontal tabscomponents/settings/- Settings tab components (API Keys, MCP Servers, Provider Health, Preferences)components/mcp-server-manager.tsx- MCP server managementcomponents/api-key-manager.tsx- API key management with embedded mode supportcomponents/api-key-manager.tsx- API key management with embedded mode support
AUTH_SECRET- Better Auth secretPOLAR_ACCESS_TOKEN- Polar API tokenPOLAR_PRODUCT_ID- Polar product IDSUCCESS_URL- Checkout success URLPOLAR_SERVER_ENV- "production" or "sandbox"
- Production:
NEXT_PUBLIC_GOOGLE_CLIENT_ID_PROD,GOOGLE_CLIENT_SECRET_PROD - Development:
NEXT_PUBLIC_GOOGLE_CLIENT_ID_DEV,GOOGLE_CLIENT_SECRET_DEV
Follow .cursor/rules/ workflow:
- Create feature branch using standardized naming
- Use
vercel deployfor preview testing (never--prodon features) - Follow conventional commit messages
- Test thoroughly before merging
- Modify schema in
lib/db/schema.ts - Run
npm run db:generateto create migration - Run
npm run db:migrateto apply changes - Test with
npm run db:studio
- Use Playwright for E2E testing
- Test both anonymous and authenticated user flows
- Test credit system and payment flows
- Run
npm run test:anonymousfor anonymous user tests
- Add provider config to
ai/providers.ts - Update model picker in
components/model-picker.tsx - Test with credit system integration
- Update
lib/db/schema.ts - Generate migration with
npm run db:generate - Review migration in
drizzle/directory - Apply with
npm run db:migrate
- Create in
app/api/following existing patterns - Add authentication checks for protected routes
- Include credit validation for paid features
- Test with both anonymous and authenticated users
- All API keys stored in environment variables
- Credit validation prevents negative balances
- User data protection with anonymous usage support
- Secure authentication with Better Auth
- CORS configuration for trusted origins
- Turbopack for fast development builds
- Database query optimization with Drizzle
- Token usage tracking for cost optimization
- Real-time pricing analysis tools available