Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/vscode-node/slashCommands/claudeSlashCommandRegistry.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import { CancellationToken } from 'vscode';
8
9
/**
10
* Interface for Claude slash command handlers.
11
*
12
* Implement this interface to create a new slash command for the Claude chat participant.
13
* Register handlers using `registerClaudeSlashCommand()`.
14
*/
15
export interface IClaudeSlashCommandHandler {
16
/**
17
* The command name without the leading slash (e.g., "hooks" for /hooks)
18
*/
19
readonly commandName: string;
20
21
/**
22
* Human-readable description of what this command does
23
*/
24
readonly description: string;
25
26
/**
27
* Optional VS Code command ID for Command Palette registration.
28
* If provided, the command will be registered as a VS Code command
29
* and can be invoked via the Command Palette.
30
*
31
* @example "copilot.claude.hooks"
32
*/
33
readonly commandId?: string;
34
35
/**
36
* Handle the slash command.
37
*
38
* @param args - Arguments passed after the command name
39
* @param stream - Response stream for sending messages to the chat (undefined when invoked from Command Palette)
40
* @param token - Cancellation token
41
* @returns Chat result or void
42
*/
43
handle(
44
args: string,
45
stream: vscode.ChatResponseStream | undefined,
46
token: CancellationToken
47
): Promise<vscode.ChatResult | void>;
48
}
49
50
/**
51
* Constructor type for slash command handlers.
52
* Handlers are instantiated via the instantiation service for dependency injection.
53
*/
54
export interface IClaudeSlashCommandHandlerCtor {
55
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56
new(...args: any[]): IClaudeSlashCommandHandler;
57
}
58
59
// Registry of slash command handler constructors
60
const handlerRegistry: IClaudeSlashCommandHandlerCtor[] = [];
61
62
/**
63
* Register a slash command handler.
64
* Call this at module load time to register your handler.
65
*
66
* ## Adding a new slash command
67
*
68
* 1. Create a handler class implementing `IClaudeSlashCommandHandler`
69
* 2. Call `registerClaudeSlashCommand(YourHandler)` at module load time
70
* 3. Import it in `slashCommands/index.ts`
71
* 4. Add entries to `package.json`:
72
* - Under `contributes.commands`: Add the VS Code command (e.g., `copilot.claude.mycommand`)
73
* - Under `contributes.chatSessions[type="claude-code"].commands`: Add the slash command name and description
74
*
75
* @param ctor - The handler constructor class
76
*
77
* @example
78
* ```typescript
79
* export class MyCommand implements IClaudeSlashCommandHandler {
80
* readonly commandName = 'mycommand';
81
* readonly description = 'Does something useful';
82
* readonly commandId = 'copilot.claude.mycommand'; // For Command Palette
83
*
84
* async handle(args: string, stream: vscode.ChatResponseStream, token: CancellationToken) {
85
* stream.markdown('Hello!');
86
* return {};
87
* }
88
* }
89
*
90
* registerClaudeSlashCommand(MyCommand);
91
* ```
92
*/
93
export function registerClaudeSlashCommand(ctor: IClaudeSlashCommandHandlerCtor): void {
94
handlerRegistry.push(ctor);
95
}
96
97
/**
98
* Get all registered slash command handler constructors.
99
* Used by ClaudeSlashCommandService to instantiate handlers.
100
*/
101
export function getClaudeSlashCommandRegistry(): readonly IClaudeSlashCommandHandlerCtor[] {
102
return handlerRegistry;
103
}
104
105