use crate::error::LlmError;
use crate::provider::{ChatParams, DynProvider};
use super::LoopDepth;
use super::ToolRegistry;
use super::config::{ToolLoopConfig, ToolLoopResult};
use super::loop_resumable::{ToolLoopHandle, TurnResult};
pub async fn tool_loop<Ctx: LoopDepth + Send + Sync + 'static>(
provider: &dyn DynProvider,
registry: &ToolRegistry<Ctx>,
params: ChatParams,
config: ToolLoopConfig,
ctx: &Ctx,
) -> Result<ToolLoopResult, LlmError> {
let mut handle = ToolLoopHandle::new(provider, registry, params, config, ctx);
loop {
match handle.next_turn().await {
TurnResult::Yielded(turn) => turn.continue_loop(),
TurnResult::Completed(done) => {
return Ok(ToolLoopResult {
response: done.response,
iterations: done.iterations,
total_usage: done.total_usage,
termination_reason: done.termination_reason,
});
}
TurnResult::Error(err) => return Err(err.error),
}
}
}