chore(backend): save remaining API and deployment changes

Expose frozen factual review policy, update API descriptions and Swagger settings, and adjust local deployment and embedding connectivity.

Include the requested extraction candidates and review-policy regression coverage. Validation: 31 OCR policy tests passed; staged diff whitespace checks passed.
This commit is contained in:
amirhosein mazaheri
2026-10-10 17:43:17 +03:30
parent a769196bfa
commit 21a0ea076a
18 changed files with 268 additions and 49 deletions
+70 -4
View File
@@ -66,11 +66,76 @@ async def lifespan(app):
if startup_error is None:
raise
app = FastAPI(title='Technical Documentation Assistant API', lifespan=lifespan)
API_DESCRIPTION = """
## 🤖 Technical Documentation Assistant API
A modular, production-ready backend combining RAG (Retrieval-Augmented Generation) document search with bidirectional live voice streaming.
---
### 🔑 Authentication & Security
* **Session Cookies:** Authenticated requests rely on the opaque `rag_session` HTTP-only cookie.
* **CSRF Protection:** State-changing requests (`POST`, `PATCH`, `DELETE`) require `X-CSRF-Token` header.
* **Host & Origin Validation:** Requests are filtered by strict loopback/trusted host gating.
### 📚 Core Modules
1. **Authentication:** User login/logout and session validation.
2. **Conversations:** Creation and state tracking for multi-turn chats.
3. **Voice Sessions:** Realtime bidirectional voice via WebSocket stream.
4. **Documents:** Document upload (PDF, DOCX, Markdown, Images), automated extraction, and quality control.
5. **Knowledge & Configuration:** Organizational knowledge selection and runtime capabilities discovery.
6. **Health & Monitoring:** Kubernetes/Docker liveness and readiness probes.
"""
TAGS_METADATA = [
{
"name": "Authentication",
"description": "User login, session lifecycle management, CSRF validation, and identity introspection.",
},
{
"name": "Conversations",
"description": "Creating and managing multi-turn text and voice conversations.",
},
{
"name": "Voice Sessions",
"description": "Bidirectional realtime voice sessions and WebSocket audio streaming endpoints.",
},
{
"name": "Documents",
"description": "Document ingestion pipeline, file intake, OCR review workflows, and error retries.",
},
{
"name": "Knowledge & Configuration",
"description": "Organizational knowledge selection, revision fencing, and assistant capabilities inquiry.",
},
{
"name": "Health & Monitoring",
"description": "Service liveness and readiness probes for orchestrators and monitoring.",
},
]
SWAGGER_UI_PARAMETERS = {
"defaultModelsExpandDepth": -1, # Collapse Schemas section by default for clean view
"docExpansion": "list", # Group endpoints neatly by tag
"filter": True, # Real-time search filter bar
"syntaxHighlight.theme": "monokai", # Modern dark syntax highlight for JSON
"tryItOutEnabled": True, # One-click try it out
"persistAuthorization": True, # Persist auth cookies across reloads
"displayRequestDuration": True, # Show execution duration in ms
}
app = FastAPI(
title='Technical Documentation Assistant API',
description=API_DESCRIPTION,
version='1.0.0',
openapi_tags=TAGS_METADATA,
swagger_ui_parameters=SWAGGER_UI_PARAMETERS,
lifespan=lifespan,
)
app.state.ready = False
config = VoiceSettings()
app.add_middleware(CORSMiddleware, allow_origins=list(config.trusted_origins), allow_credentials=True,
allow_methods=['GET','POST','PATCH'], allow_headers=['Content-Type','X-CSRF-Token','X-Request-ID','Idempotency-Key'],
allow_methods=['GET','POST','PATCH','OPTIONS'], allow_headers=['Content-Type','X-CSRF-Token','X-Request-ID','Idempotency-Key','Accept','Authorization'],
expose_headers=['X-Request-ID','Idempotency-Replayed'])
@app.middleware('http')
@@ -111,11 +176,12 @@ async def unexpected_error(request, exc):
for router in (auth_router, identity_router, conversations_router, voice_router, documents_router, knowledge_router):
app.include_router(router, prefix='/api/v1')
@app.get('/health/live')
@app.get('/health/live', tags=['Health & Monitoring'], summary='Liveness Probe', description='Returns HTTP 200 if the API process is alive and receiving traffic.')
def live():
return {'status': 'ok'}
@app.get('/health/ready')
@app.get('/health/ready', tags=['Health & Monitoring'], summary='Readiness Probe', description='Returns HTTP 200 if the database and search dependencies have reconciled and the API is ready to serve requests.')
def ready():
return JSONResponse(status_code=200 if app.state.ready else 503,
content={'status': 'ready', 'ingestion': 'available'} if app.state.ready else {'status': 'not_ready'})