Skip to content

Commit 910a1ea

Browse files
authored
Merge pull request #2847 from Kilo-Org/mark/remove-relevant-context
Simpler structure for prompt creation in strategies
2 parents 80f0f3a + 9443f72 commit 910a1ea

File tree

8 files changed

+43
-142
lines changed

8 files changed

+43
-142
lines changed

src/services/ghost/strategies/AutoTriggerStrategy.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,23 @@ export class AutoTriggerStrategy extends BasePromptStrategy {
2121
return !!context.document
2222
}
2323

24-
/**
25-
* Minimal context for auto-trigger
26-
* Focus on immediate context only
27-
*/
28-
getRelevantContext(context: GhostSuggestionContext): Partial<GhostSuggestionContext> {
29-
return {
30-
document: context.document,
31-
range: context.range,
32-
recentOperations: context.recentOperations?.slice(0, 3), // Only last 3 operations
33-
// Exclude:
34-
// - userInput (no explicit request)
35-
// - diagnostics (not fixing errors)
36-
// - openFiles (reduces tokens)
37-
}
38-
}
39-
4024
/**
4125
* System instructions for auto-trigger
4226
*/
43-
protected getSpecificSystemInstructions(): string {
44-
return `Task: Subtle Auto-Completion
27+
getSystemInstructions(): string {
28+
return (
29+
this.getBaseSystemInstructions() +
30+
`Task: Subtle Auto-Completion
4531
Provide non-intrusive completions after a typing pause. Be conservative and helpful.
4632
4733
`
34+
)
4835
}
4936

5037
/**
5138
* Build minimal prompt for auto-trigger
5239
*/
53-
protected buildUserPrompt(context: Partial<GhostSuggestionContext>): string {
40+
getUserPrompt(context: GhostSuggestionContext): string {
5441
let prompt = ""
5542

5643
// Start with recent typing context

src/services/ghost/strategies/BasePromptStrategy.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@ export abstract class BasePromptStrategy implements PromptStrategy {
2424
*/
2525
abstract canHandle(context: GhostSuggestionContext): boolean
2626

27-
/**
28-
* Filters the context to only include relevant fields for this strategy
29-
*/
30-
abstract getRelevantContext(context: GhostSuggestionContext): Partial<GhostSuggestionContext>
31-
32-
/**
33-
* Generates system instructions for the AI model
34-
*/
35-
getSystemInstructions(customInstructions?: string): string {
36-
const baseInstructions = this.getBaseSystemInstructions()
37-
const specificInstructions = this.getSpecificSystemInstructions()
38-
39-
return `${baseInstructions}\n\n---\n\n${specificInstructions}${
40-
customInstructions ? `\n\n---\n\n${customInstructions}` : ""
41-
}`
42-
}
43-
4427
/**
4528
* Gets the base system instructions that apply to all strategies
4629
*/
@@ -78,28 +61,23 @@ EXAMPLE:
7861
// old code
7962
}]]></search><replace><![CDATA[function example() {
8063
// new code
81-
}]]></replace></change>`
64+
}]]></replace></change>
65+
66+
--
67+
68+
`
8269
}
8370

8471
/**
8572
* Gets strategy-specific system instructions
8673
* Must be implemented by each strategy
8774
*/
88-
protected abstract getSpecificSystemInstructions(): string
75+
abstract getSystemInstructions(): string
8976

9077
/**
9178
* Generates the user prompt with context
9279
*/
93-
getUserPrompt(context: GhostSuggestionContext): string {
94-
const relevantContext = this.getRelevantContext(context)
95-
return this.buildUserPrompt(relevantContext)
96-
}
97-
98-
/**
99-
* Builds the user prompt from the relevant context
100-
* Must be implemented by each strategy
101-
*/
102-
protected abstract buildUserPrompt(context: Partial<GhostSuggestionContext>): string
80+
abstract getUserPrompt(context: GhostSuggestionContext): string
10381

10482
/**
10583
* Adds the cursor marker to the document text at the specified position

src/services/ghost/strategies/CommentDrivenStrategy.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,10 @@ export class CommentDrivenStrategy extends BasePromptStrategy {
3030
return isComment && !context.userInput // User input takes precedence
3131
}
3232

33-
getRelevantContext(context: GhostSuggestionContext): Partial<GhostSuggestionContext> {
34-
return {
35-
document: context.document,
36-
range: context.range,
37-
recentOperations: context.recentOperations,
38-
}
39-
}
40-
41-
protected getSpecificSystemInstructions(): string {
42-
return `You are an expert code generation assistant that implements code based on comments.
33+
getSystemInstructions(): string {
34+
return (
35+
this.getBaseSystemInstructions() +
36+
`You are an expert code generation assistant that implements code based on comments.
4337
4438
## Core Responsibilities:
4539
1. Read and understand the comment's intent
@@ -71,9 +65,10 @@ export class CommentDrivenStrategy extends BasePromptStrategy {
7165
- Do not add explanatory comments unless necessary for complex logic
7266
- Ensure the code is production-ready
7367
- When using search/replace format, include ALL existing code to preserve it`
68+
)
7469
}
7570

76-
protected buildUserPrompt(context: Partial<GhostSuggestionContext>): string {
71+
getUserPrompt(context: GhostSuggestionContext): string {
7772
if (!context.document || !context.range) {
7873
return "No context available for comment-driven generation."
7974
}

src/services/ghost/strategies/InlineCompletionStrategy.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,10 @@ export class InlineCompletionStrategy extends BasePromptStrategy {
2525
return hasContentBefore && !context.userInput && !context.range.isEmpty === false && isNotAtEnd
2626
}
2727

28-
getRelevantContext(context: GhostSuggestionContext): Partial<GhostSuggestionContext> {
29-
return {
30-
document: context.document,
31-
range: context.range,
32-
}
33-
}
34-
35-
protected getSpecificSystemInstructions(): string {
36-
return `You are an expert code completion assistant specializing in inline completions.
28+
getSystemInstructions(): string {
29+
return (
30+
this.getBaseSystemInstructions() +
31+
`You are an expert code completion assistant specializing in inline completions.
3732
3833
## Core Responsibilities:
3934
1. Complete partial statements and expressions
@@ -70,9 +65,10 @@ export class InlineCompletionStrategy extends BasePromptStrategy {
7065
- Complete just enough to finish the current expression
7166
- Ensure syntactic correctness
7267
- Match the existing code style`
68+
)
7369
}
7470

75-
protected buildUserPrompt(context: Partial<GhostSuggestionContext>): string {
71+
getUserPrompt(context: GhostSuggestionContext): string {
7672
if (!context.document || !context.range) {
7773
return "No context available for inline completion."
7874
}

src/services/ghost/strategies/NewLineCompletionStrategy.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,13 @@ export class NewLineCompletionStrategy extends BasePromptStrategy {
2222
return line.trim() === ""
2323
}
2424

25-
/**
26-
* Focus on surrounding code and recent actions
27-
* Exclude diagnostics and user input
28-
*/
29-
getRelevantContext(context: GhostSuggestionContext): Partial<GhostSuggestionContext> {
30-
return {
31-
document: context.document,
32-
range: context.range,
33-
recentOperations: context.recentOperations,
34-
// Exclude:
35-
// - userInput (no explicit request)
36-
// - diagnostics (not relevant for new line)
37-
// - openFiles (reduces tokens)
38-
}
39-
}
40-
4125
/**
4226
* System instructions for new line completion
4327
*/
44-
protected getSpecificSystemInstructions(): string {
45-
return `Task: Proactive Code Completion for New Lines
28+
getSystemInstructions(): string {
29+
return (
30+
this.getBaseSystemInstructions() +
31+
`Task: Proactive Code Completion for New Lines
4632
The user has created a new line. Suggest the most logical next code based on context.
4733
4834
Completion Guidelines:
@@ -75,12 +61,13 @@ Important:
7561
- Respect existing code patterns and comments
7662
- Maintain consistent style
7763
- Consider the most likely next step`
64+
)
7865
}
7966

8067
/**
8168
* Build prompt focused on surrounding context
8269
*/
83-
protected buildUserPrompt(context: Partial<GhostSuggestionContext>): string {
70+
getUserPrompt(context: GhostSuggestionContext): string {
8471
let prompt = ""
8572

8673
// Start with cursor context

src/services/ghost/strategies/SelectionRefactorStrategy.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@ export class SelectionRefactorStrategy extends BasePromptStrategy {
1717
return !!(context.range && !context.range.isEmpty && !context.userInput)
1818
}
1919

20-
getRelevantContext(context: GhostSuggestionContext): Partial<GhostSuggestionContext> {
21-
return {
22-
document: context.document,
23-
range: context.range,
24-
diagnostics: context.diagnostics,
25-
}
26-
}
27-
28-
protected getSpecificSystemInstructions(): string {
29-
return `You are an expert code refactoring assistant. Your task is to improve selected code while maintaining its functionality.
20+
getSystemInstructions(): string {
21+
return (
22+
this.getBaseSystemInstructions() +
23+
`You are an expert code refactoring assistant. Your task is to improve selected code while maintaining its functionality.
3024
3125
## Core Responsibilities:
3226
1. Analyze the selected code for improvement opportunities
@@ -52,9 +46,10 @@ export class SelectionRefactorStrategy extends BasePromptStrategy {
5246
- Do not include explanations outside the code
5347
5448
Remember: The goal is to improve the code quality while keeping the exact same behavior.`
49+
)
5550
}
5651

57-
protected buildUserPrompt(context: Partial<GhostSuggestionContext>): string {
52+
getUserPrompt(context: GhostSuggestionContext): string {
5853
if (!context.document || !context.range) {
5954
return "No selection available for refactoring."
6055
}

src/services/ghost/strategies/UserRequestStrategy.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,13 @@ export class UserRequestStrategy extends BasePromptStrategy {
1919
return !!context.userInput && context.userInput.trim().length > 0
2020
}
2121

22-
/**
23-
* Include user input, document, selection, and diagnostics
24-
* Exclude recent operations and open files as they're less relevant
25-
*/
26-
getRelevantContext(context: GhostSuggestionContext): Partial<GhostSuggestionContext> {
27-
return {
28-
document: context.document,
29-
userInput: context.userInput,
30-
range: context.range,
31-
diagnostics: context.diagnostics,
32-
// Explicitly exclude:
33-
// - recentOperations (not needed for explicit requests)
34-
// - openFiles (reduces token usage)
35-
}
36-
}
37-
3822
/**
3923
* System instructions specific to user requests
4024
*/
41-
protected getSpecificSystemInstructions(): string {
42-
return `Task: Execute User's Explicit Request
25+
getSystemInstructions(): string {
26+
return (
27+
this.getBaseSystemInstructions() +
28+
`Task: Execute User's Explicit Request
4329
You are responding to a direct user instruction. Your primary goal is to fulfill their specific request accurately.
4430
4531
Priority Order:
@@ -64,12 +50,13 @@ Common Request Patterns:
6450
- "add comments" → add JSDoc or inline comments
6551
- "extract function" → move selected code to a new function
6652
- "fix" → resolve errors, warnings, or obvious issues`
53+
)
6754
}
6855

6956
/**
7057
* Build the user prompt with all relevant context
7158
*/
72-
protected buildUserPrompt(context: Partial<GhostSuggestionContext>): string {
59+
getUserPrompt(context: GhostSuggestionContext): string {
7360
let prompt = ""
7461

7562
// User request is the most important part
@@ -139,23 +126,6 @@ Common Request Patterns:
139126
return prompt
140127
}
141128

142-
/**
143-
* Override to provide more context for certain types of requests
144-
*/
145-
override getUserPrompt(context: GhostSuggestionContext): string {
146-
// For certain requests, we might want to include more context
147-
const request = context.userInput?.toLowerCase() || ""
148-
149-
// If the request mentions other files or imports, include more context
150-
if (request.includes("import") || request.includes("from")) {
151-
// Could potentially include information about available modules
152-
// For now, we'll use the standard approach
153-
}
154-
155-
// Use the standard prompt building
156-
return super.getUserPrompt(context)
157-
}
158-
159129
/**
160130
* Gets the file path from the document
161131
*/

src/services/ghost/types/PromptStrategy.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ export interface PromptStrategy {
3434
*/
3535
canHandle(context: GhostSuggestionContext): boolean
3636

37-
/**
38-
* Filters the context to only include relevant fields for this strategy
39-
* @param context The full suggestion context
40-
* @returns Partial context with only relevant fields
41-
*/
42-
getRelevantContext(context: GhostSuggestionContext): Partial<GhostSuggestionContext>
43-
4437
/**
4538
* Generates system instructions for the AI model
4639
* @param customInstructions Optional custom instructions to append

0 commit comments

Comments
 (0)