Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ccbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ async def handle_new_message(msg: NewMessage, bot: Bot) -> None:
msg.is_complete,
msg.content_type,
msg.role,
max_thinking_chars=config.max_thinking_chars,
)

if msg.is_complete:
Expand Down
4 changes: 4 additions & 0 deletions src/ccbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def __init__(self) -> None:
os.getenv("CCBOT_SHOW_TOOL_CALLS", "true").lower() != "false"
)

# Max characters for thinking content before truncation.
# 0 = no truncation (full thinking forwarded to Telegram).
self.max_thinking_chars = int(os.getenv("CCBOT_MAX_THINKING_CHARS", "0"))

# Show hidden (dot) directories in directory browser
self.show_hidden_dirs = (
os.getenv("CCBOT_SHOW_HIDDEN_DIRS", "").lower() == "true"
Expand Down
17 changes: 10 additions & 7 deletions src/ccbot/handlers/response_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ def build_response_parts(
is_complete: bool,
content_type: str = "text",
role: str = "assistant",
max_thinking_chars: int = 0,
) -> list[str]:
"""Build paginated response messages for Telegram.

Returns a list of raw markdown strings, each within Telegram's 4096 char limit.
Multi-part messages get a [1/N] suffix.
Markdown-to-MarkdownV2 conversion is done by the send layer, not here.

Args:
max_thinking_chars: Max characters for thinking content. 0 = no truncation.
"""
text = text.strip()

Expand All @@ -41,18 +45,17 @@ def build_response_parts(
text = text[:3000] + "…"
return [f"{prefix}{text}"]

# Truncate thinking content to keep it compact
if content_type == "thinking" and is_complete:
# Truncate thinking content to keep it compact (0 = disabled)
if max_thinking_chars > 0 and content_type == "thinking" and is_complete:
start_tag = TranscriptParser.EXPANDABLE_QUOTE_START
end_tag = TranscriptParser.EXPANDABLE_QUOTE_END
max_thinking = 500
if start_tag in text and end_tag in text:
inner = text[text.index(start_tag) + len(start_tag) : text.index(end_tag)]
if len(inner) > max_thinking:
inner = inner[:max_thinking] + "\n\n… (thinking truncated)"
if len(inner) > max_thinking_chars:
inner = inner[:max_thinking_chars] + "\n\n… (thinking truncated)"
text = start_tag + inner + end_tag
elif len(text) > max_thinking:
text = text[:max_thinking] + "\n\n… (thinking truncated)"
elif len(text) > max_thinking_chars:
text = text[:max_thinking_chars] + "\n\n… (thinking truncated)"

# Format based on content type
if content_type == "thinking":
Expand Down
21 changes: 20 additions & 1 deletion tests/ccbot/handlers/test_response_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,30 @@ def test_user_message_truncated_at_3000_chars(self):
assert len(parts[0]) < len(long_text)
assert len(short_parts[0]) < len(parts[0])

def test_thinking_content_truncated_at_500_chars(self):
def test_thinking_not_truncated_by_default(self):
inner = "x" * 800
text = f"{EXP_START}{inner}{EXP_END}"
parts = build_response_parts(text, is_complete=True, content_type="thinking")
assert len(parts) == 1
assert "truncated" not in parts[0].lower()
assert inner in parts[0]

def test_thinking_truncated_when_max_set(self):
inner = "x" * 800
text = f"{EXP_START}{inner}{EXP_END}"
parts = build_response_parts(
text, is_complete=True, content_type="thinking", max_thinking_chars=500
)
assert len(parts) == 1
assert "truncated" in parts[0].lower()
assert inner not in parts[0]

def test_thinking_truncated_plain_text(self):
text = "y" * 800
parts = build_response_parts(
text, is_complete=True, content_type="thinking", max_thinking_chars=500
)
assert len(parts) == 1
assert "truncated" in parts[0].lower()

def test_plain_text_single_part(self):
Expand Down
Loading