Skip to content

Latest commit

 

History

History
325 lines (291 loc) · 14.4 KB

File metadata and controls

325 lines (291 loc) · 14.4 KB

DeepCode Architecture & Data Flow

System Architecture

┌────────────────────────────────────────────────────────────────┐
│                         USER TERMINAL                          │
│                                                                │
│  "build a rest api with python"  ← Your task                 │
│              ↓                                                 │
│  ┌──────────────────────────────────────────────┐            │
│  │      DEEPCODE AGENT (deepcode.py)           │            │
│  │  ✓ Error Detection (NEW)                    │            │
│  │  ✓ Connection Checking (NEW)               │            │
│  │  ✓ Smart Model Selection                   │            │
│  │  ✓ Multi-Agent Collaboration               │            │
│  │  ✓ Code Evolution Tracking                 │            │
│  │  ✓ Smart Context Analysis                  │            │
│  └──────────────────────────────────────────────┘            │
└────────────────────────────────────────────────────────────────┘
                           ↓
              HTTP Requests (localhost:11434)
                           ↓
┌────────────────────────────────────────────────────────────────┐
│                      OLLAMA SERVER                             │
│              (ollama serve - must be running)                  │
│                                                                │
│  Endpoints:                                                    │
│  • GET  /api/tags         ← List models                       │
│  • POST /api/chat         ← Chat interface                    │
│  • POST /api/generate     ← Generate text                     │
│                                                                │
│  Models Loaded:                                               │
│  ⚡ qwen2.5-coder          (Fast model)                        │
│  🚀 deepseek-coder-v2:16b (Powerful model)                   │
└────────────────────────────────────────────────────────────────┘
                           ↓
            AI Model Inference & Response
                           ↓
┌────────────────────────────────────────────────────────────────┐
│                  GENERATED RESULTS                             │
│                                                                │
│  ✓ Code files created                                         │
│  ✓ Files modified                                             │
│  ✓ Commands executed                                          │
│  ✓ Evolution tracked                                          │
└────────────────────────────────────────────────────────────────┘

Request/Response Flow

1. Initial Connection Check (NEW)

DeepCode Startup
    ↓
_check_connection()
    ↓
GET http://localhost:11434/api/tags
    ├─ Status 200 → "✓ Connected to Ollama"
    ├─ Status 404 → "❌ Endpoint not found"
    └─ Connection Error → "❌ Cannot connect to Ollama"

2. Model Selection & Task Processing

User Input: "build a rest api"
    ↓
ModelRouter.select_model()
    ├─ Complexity Score Analysis
    ├─ Keyword Detection
    └─ Choose: Qwen2 (Fast) or DeepSeek (Powerful)
    ↓
process_task()
    ├─ Analyze Project
    ├─ Get Relevant Context
    └─ Start Iteration Loop
    ↓
Iteration 1:
    ├─ call_llm(prompt, selected_model)
    │   ↓
    │   POST http://localhost:11434/api/chat
    │   {
    │     "model": "qwen2.5-coder:latest",
    │     "messages": [...],
    │     "stream": false
    │   }
    │   ↓
    │   ← Response: Model's answer
    ├─ Parse Response
    ├─ Extract ACTION
    ├─ Execute Action (read/write/run)
    └─ Update Context
    ↓
Iteration 2, 3, ... until DONE

3. Error Handling (NEW)

POST Request to Ollama
    ↓
Response Received
    ├─ Status 200
    │   ├─ Parse JSON
    │   ├─ Extract message content
    │   └─ Return to agent
    │
    ├─ Status 404
    │   ├─ Check if model exists
    │   ├─ Suggest: ollama pull [model]
    │   └─ Show /api/tags endpoint
    │
    ├─ Status 500
    │   ├─ Ollama server error
    │   ├─ Suggest: Restart Ollama
    │   └─ Check logs
    │
    ├─ Timeout (>120s)
    │   ├─ Likely slow/hanging response
    │   ├─ Suggest: Restart Ollama
    │   └─ Check system resources
    │
    └─ Connection Error
        ├─ Ollama not running
        ├─ Show: "ollama serve"
        └─ Provide troubleshooting tips

Component Interaction

┌─────────────────────────────────────────────────────────────┐
│                     DEEPCODE AGENT                          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌──────────────┐    ┌──────────────┐    ┌─────────────┐  │
│  │   Router     │    │   Context    │    │  Evolution  │  │
│  │              │    │  Analyzer    │    │  Tracker    │  │
│  │ • Model      │    │              │    │             │  │
│  │   Selection  │    │ • Project    │    │ • Track     │  │
│  │ • Auto       │    │   Analysis   │    │   Changes   │  │
│  │   Switch     │    │ • Smart      │    │ • Diffs     │  │
│  │              │    │   Context    │    │ • Stats     │  │
│  └──────────────┘    └──────────────┘    └─────────────┘  │
│                           ↑                     │           │
│                           │ Metadata            │           │
│                           │                     ↓           │
│  ┌──────────────────────────────────────────────────────┐  │
│  │          Multi-Agent System (Optional)              │  │
│  │                                                      │  │
│  │  • Architect   → Plans design                      │  │
│  │  • Coder       → Implements                        │  │
│  │  • Reviewer    → Reviews code                      │  │
│  │  • Tester      → Tests                             │  │
│  │  • Optimizer   → Optimizes                         │  │
│  └──────────────────────────────────────────────────────┘  │
│                           ↓                                 │
│  ┌──────────────────────────────────────────────────────┐  │
│  │            LLM Interface (FIXED)                    │  │
│  │                                                      │  │
│  │  • Connection Check ✓                              │  │
│  │  • Status Code Handling ✓                          │  │
│  │  • Error Messages ✓                                │  │
│  │  • Timeout Detection ✓                             │  │
│  │  • Troubleshooting Tips ✓                          │  │
│  └──────────────────────────────────────────────────────┘  │
│                           ↓                                 │
│          HTTP POST → Ollama API Gateway                     │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Data Flow: User Request to Result

START
  │
  ├─ Input: "build a rest api with python"
  │
  ├─ ModelRouter determines complexity
  │   └─ "simple" → Uses Qwen2 (fast)
  │
  ├─ Analyze Project Context
  │   ├─ Scan files
  │   ├─ Detect dependencies
  │   └─ Identify tech stack
  │
  ├─ Call Ollama with prompt
  │   ├─ Connection Check ✓ NEW
  │   ├─ Model Validation ✓ NEW
  │   └─ Send request
  │
  ├─ Receive Response from Ollama
  │   ├─ HTTP Status Code Handling ✓ NEW
  │   ├─ Error Detection ✓ NEW
  │   └─ Parse content
  │
  ├─ Parse Response for Actions
  │   ├─ Extract THOUGHT
  │   ├─ Extract ACTION
  │   ├─ Extract INPUT
  │   └─ Identify if DONE
  │
  ├─ Execute Action
  │   ├─ read_file() - Read source code
  │   ├─ write_file() - Write generated code
  │   ├─ run_command() - Execute commands
  │   └─ search_files() - Search codebase
  │
  ├─ Track Evolution ✓ NEW
  │   └─ Record changes with model attribution
  │
  ├─ Loop until action = "done" or max iterations
  │
  └─ END: Return results

Fixed: Error Handling Flow

BEFORE (Generic):
Request → 404 Error → Display "Error: 404" → User Confused ❌

AFTER (Intelligent):
Request → 404 Error → Detect cause → Show specific message → Help user fix ✓

                    ↓
    ┌──────────────────────────────────────┐
    │ Is Ollama running?                  │
    ├──────────────────────────────────────┤
    │ ✓ YES: Check model                  │
    │ ✗ NO: Suggest "ollama serve"       │
    └──────────────────────────────────────┘
                    ↓
    ┌──────────────────────────────────────┐
    │ Is model installed?                 │
    ├──────────────────────────────────────┤
    │ ✓ YES: Check API endpoint           │
    │ ✗ NO: Suggest "ollama pull model"  │
    └──────────────────────────────────────┘
                    ↓
    ┌──────────────────────────────────────┐
    │ Is endpoint correct?                │
    ├──────────────────────────────────────┤
    │ ✓ YES: Try again                    │
    │ ✗ NO: Show /api/tags endpoint       │
    └──────────────────────────────────────┘
                    ↓
            Show Troubleshooting Tips

File Structure

DeepCode/
│
├── deepcode.py              Main agent (ENHANCED ✓)
│   ├── ModelRouter
│   ├── MultiAgentSystem
│   ├── CodeEvolutionTracker
│   ├── SmartContextAnalyzer
│   └── DeepCodeAgent
│       ├── _check_connection() [NEW]
│       ├── _check_models() [IMPROVED]
│       ├── call_llm() [IMPROVED]
│       └── process_task() [IMPROVED]
│
├── README.md                Setup & usage guide [NEW]
├── TROUBLESHOOTING.md       Fix common issues [NEW]
├── QUICK_START.md          Visual guide [NEW]
├── FIXES.md               What was fixed [NEW]
├── requirements.txt        Dependencies [NEW]
└── setup.bat              Windows setup helper [NEW]

Key Improvements Summary

Component Before After
Error Messages Generic Specific & actionable
Connection Check None Automatic at startup
Status Codes Ignored Properly handled
User Guidance None Built-in troubleshooting
Documentation None Comprehensive guides
Setup Helper Manual Automated script

Connection Verification

# Test connection manually
curl http://localhost:11434/api/tags

# Expected response:
{
  "models": [
    {
      "name": "qwen2.5-coder:latest",
      ...
    }
  ]
}

# If it fails, run:
ollama serve

This architecture ensures reliability, clarity, and user-friendly error handling for your DeepCode REST API agent! 🚀