From 7010ca2f3684a58887a4435f6daef5e0b3fab6be Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Thu, 2 Oct 2025 18:30:48 -0700 Subject: [PATCH 1/2] improv: add invokable rule support to cli --- extensions/cli/src/commands/commands.ts | 35 +++++++++- extensions/cli/src/slashCommands.test.ts | 88 ++++++++++++++++++++++++ extensions/cli/src/slashCommands.ts | 17 +++++ extensions/cli/src/ui/SlashCommandUI.tsx | 2 +- 4 files changed, 140 insertions(+), 2 deletions(-) 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..0af76e414 100644 --- a/extensions/cli/src/slashCommands.test.ts +++ b/extensions/cli/src/slashCommands.test.ts @@ -335,5 +335,93 @@ 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..abe8cc396 100644 --- a/extensions/cli/src/slashCommands.ts +++ b/extensions/cli/src/slashCommands.ts @@ -233,6 +233,23 @@ 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 From 49ac0d176613f35de074b4a22a312069b826dd0a Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Thu, 2 Oct 2025 18:44:20 -0700 Subject: [PATCH 2/2] fix: prettier --- extensions/cli/src/slashCommands.test.ts | 12 +++++++++--- extensions/cli/src/slashCommands.ts | 18 ++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/extensions/cli/src/slashCommands.test.ts b/extensions/cli/src/slashCommands.test.ts index 0af76e414..30df8e832 100644 --- a/extensions/cli/src/slashCommands.test.ts +++ b/extensions/cli/src/slashCommands.test.ts @@ -354,7 +354,9 @@ describe("slashCommands", () => { ); expect(result).toBeDefined(); - expect(result?.newInput).toBe("Please explain the following code:some code"); + expect(result?.newInput).toBe( + "Please explain the following code:some code", + ); }); it("should handle invokable rules", async () => { @@ -376,7 +378,9 @@ describe("slashCommands", () => { ); expect(result).toBeDefined(); - expect(result?.newInput).toBe("Perform a code review on the following: some code"); + expect(result?.newInput).toBe( + "Perform a code review on the following: some code", + ); }); it("should not treat non-invokable rules as slash commands", async () => { @@ -421,7 +425,9 @@ describe("slashCommands", () => { ); expect(result).toBeDefined(); - expect(result?.newInput).toBe("Analyze the following code for patterns: my code"); + 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 abe8cc396..503d13901 100644 --- a/extensions/cli/src/slashCommands.ts +++ b/extensions/cli/src/slashCommands.ts @@ -234,16 +234,14 @@ export async function handleSlashCommands( } // 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; - }, - ); + 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(" ");