Merge pull request #8077 from continuedev/tomasz/add-cli-invokable-rule-support

Add invokable rule support to cli
This commit is contained in:
Dallin Romney 2025-10-05 23:34:30 -07:00 • committed by GitHub
commit f84e07a686
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 144 additions and 2 deletions

View file

@ -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,
};
});
}

View file

@ -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",
);
});
});
});

View file

@ -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,

View file

@ -31,7 +31,7 @@ const SlashCommandUI: React.FC<SlashCommandUIProps> = ({
{ 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