pub struct Session { /* private fields */ }Expand description
运行时会话 — 一次 Agent 对话的完整内存状态。
由 Session::new() 创建,通过方法驱动消息追加、状态转换和用量累计。
§Examples
use katu_agent::session::Session;
use katu_core::{AgentDefinition, AgentRole, ModelId};
let agent = AgentDefinition::new("build", AgentRole::Primary)
.with_max_steps(50);
let session = Session::new(agent, ModelId::new("gpt-4o"));
assert!(session.status().is_idle());
assert_eq!(session.step_count(), 0);
assert!(session.messages().next().is_none());Implementations§
Source§impl Session
impl Session
Sourcepub fn new(agent: AgentDefinition, model_id: ModelId) -> Self
pub fn new(agent: AgentDefinition, model_id: ModelId) -> Self
Sourcepub fn with_context_window(self, context_window: u64) -> Self
pub fn with_context_window(self, context_window: u64) -> Self
设置 context window 大小(模型最大 token)。
Sourcepub fn with_compaction_config(self, config: CompactionConfig) -> Self
pub fn with_compaction_config(self, config: CompactionConfig) -> Self
设置上下文压缩配置。
Sourcepub fn with_cancel_token(self, token: CancellationToken) -> Self
pub fn with_cancel_token(self, token: CancellationToken) -> Self
注入已有的取消令牌。
Source§impl Session
impl Session
Sourcepub fn message_count(&self) -> usize
pub fn message_count(&self) -> usize
消息总数。
Sourcepub fn last_assistant(&self) -> Option<&AssistantMessage>
pub fn last_assistant(&self) -> Option<&AssistantMessage>
最后一条 Assistant 消息。
Sourcepub fn has_pending_tool_calls(&self) -> bool
pub fn has_pending_tool_calls(&self) -> bool
最后一条 Assistant 消息是否包含工具调用。
Sourcepub fn push_user(&mut self, content: impl Into<UserContent>)
pub fn push_user(&mut self, content: impl Into<UserContent>)
追加用户消息。
Sourcepub fn push_assistant(&mut self, message: AssistantMessage)
pub fn push_assistant(&mut self, message: AssistantMessage)
追加 Assistant 消息并累加用量。
Sourcepub fn push_tool_results(&mut self, results: Vec<ToolResultMessage>)
pub fn push_tool_results(&mut self, results: Vec<ToolResultMessage>)
批量追加工具结果消息。
Sourcepub fn push_message(&mut self, message: Message)
pub fn push_message(&mut self, message: Message)
追加任意消息(用于恢复等场景)。
Sourcepub fn replace_messages(&mut self, messages: Vec<Message>)
pub fn replace_messages(&mut self, messages: Vec<Message>)
替换全部消息(用于压缩后重建上下文)。
Sourcepub fn message_slice(&self) -> &[Message]
pub fn message_slice(&self) -> &[Message]
消息切片引用。
Sourcepub fn truncate_tool_result(
&mut self,
index: usize,
truncation_msg: &str,
max_chars: usize,
)
pub fn truncate_tool_result( &mut self, index: usize, truncation_msg: &str, max_chars: usize, )
截断指定索引处的 ToolResult 消息内容。
用于 Prune 阶段截断旧工具输出,释放 token 空间。 如果索引处不是 ToolResult 消息,静默忽略。
Source§impl Session
impl Session
Sourcepub fn status(&self) -> SessionStatus
pub fn status(&self) -> SessionStatus
当前运行状态。
Sourcepub fn cancel_token(&self) -> &CancellationToken
pub fn cancel_token(&self) -> &CancellationToken
取消令牌引用(共享给 StreamConsumer / ToolExecutor)。
Sourcepub fn reset_after_cancel(&mut self)
pub fn reset_after_cancel(&mut self)
从取消状态恢复为空闲(准备下一轮)。
Source§impl Session
impl Session
Sourcepub fn step_count(&self) -> u32
pub fn step_count(&self) -> u32
当前步数。
Sourcepub fn increment_step(&mut self) -> u32
pub fn increment_step(&mut self) -> u32
递增步数,返回新步数。
Sourcepub fn is_over_step_limit(&self) -> bool
pub fn is_over_step_limit(&self) -> bool
是否已达到步数上限。
Source§impl Session
impl Session
Sourcepub fn context_tokens(&self) -> u64
pub fn context_tokens(&self) -> u64
当前上下文 token 估计。
Sourcepub fn context_window(&self) -> u64
pub fn context_window(&self) -> u64
模型 context window 大小。
Sourcepub fn compaction_config(&self) -> &CompactionConfig
pub fn compaction_config(&self) -> &CompactionConfig
压缩配置。
Sourcepub fn set_context_tokens(&mut self, tokens: u64)
pub fn set_context_tokens(&mut self, tokens: u64)
更新上下文 token 计数(由外部 token 计数器设置)。
Sourcepub fn budget_state(&self) -> TokenBudgetState
pub fn budget_state(&self) -> TokenBudgetState
计算当前 token 预算状态。
Sourcepub fn should_compact(&self) -> bool
pub fn should_compact(&self) -> bool
是否应触发自动压缩。
Sourcepub fn compaction_state(&self) -> &CompactionState
pub fn compaction_state(&self) -> &CompactionState
压缩运行时状态引用。
Sourcepub fn compaction_state_mut(&mut self) -> &mut CompactionState
pub fn compaction_state_mut(&mut self) -> &mut CompactionState
压缩运行时状态可变引用。
Auto Trait Implementations§
impl Freeze for Session
impl RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
impl UnwindSafe for Session
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