feat: add ToolSelector module for config-driven tool selection
新增: - ToolSelector 模塊 - 配置驅動的工具選擇 - 優先級排序 - 自動故障轉移 功能: - 從配置讀取工具列表 - 按優先級選擇可用工具 - 支持參數配置 下一步: - 替換 POSE 處理器 hard-coded 邏輯 - 其他處理器遷移
This commit is contained in:
@@ -195,3 +195,4 @@ pub use tkg::{
|
||||
build_tkg, query_auto_representative_frame, FrameTraceInfo, MainIdentityInfo,
|
||||
RepresentativeFrameResult, TkgResult,
|
||||
};
|
||||
pub mod tool_selector;
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
use crate::core::admin_config::{self, ProcessorConfig, ToolConfig};
|
||||
use anyhow::{Context, Result};
|
||||
use std::path::PathBuf;
|
||||
use tracing::info;
|
||||
|
||||
pub struct ToolSelector {
|
||||
processor_name: String,
|
||||
}
|
||||
|
||||
impl ToolSelector {
|
||||
pub fn new(processor_name: &str) -> Self {
|
||||
Self {
|
||||
processor_name: processor_name.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn select_tool(&self, executor: &super::PythonExecutor) -> Result<Option<SelectedTool>> {
|
||||
let config = admin_config::get_processor_config(&self.processor_name)
|
||||
.await
|
||||
.context("Failed to get processor config")?;
|
||||
|
||||
if !config.enabled {
|
||||
info!("[{}] Processor disabled in config", self.processor_name);
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let mut tools = config.tools.clone();
|
||||
tools.sort_by_key(|t| t.priority);
|
||||
|
||||
for tool in tools {
|
||||
if !tool.enabled {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(tool_path) = self.find_tool(&tool.name, executor) {
|
||||
info!(
|
||||
"[{}] Selected tool: {} (priority {})",
|
||||
self.processor_name, tool.name, tool.priority
|
||||
);
|
||||
return Ok(Some(SelectedTool {
|
||||
name: tool.name.clone(),
|
||||
path: tool_path,
|
||||
parameters: tool.parameters.clone(),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(default) = config.default_tool {
|
||||
if let Some(tool_path) = self.find_tool(&default, executor) {
|
||||
info!(
|
||||
"[{}] Using default tool: {}",
|
||||
self.processor_name, default
|
||||
);
|
||||
return Ok(Some(SelectedTool {
|
||||
name: default.clone(),
|
||||
path: tool_path,
|
||||
parameters: serde_json::Map::new(),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
info!("[{}] No available tool found", self.processor_name);
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn find_tool(&self, tool_name: &str, executor: &super::PythonExecutor) -> Option<PathBuf> {
|
||||
match tool_name {
|
||||
"mediapipe" => {
|
||||
let script = executor.script_path("pose_processor_mediapipe.py");
|
||||
if script.exists() {
|
||||
Some(script)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
"rust" => executor.rust_binary_path("rust_pose"),
|
||||
"swift" => executor.swift_binary_path("swift_pose"),
|
||||
"yolov8" => {
|
||||
let script = executor.script_path("pose_processor_mps.py");
|
||||
if script.exists() {
|
||||
Some(script)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
"python" => {
|
||||
let script = executor.script_path("pose_processor.py");
|
||||
if script.exists() {
|
||||
Some(script)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SelectedTool {
|
||||
pub name: String,
|
||||
pub path: PathBuf,
|
||||
pub parameters: serde_json::Map<String, serde_json::Value>,
|
||||
}
|
||||
Reference in New Issue
Block a user