Skip to content

Commit 085d428

Browse files
committed
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
1 parent 149e86e commit 085d428

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { getNonce } from "./getNonce"
3636
import { getUri } from "./getUri"
3737
import { playSound, setSoundEnabled, setSoundVolume } from "../../utils/sound"
3838
import { checkExistKey } from "../../shared/checkExistApiConfig"
39-
import { enhancePrompt } from "../../utils/enhance-prompt"
39+
import { singleCompletionHandler } from "../../utils/single-completion-handler"
4040
import { getCommitInfo, searchCommits, getWorkingState } from "../../utils/git"
4141
import { ConfigManager } from "../config/ConfigManager"
4242
import { CustomModesManager } from "../config/CustomModesManager"
@@ -996,11 +996,17 @@ export class ClineProvider implements vscode.WebviewViewProvider {
996996
}
997997
}
998998

999-
const enhancedPrompt = await enhancePrompt(
999+
const enhancedPrompt = await singleCompletionHandler(
10001000
configToUse,
1001-
message.text,
1002-
supportPrompt.get(customPrompts, "ENHANCE"),
1001+
supportPrompt.create(
1002+
"ENHANCE",
1003+
{
1004+
userInput: message.text,
1005+
},
1006+
customPrompts,
1007+
),
10031008
)
1009+
10041010
await this.postMessageToWebview({
10051011
type: "enhancedPrompt",
10061012
text: enhancedPrompt,

src/shared/__tests__/support-prompts.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@ describe("Code Action Prompts", () => {
7777
describe("ENHANCE action", () => {
7878
it("should format enhance prompt correctly", () => {
7979
const prompt = supportPrompt.create("ENHANCE", {
80-
filePath: testFilePath,
81-
selectedText: testCode,
80+
userInput: "test",
8281
})
8382

8483
expect(prompt).toBe(
85-
"Generate an enhanced version of this prompt (reply with only the enhanced prompt - no conversation, explanations, lead-in, bullet points, placeholders, or surrounding quotes):",
84+
"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",
8685
)
8786
// Verify it ignores parameters since ENHANCE template doesn't use any
8887
expect(prompt).not.toContain(testFilePath)

src/shared/support-prompt.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ Please suggest improvements for:
6868
Provide the improved code along with explanations for each enhancement.
6969
`
7070

71-
const ENHANCE_TEMPLATE =
72-
"Generate an enhanced version of this prompt (reply with only the enhanced prompt - no conversation, explanations, lead-in, bullet points, placeholders, or surrounding quotes):"
71+
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):
72+
73+
\${userInput}`
7374

7475
// Get template based on prompt type
7576
const defaultTemplates = {

src/utils/__tests__/enhance-prompt.test.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { enhancePrompt } from "../enhance-prompt"
1+
import { singleCompletionHandler } from "../single-completion-handler"
22
import { ApiConfiguration } from "../../shared/api"
33
import { buildApiHandler, SingleCompletionHandler } from "../../api"
4-
import { defaultPrompts } from "../../shared/modes"
4+
import { supportPrompt } from "../../shared/support-prompt"
55

66
// Mock the API handler
77
jest.mock("../../api", () => ({
@@ -34,29 +34,41 @@ describe("enhancePrompt", () => {
3434
})
3535

3636
it("enhances prompt using default enhancement prompt when no custom prompt provided", async () => {
37-
const result = await enhancePrompt(mockApiConfig, "Test prompt")
37+
const result = await singleCompletionHandler(mockApiConfig, "Test prompt")
3838

3939
expect(result).toBe("Enhanced prompt")
4040
const handler = buildApiHandler(mockApiConfig)
41-
expect((handler as any).completePrompt).toHaveBeenCalledWith(`${defaultPrompts.enhance}\n\nTest prompt`)
41+
expect((handler as any).completePrompt).toHaveBeenCalledWith(`Test prompt`)
4242
})
4343

4444
it("enhances prompt using custom enhancement prompt when provided", async () => {
4545
const customEnhancePrompt = "You are a custom prompt enhancer"
46-
47-
const result = await enhancePrompt(mockApiConfig, "Test prompt", customEnhancePrompt)
46+
const customEnhancePromptWithTemplate = customEnhancePrompt + "\n\n${userInput}"
47+
48+
const result = await singleCompletionHandler(
49+
mockApiConfig,
50+
supportPrompt.create(
51+
"ENHANCE",
52+
{
53+
userInput: "Test prompt",
54+
},
55+
{
56+
ENHANCE: customEnhancePromptWithTemplate,
57+
},
58+
),
59+
)
4860

4961
expect(result).toBe("Enhanced prompt")
5062
const handler = buildApiHandler(mockApiConfig)
5163
expect((handler as any).completePrompt).toHaveBeenCalledWith(`${customEnhancePrompt}\n\nTest prompt`)
5264
})
5365

5466
it("throws error for empty prompt input", async () => {
55-
await expect(enhancePrompt(mockApiConfig, "")).rejects.toThrow("No prompt text provided")
67+
await expect(singleCompletionHandler(mockApiConfig, "")).rejects.toThrow("No prompt text provided")
5668
})
5769

5870
it("throws error for missing API configuration", async () => {
59-
await expect(enhancePrompt({} as ApiConfiguration, "Test prompt")).rejects.toThrow(
71+
await expect(singleCompletionHandler({} as ApiConfiguration, "Test prompt")).rejects.toThrow(
6072
"No valid API configuration provided",
6173
)
6274
})
@@ -75,7 +87,7 @@ describe("enhancePrompt", () => {
7587
}),
7688
})
7789

78-
await expect(enhancePrompt(mockApiConfig, "Test prompt")).rejects.toThrow(
90+
await expect(singleCompletionHandler(mockApiConfig, "Test prompt")).rejects.toThrow(
7991
"The selected API provider does not support prompt enhancement",
8092
)
8193
})
@@ -101,7 +113,7 @@ describe("enhancePrompt", () => {
101113
}),
102114
} as unknown as SingleCompletionHandler)
103115

104-
const result = await enhancePrompt(openRouterConfig, "Test prompt")
116+
const result = await singleCompletionHandler(openRouterConfig, "Test prompt")
105117

106118
expect(buildApiHandler).toHaveBeenCalledWith(openRouterConfig)
107119
expect(result).toBe("Enhanced prompt")
@@ -121,6 +133,6 @@ describe("enhancePrompt", () => {
121133
}),
122134
} as unknown as SingleCompletionHandler)
123135

124-
await expect(enhancePrompt(mockApiConfig, "Test prompt")).rejects.toThrow("API Error")
136+
await expect(singleCompletionHandler(mockApiConfig, "Test prompt")).rejects.toThrow("API Error")
125137
})
126138
})
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { ApiConfiguration } from "../shared/api"
22
import { buildApiHandler, SingleCompletionHandler } from "../api"
3-
import { defaultPrompts } from "../shared/modes"
43

54
/**
65
* Enhances a prompt using the configured API without creating a full Cline instance or task history.
76
* This is a lightweight alternative that only uses the API's completion functionality.
87
*/
9-
export async function enhancePrompt(
10-
apiConfiguration: ApiConfiguration,
11-
promptText: string,
12-
enhancePrompt?: string,
13-
): Promise<string> {
8+
export async function singleCompletionHandler(apiConfiguration: ApiConfiguration, promptText: string): Promise<string> {
149
if (!promptText) {
1510
throw new Error("No prompt text provided")
1611
}
@@ -25,7 +20,5 @@ export async function enhancePrompt(
2520
throw new Error("The selected API provider does not support prompt enhancement")
2621
}
2722

28-
const enhancePromptText = enhancePrompt ?? defaultPrompts.enhance
29-
const prompt = `${enhancePromptText}\n\n${promptText}`
30-
return (handler as SingleCompletionHandler).completePrompt(prompt)
23+
return (handler as SingleCompletionHandler).completePrompt(promptText)
3124
}

0 commit comments

Comments
 (0)