feat: add max_buffer_size to MagicFileMetaPlugin and refactor MetaPluginProgram

Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-18 07:56:56 -03:00
parent 133538881f
commit c38ae0d4a9
2 changed files with 91 additions and 80 deletions

View File

@@ -1,21 +1,21 @@
use crate::plugins::ProgramWriter;
use anyhow::{Context, Result, anyhow};
use log::*;
use std::io;
use std::io::Write;
use std::process::{Command, Stdio};
use std::process::{Command, Stdio, Child};
use which::which;
use crate::meta_plugin::MetaPlugin;
#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct MetaPluginProgram {
pub program: String,
pub args: Vec<String>,
pub supported: bool,
pub meta_name: String,
pub split_whitespace: bool,
buffer: Vec<u8>,
process: Option<Child>,
writer: Option<Box<dyn Write>>,
}
impl MetaPluginProgram {
@@ -29,7 +29,8 @@ impl MetaPluginProgram {
supported,
meta_name,
split_whitespace,
buffer: Vec::new(),
process: None,
writer: None,
}
}
}
@@ -43,8 +44,8 @@ impl MetaPlugin for MetaPluginProgram {
false
}
fn create(&self) -> Result<Box<dyn Write>> {
debug!("META: Writing using {:?}", *self);
fn initialize(&mut self, _conn: &rusqlite::Connection, _item_id: i64) -> Result<()> {
debug!("META: Initializing program plugin: {:?}", self);
let program = self.program.clone();
let args = self.args.clone();
@@ -55,6 +56,7 @@ impl MetaPlugin for MetaPluginProgram {
.args(args.clone())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.context(anyhow!(
"Problem spawning child process: {:?} {:?}",
@@ -62,58 +64,59 @@ impl MetaPlugin for MetaPluginProgram {
args
))?;
Ok(Box::new(ProgramWriter {
stdin: process.stdin.take().unwrap(),
}))
let stdin = process.stdin.take().unwrap();
self.writer = Some(Box::new(stdin));
self.process = Some(process);
Ok(())
}
fn finalize(&mut self) -> io::Result<String> {
let program = self.program.clone();
let args = self.args.clone();
debug!("META: Finalizing program plugin");
debug!("META: Executing command for finalize: {:?} {:?}", program, args);
// Close stdin to signal EOF to the process
self.writer.take();
let mut process = Command::new(program)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to spawn process: {}", e)))?;
if let Some(mut process) = self.process.take() {
let output = process.wait_with_output()
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to wait for process: {}", e)))?;
let stdin = process.stdin.as_mut().unwrap();
stdin.write_all(&self.buffer)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to write to stdin: {}", e)))?;
let output = process.wait_with_output()
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to wait for process: {}", e)))?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let trimmed_result = stdout.trim();
// For certain programs, we only want the first part before whitespace
if self.split_whitespace {
let parts: Vec<&str> = trimmed_result.split_whitespace().collect();
if !parts.is_empty() {
Ok(parts[0].to_string())
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let trimmed_result = stdout.trim();
// For certain programs, we only want the first part before whitespace
if self.split_whitespace {
let parts: Vec<&str> = trimmed_result.split_whitespace().collect();
if !parts.is_empty() {
Ok(parts[0].to_string())
} else {
Ok(trimmed_result.to_string())
}
} else {
Ok(trimmed_result.to_string())
}
} else {
Ok(trimmed_result.to_string())
let stderr = String::from_utf8_lossy(&output.stderr);
Err(io::Error::new(
io::ErrorKind::Other,
format!("Command failed: {}", stderr.trim()),
))
}
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
Err(io::Error::new(
io::ErrorKind::Other,
format!("Command failed: {}", stderr.trim()),
"No process to finalize".to_string(),
))
}
}
fn update(&mut self, data: &[u8]) {
self.buffer.extend_from_slice(data);
if let Some(ref mut writer) = self.writer {
if let Err(e) = writer.write_all(data) {
debug!("META: Failed to write to process stdin: {}", e);
}
}
}
fn meta_name(&mut self) -> String {