diff --git a/apps/ai-game-creator-shell/src-tauri/src/agent/agent_runtime_client.rs b/apps/ai-game-creator-shell/src-tauri/src/agent/agent_runtime_client.rs index 2867619d3..4059aacfa 100644 --- a/apps/ai-game-creator-shell/src-tauri/src/agent/agent_runtime_client.rs +++ b/apps/ai-game-creator-shell/src-tauri/src/agent/agent_runtime_client.rs @@ -10,6 +10,7 @@ use std::process::{Command, Stdio}; use std::time::Duration; const AGENT_RUNTIME_PROGRAM_ENV: &str = "GENARRATIVE_AGENT_RUNTIME_PROGRAM"; +const AGENT_RUNTIME_PROVIDER_ENV: &str = "GENARRATIVE_AGENT_RUNTIME_PROVIDER"; const AGENT_RUNTIME_MAX_LINE_BYTES: usize = 4 * 1024 * 1024; const AGENT_RUNTIME_TIMEOUT: Duration = Duration::from_secs(120); @@ -51,8 +52,12 @@ fn request_game_creator_agent_runtime_blocking( platform_llm::LlmError::Transport(format!("创建 Agent 临时目录失败:{error}")) })?; let db = temp.path().join("agent.db"); + let provider = std::env::var(AGENT_RUNTIME_PROVIDER_ENV) + .ok() + .filter(|value| matches!(value.as_str(), "fake" | "openai")) + .unwrap_or_else(|| "openai".to_string()); let config = temp.path().join("agent.toml"); - std::fs::write(&config, "provider = \"openai\"\n").map_err(|error| { + std::fs::write(&config, format!("provider = \"{provider}\"\n")).map_err(|error| { platform_llm::LlmError::Transport(format!("创建 Agent 配置失败:{error}")) })?; let home = temp.path().join("home"); @@ -69,7 +74,7 @@ fn request_game_creator_agent_runtime_blocking( .env("TMPDIR", temp.path()) .env("AGENT_CONFIG", &config) .env("AGENT_DB", &db) - .env("AGENT_PROVIDER", "openai") + .env("AGENT_PROVIDER", &provider) .env("AGENT_MODEL", &llm.model) .env("OPENAI_BASE_URL", &llm.base_url) .env("OPENAI_API_KEY", &llm.api_key) @@ -189,6 +194,43 @@ fn request_game_creator_agent_runtime_blocking( } } +#[cfg(all(test, unix))] +mod tests { + use super::*; + use std::sync::Mutex; + + static ENV_LOCK: Mutex<()> = Mutex::new(()); + + #[test] + fn standalone_agent_fake_process_completes_text_turn() { + let _guard = ENV_LOCK.lock().expect("env lock"); + let program = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../../rust/target/debug/agent"); + assert!( + program.is_file(), + "先构建 rust agent-cli: {}", + program.display() + ); + let previous_program = std::env::var_os(AGENT_RUNTIME_PROGRAM_ENV); + let previous_provider = std::env::var_os(AGENT_RUNTIME_PROVIDER_ENV); + std::env::set_var(AGENT_RUNTIME_PROGRAM_ENV, &program); + std::env::set_var(AGENT_RUNTIME_PROVIDER_ENV, "fake"); + let llm = GameCreatorLlmConfig::default(); + let request = platform_llm::LlmRunRequest::single_turn("系统", "请简短回答:你好"); + let response = request_game_creator_agent_runtime_blocking(&llm, request) + .expect("独立 Agent 应完成文本回合"); + assert_eq!(response.text, "fake provider complete"); + match previous_program { + Some(value) => std::env::set_var(AGENT_RUNTIME_PROGRAM_ENV, value), + None => std::env::remove_var(AGENT_RUNTIME_PROGRAM_ENV), + } + match previous_provider { + Some(value) => std::env::set_var(AGENT_RUNTIME_PROVIDER_ENV, value), + None => std::env::remove_var(AGENT_RUNTIME_PROVIDER_ENV), + } + } +} + fn write_rpc( stdin: &mut impl Write, value: serde_json::Value, diff --git a/rust/crates/agent-cli/src/main.rs b/rust/crates/agent-cli/src/main.rs index 8b502e7ab..edd206ad7 100644 --- a/rust/crates/agent-cli/src/main.rs +++ b/rust/crates/agent-cli/src/main.rs @@ -437,19 +437,16 @@ fn configure_host_with_fake_call_id( ) -> Result> { match config.provider().as_str() { "fake" => { - host = if let Some(call_id) = fake_call_id { - host.with_provider( - Arc::new(agent_provider_fake::FakeProvider::tool_then_text( - call_id, - "echo", - json!({"text": "hello from fake provider"}), - "fake provider complete", - )), - "fake", - ) - } else { - host.with_fake_provider() - } + let call_id = fake_call_id.unwrap_or("echo-call-1"); + host = host.with_provider( + Arc::new(agent_provider_fake::FakeProvider::tool_then_text( + call_id, + "echo", + json!({"text": "hello from fake provider"}), + "fake provider complete", + )), + config.model(), + ) } "openai" => { let model = config.model();