mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-22 13:21:07 -05:00
Merge pull request #329 from samhvw8/feat/roo-cline-code-action
New Feature code action
This commit is contained in:
@@ -4,7 +4,8 @@ import { ApiConfiguration, ApiProvider, ModelInfo } from "./api"
|
||||
import { HistoryItem } from "./HistoryItem"
|
||||
import { McpServer } from "./mcp"
|
||||
import { GitCommit } from "../utils/git"
|
||||
import { Mode, CustomPrompts, ModeConfig } from "./modes"
|
||||
import { Mode, CustomModePrompts, ModeConfig } from "./modes"
|
||||
import { CustomSupportPrompts } from "./support-prompt"
|
||||
|
||||
export interface LanguageModelChatSelector {
|
||||
vendor?: string
|
||||
@@ -82,7 +83,8 @@ export interface ExtensionState {
|
||||
currentApiConfigName?: string
|
||||
listApiConfigMeta?: ApiConfigMeta[]
|
||||
customInstructions?: string
|
||||
customPrompts?: CustomPrompts
|
||||
customModePrompts?: CustomModePrompts
|
||||
customSupportPrompts?: CustomSupportPrompts
|
||||
alwaysAllowReadOnly?: boolean
|
||||
alwaysAllowWrite?: boolean
|
||||
alwaysAllowExecute?: boolean
|
||||
|
||||
@@ -68,7 +68,8 @@ export interface WebviewMessage {
|
||||
| "requestVsCodeLmModels"
|
||||
| "mode"
|
||||
| "updatePrompt"
|
||||
| "updateEnhancedPrompt"
|
||||
| "updateSupportPrompt"
|
||||
| "resetSupportPrompt"
|
||||
| "getSystemPrompt"
|
||||
| "systemPrompt"
|
||||
| "enhancementApiConfigId"
|
||||
|
||||
153
src/shared/__tests__/support-prompts.test.ts
Normal file
153
src/shared/__tests__/support-prompts.test.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { supportPrompt } from "../support-prompt"
|
||||
|
||||
describe("Code Action Prompts", () => {
|
||||
const testFilePath = "test/file.ts"
|
||||
const testCode = "function test() { return true; }"
|
||||
|
||||
describe("EXPLAIN action", () => {
|
||||
it("should format explain prompt correctly", () => {
|
||||
const prompt = supportPrompt.create("EXPLAIN", {
|
||||
filePath: testFilePath,
|
||||
selectedText: testCode,
|
||||
})
|
||||
|
||||
expect(prompt).toContain(`@/${testFilePath}`)
|
||||
expect(prompt).toContain(testCode)
|
||||
expect(prompt).toContain("purpose and functionality")
|
||||
expect(prompt).toContain("Key components")
|
||||
expect(prompt).toContain("Important patterns")
|
||||
})
|
||||
})
|
||||
|
||||
describe("FIX action", () => {
|
||||
it("should format fix prompt without diagnostics", () => {
|
||||
const prompt = supportPrompt.create("FIX", {
|
||||
filePath: testFilePath,
|
||||
selectedText: testCode,
|
||||
})
|
||||
|
||||
expect(prompt).toContain(`@/${testFilePath}`)
|
||||
expect(prompt).toContain(testCode)
|
||||
expect(prompt).toContain("Address all detected problems")
|
||||
expect(prompt).not.toContain("Current problems detected")
|
||||
})
|
||||
|
||||
it("should format fix prompt with diagnostics", () => {
|
||||
const diagnostics = [
|
||||
{
|
||||
source: "eslint",
|
||||
message: "Missing semicolon",
|
||||
code: "semi",
|
||||
},
|
||||
{
|
||||
message: "Unused variable",
|
||||
severity: 1,
|
||||
},
|
||||
]
|
||||
|
||||
const prompt = supportPrompt.create("FIX", {
|
||||
filePath: testFilePath,
|
||||
selectedText: testCode,
|
||||
diagnostics,
|
||||
})
|
||||
|
||||
expect(prompt).toContain("Current problems detected:")
|
||||
expect(prompt).toContain("[eslint] Missing semicolon (semi)")
|
||||
expect(prompt).toContain("[Error] Unused variable")
|
||||
expect(prompt).toContain(testCode)
|
||||
})
|
||||
})
|
||||
|
||||
describe("IMPROVE action", () => {
|
||||
it("should format improve prompt correctly", () => {
|
||||
const prompt = supportPrompt.create("IMPROVE", {
|
||||
filePath: testFilePath,
|
||||
selectedText: testCode,
|
||||
})
|
||||
|
||||
expect(prompt).toContain(`@/${testFilePath}`)
|
||||
expect(prompt).toContain(testCode)
|
||||
expect(prompt).toContain("Code readability")
|
||||
expect(prompt).toContain("Performance optimization")
|
||||
expect(prompt).toContain("Best practices")
|
||||
expect(prompt).toContain("Error handling")
|
||||
})
|
||||
})
|
||||
|
||||
describe("ENHANCE action", () => {
|
||||
it("should format enhance prompt correctly", () => {
|
||||
const prompt = supportPrompt.create("ENHANCE", {
|
||||
userInput: "test",
|
||||
})
|
||||
|
||||
expect(prompt).toBe(
|
||||
"Generate an enhanced version of this prompt (reply with only the enhanced prompt - no conversation, explanations, lead-in, bullet points, placeholders, or surrounding quotes):\n\ntest",
|
||||
)
|
||||
// Verify it ignores parameters since ENHANCE template doesn't use any
|
||||
expect(prompt).not.toContain(testFilePath)
|
||||
expect(prompt).not.toContain(testCode)
|
||||
})
|
||||
})
|
||||
|
||||
describe("get template", () => {
|
||||
it("should return default template when no custom prompts provided", () => {
|
||||
const template = supportPrompt.get(undefined, "EXPLAIN")
|
||||
expect(template).toBe(supportPrompt.default.EXPLAIN)
|
||||
})
|
||||
|
||||
it("should return custom template when provided", () => {
|
||||
const customTemplate = "Custom template for explaining code"
|
||||
const customSupportPrompts = {
|
||||
EXPLAIN: customTemplate,
|
||||
}
|
||||
const template = supportPrompt.get(customSupportPrompts, "EXPLAIN")
|
||||
expect(template).toBe(customTemplate)
|
||||
})
|
||||
|
||||
it("should return default template when custom prompts does not include type", () => {
|
||||
const customSupportPrompts = {
|
||||
SOMETHING_ELSE: "Other template",
|
||||
}
|
||||
const template = supportPrompt.get(customSupportPrompts, "EXPLAIN")
|
||||
expect(template).toBe(supportPrompt.default.EXPLAIN)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create with custom prompts", () => {
|
||||
it("should use custom template when provided", () => {
|
||||
const customTemplate = "Custom template for ${filePath}"
|
||||
const customSupportPrompts = {
|
||||
EXPLAIN: customTemplate,
|
||||
}
|
||||
|
||||
const prompt = supportPrompt.create(
|
||||
"EXPLAIN",
|
||||
{
|
||||
filePath: testFilePath,
|
||||
selectedText: testCode,
|
||||
},
|
||||
customSupportPrompts,
|
||||
)
|
||||
|
||||
expect(prompt).toContain(`Custom template for ${testFilePath}`)
|
||||
expect(prompt).not.toContain("purpose and functionality")
|
||||
})
|
||||
|
||||
it("should use default template when custom prompts does not include type", () => {
|
||||
const customSupportPrompts = {
|
||||
EXPLAIN: "Other template",
|
||||
}
|
||||
|
||||
const prompt = supportPrompt.create(
|
||||
"EXPLAIN",
|
||||
{
|
||||
filePath: testFilePath,
|
||||
selectedText: testCode,
|
||||
},
|
||||
customSupportPrompts,
|
||||
)
|
||||
|
||||
expect(prompt).toContain("Other template")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -12,6 +12,16 @@ export type ModeConfig = {
|
||||
groups: readonly ToolGroup[] // Now uses groups instead of tools array
|
||||
}
|
||||
|
||||
// Mode-specific prompts only
|
||||
export type PromptComponent = {
|
||||
roleDefinition?: string
|
||||
customInstructions?: string
|
||||
}
|
||||
|
||||
export type CustomModePrompts = {
|
||||
[key: string]: PromptComponent | undefined
|
||||
}
|
||||
|
||||
// Helper to get all tools for a mode
|
||||
export function getToolsForMode(groups: readonly ToolGroup[]): string[] {
|
||||
const tools = new Set<string>()
|
||||
@@ -130,35 +140,8 @@ export function isToolAllowedForMode(
|
||||
return mode.groups.some((group) => TOOL_GROUPS[group].includes(tool as string))
|
||||
}
|
||||
|
||||
export type PromptComponent = {
|
||||
roleDefinition?: string
|
||||
customInstructions?: string
|
||||
}
|
||||
|
||||
// Mode-specific prompts only
|
||||
export type CustomPrompts = {
|
||||
[key: string]: PromptComponent | undefined
|
||||
}
|
||||
|
||||
// Separate enhance prompt type and definition
|
||||
export type EnhanceConfig = {
|
||||
prompt: string
|
||||
}
|
||||
|
||||
export const enhance: EnhanceConfig = {
|
||||
prompt: "Generate an enhanced version of this prompt (reply with only the enhanced prompt - no conversation, explanations, lead-in, bullet points, placeholders, or surrounding quotes):",
|
||||
} as const
|
||||
|
||||
// Completely separate enhance prompt handling
|
||||
export const enhancePrompt = {
|
||||
default: enhance.prompt,
|
||||
get: (customPrompts: Record<string, any> | undefined): string => {
|
||||
return customPrompts?.enhance ?? enhance.prompt
|
||||
},
|
||||
} as const
|
||||
|
||||
// Create the mode-specific default prompts
|
||||
export const defaultPrompts: Readonly<CustomPrompts> = Object.freeze(
|
||||
export const defaultPrompts: Readonly<CustomModePrompts> = Object.freeze(
|
||||
Object.fromEntries(modes.map((mode) => [mode.slug, { roleDefinition: mode.roleDefinition }])),
|
||||
)
|
||||
|
||||
|
||||
123
src/shared/support-prompt.ts
Normal file
123
src/shared/support-prompt.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
// Support prompts
|
||||
type PromptParams = Record<string, string | any[]>
|
||||
|
||||
const generateDiagnosticText = (diagnostics?: any[]) => {
|
||||
if (!diagnostics?.length) return ""
|
||||
return `\nCurrent problems detected:\n${diagnostics
|
||||
.map((d) => `- [${d.source || "Error"}] ${d.message}${d.code ? ` (${d.code})` : ""}`)
|
||||
.join("\n")}`
|
||||
}
|
||||
|
||||
export const createPrompt = (template: string, params: PromptParams): string => {
|
||||
let result = template
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
if (key === "diagnostics") {
|
||||
result = result.replaceAll("${diagnosticText}", generateDiagnosticText(value as any[]))
|
||||
} else {
|
||||
result = result.replaceAll(`\${${key}}`, value as string)
|
||||
}
|
||||
}
|
||||
|
||||
// Replace any remaining user_input placeholders with empty string
|
||||
result = result.replaceAll("${userInput}", "")
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
interface SupportPromptConfig {
|
||||
label: string
|
||||
description: string
|
||||
template: string
|
||||
}
|
||||
|
||||
const supportPromptConfigs: Record<string, SupportPromptConfig> = {
|
||||
ENHANCE: {
|
||||
label: "Enhance Prompt",
|
||||
description:
|
||||
"Use prompt enhancement to get tailored suggestions or improvements for your inputs. This ensures Roo understands your intent and provides the best possible responses. Available via the ✨ icon in chat.",
|
||||
template: `Generate an enhanced version of this prompt (reply with only the enhanced prompt - no conversation, explanations, lead-in, bullet points, placeholders, or surrounding quotes):
|
||||
|
||||
\${userInput}`,
|
||||
},
|
||||
EXPLAIN: {
|
||||
label: "Explain Code",
|
||||
description:
|
||||
"Get detailed explanations of code snippets, functions, or entire files. Useful for understanding complex code or learning new patterns. Available in the editor context menu (right-click on selected code).",
|
||||
template: `Explain the following code from file path @/\${filePath}:
|
||||
\${userInput}
|
||||
|
||||
\`\`\`
|
||||
\${selectedText}
|
||||
\`\`\`
|
||||
|
||||
Please provide a clear and concise explanation of what this code does, including:
|
||||
1. The purpose and functionality
|
||||
2. Key components and their interactions
|
||||
3. Important patterns or techniques used`,
|
||||
},
|
||||
FIX: {
|
||||
label: "Fix Issues",
|
||||
description:
|
||||
"Get help identifying and resolving bugs, errors, or code quality issues. Provides step-by-step guidance for fixing problems. Available in the editor context menu (right-click on selected code).",
|
||||
template: `Fix any issues in the following code from file path @/\${filePath}
|
||||
\${diagnosticText}
|
||||
\${userInput}
|
||||
|
||||
\`\`\`
|
||||
\${selectedText}
|
||||
\`\`\`
|
||||
|
||||
Please:
|
||||
1. Address all detected problems listed above (if any)
|
||||
2. Identify any other potential bugs or issues
|
||||
3. Provide corrected code
|
||||
4. Explain what was fixed and why`,
|
||||
},
|
||||
IMPROVE: {
|
||||
label: "Improve Code",
|
||||
description:
|
||||
"Receive suggestions for code optimization, better practices, and architectural improvements while maintaining functionality. Available in the editor context menu (right-click on selected code).",
|
||||
template: `Improve the following code from file path @/\${filePath}:
|
||||
\${userInput}
|
||||
|
||||
\`\`\`
|
||||
\${selectedText}
|
||||
\`\`\`
|
||||
|
||||
Please suggest improvements for:
|
||||
1. Code readability and maintainability
|
||||
2. Performance optimization
|
||||
3. Best practices and patterns
|
||||
4. Error handling and edge cases
|
||||
|
||||
Provide the improved code along with explanations for each enhancement.`,
|
||||
},
|
||||
} as const
|
||||
|
||||
type SupportPromptType = keyof typeof supportPromptConfigs
|
||||
|
||||
export const supportPrompt = {
|
||||
default: Object.fromEntries(Object.entries(supportPromptConfigs).map(([key, config]) => [key, config.template])),
|
||||
get: (customSupportPrompts: Record<string, any> | undefined, type: SupportPromptType): string => {
|
||||
return customSupportPrompts?.[type] ?? supportPromptConfigs[type].template
|
||||
},
|
||||
create: (type: SupportPromptType, params: PromptParams, customSupportPrompts?: Record<string, any>): string => {
|
||||
const template = supportPrompt.get(customSupportPrompts, type)
|
||||
return createPrompt(template, params)
|
||||
},
|
||||
} as const
|
||||
|
||||
export type { SupportPromptType }
|
||||
|
||||
// Expose labels and descriptions for UI
|
||||
export const supportPromptLabels = Object.fromEntries(
|
||||
Object.entries(supportPromptConfigs).map(([key, config]) => [key, config.label]),
|
||||
) as Record<SupportPromptType, string>
|
||||
|
||||
export const supportPromptDescriptions = Object.fromEntries(
|
||||
Object.entries(supportPromptConfigs).map(([key, config]) => [key, config.description]),
|
||||
) as Record<SupportPromptType, string>
|
||||
|
||||
export type CustomSupportPrompts = {
|
||||
[key: string]: string | undefined
|
||||
}
|
||||
Reference in New Issue
Block a user