File size: 1,922 Bytes
be60023
 
aef8bee
 
be60023
 
 
 
 
 
 
 
 
 
 
 
82f0c2f
 
aef8bee
be60023
 
82f0c2f
be60023
 
 
 
82f0c2f
 
be60023
82f0c2f
 
 
 
 
 
be60023
 
82f0c2f
 
be60023
 
82f0c2f
 
aef8bee
be60023
 
82f0c2f
be60023
 
 
82f0c2f
 
 
 
 
 
 
 
 
 
aef8bee
 
 
 
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
# 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
    }