From 085d42873c8c8b6e04da2023198328dfc3b64781 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Fri, 24 Jan 2025 01:14:48 +0700 Subject: [PATCH] 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 --- src/core/webview/ClineProvider.ts | 14 +++++--- src/shared/__tests__/support-prompts.test.ts | 5 ++- src/shared/support-prompt.ts | 5 +-- src/utils/__tests__/enhance-prompt.test.ts | 32 +++++++++++++------ ...prompt.ts => single-completion-handler.ts} | 11 ++----- 5 files changed, 39 insertions(+), 28 deletions(-) rename src/utils/{enhance-prompt.ts => single-completion-handler.ts} (71%) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index b28bad6..797def3 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -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, diff --git a/src/shared/__tests__/support-prompts.test.ts b/src/shared/__tests__/support-prompts.test.ts index ee7253e..cd27a38 100644 --- a/src/shared/__tests__/support-prompts.test.ts +++ b/src/shared/__tests__/support-prompts.test.ts @@ -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) diff --git a/src/shared/support-prompt.ts b/src/shared/support-prompt.ts index 406e981..715d8c6 100644 --- a/src/shared/support-prompt.ts +++ b/src/shared/support-prompt.ts @@ -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 = { diff --git a/src/utils/__tests__/enhance-prompt.test.ts b/src/utils/__tests__/enhance-prompt.test.ts index 69fdd04..d3cca04 100644 --- a/src/utils/__tests__/enhance-prompt.test.ts +++ b/src/utils/__tests__/enhance-prompt.test.ts @@ -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") }) }) diff --git a/src/utils/enhance-prompt.ts b/src/utils/single-completion-handler.ts similarity index 71% rename from src/utils/enhance-prompt.ts rename to src/utils/single-completion-handler.ts index 3724757..5e049d4 100644 --- a/src/utils/enhance-prompt.ts +++ b/src/utils/single-completion-handler.ts @@ -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 { +export async function singleCompletionHandler(apiConfiguration: ApiConfiguration, promptText: string): Promise { 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) }