diff --git a/src/core/processor/mod.rs b/src/core/processor/mod.rs index 69c4ae5e..c4936032 100644 --- a/src/core/processor/mod.rs +++ b/src/core/processor/mod.rs @@ -196,3 +196,4 @@ pub use tkg::{ RepresentativeFrameResult, TkgResult, }; pub mod tool_selector; +pub mod processor_config_v3; diff --git a/src/core/processor/processor_config_v3.rs b/src/core/processor/processor_config_v3.rs new file mode 100644 index 00000000..24691a82 --- /dev/null +++ b/src/core/processor/processor_config_v3.rs @@ -0,0 +1,158 @@ +use anyhow::{Context, Result}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::path::PathBuf; +use tracing::info; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ProcessorConfigV3 { + pub implementations: Vec, + pub default: DefaultConfig, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Implementation { + pub tool: String, + pub model: Option, + pub priority: u8, + pub enabled: bool, + pub command: CommandConfig, + #[serde(default)] + pub parameters: HashMap, + pub output_file: String, + #[serde(default = "default_timeout")] + pub timeout: u64, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct CommandConfig { + pub executable: String, + #[serde(default)] + pub script: Option, + #[serde(default)] + pub args: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DefaultConfig { + pub tool: String, + pub model: Option, +} + +fn default_timeout() -> u64 { + 3600 +} + +pub struct ProcessorConfigManager { + config: HashMap, +} + +impl ProcessorConfigManager { + pub async fn load() -> Result { + let config_path = std::path::Path::new("config/processor_tools_v3.json"); + + if !config_path.exists() { + anyhow::bail!("Config file not found: {:?}", config_path); + } + + let content = tokio::fs::read_to_string(config_path) + .await + .context("Failed to read config file")?; + + let full_config: serde_json::Value = serde_json::from_str(&content) + .context("Failed to parse config JSON")?; + + let processor_config = full_config + .get("processor_config") + .context("Missing processor_config in config")? + .clone(); + + let config: HashMap = serde_json::from_value(processor_config) + .context("Failed to parse processor configs")?; + + info!("[ConfigV3] Loaded {} processor configs", config.len()); + + Ok(Self { config }) + } + + pub fn get_processor(&self, name: &str) -> Option<&ProcessorConfigV3> { + self.config.get(name) + } + + pub fn get_implementation(&self, processor: &str, tool: &str) -> Option<&Implementation> { + self.config.get(processor)?.implementations.iter() + .find(|i| i.tool == tool) + } + + pub fn get_default_implementation(&self, processor: &str) -> Option<&Implementation> { + let proc_config = self.config.get(processor)?; + let default_tool = &proc_config.default.tool; + + proc_config.implementations.iter() + .find(|i| i.tool == *default_tool && i.enabled) + } + + pub fn resolve_command( + &self, + processor: &str, + tool: &str, + variables: &HashMap, + ) -> Result)>> { + let implementation = self.get_implementation(processor, tool) + .context(format!("Implementation not found: {}/{}", processor, tool))?; + + if !implementation.enabled { + return Ok(None); + } + + let executable = Self::resolve_variables(&implementation.command.executable, variables); + let executable_path = PathBuf::from(&executable); + + if !executable_path.exists() { + info!("[ConfigV3] Executable not found: {:?}", executable_path); + return Ok(None); + } + + let mut args = Vec::new(); + for arg in &implementation.command.args { + let resolved = Self::resolve_variables(arg, variables); + args.push(resolved); + } + + info!( + "[ConfigV3] Resolved command: {:?} {:?}", + executable_path, args + ); + + Ok(Some((executable_path, args))) + } + + fn resolve_variables(template: &str, variables: &HashMap) -> String { + let mut result = template.to_string(); + for (key, value) in variables { + let placeholder = format!("{{{}}}", key); + result = result.replace(&placeholder, value); + } + result + } + + pub fn get_all_processors(&self) -> Vec { + self.config.keys().cloned().collect() + } + + pub fn get_implementations_by_priority(&self, processor: &str) -> Vec<&Implementation> { + if let Some(proc_config) = self.config.get(processor) { + let mut implementations: Vec<_> = proc_config.implementations.iter() + .filter(|i| i.enabled) + .collect(); + implementations.sort_by_key(|i| i.priority); + implementations + } else { + Vec::new() + } + } +} + +pub async fn get_config_manager() -> Result { + ProcessorConfigManager::load().await +} diff --git a/tests/test_processor_config_v3.rs b/tests/test_processor_config_v3.rs new file mode 100644 index 00000000..80d3b839 --- /dev/null +++ b/tests/test_processor_config_v3.rs @@ -0,0 +1,54 @@ +use momentry_core::core::processor::processor_config_v3::ProcessorConfigManager; + +#[tokio::test] +async fn test_load_config() { + let manager = ProcessorConfigManager::load().await; + assert!(manager.is_ok(), "Failed to load config"); + + let manager = manager.unwrap(); + let processors = manager.get_all_processors(); + + assert!(!processors.is_empty(), "No processors found"); + println!("Processors: {:?}", processors); + + // Test POSE processor + let pose_impls = manager.get_implementations_by_priority("pose"); + assert!(!pose_impls.is_empty(), "No POSE implementations"); + + let first_impl = &pose_impls[0]; + assert_eq!(first_impl.tool, "rust", "First POSE implementation should be Rust"); + assert_eq!(first_impl.priority, 0); + assert!(first_impl.enabled); + + println!("POSE implementations: {:#?}", pose_impls); +} + +#[tokio::test] +async fn test_resolve_command() { + let manager = ProcessorConfigManager::load().await.unwrap(); + + let mut variables = std::collections::HashMap::new(); + variables.insert("video_path".to_string(), "/path/to/video.mp4".to_string()); + variables.insert("output_path".to_string(), "/path/to/output.json".to_string()); + variables.insert("file_uuid".to_string(), "test-uuid".to_string()); + + // Test Rust POSE + let result = manager.resolve_command("pose", "rust", &variables); + assert!(result.is_ok(), "Failed to resolve command"); + + if let Ok(Some((executable, args))) = result { + println!("Executable: {:?}", executable); + println!("Args: {:?}", args); + } +} + +#[tokio::test] +async fn test_get_default() { + let manager = ProcessorConfigManager::load().await.unwrap(); + + let default = manager.get_default_implementation("pose"); + assert!(default.is_some(), "No default implementation for POSE"); + + let default = default.unwrap(); + println!("Default POSE implementation: {:?}", default.tool); +}