Building Slash Command Extensions
Slash commands extend the Glass Assistant with custom commands that can generate text, fetch data, or transform content. They appear in the Assistant’s command palette when you type/.
Slash Command Basics
Slash commands:- Start with
/in the Assistant panel - Can take arguments
- Return text that’s inserted into the conversation
- Can provide argument completions
- Have access to the current worktree
Example Extension
Theslash-commands-example extension demonstrates slash command capabilities. Install it as a dev extension to try it:
Defining Slash Commands
[slash_commands.echo]
description = "echoes the provided input"
requires_argument = true
[slash_commands.pick-one]
description = "pick one of three options"
requires_argument = true
[slash_commands.current-time]
description = "insert the current time"
requires_argument = false
description - Shown in the command completion menurequires_argument - Whether the command needs at least one argumentuse zed_extension_api::{
self as zed, SlashCommand, SlashCommandOutput,
SlashCommandOutputSection, Worktree,
};
struct MyExtension;
impl zed::Extension for MyExtension {
fn new() -> Self {
Self
}
fn run_slash_command(
&self,
command: SlashCommand,
args: Vec<String>,
worktree: Option<&Worktree>,
) -> Result<SlashCommandOutput, String> {
match command.name.as_str() {
"echo" => self.echo_command(args),
"pick-one" => self.pick_one_command(args),
"current-time" => self.current_time_command(),
command => Err(format!("unknown slash command: \"{command}\"")),
}
}
}
Implementing Command Logic
Simple Text Output
Return text directly:Commands with Validation
Validate arguments before processing:Using Worktree Context
Access project files and structure:Output Sections
SlashCommandOutputSection creates collapsible sections in the Assistant:
Argument Completion
Provide completions when users type arguments:label- Text shown in the completion menunew_text- Text inserted when selectedrun_command- Whether to execute the command immediately after insertion