|
| 1 | +from fastapi import APIRouter, Depends, HTTPException |
| 2 | +from fastapi.responses import StreamingResponse |
| 3 | +from pydantic import BaseModel, Field |
| 4 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 5 | + |
| 6 | +from app.db import User, get_async_session |
| 7 | +from app.services.new_streaming_service import VercelStreamingService |
| 8 | +from app.services.vision_autocomplete_service import stream_vision_autocomplete |
| 9 | +from app.users import current_active_user |
| 10 | +from app.utils.rbac import check_search_space_access |
| 11 | + |
| 12 | +router = APIRouter(prefix="/autocomplete", tags=["autocomplete"]) |
| 13 | + |
| 14 | +MAX_SCREENSHOT_SIZE = 20 * 1024 * 1024 # 20 MB base64 ceiling |
| 15 | + |
| 16 | + |
| 17 | +class VisionAutocompleteRequest(BaseModel): |
| 18 | + screenshot: str = Field(..., max_length=MAX_SCREENSHOT_SIZE) |
| 19 | + search_space_id: int |
| 20 | + app_name: str = "" |
| 21 | + window_title: str = "" |
| 22 | + |
| 23 | + |
| 24 | +@router.post("/vision/stream") |
| 25 | +async def vision_autocomplete_stream( |
| 26 | + body: VisionAutocompleteRequest, |
| 27 | + user: User = Depends(current_active_user), |
| 28 | + session: AsyncSession = Depends(get_async_session), |
| 29 | +): |
| 30 | + await check_search_space_access(session, user, body.search_space_id) |
| 31 | + |
| 32 | + return StreamingResponse( |
| 33 | + stream_vision_autocomplete( |
| 34 | + body.screenshot, body.search_space_id, session, |
| 35 | + app_name=body.app_name, window_title=body.window_title, |
| 36 | + ), |
| 37 | + media_type="text/event-stream", |
| 38 | + headers={ |
| 39 | + **VercelStreamingService.get_response_headers(), |
| 40 | + "X-Accel-Buffering": "no", |
| 41 | + }, |
| 42 | + ) |
0 commit comments