diff --git a/extensions/cli/src/commands/commands.ts b/extensions/cli/src/commands/commands.ts index bf19c099f..d9886273e 100644 --- a/extensions/cli/src/commands/commands.ts +++ b/extensions/cli/src/commands/commands.ts @@ -146,7 +146,10 @@ export function getAllSlashCommands( category: "assistant" as const, })) || []; - return [...systemCommands, ...assistantCommands]; + // Get invokable rule commands + const invokableRuleCommands = getInvokableRuleSlashCommands(assistant); + + return [...systemCommands, ...assistantCommands, ...invokableRuleCommands]; } /** @@ -163,3 +166,33 @@ export function getAssistantSlashCommands( })) || [] ); } + +/** + * Get invokable rule commands from assistant config + */ +export function getInvokableRuleSlashCommands( + assistant: AssistantConfig, +): SlashCommand[] { + if (!assistant?.rules) { + return []; + } + + return assistant.rules + .filter((rule) => { + // Handle both string rules and rule objects + if (!rule || typeof rule === "string") { + return false; + } + // Only include rules with invokable: true + return rule.invokable === true; + }) + .map((rule) => { + // TypeScript now knows rule is an object with invokable: true + const ruleObj = rule as any; + return { + name: ruleObj.name || "", + description: ruleObj.description || "", + category: "assistant" as const, + }; + }); +} diff --git a/extensions/cli/src/slashCommands.test.ts b/extensions/cli/src/slashCommands.test.ts index f6db2be36..30df8e832 100644 --- a/extensions/cli/src/slashCommands.test.ts +++ b/extensions/cli/src/slashCommands.test.ts @@ -335,5 +335,99 @@ describe("slashCommands", () => { ); expect(result?.exit).toBe(false); }); + + it("should handle custom assistant prompts", async () => { + const assistantWithPrompts: AssistantUnrolled = { + ...mockAssistant, + prompts: [ + { + name: "explain", + description: "Explain the code", + prompt: "Please explain the following code:", + }, + ], + }; + + const result = await handleSlashCommands( + "/explain some code", + assistantWithPrompts, + ); + + expect(result).toBeDefined(); + expect(result?.newInput).toBe( + "Please explain the following code:some code", + ); + }); + + it("should handle invokable rules", async () => { + const assistantWithRules: AssistantUnrolled = { + ...mockAssistant, + rules: [ + { + name: "review", + description: "Review this code", + rule: "Perform a code review on the following:", + invokable: true, + }, + ], + }; + + const result = await handleSlashCommands( + "/review some code", + assistantWithRules, + ); + + expect(result).toBeDefined(); + expect(result?.newInput).toBe( + "Perform a code review on the following: some code", + ); + }); + + it("should not treat non-invokable rules as slash commands", async () => { + const assistantWithNonInvokableRule: AssistantUnrolled = { + ...mockAssistant, + rules: [ + { + name: "regular", + description: "A regular rule", + rule: "This is a regular rule", + invokable: false, + }, + ], + }; + + const result = await handleSlashCommands( + "/regular", + assistantWithNonInvokableRule, + ); + + // Should return null because the rule is not invokable + expect(result).toBeNull(); + }); + + it("should handle invokable rules from hub (string rules ignored)", async () => { + const assistantWithMixedRules: AssistantUnrolled = { + ...mockAssistant, + rules: [ + "string rule", + { + name: "analyze", + description: "Analyze code patterns", + rule: "Analyze the following code for patterns:", + invokable: true, + }, + ], + }; + + const result = await handleSlashCommands( + "/analyze my code", + assistantWithMixedRules, + ); + + expect(result).toBeDefined(); + expect(result?.newInput).toBe( + "Analyze the following code for patterns: my code", + ); + }); }); }); diff --git a/extensions/cli/src/slashCommands.ts b/extensions/cli/src/slashCommands.ts index 69b3f7437..503d13901 100644 --- a/extensions/cli/src/slashCommands.ts +++ b/extensions/cli/src/slashCommands.ts @@ -233,6 +233,21 @@ export async function handleSlashCommands( return { newInput }; } + // Check for invokable rules + const invokableRule = assistant.rules?.find((rule) => { + // Handle both string rules and rule objects + if (!rule || typeof rule === "string") { + return false; + } + const ruleObj = rule as any; + return ruleObj.invokable === true && ruleObj.name === command; + }); + if (invokableRule) { + const ruleObj = invokableRule as any; + const newInput = ruleObj.rule + " " + args.join(" "); + return { newInput }; + } + // Check if this command would match any available commands (same logic as UI) const allCommands = getAllSlashCommands(assistant, { isRemoteMode: options?.isRemoteMode, diff --git a/extensions/cli/src/ui/SlashCommandUI.tsx b/extensions/cli/src/ui/SlashCommandUI.tsx index 15d88bf29..b3d5765e3 100644 --- a/extensions/cli/src/ui/SlashCommandUI.tsx +++ b/extensions/cli/src/ui/SlashCommandUI.tsx @@ -31,7 +31,7 @@ const SlashCommandUI: React.FC = ({ { name: "clear", description: "Clear the chat history" }, { name: "exit", description: "Exit the chat" }, ]; - }, [isRemoteMode, assistant?.prompts]); + }, [isRemoteMode, assistant?.prompts, assistant?.rules]); // Filter commands based on the current filter const filteredCommands = allCommands