AICEO / app /routes /pipeline.py
mgbam's picture
Update app/routes/pipeline.py
82f0c2f verified
raw
history blame contribute delete
1.92 kB
# app/routes/pipeline.py
import os
import requests
from fastapi import APIRouter, Depends, Query
from datetime import datetime
from agent_manager import AgentManager
from memory.database import init_db, log_action
from app.dependencies import require_api_key
router = APIRouter(
prefix="/pipeline",
tags=["Pipeline"],
dependencies=[Depends(require_api_key)]
)
# Webhook URL for Zapier; set this as ZAPIER_WEBHOOK in your HF Secrets
WEBHOOK_URL = os.getenv("ZAPIER_WEBHOOK")
@router.on_event("startup")
def startup_db():
"""Initialize the SQLite database on startup."""
init_db()
@router.get("/run", summary="Run full multi‑agent pipeline")
def run_pipeline(
niche: str = Query("fitness", description="Business niche"),
business_type: str = Query("dropshipping", description="Type of business")
):
"""
Executes Strategy, Copy, Ads & Email agents in sequence,
logs each result to the database, fires a Zapier webhook,
and returns a combined JSON summary.
"""
# 1. Run all agents
manager = AgentManager(niche, business_type)
summary = manager.run_all()
# 2. Log each agent's output
for agent_name, result in summary.items():
log_action(agent_name, "pipeline_run", result)
# 3. Build the payload (include a test email for Zapier mapping)
payload = {
"niche": niche,
"business_type": business_type,
"email": "[email protected]", # replace with real user email later
"results": summary,
"timestamp": datetime.utcnow().isoformat()
}
# 4. Fire the webhook (non‑blocking)
if WEBHOOK_URL:
try:
requests.post(WEBHOOK_URL, json=payload, timeout=2)
except Exception:
# Silently ignore any webhook failures
pass
# 5. Return the pipeline summary
return {
"status": "pipeline_executed",
**payload
}