pub struct PersistencePlugin { /* private fields */ }Expand description
持久化插件
实现 AgentPlugin trait,提供完整的持久化能力:
- 从数据库加载会话历史
- 自动记录用户消息、助手消息、API 调用
§示例
ⓘ
use mofa_foundation::persistence::{PersistencePlugin, PostgresStore};
use mofa_sdk::llm::LLMAgentBuilder;
use uuid::Uuid;
let store = PostgresStore::connect("postgres://localhost/mofa").await?;
let user_id = Uuid::now_v7();
let tenant_id = Uuid::now_v7();
let agent_id = Uuid::now_v7();
let session_id = Uuid::now_v7();
let plugin = PersistencePlugin::from_store(
"persistence-plugin",
store,
user_id,
tenant_id,
agent_id,
session_id,
);
let agent = LLMAgentBuilder::new()
.with_plugin(plugin)
.build_async()
.await;Implementations§
Source§impl PersistencePlugin
impl PersistencePlugin
Sourcepub fn new(
plugin_id: &str,
message_store: Arc<dyn MessageStore + Sync + Send>,
api_call_store: Arc<dyn ApiCallStore + Sync + Send>,
user_id: Uuid,
tenant_id: Uuid,
agent_id: Uuid,
session_id: Uuid,
) -> PersistencePlugin
pub fn new( plugin_id: &str, message_store: Arc<dyn MessageStore + Sync + Send>, api_call_store: Arc<dyn ApiCallStore + Sync + Send>, user_id: Uuid, tenant_id: Uuid, agent_id: Uuid, session_id: Uuid, ) -> PersistencePlugin
创建持久化插件
§参数
plugin_id: 插件唯一标识message_store: 消息存储后端api_call_store: API 调用存储后端user_id: 用户 IDtenant_id: 租户 IDagent_id: Agent IDsession_id: 会话 ID
Sourcepub fn from_store<S>(
plugin_id: &str,
store: S,
user_id: Uuid,
tenant_id: Uuid,
agent_id: Uuid,
session_id: Uuid,
) -> PersistencePlugin
pub fn from_store<S>( plugin_id: &str, store: S, user_id: Uuid, tenant_id: Uuid, agent_id: Uuid, session_id: Uuid, ) -> PersistencePlugin
创建持久化插件(便捷方法,使用单个存储后端)
§参数
plugin_id: 插件唯一标识store: 持久化存储后端(需要同时实现 MessageStore、ApiCallStore、SessionStore)user_id: 用户 IDtenant_id: 租户 IDagent_id: Agent IDsession_id: 会话 ID
Sourcepub async fn with_session_id(&self, session_id: Uuid)
pub async fn with_session_id(&self, session_id: Uuid)
更新会话 ID
Sourcepub async fn session_id(&self) -> Uuid
pub async fn session_id(&self) -> Uuid
获取当前会话 ID
Sourcepub async fn load_history(&self) -> Result<Vec<LLMMessage>, PersistenceError>
pub async fn load_history(&self) -> Result<Vec<LLMMessage>, PersistenceError>
获取历史消息(用于 build_async)
Sourcepub fn message_store(&self) -> Arc<dyn MessageStore + Sync + Send>
pub fn message_store(&self) -> Arc<dyn MessageStore + Sync + Send>
获取消息存储引用
Sourcepub fn api_call_store(&self) -> Arc<dyn ApiCallStore + Sync + Send>
pub fn api_call_store(&self) -> Arc<dyn ApiCallStore + Sync + Send>
获取 API 调用存储引用
Sourcepub fn session_store(&self) -> Option<Arc<dyn SessionStore + Sync + Send>>
pub fn session_store(&self) -> Option<Arc<dyn SessionStore + Sync + Send>>
获取会话存储引用
Trait Implementations§
Source§impl AgentPlugin for PersistencePlugin
impl AgentPlugin for PersistencePlugin
Source§fn metadata(&self) -> &PluginMetadata
fn metadata(&self) -> &PluginMetadata
获取插件元数据
Source§fn state(&self) -> PluginState
fn state(&self) -> PluginState
获取插件状态
Source§fn load<'life0, 'life1, 'async_trait>(
&'life0 mut self,
_ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
PersistencePlugin: 'async_trait,
fn load<'life0, 'life1, 'async_trait>(
&'life0 mut self,
_ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
PersistencePlugin: 'async_trait,
插件加载(分配资源)
Source§fn init_plugin<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
fn init_plugin<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
插件初始化(配置初始化)
Source§fn start<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
fn start<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
插件启动
Source§fn stop<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
fn stop<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
插件停止
Source§fn unload<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
fn unload<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
插件卸载(释放资源)
Source§fn execute<'life0, 'async_trait>(
&'life0 mut self,
_input: String,
) -> Pin<Box<dyn Future<Output = Result<String, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
fn execute<'life0, 'async_trait>(
&'life0 mut self,
_input: String,
) -> Pin<Box<dyn Future<Output = Result<String, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
PersistencePlugin: 'async_trait,
执行插件功能
Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
转换为可变 Any
Source§fn plugin_type(&self) -> PluginType
fn plugin_type(&self) -> PluginType
获取插件类型
Source§fn pause<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn pause<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
插件暂停
Source§impl Clone for PersistencePlugin
impl Clone for PersistencePlugin
Source§fn clone(&self) -> PersistencePlugin
fn clone(&self) -> PersistencePlugin
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl LLMAgentEventHandler for PersistencePlugin
impl LLMAgentEventHandler for PersistencePlugin
Source§fn before_chat<'life0, 'life1, 'async_trait>(
&'life0 self,
message: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
PersistencePlugin: 'async_trait,
fn before_chat<'life0, 'life1, 'async_trait>(
&'life0 self,
message: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
PersistencePlugin: 'async_trait,
在发送用户消息前调用 - 记录用户消息
Source§fn before_chat_with_model<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 str,
model: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
PersistencePlugin: 'async_trait,
fn before_chat_with_model<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 str,
model: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
PersistencePlugin: 'async_trait,
在发送用户消息前调用(带模型名称)- 记录用户消息和模型
Source§fn after_chat<'life0, 'life1, 'async_trait>(
&'life0 self,
response: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
PersistencePlugin: 'async_trait,
fn after_chat<'life0, 'life1, 'async_trait>(
&'life0 self,
response: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
PersistencePlugin: 'async_trait,
在收到 LLM 响应后调用 - 记录助手消息和 API 调用
Source§fn after_chat_with_metadata<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
response: &'life1 str,
metadata: &'life2 LLMResponseMetadata,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
PersistencePlugin: 'async_trait,
fn after_chat_with_metadata<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
response: &'life1 str,
metadata: &'life2 LLMResponseMetadata,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
PersistencePlugin: 'async_trait,
在收到 LLM 响应后调用 - 记录助手消息和 API 调用(带元数据)
Source§fn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
error: &'life1 LLMError,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
PersistencePlugin: 'async_trait,
fn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
error: &'life1 LLMError,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
PersistencePlugin: 'async_trait,
在发生错误时调用 - 记录 API 错误
Source§fn clone_box(&self) -> Box<dyn LLMAgentEventHandler>
fn clone_box(&self) -> Box<dyn LLMAgentEventHandler>
Clone this handler trait object
Source§fn on_tool_call<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
name: &'life1 str,
arguments: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn on_tool_call<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
name: &'life1 str,
arguments: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LLMError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
处理工具调用
Auto Trait Implementations§
impl Freeze for PersistencePlugin
impl !RefUnwindSafe for PersistencePlugin
impl Send for PersistencePlugin
impl Sync for PersistencePlugin
impl Unpin for PersistencePlugin
impl UnsafeUnpin for PersistencePlugin
impl !UnwindSafe for PersistencePlugin
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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