linnet/app/api/sessions.py
pyr0ball 7e14f9135e feat: Notation v0.1.x scaffold — full backend + frontend + tests
FastAPI backend (port 8522):
- Session lifecycle: POST /session/start, DELETE /session/{id}/end, GET /session/{id}
- SSE stream: GET /session/{id}/stream — per-subscriber asyncio.Queue fan-out, 15s heartbeat
- History: GET /session/{id}/history with min_confidence + limit filters
- Audio: WS /session/{id}/audio — binary PCM ingestion stub (real inference in v0.2.x)
- Export: GET /session/{id}/export — downloadable JSON session log
- ContextClassifier background task per session (CF_VOICE_MOCK=1 in dev)
- ToneEvent SSE wire format per cf-core#40 (locked field names)
- Tier gate: CFG-LNNT- prefix check, 402 for paid features

Vue 3 frontend (port 8521, Vite + UnoCSS + Pinia):
- NowPanel: affect-aware border tint, subtext, prosody flags, shift indicator
- HistoryStrip: horizontal scroll, last 8 events with affect color
- ComposeBar: start/stop session, SSE connection lifecycle
- useToneStream: EventSource composable
- useAudioCapture: AudioWorklet → Int16 PCM → WebSocket (v0.1.x stub)
- audio-processor.js: 100ms chunk accumulator in AudioWorklet thread
- Respects prefers-reduced-motion globally

26 tests passing, manage.sh, Dockerfile, compose.yml included.
2026-04-06 18:23:52 -07:00

51 lines
1.6 KiB
Python

# app/api/sessions.py — session lifecycle endpoints
from __future__ import annotations
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from app.services import session_store
router = APIRouter(prefix="/session", tags=["sessions"])
class StartRequest(BaseModel):
elcor: bool = False # enable Elcor subtext format (easter egg)
class SessionResponse(BaseModel):
session_id: str
state: str
elcor: bool
@router.post("/start", response_model=SessionResponse)
async def start_session(req: StartRequest = StartRequest()) -> SessionResponse:
"""Start a new annotation session and begin streaming VoiceFrames."""
session = session_store.create_session(elcor=req.elcor)
return SessionResponse(
session_id=session.session_id,
state=session.state,
elcor=session.elcor,
)
@router.delete("/{session_id}/end")
async def end_session(session_id: str) -> dict:
"""Stop a session and release its classifier."""
removed = session_store.end_session(session_id)
if not removed:
raise HTTPException(status_code=404, detail=f"Session {session_id} not found")
return {"session_id": session_id, "state": "stopped"}
@router.get("/{session_id}")
def get_session(session_id: str) -> SessionResponse:
session = session_store.get_session(session_id)
if session is None:
raise HTTPException(status_code=404, detail=f"Session {session_id} not found")
return SessionResponse(
session_id=session.session_id,
state=session.state,
elcor=session.elcor,
)