File size: 9,063 Bytes
4aec5bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | #!/usr/bin/env bash
set -euo pipefail
echo "π AI-Assisted Development Environment Setup"
echo "=============================================="
# Detect OS
OS="unknown"
case "$(uname -s)" in
Linux*) OS="linux";;
Darwin*) OS="macos";;
CYGWIN*|MINGW*|MSYS*) OS="windows";;
esac
echo "Detected OS: $OS"
# --- 1. Check Python ---
echo ""
echo "π¦ Checking Python..."
if ! command -v python3 &> /dev/null; then
echo "β Python 3 not found. Please install Python 3.11+."
exit 1
fi
PYTHON_VERSION=$(python3 --version | grep -oE '[0-9]+\.[0-9]+')
echo "β Python $PYTHON_VERSION found"
# --- 2. Create virtual environment ---
echo ""
echo "πΏ Setting up virtual environment..."
if [ ! -d ".venv" ]; then
python3 -m venv .venv
echo "β Created .venv/"
else
echo "β .venv/ already exists"
fi
source .venv/bin/activate
# --- 3. Install Python dependencies ---
echo ""
echo "π₯ Installing Python dependencies..."
pip install --upgrade pip
# Core
pip install fastapi uvicorn pydantic pydantic-settings sqlalchemy pytest pytest-asyncio \
pytest-mock pytest-cov black ruff mypy httpx factory-boy
# AI Agent libraries (optional but recommended)
pip install smolagents openai
echo "β Core dependencies installed"
# --- 4. Check Ollama ---
echo ""
echo "π¦ Checking Ollama..."
if command -v ollama &> /dev/null; then
echo "β Ollama found"
# Check if gemma4:4b is available
if ollama list | grep -q "gemma4"; then
echo "β Gemma 4 model available"
else
echo "β Gemma 4 not found. Pull it with: ollama pull gemma4:4b"
echo " (Optional: ollama pull gemma4:9b for stronger reasoning)"
fi
else
echo "β Ollama not found. Install from https://ollama.com"
echo " After install, run: ollama pull gemma4:4b"
fi
# --- 5. Check Node.js (for MCP servers) ---
echo ""
echo "β¬’ Checking Node.js..."
if command -v node &> /dev/null; then
NODE_VERSION=$(node --version)
echo "β Node.js $NODE_VERSION found"
else
echo "β Node.js not found. Install for MCP server support."
fi
# --- 6. Check Git ---
echo ""
echo "π Checking Git..."
if [ -d ".git" ]; then
echo "β Git repository initialized"
else
git init
echo "β Initialized git repository"
fi
# --- 7. Install pre-commit hooks ---
echo ""
echo "πͺ Setting up pre-commit..."
pip install pre-commit
pre-commit install || true
# --- 8. Create .env file ---
echo ""
echo "π Creating .env template..."
if [ ! -f ".env" ]; then
cat > .env << 'EOF'
# Environment Configuration
# Copy this to .env and fill in real values
APP_NAME="MyApp"
DEBUG=true
DATABASE_URL="sqlite:///./app.db"
SECRET_KEY="change-me-in-production"
ALGORITHM="HS256"
ACCESS_TOKEN_EXPIRE_MINUTES=30
# Hugging Face (optional, for HF model fallback)
HF_TOKEN=""
# Ollama (local)
OLLAMA_BASE_URL="http://localhost:11434"
OLLAMA_MODEL="gemma4:4b"
EOF
echo "β Created .env (edit with your values)"
else
echo "β .env already exists"
fi
# --- 9. Initialize project structure ---
echo ""
echo "π Initializing project structure..."
touch src/__init__.py
touch src/core/__init__.py
touch src/api/__init__.py
touch src/services/__init__.py
touch src/utils/__init__.py
touch tests/__init__.py
touch tests/unit/__init__.py
touch tests/integration/__init__.py
echo "β Structure initialized"
# --- 10. Create initial files ---
echo ""
echo "π Creating starter files..."
# core/config.py
cat > src/core/config.py << 'EOF'
"""Application configuration using pydantic-settings."""
from functools import lru_cache
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
"""Application settings loaded from environment variables."""
app_name: str = "MyApp"
debug: bool = False
database_url: str = "sqlite:///./app.db"
secret_key: str = "change-me"
algorithm: str = "HS256"
access_token_expire_minutes: int = 30
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
@lru_cache
def get_settings() -> Settings:
"""Return cached Settings instance."""
return Settings()
EOF
# core/models.py
cat > src/core/models.py << 'EOF'
"""SQLAlchemy ORM models."""
from sqlalchemy import Column, Integer, String, DateTime, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import func
Base = declarative_base()
class BaseModel:
"""Base model with timestamps."""
id = Column(Integer, primary_key=True, index=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
EOF
# core/database.py
cat > src/core/database.py << 'EOF'
"""Database connection and session management."""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from .config import get_settings
settings = get_settings()
engine = create_engine(
settings.database_url,
connect_args={"check_same_thread": False} if "sqlite" in settings.database_url else {},
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db() -> Session:
"""Yield a database session. Use as FastAPI dependency."""
db = SessionLocal()
try:
yield db
finally:
db.close()
EOF
# utils/exceptions.py
cat > src/utils/exceptions.py << 'EOF'
"""Custom application exceptions."""
class AppException(Exception):
"""Base application exception."""
def __init__(self, message: str, status_code: int = 500) -> None:
self.message = message
self.status_code = status_code
super().__init__(message)
class ValidationError(AppException):
"""Input validation failed."""
def __init__(self, message: str) -> None:
super().__init__(message, status_code=400)
class NotFoundError(AppException):
"""Resource not found."""
def __init__(self, message: str) -> None:
super().__init__(message, status_code=404)
class AuthenticationError(AppException):
"""Authentication failed."""
def __init__(self, message: str = "Authentication failed") -> None:
super().__init__(message, status_code=401)
EOF
# utils/logger.py
cat > src/utils/logger.py << 'EOF'
"""Structured logging configuration."""
import logging
import sys
from typing import Any
def get_logger(name: str) -> logging.Logger:
"""Return a configured logger."""
logger = logging.getLogger(name)
if not logger.handlers:
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(
"%(asctime)s | %(name)s | %(levelname)s | %(message)s"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
EOF
# main.py
cat > src/main.py << 'EOF'
"""FastAPI application entry point."""
from fastapi import FastAPI
from contextlib import asynccontextmanager
from .core.config import get_settings
from .utils.logger import get_logger
logger = get_logger(__name__)
settings = get_settings()
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events."""
logger.info(f"Starting {settings.app_name}")
yield
logger.info(f"Shutting down {settings.app_name}")
app = FastAPI(
title=settings.app_name,
debug=settings.debug,
lifespan=lifespan,
)
@app.get("/health")
async def health_check() -> dict[str, str]:
"""Health check endpoint."""
return {"status": "ok", "app": settings.app_name}
EOF
# tests/conftest.py
cat > tests/conftest.py << 'EOF'
"""pytest fixtures shared across all tests."""
import pytest
from fastapi.testclient import TestClient
from src.main import app
@pytest.fixture
def client() -> TestClient:
"""Return a FastAPI test client."""
return TestClient(app)
@pytest.fixture
def sample_user() -> dict:
"""Return sample user data."""
return {
"email": "test@example.com",
"password": "securepassword123",
}
EOF
# tests/unit/test_main.py
cat > tests/unit/test_main.py << 'EOF'
"""Tests for main application."""
from fastapi.testclient import TestClient
from src.main import app
class TestHealthCheck:
"""Test health check endpoint."""
def test_health_returns_ok(self, client: TestClient) -> None:
"""Health endpoint should return status ok."""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
EOF
echo "β Starter files created"
# --- 11. Final instructions ---
echo ""
echo "β
Setup complete!"
echo ""
echo "Next steps:"
echo " 1. Edit .env with your configuration"
echo " 2. Edit docs/PRD.md with your project requirements"
echo " 3. Start Ollama: ./scripts/start-ollama.sh"
echo " 4. Activate venv: source .venv/bin/activate"
echo " 5. Start coding with AI assistance!"
echo ""
echo "Agent commands:"
echo " python agents/smolagent_runner.py 'your task here'"
echo " python agents/multiagent_workflow.py 'your project here'"
|