refactor: move filtering logic to filter plugins

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-28 20:38:57 -03:00
parent ee2a9c63ee
commit 35e2368dea

View File

@@ -433,27 +433,15 @@ impl MetaPlugin for TextMetaPlugin {
let head_bytes = self.base.options.get("head_bytes") let head_bytes = self.base.options.get("head_bytes")
.and_then(|v| v.as_u64()) .and_then(|v| v.as_u64())
.map(|v| v as usize); .map(|v| v as usize);
let head_words = self.base.options.get("head_words")
.and_then(|v| v.as_u64())
.map(|v| v as usize);
let head_lines = self.base.options.get("head_lines") let head_lines = self.base.options.get("head_lines")
.and_then(|v| v.as_u64()) .and_then(|v| v.as_u64())
.map(|v| v as usize); .map(|v| v as usize);
let tail_bytes = self.base.options.get("tail_bytes") let tail_bytes = self.base.options.get("tail_bytes")
.and_then(|v| v.as_u64()) .and_then(|v| v.as_u64())
.map(|v| v as usize); .map(|v| v as usize);
let tail_words = self.base.options.get("tail_words")
.and_then(|v| v.as_u64())
.map(|v| v as usize);
let tail_lines = self.base.options.get("tail_lines") let tail_lines = self.base.options.get("tail_lines")
.and_then(|v| v.as_u64()) .and_then(|v| v.as_u64())
.map(|v| v as usize); .map(|v| v as usize);
let line_start = self.base.options.get("line_start")
.and_then(|v| v.as_u64())
.map(|v| v as usize);
let line_end = self.base.options.get("line_end")
.and_then(|v| v.as_u64())
.map(|v| v as usize);
// Build filter string from individual parameters // Build filter string from individual parameters
let mut filter_parts = Vec::new(); let mut filter_parts = Vec::new();
@@ -469,7 +457,6 @@ impl MetaPlugin for TextMetaPlugin {
if let Some(lines) = tail_lines { if let Some(lines) = tail_lines {
filter_parts.push(format!("tail_lines({})", lines)); filter_parts.push(format!("tail_lines({})", lines));
} }
// TODO: Add support for head_words, tail_words, line_start, line_end in filter plugins
// Use the filter service to process data // Use the filter service to process data
let processed_data = if !filter_parts.is_empty() { let processed_data = if !filter_parts.is_empty() {
@@ -577,39 +564,56 @@ impl MetaPlugin for TextMetaPlugin {
let head_bytes = self.base.options.get("head_bytes") let head_bytes = self.base.options.get("head_bytes")
.and_then(|v| v.as_u64()) .and_then(|v| v.as_u64())
.map(|v| v as usize); .map(|v| v as usize);
let head_words = self.base.options.get("head_words")
.and_then(|v| v.as_u64())
.map(|v| v as usize);
let head_lines = self.base.options.get("head_lines") let head_lines = self.base.options.get("head_lines")
.and_then(|v| v.as_u64()) .and_then(|v| v.as_u64())
.map(|v| v as usize); .map(|v| v as usize);
let tail_bytes = self.base.options.get("tail_bytes") let tail_bytes = self.base.options.get("tail_bytes")
.and_then(|v| v.as_u64()) .and_then(|v| v.as_u64())
.map(|v| v as usize); .map(|v| v as usize);
let tail_words = self.base.options.get("tail_words")
.and_then(|v| v.as_u64())
.map(|v| v as usize);
let tail_lines = self.base.options.get("tail_lines") let tail_lines = self.base.options.get("tail_lines")
.and_then(|v| v.as_u64()) .and_then(|v| v.as_u64())
.map(|v| v as usize); .map(|v| v as usize);
let line_start = self.base.options.get("line_start")
.and_then(|v| v.as_u64())
.map(|v| v as usize);
let line_end = self.base.options.get("line_end")
.and_then(|v| v.as_u64())
.map(|v| v as usize);
// If we haven't determined binary status yet, do it now with whatever we have // If we haven't determined binary status yet, do it now with whatever we have
if self.is_binary_content.is_none() { if self.is_binary_content.is_none() {
if let Some(buffer) = &self.buffer { if let Some(buffer) = &self.buffer {
if !buffer.is_empty() { if !buffer.is_empty() {
// Build filter string from individual parameters
let mut filter_parts = Vec::new();
if let Some(bytes) = head_bytes {
filter_parts.push(format!("head_bytes({})", bytes));
}
if let Some(lines) = head_lines {
filter_parts.push(format!("head_lines({})", lines));
}
if let Some(bytes) = tail_bytes {
filter_parts.push(format!("tail_bytes({})", bytes));
}
if let Some(lines) = tail_lines {
filter_parts.push(format!("tail_lines({})", lines));
}
// Apply content filtering to the buffer if needed // Apply content filtering to the buffer if needed
let processed_buffer = if head_bytes.is_some() || head_words.is_some() || head_lines.is_some() { let processed_buffer = if !filter_parts.is_empty() {
self.process_head(buffer, head_bytes, head_words, head_lines) let filter_str = filter_parts.join(" | ");
} else if tail_bytes.is_some() || tail_words.is_some() || tail_lines.is_some() { let filter_service = crate::services::filter_service::FilterService::new();
self.process_tail(buffer, tail_bytes, tail_words, tail_lines) let mut filter_chain = filter_service.create_filter_chain(Some(&filter_str))
} else if line_start.is_some() || line_end.is_some() { .map_err(|e| {
self.process_line_range(buffer, line_start, line_end) log::error!("Failed to create filter chain: {}", e);
buffer.clone()
})
.unwrap_or_else(|_| buffer.clone());
// Process the data through the filter chain
filter_service.process_data(&mut filter_chain, buffer)
.and_then(|data| filter_service.finish_processing(&mut filter_chain).map(|mut finish_data| {
data.extend_from_slice(&finish_data);
data
}))
.unwrap_or_else(|e| {
log::error!("Failed to process data through filter: {}", e);
buffer.clone()
})
} else { } else {
buffer.clone() buffer.clone()
}; };