mofa_kernel/agent/plugins/
mod.rs1use crate::agent::context::AgentContext;
14use crate::agent::error::AgentResult;
15use crate::agent::types::{AgentInput, AgentOutput};
16use async_trait::async_trait;
17use std::collections::HashMap;
18use std::sync::Arc;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum PluginStage {
27 PreRequest,
29 PreContext,
31 PostResponse,
33 PostProcess,
35}
36
37#[derive(Debug, Clone, Default)]
39pub struct PluginMetadata {
40 pub name: String,
42 pub description: String,
44 pub version: String,
46 pub stages: Vec<PluginStage>,
48 pub custom: HashMap<String, String>,
50}
51
52#[async_trait]
54pub trait Plugin: Send + Sync {
55 fn name(&self) -> &str;
57
58 fn description(&self) -> &str;
60
61 fn metadata(&self) -> PluginMetadata;
63
64 async fn pre_request(&self, input: AgentInput, _ctx: &AgentContext) -> AgentResult<AgentInput> {
67 Ok(input)
68 }
69
70 async fn pre_context(&self, _ctx: &AgentContext) -> AgentResult<()> {
73 Ok(())
74 }
75
76 async fn post_response(
79 &self,
80 output: AgentOutput,
81 _ctx: &AgentContext,
82 ) -> AgentResult<AgentOutput> {
83 Ok(output)
84 }
85
86 async fn post_process(&self, _ctx: &AgentContext) -> AgentResult<()> {
89 Ok(())
90 }
91}
92
93pub trait PluginRegistry: Send + Sync {
99 fn register(&self, plugin: Arc<dyn Plugin>) -> AgentResult<()>;
101
102 fn register_all(&self, plugins: Vec<Arc<dyn Plugin>>) -> AgentResult<()> {
104 for plugin in plugins {
105 self.register(plugin)?;
106 }
107 Ok(())
108 }
109
110 fn unregister(&self, name: &str) -> AgentResult<bool>;
112
113 fn get(&self, name: &str) -> Option<Arc<dyn Plugin>>;
115
116 fn list(&self) -> Vec<Arc<dyn Plugin>>;
118
119 fn list_by_stage(&self, stage: PluginStage) -> Vec<Arc<dyn Plugin>>;
121
122 fn contains(&self, name: &str) -> bool;
124
125 fn count(&self) -> usize;
127}