Files
tatlock/src/chat/router.py
T
jpmschweitzerandClaude 78066fab1b style: apply ruff's automatic fixes and formatter
Mechanical only, and separated from the judgment calls that follow so the
reviewable changes are not buried in a 98-file whitespace diff.

227 automatic fixes: 60 blank lines carrying whitespace, 60 unsorted import
blocks, 34 Optional[X] to X | None, 28 unused imports, 16 deprecated typing
imports, 12 datetime.timezone.utc to datetime.UTC, and assorted smaller
modernisations. Then `ruff format` over src and tests: 98 files reformatted,
35 already conforming.

No file among the unused-import findings defines __all__ or is an __init__.py,
so nothing here removes a re-export.

`make test`: 658 passed, unchanged from HEAD.

Two things observed while verifying, neither addressed here:

`pytest tests/` cannot collect — tests/e2e/test_orchestration_e2e.py uses an
`e2e` marker that is not registered, and the config is strict about markers.
This fails identically at HEAD, so it predates this change; `make test` passes
because it ignores tests/e2e, tests/integration and tests/contracts.

test_tatlock_tool_call_logging_calculator is flaky. It failed once in a full run
with these changes and passed on the next, passes in isolation with them, and
fails in isolation at HEAD. It is order- or timing-dependent, not a regression
from this commit — established by running the full suite both ways rather than
by reasoning about which change could have caused it.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-11 17:25:18 +02:00

74 lines
1.9 KiB
Python

"""
Chat completion router.
OpenAI-compatible /v1/chat/completions endpoint.
"""
import json
import logging
from collections.abc import AsyncGenerator
from fastapi import APIRouter
from starlette.responses import StreamingResponse
from src.chat import service
from src.chat.schemas import (
ChatCompletionRequest,
ChatCompletionResponse,
)
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/chat", tags=["chat"])
async def _stream_response(
request: ChatCompletionRequest,
) -> AsyncGenerator[str, None]:
"""
Generate SSE stream for chat completion.
Yields raw SSE-formatted strings matching OpenAI's format exactly:
data: {json}\n\n
"""
try:
async for chunk in service.create_chat_completion_stream(request):
yield f"data: {chunk.model_dump_json(exclude_unset=True)}\n\n"
yield "data: [DONE]\n\n"
except Exception as e:
logger.error(f"Error in streaming response: {e}")
error_data = json.dumps({"error": {"message": str(e), "type": "internal_error"}})
yield f"data: {error_data}\n\n"
@router.post("/completions", response_model=ChatCompletionResponse)
async def create_chat_completion(
request: ChatCompletionRequest,
) -> ChatCompletionResponse | StreamingResponse:
"""
Create chat completion (OpenAI-compatible).
Supports both regular and streaming responses.
Args:
request: Chat completion request
Returns:
Chat completion response or SSE stream
"""
logger.info(f"Chat completion request for model: {request.model}")
if request.stream:
logger.info("Streaming response requested")
return StreamingResponse(
_stream_response(request),
media_type="text/event-stream",
headers={
"Cache-Control": "no-store",
"X-Accel-Buffering": "no",
},
)
return await service.create_chat_completion(request)