diff --git a/backend/app/main.py b/backend/app/main.py index d73542f..cd4c29f 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -47,8 +47,25 @@ async def event_stream(request: ChatRequest) -> AsyncGenerator[str, None]: "Accept": "text/event-stream", } - async with httpx.AsyncClient(timeout=None, follow_redirects=True) as client: + async with httpx.AsyncClient(timeout=None, follow_redirects=False) as client: async with client.stream("POST", OLLAMA_API_URL, json=payload, headers=headers) as response: + if response.status_code in (301, 302, 307, 308) and response.headers.get("location"): + location = response.headers["location"] + await response.aclose() + logger.debug("Following Ollama redirect to %s", location) + async with client.stream("POST", location, json=payload, headers=headers, follow_redirects=False) as response: + if response.status_code >= 400: + text = await response.aread() + raise HTTPException(status_code=502, detail=f"Ollama Cloud error: {text.decode('utf-8', 'ignore')}" ) + + async for chunk in response.aiter_text(): + if not chunk: + continue + for line in chunk.splitlines(): + if line.strip(): + yield f"data: {line}\n\n" + return + if response.status_code >= 400: text = await response.aread() raise HTTPException(status_code=502, detail=f"Ollama Cloud error: {text.decode('utf-8', 'ignore')}" ) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 38abf90..4fb9fda 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -263,6 +263,13 @@ function App() { [token], ) + useEffect(() => { + if (token && isJwtExpired(token)) { + clearLocalToken() + setToken(null) + } + }, [token]) + async function login() { clearLocalToken() try { @@ -298,6 +305,12 @@ function App() { async function submitMessage() { if (!input.trim() || !token) return + if (isJwtExpired(token)) { + clearLocalToken() + setToken(null) + setError('Session expired, please log in again.') + return + } const userMessage: Message = { role: 'user', content: input.trim() } const messageBatch = [...messages, userMessage]