Collapse blockquote and strip <think> blocks from AI responses

- blockquote expandable: replies are collapsed by default in Telegram
- strip <think>...</think> sections (chain-of-thought from reasoning
  models like DeepSeek R1 / QwQ) before sending the response

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-18 14:40:33 +02:00
parent 777aa90f17
commit 7356a1812e
2 changed files with 8 additions and 3 deletions

View File

@@ -626,12 +626,17 @@ class AIService:
return "" return ""
content = getattr(message, "content", None) content = getattr(message, "content", None)
if isinstance(content, str): if isinstance(content, str):
return content.strip() return self._strip_think(content)
if isinstance(content, list): if isinstance(content, list):
parts = [] parts = []
for part in content: for part in content:
text = getattr(part, "text", None) text = getattr(part, "text", None)
if isinstance(text, str): if isinstance(text, str):
parts.append(text) parts.append(text)
return "\n".join(parts).strip() return self._strip_think("\n".join(parts))
return "" return ""
@staticmethod
def _strip_think(text: str) -> str:
import re
return re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()

View File

@@ -107,7 +107,7 @@ async def _reply_long(ctx: "CommandContext", text: str) -> None:
if not chunks: if not chunks:
await ctx.reply("") await ctx.reply("")
return return
html_chunks = [f"<blockquote>{_md_to_tg(c)}</blockquote>" for c in chunks] html_chunks = [f"<blockquote expandable>{_md_to_tg(c)}</blockquote>" for c in chunks]
first = await ctx.reply(html_chunks[0], parse_mode=enums.ParseMode.HTML) first = await ctx.reply(html_chunks[0], parse_mode=enums.ParseMode.HTML)
for chunk in html_chunks[1:]: for chunk in html_chunks[1:]:
await ctx.client.send_message( await ctx.client.send_message(