| Crates.io | aidale-plugin |
| lib.rs | aidale-plugin |
| version | 0.1.0 |
| created_at | 2025-11-01 04:16:30.863968+00 |
| updated_at | 2025-11-01 04:16:30.863968+00 |
| description | Built-in plugins for Aidale (tool use, etc.) |
| homepage | https://github.com/hanxuanliang/aidale |
| repository | https://github.com/hanxuanliang/aidale |
| max_upload_size | |
| id | 1911680 |
| size | 59,560 |
Built-in plugins for Aidale (tool use, RAG, etc.).
aidale-plugin provides runtime extensions through lifecycle hooks:
Enables AI to call external functions/tools:
use aidale_plugin::{ToolUsePlugin, ToolRegistry, FunctionTool};
use std::sync::Arc;
// Define a tool
async fn get_weather(location: String) -> Result<String> {
Ok(format!("Weather in {}: Sunny, 72°F", location))
}
// Register tools
let mut tools = ToolRegistry::new();
tools.register("get_weather", Arc::new(get_weather));
// Add plugin to executor
let executor = RuntimeExecutor::builder(provider)
.plugin(Arc::new(ToolUsePlugin::new(Arc::new(tools))))
.finish();
The plugin automatically:
Plugins use lifecycle hooks to extend runtime behavior:
#[async_trait]
pub trait Plugin: Send + Sync {
// Before sending request
async fn on_request(&self, ctx: &mut RequestContext) -> Result<()>;
// After receiving response
async fn on_response(&self, ctx: &mut ResponseContext) -> Result<()>;
// On error
async fn on_error(&self, ctx: &mut ErrorContext) -> Result<()>;
}
Flexible tool registration system:
use aidale_plugin::ToolRegistry;
let mut registry = ToolRegistry::new();
// Register function
registry.register("my_tool", Arc::new(my_function));
// Get tool
if let Some(tool) = registry.get("my_tool") {
let result = tool.call(args).await?;
}
Via the main aidale crate:
[dependencies]
aidale = { version = "0.1", features = ["plugins"] }
Directly:
[dependencies]
aidale-plugin = "0.1"
aidale-core = "0.1"
use aidale::prelude::*;
use aidale_plugin::{ToolUsePlugin, ToolRegistry};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<()> {
// Setup tools
let mut tools = ToolRegistry::new();
tools.register("calculator", Arc::new(calculator_tool));
// Build executor
let provider = aidale::provider::OpenAiProvider::new("sk-...");
let executor = RuntimeExecutor::builder(provider)
.plugin(Arc::new(ToolUsePlugin::new(Arc::new(tools))))
.finish();
// Generate with tools
let params = TextParams::new(vec![
Message::user("What is 123 * 456?"),
]);
let result = executor.generate_text("gpt-4", params).await?;
println!("{}", result.content); // "56,088"
Ok(())
}
Implement the Plugin trait:
use aidale_core::{Plugin, RequestContext, ResponseContext, ErrorContext};
use async_trait::async_trait;
pub struct MyCustomPlugin {
// Your fields
}
#[async_trait]
impl Plugin for MyCustomPlugin {
async fn on_request(&self, ctx: &mut RequestContext) -> Result<()> {
// Modify request before sending
println!("Sending request to {}", ctx.model);
Ok(())
}
async fn on_response(&self, ctx: &mut ResponseContext) -> Result<()> {
// Process response
println!("Received {} tokens", ctx.response.usage.total_tokens);
Ok(())
}
async fn on_error(&self, ctx: &mut ErrorContext) -> Result<()> {
// Handle errors
eprintln!("Error: {}", ctx.error);
Ok(())
}
}
When to use Plugins:
When to use Layers:
aidale-core - Core traits and runtimeaidale-provider - Provider implementationsaidale-layer - Middleware layersMIT OR Apache-2.0