feat: add ExecutionMode and CliExecutor for CLI orchestration
新增功能: - ExecutionMode 枚舉 (Python/Cli/Hybrid) - EXECUTION_MODE 全局配置 - CliExecutor 結構體 實現細節: - config.rs: 添加 ExecutionMode 和相關函數 - executor.rs: 添加 CliExecutor 用於調用 Swift/Rust CLI 配置方式: - MOMENTRY_EXECUTION_MODE=python/cli/hybrid (默認: hybrid) 下一步: - 實現具體的 CLI 調用邏輯 - 添加環境變數切換測試
This commit is contained in:
@@ -2,6 +2,33 @@ use once_cell::sync::Lazy;
|
||||
use std::env;
|
||||
use std::sync::RwLock;
|
||||
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// Execution mode for processors (Python, CLI, or Hybrid)
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ExecutionMode {
|
||||
Python, // Use Python scripts (default)
|
||||
Cli, // Use Swift/Rust CLI tools
|
||||
Hybrid, // Use CLI for performance-critical processors, Python for others
|
||||
}
|
||||
|
||||
pub static EXECUTION_MODE: Lazy<ExecutionMode> = Lazy::new(|| {
|
||||
match env::var("MOMENTRY_EXECUTION_MODE")
|
||||
.unwrap_or_else(|_| "hybrid".to_string())
|
||||
.to_lowercase()
|
||||
.as_str()
|
||||
{
|
||||
"cli" => ExecutionMode::Cli,
|
||||
"python" => ExecutionMode::Python,
|
||||
_ => ExecutionMode::Hybrid,
|
||||
}
|
||||
});
|
||||
|
||||
pub fn get_execution_mode() -> ExecutionMode {
|
||||
*EXECUTION_MODE
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// Runtime switches (mutable via API, persisted in memory)
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1411,6 +1411,55 @@ impl Default for PythonExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// CLI Executor for Swift/Rust processors
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
|
||||
pub struct CliExecutor {
|
||||
cli_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl CliExecutor {
|
||||
pub fn new() -> Result<Self> {
|
||||
let cli_dir = std::env::var("MOMENTRY_CLI_DIR")
|
||||
.unwrap_or_else(|_| {
|
||||
let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
manifest.join("tools/cli").to_string_lossy().to_string()
|
||||
});
|
||||
|
||||
let cli_path = PathBuf::from(&cli_dir);
|
||||
|
||||
if !cli_path.exists() {
|
||||
anyhow::bail!(
|
||||
"CLI directory not found at {} (set MOMENTRY_CLI_DIR env var)",
|
||||
cli_dir
|
||||
);
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
cli_dir: cli_path,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn swift_cli_path(&self, name: &str) -> PathBuf {
|
||||
self.cli_dir.join("swift").join(".build").join("release").join(name)
|
||||
}
|
||||
|
||||
pub fn rust_cli_path(&self, name: &str) -> PathBuf {
|
||||
self.cli_dir.join("rust").join("target").join("release").join(name)
|
||||
}
|
||||
|
||||
pub fn cli_dir(&self) -> &PathBuf {
|
||||
&self.cli_dir
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CliExecutor {
|
||||
fn default() -> Self {
|
||||
Self::new().expect("Failed to create CliExecutor")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user