Skip to content

Comments

feat(twitch): truncate messages to 500-char limit instead of splitting#118

Open
PyRo1121 wants to merge 1 commit intospacedriveapp:mainfrom
PyRo1121:feat/twitch-message-truncation
Open

feat(twitch): truncate messages to 500-char limit instead of splitting#118
PyRo1121 wants to merge 1 commit intospacedriveapp:mainfrom
PyRo1121:feat/twitch-message-truncation

Conversation

@PyRo1121
Copy link
Contributor

Summary

  • Replace split_message() with truncate_message() so Twitch responses are a single message capped at 500 characters, instead of flooding chat with multiple chunked messages
  • When truncated, cuts at the last word boundary and appends to indicate content was trimmed

Problem

Long LLM responses were split into multiple sequential Twitch chat messages (each up to 500 chars). This created chat spam — a single response could produce 3-5+ rapid-fire messages, disrupting the channel and making the bot obnoxious.

Solution

Single-message truncation with a clean word-boundary cut:

fn truncate_message(text: &str, max_len: usize) -> String {
    if text.len() <= max_len {
        return text.to_string();
    }
    let budget = max_len.saturating_sub("…".len());
    let candidate = &text[..text.floor_char_boundary(budget)];
    let cut = candidate
        .rfind(|c: char| c.is_whitespace())
        .unwrap_or(candidate.len());
    format!("{}…", candidate[..cut].trim_end())
}
  • Uses floor_char_boundary() for safe UTF-8 handling (no panics on multi-byte characters)
  • Falls back to hard cut if no word boundary found within budget

Scope

All 8 outbound code paths now use truncation:

Response Type Before After
Text Multi-chunk split Single truncated message
RichMessage Multi-chunk split Single truncated message
ThreadReply Multi-chunk split Single truncated message
File Sent raw (could exceed limit) Truncated
Ephemeral Sent raw (could exceed limit) Truncated
ScheduledMessage Sent raw (could exceed limit) Truncated
broadcast(Text) Multi-chunk split Single truncated message
broadcast(RichMessage) Multi-chunk split Single truncated message

Testing

  • LSP diagnostics: zero errors
  • The MAX_MESSAGE_LENGTH constant (500) is unchanged and used consistently across all paths

…plitting into chunks

Replace split_message() with truncate_message() to send a single truncated
message rather than flooding chat with multiple chunks when responses exceed
Twitch's 500-character limit. Truncation cuts at the last word boundary and
appends '…' to indicate content was trimmed.

Applies to all 8 outbound code paths: Text, RichMessage, ThreadReply, File,
Ephemeral, ScheduledMessage, and both broadcast variants.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant