Skip to content

Commit 558d0ff

Browse files
authored
Merge pull request #2843 from Kilo-Org/mark/generate-prompts-once
Generate autocomplete prompts once
2 parents 34528de + c53382b commit 558d0ff

File tree

6 files changed

+42
-70
lines changed

6 files changed

+42
-70
lines changed

src/services/ghost/GhostProvider.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ export class GhostProvider {
253253
this.isRequestCancelled = false
254254

255255
const context = await this.ghostContext.generate(initialContext)
256-
const systemPrompt = this.strategy.getSystemPrompt(context)
257-
const userPrompt = this.strategy.getSuggestionPrompt(context)
256+
const { systemPrompt, userPrompt } = this.strategy.getPrompts(context)
258257
if (this.isRequestCancelled) {
259258
return
260259
}

src/services/ghost/GhostStrategy.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,16 @@ export class GhostStrategy {
1414
}
1515

1616
/**
17-
* Get the system prompt based on context using the new strategy system
18-
* Overloaded to support both new context-based and legacy string-only calls
19-
*/
20-
getSystemPrompt(context: GhostSuggestionContext): string {
21-
const { systemPrompt, strategy } = this.strategyManager.buildPrompt(context)
22-
if (this.debug) {
23-
console.log(`[GhostStrategy] Using strategy: ${strategy.name}`)
24-
}
25-
return systemPrompt
26-
}
27-
28-
/**
29-
* Get the user prompt based on context using the new strategy system
17+
* Get both system and user prompts based on context
3018
* @param context The suggestion context
31-
* @returns The user prompt
19+
* @returns Object containing systemPrompt and userPrompt
3220
*/
33-
getSuggestionPrompt(context: GhostSuggestionContext): string {
34-
const { userPrompt, strategy } = this.strategyManager.buildPrompt(context)
35-
21+
getPrompts(context: GhostSuggestionContext): { systemPrompt: string; userPrompt: string } {
22+
const { systemPrompt, userPrompt, strategy } = this.strategyManager.buildPrompt(context)
3623
if (this.debug) {
37-
console.log(`[GhostStrategy] Generated prompt with strategy: ${strategy.name}`)
24+
console.log(`[GhostStrategy] Using strategy: ${strategy.name}`)
3825
}
39-
40-
return userPrompt
26+
return { systemPrompt, userPrompt }
4127
}
4228

4329
/**

src/services/ghost/__tests__/GhostModelPerformance.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ describe("GhostModelPerformance", () => {
2525
document: document,
2626
}
2727

28-
const systemPrompt = strategy.getSystemPrompt(context)
29-
const suggestionPrompt = strategy.getSuggestionPrompt(context)
28+
const { systemPrompt, userPrompt } = strategy.getPrompts(context)
3029

31-
return { systemPrompt, suggestionPrompt }
30+
return { systemPrompt, suggestionPrompt: userPrompt }
3231
}
3332

3433
const performTest = async (apiHandler: ApiHandler, prompt: { systemPrompt: string; suggestionPrompt: string }) => {

src/services/ghost/__tests__/GhostRecentOperations.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ describe("GhostRecentOperations", () => {
116116
expect(enrichedContext.recentOperations?.length).toBeGreaterThan(0)
117117

118118
// Generate prompt
119-
const prompt = strategy.getSuggestionPrompt(enrichedContext)
119+
const { userPrompt } = strategy.getPrompts(enrichedContext)
120120

121121
// Verify that the prompt includes the recent operations section
122122
// The new strategy system uses "## Recent Typing" format
123-
expect(prompt).toContain("## Recent Typing")
123+
expect(userPrompt).toContain("## Recent Typing")
124124
})
125125

126126
it("should not include recent operations in the prompt when not available", async () => {
@@ -133,11 +133,11 @@ describe("GhostRecentOperations", () => {
133133
const enrichedContext = await context.generate(suggestionContext)
134134

135135
// Generate prompt
136-
const prompt = strategy.getSuggestionPrompt(enrichedContext)
136+
const { userPrompt } = strategy.getPrompts(enrichedContext)
137137

138138
// Verify that the prompt does not include recent operations section
139139
// The current document content will still be in the prompt, so we should only check
140140
// that the "**Recent Changes (Diff):**" section is not present
141-
expect(prompt.includes("**Recent Changes (Diff):**")).toBe(false)
141+
expect(userPrompt.includes("**Recent Changes (Diff):**")).toBe(false)
142142
})
143143
})

src/services/ghost/__tests__/GhostStrategy.spec.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,39 @@ describe("GhostStrategy", () => {
232232
})
233233

234234
describe("prompt generation", () => {
235-
it("should generate system prompt", () => {
235+
it("should generate both prompts efficiently with getPrompts", () => {
236+
const context: GhostSuggestionContext = {
237+
document: mockDocument,
238+
userInput: "Add a comment",
239+
range: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)),
240+
}
241+
242+
const { systemPrompt, userPrompt } = strategy.getPrompts(context)
243+
expect(systemPrompt).toContain("CRITICAL OUTPUT FORMAT")
244+
expect(systemPrompt).toContain("XML-formatted changes")
245+
expect(userPrompt).toContain("Add a comment")
246+
expect(userPrompt).toContain("<<<AUTOCOMPLETE_HERE>>>")
247+
})
248+
249+
it("should generate system prompt via getPrompts", () => {
236250
const context: GhostSuggestionContext = {
237251
document: mockDocument,
238252
}
239-
const systemPrompt = strategy.getSystemPrompt(context)
253+
const { systemPrompt } = strategy.getPrompts(context)
240254
expect(systemPrompt).toContain("CRITICAL OUTPUT FORMAT")
241255
expect(systemPrompt).toContain("XML-formatted changes")
242256
})
243257

244-
it("should generate suggestion prompt with context", () => {
258+
it("should generate suggestion prompt with context via getPrompts", () => {
245259
const context: GhostSuggestionContext = {
246260
document: mockDocument,
247261
userInput: "Add a comment",
248262
range: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)),
249263
}
250264

251-
const suggestionPrompt = strategy.getSuggestionPrompt(context)
252-
expect(suggestionPrompt).toContain("Add a comment")
253-
expect(suggestionPrompt).toContain("<<<AUTOCOMPLETE_HERE>>>")
265+
const { userPrompt } = strategy.getPrompts(context)
266+
expect(userPrompt).toContain("Add a comment")
267+
expect(userPrompt).toContain("<<<AUTOCOMPLETE_HERE>>>")
254268
})
255269
})
256270
})

src/services/ghost/__tests__/GhostStrategy.test.ts

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ describe("GhostStrategy", () => {
1313
strategy = new GhostStrategy()
1414
})
1515

16-
describe("getSystemPrompt", () => {
17-
it("should use PromptStrategyManager to generate system prompt", () => {
16+
describe("getPrompts", () => {
17+
it("should use PromptStrategyManager to generate prompts", () => {
1818
const mockDocument = {
1919
languageId: "typescript",
2020
getText: () => "const x = 1;",
@@ -27,10 +27,10 @@ describe("GhostStrategy", () => {
2727
userInput: "Complete this function",
2828
}
2929

30-
const prompt = strategy.getSystemPrompt(context)
30+
const { systemPrompt } = strategy.getPrompts(context)
3131

3232
// Should contain base instructions from strategy system
33-
expect(prompt).toContain("CRITICAL OUTPUT FORMAT")
33+
expect(systemPrompt).toContain("CRITICAL OUTPUT FORMAT")
3434
})
3535

3636
it("should select UserRequestStrategy when user input is provided", () => {
@@ -47,8 +47,7 @@ describe("GhostStrategy", () => {
4747
userInput: "Add a function to calculate sum",
4848
}
4949

50-
const systemPrompt = strategy.getSystemPrompt(context)
51-
const userPrompt = strategy.getSuggestionPrompt(context)
50+
const { systemPrompt, userPrompt } = strategy.getPrompts(context)
5251

5352
// UserRequestStrategy should be selected
5453
expect(systemPrompt).toContain("Execute User's Explicit Request")
@@ -80,35 +79,13 @@ describe("GhostStrategy", () => {
8079
],
8180
}
8281

83-
const systemPrompt = strategy.getSystemPrompt(context)
82+
const { systemPrompt } = strategy.getPrompts(context)
8483

8584
// ErrorFixStrategy should be selected
8685
expect(systemPrompt).toContain("Fix Compilation Errors and Warnings")
8786
})
8887
})
8988

90-
describe("getSuggestionPrompt", () => {
91-
it("should delegate to PromptStrategyManager", () => {
92-
const mockDocument = {
93-
languageId: "typescript",
94-
getText: () => "const x = 1;",
95-
lineAt: (line: number) => ({ text: "const x = 1;" }),
96-
uri: { toString: () => "file:///test.ts" },
97-
offsetAt: (position: vscode.Position) => 13,
98-
} as vscode.TextDocument
99-
100-
const context: GhostSuggestionContext = {
101-
document: mockDocument,
102-
}
103-
104-
const prompt = strategy.getSuggestionPrompt(context)
105-
106-
// Should return a structured prompt
107-
expect(prompt).toBeDefined()
108-
expect(prompt.length).toBeGreaterThan(0)
109-
})
110-
})
111-
11289
describe("Integration", () => {
11390
it("should work with both system and user prompts", () => {
11491
const mockDocument = {
@@ -124,8 +101,7 @@ describe("GhostStrategy", () => {
124101
userInput: "Complete this function",
125102
}
126103

127-
const systemPrompt = strategy.getSystemPrompt(context)
128-
const userPrompt = strategy.getSuggestionPrompt(context)
104+
const { systemPrompt, userPrompt } = strategy.getPrompts(context)
129105

130106
// System prompt should contain format instructions
131107
expect(systemPrompt).toContain("CRITICAL OUTPUT FORMAT")
@@ -220,8 +196,7 @@ describe("GhostStrategy", () => {
220196
document: mockDocument,
221197
}
222198

223-
const systemPrompt = strategy.getSystemPrompt(context)
224-
const userPrompt = strategy.getSuggestionPrompt(context)
199+
const { systemPrompt, userPrompt } = strategy.getPrompts(context)
225200

226201
expect(systemPrompt).toBeDefined()
227202
expect(systemPrompt.length).toBeGreaterThan(0)
@@ -279,8 +254,7 @@ describe("GhostStrategy", () => {
279254
]
280255

281256
contexts.forEach((context) => {
282-
const systemPrompt = strategy.getSystemPrompt(context as GhostSuggestionContext)
283-
const userPrompt = strategy.getSuggestionPrompt(context as GhostSuggestionContext)
257+
const { systemPrompt, userPrompt } = strategy.getPrompts(context as GhostSuggestionContext)
284258

285259
expect(systemPrompt).toBeDefined()
286260
expect(systemPrompt.length).toBeGreaterThan(0)

0 commit comments

Comments
 (0)