mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-20 04:11:10 -05:00
refactor: generalize prompt enhancement into single completion handler
- Rename enhance-prompt.ts to single-completion-handler.ts for better clarity - Refactor enhancement logic to be more generic and reusable - Update prompt template handling to use template literals - Adjust tests and imports accordingly
This commit is contained in:
@@ -36,7 +36,7 @@ import { getNonce } from "./getNonce"
|
||||
import { getUri } from "./getUri"
|
||||
import { playSound, setSoundEnabled, setSoundVolume } from "../../utils/sound"
|
||||
import { checkExistKey } from "../../shared/checkExistApiConfig"
|
||||
import { enhancePrompt } from "../../utils/enhance-prompt"
|
||||
import { singleCompletionHandler } from "../../utils/single-completion-handler"
|
||||
import { getCommitInfo, searchCommits, getWorkingState } from "../../utils/git"
|
||||
import { ConfigManager } from "../config/ConfigManager"
|
||||
import { CustomModesManager } from "../config/CustomModesManager"
|
||||
@@ -996,11 +996,17 @@ export class ClineProvider implements vscode.WebviewViewProvider {
|
||||
}
|
||||
}
|
||||
|
||||
const enhancedPrompt = await enhancePrompt(
|
||||
const enhancedPrompt = await singleCompletionHandler(
|
||||
configToUse,
|
||||
message.text,
|
||||
supportPrompt.get(customPrompts, "ENHANCE"),
|
||||
supportPrompt.create(
|
||||
"ENHANCE",
|
||||
{
|
||||
userInput: message.text,
|
||||
},
|
||||
customPrompts,
|
||||
),
|
||||
)
|
||||
|
||||
await this.postMessageToWebview({
|
||||
type: "enhancedPrompt",
|
||||
text: enhancedPrompt,
|
||||
|
||||
@@ -77,12 +77,11 @@ describe("Code Action Prompts", () => {
|
||||
describe("ENHANCE action", () => {
|
||||
it("should format enhance prompt correctly", () => {
|
||||
const prompt = supportPrompt.create("ENHANCE", {
|
||||
filePath: testFilePath,
|
||||
selectedText: testCode,
|
||||
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):",
|
||||
"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)
|
||||
|
||||
@@ -68,8 +68,9 @@ Please suggest improvements for:
|
||||
Provide the improved code along with explanations for each enhancement.
|
||||
`
|
||||
|
||||
const ENHANCE_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):"
|
||||
const ENHANCE_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}`
|
||||
|
||||
// Get template based on prompt type
|
||||
const defaultTemplates = {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { enhancePrompt } from "../enhance-prompt"
|
||||
import { singleCompletionHandler } from "../single-completion-handler"
|
||||
import { ApiConfiguration } from "../../shared/api"
|
||||
import { buildApiHandler, SingleCompletionHandler } from "../../api"
|
||||
import { defaultPrompts } from "../../shared/modes"
|
||||
import { supportPrompt } from "../../shared/support-prompt"
|
||||
|
||||
// Mock the API handler
|
||||
jest.mock("../../api", () => ({
|
||||
@@ -34,17 +34,29 @@ describe("enhancePrompt", () => {
|
||||
})
|
||||
|
||||
it("enhances prompt using default enhancement prompt when no custom prompt provided", async () => {
|
||||
const result = await enhancePrompt(mockApiConfig, "Test prompt")
|
||||
const result = await singleCompletionHandler(mockApiConfig, "Test prompt")
|
||||
|
||||
expect(result).toBe("Enhanced prompt")
|
||||
const handler = buildApiHandler(mockApiConfig)
|
||||
expect((handler as any).completePrompt).toHaveBeenCalledWith(`${defaultPrompts.enhance}\n\nTest prompt`)
|
||||
expect((handler as any).completePrompt).toHaveBeenCalledWith(`Test prompt`)
|
||||
})
|
||||
|
||||
it("enhances prompt using custom enhancement prompt when provided", async () => {
|
||||
const customEnhancePrompt = "You are a custom prompt enhancer"
|
||||
const customEnhancePromptWithTemplate = customEnhancePrompt + "\n\n${userInput}"
|
||||
|
||||
const result = await enhancePrompt(mockApiConfig, "Test prompt", customEnhancePrompt)
|
||||
const result = await singleCompletionHandler(
|
||||
mockApiConfig,
|
||||
supportPrompt.create(
|
||||
"ENHANCE",
|
||||
{
|
||||
userInput: "Test prompt",
|
||||
},
|
||||
{
|
||||
ENHANCE: customEnhancePromptWithTemplate,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
expect(result).toBe("Enhanced prompt")
|
||||
const handler = buildApiHandler(mockApiConfig)
|
||||
@@ -52,11 +64,11 @@ describe("enhancePrompt", () => {
|
||||
})
|
||||
|
||||
it("throws error for empty prompt input", async () => {
|
||||
await expect(enhancePrompt(mockApiConfig, "")).rejects.toThrow("No prompt text provided")
|
||||
await expect(singleCompletionHandler(mockApiConfig, "")).rejects.toThrow("No prompt text provided")
|
||||
})
|
||||
|
||||
it("throws error for missing API configuration", async () => {
|
||||
await expect(enhancePrompt({} as ApiConfiguration, "Test prompt")).rejects.toThrow(
|
||||
await expect(singleCompletionHandler({} as ApiConfiguration, "Test prompt")).rejects.toThrow(
|
||||
"No valid API configuration provided",
|
||||
)
|
||||
})
|
||||
@@ -75,7 +87,7 @@ describe("enhancePrompt", () => {
|
||||
}),
|
||||
})
|
||||
|
||||
await expect(enhancePrompt(mockApiConfig, "Test prompt")).rejects.toThrow(
|
||||
await expect(singleCompletionHandler(mockApiConfig, "Test prompt")).rejects.toThrow(
|
||||
"The selected API provider does not support prompt enhancement",
|
||||
)
|
||||
})
|
||||
@@ -101,7 +113,7 @@ describe("enhancePrompt", () => {
|
||||
}),
|
||||
} as unknown as SingleCompletionHandler)
|
||||
|
||||
const result = await enhancePrompt(openRouterConfig, "Test prompt")
|
||||
const result = await singleCompletionHandler(openRouterConfig, "Test prompt")
|
||||
|
||||
expect(buildApiHandler).toHaveBeenCalledWith(openRouterConfig)
|
||||
expect(result).toBe("Enhanced prompt")
|
||||
@@ -121,6 +133,6 @@ describe("enhancePrompt", () => {
|
||||
}),
|
||||
} as unknown as SingleCompletionHandler)
|
||||
|
||||
await expect(enhancePrompt(mockApiConfig, "Test prompt")).rejects.toThrow("API Error")
|
||||
await expect(singleCompletionHandler(mockApiConfig, "Test prompt")).rejects.toThrow("API Error")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
import { ApiConfiguration } from "../shared/api"
|
||||
import { buildApiHandler, SingleCompletionHandler } from "../api"
|
||||
import { defaultPrompts } from "../shared/modes"
|
||||
|
||||
/**
|
||||
* Enhances a prompt using the configured API without creating a full Cline instance or task history.
|
||||
* This is a lightweight alternative that only uses the API's completion functionality.
|
||||
*/
|
||||
export async function enhancePrompt(
|
||||
apiConfiguration: ApiConfiguration,
|
||||
promptText: string,
|
||||
enhancePrompt?: string,
|
||||
): Promise<string> {
|
||||
export async function singleCompletionHandler(apiConfiguration: ApiConfiguration, promptText: string): Promise<string> {
|
||||
if (!promptText) {
|
||||
throw new Error("No prompt text provided")
|
||||
}
|
||||
@@ -25,7 +20,5 @@ export async function enhancePrompt(
|
||||
throw new Error("The selected API provider does not support prompt enhancement")
|
||||
}
|
||||
|
||||
const enhancePromptText = enhancePrompt ?? defaultPrompts.enhance
|
||||
const prompt = `${enhancePromptText}\n\n${promptText}`
|
||||
return (handler as SingleCompletionHandler).completePrompt(prompt)
|
||||
return (handler as SingleCompletionHandler).completePrompt(promptText)
|
||||
}
|
||||
Reference in New Issue
Block a user