tiny-agent 0.1.0

一个小而完整的 Rust LLM Agent 运行时:可中断、可恢复、可观测、可插拔的 agent loop / A small but complete LLM agent runtime in Rust — an interruptible, resumable, observable, pluggable agent loop.
Documentation
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>,
    /// 可重试失败的退避基数(指数退避 + jitter 的 base)。设为 0 关闭退避。
    pub(crate) retry_backoff: Duration,
    /// 单个工具调用的超时时间。设为 0 时关闭工具超时。
    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()),
    )
}