fix: unify stream types with trait object

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-25 21:17:06 -03:00
parent bfeba4151e
commit bbdbdfa5be

View File

@@ -240,22 +240,24 @@ impl AsyncItemService {
let stream = tokio_stream::wrappers::ReceiverStream::new(rx); let stream = tokio_stream::wrappers::ReceiverStream::new(rx);
// If length is specified, we need to limit the stream // If length is specified, we need to limit the stream
let limited_stream = if length > 0 { // Use a trait object to ensure both branches have the same type
// We need to track how many bytes we've sent let limited_stream: std::pin::Pin<Box<dyn tokio_stream::Stream<Item = Result<tokio_util::bytes::Bytes, std::io::Error>> + Send>> =
let mut bytes_sent = 0; if length > 0 {
let limited = stream.take_while(move |result| { // We need to track how many bytes we've sent
if bytes_sent >= length { let mut bytes_sent = 0;
return false; let limited = stream.take_while(move |result| {
} if bytes_sent >= length {
if let Ok(chunk) = result { return false;
bytes_sent += chunk.len() as u64; }
} if let Ok(chunk) = result {
true bytes_sent += chunk.len() as u64;
}); }
Box::pin(limited) true
} else { });
Box::pin(stream) Box::pin(limited)
}; } else {
Box::pin(stream)
};
Ok((limited_stream, mime_type)) Ok((limited_stream, mime_type))
} }