mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-09 20:27:25 -04:00
Compare commits
11 Commits
v0.3.0-alp
...
v0.3.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| 71ec53db45 | |||
| b6bd949d23 | |||
| b5f18ac0f3 | |||
| 90b6ad400d | |||
| 6e4b2a4cc5 | |||
| 18bd4d169d | |||
| 8b91c75b32 | |||
| bdb3f6a6a2 | |||
| 3502a7ffa8 | |||
| 68d9f241e1 | |||
| 4fec5826bb |
63
api/main.py
63
api/main.py
@@ -17,6 +17,7 @@ from pathlib import Path
|
|||||||
from fastapi import FastAPI, HTTPException, Query
|
from fastapi import FastAPI, HTTPException, Query
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from pydantic import BaseModel, Field, field_validator
|
from pydantic import BaseModel, Field, field_validator
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from api.job_manager import JobManager
|
from api.job_manager import JobManager
|
||||||
from api.simulation_worker import SimulationWorker
|
from api.simulation_worker import SimulationWorker
|
||||||
@@ -115,7 +116,7 @@ class HealthResponse(BaseModel):
|
|||||||
|
|
||||||
def create_app(
|
def create_app(
|
||||||
db_path: str = "data/jobs.db",
|
db_path: str = "data/jobs.db",
|
||||||
config_path: str = "configs/default_config.json"
|
config_path: str = "/tmp/runtime_config.json" if Path("/tmp/runtime_config.json").exists() else "configs/default_config.json"
|
||||||
) -> FastAPI:
|
) -> FastAPI:
|
||||||
"""
|
"""
|
||||||
Create FastAPI application instance.
|
Create FastAPI application instance.
|
||||||
@@ -127,21 +128,46 @@ def create_app(
|
|||||||
Returns:
|
Returns:
|
||||||
Configured FastAPI app
|
Configured FastAPI app
|
||||||
"""
|
"""
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
"""Initialize database on startup, cleanup on shutdown if needed"""
|
||||||
|
from tools.deployment_config import is_dev_mode, get_db_path
|
||||||
|
from api.database import initialize_dev_database, initialize_database
|
||||||
|
|
||||||
|
# Startup - use closure to access db_path from create_app scope
|
||||||
|
logger.info("🚀 FastAPI application starting...")
|
||||||
|
logger.info("📊 Initializing database...")
|
||||||
|
|
||||||
|
if is_dev_mode():
|
||||||
|
# Initialize dev database (reset unless PRESERVE_DEV_DATA=true)
|
||||||
|
logger.info(" 🔧 DEV mode detected - initializing dev database")
|
||||||
|
dev_db_path = get_db_path(db_path)
|
||||||
|
initialize_dev_database(dev_db_path)
|
||||||
|
log_dev_mode_startup_warning()
|
||||||
|
else:
|
||||||
|
# Ensure production database schema exists
|
||||||
|
logger.info(" 🏭 PROD mode - ensuring database schema exists")
|
||||||
|
initialize_database(db_path)
|
||||||
|
|
||||||
|
logger.info("✅ Database initialized")
|
||||||
|
logger.info("🌐 API server ready to accept requests")
|
||||||
|
|
||||||
|
yield
|
||||||
|
|
||||||
|
# Shutdown (if needed in future)
|
||||||
|
logger.info("🛑 FastAPI application shutting down...")
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="AI-Trader Simulation API",
|
title="AI-Trader Simulation API",
|
||||||
description="REST API for triggering and monitoring AI trading simulations",
|
description="REST API for triggering and monitoring AI trading simulations",
|
||||||
version="1.0.0"
|
version="1.0.0",
|
||||||
|
lifespan=lifespan
|
||||||
)
|
)
|
||||||
|
|
||||||
# Store paths in app state
|
# Store paths in app state
|
||||||
app.state.db_path = db_path
|
app.state.db_path = db_path
|
||||||
app.state.config_path = config_path
|
app.state.config_path = config_path
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
async def startup_event():
|
|
||||||
"""Display DEV mode warning on startup if applicable"""
|
|
||||||
log_dev_mode_startup_warning()
|
|
||||||
|
|
||||||
@app.post("/simulate/trigger", response_model=SimulateTriggerResponse, status_code=200)
|
@app.post("/simulate/trigger", response_model=SimulateTriggerResponse, status_code=200)
|
||||||
async def trigger_simulation(request: SimulateTriggerRequest):
|
async def trigger_simulation(request: SimulateTriggerRequest):
|
||||||
"""
|
"""
|
||||||
@@ -496,11 +522,30 @@ def create_app(
|
|||||||
# Create default app instance
|
# Create default app instance
|
||||||
app = create_app()
|
app = create_app()
|
||||||
|
|
||||||
|
# Ensure database is initialized when module is loaded
|
||||||
|
# This handles cases where lifespan might not be triggered properly
|
||||||
|
logger.info("🔧 Module-level database initialization check...")
|
||||||
|
|
||||||
|
from tools.deployment_config import is_dev_mode, get_db_path
|
||||||
|
from api.database import initialize_dev_database, initialize_database
|
||||||
|
|
||||||
|
_db_path = app.state.db_path
|
||||||
|
|
||||||
|
if is_dev_mode():
|
||||||
|
logger.info(" 🔧 DEV mode - initializing dev database at module load")
|
||||||
|
_dev_db_path = get_db_path(_db_path)
|
||||||
|
initialize_dev_database(_dev_db_path)
|
||||||
|
else:
|
||||||
|
logger.info(" 🏭 PROD mode - ensuring database exists at module load")
|
||||||
|
initialize_database(_db_path)
|
||||||
|
|
||||||
|
logger.info("✅ Module-level database initialization complete")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
# Display DEV mode warning if applicable
|
# Note: Database initialization happens in lifespan AND at module load
|
||||||
log_dev_mode_startup_warning()
|
# for maximum reliability
|
||||||
|
|
||||||
uvicorn.run(app, host="0.0.0.0", port=8080)
|
uvicorn.run(app, host="0.0.0.0", port=8080)
|
||||||
|
|||||||
@@ -6,30 +6,28 @@
|
|||||||
},
|
},
|
||||||
"models": [
|
"models": [
|
||||||
{
|
{
|
||||||
"name": "claude-3.7-sonnet",
|
"name": "claude-sonnet-4-5",
|
||||||
"basemodel": "anthropic/claude-3.7-sonnet",
|
"basemodel": "anthropic/claude-sonnet-4.5",
|
||||||
"signature": "claude-3.7-sonnet",
|
"signature": "claude-sonnet-4.5",
|
||||||
"enabled": false,
|
"enabled": true
|
||||||
"openai_base_url": "Optional: YOUR_OPENAI_BASE_URL,you can write them in .env file",
|
|
||||||
"openai_api_key": "Optional: YOUR_OPENAI_API_KEY,you can write them in .env file"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "deepseek-chat-v3.1",
|
"name": "deepseek-chat-v3.1",
|
||||||
"basemodel": "deepseek/deepseek-chat-v3.1",
|
"basemodel": "deepseek/deepseek-chat-v3.1",
|
||||||
"signature": "deepseek-chat-v3.1",
|
"signature": "deepseek-chat-v3.1",
|
||||||
"enabled": false
|
"enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "qwen3-max",
|
"name": "qwen3-max",
|
||||||
"basemodel": "qwen/qwen3-max",
|
"basemodel": "qwen/qwen3-max",
|
||||||
"signature": "qwen3-max",
|
"signature": "qwen3-max",
|
||||||
"enabled": false
|
"enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "gemini-2.5-flash",
|
"name": "gemini-2.5-flash",
|
||||||
"basemodel": "google/gemini-2.5-flash",
|
"basemodel": "google/gemini-2.5-flash",
|
||||||
"signature": "gemini-2.5-flash",
|
"signature": "gemini-2.5-flash",
|
||||||
"enabled": false
|
"enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "gpt-5",
|
"name": "gpt-5",
|
||||||
|
|||||||
@@ -36,12 +36,7 @@ fi
|
|||||||
|
|
||||||
echo "✅ Environment variables validated"
|
echo "✅ Environment variables validated"
|
||||||
|
|
||||||
# Step 1: Initialize database
|
# Step 1: Merge and validate configuration
|
||||||
echo "📊 Initializing database..."
|
|
||||||
python -c "from api.database import initialize_database; initialize_database('data/jobs.db')"
|
|
||||||
echo "✅ Database initialized"
|
|
||||||
|
|
||||||
# Step 2: Merge and validate configuration
|
|
||||||
echo "🔧 Merging and validating configuration..."
|
echo "🔧 Merging and validating configuration..."
|
||||||
python -c "from tools.config_merger import merge_and_validate; merge_and_validate()" || {
|
python -c "from tools.config_merger import merge_and_validate; merge_and_validate()" || {
|
||||||
echo "❌ Configuration validation failed"
|
echo "❌ Configuration validation failed"
|
||||||
@@ -50,7 +45,7 @@ python -c "from tools.config_merger import merge_and_validate; merge_and_validat
|
|||||||
export CONFIG_PATH=/tmp/runtime_config.json
|
export CONFIG_PATH=/tmp/runtime_config.json
|
||||||
echo "✅ Configuration validated and merged"
|
echo "✅ Configuration validated and merged"
|
||||||
|
|
||||||
# Step 3: Start MCP services in background
|
# Step 2: Start MCP services in background
|
||||||
echo "🔧 Starting MCP services..."
|
echo "🔧 Starting MCP services..."
|
||||||
cd /app
|
cd /app
|
||||||
python agent_tools/start_mcp_services.py &
|
python agent_tools/start_mcp_services.py &
|
||||||
@@ -59,11 +54,11 @@ MCP_PID=$!
|
|||||||
# Setup cleanup trap before starting uvicorn
|
# Setup cleanup trap before starting uvicorn
|
||||||
trap "echo '🛑 Stopping services...'; kill $MCP_PID 2>/dev/null; exit 0" EXIT SIGTERM SIGINT
|
trap "echo '🛑 Stopping services...'; kill $MCP_PID 2>/dev/null; exit 0" EXIT SIGTERM SIGINT
|
||||||
|
|
||||||
# Step 4: Wait for services to initialize
|
# Step 3: Wait for services to initialize
|
||||||
echo "⏳ Waiting for MCP services to start..."
|
echo "⏳ Waiting for MCP services to start..."
|
||||||
sleep 3
|
sleep 3
|
||||||
|
|
||||||
# Step 5: Start FastAPI server with uvicorn (this blocks)
|
# Step 4: Start FastAPI server with uvicorn (this blocks)
|
||||||
# Note: Container always uses port 8080 internally
|
# Note: Container always uses port 8080 internally
|
||||||
# The API_PORT env var only affects the host port mapping in docker-compose.yml
|
# The API_PORT env var only affects the host port mapping in docker-compose.yml
|
||||||
echo "🌐 Starting FastAPI server on port 8080..."
|
echo "🌐 Starting FastAPI server on port 8080..."
|
||||||
|
|||||||
@@ -69,8 +69,16 @@ def get_db_path(base_db_path: str) -> str:
|
|||||||
Example:
|
Example:
|
||||||
PROD: "data/trading.db" -> "data/trading.db"
|
PROD: "data/trading.db" -> "data/trading.db"
|
||||||
DEV: "data/trading.db" -> "data/trading_dev.db"
|
DEV: "data/trading.db" -> "data/trading_dev.db"
|
||||||
|
|
||||||
|
Note:
|
||||||
|
This function is idempotent - calling it multiple times on the same
|
||||||
|
path will not add multiple _dev suffixes.
|
||||||
"""
|
"""
|
||||||
if is_dev_mode():
|
if is_dev_mode():
|
||||||
|
# Check if already has _dev suffix (idempotent)
|
||||||
|
if "_dev.db" in base_db_path:
|
||||||
|
return base_db_path
|
||||||
|
|
||||||
# Insert _dev before .db extension
|
# Insert _dev before .db extension
|
||||||
if base_db_path.endswith(".db"):
|
if base_db_path.endswith(".db"):
|
||||||
return base_db_path[:-3] + "_dev.db"
|
return base_db_path[:-3] + "_dev.db"
|
||||||
|
|||||||
Reference in New Issue
Block a user