From 9d60461354f251961d6292ec1210e12301e37f85 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 26 Aug 2025 17:11:27 -0300 Subject: [PATCH] feat: add is_finalized to MetaPluginResponse and remove direct db interaction from meta plugins Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) --- src/meta_plugin.rs | 16 +++++++-- src/meta_plugin/program.rs | 66 ++++++++++++++++++++++-------------- src/services/item_service.rs | 8 ++--- 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/src/meta_plugin.rs b/src/meta_plugin.rs index dd27a7c..15d5cea 100644 --- a/src/meta_plugin.rs +++ b/src/meta_plugin.rs @@ -25,6 +25,7 @@ pub struct MetaData { #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct MetaPluginResponse { pub metadata: Vec, + pub is_finalized: bool, } #[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display, strum::EnumString)] @@ -93,12 +94,18 @@ pub trait MetaPlugin { // Update the meta plugin with new data fn update(&mut self, data: &[u8]) -> MetaPluginResponse { // Default implementation does nothing - MetaPluginResponse::default() + MetaPluginResponse { + metadata: Vec::new(), + is_finalized: false, + } } fn finalize(&mut self) -> MetaPluginResponse { // Default implementation does nothing - MetaPluginResponse::default() + MetaPluginResponse { + metadata: Vec::new(), + is_finalized: true, + } } fn meta_name(&self) -> String; @@ -111,7 +118,10 @@ pub trait MetaPlugin { // Initialize the plugin fn initialize(&mut self) -> MetaPluginResponse { // Default implementation does nothing - MetaPluginResponse::default() + MetaPluginResponse { + metadata: Vec::new(), + is_finalized: false, + } } // Access to outputs mapping diff --git a/src/meta_plugin/program.rs b/src/meta_plugin/program.rs index 0d1c16a..b6a5eae 100644 --- a/src/meta_plugin/program.rs +++ b/src/meta_plugin/program.rs @@ -95,37 +95,42 @@ impl MetaPlugin for MetaPluginProgram { false } - fn initialize(&mut self, item_id: i64) -> Result { + fn initialize(&mut self) -> MetaPluginResponse { debug!("META: Initializing program plugin: {:?}", self); - // Store item ID for later use - self.item_id = Some(item_id); - let program = self.program.clone(); let args = self.args.clone(); debug!("META: Executing command: {:?} {:?}", program, args); - let mut process = Command::new(program.clone()) + let mut process = match Command::new(program.clone()) .args(args.clone()) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) - .spawn() - .context(anyhow!( - "Problem spawning child process: {:?} {:?}", - program, - args - ))?; + .spawn() + { + Ok(process) => process, + Err(e) => { + debug!("META: Failed to spawn process: {}", e); + return MetaPluginResponse { + metadata: Vec::new(), + is_finalized: true, + }; + } + }; let stdin = process.stdin.take().unwrap(); self.writer = Some(Box::new(stdin)); self.process = Some(process); - Ok(MetaPluginResponse::default()) + MetaPluginResponse { + metadata: Vec::new(), + is_finalized: false, + } } - fn finalize(&mut self) -> Result { + fn finalize(&mut self) -> MetaPluginResponse { debug!("META: Finalizing program plugin"); let mut metadata = Vec::new(); @@ -134,7 +139,16 @@ impl MetaPlugin for MetaPluginProgram { drop(self.writer.take()); // Wait for the process to complete - let output = process.wait_with_output()?; + let output = match process.wait_with_output() { + Ok(output) => output, + Err(e) => { + debug!("META: Failed to get process output: {}", e); + return MetaPluginResponse { + metadata: Vec::new(), + is_finalized: true, + }; + } + }; if output.status.success() { // Process the output @@ -149,12 +163,11 @@ impl MetaPlugin for MetaPluginProgram { debug!("META: Program output: {}", result); self.result = Some(result); - // Create metadata to be stored - if let Some(item_id) = self.item_id { - if let Some(meta) = self.create_meta(item_id, &self.meta_name, result) { - metadata.push(meta); - } - } + // Create metadata to be returned + metadata.push(MetaData { + name: self.meta_name.clone(), + value: result, + }); } } else { debug!("META: Program failed with status: {:?}", output.status); @@ -165,19 +178,22 @@ impl MetaPlugin for MetaPluginProgram { } } - Ok(MetaPluginResponse { - metadata: if metadata.is_empty() { None } else { Some(metadata) }, + MetaPluginResponse { + metadata, is_finalized: true, - }) + } } - fn update(&mut self, data: &[u8]) -> Result { + fn update(&mut self, data: &[u8]) -> MetaPluginResponse { 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); } } - Ok(PluginResponse::default()) + MetaPluginResponse { + metadata: Vec::new(), + is_finalized: false, + } } fn meta_name(&self) -> String { diff --git a/src/services/item_service.rs b/src/services/item_service.rs index 9923dc0..6e40804 100644 --- a/src/services/item_service.rs +++ b/src/services/item_service.rs @@ -285,7 +285,7 @@ impl ItemService { total_bytes += n as i64; item_out.write_all(&buffer[..n])?; - self.meta_service.process_chunk(&mut plugins, &buffer[..n], &tx); + self.meta_service.process_chunk(&mut plugins, &buffer[..n], &tx, item_id); } debug!("ITEM_SERVICE: Processed {} bytes total", total_bytes); @@ -293,7 +293,7 @@ impl ItemService { drop(item_out); debug!("ITEM_SERVICE: Finalizing meta plugins"); - self.meta_service.finalize_plugins(&mut plugins, &tx); + self.meta_service.finalize_plugins(&mut plugins, &tx, item_id); item.size = Some(total_bytes); db::update_item(&tx, item.clone())?; @@ -365,8 +365,8 @@ impl ItemService { self.meta_service .initialize_plugins(&mut plugins, &tx, item_id); self.meta_service - .process_chunk(&mut plugins, content, &tx); - self.meta_service.finalize_plugins(&mut plugins, &tx); + .process_chunk(&mut plugins, content, &tx, item_id); + self.meta_service.finalize_plugins(&mut plugins, &tx, item_id); debug!("ITEM_SERVICE: Processed MCP item through meta plugins"); item.size = Some(content.len() as i64);