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.
58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
# app/models/session.py — Session dataclass
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import time
|
|
import uuid
|
|
from dataclasses import dataclass, field
|
|
from typing import Literal
|
|
|
|
from app.models.tone_event import ToneEvent
|
|
|
|
SessionState = Literal["starting", "running", "stopped"]
|
|
|
|
|
|
@dataclass
|
|
class Session:
|
|
"""
|
|
An active annotation session.
|
|
|
|
The session owns the subscriber queue fan-out: each SSE connection
|
|
subscribes by calling subscribe() and gets its own asyncio.Queue.
|
|
The session_store fans out ToneEvents to all queues via broadcast().
|
|
"""
|
|
session_id: str = field(default_factory=lambda: str(uuid.uuid4())[:8])
|
|
state: SessionState = "starting"
|
|
created_at: float = field(default_factory=time.monotonic)
|
|
elcor: bool = False
|
|
|
|
# History: last 50 events retained for GET /session/{id}/history
|
|
history: list[ToneEvent] = field(default_factory=list)
|
|
_subscribers: list[asyncio.Queue] = field(default_factory=list, repr=False)
|
|
|
|
def subscribe(self) -> asyncio.Queue:
|
|
"""Add an SSE subscriber. Returns its dedicated queue."""
|
|
q: asyncio.Queue = asyncio.Queue(maxsize=100)
|
|
self._subscribers.append(q)
|
|
return q
|
|
|
|
def unsubscribe(self, q: asyncio.Queue) -> None:
|
|
try:
|
|
self._subscribers.remove(q)
|
|
except ValueError:
|
|
pass
|
|
|
|
def broadcast(self, event: ToneEvent) -> None:
|
|
"""Fan out a ToneEvent to all current SSE subscribers."""
|
|
if len(self.history) >= 50:
|
|
self.history.pop(0)
|
|
self.history.append(event)
|
|
|
|
dead: list[asyncio.Queue] = []
|
|
for q in self._subscribers:
|
|
try:
|
|
q.put_nowait(event)
|
|
except asyncio.QueueFull:
|
|
dead.append(q)
|
|
for q in dead:
|
|
self.unsubscribe(q)
|