An advanced example plugin demonstrating comprehensive Cortex plugin capabilities.
/stats(aliases:/statistics,/metrics) - Display current session code statistics/stats-reset(alias:/reset-stats) - Reset all statistics counters to zero/stats-export(alias:/export-stats) - Export statistics to JSON format
file_operation_after- Tracks statistics after each file operationsession_end- Saves statistics when the session endswidget_register- Registers the status bar widget
- Status Bar Widget - Displays compact statistics (
+123 -45 (67)) in the status bar
auto_save(boolean, default: true) - Automatically save stats on session enddisplay_format(string, default: "compact") - Format for displaying stats (compact, detailed)track_by_language(boolean, default: true) - Track statistics per programming language
code-stats/
├── plugin.toml # Plugin manifest (metadata, commands, hooks, config)
├── Cargo.toml # Rust build configuration
├── src/
│ └── lib.rs # Plugin implementation
└── README.md # This file
- Install Rust: https://rustup.rs/
- Add the WASM target:
rustup target add wasm32-wasi
cd examples/plugins/code-stats
cargo build --target wasm32-wasi --releaseThe compiled WASM module will be at:
target/wasm32-wasi/release/code_stats_plugin.wasm
Copy the compiled WASM and manifest to your Cortex plugins directory:
# Create plugin directory
mkdir -p ~/.cortex/plugins/code-stats
# Copy manifest
cp plugin.toml ~/.cortex/plugins/code-stats/
# Copy compiled WASM
cp target/wasm32-wasi/release/code_stats_plugin.wasm ~/.cortex/plugins/code-stats/plugin.wasmAdd to your Cortex configuration to customize the plugin:
[plugins.code-stats]
enabled = true
auto_save = true
display_format = "detailed" # or "compact"
track_by_language = true/stats
Output: Lines: +123 -45 | Files: 10 modified, 3 created, 1 deleted | Total ops: 14
/stats-reset
This clears all counters to zero for a fresh start.
/stats-export
Exports statistics as JSON to the event stream:
{
"lines_added": 123,
"lines_removed": 45,
"files_modified": 10,
"files_created": 3,
"files_deleted": 1,
"total_operations": 14
}The plugin uses atomic counters to track:
- lines_added - Total lines of code added
- lines_removed - Total lines of code removed
- files_modified - Number of files edited
- files_created - Number of new files created
- files_deleted - Number of files removed
- total_operations - Total file operations
The plugin emits events for external integrations:
| Event Name | Trigger | Data |
|---|---|---|
code_stats.displayed |
/stats command |
Current stats JSON |
code_stats.before_reset |
/stats-reset command |
Stats before reset |
code_stats.exported |
/stats-export command |
Full stats JSON |
code_stats.session_end |
Session ending | Final stats JSON |
code_stats.session_final |
Plugin shutdown | Final stats JSON |
The status bar widget displays a compact summary:
+{lines_added} -{lines_removed} ({total_ops})
Example: +123 -45 (14)
-
Add an atomic counter in
src/lib.rs:static MY_METRIC: AtomicU64 = AtomicU64::new(0);
-
Update the
get_stats_summary()function to include it. -
Update the
get_stats_json()function to include it. -
Add a recording function:
fn record_my_metric(value: u64) { MY_METRIC.fetch_add(value, Ordering::Relaxed); }
-
Add the command definition in
plugin.toml:[[commands]] name = "my-command" description = "My new command" usage = "/my-command [args]"
-
Implement the handler in
src/lib.rs:#[no_mangle] pub extern "C" fn cmd_my_command() -> i32 { log_info("My command executed!"); 0 }
-
Add the hook definition in
plugin.toml:[[hooks]] hook_type = "tool_execute_after" priority = 100 function = "hook_tool_execute_after"
-
Implement the handler in
src/lib.rs:#[no_mangle] pub extern "C" fn hook_tool_execute_after() -> i32 { log_debug("Tool execution completed"); 0 }
| Function | Purpose | Returns |
|---|---|---|
init() |
Plugin initialization | 0 on success |
shutdown() |
Plugin cleanup | 0 on success |
cmd_stats() |
Display stats | 0 on success |
cmd_stats_reset() |
Reset counters | 0 on success |
cmd_stats_export() |
Export JSON | 0 on success |
hook_file_operation_after() |
Track file ops | 0 to continue |
hook_session_end() |
Save on exit | 0 to continue |
hook_widget_register() |
Register widget | 0 to continue |
widget_render_code_stats() |
Render widget | 0 on success |
api_get_stats_json() |
Get stats JSON | Length or error |
| Function | Purpose |
|---|---|
log(level, ptr, len) |
Logging |
get_context() |
Access host context |
register_widget(region, ptr, len) |
Register UI widget |
show_toast(level, ptr, len, duration) |
Show notification |
emit_event(name_ptr, name_len, data_ptr, data_len) |
Emit custom event |
The plugin can be tested by:
- Building and installing the plugin
- Performing various file operations in Cortex
- Running
/statsto verify tracking - Running
/stats-resetand verifying counters reset - Running
/stats-exportand verifying JSON output
MIT License - See LICENSE file for details.