Skip to main content

tiny_agent/core/
mod.rs

1mod builder;
2mod failure;
3mod prompt;
4mod reasoning;
5mod runtime;
6#[cfg(test)]
7mod tests;
8mod tool_execution;
9
10pub use builder::{
11    AgentRunTimeBuilder, AgentRuntimeConfig, AgentRuntimeConfigInput, AgentRuntimeOptions,
12    AgentRuntimeStorage,
13};
14pub use failure::{AgentFailure, FailureKind};
15
16use crate::{
17    checkpoint::CheckpointStorage,
18    error::StorageError,
19    messages::MessageChannel,
20    middleware::MiddlewareChain,
21    providers::LlmProvider,
22    sandbox::Sandbox,
23    shared::{ToolCall, UserInteraction},
24    skills::SkillManager,
25    state::AgentState,
26    tools::ToolRegistry,
27    transcript::TranscriptStorage,
28};
29use prompt::PromptComposer;
30use serde::{Deserialize, Serialize};
31use std::time::Duration;
32use std::{
33    collections::HashMap,
34    sync::{Arc, Weak},
35};
36use tokio::sync::Mutex;
37
38#[derive(Debug, Serialize, Deserialize)]
39pub enum Agent {
40    Ready(AgentState),
41    Reasoning(AgentState),
42    ToolExecuting(AgentState, ToolRunState),
43    WaitingForUser(AgentState, UserInteraction),
44    Success(AgentState),
45    Interrupted(AgentState),
46    Fail(AgentState, AgentFailure),
47}
48
49#[derive(Debug)]
50pub struct AgentTurn {
51    pub session_id: String,
52    pub outcome: Agent,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct ToolRunState {
57    pub calls: Vec<ToolCall>,
58    pub cursor: usize,
59}
60
61impl ToolRunState {
62    pub fn new(calls: Vec<ToolCall>) -> Self {
63        Self { calls, cursor: 0 }
64    }
65}
66
67pub struct AgentRunTime {
68    pub(crate) provider: Arc<dyn LlmProvider>,
69    pub(crate) max_iter: u32,
70    pub(crate) max_consecutive_fail_count: u32,
71    pub(crate) model: String,
72    pub(crate) system_prompt: Option<String>,
73    pub(crate) temperature: Option<f32>,
74    pub(crate) max_tokens: Option<u32>,
75    pub(crate) checkpoint_storage: Arc<dyn CheckpointStorage>,
76    pub(crate) transcript_storage: Arc<dyn TranscriptStorage>,
77    pub(crate) sandbox: Arc<dyn Sandbox>,
78    pub(crate) tools: ToolRegistry,
79    pub(crate) skills: Option<Arc<SkillManager>>,
80    pub(crate) prompt_composer: PromptComposer,
81    pub(crate) middleware: MiddlewareChain,
82    /// 对前端的实时消息通道(见 [`crate::messages`])。
83    pub(crate) messages: MessageChannel,
84    /// 后台工具任务登记表(asynchronous 工具 + `check`/`kill` 按 id 查回,见 [`crate::tasks`])。
85    pub(crate) tasks: crate::tasks::TaskManager,
86    /// 可重试失败的退避基数(指数退避 + jitter 的 base)。设为 0 关闭退避。
87    pub(crate) retry_backoff: Duration,
88    /// 单个工具调用的超时时间。设为 0 时关闭工具超时。
89    pub(crate) tool_timeout: Duration,
90    pub(crate) session_locks: Mutex<HashMap<String, Weak<Mutex<()>>>>,
91}
92
93#[derive(Debug, Default)]
94pub(crate) struct PendingToolCall {
95    pub(crate) id: Option<String>,
96    pub(crate) name: Option<String>,
97    pub(crate) arguments: String,
98}
99
100pub(crate) fn storage_failure(ctx: AgentState, error: StorageError) -> Agent {
101    Agent::Fail(
102        ctx,
103        AgentFailure::new(FailureKind::Storage, error.to_string()),
104    )
105}