fix: handle HTTP redirects in event stream and improve JWT expiration handling
This commit is contained in:
+18
-1
@@ -47,8 +47,25 @@ async def event_stream(request: ChatRequest) -> AsyncGenerator[str, None]:
|
|||||||
"Accept": "text/event-stream",
|
"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:
|
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:
|
if response.status_code >= 400:
|
||||||
text = await response.aread()
|
text = await response.aread()
|
||||||
raise HTTPException(status_code=502, detail=f"Ollama Cloud error: {text.decode('utf-8', 'ignore')}" )
|
raise HTTPException(status_code=502, detail=f"Ollama Cloud error: {text.decode('utf-8', 'ignore')}" )
|
||||||
|
|||||||
@@ -263,6 +263,13 @@ function App() {
|
|||||||
[token],
|
[token],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (token && isJwtExpired(token)) {
|
||||||
|
clearLocalToken()
|
||||||
|
setToken(null)
|
||||||
|
}
|
||||||
|
}, [token])
|
||||||
|
|
||||||
async function login() {
|
async function login() {
|
||||||
clearLocalToken()
|
clearLocalToken()
|
||||||
try {
|
try {
|
||||||
@@ -298,6 +305,12 @@ function App() {
|
|||||||
|
|
||||||
async function submitMessage() {
|
async function submitMessage() {
|
||||||
if (!input.trim() || !token) return
|
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 userMessage: Message = { role: 'user', content: input.trim() }
|
||||||
const messageBatch = [...messages, userMessage]
|
const messageBatch = [...messages, userMessage]
|
||||||
|
|||||||
Reference in New Issue
Block a user