Backend (FastAPI + SQLAlchemy + Claude API + RAG): - Config management with Pydantic v2 - Database engine with connection pooling and SQL injection prevention - AI engine with Claude API integration (support custom base URL) - RAG engine with ChromaDB and sentence-transformers - Requirement analysis service - Config generation service - Executor engine with SQL validation - REST API endpoints: /analyze, /generate, /execute Frontend (Vue 3 + Element Plus + Pinia): - Complete 3-step workflow: analyze → generate → execute - Step indicator with progress visualization - Analysis result display with field table - SQL preview with monospace font - Execute confirmation dialog with safety warning - Execution result display - State management with Pinia - API service integration Security: - SQL injection prevention with parameterized queries - Dangerous SQL operation blocking - Database password URL encoding - Transaction auto-rollback - Pydantic config validation Features: - Natural language requirement analysis - Automated SQL configuration generation - Safe execution with human review - LAN access support - Custom Claude API endpoint support Documentation: - README with quick start guide - Quick start guide - LAN access configuration - Dependency fixes guide - Claude API configuration - Git operation guide - Implementation report Dependencies fixed: - numpy<2.0.0 for chromadb compatibility - sentence-transformers==2.7.0 for huggingface_hub compatibility Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
"""Generate API endpoint for configuration generation.
|
|
|
|
This module provides the /generate endpoint for generating SQL configuration.
|
|
"""
|
|
|
|
from fastapi import APIRouter, HTTPException, status
|
|
from loguru import logger
|
|
|
|
from app.models.request import GenerateRequest
|
|
from app.models.response import GenerateResponse, ErrorResponse
|
|
from app.services.config_service import ConfigService
|
|
from app.api.execute import store_session_sql # Import SQL storage function
|
|
|
|
# Create router
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post(
|
|
"/generate",
|
|
response_model=GenerateResponse,
|
|
responses={
|
|
400: {"model": ErrorResponse, "description": "Invalid request"},
|
|
500: {"model": ErrorResponse, "description": "Internal server error"}
|
|
},
|
|
summary="Generate SQL configuration",
|
|
description="Generate SQL configuration based on structured requirements"
|
|
)
|
|
async def generate_config(request: GenerateRequest) -> GenerateResponse:
|
|
"""Generate SQL configuration based on structured requirements.
|
|
|
|
This endpoint takes structured requirements from the analysis phase
|
|
and generates SQL configuration statements using Claude AI.
|
|
|
|
Args:
|
|
request: GenerateRequest with session_id and requirements
|
|
|
|
Returns:
|
|
GenerateResponse with session_id, status, and generated config
|
|
|
|
Raises:
|
|
HTTPException: 400 for invalid input, 500 for processing errors
|
|
"""
|
|
try:
|
|
# Validate requirements
|
|
if not request.requirements:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail={
|
|
"code": "EMPTY_REQUIREMENTS",
|
|
"message": "requirements cannot be empty",
|
|
"session_id": request.session_id
|
|
}
|
|
)
|
|
|
|
logger.info(f"[{request.session_id}] Processing generate request")
|
|
|
|
# Create service and generate config
|
|
service = ConfigService()
|
|
result = await service.generate(
|
|
requirements=request.requirements,
|
|
session_id=request.session_id
|
|
)
|
|
|
|
# Store generated SQL for later execution
|
|
if result and result.get("配置方案") and result["配置方案"].get("sql_list"):
|
|
sql_list = result["配置方案"]["sql_list"]
|
|
store_session_sql(request.session_id, sql_list)
|
|
logger.info(f"[{request.session_id}] Stored {len(sql_list)} SQL statements for execution")
|
|
|
|
logger.success(f"[{request.session_id}] Config generation completed")
|
|
|
|
return GenerateResponse(
|
|
session_id=request.session_id,
|
|
status="success",
|
|
data=result
|
|
)
|
|
|
|
except HTTPException:
|
|
# Re-raise HTTP exceptions
|
|
raise
|
|
|
|
except ValueError as e:
|
|
logger.error(f"[{request.session_id}] Validation error: {e}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail={
|
|
"code": "VALIDATION_ERROR",
|
|
"message": str(e),
|
|
"session_id": request.session_id
|
|
}
|
|
)
|
|
|
|
except Exception as e:
|
|
logger.error(f"[{request.session_id}] Config generation failed: {e}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail={
|
|
"code": "GENERATION_FAILED",
|
|
"message": f"Failed to generate config: {str(e)}",
|
|
"session_id": request.session_id
|
|
}
|
|
) |