use std::num::NonZeroU32;
use std::time::Duration;
use crate::TruncationConfig;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ModelCaps {
pub images: bool,
pub tool_search: bool,
pub reasoning: bool,
}
impl Default for ModelCaps {
fn default() -> Self {
Self {
images: true,
tool_search: false,
reasoning: false,
}
}
}
impl ModelCaps {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_images(mut self, images: bool) -> Self {
self.images = images;
self
}
#[must_use]
pub fn with_tool_search(mut self, tool_search: bool) -> Self {
self.tool_search = tool_search;
self
}
#[must_use]
pub fn with_reasoning(mut self, reasoning: bool) -> Self {
self.reasoning = reasoning;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum PromptCacheKey {
#[default]
PerLoop,
Custom(String),
Omit,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct RetryConfig {
pub max_retries: u32,
pub base_delay: Duration,
pub max_delay: Duration,
}
impl Default for RetryConfig {
fn default() -> Self {
Self {
max_retries: 5,
base_delay: Duration::from_millis(200),
max_delay: Duration::from_secs(60),
}
}
}
impl RetryConfig {
#[must_use]
pub fn new(max_retries: u32) -> Self {
Self {
max_retries,
..Self::default()
}
}
#[must_use]
pub fn none() -> Self {
Self::new(0)
}
#[must_use]
pub fn with_base_delay(mut self, delay: Duration) -> Self {
self.base_delay = delay;
self
}
#[must_use]
pub fn with_max_delay(mut self, delay: Duration) -> Self {
self.max_delay = delay;
self
}
pub(crate) fn backoff(&self, attempt: u32, jitter: f64) -> Duration {
let exponent = attempt.saturating_sub(1).min(30);
let delay = self.base_delay.saturating_mul(1_u32 << exponent);
let factor = jitter.clamp(-1.0, 1.0).mul_add(0.1, 1.0);
delay.mul_f64(factor).min(self.max_delay)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ToolSearchConfig {
pub limit: usize,
pub description: String,
}
impl Default for ToolSearchConfig {
fn default() -> Self {
Self {
limit: 8,
description: "Search for more tools by what they do. The tools found can be called \
from the next step on."
.to_string(),
}
}
}
impl ToolSearchConfig {
#[must_use]
pub fn new(limit: usize) -> Self {
Self {
limit,
..Self::default()
}
}
#[must_use]
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
#[allow(clippy::struct_excessive_bools)] pub struct LoopConfig {
pub max_iterations: Option<NonZeroU32>,
pub store: bool,
pub include_encrypted_reasoning: bool,
pub prompt_cache_key: PromptCacheKey,
pub parallel_tool_calls: bool,
pub truncation: TruncationConfig,
pub retry: RetryConfig,
pub stream: bool,
pub caps: ModelCaps,
pub tool_search: Option<ToolSearchConfig>,
}
impl Default for LoopConfig {
fn default() -> Self {
Self {
max_iterations: NonZeroU32::new(16),
store: false,
include_encrypted_reasoning: true,
prompt_cache_key: PromptCacheKey::PerLoop,
parallel_tool_calls: true,
truncation: TruncationConfig::default(),
retry: RetryConfig::default(),
stream: true,
caps: ModelCaps::default(),
tool_search: Some(ToolSearchConfig::default()),
}
}
}
impl LoopConfig {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_max_iterations(mut self, max: u32) -> Self {
self.max_iterations = NonZeroU32::new(max);
self
}
#[must_use]
pub fn with_store(mut self, store: bool) -> Self {
self.store = store;
self
}
#[must_use]
pub fn with_encrypted_reasoning(mut self, include: bool) -> Self {
self.include_encrypted_reasoning = include;
self
}
#[must_use]
pub fn with_prompt_cache_key(mut self, key: PromptCacheKey) -> Self {
self.prompt_cache_key = key;
self
}
#[must_use]
pub fn with_parallel_tool_calls(mut self, parallel: bool) -> Self {
self.parallel_tool_calls = parallel;
self
}
#[must_use]
pub fn with_truncation(mut self, truncation: TruncationConfig) -> Self {
self.truncation = truncation;
self
}
#[must_use]
pub fn with_retry(mut self, retry: RetryConfig) -> Self {
self.retry = retry;
self
}
#[must_use]
pub fn with_stream(mut self, stream: bool) -> Self {
self.stream = stream;
self
}
#[must_use]
pub fn with_caps(mut self, caps: ModelCaps) -> Self {
self.caps = caps;
self
}
#[must_use]
pub fn with_tool_search(mut self, search: Option<ToolSearchConfig>) -> Self {
self.tool_search = search;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn backoff_doubles_and_is_capped() {
let retry = RetryConfig::default().with_max_delay(Duration::from_secs(1));
assert_eq!(retry.backoff(1, 0.0), Duration::from_millis(200));
assert_eq!(retry.backoff(2, 0.0), Duration::from_millis(400));
assert_eq!(retry.backoff(3, 1.0), Duration::from_millis(880));
assert_eq!(retry.backoff(4, 0.0), Duration::from_secs(1));
assert_eq!(retry.backoff(100, -1.0), Duration::from_secs(1));
}
}