Skip to content
Merged
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
22 changes: 8 additions & 14 deletions src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,31 +217,25 @@ async fn stream_prompt(
text_buf.push_str("⚠️ _Session expired, starting fresh..._\n\n");
}

// Spawn edit-streaming task
// Spawn edit-streaming task — only edits the single message, never sends new ones.
// Long content is truncated during streaming; final multi-message split happens after.
let edit_handle = {
let ctx = ctx.clone();
let mut buf_rx = buf_rx.clone();
tokio::spawn(async move {
let mut last_content = String::new();
let mut current_edit_msg = msg_id;
loop {
tokio::time::sleep(std::time::Duration::from_millis(1500)).await;
if buf_rx.has_changed().unwrap_or(false) {
let content = buf_rx.borrow_and_update().clone();
if content != last_content {
if content.len() > 1900 {
let chunks = format::split_message(&content, 1900);
if let Some(first) = chunks.first() {
let _ = edit(&ctx, channel, current_edit_msg, first).await;
}
for chunk in chunks.iter().skip(1) {
if let Ok(new_msg) = channel.say(&ctx.http, chunk).await {
current_edit_msg = new_msg.id;
}
}
let display = if content.chars().count() > 1900 {
let truncated = format::truncate_chars(&content, 1900);
format!("{truncated}…")
} else {
let _ = edit(&ctx, channel, current_edit_msg, &content).await;
}
content.clone()
};
let _ = edit(&ctx, channel, msg_id, &display).await;
last_content = content;
}
}
Expand Down
34 changes: 26 additions & 8 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
/// Split text into chunks at line boundaries, each <= limit chars.
/// Split text into chunks at line boundaries, each <= limit Unicode characters (UTF-8 safe).
/// Discord's message limit counts Unicode characters, not bytes.
pub fn split_message(text: &str, limit: usize) -> Vec<String> {
if text.len() <= limit {
if text.chars().count() <= limit {
return vec![text.to_string()];
}

let mut chunks = Vec::new();
let mut current = String::new();
let mut current_len: usize = 0;

for line in text.split('\n') {
let line_chars = line.chars().count();
// +1 for the newline
if !current.is_empty() && current.len() + line.len() + 1 > limit {
if !current.is_empty() && current_len + line_chars + 1 > limit {
chunks.push(current);
current = String::new();
current_len = 0;
}
if !current.is_empty() {
current.push('\n');
current_len += 1;
}
// If a single line exceeds limit, hard-split it
if line.len() > limit {
for chunk in line.as_bytes().chunks(limit) {
if !current.is_empty() {
// If a single line exceeds limit, hard-split on char boundaries
if line_chars > limit {
for ch in line.chars() {
if current_len + 1 > limit {
chunks.push(current);
current = String::new();
current_len = 0;
}
current = String::from_utf8_lossy(chunk).to_string();
current.push(ch);
current_len += 1;
}
} else {
current.push_str(line);
current_len += line_chars;
}
}
if !current.is_empty() {
chunks.push(current);
}
chunks
}

/// Truncate a string to at most `limit` Unicode characters.
/// Discord's message limit counts Unicode characters, not bytes.
pub fn truncate_chars(s: &str, limit: usize) -> &str {
match s.char_indices().nth(limit) {
Some((idx, _)) => &s[..idx],
None => s,
}
}