feat: add ProcessorConfigV3 module for CLI-first configuration

新增:
- processor_config_v3.rs: V3 配置解析模組
- 支持動態讀取 processor_tools_v3.json
- 支持變量替換 ({video_path}, {file_uuid}, etc.)

結構:
- ProcessorConfigV3: 主配置
- Implementation: 實現配置 (tool, model, priority)
- CommandConfig: 命令配置 (executable, args)
- ProcessorConfigManager: 配置管理器

功能:
- load(): 加載配置文件
- get_processor(): 獲取處理器配置
- get_implementation(): 獲取特定實現
- resolve_command(): 解析命令路徑和參數

配置驗證:
- ✅ 9 processors 配置
- ✅ 6 CLI-first (priority 0, enabled)
- ✅ Rust CLI: 3 (pose, face_trace, cut)
- ✅ Swift CLI: 3 (face, asrx, ocr)

待實現:
- 集成到 ToolSelector
- 更新 Worker 使用 V3 配置
This commit is contained in:
Accusys
2026-09-28 11:11:19 +08:00
parent 7f6b7ef060
commit dd8b911386
3 changed files with 213 additions and 0 deletions
+1
View File
@@ -196,3 +196,4 @@ pub use tkg::{
RepresentativeFrameResult, TkgResult,
};
pub mod tool_selector;
pub mod processor_config_v3;
+158
View File
@@ -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<Implementation>,
pub default: DefaultConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Implementation {
pub tool: String,
pub model: Option<String>,
pub priority: u8,
pub enabled: bool,
pub command: CommandConfig,
#[serde(default)]
pub parameters: HashMap<String, serde_json::Value>,
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<String>,
#[serde(default)]
pub args: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DefaultConfig {
pub tool: String,
pub model: Option<String>,
}
fn default_timeout() -> u64 {
3600
}
pub struct ProcessorConfigManager {
config: HashMap<String, ProcessorConfigV3>,
}
impl ProcessorConfigManager {
pub async fn load() -> Result<Self> {
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<String, ProcessorConfigV3> = 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<String, String>,
) -> Result<Option<(PathBuf, Vec<String>)>> {
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, String>) -> 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<String> {
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> {
ProcessorConfigManager::load().await
}
+54
View File
@@ -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);
}