pub struct AgentSession {
pub run_state: RunState,
/* private fields */
}Expand description
Stable identity + rolling state of a single chat thread.
Default returns a fresh session; load persisted state via serde.
Backward-compatible with the old flat-field format (pre-RunState migration).
Fields§
§run_state: RunStateRun-level state tracking for the react loop. Holds ALL counting state
(D5): run_state.total_actions is the session-lifetime total middleware
decisions like “first_turn_only” read.
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>
Mutable reference, for advanced scenarios that need to operate on messages directly.
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_message_ephemeral(
&mut self,
role: MessageRole,
content: impl Into<String>,
)
pub fn push_message_ephemeral( &mut self, role: MessageRole, content: impl Into<String>, )
Push a message as ephemeral (user/system only): the LLM sees it this
turn, then remove_ephemeral_messages strips it at turn end — from
both memory and persistence.
Sourcepub fn set_system_prompt(&mut self, content: impl Into<String>)
pub fn set_system_prompt(&mut self, content: impl Into<String>)
Replace the system prompt — the first non-ephemeral System message
(wherever it sits, matching how context locates it); inserted at
index 0 if none exists. All other messages are untouched. Hosts that
own prompt composition (e.g. skill activation) re-bake through this.
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, )
Sourcepub fn render_assistant_tool_calls(
tool_calls: &[(String, String, String)],
reasoning: Option<String>,
content: Option<String>,
) -> ChatMessage
pub fn render_assistant_tool_calls( tool_calls: &[(String, String, String)], reasoning: Option<String>, content: Option<String>, ) -> ChatMessage
Render one assistant message carrying tool calls — the tool backend’s
history protocol (invalid-JSON sanitization included). Split out of
Self::push_assistant_tool_calls so the backend can both push the
message and carry it in Observation::history_messages for checkpoint
replay (M3 seam).
pub fn push_assistant_tool_calls( &mut self, tool_calls: &[(String, String, String)], reasoning: Option<String>, content: Option<String>, )
Sourcepub fn push_chat_message(&mut self, message: ChatMessage)
pub fn push_chat_message(&mut self, message: ChatMessage)
Push one already-built message — the kernel’s verbatim push for
backend-rendered history (crate::types::Observation::history_messages).
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)
Remove all ephemeral messages (ephemeral=true).
Called at the end of a turn to ensure injected ephemeral content does not linger into the next 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 — for persistence restore and window compaction. Validates message sequence before replacing.
Counters are NOT touched (D5): the counting source is explicit. A
checkpoint restore trusts the persisted RunState (co-written with the
checkpoint/journal — OV-11); only a messages-only restore — the
old-format corpus — recounts via Self::recount_actions_from_messages.
Recounting here would also deflate the total after compaction and lie
for restored histories that carry synthesized “已中断/未执行” results.
Sourcepub fn recount_actions_from_messages(&mut self)
pub fn recount_actions_from_messages(&mut self)
Old-format compatibility (D5: 消息重算只做旧格式兼容): rebuild the session-lifetime action total from the history’s assistant tool-call lens. Call ONLY on restores that carry no persisted counting state — the lens counts call attempts, not executed actions, so it is a fallback, never the primary source.