pub struct AgentState {
pub messages: Vec<Message>,
pub iterations: usize,
pub total_tool_calls: usize,
pub output_tokens: usize,
pub reasoning_tokens: usize,
pub compact_count: usize,
pub stop_reason: Option<StopReason>,
pub last_response: Option<ChatResponse>,
}Expand description
Agent 类型化状态。
替代 HashMap<String, Value> 中通过 StateKey<T> 存取的模式。
所有字段编译期可见,零运行时类型检查开销。
Fields§
§messages: Vec<Message>消息历史(Assistant + Tool + User)
iterations: usize当前迭代轮次
total_tool_calls: usize累计工具调用总数
output_tokens: usize累计输出 Token 数(Text,不含 Thinking)
reasoning_tokens: usize累计推理 Token 数(Thinking,不含 Text)
compact_count: usize累计压缩次数
stop_reason: Option<StopReason>停止原因(终态)
last_response: Option<ChatResponse>最后一次 LLM 响应
Implementations§
Source§impl AgentState
impl AgentState
Sourcepub fn from_messages(messages: Vec<Message>) -> Self
pub fn from_messages(messages: Vec<Message>) -> Self
从初始消息列表创建 AgentState。
Sourcepub fn estimated_context_tokens(&self) -> usize
pub fn estimated_context_tokens(&self) -> usize
实时派生上下文 Token 数(从 messages 估算)。 供 BudgetCondition 判断是否需要压缩。
Sourcepub fn reached_max(&self, max_iterations: usize) -> bool
pub fn reached_max(&self, max_iterations: usize) -> bool
检查是否已达到最大迭代轮次。
Sourcepub fn exceeded_output(&self, max: Option<u32>) -> bool
pub fn exceeded_output(&self, max: Option<u32>) -> bool
检查是否超过总输出预算。
Sourcepub fn exceeded_reasoning(&self, max: Option<u32>) -> bool
pub fn exceeded_reasoning(&self, max: Option<u32>) -> bool
检查是否超过总推理预算。
Sourcepub fn exceeded_output_with_extra(&self, max: Option<u32>, extra: usize) -> bool
pub fn exceeded_output_with_extra(&self, max: Option<u32>, extra: usize) -> bool
检查加上额外 Token 后是否超过总输出预算。
用于 Mutation 未 apply 时的预判(节点 emit Mutation 之前)。
Sourcepub fn exceeded_reasoning_with_extra(
&self,
max: Option<u32>,
extra: usize,
) -> bool
pub fn exceeded_reasoning_with_extra( &self, max: Option<u32>, extra: usize, ) -> bool
检查加上额外 Token 后是否超过总推理预算。
Sourcepub fn is_terminal(&self) -> bool
pub fn is_terminal(&self) -> bool
检查是否已终止(有 stop_reason)。
Sourcepub fn messages_ref(&self) -> &[Message]
pub fn messages_ref(&self) -> &[Message]
获取当前消息引用。
Trait Implementations§
Source§impl Clone for AgentState
impl Clone for AgentState
Source§fn clone(&self) -> AgentState
fn clone(&self) -> AgentState
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AgentState
impl Debug for AgentState
Source§impl Default for AgentState
impl Default for AgentState
Source§fn default() -> AgentState
fn default() -> AgentState
Returns the “default value” for a type. Read more
Source§impl<'de> Deserialize<'de> for AgentState
impl<'de> Deserialize<'de> for AgentState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl LeafNode<AgentState> for PostLLMGuard
impl LeafNode<AgentState> for PostLLMGuard
Source§fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
执行节点逻辑。
Source§impl LeafNode<AgentState> for CompactorNode
impl LeafNode<AgentState> for CompactorNode
Source§fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
执行节点逻辑。
Source§impl LeafNode<AgentState> for BudgetCondition
impl LeafNode<AgentState> for BudgetCondition
Source§fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
执行节点逻辑。
Source§impl LeafNode<AgentState> for LLMNode
impl LeafNode<AgentState> for LLMNode
Source§fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
执行节点逻辑。
Source§impl LeafNode<AgentState> for ToolNode
impl LeafNode<AgentState> for ToolNode
Source§fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn execute<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut LeafContext<'life2, AgentState>,
) -> Pin<Box<dyn Future<Output = Result<(), GraphError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
执行节点逻辑。
Source§impl MergeStrategy<AgentState> for AgentStateMerge
impl MergeStrategy<AgentState> for AgentStateMerge
Source§fn merge(branches: Vec<AgentState>) -> Result<AgentState, WorkflowError>
fn merge(branches: Vec<AgentState>) -> Result<AgentState, WorkflowError>
合并多个并行分支的状态。 Read more
Source§fn default_instance() -> Self
fn default_instance() -> Self
创建策略的默认实例(供 ParallelNodeBuilder 使用)。
对于无状态策略(如 StateMerge、LastWriteWins),直接返回自身。
Source§impl Serialize for AgentState
impl Serialize for AgentState
Source§impl StateMutation<AgentState> for AgentMutation
impl StateMutation<AgentState> for AgentMutation
Source§impl WorkflowState for AgentState
impl WorkflowState for AgentState
Source§type Mutation = AgentMutation
type Mutation = AgentMutation
与此状态关联的 Mutation 类型。
Source§fn apply_batch(&mut self, mutations: impl IntoIterator<Item = Self::Mutation>)
fn apply_batch(&mut self, mutations: impl IntoIterator<Item = Self::Mutation>)
批量应用 Mutation — 唯一公开入口。 Read more
Auto Trait Implementations§
impl Freeze for AgentState
impl RefUnwindSafe for AgentState
impl Send for AgentState
impl Sync for AgentState
impl Unpin for AgentState
impl UnsafeUnpin for AgentState
impl UnwindSafe for AgentState
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