mod builder;
mod failure;
mod prompt;
mod reasoning;
mod runtime;
#[cfg(test)]
mod tests;
mod tool_execution;
pub use builder::{AgentRunTimeBuilder, AgentRuntimeConfig};
pub use failure::{AgentFailure, FailureKind};
use crate::{
checkpoint::CheckpointStorage,
error::StorageError,
middleware::MiddlewareChain,
providers::LlmProvider,
sandbox::Sandbox,
shared::{ToolCall, UserInteraction},
skills::SkillManager,
state::AgentState,
tools::ToolRegistry,
trajectory::TrajectorySink,
transcript::TranscriptStorage,
};
use prompt::PromptComposer;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use std::{
collections::HashMap,
sync::{Arc, Weak},
};
use tokio::sync::Mutex;
#[derive(Debug, Serialize, Deserialize)]
pub enum Agent {
Ready(AgentState),
Reasoning(AgentState),
ToolExecuting(AgentState, ToolRunState),
WaitingForUser(AgentState, UserInteraction),
Success(AgentState),
Interrupted(AgentState),
Fail(AgentState, AgentFailure),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolRunState {
pub calls: Vec<ToolCall>,
pub cursor: usize,
}
impl ToolRunState {
pub fn new(calls: Vec<ToolCall>) -> Self {
Self { calls, cursor: 0 }
}
}
pub struct AgentRunTime {
pub(crate) provider: Arc<dyn LlmProvider>,
pub(crate) max_iter: u32,
pub(crate) max_consecutive_fail_count: u32,
pub(crate) model: String,
pub(crate) system_prompt: Option<String>,
pub(crate) temperature: Option<f32>,
pub(crate) max_tokens: Option<u32>,
pub(crate) checkpoint_storage: Arc<dyn CheckpointStorage>,
pub(crate) transcript_storage: Arc<dyn TranscriptStorage>,
pub(crate) sandbox: Arc<dyn Sandbox>,
pub(crate) tools: ToolRegistry,
pub(crate) skills: Option<Arc<SkillManager>>,
pub(crate) prompt_composer: PromptComposer,
pub(crate) middleware: MiddlewareChain,
pub(crate) trajectory: Arc<dyn TrajectorySink>,
pub(crate) retry_backoff: Duration,
pub(crate) tool_timeout: Duration,
pub(crate) session_locks: Mutex<HashMap<String, Weak<Mutex<()>>>>,
}
#[derive(Debug, Default)]
pub(crate) struct PendingToolCall {
pub(crate) id: Option<String>,
pub(crate) name: Option<String>,
pub(crate) arguments: String,
}
pub(crate) fn storage_failure(ctx: AgentState, error: StorageError) -> Agent {
Agent::Fail(
ctx,
AgentFailure::new(FailureKind::Storage, error.to_string()),
)
}