Skip to main content

ai_agents_runtime/
lib.rs

1//! Runtime agent and builder for AI Agents framework
2
3mod builder;
4pub mod optimization;
5mod runtime;
6mod streaming;
7mod turn_context;
8
9pub mod orchestration;
10pub mod spawner;
11pub mod spec;
12
13pub use builder::AgentBuilder;
14pub use optimization::{
15    AwaitBeforeNextTurn, BackgroundOverflowPolicy, MainResponseDraft, MaintenanceMode,
16    MaintenanceTaskPolicy, PostTurnOptimizationConfig, RuntimeBranch, RuntimeBranchOutcome,
17    RuntimeBranchResult, RuntimeBranchStatus, RuntimeCommitBehavior, RuntimeConfig,
18    RuntimeOptimizationConfig, RuntimeOptimizationKind, RuntimeTaskPriority, RuntimeTaskPurpose,
19    ScheduledBranchSet, SkillCandidate, StreamBranchBuffer, StreamingDraftResult,
20    StreamingOptimizationPolicy, TurnBranchScheduler, TurnOptimizationContext,
21};
22pub use runtime::{RuntimeAgent, RuntimeControlHandle};
23pub use streaming::{StreamChunk, StreamingConfig};
24pub use turn_context::TurnActorContext;
25
26pub use ai_agents_core::{AgentInfo, AgentResponse, Result, ToolCall};
27
28// Retry only transient Windows SQLite sharing violations so test cleanup remains strict for every other error.
29#[cfg(test)]
30pub(crate) async fn remove_sqlite_test_directory(path: &std::path::Path) -> std::io::Result<()> {
31    const MAX_ATTEMPTS: usize = 20;
32    const RETRY_DELAY: std::time::Duration = std::time::Duration::from_millis(50);
33
34    for attempt in 0..MAX_ATTEMPTS {
35        match std::fs::remove_dir_all(path) {
36            Ok(()) => return Ok(()),
37            Err(error)
38                if cfg!(windows)
39                    && error.raw_os_error() == Some(32)
40                    && attempt + 1 < MAX_ATTEMPTS =>
41            {
42                tokio::time::sleep(RETRY_DELAY).await;
43            }
44            Err(error) => return Err(error),
45        }
46    }
47
48    unreachable!("the final cleanup attempt always returns")
49}
50
51use async_trait::async_trait;
52use serde::{Deserialize, Serialize};
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct ParallelToolsConfig {
56    #[serde(default = "default_parallel_enabled")]
57    pub enabled: bool,
58    #[serde(default = "default_max_parallel")]
59    pub max_parallel: usize,
60}
61
62fn default_parallel_enabled() -> bool {
63    true
64}
65
66fn default_max_parallel() -> usize {
67    5
68}
69
70impl Default for ParallelToolsConfig {
71    fn default() -> Self {
72        Self {
73            enabled: true,
74            max_parallel: 5,
75        }
76    }
77}
78
79#[async_trait]
80pub trait Agent: Send + Sync {
81    async fn chat(&self, input: &str) -> Result<AgentResponse>;
82    fn info(&self) -> AgentInfo;
83    async fn reset(&self) -> Result<()>;
84}