fix: переадресация провайдера — отказ, сбой журнала не отменяет запасной путь (находки зайки)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-09-24 02:22:07 +10:00
co-authored by Claude Opus 5.5
parent 454108655b
commit 58bb6a3406
3 changed files with 34 additions and 4 deletions
+9 -4
View File
@@ -83,7 +83,8 @@ async def stream_chat(ep: Endpoint, messages: list) -> AsyncGenerator[tuple[str,
async with client.stream("POST", f"{ep.base_url}/chat/completions",
headers={"Authorization": f"Bearer {ep.key}"},
json=request_body(ep.model, messages)) as resp:
if resp.status_code >= 400:
# httpx не идёт за переадресацией: 3xx — тоже отказ, а не пустой ответ.
if not 200 <= resp.status_code < 300:
raise ProviderRefused(resp.status_code,
(await resp.aread()).decode("utf-8", "replace"))
async for line in resp.aiter_lines():
@@ -160,9 +161,13 @@ async def stream_with_fallback(messages: list, *, primary: Endpoint, username: s
nxt = chain[idx + 1].name
_log.warning("справочный путь: %s отказал до первого текста (%s) — отвечает %s",
ep.name, failure, nxt)
import db
await asyncio.to_thread(db.log_action, username, "qa_fallback", "STREAM", "/api/chat",
f"{ep.name}→{nxt}: {failure}"[:200], None, 502)
try:
import db
await asyncio.to_thread(db.log_action, username, "qa_fallback", "STREAM",
"/api/chat", f"{ep.name}→{nxt}: {failure}"[:200], None, 502)
except Exception: # noqa: BLE001 — сбой журнала не отменяет спасение ответа
_log.warning("справочный путь: переключение не записано в журнал действий",
exc_info=True)
continue
yield "error", failure
return