Skip to content

Commands

Comprehensive reference for creating and registering commands in Synerix.

Built-in Commands

User Commands

CommandDescription
/help [command]Show help for all or a specific command
/clearClear the current conversation
/exitExit Synerix
/model <name>Switch to a model preset
/model listShow available model presets
/mode <name>Switch coding mode: plan, vibe
/mcp listList configured MCP servers
/skillManage skills and sources

Creating Commands

Basic Command

rust
use async_trait::async_trait;
use synerix::command::{Command, CommandContext, CommandResult};

pub struct GreetCommand;

#[async_trait]
impl Command for GreetCommand {
    fn name(&self) -> &'static str {
        "greet"
    }

    fn description(&self) -> &'static str {
        "Greet someone by name"
    }

    fn usage(&self) -> &'static str {
        "greet <name>"
    }

    async fn execute(&self, ctx: CommandContext) -> CommandResult {
        let name = ctx.args().first()
            .ok_or("Please provide a name")?;

        ctx.reply(&format!("Hello, {name}!")).await;
        CommandResult::Success
    }
}

Registering a Command

rust
use synerix::app::App;

let mut app = App::new(config).build().await?;
app.command_registry()
   .register(Box::new(GreetCommand))
   .await?;

Command Context

The CommandContext provides everything a command needs:

MethodReturnsDescription
ctx.args()&[String]Positional arguments
ctx.has_flag(name)boolCheck for a flag (e.g. --verbose)
ctx.get_option(name)Option<&str>Get named option value
ctx.reply(msg)()Send a response message
ctx.app()&AppAccess the full application state

Command Result

rust
pub enum CommandResult {
    Success,
    Error(String),
    NotImplemented,
}

Lifecycle Hooks

rust
#[async_trait]
impl Command for MyCommand {
    async fn before(&self, ctx: &CommandContext) -> CommandResult {
        // Runs before execute
        CommandResult::Success
    }

    async fn after(&self, ctx: &CommandContext, result: &CommandResult) {
        // Runs after execute, regardless of outcome
    }
}

Next Steps

基于 VitePress 构建