use crate::agent::context::AgentContext;
use crate::agent::error::AgentResult;
use crate::agent::types::{AgentInput, AgentOutput};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PluginStage {
PreRequest,
PreContext,
PostResponse,
PostProcess,
}
#[derive(Debug, Clone, Default)]
pub struct PluginMetadata {
pub name: String,
pub description: String,
pub version: String,
pub stages: Vec<PluginStage>,
pub custom: HashMap<String, String>,
}
#[async_trait]
pub trait Plugin: Send + Sync {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn metadata(&self) -> PluginMetadata;
async fn pre_request(&self, input: AgentInput, _ctx: &AgentContext) -> AgentResult<AgentInput> {
Ok(input)
}
async fn pre_context(&self, _ctx: &AgentContext) -> AgentResult<()> {
Ok(())
}
async fn post_response(
&self,
output: AgentOutput,
_ctx: &AgentContext,
) -> AgentResult<AgentOutput> {
Ok(output)
}
async fn post_process(&self, _ctx: &AgentContext) -> AgentResult<()> {
Ok(())
}
}
pub trait PluginRegistry: Send + Sync {
fn register(&self, plugin: Arc<dyn Plugin>) -> AgentResult<()>;
fn register_all(&self, plugins: Vec<Arc<dyn Plugin>>) -> AgentResult<()> {
for plugin in plugins {
self.register(plugin)?;
}
Ok(())
}
fn unregister(&self, name: &str) -> AgentResult<bool>;
fn get(&self, name: &str) -> Option<Arc<dyn Plugin>>;
fn list(&self) -> Vec<Arc<dyn Plugin>>;
fn list_by_stage(&self, stage: PluginStage) -> Vec<Arc<dyn Plugin>>;
fn contains(&self, name: &str) -> bool;
fn count(&self) -> usize;
}