Skip to main content

PersistencePlugin

Struct PersistencePlugin 

Source
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

Source

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: 用户 ID
  • tenant_id: 租户 ID
  • agent_id: Agent ID
  • session_id: 会话 ID
Source

pub fn from_store<S>( plugin_id: &str, store: S, user_id: Uuid, tenant_id: Uuid, agent_id: Uuid, session_id: Uuid, ) -> PersistencePlugin
where S: MessageStore + ApiCallStore + SessionStore + Send + Sync + 'static,

创建持久化插件(便捷方法,使用单个存储后端)

§参数
  • plugin_id: 插件唯一标识
  • store: 持久化存储后端(需要同时实现 MessageStore、ApiCallStore、SessionStore)
  • user_id: 用户 ID
  • tenant_id: 租户 ID
  • agent_id: Agent ID
  • session_id: 会话 ID
Source

pub async fn with_session_id(&self, session_id: Uuid)

更新会话 ID

Source

pub async fn session_id(&self) -> Uuid

获取当前会话 ID

Source

pub async fn load_history(&self) -> Result<Vec<LLMMessage>, PersistenceError>

获取历史消息(用于 build_async)

Source

pub fn message_store(&self) -> Arc<dyn MessageStore + Sync + Send>

获取消息存储引用

Source

pub fn api_call_store(&self) -> Arc<dyn ApiCallStore + Sync + Send>

获取 API 调用存储引用

Source

pub fn session_store(&self) -> Option<Arc<dyn SessionStore + Sync + Send>>

获取会话存储引用

Source

pub fn user_id(&self) -> Uuid

获取用户 ID

Source

pub fn tenant_id(&self) -> Uuid

获取租户 ID

Source

pub fn agent_id(&self) -> Uuid

获取 Agent ID

Source

pub async fn save_user_message(&self, content: &str) -> Result<Uuid, LLMError>

保存用户消息

Source

pub async fn save_assistant_message( &self, content: &str, ) -> Result<Uuid, LLMError>

保存助手消息

Trait Implementations§

Source§

impl AgentPlugin for PersistencePlugin

Source§

fn metadata(&self) -> &PluginMetadata

获取插件元数据
Source§

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,

插件加载(分配资源)
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,

插件初始化(配置初始化)
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,

插件启动
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,

插件停止
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,

插件卸载(释放资源)
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,

执行插件功能
Source§

fn stats(&self) -> HashMap<String, Value>

获取插件统计信息
Source§

fn as_any(&self) -> &(dyn Any + 'static)

转换为 Any(用于向下转型)
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

转换为可变 Any
Source§

fn into_any(self: Box<PersistencePlugin>) -> Box<dyn Any>

消费并转换为 Any(用于提取具体的插件类型)
Source§

fn plugin_id(&self) -> &str

获取插件 ID(便捷方法)
Source§

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,

插件暂停
Source§

fn resume<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

插件恢复
Source§

fn health_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

健康检查
Source§

impl Clone for PersistencePlugin

Source§

fn clone(&self) -> PersistencePlugin

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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,

在发送用户消息前调用 - 记录用户消息

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,

在发送用户消息前调用(带模型名称)- 记录用户消息和模型

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,

在收到 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,

在收到 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,

在发生错误时调用 - 记录 API 错误

Source§

fn clone_box(&self) -> Box<dyn LLMAgentEventHandler>

Clone this handler trait object
Source§

fn as_any(&self) -> &(dyn Any + 'static)

获取 Any 类型用于 downcasting
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,

处理工具调用

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Message for T
where T: Any + Send + 'static,

Source§

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>

Convert this message to a BoxedMessage
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> OutputMessage for T
where T: Message + Clone,

Source§

impl<T> State for T
where T: Any + Send + 'static,