update ai dolboeba fix1

This commit is contained in:
q 2026-04-11 16:19:36 +03:00
parent 4d0a3841b7
commit 597839bb55

View file

@ -324,17 +324,23 @@ async def _call_llm(
async with session.post(url, json=payload) as resp:
raw_text = await resp.text()
if resp.status >= 400:
logger.error("LLM API returned status %s: %s", resp.status, _clip_text(raw_text, 300))
raise RuntimeError(f"LLM API error {resp.status}: {raw_text[:300]}")
try:
data = await resp.json(content_type=None)
except Exception as exc:
logger.error("LLM API returned invalid JSON: %s", _clip_text(raw_text, 300))
raise RuntimeError(f"Invalid LLM API response: {raw_text[:300]}") from exc
choices = data.get("choices") or []
if not choices:
logger.error("LLM API returned no choices: %s", _clip_text(str(data), 300))
raise RuntimeError(f"LLM API returned no choices: {data}")
content = choices[0].get("message", {}).get("content", "")
return content.strip()
cleaned_content = content.strip()
if not cleaned_content:
logger.warning("LLM returned empty content. Raw response: %s", _clip_text(raw_text, 300))
return cleaned_content
async def _maybe_refresh_summary(chat_id: int) -> None:
@ -383,6 +389,7 @@ async def _maybe_refresh_summary(chat_id: int) -> None:
cleaned_summary = summary.strip()
if not cleaned_summary:
logger.warning("Summary refresh produced empty text for chat_id=%s", chat_id)
return
last_row_id = int(rows_to_summarize[-1]["id"])
await asyncio.to_thread(_store_summary_and_prune, chat_id, cleaned_summary, last_row_id)
@ -537,6 +544,12 @@ async def handle_chat_message(message: Message, *, store_message: bool = True, a
normalized_response = _normalize_reply(response)
if not normalized_response:
logger.warning(
"Reply skipped after normalization. chat_id=%s reason=%s raw=%s",
chat_id,
reason,
_clip_text(response, 200),
)
return False
await asyncio.to_thread(push_message, chat_id, "assistant", BOT_MEMORY_NAME, normalized_response)
@ -554,7 +567,14 @@ async def generate_autoreply(chat_id: int, text: str, user_name: str) -> str:
temperature=0.9,
top_p=0.9,
)
return _normalize_reply(response)
normalized_response = _normalize_reply(response)
if not normalized_response:
logger.warning(
"Legacy generate_autoreply produced empty/skip response. chat_id=%s raw=%s",
chat_id,
_clip_text(response, 200),
)
return normalized_response
async def handle_talk(message: Message) -> None:
@ -590,7 +610,15 @@ async def handle_talk(message: Message) -> None:
await message.reply(ERROR_RESPONSE_TEXT)
return
normalized_response = _normalize_reply(response) or EMPTY_RESPONSE_TEXT
normalized_response = _normalize_reply(response)
if not normalized_response:
logger.warning(
"Talk command produced empty/skip response. chat_id=%s user=%s raw=%s",
chat_id,
user_name,
_clip_text(response, 200),
)
normalized_response = EMPTY_RESPONSE_TEXT
await asyncio.to_thread(push_message, chat_id, "assistant", BOT_MEMORY_NAME, normalized_response)
await message.reply(normalized_response, parse_mode=None)