feat: integrate V3 config into ToolSelector
新增: - select_tool_v3(): 支持 V3 配置格式 - build_variables(): 構建變量映射 - SelectedTool 新增 args 和 timeout 字段 改進: - select_tool() 優先使用 V3 配置 - V3 失敗自動降級到 V2 - 保留 V2 作為 fallback 流程: 1. 嘗試 select_tool_v3() 2. 成功 → 返回 V3 工具 3. 失敗 → 降級到 select_tool_v2() SelectedTool: - name: 工具名稱 - path: 執行路徑 - args: 參數列表 (新增) - parameters: 配置參數 - timeout: 超時時間 (新增) 下一步: - 更新 Worker 使用新 SelectedTool - 測試完整流程
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use crate::core::admin_config::{self, ProcessorConfig, ToolConfig};
|
||||
use crate::core::processor::processor_config_v3;
|
||||
use anyhow::{Context, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use tracing::info;
|
||||
|
||||
@@ -15,6 +17,19 @@ impl ToolSelector {
|
||||
}
|
||||
|
||||
pub async fn select_tool(&self, executor: &super::PythonExecutor) -> Result<Option<SelectedTool>> {
|
||||
match self.select_tool_v3().await {
|
||||
Ok(Some(tool)) => {
|
||||
info!("[{}] Selected tool via V3: {}", self.processor_name, tool.name);
|
||||
return Ok(Some(tool));
|
||||
}
|
||||
Ok(None) => {
|
||||
info!("[{}] No tool found via V3, falling back to V2", self.processor_name);
|
||||
}
|
||||
Err(e) => {
|
||||
info!("[{}] V3 config failed: {}, falling back to V2", self.processor_name, e);
|
||||
}
|
||||
}
|
||||
|
||||
let config = admin_config::get_processor_config(&self.processor_name)
|
||||
.await
|
||||
.context("Failed to get processor config")?;
|
||||
@@ -62,6 +77,63 @@ impl ToolSelector {
|
||||
info!("[{}] No available tool found", self.processor_name);
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub async fn select_tool_v3(&self) -> Result<Option<SelectedTool>> {
|
||||
let manager = processor_config_v3::get_config_manager().await?;
|
||||
|
||||
let implementations = manager.get_implementations_by_priority(&self.processor_name);
|
||||
|
||||
if implementations.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let variables = self.build_variables();
|
||||
|
||||
for impl_ in implementations {
|
||||
if !impl_.enabled {
|
||||
continue;
|
||||
}
|
||||
|
||||
match manager.resolve_command(&self.processor_name, &impl_.tool, &variables) {
|
||||
Ok(Some((executable, args))) => {
|
||||
info!(
|
||||
"[ToolSelector V3] Selected: {} (priority {}, timeout {}s)",
|
||||
impl_.tool, impl_.priority, impl_.timeout
|
||||
);
|
||||
|
||||
return Ok(Some(SelectedTool {
|
||||
name: impl_.tool.clone(),
|
||||
path: executable,
|
||||
args,
|
||||
parameters: impl_.parameters.clone(),
|
||||
timeout: impl_.timeout,
|
||||
}));
|
||||
}
|
||||
Ok(None) => {
|
||||
info!("[ToolSelector V3] Tool {} not available, trying next", impl_.tool);
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
info!("[ToolSelector V3] Failed to resolve {}: {}", impl_.tool, e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn build_variables(&self) -> HashMap<String, String> {
|
||||
let mut vars = HashMap::new();
|
||||
|
||||
if let Ok(output_dir) = std::env::var("MOMENTRY_OUTPUT_DIR") {
|
||||
vars.insert("output_dir".to_string(), output_dir);
|
||||
} else {
|
||||
vars.insert("output_dir".to_string(), "/Users/accusys/momentry/output".to_string());
|
||||
}
|
||||
|
||||
vars
|
||||
}
|
||||
|
||||
fn find_tool(&self, tool_name: &str, executor: &super::PythonExecutor) -> Option<PathBuf> {
|
||||
match (self.processor_name.as_str(), tool_name) {
|
||||
@@ -197,5 +269,19 @@ impl ToolSelector {
|
||||
pub struct SelectedTool {
|
||||
pub name: String,
|
||||
pub path: PathBuf,
|
||||
pub args: Vec<String>,
|
||||
pub parameters: serde_json::Map<String, serde_json::Value>,
|
||||
pub timeout: u64,
|
||||
}
|
||||
|
||||
impl Default for SelectedTool {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: String::new(),
|
||||
path: PathBuf::new(),
|
||||
args: Vec::new(),
|
||||
parameters: serde_json::Map::new(),
|
||||
timeout: 3600,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user