From 11319c04939ff8d29bf878d0ade27bb0df713da5 Mon Sep 17 00:00:00 2001 From: Accusys Date: Sun, 27 Sep 2026 13:19:11 +0800 Subject: [PATCH] feat: add ExecutionMode and CliExecutor for CLI orchestration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增功能: - ExecutionMode 枚舉 (Python/Cli/Hybrid) - EXECUTION_MODE 全局配置 - CliExecutor 結構體 實現細節: - config.rs: 添加 ExecutionMode 和相關函數 - executor.rs: 添加 CliExecutor 用於調用 Swift/Rust CLI 配置方式: - MOMENTRY_EXECUTION_MODE=python/cli/hybrid (默認: hybrid) 下一步: - 實現具體的 CLI 調用邏輯 - 添加環境變數切換測試 --- src/core/config.rs | 27 +++++++++++++++++++ src/core/processor/executor.rs | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/core/config.rs b/src/core/config.rs index 1a54eb0d..d3485658 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -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 = 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) // ───────────────────────────────────────────────────────────── diff --git a/src/core/processor/executor.rs b/src/core/processor/executor.rs index 9850254c..f8e5700e 100644 --- a/src/core/processor/executor.rs +++ b/src/core/processor/executor.rs @@ -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 { + 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::*;