Let ask mode write markdown too

This commit is contained in:
Matt Rubens
2025-01-23 23:48:38 -05:00
parent 4e77fb93bb
commit 7413d6494e
3 changed files with 70 additions and 5 deletions

View File

@@ -83,6 +83,26 @@ describe("isToolAllowedForMode", () => {
const diffError = isToolAllowedForMode("apply_diff", "markdown-editor", customModes, undefined, "test.js")
expect(diffError).toBeInstanceOf(FileRestrictionError)
})
it("allows ask mode to edit markdown files only", () => {
// Should allow editing markdown files
const mdResult = isToolAllowedForMode("write_to_file", "ask", [], undefined, "test.md")
expect(mdResult).toBe(true)
// Should allow applying diffs to markdown files
const diffResult = isToolAllowedForMode("apply_diff", "ask", [], undefined, "readme.md")
expect(diffResult).toBe(true)
// Should reject non-markdown files
const jsResult = isToolAllowedForMode("write_to_file", "ask", [], undefined, "test.js")
expect(jsResult).toBeInstanceOf(FileRestrictionError)
expect((jsResult as FileRestrictionError).message).toContain("Markdown files only")
// Should maintain read capabilities
expect(isToolAllowedForMode("read_file", "ask", [])).toBe(true)
expect(isToolAllowedForMode("browser_action", "ask", [])).toBe(true)
expect(isToolAllowedForMode("use_mcp_tool", "ask", [])).toBe(true)
})
})
it("handles non-existent modes", () => {
@@ -99,9 +119,15 @@ describe("isToolAllowedForMode", () => {
})
describe("FileRestrictionError", () => {
it("formats error message correctly", () => {
it("formats error message with pattern when no description provided", () => {
const error = new FileRestrictionError("Markdown Editor", "\\.md$")
expect(error.message).toBe("This mode (Markdown Editor) can only edit files matching the pattern: \\.md$")
expect(error.name).toBe("FileRestrictionError")
})
it("formats error message with description when provided", () => {
const error = new FileRestrictionError("Markdown Editor", "\\.md$", "Markdown files only")
expect(error.message).toBe("This mode (Markdown Editor) can only edit files matching Markdown files only")
expect(error.name).toBe("FileRestrictionError")
})
})

View File

@@ -89,7 +89,7 @@ export const modes: readonly ModeConfig[] = [
name: "Ask",
roleDefinition:
"You are Roo, a knowledgeable technical assistant focused on answering questions and providing information about software development, technology, and related topics. You can analyze code, explain concepts, and access external resources while maintaining a read-only approach to the codebase. Make sure to answer the user's questions and don't rush to switch to implementing code.",
groups: ["read", "browser", "mcp"],
groups: ["read", ["edit", { fileRegex: "\\.md$", description: "Markdown files only" }], "browser", "mcp"],
},
] as const
@@ -146,8 +146,10 @@ export function isCustomMode(slug: string, customModes?: ModeConfig[]): boolean
// Custom error class for file restrictions
export class FileRestrictionError extends Error {
constructor(mode: string, pattern: string) {
super(`This mode (${mode}) can only edit files matching the pattern: ${pattern}`)
constructor(mode: string, pattern: string, description?: string) {
super(
`This mode (${mode}) can only edit files matching ${description ? description : `the pattern: ${pattern}`}`,
)
this.name = "FileRestrictionError"
}
}
@@ -194,7 +196,7 @@ export function isToolAllowedForMode(
// For the edit group, check file regex if specified
if (groupName === "edit" && options.fileRegex) {
if (!filePath || !doesFileMatchRegex(filePath, options.fileRegex)) {
return new FileRestrictionError(mode.name, options.fileRegex)
return new FileRestrictionError(mode.name, options.fileRegex, options.description)
}
return true
}