50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
use std::{future::Future, time::Duration};
|
|
|
|
use tokio::time::timeout;
|
|
|
|
use crate::error::PlatformAgentError;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct FunctionAgentLimits {
|
|
pub max_tool_calls: usize,
|
|
pub timeout_ms: u64,
|
|
}
|
|
|
|
impl Default for FunctionAgentLimits {
|
|
fn default() -> Self {
|
|
Self {
|
|
max_tool_calls: 8,
|
|
timeout_ms: 30_000,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl FunctionAgentLimits {
|
|
pub fn validate(&self) -> Result<(), PlatformAgentError> {
|
|
if self.max_tool_calls == 0 {
|
|
return Err(PlatformAgentError::InvalidInput(
|
|
"Agent max_tool_calls 必须大于 0".to_string(),
|
|
));
|
|
}
|
|
if self.timeout_ms == 0 {
|
|
return Err(PlatformAgentError::InvalidInput(
|
|
"Agent timeout_ms 必须大于 0".to_string(),
|
|
));
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn run_with_timeout<F, T>(&self, future: F) -> Result<T, PlatformAgentError>
|
|
where
|
|
F: Future<Output = Result<T, PlatformAgentError>>,
|
|
{
|
|
self.validate()?;
|
|
timeout(Duration::from_millis(self.timeout_ms), future)
|
|
.await
|
|
.map_err(|_| PlatformAgentError::Timeout {
|
|
timeout_ms: self.timeout_ms,
|
|
})?
|
|
}
|
|
}
|