pub struct ChatSession { /* private fields */ }Expand description
对话会话
管理多轮对话的消息历史
Implementations§
Source§impl ChatSession
impl ChatSession
Sourcepub fn new(client: LLMClient) -> ChatSession
pub fn new(client: LLMClient) -> ChatSession
创建新会话(自动生成 ID)
Sourcepub fn new_with_stores(
client: LLMClient,
user_id: Uuid,
tenant_id: Uuid,
agent_id: Uuid,
message_store: Arc<dyn MessageStore>,
session_store: Arc<dyn SessionStore>,
) -> ChatSession
pub fn new_with_stores( client: LLMClient, user_id: Uuid, tenant_id: Uuid, agent_id: Uuid, message_store: Arc<dyn MessageStore>, session_store: Arc<dyn SessionStore>, ) -> ChatSession
创建新会话并指定存储实现
Sourcepub fn with_id(session_id: Uuid, client: LLMClient) -> ChatSession
pub fn with_id(session_id: Uuid, client: LLMClient) -> ChatSession
使用指定 UUID 创建会话
Sourcepub fn with_id_str(session_id: &str, client: LLMClient) -> ChatSession
pub fn with_id_str(session_id: &str, client: LLMClient) -> ChatSession
使用指定字符串 ID 创建会话
Sourcepub fn with_id_and_stores(
session_id: Uuid,
client: LLMClient,
user_id: Uuid,
tenant_id: Uuid,
agent_id: Uuid,
message_store: Arc<dyn MessageStore>,
session_store: Arc<dyn SessionStore>,
context_window_size: Option<usize>,
) -> ChatSession
pub fn with_id_and_stores( session_id: Uuid, client: LLMClient, user_id: Uuid, tenant_id: Uuid, agent_id: Uuid, message_store: Arc<dyn MessageStore>, session_store: Arc<dyn SessionStore>, context_window_size: Option<usize>, ) -> ChatSession
使用指定 ID 和存储实现创建会话
Sourcepub async fn with_id_and_stores_and_persist(
session_id: Uuid,
client: LLMClient,
user_id: Uuid,
tenant_id: Uuid,
agent_id: Uuid,
message_store: Arc<dyn MessageStore>,
session_store: Arc<dyn SessionStore>,
context_window_size: Option<usize>,
) -> Result<ChatSession, PersistenceError>
pub async fn with_id_and_stores_and_persist( session_id: Uuid, client: LLMClient, user_id: Uuid, tenant_id: Uuid, agent_id: Uuid, message_store: Arc<dyn MessageStore>, session_store: Arc<dyn SessionStore>, context_window_size: Option<usize>, ) -> Result<ChatSession, PersistenceError>
Sourcepub fn session_id(&self) -> Uuid
pub fn session_id(&self) -> Uuid
获取会话 ID
Sourcepub fn session_id_str(&self) -> String
pub fn session_id_str(&self) -> String
获取会话 ID 字符串
Sourcepub fn created_at(&self) -> Instant
pub fn created_at(&self) -> Instant
获取会话创建时间
Sourcepub async fn load(
session_id: Uuid,
client: LLMClient,
user_id: Uuid,
tenant_id: Uuid,
agent_id: Uuid,
message_store: Arc<dyn MessageStore>,
session_store: Arc<dyn SessionStore>,
context_window_size: Option<usize>,
) -> Result<ChatSession, PersistenceError>
pub async fn load( session_id: Uuid, client: LLMClient, user_id: Uuid, tenant_id: Uuid, agent_id: Uuid, message_store: Arc<dyn MessageStore>, session_store: Arc<dyn SessionStore>, context_window_size: Option<usize>, ) -> Result<ChatSession, PersistenceError>
Sourcepub fn get_metadata(&self, key: &str) -> Option<&String>
pub fn get_metadata(&self, key: &str) -> Option<&String>
获取元数据
Sourcepub fn with_system(self, prompt: impl Into<String>) -> ChatSession
pub fn with_system(self, prompt: impl Into<String>) -> ChatSession
设置系统提示
Sourcepub fn with_context_window_size(self, size: Option<usize>) -> ChatSession
pub fn with_context_window_size(self, size: Option<usize>) -> ChatSession
Sourcepub fn with_tools(
self,
tools: Vec<Tool>,
executor: Arc<dyn ToolExecutor>,
) -> ChatSession
pub fn with_tools( self, tools: Vec<Tool>, executor: Arc<dyn ToolExecutor>, ) -> ChatSession
设置工具
Sourcepub fn with_tool_executor(self, executor: Arc<dyn ToolExecutor>) -> ChatSession
pub fn with_tool_executor(self, executor: Arc<dyn ToolExecutor>) -> ChatSession
仅设置工具执行器(工具列表自动发现)
Sourcepub async fn send_with_content(
&mut self,
content: MessageContent,
) -> Result<String, LLMError>
pub async fn send_with_content( &mut self, content: MessageContent, ) -> Result<String, LLMError>
发送结构化消息(支持多模态)
Sourcepub fn messages(&self) -> &[ChatMessage]
pub fn messages(&self) -> &[ChatMessage]
获取消息历史
Sourcepub fn messages_mut(&mut self) -> &mut Vec<ChatMessage>
pub fn messages_mut(&mut self) -> &mut Vec<ChatMessage>
获取消息历史(可变引用)
Sourcepub fn set_context_window_size(&mut self, size: Option<usize>)
pub fn set_context_window_size(&mut self, size: Option<usize>)
Sourcepub fn context_window_size(&self) -> Option<usize>
pub fn context_window_size(&self) -> Option<usize>
获取上下文窗口大小(轮数)
Sourcepub fn last_response_metadata(&self) -> Option<&LLMResponseMetadata>
pub fn last_response_metadata(&self) -> Option<&LLMResponseMetadata>
获取最后一次 LLM 响应的元数据
Sourcepub fn apply_sliding_window_static(
messages: &[ChatMessage],
window_size: Option<usize>,
) -> Vec<ChatMessage>
pub fn apply_sliding_window_static( messages: &[ChatMessage], window_size: Option<usize>, ) -> Vec<ChatMessage>
静态方法:应用滑动窗口到消息列表
这个方法可以在不拥有 ChatSession 实例的情况下使用, 例如在从数据库加载消息时。
§参数
messages: 要过滤的消息列表window_size: 保留的最大对话轮数(None 表示不限制)
Sourcepub async fn save(&self) -> Result<(), PersistenceError>
pub async fn save(&self) -> Result<(), PersistenceError>
保存会话和消息到数据库
Sourcepub async fn delete(&self) -> Result<(), PersistenceError>
pub async fn delete(&self) -> Result<(), PersistenceError>
从数据库删除会话和消息
Auto Trait Implementations§
impl Freeze for ChatSession
impl !RefUnwindSafe for ChatSession
impl Send for ChatSession
impl Sync for ChatSession
impl Unpin for ChatSession
impl UnsafeUnpin for ChatSession
impl !UnwindSafe for ChatSession
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Message for T
impl<T> Message for T
Source§fn from_boxed(m: BoxedMessage) -> Result<Self, BoxedDowncastErr>
fn from_boxed(m: BoxedMessage) -> Result<Self, BoxedDowncastErr>
Convert a BoxedMessage to this concrete type
Source§fn box_message(self, pid: &ActorId) -> Result<BoxedMessage, BoxedDowncastErr>
fn box_message(self, pid: &ActorId) -> Result<BoxedMessage, BoxedDowncastErr>
Convert this message to a BoxedMessage