Skip to content

Commit e53bee2

Browse files
committed
fix: move promptContent
1 parent 993ba91 commit e53bee2

File tree

4 files changed

+121
-154
lines changed

4 files changed

+121
-154
lines changed

src/services/codeCompletion/CodeCompletionService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
22

33
import type {DiscardReason} from '../../types/api/codeAssistant';
4-
import {getPromptFileContent} from '../../utils/monaco/codeAssistTelemetry';
54

5+
import {getPromptFileContent} from './promptContent';
66
import type {
77
EnrichedCompletion,
88
ICodeCompletionAPI,

src/services/codeCompletion/README.md

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ The code completion system consists of several key components working together:
2121
- Records acceptance, decline, and ignore events
2222
- Captures timing and interaction data
2323

24-
3. **Factory** (`factory.ts`)
24+
3. **PromptContent** (`promptContent.ts`)
25+
26+
- Handles extraction of code context for suggestions
27+
- Manages text fragments before and after cursor
28+
- Implements text length limits and cursor positioning
29+
- Creates structured prompt data for API requests
30+
31+
4. **Factory** (`factory.ts`)
2532

2633
- Creates and configures the completion provider
2734
- Wires together the completion and telemetry services
2835

29-
4. **Command Registration** (`registerCommands.ts`)
36+
5. **Command Registration** (`registerCommands.ts`)
3037

3138
- Registers Monaco editor commands for completion actions
3239
- Handles accept/decline completion commands
3340

34-
5. **CodeAssistantTelemetry** (`CodeAssistantTelemetry.tsx`)
41+
6. **CodeAssistantTelemetry** (`CodeAssistantTelemetry.tsx`)
3542
- Optional context caching mechanism
3643
- Tracks query history as virtual "open tabs"
3744
- Sends historical queries to the backend for context-aware suggestions
@@ -60,48 +67,62 @@ The code completion system consists of several key components working together:
6067
-> sendOpenTabs -> Backend Context Cache
6168
```
6269

63-
2. **Suggestion Generation**
70+
2. **Prompt Generation**
71+
72+
```
73+
Editor Change -> PromptContent.getPromptFileContent
74+
-> Text Fragments with Cursor Position -> API Request
75+
```
76+
77+
3. **Suggestion Generation**
6478

6579
```
66-
Editor Change -> CodeCompletionService.provideInlineCompletions
80+
Prompt Data -> CodeCompletionService.provideInlineCompletions
6781
-> API Request (with cached context) -> Suggestions Returned -> Monaco Display
6882
```
6983

70-
3. **Suggestion Acceptance**
84+
4. **Suggestion Acceptance**
7185

7286
```
7387
User Accept -> handleAccept -> TelemetryService.sendAcceptTelemetry
7488
-> API Telemetry Event
7589
```
7690

77-
4. **Suggestion Rejection**
91+
5. **Suggestion Rejection**
7892
```
7993
User Decline -> commandDiscard -> TelemetryService.sendDeclineTelemetry
8094
-> API Telemetry Event
8195
```
8296

8397
## Key Features
8498

85-
1. **Context Caching**
99+
1. **Prompt Generation**
100+
101+
- Extracts relevant code context around cursor
102+
- Implements smart text truncation (8000 chars before, 1000 after cursor)
103+
- Maintains cursor position information
104+
- Creates structured prompt format for API
105+
106+
2. **Context Caching**
86107

87108
- Maintains history of queries as virtual open tabs
88109
- Each query is assigned a unique ID and treated as a `.yql` file
89110
- Provides historical context to improve suggestion relevance
90111
- Optional feature that can be enabled/disabled
91112

92-
2. **Suggestion Caching**
113+
3. **Suggestion Caching**
93114

94115
- Caches suggestions to reduce API calls
95116
- Tracks suggestion display count
96117
- Manages suggestion lifecycle
97118

98-
3. **Telemetry**
119+
4. **Telemetry**
99120

100121
- Tracks suggestion acceptance rate
101122
- Records user interaction patterns
102123
- Monitors suggestion quality
103124

104-
4. **Command Integration**
125+
5. **Command Integration**
105126
- Keyboard shortcuts for completion actions
106127
- Context menu integration
107128
- Custom commands for completion workflow
@@ -117,6 +138,22 @@ interface ICodeCompletionAPI {
117138
}
118139
```
119140

141+
## Prompt Format
142+
143+
The prompt generation creates structured data in this format:
144+
145+
```typescript
146+
interface PromptFile {
147+
Fragments: {
148+
Text: string;
149+
Start: {Ln: number; Col: number};
150+
End: {Ln: number; Col: number};
151+
}[];
152+
Cursor: {Ln: number; Col: number};
153+
Path: string;
154+
}
155+
```
156+
120157
## Context Caching Format
121158

122159
When using the context caching feature, queries are sent in this format:
@@ -164,25 +201,31 @@ registerCompletionCommands(editor, monaco, completionService);
164201

165202
## Best Practices
166203

167-
1. **Context Management**
204+
1. **Prompt Generation**
205+
206+
- Consider text limits for optimal performance
207+
- Maintain cursor position accuracy
208+
- Handle edge cases in text extraction
209+
210+
2. **Context Management**
168211

169212
- Consider enabling context caching for better suggestions
170213
- Monitor the size of historical context
171214
- Clean up old context when no longer relevant
172215

173-
2. **Error Handling**
216+
3. **Error Handling**
174217

175218
- All API calls should be wrapped in try-catch blocks
176219
- Failed suggestions should not break the editor
177220
- Telemetry failures should be logged but not impact user experience
178221

179-
3. **Performance**
222+
4. **Performance**
180223

181224
- Suggestions are throttled to prevent excessive API calls
182225
- Caching reduces server load
183226
- Completion provider is disposed when not needed
184227

185-
4. **User Experience**
228+
5. **User Experience**
186229
- Suggestions appear inline with minimal delay
187230
- Clear feedback for acceptance/rejection
188231
- Non-intrusive telemetry collection
@@ -197,6 +240,7 @@ When modifying the code completion system:
197240
4. Consider performance implications of changes
198241
5. Document new features or changes
199242
6. Consider impact on context caching when modifying query handling
243+
7. Follow text limit guidelines in prompt generation
200244

201245
## Related Components
202246

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type Monaco from 'monaco-editor';
2+
import {v4} from 'uuid';
3+
4+
import type {PromptFile} from '../../types/api/codeAssistant';
5+
6+
const limitBeforeCursor = 8_000;
7+
const limitAfterCursor = 1_000;
8+
9+
const sessionId = v4();
10+
11+
export function getPromptFileContent(
12+
model: Monaco.editor.ITextModel,
13+
position: Monaco.Position,
14+
): PromptFile[] | undefined {
15+
const linesContent = model.getLinesContent();
16+
const prevTextInCurrentLine = linesContent[position.lineNumber - 1].slice(
17+
0,
18+
position.column - 1,
19+
);
20+
const postTextInCurrentLine = linesContent[position.lineNumber - 1].slice(position.column - 1);
21+
const prevText = linesContent
22+
.slice(0, position.lineNumber - 1)
23+
.concat([prevTextInCurrentLine])
24+
.join('\n');
25+
const postText = [postTextInCurrentLine]
26+
.concat(linesContent.slice(position.lineNumber))
27+
.join('\n');
28+
const cursorPostion = {Ln: position.lineNumber, Col: position.column};
29+
30+
const fragments = [];
31+
if (prevText) {
32+
fragments.push({
33+
Text:
34+
prevText.length > limitBeforeCursor
35+
? prevText.slice(prevText.length - limitBeforeCursor)
36+
: prevText,
37+
Start: {Ln: 1, Col: 1},
38+
End: cursorPostion,
39+
});
40+
}
41+
if (postText) {
42+
fragments.push({
43+
Text: postText.slice(0, limitAfterCursor),
44+
Start: cursorPostion,
45+
End: {
46+
Ln: linesContent.length,
47+
Col: linesContent[linesContent.length - 1].length,
48+
},
49+
});
50+
}
51+
52+
return fragments.length
53+
? [
54+
{
55+
Fragments: fragments,
56+
Cursor: cursorPostion,
57+
Path: `${sessionId}/query.yql`,
58+
},
59+
]
60+
: undefined;
61+
}

src/utils/monaco/codeAssistTelemetry.ts

Lines changed: 0 additions & 138 deletions
This file was deleted.

0 commit comments

Comments
 (0)