pub struct AgentSession {
pub total_tool_calls: usize,
pub nudge_count: usize,
pub turn_tool_calls: usize,
/* private fields */
}Fields§
§total_tool_calls: usizeTotal number of tool calls made in this session (across all turns). Used by middleware for decisions like “first_turn_only” enforcement.
nudge_count: usizeNumber of tool-enforcement nudges issued in the current turn.
Reset to 0 at the start of each turn (when a new user message arrives).
Used by ToolEnforcementMiddleware to cap nudge attempts per turn.
turn_tool_calls: usizeNumber of tool calls already executed in the current turn.
Reset to 0 at the start of each turn (when a new user message arrives).
Used by TurnToolLimitMiddleware to enforce per-turn tool call limits.
Implementations§
Source§impl AgentSession
impl AgentSession
pub fn new(id: SessionId) -> Self
pub fn id(&self) -> Option<SessionId>
Sourcepub fn simple_messages(&self) -> Vec<Message>
pub fn simple_messages(&self) -> Vec<Message>
Derive a simplified Vec<Message> view from the canonical chat_messages.
Assistant messages that contain only tool_calls (no text content) are
skipped, since they have no corresponding simplified representation.
pub fn chat_messages(&self) -> &[ChatMessage]
Sourcepub fn chat_messages_mut(&mut self) -> &mut Vec<ChatMessage>
pub fn chat_messages_mut(&mut self) -> &mut Vec<ChatMessage>
可变引用,仅用于需要直接操作消息的高级场景。
pub fn is_action_allowed(&self, action_key: &str) -> bool
pub fn allow_action(&mut self, action_key: impl Into<String>)
pub fn push_message(&mut self, role: MessageRole, content: impl Into<String>)
Sourcepub fn push_assistant_with_reasoning(
&mut self,
content: impl Into<String>,
reasoning: impl Into<String>,
)
pub fn push_assistant_with_reasoning( &mut self, content: impl Into<String>, reasoning: impl Into<String>, )
Push an assistant message with reasoning/thinking content preserved. This allows the LLM to see its own prior reasoning in subsequent turns, preventing it from re-deriving the same conclusions every turn.
pub fn push_user_message_with_images( &mut self, content: impl Into<String>, images: Vec<ImageAttachment>, )
pub fn push_assistant_tool_call( &mut self, tool_call_id: &str, tool_name: &str, arguments_json: &str, )
pub fn push_assistant_tool_calls( &mut self, tool_calls: &[(String, String, String)], reasoning: Option<String>, )
pub fn push_tool_result( &mut self, tool_call_id: &str, content: impl Into<String>, )
Sourcepub fn remove_ephemeral_messages(&mut self)
pub fn remove_ephemeral_messages(&mut self)
移除所有临时消息(ephemeral=true)。
在 turn 结束时调用,确保注入的临时内容不残留到下一轮。
Sourcepub fn turn_count(&self) -> usize
pub fn turn_count(&self) -> usize
Count the number of conversation turns. A turn starts with a User message and includes subsequent Assistant/Tool messages.
Sourcepub fn trim_oldest_turns(&mut self, max_turns: usize)
pub fn trim_oldest_turns(&mut self, max_turns: usize)
Remove the oldest turns from the front until turn count ≤ max_turns. Preserves the System message at index 0 if present.
Sourcepub fn pop_last_message(&mut self)
pub fn pop_last_message(&mut self)
Remove the last message from chat_messages.
Used by the max_message_tokens safety valve to discard oversized messages.
pub fn close_dangling_tool_calls(&mut self, error_summary: &str)
Sourcepub fn set_chat_messages(
&mut self,
messages: Vec<ChatMessage>,
) -> Result<(), String>
pub fn set_chat_messages( &mut self, messages: Vec<ChatMessage>, ) -> Result<(), String>
Replace chat messages — only for persistence restore. Validates message sequence before replacing.
仅供持久化恢复使用。调用方必须保证 messages 序列合法。
Trait Implementations§
Source§impl Clone for AgentSession
impl Clone for AgentSession
Source§fn clone(&self) -> AgentSession
fn clone(&self) -> AgentSession
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more