File size: 7,604 Bytes
09da05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""REST API server for the Application layer.

This module is intentionally not imported from `adsp.app.__init__` so that the
core package remains usable even when FastAPI/uvicorn are not installed.
"""

from __future__ import annotations

from dataclasses import dataclass
import os
from pathlib import Path
import threading
import time
from typing import Any, Callable, Optional

from loguru import logger

from adsp.app.api.routes import (
    register_auth_routes,
    register_chat_routes,
    register_group_routes,
    register_ingestion_routes,
    register_persona_routes,
    register_report_routes,
    register_survey_routes,
    register_system_routes,
)
from adsp.app.auth_service import AuthService
from adsp.app.ingestion_service import IngestionService
from adsp.app.qa_service import QAService
from adsp.app.report_service import ReportService
from adsp.config import PROCESSED_DATA_DIR


def _require_fastapi():
    try:
        from fastapi import FastAPI  # noqa: F401
    except ImportError as exc:  # pragma: no cover - import guard
        raise ImportError(
            "FastAPI is required to run the REST API server. "
            "Install with `pip install fastapi uvicorn` (or `make install`)."
        ) from exc


class LazyQAService:
    """Lazily build the QA stack so API startup can return quickly."""

    def __init__(self, factory: Optional[Callable[[], QAService]] = None) -> None:
        self._factory = factory or QAService
        self._instance: Optional[QAService] = None
        self._error: Optional[Exception] = None
        self._lock = threading.Lock()
        self._status = "idle"

    @property
    def status(self) -> str:
        return self._status

    def warmup_async(self) -> None:
        if self._instance is not None or self._error is not None or self._status == "warming":
            return
        self._status = "warming"
        logger.info("API startup step 4/5: starting QA warmup thread")
        thread = threading.Thread(target=self._warmup, name="qa-service-warmup", daemon=True)
        thread.start()

    def _warmup(self) -> None:
        logger.info("QA warmup step 1/2: building QA service")
        try:
            self._get_instance()
        except Exception:
            logger.exception("QA service warmup failed")

    def _get_instance(self) -> QAService:
        instance = self._instance
        if instance is not None:
            return instance

        with self._lock:
            if self._instance is not None:
                return self._instance
            if self._error is not None:
                raise RuntimeError("QA service initialization failed") from self._error

            start = time.perf_counter()
            try:
                instance = self._factory()
            except Exception as exc:
                self._error = exc
                self._status = "error"
                raise RuntimeError("QA service initialization failed") from exc

            self._instance = instance
            self._status = "ready"
            logger.info(
                "QA warmup step 2/2: QA service ready in {:.2f}s",
                time.perf_counter() - start,
            )
            return instance

    def get_instance(self) -> QAService:
        return self._get_instance()

    def __getattr__(self, name: str) -> Any:
        return getattr(self._get_instance(), name)


@dataclass
class AppServices:
    auth: AuthService
    qa: Any
    ingestion: IngestionService
    reports: ReportService
    surveys: Any
    groups: Any
    responses: Any


def create_app() -> Any:
    """Create and configure the FastAPI application."""

    _require_fastapi()
    from fastapi import Depends, FastAPI, Header, HTTPException

    title = os.environ.get("ADSP_API_TITLE", "Lavazza AI Personas API")
    version = os.environ.get("ADSP_API_VERSION", "0.1.0")
    debug = os.environ.get("ADSP_API_DEBUG", "false").strip().lower() in {"1", "true", "yes", "on"}
    description = (
        "REST API for the Lavazza AI Personas application layer.\n\n"
        "Swagger UI: `/docs`\n"
        "OpenAPI JSON: `/openapi.json`"
    )

    app = FastAPI(
        title=title,
        version=version,
        description=description,
        debug=debug,
        docs_url="/docs",
        redoc_url="/redoc",
        openapi_url="/openapi.json",
    )

    @app.on_event("startup")
    def _startup() -> None:
        from adsp.core.survey.group_generator import GroupGenerator
        from adsp.core.survey.response_synthesizer import ResponseSynthesizer
        from adsp.core.survey.survey_manager import SurveyManager

        logger.info("API startup step 1/5: preparing application services")
        bucket = os.environ.get("ADSP_INGESTION_BUCKET", "uploads")
        reports_dir = Path(os.environ.get("ADSP_REPORTS_DIR", "reports/api"))
        reports_dir.mkdir(parents=True, exist_ok=True)
        surveys_dir = Path(os.environ.get("ADSP_SURVEYS_DIR", str(PROCESSED_DATA_DIR / "surveys")))
        logger.info("API startup step 2/5: reports directory ready at {}", reports_dir)
        qa = LazyQAService()
        logger.info("API startup step 3/5: creating auth, ingestion, and report services")
        app.state.services = AppServices(
            auth=AuthService(),
            qa=qa,
            ingestion=IngestionService(bucket=bucket),
            reports=ReportService(output_dir=reports_dir),
            surveys=SurveyManager(base_dir=surveys_dir),
            groups=GroupGenerator(base_dir=surveys_dir),
            responses=ResponseSynthesizer(base_dir=surveys_dir),
        )
        qa.warmup_async()
        logger.info("API startup step 5/5: server ready; health endpoint available; qa_status={}", qa.status)

    def get_services() -> AppServices:
        services = getattr(app.state, "services", None)
        if services is None:
            raise HTTPException(status_code=503, detail="Application services are not initialized")
        return services

    def get_qa_service(services: AppServices = Depends(get_services)) -> Any:
        try:
            qa = services.qa
            if isinstance(qa, LazyQAService):
                return qa.get_instance()
            return qa
        except Exception as exc:
            raise HTTPException(status_code=503, detail=f"QA service unavailable: {exc}") from exc

    def auth_required() -> bool:
        raw = os.environ.get("ADSP_REQUIRE_AUTH", "false").strip().lower()
        return raw in {"1", "true", "yes", "on"}

    def authorize(
        services: AppServices = Depends(get_services),
        x_user: Optional[str] = Header(None, alias="X-User"),
        x_token: Optional[str] = Header(None, alias="X-Token"),
    ) -> None:
        if not auth_required():
            return
        if not x_user or not x_token:
            raise HTTPException(status_code=401, detail="Missing X-User/X-Token")
        if not services.auth.is_authorized(x_user, x_token):
            raise HTTPException(status_code=401, detail="Unauthorized")

    register_system_routes(app, version=version)
    register_auth_routes(app, get_services=get_services)
    register_persona_routes(app, get_qa_service=get_qa_service)
    register_chat_routes(app, get_qa_service=get_qa_service, authorize=authorize)
    register_ingestion_routes(app, get_services=get_services, authorize=authorize)
    register_report_routes(app, get_services=get_services, authorize=authorize)
    register_group_routes(app, get_services=get_services)
    register_survey_routes(app, get_services=get_services)

    return app


app = create_app()