| 51 | } |
| 52 | |
| 53 | fn merge_stream_text(text_content: &mut String, incoming: &str) -> Option<String> { |
| 54 | if incoming.is_empty() { |
| 55 | return None; |
| 56 | } |
| 57 | if text_content.is_empty() { |
| 58 | text_content.push_str(incoming); |
| 59 | return Some(incoming.to_string()); |
| 60 | } |
| 61 | if incoming == text_content.as_str() || text_content.ends_with(incoming) { |
| 62 | return None; |
| 63 | } |
| 64 | // If incoming contains text_content as a prefix (incoming is the full content), |
| 65 | // replace text_content instead of appending (avoids duplicate full content) |
| 66 | if incoming.starts_with(text_content.as_str()) && incoming.len() > text_content.len() { |
| 67 | let suffix = &incoming[text_content.len()..]; |
| 68 | if !suffix.is_empty() { |
| 69 | *text_content = incoming.to_string(); |
| 70 | return Some(suffix.to_string()); |
| 71 | } |
| 72 | return None; |
| 73 | } |
| 74 | if let Some(suffix) = incoming.strip_prefix(text_content.as_str()) { |
| 75 | if suffix.is_empty() { |
| 76 | return None; |
| 77 | } |
| 78 | text_content.push_str(suffix); |
| 79 | return Some(suffix.to_string()); |
| 80 | } |
| 81 | text_content.push_str(incoming); |
| 82 | Some(incoming.to_string()) |
| 83 | } |
| 84 | |
| 85 | pub fn new(api_key: String, model: String) -> Self { |
| 86 | Self { |