Skip to main content

AgentState

Struct AgentState 

Source
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

Source

pub fn from_messages(messages: Vec<Message>) -> Self

从初始消息列表创建 AgentState。

Source

pub fn estimated_context_tokens(&self) -> usize

实时派生上下文 Token 数(从 messages 估算)。 供 BudgetCondition 判断是否需要压缩。

Source

pub fn reached_max(&self, max_iterations: usize) -> bool

检查是否已达到最大迭代轮次。

Source

pub fn exceeded_output(&self, max: Option<u32>) -> bool

检查是否超过总输出预算。

Source

pub fn exceeded_reasoning(&self, max: Option<u32>) -> bool

检查是否超过总推理预算。

Source

pub fn exceeded_output_with_extra(&self, max: Option<u32>, extra: usize) -> bool

检查加上额外 Token 后是否超过总输出预算。

用于 Mutation 未 apply 时的预判(节点 emit Mutation 之前)。

Source

pub fn exceeded_reasoning_with_extra( &self, max: Option<u32>, extra: usize, ) -> bool

检查加上额外 Token 后是否超过总推理预算。

Source

pub fn is_terminal(&self) -> bool

检查是否已终止(有 stop_reason)。

Source

pub fn messages_ref(&self) -> &[Message]

获取当前消息引用。

Trait Implementations§

Source§

impl Clone for AgentState

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AgentState

Source§

fn default() -> AgentState

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for AgentState

Source§

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

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,

执行节点逻辑。
Source§

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,

执行节点逻辑。
Source§

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,

执行节点逻辑。
Source§

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,

执行节点逻辑。
Source§

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,

执行节点逻辑。
Source§

impl MergeStrategy<AgentState> for AgentStateMerge

Source§

fn merge(branches: Vec<AgentState>) -> Result<AgentState, WorkflowError>

合并多个并行分支的状态。 Read more
Source§

fn default_instance() -> Self

创建策略的默认实例(供 ParallelNodeBuilder 使用)。 对于无状态策略(如 StateMerge、LastWriteWins),直接返回自身。
Source§

impl Serialize for AgentState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StateMutation<AgentState> for AgentMutation

Source§

fn apply(self, state: &mut AgentState)

将此 Mutation 应用到目标 State。 Read more
Source§

fn combine(self, _other: Self) -> Option<Self>

将此 Mutation 合并到另一个同类型 Mutation 中(可选)。 Read more
Source§

impl WorkflowState for AgentState

Source§

type Mutation = AgentMutation

与此状态关联的 Mutation 类型。
Source§

fn apply_batch(&mut self, mutations: impl IntoIterator<Item = Self::Mutation>)

批量应用 Mutation — 唯一公开入口。 Read more
Source§

fn initial() -> Self
where Self: Default,

创建默认/初始状态。

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more