Skip to content

AVCodz/codecraft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

97 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CodeCraft AI Documentation

Welcome to the CodeCraft AI documentation. This folder contains comprehensive technical documentation for the project architecture.

πŸ“š Core Architecture Documentation

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

🎯 Quick Start Guide

For New Developers

  1. Start with State Management to understand data flow
  2. Read Authentication to understand user sessions
  3. Review Tool Calling to understand AI features

For Contributing Features

  • Adding a new store? β†’ See State Management patterns
  • Adding a new tool? β†’ See Tool Calling executors
  • Modifying auth? β†’ See Authentication flows

For Debugging

  • State not updating? β†’ State Management troubleshooting
  • AI not executing tools? β†’ Tool Calling debugging section
  • User logged out randomly? β†’ Authentication troubleshooting

πŸ“‚ Additional Documentation

Other Files in This Directory:

  • idea.md - Original project concept and feature ideas
  • database.md - Database schema and Appwrite collections
  • MULTI_PROJECT_CACHE.md - Multi-project caching strategy
  • PREVIEW_OPTIMIZATIONS.md - WebContainer preview performance
  • PREVIEW_PERFORMANCE_GUIDE.md - Preview optimization guide
  • OPENROUTER_STREAMING_FIX.md - OpenRouter streaming implementation

Subdirectories:

  • auth/ - Authentication architecture diagrams
  • LocalDB and appwrite/ - Database sync architecture
  • Realtime architecture/ - WebSocket realtime documentation
  • tool calling/ - Tool calling architecture diagrams

πŸ—οΈ System Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         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                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”„ Data Flow Summary

Read Operations (Fast)

LocalDB (1ms) β†’ Zustand β†’ UI
    ↓ (Background)
Appwrite sync β†’ Update LocalDB β†’ Update Zustand β†’ UI refresh

Write Operations (Reliable)

User Action β†’ Appwrite API β†’ Zustand update β†’ UI refresh
                           β†’ LocalDB cache update
                           β†’ Realtime broadcast to other clients

Realtime Updates (Collaborative)

Other Client β†’ Appwrite β†’ WebSocket β†’ 
    β†’ Zustand update β†’ UI refresh
    β†’ LocalDB cache update

πŸ› οΈ Technology Stack

Frontend

  • React 18 - UI framework
  • Next.js 15 - App framework with Turbopack
  • TypeScript - Type safety
  • Tailwind CSS - Styling
  • Zustand - State management
  • Monaco Editor - Code editor

Backend/Services

  • Appwrite - Database, Auth, Realtime
  • OpenRouter - AI API (Gemini 2.0 Flash)
  • WebContainer - Browser-based Node.js runtime

Development

  • ESLint - Linting
  • TypeScript - Type checking
  • Turbopack - Fast bundling

πŸ“– Code Documentation

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

🀝 Contributing

When contributing to this project:

  1. Read relevant architecture docs first
  2. Follow existing patterns (see code comments)
  3. Update documentation if changing architecture
  4. Add header comments to new files
  5. Test thoroughly (especially realtime sync)

πŸ› Common Issues & Solutions

Issue: State not syncing

Solution: Check STATE_MANAGEMENT.md β†’ Troubleshooting section

Issue: AI not executing tools correctly

Solution: Check TOOL_CALLING_FLOW.md β†’ Debugging section

Issue: User logged out on refresh

Solution: Check AUTHENTICATION_FLOW.md β†’ Troubleshooting section

Issue: Realtime updates not working

Solution: Check STATE_MANAGEMENT.md β†’ Realtime Service Architecture


πŸ“ž Support

For questions or issues:

  1. Check relevant documentation above
  2. Search existing issues in repository
  3. Review code comments in related files
  4. Create detailed issue with reproduction steps

πŸ”— Quick Links


Last Updated: January 2025
Documentation Version: 1.0.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •