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:
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user