feat: expose recommendation streaming api
This commit is contained in:
parent
e8d20158e3
commit
41aa93c3c7
7 changed files with 473 additions and 12 deletions
178
backend/app/api/recommendations.py
Normal file
178
backend/app/api/recommendations.py
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
"""Authenticated recommendation streaming and playlist creation routes."""
|
||||
|
||||
import asyncio
|
||||
from collections.abc import AsyncIterator, Callable
|
||||
from typing import cast
|
||||
|
||||
import structlog
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from app.adapters.spotify.errors import SpotifyError
|
||||
from app.adapters.spotify.session import SpotifySession
|
||||
from app.api.routes import resolve_session
|
||||
from app.api.schemas import (
|
||||
NDJSON_CONTENT_TYPE,
|
||||
DoneEvent,
|
||||
ErrorEvent,
|
||||
MetadataEvent,
|
||||
PlaylistCreateRequest,
|
||||
PlaylistCreateResponse,
|
||||
RecommendationRequest,
|
||||
StreamEvent,
|
||||
TrackCard,
|
||||
TrackEvent,
|
||||
WarningEvent,
|
||||
)
|
||||
from app.config import Settings
|
||||
from app.domain.models import ConversationTurn, PreviousRecommendation
|
||||
from app.pipeline.event import (
|
||||
PipelineDoneEvent,
|
||||
PipelineErrorEvent,
|
||||
PipelineEvent,
|
||||
PipelineMetadataEvent,
|
||||
PipelineTrackEvent,
|
||||
PipelineWarningEvent,
|
||||
)
|
||||
from app.pipeline.orchestrator import RecommendationPipeline
|
||||
from app.ports.protocols import MusicCatalog, PlaylistWriter
|
||||
|
||||
SpotifyClientFactory = Callable[[SpotifySession], MusicCatalog | PlaylistWriter]
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/api/recommendations")
|
||||
async def recommendations(
|
||||
request: Request,
|
||||
payload: RecommendationRequest,
|
||||
) -> StreamingResponse:
|
||||
"""Stream one authenticated discovery response as NDJSON."""
|
||||
resolved = resolve_session(request)
|
||||
if resolved is None:
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
|
||||
pipeline = cast(RecommendationPipeline, request.app.state.recommendation_pipeline)
|
||||
factory = cast(SpotifyClientFactory, request.app.state.spotify_client_factory)
|
||||
catalog = cast(MusicCatalog, factory(resolved.session))
|
||||
request_id = cast(str, request.state.request_id)
|
||||
history = tuple(ConversationTurn(turn.role, turn.content) for turn in payload.history)
|
||||
previous = tuple(
|
||||
PreviousRecommendation(
|
||||
item.rank,
|
||||
item.track_id,
|
||||
item.title,
|
||||
tuple(item.artists),
|
||||
)
|
||||
for item in payload.prior_recommendations
|
||||
)
|
||||
|
||||
return StreamingResponse(
|
||||
_stream_lines(
|
||||
request,
|
||||
pipeline,
|
||||
resolved.session_id,
|
||||
request_id,
|
||||
catalog,
|
||||
payload.query,
|
||||
history,
|
||||
previous,
|
||||
),
|
||||
media_type=NDJSON_CONTENT_TYPE,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/playlists", response_model=PlaylistCreateResponse)
|
||||
async def create_playlist(
|
||||
request: Request,
|
||||
payload: PlaylistCreateRequest,
|
||||
) -> PlaylistCreateResponse:
|
||||
"""Create and fill one authenticated Spotify playlist."""
|
||||
resolved = resolve_session(request)
|
||||
if resolved is None:
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
settings = cast(Settings, request.app.state.settings)
|
||||
factory = cast(SpotifyClientFactory, request.app.state.spotify_client_factory)
|
||||
writer = cast(PlaylistWriter, factory(resolved.session))
|
||||
playlist_name = f"{settings.playlist_name_prefix} {payload.name}"
|
||||
try:
|
||||
playlist = await writer.create_playlist(
|
||||
playlist_name,
|
||||
"Music discovery selected by the listener.",
|
||||
)
|
||||
await writer.add_tracks_to_playlist(playlist.id, payload.track_uris)
|
||||
except (SpotifyError, ValueError) as error:
|
||||
structlog.get_logger().warning("playlist_write_failed", error_type=type(error).__name__)
|
||||
raise HTTPException(status_code=502, detail="Spotify playlist creation failed") from error
|
||||
return PlaylistCreateResponse(url=playlist.url)
|
||||
|
||||
|
||||
async def _stream_lines(
|
||||
request: Request,
|
||||
pipeline: RecommendationPipeline,
|
||||
session_id: str,
|
||||
request_id: str,
|
||||
catalog: MusicCatalog,
|
||||
query: str,
|
||||
history: tuple[ConversationTurn, ...],
|
||||
previous: tuple[PreviousRecommendation, ...],
|
||||
) -> AsyncIterator[str]:
|
||||
event_stream = pipeline.stream(
|
||||
session_id,
|
||||
request_id,
|
||||
catalog,
|
||||
query,
|
||||
history,
|
||||
previous,
|
||||
)
|
||||
try:
|
||||
async for event in event_stream:
|
||||
if await request.is_disconnected():
|
||||
return
|
||||
yield f"{_to_wire_event(event).model_dump_json()}\n"
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception as error:
|
||||
structlog.get_logger().exception(
|
||||
"recommendation_stream_failed",
|
||||
error_type=type(error).__name__,
|
||||
)
|
||||
if not await request.is_disconnected():
|
||||
failure = ErrorEvent(
|
||||
code="recommendation_failed",
|
||||
message="Recommendation could not be completed.",
|
||||
)
|
||||
yield f"{failure.model_dump_json()}\n"
|
||||
finally:
|
||||
await event_stream.aclose()
|
||||
|
||||
|
||||
def _to_wire_event(event: PipelineEvent) -> StreamEvent:
|
||||
if isinstance(event, PipelineMetadataEvent):
|
||||
return MetadataEvent(
|
||||
request_id=event.request_id,
|
||||
intent_summary=event.intent_summary,
|
||||
candidate_count=event.candidate_count,
|
||||
)
|
||||
if isinstance(event, PipelineTrackEvent):
|
||||
track = event.track
|
||||
return TrackEvent(
|
||||
rank=event.rank,
|
||||
track=TrackCard(
|
||||
id=track.id,
|
||||
uri=track.uri,
|
||||
title=track.title,
|
||||
artists=list(track.artists),
|
||||
album_name=track.album_name,
|
||||
album_art_url=track.album_art_url,
|
||||
external_url=track.external_url,
|
||||
),
|
||||
justification=event.justification,
|
||||
)
|
||||
if isinstance(event, PipelineWarningEvent):
|
||||
return WarningEvent(code=event.code, message=event.message)
|
||||
if isinstance(event, PipelineErrorEvent):
|
||||
return ErrorEvent(code=event.code, message=event.message)
|
||||
if isinstance(event, PipelineDoneEvent):
|
||||
return DoneEvent(track_count=event.track_count, total_ms=event.total_ms)
|
||||
raise AssertionError("Unhandled pipeline event")
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
"""HTTP routes for Spotify login and session management."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import cast
|
||||
|
||||
import httpx2
|
||||
|
|
@ -7,7 +8,7 @@ from fastapi import APIRouter, Request
|
|||
from fastapi.responses import JSONResponse, RedirectResponse, Response
|
||||
|
||||
from app.adapters.spotify.login import begin_login, complete_login
|
||||
from app.adapters.spotify.session import PendingLogins, SessionStore
|
||||
from app.adapters.spotify.session import PendingLogins, SessionStore, SpotifySession
|
||||
from app.config import Settings
|
||||
|
||||
SESSION_COOKIE_NAME = "discovery_session"
|
||||
|
|
@ -15,6 +16,14 @@ SESSION_COOKIE_NAME = "discovery_session"
|
|||
router = APIRouter()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ResolvedSession:
|
||||
"""A session and stable cache key selected for one request."""
|
||||
|
||||
session_id: str
|
||||
session: SpotifySession
|
||||
|
||||
|
||||
@router.get("/api/auth/login")
|
||||
def login(request: Request) -> RedirectResponse:
|
||||
"""Start Spotify Authorization Code with PKCE login."""
|
||||
|
|
@ -65,12 +74,10 @@ async def callback(
|
|||
@router.get("/api/auth/me")
|
||||
def current_session(request: Request) -> JSONResponse:
|
||||
"""Return the display name for a valid application session."""
|
||||
session_id = request.cookies.get(SESSION_COOKIE_NAME)
|
||||
session_store = cast(SessionStore, request.app.state.session_store)
|
||||
session = session_store.get(session_id) if session_id is not None else None
|
||||
if session is None:
|
||||
resolved = resolve_session(request)
|
||||
if resolved is None:
|
||||
return JSONResponse({"detail": "Not authenticated"}, status_code=401)
|
||||
return JSONResponse({"display_name": session.display_name})
|
||||
return JSONResponse({"display_name": resolved.session.display_name})
|
||||
|
||||
|
||||
@router.post("/api/auth/logout", status_code=204)
|
||||
|
|
@ -95,3 +102,19 @@ def logout(request: Request) -> Response:
|
|||
|
||||
def _login_error_redirect() -> RedirectResponse:
|
||||
return RedirectResponse("/?login=error", status_code=307)
|
||||
|
||||
|
||||
def resolve_session(request: Request) -> ResolvedSession | None:
|
||||
"""Resolve the cookie session or the installed live seed session."""
|
||||
session_store = cast(SessionStore, request.app.state.session_store)
|
||||
cookie_session_id = request.cookies.get(SESSION_COOKIE_NAME)
|
||||
if cookie_session_id is not None:
|
||||
cookie_session = session_store.get(cookie_session_id)
|
||||
if cookie_session is not None:
|
||||
return ResolvedSession(cookie_session_id, cookie_session)
|
||||
|
||||
seed_session_id = cast(str | None, getattr(request.app.state, "seed_session_id", None))
|
||||
seed_session = session_store.get(seed_session_id) if seed_session_id is not None else None
|
||||
if seed_session is None or seed_session_id is None:
|
||||
return None
|
||||
return ResolvedSession(seed_session_id, seed_session)
|
||||
|
|
|
|||
|
|
@ -1,16 +1,24 @@
|
|||
"""Application factory and wiring. No logic lives here."""
|
||||
"""Application factory and dependency wiring."""
|
||||
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
import httpx2
|
||||
from anthropic import AsyncAnthropic
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from app.adapters.spotify.session import PendingLogins, SessionStore
|
||||
from app.adapters.anthropic.llm import AnthropicRecommender
|
||||
from app.adapters.spotify.auth import TokenSet, refresh_access_token
|
||||
from app.adapters.spotify.client import SpotifyClient
|
||||
from app.adapters.spotify.session import PendingLogins, SessionStore, SpotifySession
|
||||
from app.api.recommendations import router as recommendations_router
|
||||
from app.api.routes import router
|
||||
from app.config import AppMode, Settings, settings
|
||||
from app.observability.logging import configure_logging
|
||||
from app.observability.timing import RequestTimingMiddleware
|
||||
from app.pipeline.orchestrator import RecommendationPipeline
|
||||
|
||||
FRONTEND_DIST = Path(__file__).parent / "static"
|
||||
|
||||
|
|
@ -21,11 +29,11 @@ def create_app(
|
|||
) -> FastAPI:
|
||||
"""Build the FastAPI app: API routes plus the built SPA on one port."""
|
||||
active_settings = application_settings or settings
|
||||
configure_logging(active_settings.app_mode)
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(application: FastAPI) -> AsyncIterator[None]:
|
||||
if active_settings.app_mode is AppMode.LIVE and not active_settings.spotify_client_id:
|
||||
raise RuntimeError("SPOTIFY_CLIENT_ID is required in live mode")
|
||||
_validate_live_settings(active_settings)
|
||||
async with httpx2.AsyncClient(
|
||||
timeout=active_settings.spotify_timeout_seconds,
|
||||
transport=http_transport,
|
||||
|
|
@ -34,7 +42,33 @@ def create_app(
|
|||
application.state.session_store = SessionStore()
|
||||
application.state.pending_logins = PendingLogins()
|
||||
application.state.settings = active_settings
|
||||
yield
|
||||
application.state.seed_session_id = None
|
||||
anthropic_client = AsyncAnthropic(
|
||||
api_key=active_settings.anthropic_api_key or "unused-demo-key"
|
||||
)
|
||||
application.state.anthropic = anthropic_client
|
||||
application.state.recommendation_pipeline = RecommendationPipeline(
|
||||
AnthropicRecommender(anthropic_client, active_settings),
|
||||
active_settings,
|
||||
)
|
||||
|
||||
def spotify_client_factory(session: SpotifySession) -> SpotifyClient:
|
||||
return SpotifyClient(http, session, active_settings)
|
||||
|
||||
application.state.spotify_client_factory = spotify_client_factory
|
||||
if (
|
||||
active_settings.app_mode is AppMode.LIVE
|
||||
and active_settings.spotify_seed_refresh_token
|
||||
):
|
||||
application.state.seed_session_id = await _install_seed_session(
|
||||
http,
|
||||
application.state.session_store,
|
||||
active_settings,
|
||||
)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
await anthropic_client.close()
|
||||
|
||||
app = FastAPI(
|
||||
title="discovery-by-llm",
|
||||
|
|
@ -42,12 +76,14 @@ def create_app(
|
|||
redoc_url=None,
|
||||
lifespan=lifespan,
|
||||
)
|
||||
app.add_middleware(RequestTimingMiddleware)
|
||||
|
||||
@app.get("/api/health")
|
||||
def health() -> dict[str, str]:
|
||||
return {"status": "ok", "mode": active_settings.app_mode}
|
||||
|
||||
app.include_router(router)
|
||||
app.include_router(recommendations_router)
|
||||
if FRONTEND_DIST.is_dir():
|
||||
app.mount("/", StaticFiles(directory=FRONTEND_DIST, html=True), name="spa")
|
||||
|
||||
|
|
@ -55,3 +91,41 @@ def create_app(
|
|||
|
||||
|
||||
app = create_app()
|
||||
|
||||
|
||||
def _validate_live_settings(application_settings: Settings) -> None:
|
||||
if application_settings.app_mode is not AppMode.LIVE:
|
||||
return
|
||||
if not application_settings.spotify_client_id:
|
||||
raise RuntimeError("SPOTIFY_CLIENT_ID is required in live mode")
|
||||
if not application_settings.anthropic_api_key:
|
||||
raise RuntimeError("ANTHROPIC_API_KEY is required in live mode")
|
||||
|
||||
|
||||
async def _install_seed_session(
|
||||
http: httpx2.AsyncClient,
|
||||
session_store: SessionStore,
|
||||
application_settings: Settings,
|
||||
) -> str:
|
||||
tokens = await refresh_access_token(
|
||||
http,
|
||||
client_id=application_settings.spotify_client_id,
|
||||
tokens=TokenSet(
|
||||
access_token="seed-bootstrap",
|
||||
refresh_token=application_settings.spotify_seed_refresh_token,
|
||||
expires_at=0.0,
|
||||
),
|
||||
)
|
||||
bootstrap_session = SpotifySession(tokens=tokens, account_id="", display_name="")
|
||||
current_user = await SpotifyClient(
|
||||
http,
|
||||
bootstrap_session,
|
||||
application_settings,
|
||||
).fetch_current_user()
|
||||
return session_store.create(
|
||||
SpotifySession(
|
||||
tokens=bootstrap_session.tokens,
|
||||
account_id=current_user.account_id,
|
||||
display_name=current_user.display_name,
|
||||
)
|
||||
)
|
||||
|
|
|
|||
30
backend/app/observability/logging.py
Normal file
30
backend/app/observability/logging.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
"""Configure structlog for machine-readable live logs and readable development logs."""
|
||||
|
||||
import logging
|
||||
|
||||
import structlog
|
||||
|
||||
from app.config import AppMode
|
||||
|
||||
|
||||
def configure_logging(app_mode: AppMode) -> None:
|
||||
"""Install the process logging pipeline for the selected runtime mode."""
|
||||
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
||||
renderer: structlog.types.Processor
|
||||
if app_mode is AppMode.LIVE:
|
||||
renderer = structlog.processors.JSONRenderer()
|
||||
else:
|
||||
renderer = structlog.dev.ConsoleRenderer(colors=False)
|
||||
structlog.configure(
|
||||
processors=[
|
||||
structlog.contextvars.merge_contextvars,
|
||||
structlog.processors.add_log_level,
|
||||
structlog.processors.TimeStamper(fmt="iso", utc=True),
|
||||
structlog.processors.StackInfoRenderer(),
|
||||
structlog.processors.format_exc_info,
|
||||
renderer,
|
||||
],
|
||||
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
|
||||
logger_factory=structlog.PrintLoggerFactory(),
|
||||
cache_logger_on_first_use=True,
|
||||
)
|
||||
|
|
@ -8,6 +8,7 @@ from typing import cast
|
|||
|
||||
import structlog
|
||||
from starlette.types import ASGIApp, Receive, Scope, Send
|
||||
from structlog.contextvars import bind_contextvars, reset_contextvars
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -71,10 +72,12 @@ class RequestTimingMiddleware:
|
|||
counters = RequestCounters()
|
||||
counter_token = _COUNTERS.set(counters)
|
||||
request_token = _REQUEST_ID.set(request_id)
|
||||
logging_tokens = bind_contextvars(request_id=request_id)
|
||||
try:
|
||||
await self.app(scope, receive, send)
|
||||
finally:
|
||||
self._log_completion(scope, request_id, started_at, counters)
|
||||
reset_contextvars(**logging_tokens)
|
||||
_COUNTERS.reset(counter_token)
|
||||
_REQUEST_ID.reset(request_token)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue