Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/vscode-node/slashCommands/claudeSlashCommandRegistry.ts
13406 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as vscode from 'vscode';6import { CancellationToken } from 'vscode';78/**9* Interface for Claude slash command handlers.10*11* Implement this interface to create a new slash command for the Claude chat participant.12* Register handlers using `registerClaudeSlashCommand()`.13*/14export interface IClaudeSlashCommandHandler {15/**16* The command name without the leading slash (e.g., "hooks" for /hooks)17*/18readonly commandName: string;1920/**21* Human-readable description of what this command does22*/23readonly description: string;2425/**26* Optional VS Code command ID for Command Palette registration.27* If provided, the command will be registered as a VS Code command28* and can be invoked via the Command Palette.29*30* @example "copilot.claude.hooks"31*/32readonly commandId?: string;3334/**35* Handle the slash command.36*37* @param args - Arguments passed after the command name38* @param stream - Response stream for sending messages to the chat (undefined when invoked from Command Palette)39* @param token - Cancellation token40* @returns Chat result or void41*/42handle(43args: string,44stream: vscode.ChatResponseStream | undefined,45token: CancellationToken46): Promise<vscode.ChatResult | void>;47}4849/**50* Constructor type for slash command handlers.51* Handlers are instantiated via the instantiation service for dependency injection.52*/53export interface IClaudeSlashCommandHandlerCtor {54// eslint-disable-next-line @typescript-eslint/no-explicit-any55new(...args: any[]): IClaudeSlashCommandHandler;56}5758// Registry of slash command handler constructors59const handlerRegistry: IClaudeSlashCommandHandlerCtor[] = [];6061/**62* Register a slash command handler.63* Call this at module load time to register your handler.64*65* ## Adding a new slash command66*67* 1. Create a handler class implementing `IClaudeSlashCommandHandler`68* 2. Call `registerClaudeSlashCommand(YourHandler)` at module load time69* 3. Import it in `slashCommands/index.ts`70* 4. Add entries to `package.json`:71* - Under `contributes.commands`: Add the VS Code command (e.g., `copilot.claude.mycommand`)72* - Under `contributes.chatSessions[type="claude-code"].commands`: Add the slash command name and description73*74* @param ctor - The handler constructor class75*76* @example77* ```typescript78* export class MyCommand implements IClaudeSlashCommandHandler {79* readonly commandName = 'mycommand';80* readonly description = 'Does something useful';81* readonly commandId = 'copilot.claude.mycommand'; // For Command Palette82*83* async handle(args: string, stream: vscode.ChatResponseStream, token: CancellationToken) {84* stream.markdown('Hello!');85* return {};86* }87* }88*89* registerClaudeSlashCommand(MyCommand);90* ```91*/92export function registerClaudeSlashCommand(ctor: IClaudeSlashCommandHandlerCtor): void {93handlerRegistry.push(ctor);94}9596/**97* Get all registered slash command handler constructors.98* Used by ClaudeSlashCommandService to instantiate handlers.99*/100export function getClaudeSlashCommandRegistry(): readonly IClaudeSlashCommandHandlerCtor[] {101return handlerRegistry;102}103104105