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