Plugin Interfaces
A comprehensive Rust crate that provides the core interface definitions and abstractions for the chat-client plugin system. This crate serves as the foundation for building dynamic, loadable plugins that can extend the functionality of the chat-client application.
Overview
The plugin-interfaces crate defines a standardized plugin architecture that enables:
- Dynamic Plugin Loading: Load plugins at runtime as shared libraries (.dll/.so/.dylib)
- FFI-Safe Communication: Cross-language compatibility using C-style interfaces
- Event-Driven UI: Immediate-mode UI framework inspired by egui
- Message Streaming: Real-time bidirectional communication between plugins and frontend
- Plugin Lifecycle Management: Structured initialization, mounting, and cleanup processes
Architecture
Core Components
1. Plugin Handler (handler.rs)
The main trait that all plugins must implement:
2. FFI Interface (symbols.rs)
Provides C-compatible function pointers for cross-language plugin loading:
PluginInterface: FFI-safe struct containing function pointersCreatePluginFn/DestroyPluginFn: Plugin lifecycle management- Symbol exports:
create_pluginanddestroy_plugin
3. Plugin UI Framework (pluginui/)
An immediate-mode UI framework that provides:
- Components: Text inputs, buttons, combo boxes, labels
- Context Management: Creation and runtime contexts
- Event Handling: Click events and user interactions
- Response System: UI component state management
4. Message System (message/)
Comprehensive messaging infrastructure:
- PluginMessage: Standard message types (Normal, Success, Warning, Error, Info)
- StreamMessage: Real-time streaming with start/data/end lifecycle
- Frontend Communication: Bidirectional message passing
5. Host Callbacks (callbacks.rs)
Interface for plugins to communicate with the host application:
send_to_frontend: Send messages to the frontendget_app_config: Access application configurationcall_other_plugin: Inter-plugin communication
6. Configuration (config.rs)
Plugin configuration management:
- TOML-based configuration files
- Metadata extraction (id, name, version, author, etc.)
- Runtime configuration loading
7. Metadata (metadata.rs)
Plugin metadata structures:
PluginMetadata: Rust-native metadata structurePluginMetadataFFI: FFI-safe metadata for cross-language compatibility
8. Logging (logging/)
Structured logging system for plugins with different log levels and formatting.
9. API (api.rs)
High-level API functions for common plugin operations:
send_to_frontend: Simplified frontend communicationhost_send_to_frontend: Direct host communication
Plugin Lifecycle
- Loading: Host loads plugin shared library and calls
create_plugin - Initialization: Plugin receives host callbacks via
initialize - UI Setup:
init_uicalled to set up initial user interface - Mounting:
on_mountcalled with plugin metadata - Runtime:
update_uicalled for UI updates and event handlinghandle_messageprocesses incoming messageson_connect/on_disconnecthandle connection state changes
- Cleanup:
on_disposeanddestroycalled during shutdown
Usage Example
use *;
// Export plugin creation function
pub extern "C"
Features
- Type Safety: Strongly typed interfaces with Rust's type system
- Memory Safety: Safe memory management with proper cleanup
- Cross-Platform: Works on Windows, macOS, and Linux
- Async Support: Built-in support for asynchronous operations
- Streaming: Real-time data streaming capabilities
- Configuration: Flexible TOML-based configuration system
- Logging: Comprehensive logging infrastructure
- UI Framework: Immediate-mode UI with event handling
Dependencies
serde: Serialization/deserializationserde_json: JSON support for message passinguuid: Unique identifier generationtoml: Configuration file parsing
Integration
This crate is designed to be used by:
- Plugin Developers: Implement the
PluginHandlertrait to create new plugins - Host Application: Load and manage plugins using the FFI interface
- Frontend: Receive messages and UI updates from plugins
The plugin system supports both Rust-native plugins and plugins written in other languages that can export C-compatible functions.