diff --git a/poc-memory/src/agents/api.rs b/poc-memory/src/agents/api.rs index 73dab21..acdffe1 100644 --- a/poc-memory/src/agents/api.rs +++ b/poc-memory/src/agents/api.rs @@ -99,17 +99,23 @@ pub async fn call_api_with_tools( Err(format!("agent exceeded {} tool turns", max_turns)) } -/// Synchronous wrapper — creates a tokio runtime and blocks. -/// Used by the existing sync call path in knowledge.rs. +/// Synchronous wrapper — runs the async function on a dedicated thread +/// with its own tokio runtime. Safe to call from any context. pub fn call_api_with_tools_sync( agent: &str, prompt: &str, log: &dyn Fn(&str), ) -> Result { - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .map_err(|e| format!("tokio runtime: {}", e))?; - - rt.block_on(call_api_with_tools(agent, prompt, log)) + // Run on a new thread to avoid conflicts with any existing runtime + let agent = agent.to_string(); + let prompt = prompt.to_string(); + std::thread::scope(|s| { + s.spawn(|| { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .map_err(|e| format!("tokio runtime: {}", e))?; + rt.block_on(call_api_with_tools(&agent, &prompt, &|msg| eprintln!("[api] {}", msg))) + }).join().unwrap() + }) }