fix: Adapt FilteringReader to use FilterChain for streaming

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-09-02 16:31:03 -03:00
parent 1cc1ccf15e
commit 765a46a8d2

View File

@@ -50,30 +50,22 @@ impl<R: Read> Read for FilteringReader<R> {
self.buffer.clear();
self.buffer_pos = 0;
// Read from the original reader
// Read from the original reader into a temporary buffer
let mut temp_buf = vec![0; buf.len()];
let bytes_read = self.reader.read(&mut temp_buf)?;
if bytes_read == 0 {
// If we're at EOF, process any remaining data in the filter chain
if let Some(chain) = &mut self.filter_chain {
let finished_data = chain.finish()?;
if !finished_data.is_empty() {
self.buffer = finished_data;
let bytes_to_copy = std::cmp::min(buf.len(), self.buffer.len());
buf[..bytes_to_copy].copy_from_slice(&self.buffer[..bytes_to_copy]);
self.buffer_pos = bytes_to_copy;
return Ok(bytes_to_copy);
}
}
return Ok(0);
}
// Process through the filter chain if it exists
if let Some(chain) = &mut self.filter_chain {
let processed_data = chain.process(&temp_buf[..bytes_read])?;
if !processed_data.is_empty() {
self.buffer = processed_data;
// Use a cursor to read the input data
let mut input_cursor = std::io::Cursor::new(&temp_buf[..bytes_read]);
// Write filtered output to our buffer
chain.filter(&mut input_cursor, &mut self.buffer)?;
if !self.buffer.is_empty() {
let bytes_to_copy = std::cmp::min(buf.len(), self.buffer.len());
buf[..bytes_to_copy].copy_from_slice(&self.buffer[..bytes_to_copy]);
self.buffer_pos = bytes_to_copy;