The 21 the automatic pass could not make on its own. `ruff check` and `ruff format --check` are both clean now; typecheck is still red and is next. `in_reasoning` in chat/service.py was a complete state machine that nothing read: initialised False, set True when a reasoning delta arrived, set False when the summary ended — three assignments, zero reads. Ruff reported one at a time, and removing each revealed the next, so what looked like a single stray variable took three passes to bottom out. The branches themselves do real work and are untouched; only the flag is gone. Four `raise HTTPException` inside `except` blocks now chain with `from e`. Until now a failure while handling an error was indistinguishable from the error, which matters most in exactly the situation where the traceback is all you have. In biographer/tools.py the binding was unused but the call is not: MemoryType() is called for the ValueError it raises on an invalid name. The binding is gone and the call and its comment stay, because dropping the line would have removed the validation. The rest are unused bindings in tests where the assertions are on something else (call_args, mostly), plus three unused loop variables and an isinstance tuple. One correction to my own work: removing a dead comprehension in test_error_handling.py left an `if` block with nothing but comments in it, which is a SyntaxError. Ruff caught it immediately. The block now says what the test actually pins — that the stream parses without crashing, which reaching that line demonstrates — rather than computing a list nobody asserts on. `make test` is intermittent here, and it is not this change. test_tatlock_tool_call_logging_calculator failed in two of five full runs across both HEAD and this branch, and passes in the other three; it also fails in isolation at HEAD while passing in isolation here. Order- or timing-dependent. Recorded rather than chased, since tests are not gated in this repo yet. Co-Authored-By: Claude <noreply@anthropic.com>
85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
"""
|
|
Responses router.
|
|
|
|
OpenAI-compatible /v1/responses endpoint with streaming support.
|
|
"""
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from sse_starlette.sse import EventSourceResponse
|
|
|
|
from src.core.exceptions import AppException, ModelNotFoundError
|
|
from src.core.logging_config import get_logger
|
|
from src.responses import service
|
|
from src.responses.schemas import Response, ResponseRequest
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
router = APIRouter(prefix="/responses", tags=["responses"])
|
|
|
|
|
|
@router.post("", response_model=Response)
|
|
async def create_response(
|
|
request: ResponseRequest,
|
|
) -> Response | EventSourceResponse:
|
|
"""
|
|
Create a response using Responses API format.
|
|
|
|
Supports:
|
|
- Reasoning summaries (thinking/reasoning display)
|
|
- Function calling (tool usage)
|
|
- Streaming responses
|
|
- Multi-turn conversations
|
|
- Error handling
|
|
|
|
Args:
|
|
request: Response request with model, input, optional reasoning/tools
|
|
|
|
Returns:
|
|
Response object or SSE stream
|
|
"""
|
|
logger.info(
|
|
"response_request_received",
|
|
model=request.model,
|
|
user=request.user,
|
|
streaming=request.stream,
|
|
)
|
|
|
|
try:
|
|
# Check if this is a Tatlock request - use Steward preprocessing
|
|
model_id = request.model
|
|
if "." in model_id:
|
|
model_id = model_id.split(".", 1)[1]
|
|
|
|
use_steward = model_id.lower() == "tatlock"
|
|
|
|
if request.stream:
|
|
logger.info("Streaming response requested")
|
|
|
|
if use_steward:
|
|
logger.info("Streaming with Steward preprocessing for Tatlock request")
|
|
from src.responses.streaming import StreamingCoordinator
|
|
|
|
coordinator = StreamingCoordinator()
|
|
return EventSourceResponse(coordinator.stream_response_with_steward(request))
|
|
else:
|
|
return EventSourceResponse(service.create_response_stream(request))
|
|
|
|
# Non-streaming response
|
|
if use_steward:
|
|
logger.info("Using Steward preprocessing for Tatlock request")
|
|
return await service.create_response_with_steward(request)
|
|
else:
|
|
return await service.create_response(request)
|
|
|
|
except ModelNotFoundError as e:
|
|
logger.error(f"Model not found: {e}")
|
|
raise HTTPException(status_code=404, detail=str(e)) from e
|
|
|
|
except AppException as e:
|
|
logger.error(f"Application error: {e}")
|
|
raise HTTPException(status_code=e.status_code, detail=e.message) from e
|
|
|
|
except Exception as e:
|
|
logger.error(f"Unexpected error: {e}", exc_info=True)
|
|
raise HTTPException(status_code=500, detail="Internal server error") from e
|