pub struct AgentRuntime { /* private fields */ }Expand description
默认 Agent Runtime 实现
使用 ConcurrentSessionManager 管理会话,ToolRegistry 执行工具。 支持完整的执行生命周期:start -> run/pause/resume/stop。 集成 TaskDecomposer 进行任务分解,ExecutionMonitor 进行执行监控和纠错。 支持真实 LLM API 调用(通过 Layer1 LlmClient)。
Implementations§
Source§impl AgentRuntime
impl AgentRuntime
Sourcepub fn new(
session_manager: Arc<ConcurrentSessionManager>,
tool_registry: Arc<ToolRegistry>,
) -> Self
pub fn new( session_manager: Arc<ConcurrentSessionManager>, tool_registry: Arc<ToolRegistry>, ) -> Self
创建新的 AgentRuntime
Sourcepub fn with_permissions(
session_manager: Arc<ConcurrentSessionManager>,
tool_registry: Arc<ToolRegistry>,
permission_manager: Arc<PermissionManager>,
) -> Self
pub fn with_permissions( session_manager: Arc<ConcurrentSessionManager>, tool_registry: Arc<ToolRegistry>, permission_manager: Arc<PermissionManager>, ) -> Self
创建带权限管理的 AgentRuntime
Sourcepub fn with_decomposer(
session_manager: Arc<ConcurrentSessionManager>,
tool_registry: Arc<ToolRegistry>,
strategy: DecompositionStrategy,
) -> Self
pub fn with_decomposer( session_manager: Arc<ConcurrentSessionManager>, tool_registry: Arc<ToolRegistry>, strategy: DecompositionStrategy, ) -> Self
创建带任务分解器的 AgentRuntime
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
使用默认组件创建(带任务分解器)
Sourcepub fn with_llm_client(
session_manager: Arc<ConcurrentSessionManager>,
tool_registry: Arc<ToolRegistry>,
llm_client: Arc<LlmClient>,
) -> Self
pub fn with_llm_client( session_manager: Arc<ConcurrentSessionManager>, tool_registry: Arc<ToolRegistry>, llm_client: Arc<LlmClient>, ) -> Self
创建带 LLM 客户端的 AgentRuntime
Sourcepub fn set_permission_manager(&mut self, manager: Arc<PermissionManager>)
pub fn set_permission_manager(&mut self, manager: Arc<PermissionManager>)
设置权限管理器
Sourcepub fn set_decomposition_strategy(&mut self, strategy: DecompositionStrategy)
pub fn set_decomposition_strategy(&mut self, strategy: DecompositionStrategy)
设置任务分解策略
Sourcepub fn set_llm_client(&mut self, client: Arc<LlmClient>)
pub fn set_llm_client(&mut self, client: Arc<LlmClient>)
设置 LLM 客户端
Sourcepub fn permission_manager(&self) -> Option<&Arc<PermissionManager>>
pub fn permission_manager(&self) -> Option<&Arc<PermissionManager>>
获取权限管理器引用
Sourcepub fn task_decomposer(&self) -> Option<&TaskDecomposer>
pub fn task_decomposer(&self) -> Option<&TaskDecomposer>
获取任务分解器引用
Sourcepub fn llm_client(&self) -> Option<&Arc<LlmClient>>
pub fn llm_client(&self) -> Option<&Arc<LlmClient>>
获取 LLM 客户端引用
Sourcepub fn decompose_task(&self, task: &str) -> Layer2Result<Option<ExecutionPlan>>
pub fn decompose_task(&self, task: &str) -> Layer2Result<Option<ExecutionPlan>>
分解任务为执行计划
如果配置了任务分解器,将复杂任务分解为子任务序列。 返回执行计划,包含子任务列表和执行顺序。
Sourcepub fn create_monitor(&self, plan: ExecutionPlan) -> ExecutionMonitor
pub fn create_monitor(&self, plan: ExecutionPlan) -> ExecutionMonitor
创建执行监控器
为给定的执行计划创建监控器,用于跟踪执行进度和处理错误。
Sourcepub async fn run_with_plan(
&self,
task: &str,
config: AgentConfig,
) -> Layer2Result<AgentResult>
pub async fn run_with_plan( &self, task: &str, config: AgentConfig, ) -> Layer2Result<AgentResult>
使用执行计划运行 Agent
先分解任务,然后按照执行计划的顺序依次执行子任务。 使用执行监控器跟踪进度和错误。
Sourcepub fn session_manager(&self) -> &Arc<ConcurrentSessionManager> ⓘ
pub fn session_manager(&self) -> &Arc<ConcurrentSessionManager> ⓘ
获取会话管理器引用
Sourcepub fn tool_registry(&self) -> &Arc<ToolRegistry> ⓘ
pub fn tool_registry(&self) -> &Arc<ToolRegistry> ⓘ
获取工具注册表引用
Trait Implementations§
Source§impl AgentRuntimeTrait for AgentRuntime
impl AgentRuntimeTrait for AgentRuntime
Source§fn run<'life0, 'life1, 'async_trait>(
&'life0 self,
task: &'life1 str,
config: AgentConfig,
) -> Pin<Box<dyn Future<Output = Layer2Result<AgentResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn run<'life0, 'life1, 'async_trait>(
&'life0 self,
task: &'life1 str,
config: AgentConfig,
) -> Pin<Box<dyn Future<Output = Layer2Result<AgentResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
运行 Agent 执行完整循环,直到完成或出错。
这是同步(阻塞式)的执行方式:创建会话、运行循环、返回结果。
Source§fn run_stream<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
task: &'life1 str,
config: AgentConfig,
callback: &'life2 dyn AgentLoopCallback,
) -> Pin<Box<dyn Future<Output = Layer2Result<AgentResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn run_stream<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
task: &'life1 str,
config: AgentConfig,
callback: &'life2 dyn AgentLoopCallback,
) -> Pin<Box<dyn Future<Output = Layer2Result<AgentResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
流式运行 Agent 执行完整循环,通过回调通知每次迭代。
与 run() 类似,但在每次迭代前后通过回调通知外部调用者。
Source§fn run_stream_abortable<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
task: &'life1 str,
config: AgentConfig,
callback: &'life2 dyn AgentLoopCallback,
abort_flag: Arc<AtomicBool>,
) -> Pin<Box<dyn Future<Output = Layer2Result<AgentResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn run_stream_abortable<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
task: &'life1 str,
config: AgentConfig,
callback: &'life2 dyn AgentLoopCallback,
abort_flag: Arc<AtomicBool>,
) -> Pin<Box<dyn Future<Output = Layer2Result<AgentResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
流式运行 Agent(支持中断)。
与 run_stream 类似,但支持通过 abort_flag 中断执行。
Source§fn start<'life0, 'life1, 'async_trait>(
&'life0 self,
task: &'life1 str,
config: AgentConfig,
) -> Pin<Box<dyn Future<Output = Layer2Result<SessionId>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn start<'life0, 'life1, 'async_trait>(
&'life0 self,
task: &'life1 str,
config: AgentConfig,
) -> Pin<Box<dyn Future<Output = Layer2Result<SessionId>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
启动 Agent 并返回会话 ID(用于异步/流式执行)。
创建会话,设置为 Running 状态,但不执行循环。 调用者可以通过 send_message / submit_tool_result 与 Agent 交互。
Source§fn pause<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn pause<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
暂停正在执行的 Agent。
将状态从 Running/ToolCalling/WaitingTool 转换为 Stopped(暂停)。 在暂停状态下,Agent 不会继续执行迭代。
Source§fn resume<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn resume<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
恢复暂停的 Agent。
将状态从 Stopped 转换回 Running。
Source§fn stop<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn stop<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
停止 Agent 执行。
无论当前处于什么状态(除了 Completed/Idle),都转换到 Stopped。 这与 pause 的区别在于 stop 是终止性的,表示用户主动取消。
Source§fn status(&self, session_id: &SessionId) -> Layer2Result<AgentState>
fn status(&self, session_id: &SessionId) -> Layer2Result<AgentState>
获取 Agent 当前状态。
Source§fn send_message<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
message: &'life2 str,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn send_message<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
message: &'life2 str,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
向 Agent 发送消息。
将消息添加到会话的消息历史中。Agent 在下一次迭代时可以读取。
Source§fn submit_tool_result<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
tool_call_id: &'life2 str,
result: ToolResult,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn submit_tool_result<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
session_id: &'life1 SessionId,
tool_call_id: &'life2 str,
result: ToolResult,
) -> Pin<Box<dyn Future<Output = Layer2Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
提交工具调用结果。
当 Agent 处于 WaitingTool 状态时,外部系统可以通过此方法 提交工具执行的结果,使 Agent 能够继续执行。