Welcome to the CodeCraft AI documentation. This folder contains comprehensive technical documentation for the project architecture.
How Zustand + LocalDB + Appwrite Realtime + UI work together
Learn about our three-tier state management system that provides:
- Instant UI updates via Zustand
- Offline capability via LocalDB (browser localStorage)
- Real-time collaboration via Appwrite WebSocket
- Reliable persistence across all layers
Key Topics:
- The three tiers: Zustand, LocalDB, Appwrite
- Data flow patterns (Initial load, CRUD operations, Realtime sync)
- Per-project state isolation
- Sync metadata tracking
- Common patterns and troubleshooting
Read this if you want to understand:
- How data flows from database to UI
- How offline mode works
- How real-time updates are handled
- How to add new stores or modify existing ones
How AI tool calling works from user message to final response
Complete guide to our AI-powered code generation system using OpenRouter's manual tool calling loop.
Key Topics:
- Complete flow: User β API β OpenRouter β Tool Loop β Response
- Tool calling loop (max 50 iterations)
- Tool executors (create_file, update_file, etc.)
- Conversation examples
- Data sync after tool execution
- Error handling and debugging
Read this if you want to understand:
- How AI generates and modifies files
- How the tool calling loop prevents infinite loops
- How tool results are fed back to the AI
- How to add new tools or modify existing ones
- How streaming responses work
How authentication, cookies, and session management work
Detailed explanation of our multi-layer authentication system using Appwrite + Cookies + LocalDB + Zustand.
Key Topics:
- Three-layer session persistence
- User registration flow
- User login flow
- App initialization (session restoration)
- Protected route access
- User logout flow
- Cookie configuration and security
Read this if you want to understand:
- How sessions are persisted across page refreshes
- How the cookie backup system works
- How AuthGuard protects routes
- How to modify authentication behavior
- Security considerations
- Start with State Management to understand data flow
- Read Authentication to understand user sessions
- Review Tool Calling to understand AI features
- Adding a new store? β See State Management patterns
- Adding a new tool? β See Tool Calling executors
- Modifying auth? β See Authentication flows
- State not updating? β State Management troubleshooting
- AI not executing tools? β Tool Calling debugging section
- User logged out randomly? β Authentication troubleshooting
idea.md- Original project concept and feature ideasdatabase.md- Database schema and Appwrite collectionsMULTI_PROJECT_CACHE.md- Multi-project caching strategyPREVIEW_OPTIMIZATIONS.md- WebContainer preview performancePREVIEW_PERFORMANCE_GUIDE.md- Preview optimization guideOPENROUTER_STREAMING_FIX.md- OpenRouter streaming implementation
auth/- Authentication architecture diagramsLocalDB and appwrite/- Database sync architectureRealtime architecture/- WebSocket realtime documentationtool calling/- Tool calling architecture diagrams
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β USER β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β REACT UI LAYER β
β Components subscribe to Zustand stores β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ZUSTAND STATE MANAGEMENT β
β In-memory state with React subscriptions β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β LOCALDB (Browser localStorage Cache) β
β Instant load + offline capability β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β APPWRITE (Remote Database + Auth + Realtime) β
β Source of truth + WebSocket collaboration β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
LocalDB (1ms) β Zustand β UI
β (Background)
Appwrite sync β Update LocalDB β Update Zustand β UI refresh
User Action β Appwrite API β Zustand update β UI refresh
β LocalDB cache update
β Realtime broadcast to other clients
Other Client β Appwrite β WebSocket β
β Zustand update β UI refresh
β LocalDB cache update
- React 18 - UI framework
- Next.js 15 - App framework with Turbopack
- TypeScript - Type safety
- Tailwind CSS - Styling
- Zustand - State management
- Monaco Editor - Code editor
- Appwrite - Database, Auth, Realtime
- OpenRouter - AI API (Gemini 2.0 Flash)
- WebContainer - Browser-based Node.js runtime
- ESLint - Linting
- TypeScript - Type checking
- Turbopack - Fast bundling
All component and library files include header comments:
/**
* ComponentName - Brief description
* Detailed purpose explanation
* Features: Key capabilities listed
* Used in: Where this is used in the app
*/Documented Folders:
- β
src/components/auth/- Authentication components - β
src/components/chat/- Chat interface components - β
src/components/editor/- Code editor components - β
src/components/preview/- Preview components - β
src/components/project/- Project management - β
src/components/terminal/- Terminal emulator - β
src/components/ui/- UI primitives - β
src/lib/appwrite/- Appwrite services - β
src/lib/stores/- Zustand stores - β
src/lib/utils/- Utility functions - β
src/lib/ai/- AI services - β
src/lib/types/- TypeScript types
When contributing to this project:
- Read relevant architecture docs first
- Follow existing patterns (see code comments)
- Update documentation if changing architecture
- Add header comments to new files
- Test thoroughly (especially realtime sync)
Solution: Check STATE_MANAGEMENT.md β Troubleshooting section
Solution: Check TOOL_CALLING_FLOW.md β Debugging section
Solution: Check AUTHENTICATION_FLOW.md β Troubleshooting section
Solution: Check STATE_MANAGEMENT.md β Realtime Service Architecture
For questions or issues:
- Check relevant documentation above
- Search existing issues in repository
- Review code comments in related files
- Create detailed issue with reproduction steps
Last Updated: January 2025
Documentation Version: 1.0.0