pub struct RichAgentContext { /* private fields */ }Expand description
扩展的 Agent 上下文
提供业务特定的功能:
- 组件输出记录
- 执行指标跟踪
- 委托所有核心功能到 CoreAgentContext
§示例
ⓘ
use mofa_foundation::agent::context::RichAgentContext;
use mofa_kernel::agent::context::CoreAgentContext;
let core_ctx = CoreAgentContext::new("execution-123");
let rich_ctx = RichAgentContext::from(core_ctx);
// 业务特定功能
rich_ctx.record_output("llm", serde_json::json!("response")).await;
rich_ctx.increment_component_calls("llm").await;
// 核心功能委托
rich_ctx.set("key", "value").await;Implementations§
Source§impl RichAgentContext
impl RichAgentContext
Sourcepub fn new(inner: AgentContext) -> RichAgentContext
pub fn new(inner: AgentContext) -> RichAgentContext
从 CoreAgentContext 创建 RichAgentContext
Sourcepub async fn record_output(&self, component: impl Into<String>, output: Value)
pub async fn record_output(&self, component: impl Into<String>, output: Value)
记录组件输出
Sourcepub async fn get_outputs(&self) -> Vec<ComponentOutput>
pub async fn get_outputs(&self) -> Vec<ComponentOutput>
获取所有组件输出
Sourcepub async fn increment_component_calls(&self, component: &str)
pub async fn increment_component_calls(&self, component: &str)
增加组件调用计数
Sourcepub async fn add_tokens(&self, tokens: u64)
pub async fn add_tokens(&self, tokens: u64)
增加 Token 使用
Sourcepub async fn increment_tool_calls(&self)
pub async fn increment_tool_calls(&self)
增加工具调用计数
Sourcepub async fn get_metrics(&self) -> ExecutionMetrics
pub async fn get_metrics(&self) -> ExecutionMetrics
获取执行指标
Sourcepub async fn duration_ms(&self) -> u64
pub async fn duration_ms(&self) -> u64
获取执行时长 (毫秒)
Sourcepub async fn get<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
pub async fn get<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
获取值
Sourcepub async fn find<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
pub async fn find<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
从父上下文查找值
Sourcepub fn execution_id(&self) -> &str
pub fn execution_id(&self) -> &str
获取执行 ID
Sourcepub fn session_id(&self) -> Option<&str>
pub fn session_id(&self) -> Option<&str>
获取会话 ID
Sourcepub fn parent(&self) -> Option<&Arc<AgentContext>>
pub fn parent(&self) -> Option<&Arc<AgentContext>>
获取父上下文
Sourcepub fn is_interrupted(&self) -> bool
pub fn is_interrupted(&self) -> bool
检查是否被中断
Sourcepub fn trigger_interrupt(&self)
pub fn trigger_interrupt(&self)
触发中断
Sourcepub fn clear_interrupt(&self)
pub fn clear_interrupt(&self)
清除中断状态
Sourcepub fn config(&self) -> &ContextConfig
pub fn config(&self) -> &ContextConfig
获取配置
Sourcepub async fn emit_event(&self, event: AgentEvent)
pub async fn emit_event(&self, event: AgentEvent)
发送事件
Sourcepub async fn subscribe(&self, event_type: &str) -> Receiver<AgentEvent>
pub async fn subscribe(&self, event_type: &str) -> Receiver<AgentEvent>
订阅事件
Sourcepub fn inner(&self) -> &AgentContext
pub fn inner(&self) -> &AgentContext
获取内部核心上下文的引用
Trait Implementations§
Source§impl AsRef<AgentContext> for RichAgentContext
impl AsRef<AgentContext> for RichAgentContext
Source§fn as_ref(&self) -> &AgentContext
fn as_ref(&self) -> &AgentContext
Converts this type into a shared reference of the (usually inferred) input type.
Source§impl Clone for RichAgentContext
impl Clone for RichAgentContext
Source§fn clone(&self) -> RichAgentContext
fn clone(&self) -> RichAgentContext
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl From<AgentContext> for RichAgentContext
impl From<AgentContext> for RichAgentContext
Source§fn from(inner: AgentContext) -> RichAgentContext
fn from(inner: AgentContext) -> RichAgentContext
Converts to this type from the input type.
Source§impl From<RichAgentContext> for AgentContext
impl From<RichAgentContext> for AgentContext
Source§fn from(rich: RichAgentContext) -> AgentContext
fn from(rich: RichAgentContext) -> AgentContext
Converts to this type from the input type.
Auto Trait Implementations§
impl Freeze for RichAgentContext
impl !RefUnwindSafe for RichAgentContext
impl Send for RichAgentContext
impl Sync for RichAgentContext
impl Unpin for RichAgentContext
impl UnsafeUnpin for RichAgentContext
impl !UnwindSafe for RichAgentContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Message for T
impl<T> Message for T
Source§fn from_boxed(m: BoxedMessage) -> Result<Self, BoxedDowncastErr>
fn from_boxed(m: BoxedMessage) -> Result<Self, BoxedDowncastErr>
Convert a BoxedMessage to this concrete type
Source§fn box_message(self, pid: &ActorId) -> Result<BoxedMessage, BoxedDowncastErr>
fn box_message(self, pid: &ActorId) -> Result<BoxedMessage, BoxedDowncastErr>
Convert this message to a BoxedMessage