api: fix sync wrapper to be safe from any calling context
Run the async API call on a dedicated thread with its own tokio runtime so it works whether called from a sync context or from within an existing tokio runtime (daemon). Also drops the log closure capture issue — uses a simple eprintln fallback since the closure can't cross thread boundaries. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a29b6d4c5d
commit
643f9890df
1 changed files with 14 additions and 8 deletions
|
|
@ -99,17 +99,23 @@ pub async fn call_api_with_tools(
|
||||||
Err(format!("agent exceeded {} tool turns", max_turns))
|
Err(format!("agent exceeded {} tool turns", max_turns))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Synchronous wrapper — creates a tokio runtime and blocks.
|
/// Synchronous wrapper — runs the async function on a dedicated thread
|
||||||
/// Used by the existing sync call path in knowledge.rs.
|
/// with its own tokio runtime. Safe to call from any context.
|
||||||
pub fn call_api_with_tools_sync(
|
pub fn call_api_with_tools_sync(
|
||||||
agent: &str,
|
agent: &str,
|
||||||
prompt: &str,
|
prompt: &str,
|
||||||
log: &dyn Fn(&str),
|
log: &dyn Fn(&str),
|
||||||
) -> Result<String, String> {
|
) -> Result<String, String> {
|
||||||
let rt = tokio::runtime::Builder::new_current_thread()
|
// Run on a new thread to avoid conflicts with any existing runtime
|
||||||
.enable_all()
|
let agent = agent.to_string();
|
||||||
.build()
|
let prompt = prompt.to_string();
|
||||||
.map_err(|e| format!("tokio runtime: {}", e))?;
|
std::thread::scope(|s| {
|
||||||
|
s.spawn(|| {
|
||||||
rt.block_on(call_api_with_tools(agent, prompt, log))
|
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()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue